Esempio n. 1
0
        public WebRtcSignaller(IBasicSignaller signaller)
        {
            var protocol = new MessageProtocol <WebRtcMessage>();

            this.Signaller = new ProtocolSignaller <WebRtcMessage>(
                signaller,
                protocol);

            this.Signaller.ReceivedMessage += (sender, message) =>
            {
                switch (message.Type)
                {
                case WebRtcMessage.Offer:
                    var offer = this.DeserializeSessionDescription(message.Contents);
                    this.ReceivedOffer?.Invoke(this, offer);
                    break;

                case WebRtcMessage.Answer:
                    var answer = this.DeserializeSessionDescription(message.Contents);
                    this.ReceivedAnswer?.Invoke(this, answer);
                    break;

                case WebRtcMessage.IceCandidate:
                    var init      = JsonConvert.DeserializeObject <RTCIceCandidateInit>(message.Contents);
                    var candidate = new RTCIceCandidate(init);
                    this.ReceivedIceCandidate?.Invoke(this, candidate);
                    break;

                case WebRtcMessage.Plain:
                    this.ReceivedPlain?.Invoke(this, message.Contents);
                    break;

                case WebRtcMessage.Shutdown:
                    this.ReceivedShutdown?.Invoke(this, EventArgs.Empty);
                    break;

                case WebRtcMessage.CursorUpdate:
                    var update = JsonConvert.DeserializeObject <CursorUpdate>(message.Contents);
                    this.ReceivedCursorUpdate?.Invoke(this, update);
                    break;
                }
            };
        }
Esempio n. 2
0
        protected override async void OnNavigatedTo(NavigationEventArgs args)
        {
            signaller = new WebsocketSignaller("client");
            var protocol = new MessageProtocol <WebRtcSignaller.WebRtcMessage>();

            this.cursorSignaller = new ProtocolSignaller <WebRtcSignaller.WebRtcMessage>
                                   (
                signaller,
                protocol
                                   );

            // Not implemented for source, but necessary here.
            // EX: signaller connection void before source<=>client connection made.
            // This could happen if the source was disconnected before direct connection.
            signaller.ConnectionEnded += async(s, a) =>
            {
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    if (!isConnectedToSource)
                    {
                        this.ConnectedOptions.Hide();
                        this.StartupSettings.Show();
                        this.CursorElement.Hide();
                    }
                });
            };

            signaller.ConnectionFailed += (s, a) =>
            {
                this.ConnectedOptions.Hide();
                this.StartupSettings.Show();
                this.CursorElement.Hide();
            };

            dispatcher           = CoreWindow.GetForCurrentThread().Dispatcher;
            contSpeechRecognizer = new SpeechRecognizer();
            await contSpeechRecognizer.CompileConstraintsAsync();

            contSpeechRecognizer.HypothesisGenerated += ContSpeechRecognizer_HypothesisGenerated;
            contSpeechRecognizer.ContinuousRecognitionSession.ResultGenerated +=
                ContinuousRecognitionSession_ResultGenerated;


            contSpeechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;

            await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();


            this.ServerConnectButton.Click += async(s, a) =>
            {
                this.StartupSettings.Hide();
                await signaller.ConnectToServer(ServerConfig.AwsAddress);

                isConnectedToSource = false;

                if (signaller.connected)
                {
                    this.ConnectedOptions.Show();
                    this.CursorElement.Show();
                }
            };

            this.CursorElement.ManipulationDelta += async(s, e) =>
            {
                double newX = (this.t_Transform.TranslateX + e.Delta.Translation.X) / this.RemoteVideo.ActualWidth,
                       newY = (this.t_Transform.TranslateY + e.Delta.Translation.Y) / this.RemoteVideo.ActualHeight;

                //Logger.Log("newX: " + newX.ToString() + " newY: " + newY.ToString());
                if (newX < -threshold || newX > threshold ||
                    newY < -threshold || newY > threshold)
                {
                    return;
                }

                if (this.isConnectedToSource)
                {
                    await this.cursorSignaller.SendMessage(
                        WebRtcSignaller.WebRtcMessage.CursorUpdate,
                        "{ x: " + newX.ToString() + ", y: " + newY.ToString() + "}"
                        );
                }
                this.t_Transform.TranslateX += e.Delta.Translation.X;
                this.t_Transform.TranslateY += e.Delta.Translation.Y;
            };

            var config = new ConductorConfig()
            {
                CoreDispatcher = this.Dispatcher,
                RemoteVideo    = this.RemoteVideo,
                Signaller      = signaller,
                Identity       = "client"
            };

            await this.conductor.Initialize(config);

            var opts = new MediaOptions(
                new MediaOptions.Init()
            {
                ReceiveVideo = (bool)this.ReceiveVideoCheck.IsChecked,
                ReceiveAudio = (bool)this.ReceiveAudioCheck.IsChecked,
                SendAudio    = (bool)this.SendAudioCheck.IsChecked
            });

            this.conductor.SetMediaOptions(opts);

            this.conductor.UISignaller.ReceivedShutdown += async(s, a) =>
            {
                await this.conductor.Shutdown();
            };

            this.conductor.UISignaller.ReceivedPlain += (s, message) =>
            {
                Logger.Log(message);
            };

            this.conductor.UISignaller.ReceivedCursorUpdate += async(s, update) =>
            {
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    if (update.x < -threshold || update.x > threshold ||
                        update.y < -threshold || update.y > threshold)
                    {
                        return;
                    }
                    this.t_Transform.TranslateX = update.x * this.RemoteVideo.ActualWidth;
                    this.t_Transform.TranslateY = update.y * this.RemoteVideo.ActualHeight;
                });
            };
        }