123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Threading;
- using Vinno.FIS.TRTCClient.Common.Enum;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.vCloud.FIS.CrossPlatform.Common;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Protocol.Messages.Client;
- using Vinno.vCloud.Protocol.Messages.Client.Live;
- using Vinno.vCloud.Protocol.Messages.Live;
- namespace Vinno.vCloud.Common.FIS.LiveVideos
- {
- internal class LiveVideoPusherManager : ILiveVideoPusherManager
- {
- private readonly string _terminalId;
- private readonly string _terminalName;
- private readonly ManualResetEvent _timingRefreshManualResetEvent = new ManualResetEvent(false);
- private readonly ClientLeaf _leaf;
- private bool _disposed;
- private EnumPusherType _currentPushType;
- private SonopostLiveVideoPusher _liveVideoPusher;
- private readonly List<CPVideoDeviceInfo> _currentVideoDeviceInfoList;
- public bool IsPushing => _liveVideoPusher?.IsPushing ?? false;
- public EnumPusherType CurrentPushType => _currentPushType;
- public event EventHandler<ImageFrameData> PreviewImageReceived;
- public event EventHandler<PusherState> PusherStateChanged;
- public LiveVideoPusherManager(string terminalId, string terminalName, ClientLeaf leaf)
- {
- _terminalId = terminalId;
- _terminalName = terminalName;
- _leaf = leaf;
- _currentPushType = EnumPusherType.RtcMerge;
- _currentVideoDeviceInfoList = new List<CPVideoDeviceInfo>();
- _liveVideoPusher = new SonopostLiveVideoPusher(leaf, terminalId, terminalName);
- _liveVideoPusher.PusherStateChanged += OnPusherStateChanegd;
- Init();
- }
- private void Init()
- {
- RegisterPusherCreations();
- InitCurrentPusherType();
- }
- private void RegisterPusherCreations()
- {
- if (_liveVideoPusher != null)
- {
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtmpMerge, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTMPMergePusher());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtmpMulti, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTMPMultiPusher());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtmpSingle, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTMPSinglePusher());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtcMerge, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTCMergePusher());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtcMulti, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTCMultiPusher());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtcSingle, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTCSinglePusher());
- }
- }
- public void LiveStateChanged(LiveEventArgs liveEventArgs)
- {
- _liveVideoPusher?.LiveStateChanged(liveEventArgs);
- }
- public void PushModeChanged(LiveDataMode pushLiveMode, LiveProtocol liveProtocol)
- {
- SetCurrentPusherType(pushLiveMode, liveProtocol);
- UpdateDeviceResoution(_currentVideoDeviceInfoList);
- //RestartPusher();
- }
- private void SetCurrentPusherType(LiveDataMode pushLiveMode, LiveProtocol liveProtocol)
- {
- if (liveProtocol == LiveProtocol.RTC)
- {
- if (pushLiveMode == LiveDataMode.MergeLive)
- {
- if (_currentVideoDeviceInfoList.Count <= 1)
- {
- _currentPushType = EnumPusherType.RtcSingle;
- }
- else
- {
- _currentPushType = EnumPusherType.RtcMerge;
- }
- }
- else if (pushLiveMode == LiveDataMode.OnlyLive)
- {
- _currentPushType = EnumPusherType.RtcMulti;
- }
- else
- {
- throw new Exception($"Live Video Pusher Manager SetCurrentPusherType Error:Unknown Pusher Type:{pushLiveMode}|{liveProtocol}");
- }
- }
- else if (liveProtocol == LiveProtocol.Rtmp)
- {
- if (pushLiveMode == LiveDataMode.MergeLive)
- {
- if (_currentVideoDeviceInfoList.Count <= 1)
- {
- _currentPushType = EnumPusherType.RtmpSingle;
- }
- else
- {
- _currentPushType = EnumPusherType.RtmpMerge;
- }
- }
- else if (pushLiveMode == LiveDataMode.OnlyLive)
- {
- _currentPushType = EnumPusherType.RtmpMulti;
- }
- else
- {
- throw new Exception($"Live Video Pusher Manager SetCurrentPusherType Error:Unknown Pusher Type:{pushLiveMode}|{liveProtocol}");
- }
- }
- else
- {
- throw new Exception($"Live Video Pusher Manager SetCurrentPusherType Error:Unknown Pusher Type:{pushLiveMode}|{liveProtocol}");
- }
- Logger.WriteLineInfo($"Live VideoManager Current Push Type:{_currentPushType}");
- }
- public void RestartPusher()
- {
- _liveVideoPusher?.ReStartPusher();
- }
- private void OnPusherStateChanegd(object sender, PusherState e)
- {
- PusherStateChanged?.Invoke(this, e);
- }
- public void SetIsPaused(bool isPaused)
- {
- _liveVideoPusher?.SetIsPaused(isPaused);
- }
- private void UpdateDeviceResoution(List<CPVideoDeviceInfo> videoDeviceInfos)
- {
- try
- {
- Logger.WriteLineInfo("LiveVideo PusherManager UpdateDeviceResoution");
- var list = new List<CPVideoDeviceInfo>();
- if (videoDeviceInfos != null)
- {
- foreach (var device in videoDeviceInfos)
- {
- var item = device.Clone() as CPVideoDeviceInfo;
- var cability = CrossPlatformHelper.Instance.HardwareDetector.GetMaxResolutionUnderTheSpecial(item.Id, item.Width, item.Height, _currentPushType, device.Category);
- if (cability != null)
- {
- item.Width = cability.Width;
- item.Height = cability.Height;
- }
- list.Add(item);
- }
- }
- UpdateLiveChannelInfos(list);
- _liveVideoPusher?.StopPusher();
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"UpdateDeviceResoution Error:{ex}");
- }
- }
- public void SetMute(bool isMute)
- {
- _liveVideoPusher?.SetMute(isMute);
- }
- public List<string> GetBrandList()
- {
- var brandList = new List<string>();
- using (var request = MessagePool.GetMessage<GetPushLiveConfigRequest>())
- {
- request.TerminalId = _terminalId;
- var resultMessage = _leaf.Send(request);
- var result = GetPushLiveConfigResult.Convert(resultMessage);
- if (result != null)
- {
- foreach (var brand in result.PushLiveConfigs)
- {
- brandList.Add(brand.Brand);
- }
- }
- }
- return brandList;
- }
- public List<string> GetModelList(string brand)
- {
- if (string.IsNullOrWhiteSpace(brand))
- {
- Logger.WriteLineError($"LiveVideoPusherManager GetModelList Error:brand is null");
- return new List<string>();
- }
- var modelList = new List<string>();
- using (var request = MessagePool.GetMessage<GetPushLiveConfigRequest>())
- {
- request.TerminalId = _terminalId;
- var resultMessage = _leaf.Send(request);
- var result = GetPushLiveConfigResult.Convert(resultMessage);
- if (result != null)
- {
- var selectedBrand = result.PushLiveConfigs.FirstOrDefault(x => x.Brand == brand);
- if (selectedBrand != null && selectedBrand.PushLiveModelConfigs != null)
- {
- foreach (var model in selectedBrand.PushLiveModelConfigs)
- {
- modelList.Add(model.Model);
- }
- }
- }
- }
- return modelList;
- }
- public DeviceRecommandResolution GetRecommandResolution(string brand, string model)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(brand))
- {
- Logger.WriteLineError($"LiveVideoPusherManager GetRecommandResolution Error:brand is null");
- return null;
- }
- if (string.IsNullOrWhiteSpace(model))
- {
- Logger.WriteLineError($"LiveVideoPusherManager GetRecommandResolution Error:model is null");
- return null;
- }
- using (var request = MessagePool.GetMessage<GetPushLiveConfigRequest>())
- {
- request.TerminalId = _terminalId;
- var resultMessage = _leaf.Send(request);
- var result = GetPushLiveConfigResult.Convert(resultMessage);
- if (result != null)
- {
- var selectedBrand = result.PushLiveConfigs.FirstOrDefault(x => x.Brand == brand);
- if (selectedBrand != null && selectedBrand.PushLiveModelConfigs != null)
- {
- var selectedModel = selectedBrand.PushLiveModelConfigs.FirstOrDefault(x => x.Model == model);
- if (selectedModel != null)
- {
- return new DeviceRecommandResolution(brand, null, model, selectedModel.RecommendWidth, selectedModel.RecommendHeight);
- }
- else
- {
- Logger.WriteLineError($"LiveVideoPusherManager GetRecommandResolution Error:Model Didn't find RecommandResolution");
- return null;
- }
- }
- else
- {
- Logger.WriteLineError($"LiveVideoPusherManager GetRecommandResolution Error:Brand Didn't find");
- return null;
- }
- }
- }
- return null;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoPusherManager GetRecommandResolution Error:{ex}");
- return null;
- }
- }
- private void InitCurrentPusherType()
- {
- var brandList = new List<DeviceBrandInfo>();
- using (var request = MessagePool.GetMessage<GetPushLiveConfigRequest>())
- {
- request.TerminalId = _terminalId;
- var resultMessage = _leaf.Send(request);
- var result = GetPushLiveConfigResult.Convert(resultMessage);
- if (result != null)
- {
- SetCurrentPusherType(result.PushLiveMode, result.LiveProtocol);
- }
- }
- }
- private void UpdateLiveChannelInfos(IEnumerable<CPVideoDeviceInfo> infos)
- {
- var version = Assembly.GetExecutingAssembly().GetName().Version;
- using (var request = MessagePool.GetMessage<NewUpdateTerminalMultiRelatedInfoRequest>())
- {
- request.TerminalName = _terminalName;
- request.Version = version.ToString();
- var terminalChannels = new List<TerminalChannelInfoMessage>();
- foreach (var info in infos)
- {
- Logger.WriteLineInfo($"Update Channel Info:{info}");
- var channelMessage = new TerminalChannelInfoMessage();
- channelMessage.Width = info.Width;
- channelMessage.Height = info.Height;
- channelMessage.Name = info.Category.ToString();
- channelMessage.Enable = info.IsEnable && info.IsAvailable;
- channelMessage.Fps = info.FrameRate;
- channelMessage.Bitrate = info.Bitrate;
- channelMessage.Brand = info.Brand;
- channelMessage.ModelId = info.ModelId;
- channelMessage.Category = (LiveChannelCategory)info.Category;
- terminalChannels.Add(channelMessage);
- }
- request.TerminalChannels = terminalChannels;
- var result = _leaf.Send(request);
- var resultMsg = ResultMessage.Convert(result);
- string channelInfos = string.Empty;
- if (infos != null)
- {
- foreach (var info in infos)
- {
- channelInfos += info.ToString();
- }
- }
- if (resultMsg == CCR.OK)
- {
- Logger.WriteLineInfo($"UpdateLiveChannelInfos Success! {channelInfos}");
- }
- else
- {
- Logger.WriteLineError($"UpdateLiveChannelInfos Fail! {channelInfos}");
- }
- }
- }
- public void StartPreview(EnumLiveChannelCategory category)
- {
- if (_liveVideoPusher?.LivePusher is PusherBase pusher)
- {
- pusher.PreviewImageReceived += OnPreviewImageReceived;
- pusher.StartPreview(category);
- }
- }
- public void StopPreview()
- {
- if (_liveVideoPusher?.LivePusher is PusherBase pusher)
- {
- pusher.PreviewImageReceived -= OnPreviewImageReceived;
- pusher.StopPreview();
- }
- }
- private void OnPreviewImageReceived(object sender, ImageFrameData e)
- {
- PreviewImageReceived?.Invoke(sender, e);
- }
- public void Dispose()
- {
- if (!_disposed)
- {
- DoDispose();
- GC.SuppressFinalize(this);
- _disposed = true;
- }
- }
- public void DoDispose()
- {
- _timingRefreshManualResetEvent.Set();
- StopPreview();
- if (_liveVideoPusher != null)
- {
- _liveVideoPusher.PusherStateChanged -= OnPusherStateChanegd;
- _liveVideoPusher.Dispose();
- _liveVideoPusher = null;
- }
- }
- public void UpdateCurrentVideoDeviceInfoList(List<CPVideoDeviceInfo> infos)
- {
- _currentVideoDeviceInfoList.Clear();
- if (infos != null)
- {
- foreach (var info in infos)
- {
- var item = info.Clone() as CPVideoDeviceInfo;
- _currentVideoDeviceInfoList.Add(item);
- }
- }
- _liveVideoPusher.UpdateCurrentVideoDeviceInfoList(infos);
- if (_currentPushType == EnumPusherType.RtcSingle || _currentPushType == EnumPusherType.RtcMerge)
- {
- PushModeChanged(LiveDataMode.MergeLive, LiveProtocol.RTC);
- }
- else if (_currentPushType == EnumPusherType.RtcMulti)
- {
- PushModeChanged(LiveDataMode.OnlyLive, LiveProtocol.RTC);
- }
- else if (_currentPushType == EnumPusherType.RtmpSingle || _currentPushType == EnumPusherType.RtmpMerge)
- {
- PushModeChanged(LiveDataMode.MergeLive, LiveProtocol.Rtmp);
- }
- else if (_currentPushType == EnumPusherType.RtmpMulti)
- {
- PushModeChanged(LiveDataMode.OnlyLive, LiveProtocol.Rtmp);
- }
- }
- ~LiveVideoPusherManager()
- {
- Dispose();
- }
- }
- }
|