LiveVideoServiceForAndroid.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. using FISLib;
  2. using FISLib.LiveVideo;
  3. using FISLib.Notification;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. using Vinno.IUS.Common.Log;
  8. using Vinno.vCloud.Common.FIS;
  9. using Vinno.vCloud.Common.FIS.LiveVideos;
  10. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  11. namespace FISIMPL
  12. {
  13. internal class LiveVideoServiceForAndroid : ILiveVideoService, IDisposable
  14. {
  15. private ILiveVideo _liveVideo;
  16. private ILiveVideoV2 _liveVideoV2;
  17. private FileTransferReader _fileReaderForLiveVideoUltrasoundImage;
  18. private FileTransferWriter _fileWriterForLiveVideoCameraPreviewImage;
  19. private FISImageFrameData _imageFrameData;
  20. /// <summary>
  21. /// Raised when local camera live execute
  22. /// </summary>
  23. public event EventHandler<FISImageFrameData> PreviewCameraCaptured;
  24. /// <summary>
  25. /// Raised when capture image generated.(only for new server)
  26. /// </summary>
  27. public event EventHandler<string> CaptureImageGenerated;
  28. /// <summary>
  29. /// Raised when record video generated.(only for new server)
  30. /// </summary>
  31. public event EventHandler<string> RecordVideoGenerated;
  32. internal void Update(IvCloudTerminal vcloudTerminal)
  33. {
  34. try
  35. {
  36. var newLiveVideo = vcloudTerminal?.GetFeature<ILiveVideo>(TerminalFeatureType.LiveVideo);
  37. if (newLiveVideo != _liveVideo)
  38. {
  39. if (_liveVideo != null)
  40. {
  41. _liveVideo.PreviewCameraCaptured -= OnPreviewCameraCaptured;
  42. _liveVideo.CameraPreviewImageSizeChanged -= OnCameraPreviewImageSizeChanged;
  43. _liveVideo.LiveNotification -= OnLiveNotification;
  44. _liveVideo.Dispose();
  45. _liveVideo = null;
  46. if (FISIMPL.IsRunningJsonRpcMode)
  47. {
  48. _fileReaderForLiveVideoUltrasoundImage.ExceptionThrows -= OnExceptionThrows;
  49. _fileReaderForLiveVideoUltrasoundImage.DataReceived -= OnDataReceived;
  50. _fileReaderForLiveVideoUltrasoundImage.Dispose();
  51. _fileReaderForLiveVideoUltrasoundImage = null;
  52. _fileWriterForLiveVideoCameraPreviewImage.ExceptionThrows -= OnExceptionThrows;
  53. _fileWriterForLiveVideoCameraPreviewImage.Dispose();
  54. _fileWriterForLiveVideoCameraPreviewImage = null;
  55. }
  56. }
  57. _liveVideo = newLiveVideo;
  58. if (_liveVideo != null)
  59. {
  60. _liveVideo.PreviewCameraCaptured += OnPreviewCameraCaptured;
  61. _liveVideo.CameraPreviewImageSizeChanged += OnCameraPreviewImageSizeChanged;
  62. _liveVideo.LiveNotification += OnLiveNotification;
  63. if (FISIMPL.IsRunningJsonRpcMode)
  64. {
  65. _fileReaderForLiveVideoUltrasoundImage = new FileTransferReader(FISConsts.ImageTransferFolderForLiveVideoUltrasoundImage, FISConsts.ImageTransferFolderForLiveVideo, FISIMPL.UltrasoundMachineInfo?.FISFolder);
  66. _fileReaderForLiveVideoUltrasoundImage.ExceptionThrows += OnExceptionThrows;
  67. _fileReaderForLiveVideoUltrasoundImage.DataReceived += OnDataReceived;
  68. _fileReaderForLiveVideoUltrasoundImage.StartContinuousRead();
  69. _fileWriterForLiveVideoCameraPreviewImage = new FileTransferWriter(FISConsts.ImageTransferFolderForLiveVideoCameraPreviewImage, FISConsts.ImageTransferFolderForLiveVideo, FISIMPL.UltrasoundMachineInfo?.FISFolder);
  70. _fileWriterForLiveVideoCameraPreviewImage.ExceptionThrows += OnExceptionThrows;
  71. }
  72. }
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. Logger.WriteLineError($"LiveVideoServiceForAndroid Update Error:{ex}");
  78. }
  79. }
  80. internal void UpdateV2(IvCloudTerminalV2 vcloudTerminalV2)
  81. {
  82. try
  83. {
  84. var newLiveVideo = vcloudTerminalV2?.GetFeature<ILiveVideoV2>(TerminalFeatureType.LiveVideo);
  85. if (newLiveVideo != _liveVideoV2)
  86. {
  87. if (_liveVideoV2 != null)
  88. {
  89. _liveVideoV2.PreviewCameraCaptured -= OnPreviewCameraCaptured;
  90. _liveVideoV2.CameraPreviewImageSizeChanged -= OnCameraPreviewImageSizeChanged;
  91. _liveVideoV2.LiveNotification -= OnLiveNotification;
  92. _liveVideoV2.Dispose();
  93. _liveVideoV2 = null;
  94. if (FISIMPL.IsRunningJsonRpcMode)
  95. {
  96. _fileReaderForLiveVideoUltrasoundImage.ExceptionThrows -= OnExceptionThrows;
  97. _fileReaderForLiveVideoUltrasoundImage.DataReceived -= OnDataReceived;
  98. _fileReaderForLiveVideoUltrasoundImage.Dispose();
  99. _fileReaderForLiveVideoUltrasoundImage = null;
  100. _fileWriterForLiveVideoCameraPreviewImage.ExceptionThrows -= OnExceptionThrows;
  101. _fileWriterForLiveVideoCameraPreviewImage.Dispose();
  102. _fileWriterForLiveVideoCameraPreviewImage = null;
  103. }
  104. }
  105. _liveVideoV2 = newLiveVideo;
  106. if (_liveVideoV2 != null)
  107. {
  108. _liveVideoV2.PreviewCameraCaptured += OnPreviewCameraCaptured;
  109. _liveVideoV2.CameraPreviewImageSizeChanged += OnCameraPreviewImageSizeChanged;
  110. _liveVideoV2.LiveNotification += OnLiveNotification;
  111. if (FISIMPL.IsRunningJsonRpcMode)
  112. {
  113. _fileReaderForLiveVideoUltrasoundImage = new FileTransferReader(FISConsts.ImageTransferFolderForLiveVideoUltrasoundImage, FISConsts.ImageTransferFolderForLiveVideo, FISIMPL.UltrasoundMachineInfo?.FISFolder);
  114. _fileReaderForLiveVideoUltrasoundImage.ExceptionThrows += OnExceptionThrows;
  115. _fileReaderForLiveVideoUltrasoundImage.DataReceived += OnDataReceived;
  116. _fileReaderForLiveVideoUltrasoundImage.StartContinuousRead();
  117. _fileWriterForLiveVideoCameraPreviewImage = new FileTransferWriter(FISConsts.ImageTransferFolderForLiveVideoCameraPreviewImage, FISConsts.ImageTransferFolderForLiveVideo, FISIMPL.UltrasoundMachineInfo?.FISFolder);
  118. _fileWriterForLiveVideoCameraPreviewImage.ExceptionThrows += OnExceptionThrows;
  119. }
  120. }
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. Logger.WriteLineError($"LiveVideoServiceForAndroid Update Error:{ex}");
  126. }
  127. }
  128. private void OnCameraPreviewImageSizeChanged(object sender, ImageSize e)
  129. {
  130. try
  131. {
  132. if (FISIMPL.IsRunningJsonRpcMode)
  133. {
  134. var fisImageSize = new FISImageSize(e.Width, e.Height);
  135. NotificationSender.SendNotification(new FISNotificationArgs
  136. {
  137. NotificationType = FISNotificationType.FISLiveVideoCameraImageSizeChange,
  138. Params = fisImageSize
  139. });
  140. }
  141. else
  142. {
  143. if (_imageFrameData == null)
  144. {
  145. _imageFrameData = new FISImageFrameData(e.Width, e.Height);
  146. }
  147. else if (_imageFrameData.Width != e.Width || _imageFrameData.Height != e.Height)
  148. {
  149. _imageFrameData.Resize(e.Width, e.Height);
  150. }
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. Logger.WriteLineError($"LiveVideoServiceForAndroid OnCameraPreviewImageSizeChanged Error:{ex}");
  156. }
  157. }
  158. /// <summary>
  159. /// Change camera settings.
  160. /// </summary>
  161. /// <param name="cameraSetting"></param>
  162. public void ChangeCameraSetting(CameraSetting cameraSetting)
  163. {
  164. try
  165. {
  166. if (cameraSetting == null)
  167. {
  168. Logger.WriteLineError($"ChangeCameraSetting error: cameraSetting is null");
  169. return;
  170. }
  171. if (FISIMPL.IsConnectWithOldServer)
  172. {
  173. if (_liveVideo == null)
  174. {
  175. Logger.WriteLineError($"ChangeCameraSetting error: live video feature is null");
  176. return;
  177. }
  178. _liveVideo.ChangeCameraSettings(cameraSetting.CameraLiveEnabled && cameraSetting.LiveVideoEnabled, cameraSetting.CameraSource?.Id, cameraSetting.MicSource?.Id, cameraSetting.CameraPreviewEnabled, cameraSetting.LiveVideoEnabled, cameraSetting.IsMute);
  179. }
  180. else
  181. {
  182. if (_liveVideoV2 == null)
  183. {
  184. Logger.WriteLineError($"ChangeCameraSetting error: live videoV2 feature is null");
  185. return;
  186. }
  187. _liveVideoV2.ChangeCameraSettings(cameraSetting.CameraLiveEnabled && cameraSetting.LiveVideoEnabled, cameraSetting.CameraSource?.Id, cameraSetting.MicSource?.Id, cameraSetting.CameraPreviewEnabled, cameraSetting.LiveVideoEnabled, cameraSetting.IsMute);
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. Logger.WriteLineError($"LiveVideoServiceForAndroid ChangeCameraSetting Error:{ex}");
  193. }
  194. }
  195. public void ChangeCameraSettingForSonopost(CameraSettingForSonopost cameraSetting)
  196. {
  197. throw new NotImplementedException();
  198. }
  199. /// <summary>
  200. /// 获取品牌列表
  201. /// </summary>
  202. /// <returns></returns>
  203. public List<string> GetBrandList()
  204. {
  205. try
  206. {
  207. if (FISIMPL.IsConnectWithOldServer)
  208. {
  209. if (_liveVideo == null)
  210. {
  211. Logger.WriteLineError($"GetBrandList error: live video feature is null");
  212. return new List<string>();
  213. }
  214. return _liveVideo.GetBrandList();
  215. }
  216. else
  217. {
  218. if (_liveVideoV2 == null)
  219. {
  220. Logger.WriteLineError($"GetBrandList error: live videoV2 feature is null");
  221. return new List<string>();
  222. }
  223. return _liveVideoV2.GetBrandList();
  224. }
  225. }
  226. catch (Exception ex)
  227. {
  228. Logger.WriteLineError($"LiveVideoServiceForWindows GetBrandList Error:{ex}");
  229. return new List<string>();
  230. }
  231. }
  232. /// <summary>
  233. /// 获取型号列表
  234. /// </summary>
  235. /// <param name="brand"></param>
  236. /// <param name="model"></param>
  237. /// <returns></returns>
  238. public List<string> GetModelList(string brand)
  239. {
  240. try
  241. {
  242. if (FISIMPL.IsConnectWithOldServer)
  243. {
  244. if (_liveVideo == null)
  245. {
  246. Logger.WriteLineError($"GetModelList error: live video feature is null");
  247. return new List<string>();
  248. }
  249. return _liveVideo.GetModelList(brand);
  250. }
  251. else
  252. {
  253. if (_liveVideoV2 == null)
  254. {
  255. Logger.WriteLineError($"GetModelList error: live videoV2 feature is null");
  256. return new List<string>();
  257. }
  258. return _liveVideoV2.GetModelList(brand);
  259. }
  260. }
  261. catch (Exception ex)
  262. {
  263. Logger.WriteLineError($"LiveVideoServiceForWindows GetModelList Error:{ex}");
  264. return new List<string>();
  265. }
  266. }
  267. /// <summary>
  268. /// 获取推荐分辨率
  269. /// </summary>
  270. /// <param name="brand"></param>
  271. /// <param name="model"></param>
  272. /// <returns></returns>
  273. public FISDeviceRecommandResolution GetRecommandResolution(string brand, string model)
  274. {
  275. try
  276. {
  277. if (FISIMPL.IsConnectWithOldServer)
  278. {
  279. if (_liveVideo == null)
  280. {
  281. Logger.WriteLineError($"GetRecommandResolution error: live video feature is null");
  282. return null;
  283. }
  284. var result = _liveVideo.GetRecommandResolution(brand, model);
  285. return FISConverter.ConvertDeviceRecommandResolutionToFISDeviceRecommandResolution(result);
  286. }
  287. else
  288. {
  289. if (_liveVideoV2 == null)
  290. {
  291. Logger.WriteLineError($"GetRecommandResolution error: live videoV2 feature is null");
  292. return null;
  293. }
  294. var result = _liveVideoV2.GetRecommandResolution(brand, model);
  295. return FISConverter.ConvertDeviceRecommandResolutionToFISDeviceRecommandResolution(result);
  296. }
  297. }
  298. catch (Exception ex)
  299. {
  300. Logger.WriteLineError($"LiveVideoServiceForWindows GetRecommandResolution Error:{ex}");
  301. return null;
  302. }
  303. }
  304. /// <summary>
  305. /// Speed live test network and auto select good network to server
  306. /// </summary>
  307. /// <returns></returns>
  308. public bool StartSpeedTest()
  309. {
  310. try
  311. {
  312. if (FISIMPL.IsConnectWithOldServer)
  313. {
  314. if (_liveVideo == null)
  315. {
  316. Logger.WriteLineError($"ChangeCameraSetting error: live video feature is null");
  317. return false;
  318. }
  319. return _liveVideo.StartSpeedTest();
  320. }
  321. else
  322. {
  323. if (_liveVideoV2 == null)
  324. {
  325. Logger.WriteLineError($"ChangeCameraSetting error: live video V2 feature is null");
  326. return false;
  327. }
  328. return _liveVideoV2.StartSpeedTest();
  329. }
  330. }
  331. catch (Exception ex)
  332. {
  333. Logger.WriteLineError($"LiveVideoServiceForAndroid StartSpeedTest Error:{ex}");
  334. return false;
  335. }
  336. }
  337. /// <summary>
  338. /// 约耗时100ms+,不如OnDataReceived快(约50ms),故弃用。
  339. /// </summary>
  340. /// <param name="length"></param>
  341. /// <param name="width"></param>
  342. /// <param name="height"></param>
  343. public void CapturedUSImageData(int length, int width, int height)
  344. {
  345. try
  346. {
  347. var byteImage = _fileReaderForLiveVideoUltrasoundImage?.ReadFromFile(length);
  348. if (byteImage == null || byteImage.Length != length)
  349. {
  350. return;
  351. }
  352. var imageCaptureDataInfo = new CaptureDataInfo
  353. {
  354. Width = width,
  355. Height = height,
  356. Data = byteImage,
  357. };
  358. ImageDataCapturer.RaiseImdageDataCaptured(imageCaptureDataInfo);
  359. }
  360. catch (Exception ex)
  361. {
  362. Logger.WriteLineError($"LiveVideoServiceForAndroid CapturedUSImageData Error:{ex}");
  363. }
  364. }
  365. /// <summary>
  366. /// Capture ultrasound machie image data.
  367. /// </summary>
  368. /// <param name="fisVideoFrameData">image data</param>
  369. public void CapturedUSImageData_2(FISVideoFrameData fisVideoFrameData)
  370. {
  371. try
  372. {
  373. var imageCaptureDataInfo = new CaptureDataInfo
  374. {
  375. Width = fisVideoFrameData.Width,
  376. Height = fisVideoFrameData.Height,
  377. Data = fisVideoFrameData.Data,
  378. };
  379. ImageDataCapturer.RaiseImdageDataCaptured(imageCaptureDataInfo);
  380. }
  381. catch (Exception ex)
  382. {
  383. Logger.WriteLineError($"LiveVideoServiceForAndroid CapturedUSImageData Error:{ex}");
  384. }
  385. }
  386. /// <summary>
  387. /// Dispose
  388. /// </summary>
  389. public void Dispose()
  390. {
  391. try
  392. {
  393. Update(null);
  394. UpdateV2(null);
  395. }
  396. catch (Exception ex)
  397. {
  398. Logger.WriteLineError($"LiveVideoServiceForAndroid Dispose Error:{ex}");
  399. }
  400. }
  401. private void OnPreviewCameraCaptured(object sender, byte[] e)
  402. {
  403. try
  404. {
  405. if (e == null)
  406. {
  407. return;
  408. }
  409. if (FISIMPL.IsRunningJsonRpcMode)
  410. {
  411. _fileWriterForLiveVideoCameraPreviewImage.WriteToFileContinuous(e);
  412. }
  413. else
  414. {
  415. if (_imageFrameData == null || _imageFrameData.Size != e.Length)
  416. {
  417. return;
  418. }
  419. Marshal.Copy(e, 0, _imageFrameData.Data, e.Length);
  420. PreviewCameraCaptured?.Invoke(this, _imageFrameData);
  421. }
  422. }
  423. catch (Exception ex)
  424. {
  425. Logger.WriteLineError($"LiveVideoServiceForAndroid OnPreviewCameraCaptured Error:{ex}");
  426. }
  427. }
  428. private void OnLiveNotification(object sender, LiveNotificationArgs e)
  429. {
  430. try
  431. {
  432. var fisLiveNotificationArgs = new FISLiveNotificationArgs(e.IsLive, e.LiveWidth, e.LiveHeight);
  433. if (FISIMPL.IsRunningJsonRpcMode)
  434. {
  435. NotificationSender.SendNotification(new FISNotificationArgs
  436. {
  437. Params = fisLiveNotificationArgs,
  438. NotificationType = FISNotificationType.FISLiveVideoLiveNotification
  439. });
  440. }
  441. }
  442. catch (Exception ex)
  443. {
  444. Logger.WriteLineError($"LiveVideoServiceForAndroid OnLiveNotification Error:{ex}");
  445. }
  446. }
  447. private void OnExceptionThrows(object sender, Exception e)
  448. {
  449. if (sender is FileTransferReader fileReader)
  450. {
  451. Logger.WriteLineError($"File Reader {fileReader.Name} throw exception {e}");
  452. }
  453. else if (sender is FileTransferWriter fileWriter)
  454. {
  455. Logger.WriteLineError($"File Writer {fileWriter.Name} throw exception {e}");
  456. }
  457. }
  458. private void OnDataReceived(object sender, byte[] e)
  459. {
  460. FISVideoFrameData videoFrameData = new FISVideoFrameData();
  461. try
  462. {
  463. videoFrameData.LoadFromBytes(e);
  464. var imageCaptureDataInfo = new CaptureDataInfo
  465. {
  466. Width = videoFrameData.Width,
  467. Height = videoFrameData.Height,
  468. Data = videoFrameData.Data,
  469. };
  470. ImageDataCapturer.RaiseImdageDataCaptured(imageCaptureDataInfo);
  471. }
  472. catch (Exception ex)
  473. {
  474. Logger.WriteLineError($"LiveVideoServiceForAndroid OnDataReceived Error:{ex}");
  475. }
  476. finally
  477. {
  478. videoFrameData?.Close();
  479. }
  480. }
  481. public void ChangeRealTimeCaptureSetting(bool isStart)
  482. {
  483. throw new NotImplementedException();
  484. }
  485. /// <summary>
  486. /// Capture current Image
  487. /// </summary>
  488. public void CaptureCurrentImage()
  489. {
  490. throw new NotImplementedException();
  491. }
  492. /// <summary>
  493. /// Start Record Video
  494. /// </summary>
  495. public void StartRecordVideo()
  496. {
  497. throw new NotImplementedException();
  498. }
  499. /// <summary>
  500. /// Stop Record Video
  501. /// </summary>
  502. public void StopRecordVideo(bool isTimeOut)
  503. {
  504. throw new NotImplementedException();
  505. }
  506. public void ReUploadRestVid()
  507. {
  508. throw new NotImplementedException();
  509. }
  510. }
  511. }