123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- 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 WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.LiveConsultation;
- using WingInterfaceLibrary.Request.Device;
- namespace Vinno.vCloud.Common.FIS.LiveVideos
- {
- internal class LiveVideoPusherManagerV2 : ILiveVideoPusherManagerV2
- {
- private readonly string _token;
- private readonly IDeviceService _deviceService;
- private readonly LiveVideoPusherV2 _liveVideoPusher;
- private readonly List<CPVideoDeviceOutputInfo> _currentVideoDeviceInfoList;
- private readonly string _uniqueId;
- private bool _disposed;
- public bool IsPushing => _liveVideoPusher?.IsPushing ?? false;
- public event EventHandler<ImageFrameData> PreviewImageReceived;
- 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> LiveVideoNotification;
- public LiveVideoPusherManagerV2(string token, ILiveConsultationService liveConsultationService, IDeviceService deviceService, IEducationService educationService, string uniqueId, string deviceModel, string deviceType, string softwareVersion)
- {
- _uniqueId = uniqueId;
- _token = token;
- _deviceService = deviceService;
- _currentVideoDeviceInfoList = new List<CPVideoDeviceOutputInfo>();
- _liveVideoPusher = new LiveVideoPusherV2(token, liveConsultationService, deviceService, educationService, deviceModel, deviceType, softwareVersion);
- _liveVideoPusher.PusherStateChanged += OnPusherStateChanegd;
- _liveVideoPusher.LiveNotification += OnLiveNotification;
- RegisterPusherCreations();
- }
- public List<string> GetBrandList()
- {
- try
- {
- var getBrandRequest = new GetBrandsRequest
- {
- Token = _token,
- };
- return JsonRpcHelper.GetBrands(_deviceService, getBrandRequest);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoPusherManagerV2 GetBrandList Error:{ex}");
- return new List<string>();
- }
- }
- public List<string> GetModelList(string brand)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(brand))
- {
- Logger.WriteLineError($"LiveVideoPusherManagerV2 GetModelList Error:brand is null");
- return new List<string>();
- }
- var getModelsRequest = new GetModelsRequest
- {
- Token = _token,
- Brand = brand,
- };
- return JsonRpcHelper.GetModels(_deviceService, getModelsRequest);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoPusherManagerV2 GetModelList Error:{ex}");
- return new List<string>();
- }
- }
- public DeviceRecommandResolution GetRecommandResolution(string brand, string model)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(brand))
- {
- Logger.WriteLineError($"LiveVideoPusherManagerV2 GetRecommandResolution Error:brand is null");
- return null;
- }
- if (string.IsNullOrWhiteSpace(model))
- {
- Logger.WriteLineError($"LiveVideoPusherManagerV2 GetRecommandResolution Error:model is null");
- return null;
- }
- var syncBrandModelOutputConfigRequest = new SyncBrandModelOutputConfigRequest
- {
- Token = _token,
- Brand = brand,
- Model = model,
- ShortCode = _uniqueId,
- };
- var result = JsonRpcHelper.SyncBrandModelOutputConfig(_deviceService, syncBrandModelOutputConfigRequest);
- if (result == null || result.Count == 0)
- {
- throw new InvalidDataException($"JsonRPCHelper SyncBrandModelOutputConfig Result is null");
- }
- else
- {
- var selected = result.FirstOrDefault(x => x.IsSelect);
- if (selected != null)
- {
- return new DeviceRecommandResolution(brand, null, model, selected.VideoWidth, selected.VideoHeight);
- }
- else
- {
- return new DeviceRecommandResolution(brand, null, model, result[0].VideoWidth, result[0].VideoHeight);
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoPusherManagerV2 GetRecommandResolution Error:{ex}");
- return null;
- }
- }
- private void OnLiveNotification(object sender, LiveNotificationArgs e)
- {
- LiveVideoNotification?.Invoke(this, e);
- }
- public void StartHeartRateKeeper(string heartRateCode, int interval, EnumHeartRateType heartRateType)
- {
- _liveVideoPusher?.StartHeartRateKeeper(heartRateCode, interval, heartRateType);
- }
- public void StopHeartRateKeeper()
- {
- _liveVideoPusher?.StopHeartRateKeeper();
- }
- private void RegisterPusherCreations()
- {
- if (_liveVideoPusher != null)
- {
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtcMerge, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateRTCMergePusherV2());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtcMulti, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateRTCMultiPusherV2());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtcSingle, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateRTCSinglePusherV2());
- _liveVideoPusher.RegisterPusher(EnumPusherType.USRtcMerge, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateUSRTCMergePusherV2());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtmpMerge, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateRTMPMergePusherV2());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtmpMulti, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateRTMPMultiPusherV2());
- _liveVideoPusher.RegisterPusher(EnumPusherType.RtmpSingle, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateRTMPSinglePusherV2());
- _liveVideoPusher.RegisterPusher(EnumPusherType.USRtmpMerge, () => CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2.CreateUSRTMPMergePusherV2());
- }
- }
- public void LiveStateChanged(LiveEventArgs liveEventArgs)
- {
- _liveVideoPusher?.LiveStateChanged(liveEventArgs);
- }
- public void SetIsPaused(bool isPaused)
- {
- _liveVideoPusher?.SetIsPaused(isPaused);
- }
- public void RestartPusher()
- {
- _liveVideoPusher?.ReStartPusher();
- }
- private void OnPusherStateChanegd(object sender, PusherState e)
- {
- PusherStateChanged?.Invoke(this, e);
- }
- public void SetMute(bool isMute)
- {
- _liveVideoPusher?.SetMute(isMute);
- }
- public void SwitchMic(string micId)
- {
- switch (vCloudServerConfig.Instance.LiveProtocolType)
- {
- case TransactionStatusEnum.TRTC:
- _liveVideoPusher?.SwitchMic(micId);
- break;
- default:
- if (_liveVideoPusher != null && _liveVideoPusher.IsPushing)
- {
- _liveVideoPusher.StopPusher(false, true);
- }
- break;
- }
- }
- private void UpdateDeviceResoution(List<CPVideoDeviceOutputInfo> videoDeviceInfos)
- {
- try
- {
- Logger.WriteLineInfo("UpdateDeviceResoution");
- var list = new List<CPVideoDeviceOutputInfo>();
- if (videoDeviceInfos != null)
- {
- foreach (var device in videoDeviceInfos)
- {
- var item = device.Clone() as CPVideoDeviceOutputInfo;
- list.Add(item);
- }
- }
- UpdateLiveChannelInfos(list);
- _liveVideoPusher?.StopPusher(false, true);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"UpdateDeviceResoution Error:{ex}");
- }
- }
- private void UpdateLiveChannelInfos(IEnumerable<CPVideoDeviceOutputInfo> infos)
- {
- try
- {
- bool liveOpened = true;
- var videoDeviceInfoList = new List<VideoDeviceInfo>();
- if (infos != null && infos.Count() > 0)
- {
- foreach (var info in infos)
- {
- videoDeviceInfoList.Add(new VideoDeviceInfo
- {
- VideoDeviceId = info.IdForServer,
- Height = info.OutputHeight,
- Width = info.OutputWidth,
- VideoDeviceSourceType = (VideoDeviceSourceTypeEnum)info.VideoDeviceSourceType,
- });
- }
- }
- else
- {
- liveOpened = false;
- }
- var reportVideoDeviceInfoRequest = new ReportVideoDeviceInfoRequest
- {
- Token = _token,
- VideoDeviceInfos = videoDeviceInfoList,
- LiveOpened = liveOpened,
- };
- ReportVideoDeviceInfoResult result = JsonRpcHelper.ReportVideoDeviceInfo(_deviceService, reportVideoDeviceInfoRequest);
- if (result == null)
- {
- string channelInfos = string.Empty;
- if (infos != null)
- {
- foreach (var info in infos)
- {
- channelInfos += info.ToString();
- }
- }
- throw new InvalidDataException($"JsonRPCHelper ReportVideoDeviceInfoAsync Result is null: {channelInfos}");
- }
- else
- {
- if (result.Success)
- {
- string channelInfos = string.Empty;
- if (result.VideoDeviceOutputInfos != null)
- {
- foreach (var info in result.VideoDeviceOutputInfos)
- {
- channelInfos += $"VideoDeviceId:{info.VideoDeviceId},SourceType:{info.VideoDeviceSourceType},Width:{info.Width},Height:{info.Height},OutputWidth:{info.OutputWidth},OutputHeight:{info.OutputHeight},VideoFps:{info.VideoFps},VideoBitrate:{info.VideoBitrate},MinVideoBitrate:{info.MinVideoBitrate}";
- }
- }
- Logger.WriteLineInfo($"LiveVideoPusherManagerV2 UpdateLiveChannelInfos Sucess: {channelInfos}");
- }
- else
- {
- string channelInfos = string.Empty;
- if (infos != null)
- {
- foreach (var info in infos)
- {
- channelInfos += info.ToString();
- }
- }
- Logger.WriteLineError($"LiveVideoPusherManagerV2 UpdateLiveChannelInfos Fail: {channelInfos}");
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoPusherManagerV2 ReportVideoDeviceInfoAsync Error:{ex} ");
- }
- }
- public void StartPreview(EnumLiveChannelCategory category)
- {
- if (_liveVideoPusher?.LiveVideoPusher is PusherBase pusher)
- {
- pusher.PreviewImageReceived += OnPreviewImageReceived;
- pusher.StartPreview(category);
- }
- }
- public void StartOnlyPreview(EnumLiveChannelCategory category)
- {
- if (_liveVideoPusher?.LiveVideoPusher is PusherBase pusher)
- {
- pusher.StartPreview(category);
- }
- }
- public void StopPreview()
- {
- if (_liveVideoPusher?.LiveVideoPusher 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()
- {
- StopPreview();
- _liveVideoPusher.Dispose();
- _liveVideoPusher.LiveNotification -= OnLiveNotification;//若先注销事件,则Dispose收不到关闭的事件
- _liveVideoPusher.PusherStateChanged -= OnPusherStateChanegd;
- }
- public void UpdateCurrentVideoDeviceInfoList(List<CPVideoDeviceOutputInfo> infos)
- {
- _currentVideoDeviceInfoList.Clear();
- if (infos != null)
- {
- foreach (var info in infos)
- {
- var item = info.Clone() as CPVideoDeviceOutputInfo;
- _currentVideoDeviceInfoList.Add(item);
- }
- }
- _liveVideoPusher.UpdateCurrentVideoDeviceInfoList(infos);
- UpdateDeviceResoution(_currentVideoDeviceInfoList);
- }
- public void ChangeHeartRateCode(string consultationCode)
- {
- _liveVideoPusher?.ChangeConsultationCode(consultationCode);
- }
- public bool StartSpeedTest()
- {
- if (_liveVideoPusher != null)
- {
- return _liveVideoPusher.StartSpeedTest();
- }
- return false;
- }
- ~LiveVideoPusherManagerV2()
- {
- Dispose();
- }
- }
- }
|