123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- using FISLib;
- using FISLib.LiveVideo;
- using FISLib.Notification;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Threading;
- using Vinno.FIS.TRTCClient.Common.Enum;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS;
- using Vinno.vCloud.Common.FIS.LiveVideos;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
- namespace FISIMPL
- {
- internal class LiveVideoServiceForWindows : ILiveVideoService, IDisposable
- {
- private readonly ConcurrentStack<byte[]> _byteImageCache = new ConcurrentStack<byte[]>();
- private ILiveVideo _liveVideo;
- private ILiveVideoV2 _liveVideoV2;
- private PipeClient _pipeClient;
- private PipeServer _pipeServer;
- private FISImageFrameData _imageFrameData;
- /// <summary>
- /// Raised when local camera live execute
- /// </summary>
- public event EventHandler<FISImageFrameData> PreviewCameraCaptured;
- /// <summary>
- /// Raised when capture image generated.(only for new server)
- /// </summary>
- public event EventHandler<string> CaptureImageGenerated;
- /// <summary>
- /// Raised when record video generated.(only for new server)
- /// </summary>
- public event EventHandler<string> RecordVideoGenerated;
- internal void Update(IvCloudTerminal vcloudTerminal)
- {
- try
- {
- var newLiveVideo = vcloudTerminal?.GetFeature<ILiveVideo>(TerminalFeatureType.LiveVideo);
- if (newLiveVideo != _liveVideo)
- {
- if (_liveVideo != null)
- {
- _liveVideo.PreviewCameraCaptured -= OnPreviewCameraCaptured;
- _liveVideo.CameraPreviewImageSizeChanged -= OnCameraPreviewImageSizeChanged;
- _liveVideo.LiveNotification -= OnLiveNotification;
- _liveVideo.Dispose();
- _liveVideo = null;
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- _pipeClient.Dispose();
- _pipeClient.LogMsgThrow -= OnLogMsgThrow;
- _pipeClient = null;
- _pipeServer.DataReceived -= OnDataReceived;
- _pipeServer.Dispose();
- _pipeServer.LogMsgThrow -= OnLogMsgThrow;
- _pipeServer = null;
- }
- }
- _liveVideo = newLiveVideo;
- if (_liveVideo != null)
- {
- _liveVideo.PreviewCameraCaptured += OnPreviewCameraCaptured;
- _liveVideo.CameraPreviewImageSizeChanged += OnCameraPreviewImageSizeChanged;
- _liveVideo.LiveNotification += OnLiveNotification;
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- _pipeClient = new PipeClient(FISConsts.PipeForLiveVideoCameraPreviewImage);
- _pipeClient.LogMsgThrow += OnLogMsgThrow;
- _pipeClient.Start();
- _pipeServer = new PipeServer(FISConsts.PipeForLiveVideoUltrasoundImage);
- _pipeServer.LogMsgThrow += OnLogMsgThrow;
- _pipeServer.DataReceived += OnDataReceived;
- _pipeServer.StartAndReceive();
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows Update Error:{ex}");
- }
- }
- internal void UpdateV2(IvCloudTerminalV2 vcloudTerminalV2)
- {
- try
- {
- var newLiveVideo = vcloudTerminalV2?.GetFeature<ILiveVideoV2>(TerminalFeatureType.LiveVideo);
- if (newLiveVideo != _liveVideoV2)
- {
- if (_liveVideoV2 != null)
- {
- _liveVideoV2.PreviewCameraCaptured -= OnPreviewCameraCaptured;
- _liveVideoV2.CameraPreviewImageSizeChanged -= OnCameraPreviewImageSizeChanged;
- _liveVideoV2.LiveNotification -= OnLiveNotification;
- _liveVideoV2.CaptureImageGenerated -= OnCaptureImageGenerated;
- _liveVideoV2.RecordVideoGenerated -= OnRecordVideoGenerated;
- _liveVideoV2.Dispose();
- _liveVideoV2 = null;
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- _pipeClient.Dispose();
- _pipeClient.LogMsgThrow -= OnLogMsgThrow;
- _pipeClient = null;
- _pipeServer.DataReceived -= OnDataReceived;
- _pipeServer.Dispose();
- _pipeServer.LogMsgThrow -= OnLogMsgThrow;
- _pipeServer = null;
- }
- }
- _liveVideoV2 = newLiveVideo;
- if (_liveVideoV2 != null)
- {
- _liveVideoV2.PreviewCameraCaptured += OnPreviewCameraCaptured;
- _liveVideoV2.CameraPreviewImageSizeChanged += OnCameraPreviewImageSizeChanged;
- _liveVideoV2.LiveNotification += OnLiveNotification;
- _liveVideoV2.CaptureImageGenerated += OnCaptureImageGenerated;
- _liveVideoV2.RecordVideoGenerated += OnRecordVideoGenerated;
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- _pipeClient = new PipeClient(FISConsts.PipeForLiveVideoCameraPreviewImage);
- _pipeClient.LogMsgThrow += OnLogMsgThrow;
- _pipeClient.Start();
- _pipeServer = new PipeServer(FISConsts.PipeForLiveVideoUltrasoundImage);
- _pipeServer.LogMsgThrow += OnLogMsgThrow;
- _pipeServer.DataReceived += OnDataReceived;
- _pipeServer.StartAndReceive();
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows UpdateV2 Error:{ex}");
- }
- }
- private void OnCaptureImageGenerated(object sender, string e)
- {
- CaptureImageGenerated?.Invoke(this, e);
- }
- private void OnRecordVideoGenerated(object sender, string e)
- {
- RecordVideoGenerated?.Invoke(this, e);
- }
- private void OnDataReceived(object sender, byte[] e)
- {
- _byteImageCache.Push(e);
- }
- /// <summary>
- /// Change camera settings.
- /// </summary>
- /// <param name="cameraSetting"></param>
- public void ChangeCameraSetting(CameraSetting cameraSetting)
- {
- try
- {
- if (cameraSetting == null)
- {
- Logger.WriteLineError($"ChangeCameraSetting error: cameraSetting is null");
- return;
- }
- if (FISIMPL.IsConnectWithOldServer)
- {
- if (_liveVideo == null)
- {
- Logger.WriteLineError($"ChangeCameraSetting error: live video feature is null");
- return;
- }
- _liveVideo.ChangeCameraSettings(cameraSetting.CameraLiveEnabled && cameraSetting.LiveVideoEnabled, cameraSetting.CameraSource?.Id, cameraSetting.MicSource?.Id, cameraSetting.CameraPreviewEnabled, cameraSetting.LiveVideoEnabled, cameraSetting.IsMute);
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"ChangeCameraSetting error: live video v2 feature is null");
- return;
- }
- _liveVideoV2.ChangeCameraSettings(cameraSetting.CameraLiveEnabled && cameraSetting.LiveVideoEnabled, cameraSetting.CameraSource?.Id, cameraSetting.MicSource?.Id, cameraSetting.CameraPreviewEnabled, cameraSetting.LiveVideoEnabled, cameraSetting.IsMute);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows ChangeCameraSetting Error:{ex}");
- }
- }
- /// <summary>
- /// Change camera settings for sonopost
- /// </summary>
- /// <param name="cameraSetting"></param>
- public void ChangeCameraSettingForSonopost(CameraSettingForSonopost cameraSetting)
- {
- try
- {
- if (cameraSetting == null)
- {
- Logger.WriteLineError($"ChangeCameraSetting error: cameraSetting is null");
- return;
- }
- var currentLiveChannelCategory = (EnumLiveChannelCategory)cameraSetting.CurrentLiveChannelCategory;
- var videoDeviceInfoList = new List<CPVideoDeviceInfo>();
- if (cameraSetting.VideoDeviceInfoList != null)
- {
- foreach (var fisVideoDeviceInfo in cameraSetting.VideoDeviceInfoList)
- {
- var videoDeviceInfo = FISConverter.ConvertFISVideoDeviceInfoToCPVideoDeviceInfo(fisVideoDeviceInfo);
- videoDeviceInfoList.Add(videoDeviceInfo);
- }
- }
- string micId = null;
- bool isMute = true;
- if (cameraSetting.MicDeviceInfo != null)
- {
- micId = cameraSetting.MicDeviceInfo.Id;
- isMute = !cameraSetting.MicDeviceInfo.IsEnable;
- }
- var rainbowImageDetectConfig = FISConverter.ConvertFISRainbowImageDetectConfigToRainbowImageDetectConfig(cameraSetting.RainbowImageDetectConfig);
- if (FISIMPL.IsConnectWithOldServer)
- {
- if (_liveVideo == null)
- {
- Logger.WriteLineError($"ChangeCameraSetting error: live video feature is null");
- return;
- }
- _liveVideo.ChangeCameraSettingsForSonopost(cameraSetting.CameraPreviewEnabled, currentLiveChannelCategory, videoDeviceInfoList, rainbowImageDetectConfig, micId, isMute);
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"ChangeCameraSetting error: live videoV2 feature is null");
- return;
- }
- _liveVideoV2.ChangeCameraSettingsForSonopost(cameraSetting.CameraPreviewEnabled, currentLiveChannelCategory, videoDeviceInfoList, micId, isMute, rainbowImageDetectConfig);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows ChangeCameraSettingForSonopost Error:{ex}");
- }
- }
- /// <summary>
- /// Speed live test network and auto select good network to server
- /// </summary>
- /// <returns></returns>
- public bool StartSpeedTest()
- {
- try
- {
- if (FISIMPL.IsConnectWithOldServer)
- {
- if (_liveVideo == null)
- {
- Logger.WriteLineError($"ChangeCameraSetting error: live video feature is null");
- return false;
- }
- return _liveVideo.StartSpeedTest();
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"ChangeCameraSetting error: live video v2 feature is null");
- return false;
- }
- return _liveVideoV2.StartSpeedTest();
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows StartSpeedTest Error:{ex}");
- return false;
- }
- }
- /// <summary>
- /// Capture ultrasound machie image data.
- /// </summary>
- /// <param name="length">image data length</param>
- /// <param name="width">image width</param>
- /// <param name="height">image height</param>
- public void CapturedUSImageData(int length, int width, int height)
- {
- try
- {
- byte[] byteImage = null;
- int retryCount = 0;
- while (!_byteImageCache.TryPop(out byteImage) && retryCount < 50)
- {
- Thread.Sleep(10);
- retryCount++;
- }
- if (byteImage == null)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows-CapturedUSImageData Get Image Cache Fail");
- return;
- }
- if (byteImage == null)
- {
- return;
- }
- var imageCaptureDataInfo = new CaptureDataInfo
- {
- Width = width,
- Height = height,
- Data = byteImage,
- };
- ImageDataCapturer.RaiseImdageDataCaptured(imageCaptureDataInfo);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows CapturedUSImageData Error:{ex}");
- }
- }
- /// <summary>
- /// Capture ultrasound machie image data.
- /// </summary>
- /// <param name="fisVideoFrameData">image data</param>
- public void CapturedUSImageData_2(FISVideoFrameData fisVideoFrameData)
- {
- try
- {
- var imageCaptureDataInfo = new CaptureDataInfo
- {
- Width = fisVideoFrameData.Width,
- Height = fisVideoFrameData.Height,
- Data = fisVideoFrameData.Data,
- };
- ImageDataCapturer.RaiseImdageDataCaptured(imageCaptureDataInfo);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows CapturedUSImageData Error:{ex}");
- }
- }
- /// <summary>
- /// 获取品牌列表
- /// </summary>
- /// <returns></returns>
- public List<string> GetBrandList()
- {
- try
- {
- if (FISIMPL.IsConnectWithOldServer)
- {
- if (_liveVideo == null)
- {
- Logger.WriteLineError($"GetBrandList error: live video feature is null");
- return new List<string>();
- }
- return _liveVideo.GetBrandList();
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"GetBrandList error: live videoV2 feature is null");
- return new List<string>();
- }
- return _liveVideoV2.GetBrandList();
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows GetBrandList Error:{ex}");
- return new List<string>();
- }
- }
- /// <summary>
- /// 获取型号列表
- /// </summary>
- /// <param name="brand"></param>
- /// <param name="model"></param>
- /// <returns></returns>
- public List<string> GetModelList(string brand)
- {
- try
- {
- if (FISIMPL.IsConnectWithOldServer)
- {
- if (_liveVideo == null)
- {
- Logger.WriteLineError($"GetModelList error: live video feature is null");
- return new List<string>();
- }
- return _liveVideo.GetModelList(brand);
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"GetModelList error: live videoV2 feature is null");
- return new List<string>();
- }
- return _liveVideoV2.GetModelList(brand);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows GetModelList Error:{ex}");
- return new List<string>();
- }
- }
- /// <summary>
- /// 获取推荐分辨率
- /// </summary>
- /// <param name="brand"></param>
- /// <param name="model"></param>
- /// <returns></returns>
- public FISDeviceRecommandResolution GetRecommandResolution(string brand, string model)
- {
- try
- {
- if (FISIMPL.IsConnectWithOldServer)
- {
- if (_liveVideo == null)
- {
- Logger.WriteLineError($"GetRecommandResolution error: live video feature is null");
- return null;
- }
- var result = _liveVideo.GetRecommandResolution(brand, model);
- return FISConverter.ConvertDeviceRecommandResolutionToFISDeviceRecommandResolution(result);
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"GetRecommandResolution error: live videoV2 feature is null");
- return null;
- }
- var result = _liveVideoV2.GetRecommandResolution(brand, model);
- return FISConverter.ConvertDeviceRecommandResolutionToFISDeviceRecommandResolution(result);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows GetRecommandResolution Error:{ex}");
- return null;
- }
- }
- /// <summary>
- /// Dispose
- /// </summary>
- public void Dispose()
- {
- try
- {
- Update(null);
- UpdateV2(null);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows Dispose Error:{ex}");
- }
- }
- private void OnPreviewCameraCaptured(object sender, byte[] e)
- {
- try
- {
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- _pipeClient?.SendBytes(e);
- }
- else
- {
- if (_imageFrameData == null || _imageFrameData.Size != e.Length)
- {
- return;
- }
- Marshal.Copy(e, 0, _imageFrameData.Data, e.Length);
- PreviewCameraCaptured?.Invoke(this, _imageFrameData);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows OnPreviewCameraCaptured Error:{ex}");
- }
- }
- private void OnCameraPreviewImageSizeChanged(object sender, ImageSize e)
- {
- try
- {
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- var fisImageSize = new FISImageSize(e.Width, e.Height);
- NotificationSender.SendNotification(new FISNotificationArgs
- {
- NotificationType = FISNotificationType.FISLiveVideoCameraImageSizeChange,
- Params = fisImageSize
- });
- }
- else
- {
- if (_imageFrameData == null)
- {
- _imageFrameData = new FISImageFrameData(e.Width, e.Height);
- }
- else if (_imageFrameData.Width != e.Width || _imageFrameData.Height != e.Height)
- {
- _imageFrameData.Resize(e.Width, e.Height);
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows OnCameraPreviewImageSizeChanged Error:{ex}");
- }
- }
- private void OnLiveNotification(object sender, LiveNotificationArgs e)
- {
- try
- {
- var fisLiveNotificationArgs = new FISLiveNotificationArgs(e.IsLive, e.LiveWidth, e.LiveHeight);
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- NotificationSender.SendNotification(new FISNotificationArgs
- {
- Params = fisLiveNotificationArgs,
- NotificationType = FISNotificationType.FISLiveVideoLiveNotification
- });
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows OnLiveNotification Error:{ex}");
- }
- }
- private void OnLogMsgThrow(object sender, FISLogEventArgs e)
- {
- if (e == null)
- {
- return;
- }
- switch (e.LogType)
- {
- case FISDeviceLogCategory.Error:
- Logger.WriteLineError(e.Msg);
- break;
- case FISDeviceLogCategory.Warn:
- Logger.WriteLineWarn(e.Msg);
- break;
- case FISDeviceLogCategory.Verb:
- Logger.WriteLineVerbose(e.Msg);
- break;
- case FISDeviceLogCategory.Info:
- default:
- Logger.WriteLineInfo(e.Msg);
- break;
- }
- }
- public void ChangeRealTimeCaptureSetting(bool isStart)
- {
- try
- {
- if (FISIMPL.IsConnectWithOldServer)
- {
- Logger.WriteLineError($"ChangeRealTimeCaptureSetting error: old Server doesn't support");
- return;
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"ChangeRealTimeCaptureSetting error: live videoV2 feature is null");
- return;
- }
- _liveVideoV2.ChangeRealTimeCaptureSetting(isStart);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows ChangeRealTimeCaptureSetting Error:{ex}");
- }
- }
- /// <summary>
- /// Capture current Image
- /// </summary>
- public void CaptureCurrentImage()
- {
- try
- {
- if (FISIMPL.IsConnectWithOldServer)
- {
- Logger.WriteLineError($"CaptureCurrentImage error: old Server doesn't support");
- return;
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"CaptureCurrentImage error: live videoV2 feature is null");
- return;
- }
- _liveVideoV2.CaptureCurrentImage();
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows CaptureCurrentImage Error:{ex}");
- }
- }
- /// <summary>
- /// Start Record Video
- /// </summary>
- public void StartRecordVideo()
- {
- try
- {
- if (FISIMPL.IsConnectWithOldServer)
- {
- Logger.WriteLineError($"StartRecordVideo error: old Server doesn't support");
- return;
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"StartRecordVideo error: live videoV2 feature is null");
- return;
- }
- _liveVideoV2.StartRecordVideo();
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows StartRecordVideo Error:{ex}");
- }
- }
- /// <summary>
- /// Stop Record Video
- /// </summary>
- public void StopRecordVideo(bool isTimeOut)
- {
- try
- {
- if (FISIMPL.IsConnectWithOldServer)
- {
- Logger.WriteLineError($"StopRecordVideo error: old Server doesn't support");
- return;
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"StopRecordVideo error: live videoV2 feature is null");
- return;
- }
- _liveVideoV2.StopRecordVideo(isTimeOut);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows StopRecordVideo Error:{ex}");
- }
- }
- public void ReUploadRestVid()
- {
- try
- {
- if (FISIMPL.IsConnectWithOldServer)
- {
- Logger.WriteLineError($"ReUploadRestVid error: old Server doesn't support");
- return;
- }
- else
- {
- if (_liveVideoV2 == null)
- {
- Logger.WriteLineError($"ReUploadRestVid error: live videoV2 feature is null");
- }
- _liveVideoV2.ReUploadRestVid();
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForWindows StopRecordVideo Error:{ex}");
- }
- }
- }
- }
|