LiveVideoManager.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. _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.GetvCloudExamDetailInfoByExamRecordId(examRecordId);
  212. if (detailInfo == null)
  213. {
  214. Logger.WriteLineError($"LiveVideoManager GetvCloudExamDetailInfoByExamRecordId {examRecordId} Result is null");
  215. }
  216. else
  217. {
  218. if (detailInfo.ExamStatus == FISRecordStatus.NotScanned || detailInfo.ExamStatus == FISRecordStatus.Uploaded)
  219. {
  220. vidInfo.ExamRecordId = examRecordId;
  221. vidInfo.PatientInfo = detailInfo.PatientScanInfo;
  222. }
  223. Logger.WriteLineInfo($"{examRecordId} ExamStatus is {detailInfo.ExamStatus}");
  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.GetvCloudExamDetailInfoByExamRecordId(examRecordId);
  263. if (detailInfo == null)
  264. {
  265. Logger.WriteLineError($"LiveVideoManager GetvCloudExamDetailInfoByExamRecordId Result is null");
  266. }
  267. else
  268. {
  269. if (detailInfo.ExamStatus == FISRecordStatus.NotScanned || detailInfo.ExamStatus == FISRecordStatus.Uploaded)
  270. {
  271. vidInfo.ExamRecordId = examRecordId;
  272. vidInfo.PatientInfo = detailInfo.PatientScanInfo;
  273. }
  274. }
  275. }
  276. }
  277. DicomUploadQueue.Instance.Enqueue(vidInfo);
  278. }
  279. catch (Exception ex)
  280. {
  281. Logger.WriteLineError($"LiveVideoManager OnCaptureImageGenerated Error:{ex}");
  282. }
  283. }
  284. private void OnPreviewCameraCaptured(object sender, FISImageFrameData e)
  285. {
  286. try
  287. {
  288. PreviewImageReceived?.Invoke(this, e);
  289. }
  290. catch (Exception ex)
  291. {
  292. Logger.WriteLineError($"Live Video Manager On Preview Camera Captured Error:{ex}");
  293. }
  294. }
  295. public List<string> GetBrandList()
  296. {
  297. try
  298. {
  299. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  300. {
  301. return _fisLiveVideoService.GetBrandList();
  302. }
  303. return new List<string>();
  304. }
  305. catch (Exception e)
  306. {
  307. Logger.WriteLineError($"GetBrandList Error:{e}");
  308. return new List<string>();
  309. }
  310. }
  311. public List<string> GetModelList(string brand)
  312. {
  313. try
  314. {
  315. if (string.IsNullOrEmpty(brand))
  316. {
  317. return new List<string>();
  318. }
  319. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  320. {
  321. return _fisLiveVideoService.GetModelList(brand);
  322. }
  323. return new List<string>();
  324. }
  325. catch (Exception e)
  326. {
  327. Logger.WriteLineError($"GetModelList Error:{e}");
  328. return new List<string>();
  329. }
  330. }
  331. public FISDeviceRecommandResolution GetRecommandResolution(string brand, string model)
  332. {
  333. try
  334. {
  335. if (string.IsNullOrEmpty(brand) || string.IsNullOrEmpty(model))
  336. {
  337. return null;
  338. }
  339. return _fisLiveVideoService.GetRecommandResolution(brand, model);
  340. }
  341. catch (Exception e)
  342. {
  343. Logger.WriteLineError($"GetRecommandResolution Error:{e}");
  344. return null;
  345. }
  346. }
  347. public void UpdateDeviceResoution()
  348. {
  349. try
  350. {
  351. _cameraSetting.CameraPreviewEnabled = false;
  352. Logger.WriteLineInfo("LiveVideoManager UpdateDeviceResoution");
  353. var list = new List<FISVideoDeviceInfo>();
  354. foreach (var device in SonopostUserDefinedSettings.Instance.HardwareSetting.VideoDeviceInfoList)
  355. {
  356. if (device.IsAvailable && device.IsEnable)
  357. {
  358. var item = device.Clone() as FISVideoDeviceInfo;
  359. list.Add(item);
  360. }
  361. }
  362. _cameraSetting.VideoDeviceInfoList = list;
  363. _cameraSetting.MicDeviceInfo = SonopostUserDefinedSettings.Instance.HardwareSetting.MicDeviceInfo;
  364. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  365. {
  366. _fisLiveVideoService.ChangeCameraSettingForSonopost(_cameraSetting);
  367. }
  368. }
  369. catch (Exception ex)
  370. {
  371. Logger.WriteLineError($"LiveVideoManager Update Device Resoution Error:{ex}");
  372. }
  373. }
  374. public void UpdateRainbowImageDetectSetting()
  375. {
  376. try
  377. {
  378. _cameraSetting.RainbowImageDetectConfig.IsDetectRainbowImage = SonopostSystemSettings.Instance.RainbowImageDetectSetting.IsDetectRainbowImage;
  379. _cameraSetting.RainbowImageDetectConfig.BeforeDisableIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.BeforeDisableIntervalTime;
  380. _cameraSetting.RainbowImageDetectConfig.BeforeEnableIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.BeforeEnableIntervalTime;
  381. _cameraSetting.RainbowImageDetectConfig.AfterEnableIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.AfterEnableIntervalTime;
  382. _cameraSetting.RainbowImageDetectConfig.ScanIntervalTime = SonopostSystemSettings.Instance.RainbowImageDetectSetting.ScanIntervalTime;
  383. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  384. {
  385. _fisLiveVideoService.ChangeCameraSettingForSonopost(_cameraSetting);
  386. }
  387. }
  388. catch (Exception ex)
  389. {
  390. Logger.WriteLineError($"LiveVideoManager Update RainbowImageDetectSetting Error:{ex}");
  391. }
  392. }
  393. public void StartPreview(string id, int width, int height, int fps)
  394. {
  395. try
  396. {
  397. _cameraSetting.CameraPreviewEnabled = true;
  398. var deviceParameter = SonopostUserDefinedSettings.Instance.HardwareSetting.VideoDeviceInfoList;
  399. var capturingDevice = deviceParameter.FirstOrDefault(d => d.Id == id && d.IsEnable);
  400. if (capturingDevice != null)
  401. {
  402. _cameraSetting.CurrentLiveChannelCategory = capturingDevice.Category;
  403. }
  404. else
  405. {
  406. _cameraSetting.CameraPreviewEnabled = false;
  407. _cameraSetting.CurrentLiveChannelCategory = FISLiveChannelCategory.Main;
  408. }
  409. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  410. {
  411. _fisLiveVideoService.ChangeCameraSettingForSonopost(_cameraSetting);
  412. }
  413. }
  414. catch (Exception ex)
  415. {
  416. Logger.WriteLineError($"LiveVideoManager Start Preview Error:{ex}");
  417. }
  418. }
  419. public void StopPreview()
  420. {
  421. try
  422. {
  423. _cameraSetting.CameraPreviewEnabled = false;
  424. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  425. {
  426. _fisLiveVideoService.ChangeCameraSettingForSonopost(_cameraSetting);
  427. }
  428. }
  429. catch (Exception ex)
  430. {
  431. Logger.WriteLineError($"LiveVideoManager Stop Preview Error:{ex}");
  432. }
  433. }
  434. public void ChangeRealTimeCaptureSetting()
  435. {
  436. try
  437. {
  438. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  439. {
  440. _fisLiveVideoService.ChangeRealTimeCaptureSetting(SonopostUserDefinedSettings.Instance.CaptureSetting.RealTimeCaptureEnabled);
  441. }
  442. }
  443. catch (Exception ex)
  444. {
  445. Logger.WriteLineError($"LiveVideoManager ChangeRealTimeCaptureSetting Error:{ex}");
  446. }
  447. }
  448. private void ReUploadAll()
  449. {
  450. try
  451. {
  452. if (_loginManager.DeviceStatus == DeviceStatus.Logon)
  453. {
  454. _fisLiveVideoService.ReUploadRestVid();
  455. }
  456. }
  457. catch (Exception ex)
  458. {
  459. Logger.WriteLineError($"LiveVideoManager ReuploadRestVids Error:{ex}");
  460. }
  461. }
  462. public override void DoDispose()
  463. {
  464. try
  465. {
  466. _loginManager.LoginStatusChanged -= OnLoginStatusChanged;
  467. _keyBoardListenManager.LeftKeyPressedEvent -= OnLeftKeyPressedEvent;
  468. _keyBoardListenManager.RightKeyPressedEvent -= OnRightKeyPressedEvent;
  469. _fisLiveVideoService.PreviewCameraCaptured -= OnPreviewCameraCaptured;
  470. _fisLiveVideoService.CaptureImageGenerated -= OnCaptureImageGenerated;
  471. _fisLiveVideoService.RecordVideoGenerated -= OnRecordVideoGenerated;
  472. }
  473. catch (Exception ex)
  474. {
  475. Logger.WriteLineError($"LiveVideoManager DoDispose Error:{ex}");
  476. }
  477. base.DoDispose();
  478. }
  479. private void OnLoginStatusChanged(object sender, DeviceStatus e)
  480. {
  481. switch (e)
  482. {
  483. case DeviceStatus.Logon:
  484. UpdateDeviceResoution();
  485. if (!_loginManager.IsConnectWithOldServer)
  486. {
  487. ChangeRealTimeCaptureSetting();
  488. ReUploadAll();
  489. }
  490. break;
  491. default:
  492. _isRecording = false;
  493. _checkImageRecordEvent.Set();
  494. break;
  495. }
  496. }
  497. }
  498. }