public void Handle(UpdateCall update) { _call = update.Call; if (update.Call.State is CallStatePending pending) { if (update.Call.IsOutgoing && pending.IsCreated && pending.IsReceived) { if (pending.IsCreated && pending.IsReceived) { _mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Audio/voip_ringback.mp3")); _mediaPlayer.IsLoopingEnabled = true; _mediaPlayer.Play(); } } } if (update.Call.State is CallStateReady ready) { var user = CacheService.GetUser(update.Call.UserId); if (user == null) { return; } VoIPControllerWrapper.UpdateServerConfig(ready.Config); var logFile = Path.Combine(ApplicationData.Current.LocalFolder.Path, $"{SessionId}", "tgvoip.logFile.txt"); var statsDumpFile = Path.Combine(ApplicationData.Current.LocalFolder.Path, $"{SessionId}", "tgvoip.statsDump.txt"); var call_packet_timeout_ms = CacheService.GetOption <OptionValueInteger>("call_packet_timeout_ms"); var call_connect_timeout_ms = CacheService.GetOption <OptionValueInteger>("call_connect_timeout_ms"); if (_controller != null) { _controller.Dispose(); _controller = null; } _controller = new VoIPControllerWrapper(); _controller.SetConfig(call_packet_timeout_ms.Value / 1000.0, call_connect_timeout_ms.Value / 1000.0, base.Settings.UseLessData, true, true, true, logFile, statsDumpFile); _controller.CallStateChanged += (s, args) => { BeginOnUIThread(() => { if (args == libtgvoip.CallState.WaitInit || args == libtgvoip.CallState.WaitInitAck) { _mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Audio/voip_connecting.mp3")); _mediaPlayer.IsLoopingEnabled = false; _mediaPlayer.Play(); } else if (args == libtgvoip.CallState.Established) { _mediaPlayer.Source = null; } }); }; BeginOnUIThread(() => { Show(update.Call, _controller); }); var p2p = base.Settings.PeerToPeerMode == 0 || (base.Settings.PeerToPeerMode == 1 && user.OutgoingLink is LinkStateIsContact); var endpoints = new Endpoint[ready.Connections.Count]; for (int i = 0; i < endpoints.Length; i++) { endpoints[i] = new Endpoint { id = ready.Connections[i].Id, ipv4 = ready.Connections[i].Ip, ipv6 = ready.Connections[i].Ipv6, peerTag = ready.Connections[i].PeerTag.ToArray(), port = (ushort)ready.Connections[i].Port }; } _controller.SetEncryptionKey(ready.EncryptionKey.ToArray(), update.Call.IsOutgoing); _controller.SetPublicEndpoints(endpoints, ready.Protocol.UdpP2p && p2p, 74); _controller.Start(); _controller.Connect(); } else if (update.Call.State is CallStateDiscarded discarded) { if (discarded.NeedDebugInformation) { ProtoService.Send(new SendCallDebugInformation(update.Call.Id, _controller.GetDebugLog())); } if (discarded.NeedRating) { BeginOnUIThread(async() => { var dialog = new PhoneCallRatingView(); var confirm = await dialog.ShowQueuedAsync(); if (confirm == ContentDialogResult.Primary) { ProtoService.Send(new SendCallRating(update.Call.Id, dialog.Rating + 1, dialog.Rating >= 0 && dialog.Rating <= 3 ? dialog.Comment : null)); } }); } _controller?.Dispose(); _controller = null; _call = null; } BeginOnUIThread(() => { switch (update.Call.State) { case CallStateDiscarded discarded: if (update.Call.IsOutgoing && discarded.Reason is CallDiscardReasonDeclined) { _mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Audio/voip_busy.mp3")); _mediaPlayer.IsLoopingEnabled = true; _mediaPlayer.Play(); Show(update.Call, null); } else { _mediaPlayer.Source = null; Hide(); } break; case CallStateError error: Hide(); break; default: Show(update.Call, null); break; } }); }
public async void Handle(UpdateCall update) { _call = update.Call; if (update.Call.State is CallStatePending pending) { if (update.Call.IsOutgoing && pending.IsCreated && pending.IsReceived) { if (pending.IsCreated && pending.IsReceived) { _mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Audio/voip_ringback.mp3")); _mediaPlayer.IsLoopingEnabled = true; _mediaPlayer.Play(); } } } if (update.Call.State is CallStateReady ready) { var user = CacheService.GetUser(update.Call.UserId); if (user == null) { return; } VoIPControllerWrapper.UpdateServerConfig(ready.Config); var logFile = Path.Combine(ApplicationData.Current.LocalFolder.Path, $"{SessionId}", $"voip{update.Call.Id}.txt"); var statsDumpFile = Path.Combine(ApplicationData.Current.LocalFolder.Path, $"{SessionId}", "tgvoip.statsDump.txt"); var call_packet_timeout_ms = CacheService.Options.CallPacketTimeoutMs; var call_connect_timeout_ms = CacheService.Options.CallConnectTimeoutMs; if (_controller != null) { _controller.Dispose(); _controller = null; } var config = new VoIPConfig { initTimeout = call_packet_timeout_ms / 1000.0, recvTimeout = call_connect_timeout_ms / 1000.0, dataSaving = base.Settings.UseLessData, enableAEC = true, enableNS = true, enableAGC = true, enableVolumeControl = true, logFilePath = logFile, statsDumpFilePath = statsDumpFile }; _controller = new VoIPControllerWrapper(); _controller.SetConfig(config); _controller.CurrentAudioInput = SettingsService.Current.VoIP.InputDevice; _controller.CurrentAudioOutput = SettingsService.Current.VoIP.OutputDevice; _controller.SetInputVolume(SettingsService.Current.VoIP.InputVolume); _controller.SetOutputVolume(SettingsService.Current.VoIP.OutputVolume); _controller.CallStateChanged += (s, args) => { BeginOnUIThread(() => { if (args == libtgvoip.CallState.WaitInit || args == libtgvoip.CallState.WaitInitAck) { _mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Audio/voip_connecting.mp3")); _mediaPlayer.IsLoopingEnabled = false; _mediaPlayer.Play(); } else if (args == libtgvoip.CallState.Established) { _callStarted = DateTime.Now; _mediaPlayer.Source = null; } }); }; BeginOnUIThread(() => { Show(update.Call, _controller, _callStarted); }); var endpoints = new Endpoint[ready.Connections.Count]; for (int i = 0; i < endpoints.Length; i++) { endpoints[i] = ready.Connections[i].ToEndpoint(); } _controller.SetEncryptionKey(ready.EncryptionKey.ToArray(), update.Call.IsOutgoing); _controller.SetPublicEndpoints(endpoints, ready.Protocol.UdpP2p && ready.AllowP2p, ready.Protocol.MaxLayer); _controller.Start(); _controller.Connect(); } else if (update.Call.State is CallStateDiscarded discarded) { if (discarded.NeedDebugInformation) { ProtoService.Send(new SendCallDebugInformation(update.Call.Id, _controller.GetDebugLog())); } if (discarded.NeedRating) { BeginOnUIThread(async() => await SendRatingAsync(update.Call.Id)); } _controller?.Dispose(); _controller = null; _call = null; } await Dispatcher.DispatchAsync(() => { switch (update.Call.State) { case CallStateDiscarded discarded: if (update.Call.IsOutgoing && discarded.Reason is CallDiscardReasonDeclined) { _mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Audio/voip_busy.mp3")); _mediaPlayer.IsLoopingEnabled = true; _mediaPlayer.Play(); Show(update.Call, null, _callStarted); } else { _mediaPlayer.Source = null; Hide(); } break; case CallStateError error: Hide(); break; default: Show(update.Call, null, _callStarted); break; } }); }