123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- using System;
- using System.Collections.Concurrent;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using Vinno.FIS.TRTCClient.Captures;
- using Vinno.FIS.TRTCClient.Common.Enum;
- using Vinno.FIS.TRTCClient.Common.Log;
- using Vinno.FIS.TRTCClient.Common.Pipe;
- namespace Vinno.FIS.TRTCClient
- {
- /// <summary>
- /// Interaction logic for App.xaml
- /// </summary>
- public partial class App : Application
- {
- private DefaultLogEngine _logEngine;
- private TRTCChatRoom _trtcChatRoom;
- private PipeServer _messageServer;
- private PipeClient _messageClient;
- private CancellationTokenSource _tokenSource;
- private int _processId;
- private PipeClient _localVideoPipeClient;
- private int _terminalWidth;
- private int _terminalHeight;
- private int _localImageWidth;
- private int _localImageHeight;
- private byte[] _localBuffer = new byte[0];
- private ConcurrentDictionary<string, PipeRemoteImageSizeData> _remoteImageList;
- private ConcurrentDictionary<string, PipeClient> _remoteVideoPipeClientList;
- private bool _isLiveMode;
- private bool _isSonopost;
- private bool _isUsMachineImage;
- private EnumLiveChannelCategory _category;
- private TRTCPusher _trtcPusher;
- private PipeServer _localVideoPipeServer;
- private bool _hasReceivedFirstImage;
- private bool _isPreviewing;
- private bool _disposed;
- protected override void OnStartup(StartupEventArgs e)
- {
- try
- {
- _tokenSource = new CancellationTokenSource();
- _remoteVideoPipeClientList = new ConcurrentDictionary<string, PipeClient>();
- _remoteImageList = new ConcurrentDictionary<string, PipeRemoteImageSizeData>();
- if (e.Args.Length > 2)
- {
- var parentPid = GetArgValue(e.Args[0], "pid");
- var lpt = GetArgValue(e.Args[1], "lpt");
- var roomInofLen = e.Args.Length - 2;
- var roomInfoBytes = new string[roomInofLen];
- Array.Copy(e.Args, 2, roomInfoBytes, 0, roomInofLen);
- var roomInfo = TRTCRoomInfo.ConvertFromArgs(roomInfoBytes);
- _isLiveMode = roomInfo.IsForLive;
- _isSonopost = roomInfo.IsSonopost;
- _isUsMachineImage = roomInfo.IsUSMachineImage;
- _category = roomInfo.Category;
- string logPath;
- if (!string.IsNullOrEmpty(lpt))
- {
- logPath = Path.Combine(lpt, "TRTCClientLogs");
- }
- else
- {
- logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TRTCClientLogs");
- }
- if (!Directory.Exists(logPath))
- {
- Directory.CreateDirectory(logPath);
- }
- _logEngine = new DefaultLogEngine(logPath, _category);
- Logger.RegisterEngine(_logEngine);
- Logger.WriteLineInfo("TRTCClient Start Up......");
- Logger.WriteLineInfo($"Parent Process Id:{parentPid},RoomInfo:{roomInfo}");
- _processId = Process.GetCurrentProcess().Id;
- Logger.WriteLineInfo($"Current Process Id:{_processId}");
- StartMessagePipe();
- StartLocalVideoPipe();
- StartCheckParentThread(parentPid);
- if (_isLiveMode)
- {
- _trtcPusher = new TRTCPusher(logPath, _category);
- _trtcPusher.VideoFrameReceived += OnLocalVideoFrameReceived;
- _trtcPusher.TRTCImageFrameDataReceived += OnOnLocalVideoFrameReceived_2;
- _trtcPusher.EnterRoom(roomInfo);
- Logger.WriteLineInfo($"MessageClient Send BootUp");
- var bootUp = new PipeMessageData(PipeMessage.BootUp, new byte[0]);
- _messageClient?.SendBytes(bootUp.ToBytes());
- }
- else
- {
- _trtcChatRoom = new TRTCChatRoom(logPath);
- _trtcChatRoom.LocalVideoFrameArrived += OnLocalVideoFrameReceived;
- _trtcChatRoom.RemoteVideoFrameArrived += OnRemoteVideoFrameReceived;
- _trtcChatRoom.OnTRTCEnterRoomError += OnEnterRoomError;
- _trtcChatRoom.RemoteUserLeaveRoomArrived += OnRemoteUserLeaveRoomArrived;
- _trtcChatRoom.TryToReconnect += OnTryToReconnect;
- _trtcChatRoom.Enter(roomInfo);
- if (roomInfo.TerminalIsPushing && roomInfo.TerminalRoomId > 0)
- {
- _trtcChatRoom.ConnectTerminalRoom(roomInfo.TerminalRoomId, roomInfo.TerminalId);
- if (roomInfo.IsMultiChannel && !string.IsNullOrEmpty(roomInfo.CameraId))
- {
- _trtcChatRoom.ConnectTerminalCameraRoom(roomInfo.TerminalRoomId, roomInfo.CameraId);
- }
- }
- }
- }
- else
- {
- Process.GetCurrentProcess().Kill();
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Start TrtcClient error:{ex}");
- Process.GetCurrentProcess().Kill();
- }
- }
- protected override void OnExit(ExitEventArgs e)
- {
- try
- {
- Dispose();
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Exit TrtcClient error:{ex}");
- }
- finally
- {
- base.OnExit(e);
- }
- }
- public void Dispose()
- {
- if (!_disposed)
- {
- _tokenSource.Cancel();
- StopPusher();
- StopRTCRoom();
- StopMessagePipe();
- StopLocalVideoPipe();
- StopRemoteVideoPipeClient();
- _disposed = true;
- Process.GetCurrentProcess().Kill();
- }
- }
- #region Start
- private void StartLocalVideoPipe()
- {
- StartLocalVideoPipeClient();
- if (_isUsMachineImage)
- {
- StartLocalVideoPipeServer();
- }
- }
- private void StartLocalVideoPipeClient()
- {
- _localVideoPipeClient = new PipeClient($"{_processId}_LocalVideo");
- _localVideoPipeClient.LogMsgThrow += OnLogMsgThrow;
- Logger.WriteLineInfo($"Create Local Video Pipe Client :{_localVideoPipeClient.PipeName}");
- _localVideoPipeClient.Start();
- }
- private void StartLocalVideoPipeServer()
- {
- _localVideoPipeServer = new PipeServer($"{_processId}_TerminalImage");
- _localVideoPipeServer.DataReceived += OnTerminalVideoDataReceived;
- _localVideoPipeServer.LogMsgThrow += OnLogMsgThrow;
- Logger.WriteLineInfo($"Create Local Video Pipe Server :{_localVideoPipeServer.PipeName}");
- _localVideoPipeServer.StartAndReceive();
- }
- private void StartNewRemoteVideoPipeClient(string userId, int width, int height)
- {
- var remoteVideoPipeClient = new PipeClient($"{_processId}_{userId}_RemoteVideo");
- if (!_remoteVideoPipeClientList.TryAdd(userId, remoteVideoPipeClient))
- {
- Logger.WriteLineError($"Remote Video Pipe Client List Try Add Failed, Id: {userId}");
- return;
- }
- else
- {
- Logger.WriteLineInfo($"MessageClient Send AddRemoteVideoPipe Client :{remoteVideoPipeClient.PipeName},Width:{width},Height:{height}");
- var remoteImageData = new PipeRemoteImageSizeData(userId, width, height);
- var pipeMessageData = new PipeMessageData(PipeMessage.AddRemoteVideoPipe, remoteImageData.ToBytes());
- _messageClient?.SendBytes(pipeMessageData.ToBytes());
- remoteVideoPipeClient.LogMsgThrow += OnLogMsgThrow;
- remoteVideoPipeClient.Start();
- }
- }
- private void StartMessagePipe()
- {
- _messageServer = new PipeServer($"{_processId}_FIS2TRTCClientMessage");
- _messageServer.LogMsgThrow += OnLogMsgThrow;
- _messageServer.DataReceived += OnMessageDataReceived;
- _messageServer.StartAndReceive();
- _messageClient = new PipeClient($"{_processId}_TRTCClient2FISMessage");
- _messageClient.LogMsgThrow += OnLogMsgThrow;
- _messageClient.Start();
- }
- private void StartCheckParentThread(string parentProcessId)
- {
- if (string.IsNullOrEmpty(parentProcessId))
- {
- return;
- }
- if (int.TryParse(parentProcessId, out var pid))
- {
- Task.Run(() =>
- {
- Logger.WriteLineInfo($"Start Check Thread");
- while (true)
- {
- if (_tokenSource.IsCancellationRequested)
- {
- break;
- }
- try
- {
- if (pid == 0)
- {
- Process.GetCurrentProcess().Kill();
- return;
- }
- var existApp = Process.GetProcessById(pid);
- if (existApp == null)
- {
- Logger.WriteLineInfo("Parent process not exit,kill myself!");
- Process.GetCurrentProcess().Kill();
- return;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineInfo($"Check Parent Error:{ex}");
- Process.GetCurrentProcess().Kill();
- }
- finally
- {
- Thread.Sleep(5000);
- }
- }
- }, _tokenSource.Token);
- }
- }
- #endregion Start
- #region Stop
- private void StopMessagePipe()
- {
- if (_messageServer != null)
- {
- _messageServer.DataReceived -= OnMessageDataReceived;
- _messageServer.Dispose();
- _messageServer.LogMsgThrow -= OnLogMsgThrow;
- _messageServer = null;
- }
- if (_messageClient != null)
- {
- _messageClient.Dispose();
- _messageClient.LogMsgThrow -= OnLogMsgThrow;
- _messageClient = null;
- }
- }
- private void StopLocalVideoPipe()
- {
- StopLocalVideoPipeClient();
- StopLocalVideoPipeServer();
- }
- private void StopLocalVideoPipeClient()
- {
- if (_localVideoPipeClient != null)
- {
- Logger.WriteLineInfo($"Close Pipe Client Local Video{_localVideoPipeClient.PipeName}");
- _localVideoPipeClient.Dispose();
- _localVideoPipeClient.LogMsgThrow -= OnLogMsgThrow;
- _localVideoPipeClient = null;
- }
- }
- private void StopLocalVideoPipeServer()
- {
- if (_localVideoPipeServer != null)
- {
- Logger.WriteLineInfo($"Close Pipe Server Local Video{_localVideoPipeServer.PipeName}");
- _localVideoPipeServer.DataReceived -= OnTerminalVideoDataReceived;
- _localVideoPipeServer.Dispose();
- _localVideoPipeServer.LogMsgThrow -= OnLogMsgThrow;
- _localVideoPipeServer = null;
- }
- }
- private void StopRemoteVideoPipeClient()
- {
- foreach (var userId in _remoteVideoPipeClientList.Keys)
- {
- Logger.WriteLineInfo($"Close Pipe Client Remote Video{_remoteVideoPipeClientList[userId].PipeName}");
- _remoteVideoPipeClientList[userId].Dispose();
- _remoteVideoPipeClientList[userId].LogMsgThrow -= OnLogMsgThrow;
- }
- _remoteVideoPipeClientList.Clear();
- _remoteImageList.Clear();
- }
- private void StopRTCRoom()
- {
- if (_trtcChatRoom != null)
- {
- _trtcChatRoom.LocalVideoFrameArrived -= OnLocalVideoFrameReceived;
- _trtcChatRoom.RemoteVideoFrameArrived -= OnRemoteVideoFrameReceived;
- _trtcChatRoom.OnTRTCEnterRoomError -= OnEnterRoomError;
- _trtcChatRoom.RemoteUserLeaveRoomArrived -= OnRemoteUserLeaveRoomArrived;
- _trtcChatRoom.TryToReconnect -= OnTryToReconnect;
- _trtcChatRoom.Exit();
- _trtcChatRoom = null;
- Logger.WriteLineInfo("Exit RTCRoom success");
- }
- }
- private void StopPusher()
- {
- if (_trtcPusher != null)
- {
- _trtcPusher.VideoFrameReceived -= OnLocalVideoFrameReceived;
- _trtcPusher.TRTCImageFrameDataReceived -= OnOnLocalVideoFrameReceived_2;
- _trtcPusher.Dispose();
- _trtcPusher = null;
- Logger.WriteLineInfo("Stop Pusher success");
- }
- }
- #endregion Stop
- private void OnEnterRoomError(object sender, EventArgs e)
- {
- Logger.WriteLineInfo($"MessageClient Send EnterRoomError");
- var pipeMessageData = new PipeMessageData(PipeMessage.EnterRoomError, new byte[0]);
- _messageClient?.SendBytes(pipeMessageData.ToBytes());
- }
- private void OnTryToReconnect(object sender, EventArgs e)
- {
- Logger.WriteLineInfo($"MessageClient Send TryToReconnect");
- var pipeMessageData = new PipeMessageData(PipeMessage.TryToReconnect, new byte[0]);
- _messageClient?.SendBytes(pipeMessageData.ToBytes());
- }
- private void OnRemoteUserLeaveRoomArrived(object sender, RemoteUserLeaveRoomArgs args)
- {
- Logger.WriteLineInfo($"MessageClient Send RemoteUserLeaveRoom,userId:{args.UserId},reason:{args.Reason}");
- var pipeRemoteUserLeaveRoomMessage = new PipeRemoteUserLeaveRoomMessage(args.UserId, args.Reason);
- var pipeMessageData = new PipeMessageData(PipeMessage.RemoteUserLeaveRoom, pipeRemoteUserLeaveRoomMessage.ToBytes());
- _messageClient?.SendBytes(pipeMessageData.ToBytes());
- }
- private void OnTerminalVideoDataReceived(object sender, byte[] e)
- {
- if (e.Length != _terminalHeight * _terminalWidth * 3 / 2)
- {
- Logger.WriteLineError($"OnTerminalVideoDataReceived Error,Byte Length:{e.Length},TerminalHeight:{_terminalHeight},TerminalWidth:{_terminalWidth},ImageSize Should be {_terminalWidth * _terminalHeight * 3 / 2}");
- return;
- }
- _trtcPusher?.SendData((uint)_terminalWidth, (uint)_terminalHeight, e);
- }
- private void OnLocalVideoFrameReceived(object sender, VideoFrameArrivedArgs e)
- {
- if (_localImageHeight != (int)e.Frame.height || _localImageWidth != (int)e.Frame.width)
- {
- _localImageWidth = (int)e.Frame.width;
- _localImageHeight = (int)e.Frame.height;
- Logger.WriteLineInfo($"MessageClient Send ImageSizeChanged,Width:{_localImageWidth},Height:{_localImageHeight}");
- var imageSizeMessage = new PipeMessageData(PipeMessage.ImageSizeChanged, new PipeImageSizeData(_localImageWidth, _localImageHeight).ToBytes());
- _messageClient?.SendBytes(imageSizeMessage.ToBytes());
- }
- if (!_hasReceivedFirstImage)
- {
- Logger.WriteLineInfo($"MessageClient Send FirstFrameReceived");
- var firstImageMessage = new PipeMessageData(PipeMessage.FirstFrameReceived, new byte[0]);
- _messageClient?.SendBytes(firstImageMessage.ToBytes());
- _hasReceivedFirstImage = true;
- }
- if (!_isLiveMode || _isPreviewing)
- {
- _localVideoPipeClient?.SendBytes(e.Frame.data);
- }
- }
- private void OnOnLocalVideoFrameReceived_2(object sender, TRTCImageFrameData e)
- {
- if (_localImageHeight != e.Height || _localImageWidth != e.Width)
- {
- _localImageWidth = e.Width;
- _localImageHeight = e.Height;
- Array.Resize(ref _localBuffer, _localImageHeight * _localImageWidth * 4);
- Logger.WriteLineInfo($"MessageClient Send ImageSizeChanged,Width:{_localImageWidth},Height:{_localImageHeight}");
- var imageSizeMessage = new PipeMessageData(PipeMessage.ImageSizeChanged, new PipeImageSizeData(_localImageWidth, _localImageHeight).ToBytes());
- _messageClient?.SendBytes(imageSizeMessage.ToBytes());
- }
- if (!_hasReceivedFirstImage)
- {
- Logger.WriteLineInfo($"MessageClient Send FirstFrameReceived");
- var firstImageMessage = new PipeMessageData(PipeMessage.FirstFrameReceived, new byte[0]);
- _messageClient?.SendBytes(firstImageMessage.ToBytes());
- _hasReceivedFirstImage = true;
- }
- if (!_isLiveMode || _isPreviewing)
- {
- Marshal.Copy(e.Data, _localBuffer, 0, _localBuffer.Length);
- _localVideoPipeClient?.SendBytes(_localBuffer);
- }
- }
- private string GetArgValue(string arg, string tag)
- {
- var args = arg.Split('=');
- if (args.Count() == 2 && args[0] == tag)
- {
- return args[1];
- }
- return string.Empty;
- }
- private void OnMessageDataReceived(object sender, byte[] e)
- {
- if (e == null)
- {
- return;
- }
- var pipeMessageData = PipeMessageData.FromBytes(e);
- Logger.WriteLineInfo($"Message Pipe Server Receive PipeMessage{pipeMessageData?.PipeMessage}");
- switch (pipeMessageData.PipeMessage)
- {
- case PipeMessage.Exit:
- Dispose();
- break;
- case PipeMessage.ConnectToOtherRoom:
- var connectOtherRoomInfo = PipeConnectOtherRoomMessage.FromBytes(pipeMessageData.MessageData);
- if (connectOtherRoomInfo != null && !connectOtherRoomInfo.UserId.Contains("Auxiliary1"))
- {
- _trtcChatRoom.ConnectTerminalRoom((uint)connectOtherRoomInfo.RoomId, connectOtherRoomInfo.UserId);
- }
- else
- {
- _trtcChatRoom.ConnectTerminalCameraRoom((uint)connectOtherRoomInfo.RoomId, connectOtherRoomInfo.UserId);
- }
- break;
- case PipeMessage.Mute:
- var muteMessage = PipeBoolMessage.FromBytes(pipeMessageData.MessageData);
- if (muteMessage != null)
- {
- if (_trtcChatRoom != null)
- {
- _trtcChatRoom.Mute(muteMessage.Value);
- }
- if (_trtcPusher != null)
- {
- _trtcPusher.SetMute(muteMessage.Value);
- }
- }
- break;
- case PipeMessage.SwitchMic:
- var micMessage = PipeUserIdMessage.FromBytes(pipeMessageData.MessageData);
- if (micMessage != null)
- {
- if (_trtcChatRoom != null)
- {
- _trtcChatRoom.SwitchMic(micMessage.UserId);
- }
- if (_trtcPusher != null)
- {
- _trtcPusher.SwitchMic(micMessage.UserId);
- }
- }
- break;
- case PipeMessage.ImageSizeChanged:
- var imageSizeMessage = PipeImageSizeData.FromBytes(pipeMessageData.MessageData);
- if (_terminalHeight != imageSizeMessage.Height || _terminalWidth != imageSizeMessage.Width)
- {
- _terminalWidth = imageSizeMessage.Width;
- _terminalHeight = imageSizeMessage.Height;
- }
- break;
- case PipeMessage.StartPreview:
- _isPreviewing = true;
- break;
- case PipeMessage.StopPreview:
- _isPreviewing = false;
- break;
- }
- }
- private void OnRemoteVideoFrameReceived(object sender, VideoFrameArrivedArgs e)
- {
- if (string.IsNullOrEmpty(e.UserId))
- {
- return;
- }
- if (!_remoteVideoPipeClientList.ContainsKey(e.UserId))
- {
- StartNewRemoteVideoPipeClient(e.UserId, (int)e.Frame.width, (int)e.Frame.height);
- }
- if (!_remoteImageList.ContainsKey(e.UserId))
- {
- if (_remoteImageList.TryAdd(e.UserId, new PipeRemoteImageSizeData(e.UserId, (int)e.Frame.width, (int)e.Frame.height)))
- {
- SendRemoteImageSizeChanged(e.UserId, (int)e.Frame.width, (int)e.Frame.height);
- }
- }
- if (_remoteImageList[e.UserId].Width != (int)e.Frame.width || _remoteImageList[e.UserId].Height != (int)e.Frame.height)
- {
- _remoteImageList[e.UserId].Width = (int)e.Frame.width;
- _remoteImageList[e.UserId].Height = (int)e.Frame.height;
- SendRemoteImageSizeChanged(e.UserId, (int)e.Frame.width, (int)e.Frame.height);
- }
- _remoteVideoPipeClientList[e.UserId].SendBytes(e.Frame.data);
- }
- private void SendRemoteImageSizeChanged(string userId, int width, int height)
- {
- Logger.WriteLineInfo($"MessageClient Send RemoteImageSizeChanged UserId:{userId},Width:{width},Height:{height}");
- var remoteImageData = new PipeRemoteImageSizeData(userId, width, height);
- var pipeMessageData = new PipeMessageData(PipeMessage.RemoteImageSizeChanged, remoteImageData.ToBytes());
- _messageClient?.SendBytes(pipeMessageData.ToBytes());
- }
- private void OnLogMsgThrow(object sender, LogEventArgs e)
- {
- if (e == null)
- {
- return;
- }
- switch (e.LogType)
- {
- case DeviceLogCategory.Error:
- Logger.WriteLineError(e.Msg);
- break;
- case DeviceLogCategory.Warn:
- Logger.WriteLineWarn(e.Msg);
- break;
- case DeviceLogCategory.Verb:
- Logger.WriteLineVerbose(e.Msg);
- break;
- case DeviceLogCategory.Info:
- default:
- Logger.WriteLineInfo(e.Msg);
- break;
- }
- }
- }
- }
|