using Com.Tencent.Trtc; using System; using System.Collections.Generic; using System.Linq; using Vinno.FIS.TRTCClient.Common.Enum; using Vinno.vCloud.FIS.CrossPlatform.Common; using Vinno.vCloud.FIS.CrossPlatform.Common.Enum; using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo; using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface; namespace Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo.RTC { public class RtcSinglePusherV2 : PusherBase, ILiveVideoPusherV2 { private TRTCPusher _pusher; private IImageSenderV2 _imageSender; private int _videoResolution; public event EventHandler ChannelStateChanged; public RtcSinglePusherV2() { } public bool StartPusher(IExtendedData pushParams, IEnumerable deviceInfos) { try { var rtcParams = pushParams as RtcExtendedData; if (rtcParams == null || rtcParams.AppId == 0 || rtcParams.UserInfos.Count == 0) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RTC Single PusherV2: Extended Data Is Null!"); return false; } if (deviceInfos.Count() == 0) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RTC Single PusherV2: No device set!"); return false; } var userInfo = rtcParams.UserInfos.FirstOrDefault(); var deviceInfo = deviceInfos.FirstOrDefault(x => x.Category == userInfo.Category); if (deviceInfo == null) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single Pusher Category {userInfo.Category} No device found!"); return false; } _videoResolution = GetVideoResolution(userInfo.Width, userInfo.Height); _pusher = new TRTCPusher(); _pusher.FirstFrameSend += OnFirstFrameSend; if (CommonParameter.Instance.IsSonopost) { _pusher.EnterRoom(userInfo.UserId, rtcParams.RoomId, userInfo.UserSign, rtcParams.AppId, _videoResolution, userInfo.VideoFps, userInfo.VideoBitrate, userInfo.MinVideoBitrate, pushParams.IsMute, pushParams.MicDeviceId); if (_videoResolution == TRTCCloudDef.TrtcVideoResolution640480) { _imageSender = new Camera640X480SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera640X480SenderV2"); } else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960540) { _imageSender = new Camera960X540SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera960X540SenderV2"); } else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960720) { _imageSender = new Camera960X720SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera960X720SenderV2"); } else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution1280720) { _imageSender = new Camera1280X720SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera1280X720SenderV2"); } else { _imageSender = new Camera1920X1080SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height, deviceInfo.Category, deviceInfo.Category == EnumLiveChannelCategory.Main ? false : true, false); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Camera1920X1080SenderV2"); } } else { _pusher.EnterRoom(userInfo.UserId, rtcParams.RoomId, userInfo.UserSign, rtcParams.AppId, _videoResolution, userInfo.VideoFps, userInfo.VideoBitrate, userInfo.MinVideoBitrate, pushParams.IsMute, pushParams.MicDeviceId); if (_videoResolution == TRTCCloudDef.TrtcVideoResolution640480) { _imageSender = new Terminal640X480SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1280X720SenderV2"); } else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960540) { _imageSender = new Terminal960X540SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1280X720SenderV2"); } else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution960720) { _imageSender = new Terminal960X720SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1280X720SenderV2"); } else if (_videoResolution == TRTCCloudDef.TrtcVideoResolution1280720) { _imageSender = new Terminal1280X720SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1280X720SenderV2"); } else { _imageSender = new Terminal1920X1080SenderV2(deviceInfo.VideoDeviceId, userInfo.Width, userInfo.Height); CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2:Terminal1920X1080SenderV2"); } } _imageSender.VideoFrameDataReceived += OnVideoFrameDataReceived; _imageSender.PreviewImageFrameDataReceived += OnPreviewImageFrameDataReceived; _imageSender.Start(); return true; } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RTC Single PusherV2 Start Push Error:{ex}"); } return false; } private int GetVideoResolution(int width, int height) { if (width <= 640 && height <= 480) { return TRTCCloudDef.TrtcVideoResolution640480; } else if (width <= 960 && height <= 544) { return TRTCCloudDef.TrtcVideoResolution960540; } else if (width <= 960 && height <= 720) { return TRTCCloudDef.TrtcVideoResolution960720; } else if (width <= 1280 && height <= 720) { return TRTCCloudDef.TrtcVideoResolution1280720; } else { return TRTCCloudDef.TrtcVideoResolution19201080; } } private void OnVideoFrameDataReceived(object sender, CPVideoFrameData e) { _pusher?.SendData(e.Width, e.Height, e.Data); } public void SetMute(bool isMute) { _pusher?.SetMute(isMute); } public void SwitchMic(string micId) { _pusher?.SwitchMic(micId); } public bool StopPusher() { try { if (_pusher != null) { _pusher.FirstFrameSend -= OnFirstFrameSend; _pusher.ExitRoom(); _pusher = null; } if (_imageSender != null) { _imageSender.VideoFrameDataReceived -= OnVideoFrameDataReceived; _imageSender.PreviewImageFrameDataReceived -= OnPreviewImageFrameDataReceived; _imageSender.Stop(); _imageSender = null; } return true; } catch (Exception e) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Rtc Single PusherV2 Stop Pusher Error:{e}"); } return false; } private void OnPreviewImageFrameDataReceived(object sender, ImageFrameData e) { var category = (EnumLiveChannelCategory)sender; CopyPreviewData(category, e); } private void OnFirstFrameSend(object sender, EventArgs e) { ChannelStateChanged?.Invoke(this, new ChannelStateEventArgsV2(EnumLiveChannelCategory.Main, EnumDeviceLiveState.Pushing)); } public bool StartSpeedTest(uint appId, string userId, string userSign) { TRTCPusher pusher = null; try { pusher = new TRTCPusher(); return pusher.StartSpeedTest((int)appId, userId, userSign); } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Rtc Single PusherV2 StartSpeedTest Error:{ex}"); return false; } finally { if (pusher != null) { pusher.StopSpeedTest(); pusher = null; } } } public override void DoDispose() { StopPusher(); base.DoDispose(); } } }