/// <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 UpdateSender() { //STEP5: Sender update loop. Same as receiver but is calling Connect //once configure completed. sender.Update(); NetworkEvent evt; if (sender.GetConfigurationState() == MediaConfigurationState.Failed) { //did configuration fail? error Debug.Log("sender configuration failed " + sender.GetConfigurationError()); sender.ResetConfiguration(); } else if (sender.GetConfigurationState() == MediaConfigurationState.Successful && mSenderConfigured == false) { mSenderConfigured = true; //connecting to to the receiver sender.Connect(address); } while (sender.Dequeue(out evt)) { if (evt.Type == NetEventType.NewConnection) { Debug.Log("sender: New connection with id " + evt.ConnectionId); } else if (evt.Type == NetEventType.ConnectionFailed) { Debug.LogError("sender: connection failed"); } } sender.Flush(); }