Example #1
0
        protected override async void OnNavigatedTo(NavigationEventArgs args)
        {
            Logger.Log("Initializing Application.");

            #region ClientInitialization

            this.client = new SignallerClient("client");

            this.client.ConnectionSuccessUIHandler += async(s, a) =>
            {
                Logger.Log("Connected to Signaller.");
                this.changeStep(2);
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    this.client.RequestPollRooms();
                });
            };

            // Useful if signaller connection void before source <=> client connection established.
            // For example, if source disconnected prior to direct connection establishment.
            this.client.ConnectionEndedUIHandler += async(s, a) =>
            {
                Logger.Log("Disconnected from Signaller.");
                this.changeStep(1);
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    if (!this.conductor.CallInProgress())
                    {
                        this.ConnectedOptions.Hide();
                        this.StartupSettings.Show();
                        this.CursorPanel.Hide();
                        this.SourceDisconnectButton.Hide();
                        this.SourceConnectButton.Show();
                    }
                });
            };

            this.client.ConnectionFailedUIHandler += async(s, a) =>
            {
                Logger.Log("Failed to connect to Signaller.");
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    this.ConnectedOptions.Hide();
                    this.StartupSettings.Show();
                    this.CursorPanel.Hide();
                    this.SourceDisconnectButton.Hide();
                    this.SourceConnectButton.Show();
                });
            };

            this.client.MessageHandlers["Plain"] += (sender, message) =>
            {
                Logger.Log(message.Contents);
            };

            this.client.MessageHandlers["CursorUpdate"] += async(sender, message) =>
            {
                var update = JsonConvert.DeserializeObject <CursorUpdate>(message.Contents);
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    if (update.x < -cursorThreshold || update.x > cursorThreshold ||
                        update.y < -cursorThreshold || update.y > cursorThreshold)
                    {
                        return;
                    }

                    this.t_Transform.TranslateX = update.x * this.RemoteVideo.ActualWidth;
                    this.t_Transform.TranslateY = update.y * this.RemoteVideo.ActualHeight;
                });
            };

            this.client.MessageHandlers["RoomPoll"] += async(sender, message) =>
            {
                string[] rooms = message.Contents.Split(',');
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    this.JoinRoomComboBox.Items.Clear();
                    foreach (var room in rooms)
                    {
                        this.JoinRoomComboBox.Items.Add(room);
                    }
                });
            };

            this.client.RoomJoinFailedUIHandler += (s, m) =>
            {
                Logger.Log("Failed to join room.");
            };

            this.client.RoomJoinSuccessUIHandler += (s, m) =>
            {
                this.changeStep(3);
                Logger.Log("Successfully joined room.");
            };

            #endregion

            #region ConductorInitialization

            this.conductor = new WebRtcConductor();

            WebRtcConductor.Config config = new WebRtcConductor.Config()
            {
                CoreDispatcher = this.Dispatcher,
                RemoteVideo    = this.RemoteVideo,
                Signaller      = client
            };

            await this.conductor.Initialize(config);

            var opts = new WebRtcConductor.MediaOptions(
                new WebRtcConductor.MediaOptions.Init()
            {
                ReceiveVideo = true
            });

            this.conductor.SetMediaOptions(opts);

            #endregion

            #region SpeechRecognitionInitialization

            try
            {
                contSpeechRecognizer = new SpeechRecognizer();

                await contSpeechRecognizer.CompileConstraintsAsync();

                contSpeechRecognizer.HypothesisGenerated +=
                    ContSpeechRecognizer_HypothesisGenerated;
                contSpeechRecognizer.ContinuousRecognitionSession.Completed +=
                    ContinuousRecognitionSession_Completed;

                await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                Logger.Log("Please restart the Application and enable Speech Recognition in Settings -> Privacy -> Speech.");
            }

            #endregion

            #region LambdaUIHandlers

            this.ServerConnectButton.Click += async(s, a) =>
            {
                this.StartupSettings.Hide();
                await this.client.ConnectToSignaller();

                if (client.IsConnected)
                {
                    this.ConnectedOptions.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;

                // prevent sending out of bound data
                if (newX < -cursorThreshold || newX > cursorThreshold ||
                    newY < -cursorThreshold || newY > cursorThreshold)
                {
                    return;
                }

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

            #endregion

            Logger.Log("Done.");
        }
Example #2
0
        protected override async void OnNavigatedTo(NavigationEventArgs args)
        {
            Logger.Log("Initializing Application.");

            #region ClientInitialization

            this.client = new SignallerClient("source");

            this.client.ConnectionFailedUIHandler += async(s, a) =>
            {
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Logger.Log("Failed to connect to Signaller.");
                    this.Connected.Hide();
                    this.NotConnected.Show();
                });
            };

            this.client.ConnectionSuccessUIHandler += (s, a) =>
            {
                Logger.Log("Connected to Signaller.");
            };

            this.client.ConnectionEndedUIHandler += (s, a) =>
            {
                Logger.Log("Disconnected from Signaller.");
            };

            this.client.RoomCreateFailedUIHandler += (s, a) =>
            {
                Logger.Log("Room Creation Failed (either empty room or already exists).");
            };

            this.client.RoomJoinSuccessUIHandler += (s, a) =>
            {
                Logger.Log($"Joined Room {this.client.RoomId}.");
            };

            this.client.RoomJoinFailedUIHandler += (s, a) =>
            {
                Logger.Log("Room Join Failed (Nonexistent room or because source already exists).");
            };

            this.client.MessageHandlers["Plain"] += (sender, message) =>
            {
                Logger.Log(message.Contents);
            };

            #endregion

            #region ConductorInitialization

            this.conductor = new WebRtcConductor();

            var config = new WebRtcConductor.Config()
            {
                CoreDispatcher = this.Dispatcher,
                Signaller      = client,
            };

            await this.conductor.Initialize(config);

            var opts = new WebRtcConductor.MediaOptions(
                new WebRtcConductor.MediaOptions.Init()
            {
                SendVideo = true
            });

            this.conductor.SetMediaOptions(opts);

            var devices = await this.conductor.GetVideoDevices();

            this.MediaDeviceComboBox.ItemsSource   = devices;
            this.MediaDeviceComboBox.SelectedIndex = 0;

            this.CaptureFormatComboBox.ItemsSource =
                await this.conductor.GetCaptureProfiles(devices.First());

            this.CaptureFormatComboBox.SelectedIndex = 0;

            #endregion

            #region LambdaUIHandlers

            this.ConnectToServerButton.Click += async(s, a) =>
            {
                this.NotConnected.Hide();
                await client.ConnectToSignaller();

                if (client.IsConnected)
                {
                    this.Connected.Show();
                }
            };

            this.DisconnectFromServerButton.Click += async(s, a) =>
            {
                this.Connected.Hide();
                client.DisconnectFromSignaller();
                this.NotConnected.Show();
            };

            #endregion

            Logger.Log("Done.");
        }