123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- using FISLib;
- using FISLib.LiveVideo;
- using FISLib.Notification;
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- 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 LiveVideoServiceForAndroid : ILiveVideoService, IDisposable
- {
- private ILiveVideo _liveVideo;
- private ILiveVideoV2 _liveVideoV2;
- private FileTransferReader _fileReaderForLiveVideoUltrasoundImage;
- private FileTransferWriter _fileWriterForLiveVideoCameraPreviewImage;
- 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)
- {
- _fileReaderForLiveVideoUltrasoundImage.ExceptionThrows -= OnExceptionThrows;
- _fileReaderForLiveVideoUltrasoundImage.DataReceived -= OnDataReceived;
- _fileReaderForLiveVideoUltrasoundImage.Dispose();
- _fileReaderForLiveVideoUltrasoundImage = null;
- _fileWriterForLiveVideoCameraPreviewImage.ExceptionThrows -= OnExceptionThrows;
- _fileWriterForLiveVideoCameraPreviewImage.Dispose();
- _fileWriterForLiveVideoCameraPreviewImage = null;
- }
- }
- _liveVideo = newLiveVideo;
- if (_liveVideo != null)
- {
- _liveVideo.PreviewCameraCaptured += OnPreviewCameraCaptured;
- _liveVideo.CameraPreviewImageSizeChanged += OnCameraPreviewImageSizeChanged;
- _liveVideo.LiveNotification += OnLiveNotification;
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- _fileReaderForLiveVideoUltrasoundImage = new FileTransferReader(FISConsts.ImageTransferFolderForLiveVideoUltrasoundImage, FISConsts.ImageTransferFolderForLiveVideo, FISIMPL.UltrasoundMachineInfo?.FISFolder);
- _fileReaderForLiveVideoUltrasoundImage.ExceptionThrows += OnExceptionThrows;
- _fileReaderForLiveVideoUltrasoundImage.DataReceived += OnDataReceived;
- _fileReaderForLiveVideoUltrasoundImage.StartContinuousRead();
- _fileWriterForLiveVideoCameraPreviewImage = new FileTransferWriter(FISConsts.ImageTransferFolderForLiveVideoCameraPreviewImage, FISConsts.ImageTransferFolderForLiveVideo, FISIMPL.UltrasoundMachineInfo?.FISFolder);
- _fileWriterForLiveVideoCameraPreviewImage.ExceptionThrows += OnExceptionThrows;
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForAndroid 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.Dispose();
- _liveVideoV2 = null;
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- _fileReaderForLiveVideoUltrasoundImage.ExceptionThrows -= OnExceptionThrows;
- _fileReaderForLiveVideoUltrasoundImage.DataReceived -= OnDataReceived;
- _fileReaderForLiveVideoUltrasoundImage.Dispose();
- _fileReaderForLiveVideoUltrasoundImage = null;
- _fileWriterForLiveVideoCameraPreviewImage.ExceptionThrows -= OnExceptionThrows;
- _fileWriterForLiveVideoCameraPreviewImage.Dispose();
- _fileWriterForLiveVideoCameraPreviewImage = null;
- }
- }
- _liveVideoV2 = newLiveVideo;
- if (_liveVideoV2 != null)
- {
- _liveVideoV2.PreviewCameraCaptured += OnPreviewCameraCaptured;
- _liveVideoV2.CameraPreviewImageSizeChanged += OnCameraPreviewImageSizeChanged;
- _liveVideoV2.LiveNotification += OnLiveNotification;
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- _fileReaderForLiveVideoUltrasoundImage = new FileTransferReader(FISConsts.ImageTransferFolderForLiveVideoUltrasoundImage, FISConsts.ImageTransferFolderForLiveVideo, FISIMPL.UltrasoundMachineInfo?.FISFolder);
- _fileReaderForLiveVideoUltrasoundImage.ExceptionThrows += OnExceptionThrows;
- _fileReaderForLiveVideoUltrasoundImage.DataReceived += OnDataReceived;
- _fileReaderForLiveVideoUltrasoundImage.StartContinuousRead();
- _fileWriterForLiveVideoCameraPreviewImage = new FileTransferWriter(FISConsts.ImageTransferFolderForLiveVideoCameraPreviewImage, FISConsts.ImageTransferFolderForLiveVideo, FISIMPL.UltrasoundMachineInfo?.FISFolder);
- _fileWriterForLiveVideoCameraPreviewImage.ExceptionThrows += OnExceptionThrows;
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForAndroid Update 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($"LiveVideoServiceForAndroid OnCameraPreviewImageSizeChanged Error:{ex}");
- }
- }
- /// <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 videoV2 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($"LiveVideoServiceForAndroid ChangeCameraSetting Error:{ex}");
- }
- }
- public void ChangeCameraSettingForSonopost(CameraSettingForSonopost cameraSetting)
- {
- throw new NotImplementedException();
- }
- /// <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>
- /// 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($"LiveVideoServiceForAndroid StartSpeedTest Error:{ex}");
- return false;
- }
- }
- /// <summary>
- /// 约耗时100ms+,不如OnDataReceived快(约50ms),故弃用。
- /// </summary>
- /// <param name="length"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- public void CapturedUSImageData(int length, int width, int height)
- {
- try
- {
- var byteImage = _fileReaderForLiveVideoUltrasoundImage?.ReadFromFile(length);
- if (byteImage == null || byteImage.Length != length)
- {
- return;
- }
- var imageCaptureDataInfo = new CaptureDataInfo
- {
- Width = width,
- Height = height,
- Data = byteImage,
- };
- ImageDataCapturer.RaiseImdageDataCaptured(imageCaptureDataInfo);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForAndroid 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($"LiveVideoServiceForAndroid CapturedUSImageData Error:{ex}");
- }
- }
- /// <summary>
- /// Dispose
- /// </summary>
- public void Dispose()
- {
- try
- {
- Update(null);
- UpdateV2(null);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForAndroid Dispose Error:{ex}");
- }
- }
- private void OnPreviewCameraCaptured(object sender, byte[] e)
- {
- try
- {
- if (e == null)
- {
- return;
- }
- if (FISIMPL.IsRunningJsonRpcMode)
- {
- _fileWriterForLiveVideoCameraPreviewImage.WriteToFileContinuous(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($"LiveVideoServiceForAndroid OnPreviewCameraCaptured 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($"LiveVideoServiceForAndroid OnLiveNotification Error:{ex}");
- }
- }
- private void OnExceptionThrows(object sender, Exception e)
- {
- if (sender is FileTransferReader fileReader)
- {
- Logger.WriteLineError($"File Reader {fileReader.Name} throw exception {e}");
- }
- else if (sender is FileTransferWriter fileWriter)
- {
- Logger.WriteLineError($"File Writer {fileWriter.Name} throw exception {e}");
- }
- }
- private void OnDataReceived(object sender, byte[] e)
- {
- FISVideoFrameData videoFrameData = new FISVideoFrameData();
- try
- {
- videoFrameData.LoadFromBytes(e);
- var imageCaptureDataInfo = new CaptureDataInfo
- {
- Width = videoFrameData.Width,
- Height = videoFrameData.Height,
- Data = videoFrameData.Data,
- };
- ImageDataCapturer.RaiseImdageDataCaptured(imageCaptureDataInfo);
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveVideoServiceForAndroid OnDataReceived Error:{ex}");
- }
- finally
- {
- videoFrameData?.Close();
- }
- }
- public void ChangeRealTimeCaptureSetting(bool isStart)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Capture current Image
- /// </summary>
- public void CaptureCurrentImage()
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Start Record Video
- /// </summary>
- public void StartRecordVideo()
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Stop Record Video
- /// </summary>
- public void StopRecordVideo(bool isTimeOut)
- {
- throw new NotImplementedException();
- }
- public void ReUploadRestVid()
- {
- throw new NotImplementedException();
- }
- }
- }
|