LiveVideoManager.cs 22 KB

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