Exemplo n.º 1
0
 public void ConnectToPeer()
 {
     Debug.WriteLine("Device Status: SelectedCamera: {0} - SelectedMic: {1}", SelectedCamera == null ? "NULL" : "OK", SelectedMicrophone == null ? "NULL" : "OK");
     if (SelectedPeer != null)
     {
         new Task(() => { Conductor.Instance.ConnectToPeer(SelectedPeer); }).Start();
     }
     else
     {
         OnStatusMessageUpdate?.Invoke("SelectedPeer not set");
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Updates signaling capacity <see cref="Signaller.UpdateCapacity(int)"/>
        /// </summary>
        /// <param name="newCapacity">the new remaining capacity</param>
        public async void UpdateCapacity(int newCapacity)
        {
            if (IsConnectedToPeer)
            {
                var result = await Conductor.Instance.Signaller.UpdateCapacity(newCapacity);

                if (result)
                {
                    OnStatusMessageUpdate?.Invoke($"Capacity Updated to '{newCapacity}'");
                }
                else
                {
                    OnStatusMessageUpdate?.Invoke($"Unable to update Capacity to '{newCapacity}'");
                }
            }
        }
Exemplo n.º 3
0
        private void Conductor_OnAddLocalStream(MediaStreamEvent evt)
        {
            if (evt == null)
            {
                var msg = "Conductor_OnAddLocalStream--media stream NULL";
                Debug.WriteLine(msg);
                OnStatusMessageUpdate?.Invoke(msg);
            }
            _selfVideoTrack = evt.Stream.GetVideoTracks().FirstOrDefault();
            //if ((_selfVideoTrack != null) && (SelectedCamera != null))
            if (_selfVideoTrack != null)
            {
                Debug.WriteLine("selfVideoTrack Setup-IsCameraEnabled:{0}-IsMicrophoneEnabled:{1}", IsCameraEnabled, IsMicrophoneEnabled);
                RunOnUiThread(() =>
                {
                    if (IsCameraEnabled)
                    {
                        Conductor.Instance.EnableLocalVideoStream();
                    }
                    else
                    {
                        Conductor.Instance.DisableLocalVideoStream();
                    }

                    if (IsMicrophoneEnabled)
                    {
                        Conductor.Instance.UnmuteMicrophone();
                    }
                    else
                    {
                        Conductor.Instance.MuteMicrophone();
                    }

// TODO: Handle Video Loopback
//                    if ((VideoLoopbackEnabled) && (SelfVideo != null))
//                    {
//                        var source = Media.CreateMedia().CreateMediaSource(_selfVideoTrack, "SELF");
//                        SelfVideo.SetMediaStreamSource(source);
//                    }
                });
            }
            else
            {
                Debug.WriteLine("selfVideoTrack NULL");
            }
        }
Exemplo n.º 4
0
        public void Initialize()
        {
            WebRTC.Initialize(_uiDispatcher);
            Conductor.Instance.ETWStatsEnabled = false;

            Cameras             = new ObservableCollection <MediaDevice>();
            Microphones         = new ObservableCollection <MediaDevice>();
            AudioPlayoutDevices = new ObservableCollection <MediaDevice>();

            // WebRTCUWP M58 library does not support audio capture/playout devices
            //foreach (MediaDevice audioCaptureDevice in Conductor.Instance.Media.GetAudioCaptureDevices())
            //{
            //    Microphones.Add(audioCaptureDevice);
            //}

            //foreach (MediaDevice audioPlayoutDevice in Conductor.Instance.Media.GetAudioPlayoutDevices())
            //{
            //    AudioPlayoutDevices.Add(audioPlayoutDevice);
            //}

            // HACK Remove Automatic Device Assignment
            if (SelectedCamera == null && Cameras.Count > 0)
            {
                SelectedCamera = Cameras.First();
            }

            if (SelectedMicrophone == null && Microphones.Count > 0)
            {
                SelectedMicrophone = Microphones.First();
            }

            Debug.WriteLine("Device Status: SelectedCamera: {0} - SelectedMic: {1}", SelectedCamera == null ? "NULL" : "OK", SelectedMicrophone == null ? "NULL" : "OK");
            if (SelectedAudioPlayoutDevice == null && AudioPlayoutDevices.Count > 0)
            {
                SelectedAudioPlayoutDevice = AudioPlayoutDevices.First();
            }

            Conductor.Instance.Media.OnMediaDevicesChanged += OnMediaDevicesChanged;
            Conductor.Instance.Signaller.OnPeerConnected   += (peerId, peerName) =>
            {
                RunOnUiThread(() =>
                {
                    if (Peers == null)
                    {
                        Peers = new ObservableCollection <Peer>();
                        Conductor.Instance.Peers = Peers;
                    }

                    Peers.Add(new Peer {
                        Id = peerId, Name = peerName
                    });
                });
            };

            Conductor.Instance.Signaller.OnPeerDisconnected += peerId =>
            {
                RunOnUiThread(() =>
                {
                    var peerToRemove = Peers?.FirstOrDefault(p => p.Id == peerId);
                    if (peerToRemove != null)
                    {
                        Peers.Remove(peerToRemove);
                    }
                });
            };

            Conductor.Instance.Signaller.OnSignedIn += () =>
            {
                RunOnUiThread(() =>
                {
                    IsConnected         = true;
                    IsMicrophoneEnabled = false;
                    IsCameraEnabled     = false;
                    IsConnecting        = false;

                    OnStatusMessageUpdate?.Invoke("Signed-In");
                });
            };

            Conductor.Instance.Signaller.OnServerConnectionFailure += (Exception ex) =>
            {
                RunOnUiThread(() =>
                {
                    IsConnecting = false;
                    OnStatusMessageUpdate?.Invoke("Server Connection Failure: " + ex.Message + "\n" + ex.StackTrace);
                });
            };

            Conductor.Instance.Signaller.OnDisconnected += () =>
            {
                RunOnUiThread(() =>
                {
                    IsConnected         = false;
                    IsMicrophoneEnabled = false;
                    IsCameraEnabled     = false;
                    IsDisconnecting     = false;
                    Peers?.Clear();
                    OnStatusMessageUpdate?.Invoke("Disconnected");
                });
            };

            Conductor.Instance.Signaller.OnMessageFromPeer += (id, message) =>
            {
                RunOnUiThread(() =>
                {
                    // TODO: Handles All Peer Messages (Signal Channel)
                });
            };

            Conductor.Instance.Signaller.OnPeerConnected += (id, name) =>
            {
                RunOnUiThread(() =>
                {
                    SelectedPeer = Peers.First(x => x.Id == id);
                    OnStatusMessageUpdate?.Invoke(string.Format("Connected Peer: {0}-{1}", SelectedPeer.Id, SelectedPeer.Name));
                });
            };

            // TODO: Restore Event Handler in Utility Wrapper
            // Implemented in Unity Consumer due to Event Handling Issue
            // Conductor.Instance.OnAddRemoteStream += Conductor_OnAddRemoteStream does not propagate

            Conductor.Instance.OnRemoveRemoteStream    += Conductor_OnRemoveRemoteStream;
            Conductor.Instance.OnAddLocalStream        += Conductor_OnAddLocalStream;
            Conductor.Instance.OnConnectionHealthStats += Conductor_OnPeerConnectionHealthStats;
            Conductor.Instance.OnPeerConnectionCreated += () =>
            {
                RunOnUiThread(() =>
                {
                    IsReadyToConnect    = false;
                    IsConnectedToPeer   = true;
                    IsReadyToDisconnect = false;
                    IsMicrophoneEnabled = false;
                    OnStatusMessageUpdate?.Invoke("Peer Connection Created");
                });
            };

            Conductor.Instance.OnPeerConnectionClosed += () =>
            {
                RunOnUiThread(() =>
                {
                    IsConnectedToPeer   = false;
                    _peerVideoTrack     = null;
                    _selfVideoTrack     = null;
                    IsMicrophoneEnabled = false;
                    IsCameraEnabled     = false;

                    // TODO: Clean-up References
                    //GC.Collect(); // Ensure all references are truly dropped.

                    OnStatusMessageUpdate?.Invoke("Peer Connection Closed");
                });
            };

            Conductor.Instance.OnPeerMessageDataReceived += (peerId, message) =>
            {
                OnPeerMessageDataReceived?.Invoke(peerId, message);
            };

            // DATA Channel Setup
            Conductor.Instance.OnPeerMessageDataReceived += (i, s) =>
            {
            };

            Conductor.Instance.OnReadyToConnect += () => { RunOnUiThread(() => { IsReadyToConnect = true; }); };

            IceServers   = new ObservableCollection <IceServer>();
            NewIceServer = new IceServer();
            AudioCodecs  = new ObservableCollection <CodecInfo>();
            var audioCodecList = WebRTC.GetAudioCodecs();

            string[] incompatibleAudioCodecs = new string[] { "CN32000", "CN16000", "CN8000", "red8000", "telephone-event8000" };
            VideoCodecs = new ObservableCollection <CodecInfo>();

            // TODO: REMOVE DISPLAY LIST SUPPORT
            var videoCodecList = WebRTC.GetVideoCodecs().OrderBy(codec =>
            {
                switch (codec.Name)
                {
                case "VP8": return(1);

                case "VP9": return(2);

                case "H264": return(3);

                default: return(99);
                }
            });

            RunOnUiThread(() =>
            {
                foreach (var audioCodec in audioCodecList)
                {
                    if (!incompatibleAudioCodecs.Contains(audioCodec.Name + audioCodec.ClockRate))
                    {
                        AudioCodecs.Add(audioCodec);
                    }
                }

                if (AudioCodecs.Count > 0)
                {
                    SelectedAudioCodec = AudioCodecs.FirstOrDefault(x => x.Name.Contains("PCMU"));
                }

                foreach (var videoCodec in videoCodecList)
                {
                    VideoCodecs.Add(videoCodec);
                }

                if (VideoCodecs.Count > 0)
                {
                    SelectedVideoCodec = VideoCodecs.FirstOrDefault(x => x.Name.Contains("H264"));
                }
            });

            RunOnUiThread(() =>
            {
                OnInitialized?.Invoke();
            });
        }
Exemplo n.º 5
0
        public void ConnectToServer(string peerName = "", int heartbeatMs = -1)
        {
            Task.Run(async() =>
            {
                IsConnecting = true;
                await LoadSettings().ConfigureAwait(false);
                Conductor.Instance.Signaller.SetHeartbeatMs(Convert.ToInt32(HeartBeat.Value));
                Conductor.Instance.ConfigureAuth(AuthCodeUri, AuthPollUri);
                Conductor.Instance.ConfigureTemporaryTurn(TempTurnUri);

                // we need to set the temporary turn creds into the data model if we retrive them
                if (Conductor.Instance.TurnClient != null)
                {
                    Conductor.Instance.TurnClient.CredentialsRetrieved += (TemporaryTurnClient.TurnCredentials eventData) =>
                    {
                        var statusMessage = "Temporary turn got status: " + eventData.http_status;

                        if (_iceServers.Count > 0)
                        {
                            // we currently only support one manually configured ice server, so this works
                            _iceServers[0].Username   = eventData.username;
                            _iceServers[0].Credential = eventData.password;

                            // then, we have to reconfigure the conductor iceServers
                            Conductor.Instance.ConfigureIceServers(_iceServers);

                            statusMessage += ", using " + eventData.username + ":" + eventData.password;
                        }

                        if (eventData.http_status == 200)
                        {
                            statusMessage += ", starting login";
                            Conductor.Instance.StartLogin(this.Uri.Value, peerName);
                        }

                        OnStatusMessageUpdate?.Invoke(statusMessage);
                    };
                }

                // note: we don't support just a turnclient, so if we don't have auth, we don't get turn
                if (Conductor.Instance.AuthClient != null)
                {
                    Conductor.Instance.AuthClient.CodeComplete += (OAuth24DClient.CodeCompletionData eventData) =>
                    {
                        if (eventData.http_status == 200)
                        {
                            // show the user data.device_code
                            // show the user data.verification_url
                            // direct the user to enter device_code @ verification_url in a browser
                            OnStatusMessageUpdate?.Invoke(string.Format("Visit {0} - Enter '{1}'\n", eventData.verification_url, eventData.user_code));
                        }
                    };

                    Conductor.Instance.AuthClient.AuthenticationComplete += (OAuth24DClient.AuthCompletionData eventData) =>
                    {
                        if (eventData.http_status == 200 && Conductor.Instance.TurnClient == null)
                        {
                            OnStatusMessageUpdate?.Invoke("Authentication recieved, logging in");

                            Conductor.Instance.StartLogin(this.Uri.Value, peerName);
                        }
                    };

                    Conductor.Instance.AuthClient.Authenticate();
                }
                else
                {
                    Conductor.Instance.StartLogin(this.Uri.Value, peerName);
                }
            });
        }
        public void Initialize()
        {
            // WebRTCライブラリの初期化
            // WebRTC.Initialize(_uiDispathcer);
            // Conductor.Instance.ETWStatsEnabled = false;

            /*
             * Cameras = new List<MediaDevice>();
             * Microphones = new List<MediaDevice>();
             * AudioPlayoutDevices = new List<MediaDevice>();
             * // マシン上で使用できるメディアデバイスをすべて取得する
             * foreach(var videoCaptureDevice in Conductor.Instance.Media.GetVideoCaptureDevices())
             * {
             *  Cameras.Add(videoCaptureDevice);
             * }
             * foreach(var audioCaptureDevice in Conductor.Instance.Media.GetAudioCaptureDevices())
             * {
             *  Microphones.Add(audioCaptureDevice);
             * }
             * foreach(var audioPlayoutDevice in Conductor.Instance.Media.GetAudioPlayoutDevices())
             * {
             *  AudioPlayoutDevices.Add(audioPlayoutDevice);
             * }
             */

            // 各種メディアデバイスはリストの先頭のものを使用する
            // Holoはいいけど、Immersiveの場合は考え直すべきです

            /*
             * if(SelectedCamera == null && Cameras.Count > 0)
             * {
             *  SelectedCamera = Cameras.First();
             * }
             *
             * if(SelectedMicrophone == null && Microphones.Count > 0)
             * {
             *  SelectedMicrophone = Microphones.First();
             * }
             *
             * if(SelectedAudioPlayoutDevice == null && AudioPlayoutDevices.Count >0)
             * {
             *  SelectedAudioPlayoutDevice = AudioPlayoutDevices.First();
             * }
             */

            // ================================
            // シグナリング関連のイベントハンドラ
            // ================================

            // マシンに接続されたメディアデバイスが変更されたときのイベントハンドラ
            // Conductor.Instance.Media.OnMediaDevicesChanged += OnMediaDeviceChanged;
            // リモートユーザがシグナリングサーバに接続してきたときのハンドラ
            // 自分の初回ログイン、ポーリング時の新規ユーザ追加時にコールされる
            // TODO 接続ユーザの選択方法を工夫したいところ
            Conductor.Instance.Signaller.OnPeerConnected += (peerId, peerName) =>
            {
                // リモートユーザのリストを行進する
                if (Peers == null)
                {
                    Peers = new List <Peer>();
                    Conductor.Instance.Peers = Peers;
                }
                Peers.Add(new Peer {
                    Id = peerId, Name = peerName
                });
                // 接続してきたリモートユーザをPeer候補とする
                SelectedPeer = Peers.First(x => x.Id == peerId);
            };
            // リモートユーザがシグナリングサーバからログアウトしたときのハンドラ
            Conductor.Instance.Signaller.OnPeerDisconnected += (peerId) =>
            {
                var peerToRemove = Peers?.FirstOrDefault(p => p.Id == peerId);
                if (peerToRemove != null)
                {
                    Peers.Remove(peerToRemove);
                }
            };
            // シグナリングサーバへの接続が完了したときのハンドラ
            Conductor.Instance.Signaller.OnSignedIn += () =>
            {
                IsConnected         = true;
                IsMicrophoneEnabled = false;
                IsCameraEnabled     = false;
                IsConnecting        = false;

                OnStatusMessageUpdate?.Invoke("Signed in");
            };
            // シグナリングサーバへの接続が失敗したときのハンドラ
            Conductor.Instance.Signaller.OnServerConnectionFailure += () =>
            {
                IsConnecting = false;

                OnStatusMessageUpdate?.Invoke("Server Connection Failure");
            };
            // シグナリングサーバからログアウトしたときのハンドラ
            Conductor.Instance.Signaller.OnDisconnected += () =>
            {
                IsConnected         = false;
                IsMicrophoneEnabled = false;
                IsCameraEnabled     = false;
                IsDisconnecting     = false;
                Peers?.Clear();

                OnStatusMessageUpdate?.Invoke("Disconnected");
            };
            //
            Conductor.Instance.OnReadyToConnect += () =>
            {
                IsReadyToConnect = true;
            };


            // =============================
            // Peerコネクション関連のイベントハンドラ
            // =============================

            // Peerコネクションが生成されたときのイベントハンドラ(通話開始)
            Conductor.Instance.OnPeerConnectionCreated += () =>
            {
                IsReadyToConnect    = false;
                IsConnectedToPeer   = true;
                IsReadyToDisconnect = false;

                IsCameraEnabled     = true;
                IsMicrophoneEnabled = true; // ??

                OnStatusMessageUpdate?.Invoke("Peer Connection Created");
            };
            // Peerコネクションが破棄されたときのイベントハンドラ
            Conductor.Instance.OnPeerConnectionClosed += () =>
            {
                IsConnectedToPeer   = false;
                _peerVideoTrack     = null;
                _selfVideoTrack     = null;
                IsMicrophoneEnabled = false;
                IsCameraEnabled     = false;
            };
            // Peer(リモートユーザ)からメッセージを受信したときのハンドラ
            Conductor.Instance.OnPeerMessageDataReceived += (peerId, message) =>
            {
                OnPeerMessageDataReceived?.Invoke(peerId, message);
            };

            // =============================
            // コーデック設定
            // =============================

            /*
             * // オーディオコーデックの設定
             * AudioCodecs = new List<CodecInfo>();
             * var audioCodecList = WebRTC.GetAudioCodecs();
             * string[] incompatibleAudioCodecs = new string[] { "CN32000", "CN16000", "CN8000", "red8000", "telephone-event8000" };
             *
             * foreach (var audioCodec in audioCodecList)
             * {
             *  if (!incompatibleAudioCodecs.Contains(audioCodec.Name + audioCodec.ClockRate))
             *  {
             *      AudioCodecs.Add(audioCodec);
             *  }
             * }
             * if (AudioCodecs.Count > 0)
             * {
             *  SelectedAudioCodec = AudioCodecs.First();
             * }
             *
             * // ビデオコーデックの設定。デフォルトはH.264を使う
             * VideoCodecs = new List<CodecInfo>();
             * var videoCodecList = WebRTC.GetVideoCodecs().OrderBy(codec =>
             * {
             *  switch (codec.Name)
             *  {
             *      case "VP8": return 1;
             *      case "VP9": return 2;
             *      case "H264": return 3;
             *      default: return 99;
             *  }
             * });
             *
             * foreach (var videoCodec in videoCodecList)
             * {
             *  VideoCodecs.Add(videoCodec);
             * }
             * if (VideoCodecs.Count > 0)
             * {
             *  SelectedVideoCodec = VideoCodecs.FirstOrDefault(codec => codec.Name.Contains("H264"));
             * }
             */
            /*
             * // =============================
             * // Iceサーバの設定
             * // =============================
             * IceServers = new List<IceServer>();
             * NewIceServer = new IceServer();
             *
             * IceServers.Add(new IceServer("stun.l.google.com:19302", IceServer.ServerType.STUN));
             * IceServers.Add(new IceServer("stun1.l.google.com:19302", IceServer.ServerType.STUN));
             * IceServers.Add(new IceServer("stun2.l.google.com:19302", IceServer.ServerType.STUN));
             * IceServers.Add(new IceServer("stun3.l.google.com:19302", IceServer.ServerType.STUN));
             * IceServers.Add(new IceServer("stun4.l.google.com:19302", IceServer.ServerType.STUN));
             *
             * Conductor.Instance.ConfigureIceServers(IceServers);
             */
            OnInitialized?.Invoke();
        }