LiveVideoManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using FISLib.Connect;
  2. using FISLib.Hardware;
  3. using FISLib.LiveVideo;
  4. using FISLib.Remedical;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Vinno.FIS.Sonopost.Common;
  12. using Vinno.FIS.Sonopost.Features.Config;
  13. using Vinno.FIS.Sonopost.Features.Dicom;
  14. using Vinno.FIS.Sonopost.Managers.Interfaces;
  15. using Vinno.IUS.Common.Log;
  16. namespace Vinno.FIS.Sonopost.Managers
  17. {
  18. internal class LiveVideoManager : SonopostManager, ILiveVideoManager
  19. {
  20. private readonly ILoginManager _loginManager;
  21. private readonly ILiveVideoService _fisLiveVideoService;
  22. private readonly IRemedicalManager _remedicalManager;
  23. private readonly IKeyBoardListenManager _keyBoardListenManager;
  24. private IDeviceManager _deviceManager;
  25. private CameraSettingForSonopost _cameraSetting;
  26. private bool _isRecording;
  27. private AutoResetEvent _checkImageRecordEvent = new AutoResetEvent(false);
  28. /// <summary>
  29. /// 超声高清采集卡名称列表
  30. /// </summary>
  31. public List<string> SonoDeviceNames { get; private set; }
  32. /// <summary>
  33. /// 超声采集卡名称
  34. /// </summary>
  35. public FISCameraInfo SonoDevice { get; private set; }
  36. public event EventHandler<FISImageFrameData> PreviewImageReceived;
  37. public LiveVideoManager()
  38. {
  39. _loginManager = AppManager.Instance.GetManager<ILoginManager>();
  40. _keyBoardListenManager = AppManager.Instance.GetManager<IKeyBoardListenManager>();
  41. _keyBoardListenManager.LeftKeyPressedEvent += OnLeftKeyPressedEvent;
  42. _keyBoardListenManager.RightKeyPressedEvent += OnRightKeyPressedEvent;
  43. _fisLiveVideoService = AppManager.Instance.GetManager<IFISManager>().FISLiveVideoService;
  44. _fisLiveVideoService.PreviewCameraCaptured += OnPreviewCameraCaptured;
  45. _fisLiveVideoService.CaptureImageGenerated += OnCaptureImageGenerated;
  46. _fisLiveVideoService.RecordVideoGenerated += OnRecordVideoGenerated;
  47. _remedicalManager = AppManager.Instance.GetManager<IRemedicalManager>();
  48. _cameraSetting = new CameraSettingForSonopost(false, FISLiveChannelCategory.Main, new List<FISVideoDeviceInfo>(), new FISRainbowImageDetectConfig()
  49. {
  50. IsDetectRainbowImage = SonopostSystemSettings.Instance.RainbowImageDetectSetting.IsDetectRainbowImage,
  51. BeforeDisableIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.BeforeDisableIntervalTime,
  52. BeforeEnableIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.BeforeEnableIntervalTime,
  53. AfterEnableIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.AfterEnableIntervalTime,
  54. ScanIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.ScanIntervalTime,
  55. CaptureCardList = SonopostSystemSettings.Instance.RainbowImageDetectSetting.CaptureCardList.ToList(),
  56. }, new FISMicDeviceInfo());
  57. }
  58. public void Init()
  59. {
  60. _deviceManager = AppManager.Instance.GetManager<IDeviceManager>();
  61. SonoDeviceNames = SonopostSystemSettings.Instance.InputDeviceNames.ToList();
  62. var cameras = _deviceManager.GetCameras();
  63. if (cameras != null)
  64. {
  65. foreach (var camera in cameras)
  66. {
  67. if (SonoDeviceNames.Contains(camera.Name.Trim()))
  68. {
  69. var capbilities = camera.Capabilities.Where(x => x.Width <= 1920 && x.Height <= 1080).ToList();
  70. SonoDevice = new FISCameraInfo(camera.Id, camera.Name, camera.HardwareId, capbilities);
  71. }
  72. }
  73. }
  74. }
  75. private void OnLeftKeyPressedEvent(object sender, EventArgs e)
  76. {
  77. try
  78. {
  79. if (_loginManager.DeviceStatus != DeviceStatus.Logon)
  80. {
  81. Logger.WriteLineError($"LiveVideoManager OnLeftKeyPressedEvent Invoke Error,please connect the vCloud Server first");
  82. return;
  83. }
  84. if (!SonopostUserDefinedSettings.Instance.CaptureSetting.RealTimeCaptureEnabled)
  85. {
  86. Logger.WriteLineError($"LiveVideoManager OnLeftKeyPressedEvent Invoke,But the CaptureEnable is False,so skipped it");
  87. return;
  88. }
  89. if (SonopostUserDefinedSettings.Instance.CaptureSetting.ImageCaptureKeyForFootToogle == EnumFootToggleKey.LeftKey)
  90. {
  91. _fisLiveVideoService.CaptureCurrentImage();
  92. }
  93. else if (SonopostUserDefinedSettings.Instance.CaptureSetting.VideoCaptureKeyForFootToggle == EnumFootToggleKey.LeftKey)
  94. {
  95. if (!_isRecording)
  96. {
  97. _fisLiveVideoService.StartRecordVideo();
  98. TimeJudgment();
  99. _isRecording = true;
  100. }
  101. else
  102. {
  103. _fisLiveVideoService.StopRecordVideo(false);
  104. _isRecording = false;
  105. _checkImageRecordEvent.Set();
  106. }
  107. }
  108. else
  109. {
  110. Logger.WriteLineError($"LiveVideoManager OnLeftKeyPressedEvent Error,capture or record is all not left key");
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. Logger.WriteLineError($"LiveVideoManager OnLeftKeyPressedEvent Error:{ex}");
  116. }
  117. }
  118. private void OnRightKeyPressedEvent(object sender, EventArgs e)
  119. {
  120. try
  121. {
  122. if (_loginManager.DeviceStatus != DeviceStatus.Logon)
  123. {
  124. Logger.WriteLineError($"LiveVideoManager OnRightKeyPressedEvent Invoke Error,please connect the vCloud Server first");
  125. return;
  126. }
  127. if (!SonopostUserDefinedSettings.Instance.CaptureSetting.RealTimeCaptureEnabled)
  128. {
  129. Logger.WriteLineError($"LiveVideoManager OnRightKeyPressedEvent Invoke,But the CaptureEnable is False,so skipped it");
  130. return;
  131. }
  132. if (SonopostUserDefinedSettings.Instance.CaptureSetting.ImageCaptureKeyForFootToogle == EnumFootToggleKey.RightKey)
  133. {
  134. _fisLiveVideoService.CaptureCurrentImage();
  135. }
  136. else if (SonopostUserDefinedSettings.Instance.CaptureSetting.VideoCaptureKeyForFootToggle == EnumFootToggleKey.RightKey)
  137. {
  138. if (!_isRecording)
  139. {
  140. _fisLiveVideoService.StartRecordVideo();
  141. TimeJudgment();
  142. _isRecording = true;
  143. }
  144. else
  145. {
  146. _fisLiveVideoService.StopRecordVideo(false);
  147. _isRecording = false;
  148. _checkImageRecordEvent.Set();
  149. }
  150. }
  151. else
  152. {
  153. Logger.WriteLineError($"LiveVideoManager OnRightKeyPressedEvent Error,capture or record is all not left key");
  154. }
  155. }
  156. catch (Exception ex)
  157. {
  158. Logger.WriteLineError($"LiveVideoManager OnRightKeyPressedEvent Error:{ex}");
  159. }
  160. }
  161. private void TimeJudgment()
  162. {
  163. Task.Run(() =>
  164. {
  165. try
  166. {
  167. _checkImageRecordEvent.Reset();
  168. _checkImageRecordEvent.WaitOne(30000);
  169. if (_isRecording)
  170. {
  171. _fisLiveVideoService.StopRecordVideo(true);
  172. _isRecording = false;
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. Logger.WriteLineError($"LivevideoManager TimeJudgment Error:{ex}");
  178. }
  179. });
  180. }
  181. private void OnCaptureImageGenerated(object sender, string e)
  182. {
  183. try
  184. {
  185. if (string.IsNullOrEmpty(e))
  186. {
  187. Logger.WriteLineError($"Capture Image Generated Failed ,File Path is null");
  188. return;
  189. }
  190. if (!File.Exists(e))
  191. {
  192. Logger.WriteLineError($"Capture Image Generated Failed: {e} is not exist");
  193. return;
  194. }
  195. if (Path.GetExtension(e).ToLower() != $".{SonopostConstants.VidFileName}")
  196. {
  197. Logger.WriteLineError($"Capture Image Generated Failed: {e} is not vid file");
  198. return;
  199. }
  200. var vidInfo = new VidInfo()
  201. {
  202. VidFilePath = e,
  203. };
  204. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  205. {
  206. var examRecordId = _remedicalManager.GetCollcetingRecordCode();
  207. if (!string.IsNullOrEmpty(examRecordId))
  208. {
  209. var detailInfo = _remedicalManager.GetvCloudExamDetailInfoByExamRecordId(examRecordId);
  210. if (detailInfo == null)
  211. {
  212. Logger.WriteLineError($"LiveVideoManager GetvCloudExamDetailInfoByExamRecordId Result is null");
  213. }
  214. else
  215. {
  216. if (detailInfo.ExamStatus == FISRecordStatus.NotScanned || detailInfo.ExamStatus == FISRecordStatus.Uploaded)
  217. {
  218. vidInfo.ExamRecordId = examRecordId;
  219. vidInfo.PatientInfo = detailInfo.PatientScanInfo;
  220. }
  221. }
  222. }
  223. }
  224. DicomUploadQueue.Instance.Enqueue(vidInfo);
  225. }
  226. catch (Exception ex)
  227. {
  228. Logger.WriteLineError($"LiveVideoManager OnCaptureImageGenerated Error:{ex}");
  229. }
  230. }
  231. private void OnRecordVideoGenerated(object sender, string e)
  232. {
  233. try
  234. {
  235. if (string.IsNullOrEmpty(e))
  236. {
  237. Logger.WriteLineError($"Record Video Generated Failed, File Path is null");
  238. return;
  239. }
  240. if (!File.Exists(e))
  241. {
  242. Logger.WriteLineError($"Record Video Generated Failed: {e} is not exist");
  243. return;
  244. }
  245. if (Path.GetExtension(e).ToLower() != $".{SonopostConstants.VidFileName}")
  246. {
  247. Logger.WriteLineError($"Record Video Generated Failed: {e} is not vid file");
  248. return;
  249. }
  250. var vidInfo = new VidInfo()
  251. {
  252. VidFilePath = e,
  253. };
  254. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  255. {
  256. var examRecordId = _remedicalManager.GetCollcetingRecordCode();
  257. if (!string.IsNullOrEmpty(examRecordId))
  258. {
  259. var detailInfo = _remedicalManager.GetvCloudExamDetailInfoByExamRecordId(examRecordId);
  260. if (detailInfo == null)
  261. {
  262. Logger.WriteLineError($"LiveVideoManager GetvCloudExamDetailInfoByExamRecordId Result is null");
  263. }
  264. else
  265. {
  266. if (detailInfo.ExamStatus == FISRecordStatus.NotScanned || detailInfo.ExamStatus == FISRecordStatus.Uploaded)
  267. {
  268. vidInfo.ExamRecordId = examRecordId;
  269. vidInfo.PatientInfo = detailInfo.PatientScanInfo;
  270. }
  271. }
  272. }
  273. }
  274. DicomUploadQueue.Instance.Enqueue(vidInfo);
  275. }
  276. catch (Exception ex)
  277. {
  278. Logger.WriteLineError($"LiveVideoManager OnCaptureImageGenerated Error:{ex}");
  279. }
  280. }
  281. private void OnPreviewCameraCaptured(object sender, FISImageFrameData e)
  282. {
  283. try
  284. {
  285. PreviewImageReceived?.Invoke(this, e);
  286. }
  287. catch (Exception ex)
  288. {
  289. Logger.WriteLineError($"Live Video Manager On Preview Camera Captured Error:{ex}");
  290. }
  291. }
  292. public List<string> GetBrandList()
  293. {
  294. try
  295. {
  296. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  297. {
  298. return _fisLiveVideoService.GetBrandList();
  299. }
  300. return new List<string>();
  301. }
  302. catch (Exception e)
  303. {
  304. Logger.WriteLineError($"GetBrandList Error:{e}");
  305. return new List<string>();
  306. }
  307. }
  308. public List<string> GetModelList(string brand)
  309. {
  310. try
  311. {
  312. if (string.IsNullOrEmpty(brand))
  313. {
  314. return new List<string>();
  315. }
  316. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  317. {
  318. return _fisLiveVideoService.GetModelList(brand);
  319. }
  320. return new List<string>();
  321. }
  322. catch (Exception e)
  323. {
  324. Logger.WriteLineError($"GetModelList Error:{e}");
  325. return new List<string>();
  326. }
  327. }
  328. public FISDeviceRecommandResolution GetRecommandResolution(string brand, string model)
  329. {
  330. try
  331. {
  332. if (string.IsNullOrEmpty(brand) || string.IsNullOrEmpty(model))
  333. {
  334. return null;
  335. }
  336. return _fisLiveVideoService.GetRecommandResolution(brand, model);
  337. }
  338. catch (Exception e)
  339. {
  340. Logger.WriteLineError($"GetRecommandResolution Error:{e}");
  341. return null;
  342. }
  343. }
  344. public void UpdateDeviceResoution()
  345. {
  346. try
  347. {
  348. _cameraSetting.CameraPreviewEnabled = false;
  349. Logger.WriteLineInfo("LiveVideoManager UpdateDeviceResoution");
  350. var list = new List<FISVideoDeviceInfo>();
  351. foreach (var device in SonopostUserDefinedSettings.Instance.HardwareSetting.VideoDeviceInfoList)
  352. {
  353. if (device.IsAvailable && device.IsEnable)
  354. {
  355. var item = device.Clone() as FISVideoDeviceInfo;
  356. list.Add(item);
  357. }
  358. }
  359. _cameraSetting.VideoDeviceInfoList = list;
  360. _cameraSetting.MicDeviceInfo = SonopostUserDefinedSettings.Instance.HardwareSetting.MicDeviceInfo;
  361. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  362. {
  363. _fisLiveVideoService.ChangeCameraSettingForSonopost(_cameraSetting);
  364. }
  365. }
  366. catch (Exception ex)
  367. {
  368. Logger.WriteLineError($"LiveVideoManager Update Device Resoution Error:{ex}");
  369. }
  370. }
  371. public void UpdateRainbowImageDetectSetting()
  372. {
  373. try
  374. {
  375. _cameraSetting.RainbowImageDetectConfig.IsDetectRainbowImage = SonopostSystemSettings.Instance.RainbowImageDetectSetting.IsDetectRainbowImage;
  376. _cameraSetting.RainbowImageDetectConfig.BeforeDisableIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.BeforeDisableIntervalTime;
  377. _cameraSetting.RainbowImageDetectConfig.BeforeEnableIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.BeforeEnableIntervalTime;
  378. _cameraSetting.RainbowImageDetectConfig.AfterEnableIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.AfterEnableIntervalTime;
  379. _cameraSetting.RainbowImageDetectConfig.ScanIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.ScanIntervalTime;
  380. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  381. {
  382. _fisLiveVideoService.ChangeCameraSettingForSonopost(_cameraSetting);
  383. }
  384. }
  385. catch (Exception ex)
  386. {
  387. Logger.WriteLineError($"LiveVideoManager Update RainbowImageDetectSetting Error:{ex}");
  388. }
  389. }
  390. public void StartPreview(string id, int width, int height, int fps)
  391. {
  392. try
  393. {
  394. _cameraSetting.CameraPreviewEnabled = true;
  395. var deviceParameter = SonopostUserDefinedSettings.Instance.HardwareSetting.VideoDeviceInfoList;
  396. var capturingDevice = deviceParameter.FirstOrDefault(d => d.Id == id && d.IsEnable);
  397. if (capturingDevice != null)
  398. {
  399. _cameraSetting.CurrentLiveChannelCategory = capturingDevice.Category;
  400. }
  401. else
  402. {
  403. _cameraSetting.CameraPreviewEnabled = false;
  404. _cameraSetting.CurrentLiveChannelCategory = FISLiveChannelCategory.Main;
  405. }
  406. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  407. {
  408. _fisLiveVideoService.ChangeCameraSettingForSonopost(_cameraSetting);
  409. }
  410. }
  411. catch (Exception ex)
  412. {
  413. Logger.WriteLineError($"LiveVideoManager Start Preview Error:{ex}");
  414. }
  415. }
  416. public void StopPreview()
  417. {
  418. try
  419. {
  420. _cameraSetting.CameraPreviewEnabled = false;
  421. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  422. {
  423. _fisLiveVideoService.ChangeCameraSettingForSonopost(_cameraSetting);
  424. }
  425. }
  426. catch (Exception ex)
  427. {
  428. Logger.WriteLineError($"LiveVideoManager Stop Preview Error:{ex}");
  429. }
  430. }
  431. public void ChangeRealTimeCaptureSetting()
  432. {
  433. try
  434. {
  435. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  436. {
  437. _fisLiveVideoService.ChangeRealTimeCaptureSetting(SonopostUserDefinedSettings.Instance.CaptureSetting.RealTimeCaptureEnabled);
  438. }
  439. }
  440. catch (Exception ex)
  441. {
  442. Logger.WriteLineError($"LiveVideoManager ChangeRealTimeCaptureSetting Error:{ex}");
  443. }
  444. }
  445. public void ReUploadRestVid()
  446. {
  447. try
  448. {
  449. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  450. {
  451. _fisLiveVideoService.ReUploadRestVid();
  452. }
  453. }
  454. catch (Exception ex)
  455. {
  456. Logger.WriteLineError($"LiveVideoManager ReuploadRestVids Error:{ex}");
  457. }
  458. }
  459. public override void DoDispose()
  460. {
  461. try
  462. {
  463. _keyBoardListenManager.LeftKeyPressedEvent -= OnLeftKeyPressedEvent;
  464. _keyBoardListenManager.RightKeyPressedEvent -= OnRightKeyPressedEvent;
  465. _fisLiveVideoService.PreviewCameraCaptured -= OnPreviewCameraCaptured;
  466. _fisLiveVideoService.CaptureImageGenerated -= OnCaptureImageGenerated;
  467. _fisLiveVideoService.RecordVideoGenerated -= OnRecordVideoGenerated;
  468. }
  469. catch (Exception ex)
  470. {
  471. Logger.WriteLineError($"LiveVideoManager DoDispose Error:{ex}");
  472. }
  473. base.DoDispose();
  474. }
  475. }
  476. }