123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- 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.Enum;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Protocol.Messages.Client.Live;
- using Vinno.vCloud.Protocol.Messages.Live;
- namespace Vinno.vCloud.Common.FIS.LiveVideos
- {
- internal class LiveVideoForSonopost : ILiveVideo
- {
- private readonly object _operateLocker = new object();
- private readonly object _locker = new object();
- private readonly string _terminalId;
- private readonly string _terminalName;
- private readonly ClientLeaf _leaf;
- private readonly ILiveVideoPusherManager _liveVideoPusherManager;
- private readonly IPreviewManager _previewManager;
- private readonly List<CPVideoDeviceInfo> _currentVideoDeviceInfoList;
- private bool _firstCommit;
- private bool _disposed;
- private string _connectionUrl;
- private byte[] _previewBuffer;
- private int _cameraPreviewWidth;
- private int _cameraPreviewHeight;
- private string _micDeviceId;
- private string _oldPreviewCameraId;
- private bool _isMute;
- private bool _isPaused;
- private RainbowImageDetectConfig _rainbowImageDetectConfig;
- public event EventHandler<LiveProtocol> LiveProtocolChanged;
- public event EventHandler<LiveNotificationArgs> LiveNotification;
- /// <summary>
- /// Raised when camera Preview Excute
- /// </summary>
- public event EventHandler<byte[]> PreviewCameraCaptured;
- /// <summary>
- /// Raised when Camera Preview Image Size Changed
- /// </summary>
- public event EventHandler<ImageSize> CameraPreviewImageSizeChanged;
- public LiveVideoForSonopost(string terminalId, string terminalName, ClientLeaf leaf)
- {
- _terminalId = terminalId;
- _terminalName = terminalName;
- _firstCommit = true;
- _leaf = leaf;
- _leaf.MessageArrived += OnMessageArrived;
- _currentVideoDeviceInfoList = new List<CPVideoDeviceInfo>();
- _liveVideoPusherManager = new LiveVideoPusherManager(terminalId, terminalName, leaf);
- _previewManager = new PreviewManager(_liveVideoPusherManager);
- _previewManager.PreviewImageReceived += OnPreviewImageReceived;
- RainbowImageDetector.Instance.IsPaused += OnIsPaused;
- }
- private void OnPreviewImageReceived(object sender, ImageFrameData e)
- {
- if (e == null)
- {
- return;
- }
- if (_cameraPreviewHeight != e.Height || _cameraPreviewWidth != e.Width)
- {
- _cameraPreviewHeight = e.Height;
- _cameraPreviewWidth = e.Width;
- CameraPreviewImageSizeChanged?.Invoke(this, new ImageSize(_cameraPreviewWidth, _cameraPreviewHeight));
- }
- if (_previewBuffer == null)
- {
- _previewBuffer = new byte[e.Size];
- }
- else if (_previewBuffer.Length != e.Size)
- {
- Array.Resize(ref _previewBuffer, e.Size);
- }
- Marshal.Copy(e.Data, _previewBuffer, 0, e.Size);
- PreviewCameraCaptured?.Invoke(this, _previewBuffer);
- }
- private void OnMessageArrived(object sender, Message e)
- {
- var startLiveNotification = NewStartLiveNotification.Convert(e);
- if (startLiveNotification != null)
- {
- HandleNewStartLiveNotification(startLiveNotification);
- }
- var closeLiveNotification = CloseLiveNotification.Convert(e);
- if (closeLiveNotification != null)
- {
- HandleCloseLiveNotification(closeLiveNotification);
- }
- var configNotification = TerminalPushLiveConfigNotification.Convert(e);
- if (configNotification != null)
- {
- HandleTerminalPushLiveConfigNotification(configNotification);
- }
- }
- private void HandleTerminalPushLiveConfigNotification(TerminalPushLiveConfigNotification configNotification)
- {
- if (_terminalId == configNotification.TerminalId)
- {
- lock (_operateLocker)
- {
- Logger.WriteLineInfo("Receive TerminalPushLiveConfigNotification");
- _liveVideoPusherManager.PushModeChanged(configNotification.PushLiveMode, configNotification.LiveProtocol);
- }
- }
- }
- private void HandleCloseLiveNotification(CloseLiveNotification closeLiveNotification)
- {
- lock (_operateLocker)
- {
- if (_isPaused)
- {
- Logger.WriteLineInfo($"Receive CloseLiveNotification,but it is paused,so skipped it.");
- return;
- }
- Logger.WriteLineInfo("Receive CloseLiveNotification");
- _connectionUrl = string.Empty;
- var liveEventArgs = new LiveEventArgs(false);
- if (!liveEventArgs.IsLive && !_liveVideoPusherManager.IsPushing)
- {
- return;
- }
- _liveVideoPusherManager.LiveStateChanged(liveEventArgs);
- }
- }
- private void HandleNewStartLiveNotification(NewStartLiveNotification startLiveNotification)
- {
- lock (_operateLocker)
- {
- if (_isPaused)
- {
- Logger.WriteLineInfo($"Receive NewStartLiveNotification,but it is paused,so skipped it.");
- return;
- }
- Logger.WriteLineInfo("Receive NewStartLiveNotification");
- _connectionUrl = startLiveNotification.LiveServiceUrl;
- var protocol = startLiveNotification.LiveProtocol;
- var pushMode = startLiveNotification.PushLiveMode;
- var extendedData = CreateExtentedData(protocol, startLiveNotification, _micDeviceId, _isMute);
- Logger.WriteLineInfo($"ExtendedData:{extendedData}");
- var liveEventArgs = new LiveEventArgs(true, (EnumLiveProtocol)protocol, (EnumLiveDataMode)pushMode, extendedData);
- _liveVideoPusherManager.LiveStateChanged(liveEventArgs);
- }
- }
- private IExtendedData CreateExtentedData(LiveProtocol protocol, NewStartLiveNotification notification, string micId, bool isMute)
- {
- var channels = notification.PushChannels;
- if (protocol == LiveProtocol.Rtmp)
- {
- var rtmpExtentedData = new RtmpExtendedData(notification.LiveServiceUrl, micId, isMute);
- if (notification.PushLiveMode == LiveDataMode.MergeLive)
- {
- rtmpExtentedData.IsMergeChannel = true;
- rtmpExtentedData.MergeType = EnumMergeType.Merge1920X1080;
- }
- else if (notification.PushLiveMode == LiveDataMode.OnlyLive)
- {
- rtmpExtentedData.IsMergeChannel = false;
- rtmpExtentedData.MergeType = EnumMergeType.None;
- }
- foreach (var channel in channels)
- {
- if (Enum.TryParse<EnumLiveChannelCategory>(channel.Name, out var category))
- {
- rtmpExtentedData.UserInfos.Add(new RtmpUserInfo(category, channel.Url, channel.Width, channel.Height));
- }
- }
- return rtmpExtentedData;
- }
- else if (protocol == LiveProtocol.RTC)
- {
- var rtcExtentedData = new RtcExtendedData(notification.LiveServiceUrl, micId, isMute);
- rtcExtentedData.AppId = notification.AppId;
- rtcExtentedData.RoomId = notification.IntegerRoomId;
- if (notification.PushLiveMode == LiveDataMode.MergeLive)
- {
- rtcExtentedData.IsMergeChannel = true;
- rtcExtentedData.MergeType = EnumMergeType.Merge1920X1080;
- }
- else if (notification.PushLiveMode == LiveDataMode.OnlyLive)
- {
- rtcExtentedData.IsMergeChannel = false;
- rtcExtentedData.MergeType = EnumMergeType.None;
- }
- foreach (var channel in channels)
- {
- if (Enum.TryParse<EnumLiveChannelCategory>(channel.Name, out var category))
- {
- rtcExtentedData.UserInfos.Add(new RtcUserInfo(category, channel.UserId, channel.UserSign, channel.Width, channel.Height));
- }
- }
- return rtcExtentedData;
- }
- return null;
- }
- public void ChangeCameraSettings(bool enableCameraLive, string cameraId, string micId, bool showPreviewImage, bool enableLiveVideo, bool isMute)
- {
- throw new NotImplementedException();
- }
- public void ChangeCameraSettingsForSonopost(bool showPreviewImage, EnumLiveChannelCategory previewLiveChannel, IEnumerable<CPVideoDeviceInfo> infos, RainbowImageDetectConfig rainbowImageDetectConfig, string micId, bool isMute)
- {
- lock (_locker)
- {
- bool needNotifyServer = false;
- if (_firstCommit)
- {
- _firstCommit = false;
- needNotifyServer = true;
- }
- if (_micDeviceId != micId)
- {
- _micDeviceId = micId;
- needNotifyServer = true;
- }
- if (infos == null)
- {
- if (_currentVideoDeviceInfoList.Count != 0 || needNotifyServer)
- {
- _currentVideoDeviceInfoList.Clear();
- _previewManager.UpdateCurrentVideoDeviceInfoList(new List<CPVideoDeviceInfo>());
- }
- }
- else
- {
- if (infos.Count() != _currentVideoDeviceInfoList.Count() || needNotifyServer)
- {
- _currentVideoDeviceInfoList.Clear();
- foreach (var info in infos)
- {
- var item = info.Clone() as CPVideoDeviceInfo;
- _currentVideoDeviceInfoList.Add(item);
- }
- _previewManager.UpdateCurrentVideoDeviceInfoList(infos.ToList());
- }
- else
- {
- if (IsDifferent(infos.ToList()) || needNotifyServer)
- {
- _currentVideoDeviceInfoList.Clear();
- foreach (var info in infos)
- {
- var item = info.Clone() as CPVideoDeviceInfo;
- _currentVideoDeviceInfoList.Add(item);
- }
- _previewManager.UpdateCurrentVideoDeviceInfoList(infos.ToList());
- }
- }
- }
- if (_isMute != isMute)
- {
- _isMute = isMute;
- _liveVideoPusherManager.SetMute(isMute);
- }
- var device = infos.FirstOrDefault(x => x.Category == previewLiveChannel);
- if (showPreviewImage && device != null)
- {
- if (device.Id != _oldPreviewCameraId && _previewManager.IsPreviewing)
- {
- _previewManager.StopPreview(true);
- }
- _oldPreviewCameraId = device.Id;
- _previewManager.StartPreview(device.Id, device.Width, device.Height, device.FrameRate, device.Category);
- }
- else
- {
- _previewManager.StopPreview(true);
- }
- var init = false;
- var updateEnable = false;
- var updateIntervalTime = false;
- if (_rainbowImageDetectConfig == null)
- {
- updateEnable = true;
- updateIntervalTime = true;
- init = true;
- _rainbowImageDetectConfig = rainbowImageDetectConfig;
- }
- else
- {
- if (_rainbowImageDetectConfig.IsDetectRainbowImage != rainbowImageDetectConfig.IsDetectRainbowImage)
- {
- _rainbowImageDetectConfig.IsDetectRainbowImage = rainbowImageDetectConfig.IsDetectRainbowImage;
- updateEnable = true;
- }
- if (_rainbowImageDetectConfig.BeforeDisableIntervalTime != rainbowImageDetectConfig.BeforeDisableIntervalTime)
- {
- _rainbowImageDetectConfig.BeforeDisableIntervalTime = rainbowImageDetectConfig.BeforeDisableIntervalTime;
- updateIntervalTime = true;
- }
- if (_rainbowImageDetectConfig.BeforeEnableIntervalTime != rainbowImageDetectConfig.BeforeEnableIntervalTime)
- {
- _rainbowImageDetectConfig.BeforeEnableIntervalTime = rainbowImageDetectConfig.BeforeEnableIntervalTime;
- updateIntervalTime = true;
- }
- if (_rainbowImageDetectConfig.AfterEnableIntervalTime != rainbowImageDetectConfig.AfterEnableIntervalTime)
- {
- _rainbowImageDetectConfig.AfterEnableIntervalTime = rainbowImageDetectConfig.AfterEnableIntervalTime;
- updateIntervalTime = true;
- }
- if (_rainbowImageDetectConfig.ScanIntervalTime != rainbowImageDetectConfig.ScanIntervalTime)
- {
- _rainbowImageDetectConfig.ScanIntervalTime = rainbowImageDetectConfig.ScanIntervalTime;
- updateIntervalTime = true;
- }
- }
- UpdateRainbowImageDetectConfig(updateEnable, init, updateIntervalTime);
- }
- }
- private void UpdateRainbowImageDetectConfig(bool updateEnable, bool init, bool updateIntervalTime)
- {
- if (init)
- {
- CrossPlatformHelper.Instance.DriverHelper.InitializeCaptureCard(_rainbowImageDetectConfig.CaptureCardList);
- }
- if (updateIntervalTime)
- {
- RainbowImageDetector.Instance.SetIntervalTime(_rainbowImageDetectConfig.BeforeDisableIntervalTime, _rainbowImageDetectConfig.BeforeEnableIntervalTime, _rainbowImageDetectConfig.AfterEnableIntervalTime, _rainbowImageDetectConfig.ScanIntervalTime);
- }
- if (updateEnable)
- {
- if (_rainbowImageDetectConfig.IsDetectRainbowImage)
- {
- RainbowImageDetector.Instance.StartDetect();
- }
- else
- {
- RainbowImageDetector.Instance.StopDetect();
- }
- }
- }
- private bool IsDifferent(List<CPVideoDeviceInfo> infos)
- {
- for (int i = 0; i < infos.Count(); i++)
- {
- if (infos[i].ToString() != _currentVideoDeviceInfoList[i].ToString())
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 获取品牌列表
- /// </summary>
- /// <returns></returns>
- public List<string> GetBrandList()
- {
- return _liveVideoPusherManager?.GetBrandList();
- }
- /// <summary>
- /// 获取型号列表
- /// </summary>
- /// <param name="brand"></param>
- /// <param name="model"></param>
- /// <returns></returns>
- public List<string> GetModelList(string brand)
- {
- return _liveVideoPusherManager?.GetModelList(brand);
- }
- /// <summary>
- /// 获取推荐分辨率
- /// </summary>
- /// <param name="brand"></param>
- /// <param name="model"></param>
- /// <returns></returns>
- public DeviceRecommandResolution GetRecommandResolution(string brand, string model)
- {
- return _liveVideoPusherManager?.GetRecommandResolution(brand, model);
- }
- private void DoDispose()
- {
- if (!_disposed)
- {
- RainbowImageDetector.Instance.IsPaused -= OnIsPaused;
- RainbowImageDetector.Instance.StopDetect();
- _liveVideoPusherManager.Dispose();
- _previewManager.PreviewImageReceived -= OnPreviewImageReceived;
- _previewManager.Dispose();
- _leaf.MessageArrived -= OnMessageArrived;
- _disposed = true;
- }
- }
- private void OnIsPaused(object sender, bool e)
- {
- lock (_operateLocker)
- {
- Logger.WriteLineInfo($"LiveVideoForSonopost Set IsPaused:{e}");
- if (e)
- {
- _isPaused = true;
- _liveVideoPusherManager.SetIsPaused(e);
- _previewManager.SetIsPaused(e);
- }
- else
- {
- _liveVideoPusherManager.SetIsPaused(e);
- _previewManager.SetIsPaused(e);
- _isPaused = false;
- }
- }
- }
- public void Dispose()
- {
- DoDispose();
- GC.SuppressFinalize(this);
- }
- public bool StartSpeedTest()
- {
- throw new NotImplementedException();
- }
- }
- }
|