MainWindowViewModel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using RtcUtil;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Threading;
  9. using VRTC.WpfClient.Log;
  10. using WebRtc.NET;
  11. namespace VRTC.WpfClient
  12. {
  13. class MainWindowViewModel : ViewModel
  14. {
  15. private string _hostUrl;
  16. private uint _roomId;
  17. private string _userId;
  18. private VRTCClient _client;
  19. private readonly Action<Action> _runOnMainDispatcher;
  20. private bool _audioEnabled;
  21. private string _defaultVideoDevice;
  22. public ButtonCommand ConnectCommand { get; set; }
  23. public ButtonCommand ExitCommand { get; set; }
  24. public string HostUrl
  25. {
  26. get => _hostUrl;
  27. set
  28. {
  29. if (_hostUrl != value)
  30. {
  31. _hostUrl = value;
  32. OnPropertyChanged(() => HostUrl);
  33. }
  34. }
  35. }
  36. public uint RoomId
  37. {
  38. get => _roomId;
  39. set
  40. {
  41. if (_roomId != value)
  42. {
  43. _roomId = value;
  44. OnPropertyChanged(() => RoomId);
  45. }
  46. }
  47. }
  48. public string UserId
  49. {
  50. get => _userId;
  51. set
  52. {
  53. if (_userId != value)
  54. {
  55. _userId = value;
  56. OnPropertyChanged(() => UserId);
  57. }
  58. }
  59. }
  60. //private List<SequenceExecutor<int>> _sequenceExecutors = new List<SequenceExecutor<int>>();
  61. /// <summary>
  62. /// Request window close
  63. /// </summary>
  64. public event EventHandler RequestClosed;
  65. /// <summary>
  66. /// Cancel Command
  67. /// </summary>
  68. public Command CancelCommand { get; }
  69. /// <summary>
  70. /// Finish command
  71. /// </summary>
  72. public Command CloseCommand { get; }
  73. /// <summary>
  74. /// Open Output Dir Command
  75. /// </summary>
  76. public Command OpenOutputDirCommand { get; }
  77. /// <summary>
  78. /// Log infoes
  79. /// </summary>
  80. public ObservableCollection<LogItem> LogItems { get; }
  81. public Command ClearLogCommand { get; }
  82. public VideoViewModel LocalVideo { get; }
  83. public ObservableCollection<VideoViewModel> RemoteVideos { get; }
  84. public IList<string> VideoDevices { get; }
  85. public string DefaultVideoDevice
  86. {
  87. get => _defaultVideoDevice;
  88. set
  89. {
  90. if (_defaultVideoDevice != value)
  91. {
  92. _defaultVideoDevice = value;
  93. OnPropertyChanged(()=>DefaultVideoDevice);
  94. }
  95. }
  96. }
  97. public bool AudioEnabled
  98. {
  99. get { return _audioEnabled; }
  100. set
  101. {
  102. if (_audioEnabled != value)
  103. {
  104. _audioEnabled = value;
  105. OnPropertyChanged(() => AudioEnabled);
  106. }
  107. }
  108. }
  109. public MainWindowViewModel(Action<Action> runOnMainDispatcher)
  110. {
  111. LogItems = new ObservableCollection<LogItem>();
  112. Description = "AppName";
  113. CancelCommand = new ButtonCommand(OnCancelCommand, "Cancel");
  114. CloseCommand = new ButtonCommand(OnCloseCommand, "Finish");
  115. ClearLogCommand = new ButtonCommand(OnClearLogCommand, "ClearLog");
  116. HostUrl = "ws://192.168.6.175:9000/";
  117. //HostUrl = "wss://192.168.6.175:8443/wss/";
  118. RoomId = 100;
  119. UserId = "PC-1";
  120. ConnectCommand = new ButtonCommand(OnConnect, "Connect");
  121. ExitCommand = new ButtonCommand(OnExit, "Exit");
  122. LocalVideo = new VideoViewModel();
  123. RemoteVideos = new ObservableCollection<VideoViewModel>();
  124. this._runOnMainDispatcher = runOnMainDispatcher;
  125. VideoDevices = MediaDevices.EnumerateVideoDevices();
  126. DefaultVideoDevice = VideoDevices.FirstOrDefault();
  127. }
  128. private void OnExit(object obj)
  129. {
  130. if (_client != null)
  131. {
  132. _client.LocalVideoFrameReceived -= OnRenderLocal;
  133. _client.RemoteVieoFrameReceived -= OnRendRemote;
  134. }
  135. _client?.Dispose();
  136. _client = null;
  137. }
  138. private async void OnConnect(object obj)
  139. {
  140. try
  141. {
  142. var device = ManagedConductor.GetVideoDevices().First();
  143. _client = new VRTCClient(device);
  144. await _client.Connect(HostUrl, RoomId, UserId, false);
  145. _client.LocalVideoFrameReceived += OnRenderLocal;
  146. _client.RemoteVieoFrameReceived += OnRendRemote;
  147. }
  148. catch (Exception ex) { Logger.WriteLineError($"OnConnect ex:{ex}"); }
  149. }
  150. private void OnRendRemote(object? sender, VideoFrameData e)
  151. {
  152. var videoVm = RemoteVideos.FirstOrDefault(x => x.Id == e.ClientId);
  153. if (videoVm == null)
  154. {
  155. videoVm = new VideoViewModel() { Id = e.ClientId };
  156. _runOnMainDispatcher?.Invoke(() => { RemoteVideos.Add(videoVm); });
  157. }
  158. videoVm.OnFrameReceived(this, e);
  159. }
  160. private void OnRenderLocal(object? sender, VideoFrameData e)
  161. {
  162. //throw new NotImplementedException();
  163. LocalVideo.OnFrameReceived(sender, e);
  164. }
  165. private void OnClearLogCommand(object obj)
  166. {
  167. LogItems.Clear();
  168. }
  169. private void OnCancelCommand(object obj)
  170. {
  171. OnRequestClosed();
  172. }
  173. private void OnCloseCommand(object obj)
  174. {
  175. OnRequestClosed();
  176. }
  177. void OnRequestClosed()
  178. {
  179. RequestClosed?.Invoke(this, EventArgs.Empty);
  180. }
  181. }
  182. }