/// <summary> /// Init process. Sets configuration and triggers the connection process /// </summary> /// <returns> /// Returns IEnumerator so unity treats it as a Coroutine /// </returns> private IEnumerator Start() { if (sAddress == null) { //to avoid the awkward moment of connecting two random users who test this package //we use a randomized addresses now to connect only the local test Apps ;) sAddress = "OneToManyTest_" + Random.Range(0, 1000000); } if (UnityCallFactory.Instance == null) { Debug.LogError("No access to webrtc. "); } else { UnityCallFactory.Instance.RequestLogLevel(UnityCallFactory.LogLevel.Info); //Factory works. Prepare Peers NetworkConfig config = new NetworkConfig(); config.IceServers.Add(new IceServer(mStunServer)); config.SignalingUrl = mSignalingServer; mMediaNetwork = UnityCallFactory.Instance.CreateMediaNetwork(config); //keep track of multiple local instances for testing. mIndex = sInstances; sInstances++; Debug.Log("Instance " + mIndex + " created."); if (uSender) { //sender will broadcast audio and video mMediaConfig.Audio = true; mMediaConfig.Video = true; Debug.Log("Accepting incoming connections on " + sAddress); mMediaNetwork.Configure(mMediaConfig); mMediaNetwork.StartServer(sAddress); } else { //this one will just receive (but could also send if needed) mMediaConfig.Audio = false; mMediaConfig.Video = false; mMediaNetwork.Configure(mMediaConfig); } //wait a while before trying to connect othe sender //so it has time to register at the signaling server yield return(new WaitForSeconds(5)); if (uSender == false) { Debug.Log("Tring to connect to " + sAddress); mMediaNetwork.Connect(sAddress); } } }
private void UpdateReceiver() { //STEP3: Updating the receiver. Will be called ever frame //IMediaNetwork uses polling instead of events receiver.Update(); //check if the configuration state changed if (receiver.GetConfigurationState() == MediaConfigurationState.Failed) { //did configuration fail? error Debug.Log("receiver configuration failed " + receiver.GetConfigurationError()); receiver.ResetConfiguration(); } else if (receiver.GetConfigurationState() == MediaConfigurationState.Successful && mReceiverConfigured == false) { //configuration successful. mReceiverConfigured = true; //StartServer corresponds to ICall.Listen receiver.StartServer(address); } //Dequeue network events NetworkEvent evt; while (receiver.Dequeue(out evt)) { if (evt.Type == NetEventType.ServerInitialized) { //triggered if StartServer completed Debug.Log("receiver: server initialized."); //receiver is ready -> create sender SenderSetup(); } else if (evt.Type == NetEventType.ServerInitFailed) { //either network problem or address in use Debug.LogError("receiver: server init failed"); } else if (evt.Type == NetEventType.NewConnection) { //triggered if a new connection is established Debug.Log("receiver: New connection with id " + evt.ConnectionId); } } receiver.Flush(); }