123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using RtcUtil;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Threading;
- using VRTC.WpfClient.Log;
- using WebRtc.NET;
- namespace VRTC.WpfClient
- {
- class MainWindowViewModel : ViewModel
- {
- private string _hostUrl;
- private uint _roomId;
- private string _userId;
- private VRTCClient _client;
- private readonly Action<Action> _runOnMainDispatcher;
- private bool _audioEnabled;
- private string _defaultVideoDevice;
- public ButtonCommand ConnectCommand { get; set; }
- public ButtonCommand ExitCommand { get; set; }
- public string HostUrl
- {
- get => _hostUrl;
- set
- {
- if (_hostUrl != value)
- {
- _hostUrl = value;
- OnPropertyChanged(() => HostUrl);
- }
- }
- }
- public uint RoomId
- {
- get => _roomId;
- set
- {
- if (_roomId != value)
- {
- _roomId = value;
- OnPropertyChanged(() => RoomId);
- }
- }
- }
- public string UserId
- {
- get => _userId;
- set
- {
- if (_userId != value)
- {
- _userId = value;
- OnPropertyChanged(() => UserId);
- }
- }
- }
- //private List<SequenceExecutor<int>> _sequenceExecutors = new List<SequenceExecutor<int>>();
- /// <summary>
- /// Request window close
- /// </summary>
- public event EventHandler RequestClosed;
- /// <summary>
- /// Cancel Command
- /// </summary>
- public Command CancelCommand { get; }
- /// <summary>
- /// Finish command
- /// </summary>
- public Command CloseCommand { get; }
- /// <summary>
- /// Open Output Dir Command
- /// </summary>
- public Command OpenOutputDirCommand { get; }
- /// <summary>
- /// Log infoes
- /// </summary>
- public ObservableCollection<LogItem> LogItems { get; }
- public Command ClearLogCommand { get; }
- public VideoViewModel LocalVideo { get; }
- public ObservableCollection<VideoViewModel> RemoteVideos { get; }
- public IList<string> VideoDevices { get; }
- public string DefaultVideoDevice
- {
- get => _defaultVideoDevice;
- set
- {
- if (_defaultVideoDevice != value)
- {
- _defaultVideoDevice = value;
- OnPropertyChanged(()=>DefaultVideoDevice);
- }
- }
- }
- public bool AudioEnabled
- {
- get { return _audioEnabled; }
- set
- {
- if (_audioEnabled != value)
- {
- _audioEnabled = value;
- OnPropertyChanged(() => AudioEnabled);
- }
- }
- }
- public MainWindowViewModel(Action<Action> runOnMainDispatcher)
- {
- LogItems = new ObservableCollection<LogItem>();
- Description = "AppName";
- CancelCommand = new ButtonCommand(OnCancelCommand, "Cancel");
- CloseCommand = new ButtonCommand(OnCloseCommand, "Finish");
- ClearLogCommand = new ButtonCommand(OnClearLogCommand, "ClearLog");
- HostUrl = "ws://192.168.6.175:9000/";
- //HostUrl = "wss://192.168.6.175:8443/wss/";
- RoomId = 100;
- UserId = "PC-1";
- ConnectCommand = new ButtonCommand(OnConnect, "Connect");
- ExitCommand = new ButtonCommand(OnExit, "Exit");
- LocalVideo = new VideoViewModel();
- RemoteVideos = new ObservableCollection<VideoViewModel>();
- this._runOnMainDispatcher = runOnMainDispatcher;
- VideoDevices = MediaDevices.EnumerateVideoDevices();
- DefaultVideoDevice = VideoDevices.FirstOrDefault();
- }
- private void OnExit(object obj)
- {
- if (_client != null)
- {
- _client.LocalVideoFrameReceived -= OnRenderLocal;
- _client.RemoteVieoFrameReceived -= OnRendRemote;
- }
- _client?.Dispose();
- _client = null;
- }
- private async void OnConnect(object obj)
- {
- try
- {
- var device = ManagedConductor.GetVideoDevices().First();
- _client = new VRTCClient(device);
- await _client.Connect(HostUrl, RoomId, UserId, false);
- _client.LocalVideoFrameReceived += OnRenderLocal;
- _client.RemoteVieoFrameReceived += OnRendRemote;
- }
- catch (Exception ex) { Logger.WriteLineError($"OnConnect ex:{ex}"); }
- }
- private void OnRendRemote(object? sender, VideoFrameData e)
- {
- var videoVm = RemoteVideos.FirstOrDefault(x => x.Id == e.ClientId);
- if (videoVm == null)
- {
- videoVm = new VideoViewModel() { Id = e.ClientId };
- _runOnMainDispatcher?.Invoke(() => { RemoteVideos.Add(videoVm); });
- }
- videoVm.OnFrameReceived(this, e);
- }
- private void OnRenderLocal(object? sender, VideoFrameData e)
- {
- //throw new NotImplementedException();
- LocalVideo.OnFrameReceived(sender, e);
- }
- private void OnClearLogCommand(object obj)
- {
- LogItems.Clear();
- }
- private void OnCancelCommand(object obj)
- {
- OnRequestClosed();
- }
- private void OnCloseCommand(object obj)
- {
- OnRequestClosed();
- }
- void OnRequestClosed()
- {
- RequestClosed?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|