示例#1
0
        /// <summary>
        /// Initialize everything for WebRTC connection
        /// [internal use]
        /// </summary>
        protected void Initialize()
        {
#if NETFX_CORE
            // A Peer is connected to the server event handler
            Conductor.Instance.Signaller.OnPeerConnected += (peerId, peerName) =>
            {
                var task = RunOnUiThread(() =>
                {
                    lock (this)
                    {
                        Conductor.Peer peer = new Conductor.Peer {
                            Id = peerId, Name = peerName
                        };
                        Conductor.Instance.AddPeer(peer);
                        commandQueue.Add(new Command {
                            type = CommandType.AddRemotePeer, remotePeer = peer
                        });
                    }
                });
            };

            // A Peer is disconnected from the server event handler
            Conductor.Instance.Signaller.OnPeerDisconnected += peerId =>
            {
                var task = RunOnUiThread(() =>
                {
                    lock (this)
                    {
                        var peerToRemove = Conductor.Instance.GetPeers().FirstOrDefault(p => p.Id == peerId);
                        if (peerToRemove != null)
                        {
                            Conductor.Peer peer = new Conductor.Peer {
                                Id = peerToRemove.Id, Name = peerToRemove.Name
                            };
                            Conductor.Instance.RemovePeer(peer);
                            commandQueue.Add(new Command {
                                type = CommandType.RemoveRemotePeer, remotePeer = peer
                            });
                        }
                    }
                });
            };

            // The user is Signed in to the server event handler
            Conductor.Instance.Signaller.OnSignedIn += () =>
            {
                var task = RunOnUiThread(() =>
                {
                    lock (this)
                    {
                        if (WebRTCStatus == WebRTCStatuses.Connecting)
                        {
                            WebRTCStatus = WebRTCStatuses.Connected;
                            commandQueue.Add(new Command {
                                type = CommandType.SetConnected
                            });
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("Signaller.OnSignedIn() - wrong status - " + WebRTCStatus);
                        }
                    }
                });
            };

            // Failed to connect to the server event handler
            Conductor.Instance.Signaller.OnServerConnectionFailure += () =>
            {
                var task = RunOnUiThread(() =>
                {
                    lock (this)
                    {
                        if (WebRTCStatus == WebRTCStatuses.Connecting)
                        {
                            WebRTCStatus = WebRTCStatuses.NotConnected;
                            commandQueue.Add(new Command {
                                type = CommandType.SetNotConnected
                            });
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("Signaller.OnServerConnectionFailure() - wrong status - " + WebRTCStatus);
                        }
                    }
                });
            };

            // The current user is disconnected from the server event handler
            Conductor.Instance.Signaller.OnDisconnected += () =>
            {
                var task = RunOnUiThread(() =>
                {
                    lock (this)
                    {
                        if (WebRTCStatus == WebRTCStatuses.Disconnecting)
                        {
                            WebRTCStatus = WebRTCStatuses.NotConnected;
                            commandQueue.Add(new Command {
                                type = CommandType.SetNotConnected
                            });
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("Signaller.OnDisconnected() - wrong status - " + WebRTCStatus);
                        }
                    }
                });
            };

            Conductor.Instance.OnAddRemoteStream    += Conductor_OnAddRemoteStream;
            Conductor.Instance.OnRemoveRemoteStream += Conductor_OnRemoveRemoteStream;
            Conductor.Instance.OnAddLocalStream     += Conductor_OnAddLocalStream;

            // Connected to a peer event handler
            Conductor.Instance.OnPeerConnectionCreated += peerName =>
            {
                var task = RunOnUiThread(() =>
                {
                    lock (this)
                    {
                        if (WebRTCStatus == WebRTCStatuses.Calling)
                        {
                            WebRTCStatus = WebRTCStatuses.InCall;
                            commandQueue.Add(new Command {
                                type = CommandType.SetInCall
                            });
                        }
                        else if (WebRTCStatus == WebRTCStatuses.Connected)
                        {
                            WebRTCStatus = WebRTCStatuses.InCall;
                            commandQueue.Add(new Command {
                                type = CommandType.SetInCall
                            });
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("Conductor.OnPeerConnectionCreated() - wrong status - " + WebRTCStatus);
                        }
                    }
                });
            };

            // Connection between the current user and a peer is closed event handler
            Conductor.Instance.OnPeerConnectionClosed += peerName =>
            {
                var localId  = SourceIDs[ClientName];
                var remoteId = SourceIDs[MentorName];

                var task = RunOnUiThread(() =>
                {
                    lock (this)
                    {
                        if (WebRTCStatus == WebRTCStatuses.EndingCall)
                        {
                            Plugin.UnloadMediaStreamSource(localId);
                            Plugin.UnloadMediaStreamSource(remoteId);
                            WebRTCStatus = WebRTCStatuses.Connected;
                            commandQueue.Add(new Command {
                                type = CommandType.SetConnected
                            });
                        }
                        else if (WebRTCStatus == WebRTCStatuses.InCall)
                        {
                            Plugin.UnloadMediaStreamSource(localId);
                            Plugin.UnloadMediaStreamSource(remoteId);
                            WebRTCStatus = WebRTCStatuses.Connected;
                            commandQueue.Add(new Command {
                                type = CommandType.SetConnected
                            });
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("Conductor.OnPeerConnectionClosed() - wrong status - " + WebRTCStatus);
                        }
                    }
                });
            };

            // Ready to connect to the server event handler
            Conductor.Instance.OnReadyToConnect += () => { var task = RunOnUiThread(() => { }); };

            List <Conductor.IceServer> iceServers = new List <Conductor.IceServer>
            {
                new Conductor.IceServer {
                    Host = "stun.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                },
                new Conductor.IceServer {
                    Host = "stun1.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                },
                new Conductor.IceServer {
                    Host = "stun2.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                },
                new Conductor.IceServer {
                    Host = "stun3.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                },
                new Conductor.IceServer {
                    Host = "stun4.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                }
            };
            Conductor.IceServer turnServer = new Conductor.IceServer {
                Host = "turnserver3dstreaming.centralus.cloudapp.azure.com:5349", Type = Conductor.IceServer.ServerType.TURN
            };
            turnServer.Credential = "3Dtoolkit072017";
            turnServer.Username   = "******";
            iceServers.Add(turnServer);
            Conductor.Instance.ConfigureIceServers(iceServers);

            var audioCodecList = Conductor.Instance.GetAudioCodecs();
            Conductor.Instance.AudioCodec = audioCodecList.FirstOrDefault(c => c.Name == "opus");
            System.Diagnostics.Debug.WriteLine("Selected audio codec - " + Conductor.Instance.AudioCodec.Name);

            var videoCodecList = Conductor.Instance.GetVideoCodecs();
            Conductor.Instance.VideoCodec = videoCodecList.FirstOrDefault(c => c.Name == PreferredVideoCodec);

            System.Diagnostics.Debug.WriteLine("Selected video codec - " + Conductor.Instance.VideoCodec.Name);
#endif
            UpdatePreferredFrameFormat();
        }
    public void Initialize()
    {
#if !UNITY_EDITOR
        // A Peer is connected to the server event handler
        Conductor.Instance.Signaller.OnPeerConnected += (peerId, peerName) =>
        {
            var task = RunOnUiThread(() =>
            {
                lock (this)
                {
                    Conductor.Peer peer = new Conductor.Peer {
                        Id = peerId, Name = peerName
                    };
                    Conductor.Instance.AddPeer(peer);
                    commandQueue.Add(new Command {
                        type = CommandType.AddRemotePeer, remotePeer = peer
                    });
                }
            });
        };

        // A Peer is disconnected from the server event handler
        Conductor.Instance.Signaller.OnPeerDisconnected += peerId =>
        {
            var task = RunOnUiThread(() =>
            {
                lock (this)
                {
                    var peerToRemove = Conductor.Instance.GetPeers().FirstOrDefault(p => p.Id == peerId);
                    if (peerToRemove != null)
                    {
                        Conductor.Peer peer = new Conductor.Peer {
                            Id = peerToRemove.Id, Name = peerToRemove.Name
                        };
                        Conductor.Instance.RemovePeer(peer);
                        commandQueue.Add(new Command {
                            type = CommandType.RemoveRemotePeer, remotePeer = peer
                        });
                    }
                }
            });
        };

        // The user is Signed in to the server event handler
        Conductor.Instance.Signaller.OnSignedIn += () =>
        {
            var task = RunOnUiThread(() =>
            {
                lock (this)
                {
                    if (status == Status.Connecting)
                    {
                        status = Status.Connected;
                        commandQueue.Add(new Command {
                            type = CommandType.SetConnected
                        });
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Signaller.OnSignedIn() - wrong status - " + status);
                    }
                }
            });
        };

        // Failed to connect to the server event handler
        Conductor.Instance.Signaller.OnServerConnectionFailure += () =>
        {
            var task = RunOnUiThread(() =>
            {
                lock (this)
                {
                    if (status == Status.Connecting)
                    {
                        status = Status.NotConnected;
                        commandQueue.Add(new Command {
                            type = CommandType.SetNotConnected
                        });
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Signaller.OnServerConnectionFailure() - wrong status - " + status);
                    }
                }
            });
        };

        // The current user is disconnected from the server event handler
        Conductor.Instance.Signaller.OnDisconnected += () =>
        {
            var task = RunOnUiThread(() =>
            {
                lock (this)
                {
                    if (status == Status.Disconnecting)
                    {
                        status = Status.NotConnected;
                        commandQueue.Add(new Command {
                            type = CommandType.SetNotConnected
                        });
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Signaller.OnDisconnected() - wrong status - " + status);
                    }
                }
            });
        };

        Conductor.Instance.OnAddRemoteStream    += Conductor_OnAddRemoteStream;
        Conductor.Instance.OnRemoveRemoteStream += Conductor_OnRemoveRemoteStream;
        Conductor.Instance.OnAddLocalStream     += Conductor_OnAddLocalStream;

        // Connected to a peer event handler
        Conductor.Instance.OnPeerConnectionCreated += () =>
        {
            var task = RunOnUiThread(() =>
            {
                lock (this)
                {
                    if (status == Status.Calling)
                    {
                        status = Status.InCall;
                        commandQueue.Add(new Command {
                            type = CommandType.SetInCall
                        });
                    }
                    else if (status == Status.Connected)
                    {
                        status = Status.InCall;
                        commandQueue.Add(new Command {
                            type = CommandType.SetInCall
                        });
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Conductor.OnPeerConnectionCreated() - wrong status - " + status);
                    }
                }
            });
        };

        // Connection between the current user and a peer is closed event handler
        Conductor.Instance.OnPeerConnectionClosed += () =>
        {
            var task = RunOnUiThread(() =>
            {
                lock (this)
                {
                    //DestroyMediaStreamSource();
                    if (status == Status.EndingCall)
                    {
                        Plugin.UnloadLocalMediaStreamSource();
                        Plugin.UnloadRemoteMediaStreamSource();
                        status = Status.Connected;
                        commandQueue.Add(new Command {
                            type = CommandType.SetConnected
                        });
                    }
                    else if (status == Status.InCall)
                    {
                        Plugin.UnloadLocalMediaStreamSource();
                        Plugin.UnloadRemoteMediaStreamSource();
                        status = Status.Connected;
                        commandQueue.Add(new Command {
                            type = CommandType.SetConnected
                        });
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Conductor.OnPeerConnectionClosed() - wrong status - " + status);
                    }
                }
            });
        };

        // Ready to connect to the server event handler
        Conductor.Instance.OnReadyToConnect += () => { var task = RunOnUiThread(() => { }); };

        List <Conductor.IceServer> iceServers = new List <Conductor.IceServer>();
        iceServers.Add(new Conductor.IceServer {
            Host = "stun.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
        });
        iceServers.Add(new Conductor.IceServer {
            Host = "stun1.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
        });
        iceServers.Add(new Conductor.IceServer {
            Host = "stun2.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
        });
        iceServers.Add(new Conductor.IceServer {
            Host = "stun3.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
        });
        iceServers.Add(new Conductor.IceServer {
            Host = "stun4.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
        });
        Conductor.IceServer turnServer = new Conductor.IceServer {
            Host = "turnserver3dstreaming.centralus.cloudapp.azure.com:5349", Type = Conductor.IceServer.ServerType.TURN
        };
        turnServer.Credential = "3Dtoolkit072017";
        turnServer.Username   = "******";
        iceServers.Add(turnServer);
        Conductor.Instance.ConfigureIceServers(iceServers);

        var audioCodecList = Conductor.Instance.GetAudioCodecs();
        Conductor.Instance.AudioCodec = audioCodecList.FirstOrDefault(c => c.Name == "opus");
        System.Diagnostics.Debug.WriteLine("Selected audio codec - " + Conductor.Instance.AudioCodec.Name);

        // Order the video codecs so that the stable VP8 is in front.
        var videoCodecList = Conductor.Instance.GetVideoCodecs();
        Conductor.Instance.VideoCodec = videoCodecList.FirstOrDefault(c => c.Name == "H264");
        //System.Diagnostics.Debug.WriteLine("Selected video codec - " + Conductor.Instance.VideoCodec.Name);

        uint preferredWidth     = 896;
        uint preferredHeght     = 504;
        uint preferredFrameRate = 15;
        uint minSizeDiff        = uint.MaxValue;
        Conductor.CaptureCapability selectedCapability = null;
        var videoDeviceList = Conductor.Instance.GetVideoCaptureDevices();
        foreach (Conductor.MediaDevice device in videoDeviceList)
        {
            Conductor.Instance.GetVideoCaptureCapabilities(device.Id).AsTask().ContinueWith(capabilities =>
            {
                foreach (Conductor.CaptureCapability capability in capabilities.Result)
                {
                    uint sizeDiff = (uint)Math.Abs(preferredWidth - capability.Width) + (uint)Math.Abs(preferredHeght - capability.Height);
                    if (sizeDiff < minSizeDiff)
                    {
                        selectedCapability = capability;
                        minSizeDiff        = sizeDiff;
                    }
                    //System.Diagnostics.Debug.WriteLine("Video device capability - " + device.Name + " - " + capability.Width + "x" + capability.Height + "@" + capability.FrameRate);
                }
            }).Wait();
        }

        if (selectedCapability != null)
        {
            selectedCapability.FrameRate           = preferredFrameRate;
            selectedCapability.MrcEnabled          = true;
            Conductor.Instance.VideoCaptureProfile = selectedCapability;
            Conductor.Instance.UpdatePreferredFrameFormat();
            //System.Diagnostics.Debug.WriteLine("Selected video device capability - " + selectedCapability.Width + "x" + selectedCapability.Height + "@" + selectedCapability.FrameRate);
        }
#endif
    }
示例#3
0
    void RequestAccessForMediaCaptureAndInit()
    {
        Conductor.RequestAccessForMediaCapture().AsTask().ContinueWith(async(antecedent) =>
        {
            if (antecedent.Result)
            {
                List <Conductor.IceServer> iceServers = new List <Conductor.IceServer>();
                iceServers.Add(new Conductor.IceServer {
                    Host = "stun.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                });
                iceServers.Add(new Conductor.IceServer {
                    Host = "stun1.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                });
                iceServers.Add(new Conductor.IceServer {
                    Host = "stun2.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                });
                iceServers.Add(new Conductor.IceServer {
                    Host = "stun3.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                });
                iceServers.Add(new Conductor.IceServer {
                    Host = "stun4.l.google.com:19302", Type = Conductor.IceServer.ServerType.STUN
                });
                Conductor.IceServer turnServer = new Conductor.IceServer {
                    Host = "turnserver3dstreaming.centralus.cloudapp.azure.com:5349", Type = Conductor.IceServer.ServerType.TURN
                };
                turnServer.Credential = "3Dtoolkit072017";
                turnServer.Username   = "******";
                iceServers.Add(turnServer);
                Conductor.Instance.ConfigureIceServers(iceServers);

                var audioCodecList            = Conductor.GetAudioCodecs();
                Conductor.Instance.AudioCodec = audioCodecList.FirstOrDefault(c => c.Name == "opus");
                System.Diagnostics.Debug.WriteLine("Selected audio codec - " + Conductor.Instance.AudioCodec.Name);

                // Order the video codecs so that the stable VP8 is in front.
                var videoCodecList            = Conductor.GetVideoCodecs();
                Conductor.Instance.VideoCodec = videoCodecList.FirstOrDefault(c => c.Name == "H264");
                System.Diagnostics.Debug.WriteLine("Selected video codec - " + Conductor.Instance.VideoCodec.Name);

                uint preferredWidth     = 896;
                uint preferredHeght     = 504;
                uint preferredFrameRate = 15;
                uint minSizeDiff        = uint.MaxValue;
                Conductor.MediaDevice selectedDevice           = null;
                Conductor.CaptureCapability selectedCapability = null;
                foreach (Conductor.MediaDevice device in await Conductor.GetVideoCaptureDevices())
                {
                    Conductor.Instance.GetVideoCaptureCapabilities(device.Id).AsTask().ContinueWith(capabilities =>
                    {
                        foreach (Conductor.CaptureCapability capability in capabilities.Result)
                        {
                            uint sizeDiff = (uint)Math.Abs(preferredWidth - capability.Width) + (uint)Math.Abs(preferredHeght - capability.Height);
                            if (sizeDiff < minSizeDiff)
                            {
                                selectedDevice     = device;
                                selectedCapability = capability;
                                minSizeDiff        = sizeDiff;
                            }
                            System.Diagnostics.Debug.WriteLine("Video device capability - " + device.Name + " - " + capability.Width + "x" + capability.Height + "@" + capability.FrameRate);
                        }
                    }).Wait();
                }

                if (selectedDevice != null)
                {
                    selectedCapability.FrameRate  = preferredFrameRate;
                    selectedCapability.MrcEnabled = true;
                    Conductor.Instance.SelectVideoDevice(selectedDevice);
                    Conductor.Instance.VideoCaptureProfile = selectedCapability;
                    System.Diagnostics.Debug.WriteLine("Selected video device - " + selectedDevice.Name);
                    System.Diagnostics.Debug.WriteLine("Selected video device capability - " + selectedCapability.Width + "x" + selectedCapability.Height + "@" + selectedCapability.FrameRate);
                }

                Conductor.Instance.Initialized += Conductor_Initialized;
                Conductor.Instance.EnableLogging(Conductor.LogLevel.Verbose);
                Conductor.Instance.Initialize(CoreApplication.MainView.CoreWindow.Dispatcher);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Failed to obtain access to media devices");
            }
        });
    }