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.RTMP { public class RtmpSinglePusherV2 : PusherBase, ILiveVideoPusherV2 { private DsRtmpPusher _pusher; public event EventHandler ChannelStateChanged; public bool StartPusher(IExtendedData pushParams, IEnumerable deviceInfos) { try { var rtmpParams = pushParams as RtmpExtendedData; if (rtmpParams == null || rtmpParams.UserInfos.Count == 0) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RtmpSinglePusherV2 Extended Data Is Null!"); return false; } var url = rtmpParams.UserInfos.FirstOrDefault()?.PushUrl; if (string.IsNullOrEmpty(url)) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RtmpSinglePusherV2 Push Url Is Null!"); return false; } if (deviceInfos.Count() == 0) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError("RtmpSinglePusherV2 Push not have device info!"); return false; } var userInfo = rtmpParams.UserInfos.FirstOrDefault(); var deviceInfo = deviceInfos.FirstOrDefault(x => x.Category == userInfo.Category); if (deviceInfo == null) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtmpSinglePusherV2 Category {userInfo.Category} No device found!"); return false; } var device = (CPVideoDeviceOutputInfo)deviceInfo.Clone(); _pusher = new DsRtmpPusher(device, url, pushParams.MicDeviceId, pushParams.IsMute, userInfo.Width, userInfo.Height); _pusher.LiveStatusChanged += OnLiveStatusChanged; _pusher.ImageFrameReceived += OnImageFrameReceived; _pusher.Start(); return true; } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Start Rtmp Single Push Error:{ex}"); } return false; } public void SetMute(bool isMute) { _pusher?.SetMute(isMute); } public bool StopPusher() { try { if (_pusher != null) { _pusher.LiveStatusChanged -= OnLiveStatusChanged; _pusher.ImageFrameReceived -= OnImageFrameReceived; _pusher.Stop(); } return true; } catch (Exception ex) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Stop Rtmp Single Push Error:{ex}"); } return false; } private void OnImageFrameReceived(object sender, ImageFrameData e) { var category = (EnumLiveChannelCategory)sender; CopyPreviewData(category, e); } private void OnLiveStatusChanged(object sender, EnumLiveStatus e) { if (e == EnumLiveStatus.Connected) { var category = (EnumLiveChannelCategory)sender; ChannelStateChanged?.Invoke(this, new ChannelStateEventArgsV2(category, EnumDeviceLiveState.Pushing)); } } public override void DoDispose() { if (_pusher != null) { _pusher.Dispose(); _pusher = null; } base.DoDispose(); } public void SwitchMic(string micId) { } public bool StartSpeedTest(uint appId, string userId, string userSign) { CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtmpSinglePusher Doesn't support StartSpeedTest"); return false; } } }