VideoFileService.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using static QuickNV.HikvisionNetSDK.Defines;
  6. using static QuickNV.HikvisionNetSDK.Methods;
  7. namespace QuickNV.HikvisionNetSDK.Api.Service
  8. {
  9. public class VideoFileService
  10. {
  11. private HvSession session;
  12. internal VideoFileService(HvSession session)
  13. {
  14. this.session = session;
  15. }
  16. public HvRemoteFile[] FindFile(int channelId, DateTime startTime, DateTime stopTime)
  17. {
  18. NET_DVR_FILECOND_V40 findcond = new NET_DVR_FILECOND_V40()
  19. {
  20. lChannel = channelId,
  21. struStartTime = startTime.ToNET_DVR_TIME(),
  22. struStopTime = stopTime.ToNET_DVR_TIME(),
  23. };
  24. int iFindHandle = Invoke(NET_DVR_FindFile_V40(session.UserId, ref findcond));
  25. try
  26. {
  27. var fileList = new List<HvRemoteFile>();
  28. while (true)
  29. {
  30. NET_DVR_FINDDATA_V40 findData = default;
  31. var ret = Invoke(NET_DVR_FindNextFile_V40(iFindHandle, ref findData));
  32. //没有更多的文件,查找结束
  33. if (ret == NET_DVR_NOMOREFILE)
  34. break;
  35. //获取文件信息成功
  36. if (ret == NET_DVR_FILE_SUCCESS)
  37. {
  38. fileList.Add(new HvRemoteFile(findData));
  39. continue;
  40. }
  41. //未查找到文件
  42. if (ret == NET_DVR_FILE_NOFIND)
  43. break;
  44. //正在查找请等待
  45. if (ret == NET_DVR_ISFINDING)
  46. {
  47. Thread.Sleep(1000);
  48. continue;
  49. }
  50. //查找文件时异常
  51. if (ret == NET_DVR_FILE_EXCEPTION)
  52. {
  53. throw new IOException("NET_DVR_FindNextFile_V40 error,reason:" + NET_DVR_GetLastError());
  54. }
  55. throw new IOException($"NET_DVR_FindNextFile_V40 error,reason: Unknow return code:{ret}");
  56. }
  57. return fileList.ToArray();
  58. }
  59. catch
  60. {
  61. throw;
  62. }
  63. finally
  64. {
  65. NET_DVR_FindClose_V30(iFindHandle);
  66. }
  67. }
  68. public int StartDownloadFile(int channelId, DateTime startTime, DateTime stopTime, string destinationPath)
  69. {
  70. NET_DVR_PLAYCOND playcond = new NET_DVR_PLAYCOND()
  71. {
  72. dwChannel = (uint)channelId,
  73. struStartTime = startTime.ToNET_DVR_TIME(),
  74. struStopTime = stopTime.ToNET_DVR_TIME(),
  75. };
  76. int iPlayHandle = Invoke(NET_DVR_GetFileByTime_V40(session.UserId, destinationPath, ref playcond));
  77. uint iOutValue = 0;
  78. Invoke(NET_DVR_PlayBackControl_V40(iPlayHandle, NET_DVR_PLAYSTART, IntPtr.Zero, 0, IntPtr.Zero, ref iOutValue));
  79. return iPlayHandle;
  80. }
  81. public virtual int StartDownloadFile(string remoteFile, string destinationPath)
  82. {
  83. int iPlayHandle = Invoke(NET_DVR_GetFileByName(session.UserId, remoteFile, destinationPath));
  84. uint iOutValue = 0;
  85. Invoke(NET_DVR_PlayBackControl_V40(iPlayHandle, NET_DVR_PLAYSTART, IntPtr.Zero, 0, IntPtr.Zero, ref iOutValue));
  86. return iPlayHandle;
  87. }
  88. public int GetDownloadPosition(int fileHandle)=> Invoke(NET_DVR_GetDownloadPos(fileHandle));
  89. public void StopDownloadFile(int fileHandle)=> Invoke(NET_DVR_StopGetFile(fileHandle));
  90. }
  91. }