123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.FIS.TRTCClient.Common.Enum;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS.Helper;
- 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;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.LiveConsultation;
- using WingInterfaceLibrary.Request.Device;
- namespace Vinno.vCloud.Common.FIS.LiveVideos
- {
- public class LiveVideoPusherV2 : IDisposable
- {
- private readonly object _locker = new object();
- private readonly object _currentDeviceInfolocker = new object();
- private readonly ConcurrentDictionary<EnumPusherType, Func<ILiveVideoPusherV2>> _pusherCreations;
- private readonly List<CPVideoDeviceOutputInfo> _currentVideoDeviceInfoList;
- private readonly string _token;
- private readonly ILiveConsultationService _liveConsultationService;
- private readonly IDeviceService _deviceService;
- private readonly IEducationService _educationService;
- private readonly string _deviceModel;
- private readonly string _deviceType;
- private readonly string _softwareVersion;
- private ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
- private IExtendedData _currentExtendData;
- private EnumPusherType _currentPushType;
- private ILiveVideoPusherV2 _pusher;
- private bool _hasReportedStatus;
- private int _rtcRoomNo;
- private PushHeartRateKeeperV2 _pushHeartRateKeeper;
- private bool _isPaused;
- /// <summary>
- /// 是否正在推流
- /// </summary>
- public bool IsPushing { get; private set; }
- public ILiveVideoPusherV2 LiveVideoPusher => _pusher;
- /// <summary>
- /// 推流状态改变时触发,主要给预览用
- /// </summary>
- public event EventHandler<PusherState> PusherStateChanged;
- /// <summary>
- /// The event to notificate the US to show or hide the live video out put window
- /// </summary>
- public event EventHandler<LiveNotificationArgs> LiveNotification;
- public LiveVideoPusherV2(string token, ILiveConsultationService liveConsultationService, IDeviceService deviceService, IEducationService educationService, string deviceModel, string deviceType, string softwareVersion)
- {
- _token = token;
- _liveConsultationService = liveConsultationService;
- _deviceService = deviceService;
- _educationService = educationService;
- _deviceModel = deviceModel;
- _deviceType = deviceType;
- _softwareVersion = softwareVersion;
- _currentVideoDeviceInfoList = new List<CPVideoDeviceOutputInfo>();
- _pusherCreations = new ConcurrentDictionary<EnumPusherType, Func<ILiveVideoPusherV2>>();
- }
- /// <summary>
- /// 注册具体推流类
- /// </summary>
- /// <param name="type">推流类型</param>
- /// <param name="pusher">推流类的创建方法</param>
- public void RegisterPusher(EnumPusherType type, Func<ILiveVideoPusherV2> pusher)
- {
- _pusherCreations.TryAdd(type, pusher);
- }
- public void UpdateCurrentVideoDeviceInfoList(List<CPVideoDeviceOutputInfo> infos)
- {
- lock (_currentDeviceInfolocker)
- {
- _currentVideoDeviceInfoList.Clear();
- foreach (var info in infos)
- {
- var item = info.Clone() as CPVideoDeviceOutputInfo;
- _currentVideoDeviceInfoList.Add(item);
- }
- }
- }
- public void SetIsPaused(bool isPaused)
- {
- Logger.WriteLineInfo($"LiveVideoPusherV2 Set IsPaused:{isPaused}");
- _isPaused = isPaused;
- if (isPaused)
- {
- StopPusher(isPaused);
- }
- }
- public void LiveStateChanged(LiveEventArgs e)
- {
- try
- {
- if (e.IsLive && IsPushing)
- {
- Logger.WriteLineError($"LiveVideoPusherV2 LiveStateChanged Error,it is already pushing.");
- return;
- }
- if (e.IsLive)
- {
- _currentPushType = ConvertToPusherType(e);
- Logger.WriteLineInfo($"LiveVideoPusherV2 Current Push Type:{_currentPushType}");
- InitPusher(_currentPushType);
- StartPusher(e.ExtendedData);
- }
- else
- {
- StopPusher();
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoPusherV2 Receive Live State Error:{ex}");
- }
- }
- public void SetMute(bool isMute)
- {
- lock (_locker)
- {
- _pusher?.SetMute(isMute);
- }
- }
- public void SwitchMic(string micId)
- {
- lock (_locker)
- {
- _pusher?.SwitchMic(micId);
- }
- }
- /// <summary>
- /// 开始推流
- /// </summary>
- /// <param name="pushParams"></param>
- protected virtual void StartPusher(IExtendedData pushParams)
- {
- PusherStateChanged?.Invoke(this, PusherState.Preparing);
- try
- {
- lock (_locker)
- {
- _hasReportedStatus = false;
- _manualResetEvent.Reset();
- _pusher.ChannelStateChanged += OnChannelStateChanged;
- var success = _pusher.StartPusher(pushParams, _currentVideoDeviceInfoList);
- if (success)
- {
- Task.Run(ReportLiveStateTask);
- Logger.WriteLineInfo("LiveVideoPusherV2 Start Pusher Success!");
- _currentExtendData = pushParams;
- IsPushing = true;
- LiveVideoStatusChecker.Instance.IsPushing = true;
- if (pushParams is RtcExtendedData rtcExtendedData)
- {
- _rtcRoomNo = rtcExtendedData.RoomId;
- if (!CommonParameter.Instance.IsSonopost)
- {
- var deviceInfo = rtcExtendedData.UserInfos.FirstOrDefault(x => x.Category == EnumLiveChannelCategory.Main);
- if (deviceInfo != null && deviceInfo.Width > 0 && deviceInfo.Height > 0)
- {
- LiveNotification?.Invoke(this, new LiveNotificationArgs(true, deviceInfo.Width, deviceInfo.Height));
- }
- }
- }
- else if (pushParams is RtmpExtendedData rtmpExtendedData)
- {
- _rtcRoomNo = 0;
- if (!CommonParameter.Instance.IsSonopost)
- {
- var deviceInfo = rtmpExtendedData.UserInfos.FirstOrDefault(x => x.Category == EnumLiveChannelCategory.Main);
- if (deviceInfo != null && deviceInfo.Width > 0 && deviceInfo.Height > 0)
- {
- LiveNotification?.Invoke(this, new LiveNotificationArgs(true, deviceInfo.Width, deviceInfo.Height));
- }
- }
- }
- }
- else
- {
- _hasReportedStatus = true;
- UpdateChannelState(DeviceLiveStateEnum.Error, "Terminal Start Pushing Failed");
- Logger.WriteLineError("LiveVideoPusherV2 Start Pusher Fail!");
- }
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"LiveVideoPusherV2 Start Pusher Error:{e}");
- }
- PusherStateChanged?.Invoke(this, PusherState.Prepared);
- }
- private void ReportLiveStateTask()
- {
- try
- {
- _manualResetEvent.WaitOne(30000);//30秒钟内没有上报,就认为是推流失败
- if (!_hasReportedStatus)
- {
- UpdateChannelState(DeviceLiveStateEnum.Error, "Start Pushing Error");
- _hasReportedStatus = true;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"ReportLiveStateTask Error:{ex}");
- }
- }
- /// <summary>
- /// 结束推流
- /// </summary>
- public void StopPusher(bool isPaused = false, bool needInform = false)
- {
- PusherStateChanged?.Invoke(this, PusherState.Preparing);
- try
- {
- lock (_locker)
- {
- if (_pusher == null)
- return;
- _hasReportedStatus = true;
- _manualResetEvent.Set();
- _pusher.ChannelStateChanged -= OnChannelStateChanged;
- var success = _pusher.StopPusher();
- if (success)
- {
- Logger.WriteLineInfo("LiveVideoPusherV2 Stop Pusher Success!");
- StopHeartRateKeeper();
- _pusher.Dispose();
- _pusher = null;
- IsPushing = false;
- LiveVideoStatusChecker.Instance.IsPushing = false;
- }
- else
- {
- Logger.WriteLineError("LiveVideoPusherV2 Stop Pusher Fail!");
- }
- UpdateChannelState(DeviceLiveStateEnum.Closed, "Terminal Close Pushing");
- if (!CommonParameter.Instance.IsSonopost)
- {
- LiveNotification?.Invoke(this, new LiveNotificationArgs(false, 0, 0));
- }
- if (needInform)
- {
- Task.Run(() =>
- {
- LiveVideoStatusChecker.Instance.CurrentMachineConsultationStopPushingEvent?.Invoke(this, EventArgs.Empty);
- });
- }
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"LiveVideoPusherV2 Stop Pusher Error:{e}");
- }
- if (!isPaused)
- {
- PusherStateChanged?.Invoke(this, PusherState.Prepared);
- }
- }
- /// <summary>
- /// 重新推流
- /// </summary>
- public void ReStartPusher()
- {
- if (!IsPushing)
- {
- return;
- }
- Logger.WriteLineInfo("LiveVideoPusherV2 Restart Pusher");
- StopPusher();
- InitPusher(_currentPushType);
- StartPusher(_currentExtendData);
- }
- private void OnChannelStateChanged(object sender, ChannelStateEventArgsV2 e)
- {
- try
- {
- Logger.WriteLineInfo($"LiveVideoPusherV2 Update Channel State:{e.Category}_{e.State}");
- if (!_hasReportedStatus)
- {
- UpdateChannelState((DeviceLiveStateEnum)e.State, "Terminal Start Pushing");
- _hasReportedStatus = true;
- _manualResetEvent.Set();
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoPusherV2 Update Channel State Error:{ex}");
- }
- }
- public void UpdateChannelState(DeviceLiveStateEnum state, string message)
- {
- try
- {
- if (CommonParameter.Instance.IsFeatureReleased)
- {
- Logger.WriteLineError($"UpdateChannelState Skipped,because the feature released");
- return;
- }
- var reportLiveStateRequest = new ReportLiveStateRequest
- {
- Token = _token,
- RoomNo = _rtcRoomNo,
- LiveState = state,
- Message = message
- };
- bool result = JsonRpcHelper.ReportLiveState(_deviceService, reportLiveStateRequest);
- if (result)
- {
- Logger.WriteLineInfo($"JsonRPCHelper ReportLiveState Sucess: State:{state},Message:{message}");
- }
- else
- {
- Logger.WriteLineError($"JsonRPCHelper ReportLiveState Fail: State:{state},Message:{message}");
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoPusherV2 UpdateChannelState Error:{ex}");
- }
- }
- public void StartHeartRateKeeper(string heartRateCode, int interval, EnumHeartRateType heartRateType)
- {
- lock (_locker)
- {
- if (!IsPushing)
- {
- Logger.WriteLineError($"LiveVideoPusherV2 Start PushHeartRateKeeper Fail,Because it isn't Pushing");
- return;
- }
- if (string.IsNullOrWhiteSpace(heartRateCode))
- {
- Logger.WriteLineError($"LiveVideoPusherV2 Start PushHeartRateKeeper Fail,Because heartRateCode is null");
- return;
- }
- if (_pushHeartRateKeeper != null)
- {
- Logger.WriteLineError($"LiveVideoPusherV2 Start PushHeartRateKeeper Fail,Because it is busy");
- return;
- }
- else
- {
- if (_currentExtendData != null)
- {
- _currentExtendData.HeartRateCode = heartRateCode;
- }
- switch (heartRateType)
- {
- case EnumHeartRateType.LiveConsultation:
- _pushHeartRateKeeper = new PushHeartRateKeeperV2(_token, heartRateCode, interval, _liveConsultationService);
- LiveVideoStatusChecker.Instance.IsConsultationLiving = true;
- LiveVideoStatusChecker.Instance.CurrentConsultationCode = heartRateCode;
- break;
- case EnumHeartRateType.Education:
- _pushHeartRateKeeper = new PushHeartRateKeeperV2(_token, heartRateCode, interval, _educationService);
- LiveVideoStatusChecker.Instance.IsEducationLiving = true;
- break;
- }
- if (_pushHeartRateKeeper != null)
- {
- _pushHeartRateKeeper.Offlined += OnOfflined;
- _pushHeartRateKeeper.Start();
- }
- Logger.WriteLineInfo($"LiveVideoPusherV2 Start PushHeartRateKeeper,HeartRateType:{heartRateType}");
- }
- }
- }
- public void StopHeartRateKeeper()
- {
- lock (_locker)
- {
- if (_pushHeartRateKeeper != null)
- {
- _pushHeartRateKeeper.Offlined -= OnOfflined;
- _pushHeartRateKeeper.Stop();
- _pushHeartRateKeeper = null;
- Logger.WriteLineInfo("LiveVideoPusherV2 Stop PushHeartRateKeeper");
- }
- }
- LiveVideoStatusChecker.Instance.IsConsultationLiving = false;
- LiveVideoStatusChecker.Instance.IsEducationLiving = false;
- LiveVideoStatusChecker.Instance.CurrentConsultationCode = null;
- }
- private void OnOfflined(object sender, EventArgs e)
- {
- Logger.WriteLineInfo($"LiveVideoPusherV2 Pusher is offline");
- }
- private void InitPusher(EnumPusherType type)
- {
- lock (_locker)
- {
- if (_pusherCreations.TryGetValue(type, out var creation))
- {
- if (_pusher != null)
- {
- StopPusher();
- }
- _pusher = creation.Invoke();
- }
- else
- {
- throw new ArgumentException($"LiveVideoPusherV2 Not support Mode {type}");
- }
- }
- }
- private EnumPusherType ConvertToPusherType(LiveEventArgs liveEventArgs)
- {
- if (liveEventArgs.Protocol == EnumLiveProtocol.RTC)
- {
- var rtcExtendedData = liveEventArgs.ExtendedData as RtcExtendedData;
- if (liveEventArgs.PushMode == EnumLiveDataMode.MergeLive)
- {
- if (liveEventArgs.ExtendedData.MergeType == EnumMergeType.Merge1280X720)
- {
- return EnumPusherType.USRtcMerge;
- }
- else if (liveEventArgs.ExtendedData.MergeType == EnumMergeType.Merge1920X1080)
- {
- return EnumPusherType.RtcMerge;
- }
- }
- else if (liveEventArgs.PushMode == EnumLiveDataMode.OnlyLive)
- {
- if (rtcExtendedData.UserInfos.Count() <= 1)
- {
- return EnumPusherType.RtcSingle;
- }
- else
- {
- return EnumPusherType.RtcMulti;
- }
- }
- }
- else if (liveEventArgs.Protocol == EnumLiveProtocol.Rtmp)
- {
- var rtmpExtendedData = liveEventArgs.ExtendedData as RtmpExtendedData;
- if (liveEventArgs.PushMode == EnumLiveDataMode.MergeLive)
- {
- if (liveEventArgs.ExtendedData.MergeType == EnumMergeType.Merge1280X720)
- {
- return EnumPusherType.USRtmpMerge;
- }
- else if (liveEventArgs.ExtendedData.MergeType == EnumMergeType.Merge1920X1080)
- {
- return EnumPusherType.RtmpMerge;
- }
- }
- else if (liveEventArgs.PushMode == EnumLiveDataMode.OnlyLive)
- {
- if (rtmpExtendedData.UserInfos.Count() <= 1)
- {
- return EnumPusherType.RtmpSingle;
- }
- else
- {
- return EnumPusherType.RtmpMulti;
- }
- }
- }
- throw new ArgumentException("LiveVideoPusherV2 Unknown Pusher type");
- }
- public void Dispose()
- {
- StopPusher();
- _pusherCreations.Clear();
- }
- public void ChangeConsultationCode(string consultationCode)
- {
- _pushHeartRateKeeper?.SetHeartRateCode(consultationCode);
- if (_currentExtendData != null)
- {
- _currentExtendData.HeartRateCode = consultationCode;
- }
- if (LiveVideoStatusChecker.Instance.IsConsultationLiving)
- {
- LiveVideoStatusChecker.Instance.CurrentConsultationCode = consultationCode;
- }
- }
- public bool StartSpeedTest()
- {
- try
- {
- var createLiveRoomInfoRequest = new CreateLiveRoomInfoRequest
- {
- DeviceModel = _deviceModel,
- DeviceType = _deviceType,
- DeviceUniqueCode = CommonParameter.Instance.MachineId,
- SoftwareVersion = _softwareVersion,
- };
- var result = JsonRpcHelper.CreateLiveRoomInfo(_deviceService, createLiveRoomInfoRequest);
- if (result == null)
- {
- throw new InvalidDataException($"JsonRPCHelper CreateLiveRoomInfo Result is null");
- }
- else
- {
- var pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateRTCSinglePusherV2();
- return pusher.StartSpeedTest((uint)result.AppId, result.UserCode, result.UserSign);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoPusherV2 StartSpeedTest Fail:{ex}");
- return false;
- }
- }
- }
- }
|