libvideorecord_impl.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #pragma once
  2. #include "FFmpegWriter.h"
  3. #include "libvideorecord.h"
  4. #include "libaudioqueue.h"
  5. #include "libvideoqueue.h"
  6. #include "CvxText.h"
  7. #include "iaudionsinterface.h"
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #ifdef _WIN32
  11. #else
  12. #include <semaphore.h>
  13. #endif
  14. #ifndef CAPTURE_CLOCK
  15. #define CAPTURE_CLOCK 8000
  16. #endif
  17. #ifndef CAPTURE_FRAME_TIME
  18. #define CAPTURE_FRAME_TIME 20 // 20ms per frame
  19. #endif
  20. #ifndef CAPTURE_FRAME_MAX_LEN
  21. #define CAPTURE_FRAME_MAX_LEN 2048 // 48KHZ每20ms输出1920字节
  22. #endif
  23. #ifndef RECORD_FAILED_MAX_TIMES
  24. #define RECORD_FAILED_MAX_TIMES 5000
  25. #endif // !RECORD_FAILED_MAX_TIMES
  26. #ifndef GET_REMOTEVIDEO_FAILED_MAX_TIMES
  27. #define GET_REMOTEVIDEO_FAILED_MAX_TIMES 15
  28. #endif // !GET_REMOTEVIDEO_FAILED_MAX_TIMES
  29. #ifndef rvc_snprintf
  30. #ifdef _WIN32
  31. #define rvc_snprintf _snprintf
  32. #else
  33. #define rvc_snprintf snprintf
  34. #endif // _WIN32
  35. #endif // rvc_snprintf
  36. class libvideorecord_impl :
  37. public LogApi
  38. {
  39. public:
  40. libvideorecord_impl(bool* pResult, CHostApi* pHostAPI, const char* audioqueuename, const char* videoqueuename, const char* videoqueue2name, const char* salesaudioqueuename, const char* remotevideoqueuename, const char* remoteaudioqueuename);
  41. ~libvideorecord_impl();
  42. CHostApi* GetHostApi();
  43. int VideoRecord();
  44. bool StartVideoRecord(int fps, int videoquality, eVideoFormat eFormat, const SubtitleParam* subtitleParam, bool bWholeSection,
  45. bool bSessionManage, const char* pathname, int pathlength, const char* pfilename, int filenamelength, Rvc_RecordAudioParam_t* pAudioParam);//开始记录
  46. bool StopVideoRecord(); //退出
  47. bool PauseRecord(); //暂停录像
  48. bool ContinueRecord(); //继续录像
  49. void ChangeFilename(const char* newfilename);
  50. #ifdef _WIN32
  51. bool setRightWaterMark(const char* strWaterMark, uint32_t ulen);
  52. #else
  53. bool setRightWaterMark(const wchar_t* strWaterMark, uint32_t ulen);
  54. #endif
  55. bool EndRecord();
  56. void Debug(const char* fmt, ...);
  57. void vDebug(const char* str, va_list list);
  58. public:
  59. bool m_bCloseVideo; //是否关闭当前录像
  60. bool m_bReNameVideo; //是否重命名录像文件
  61. CHostApi* m_pHostApi;
  62. private:
  63. //开始录制视频
  64. bool StartRecord();
  65. //获取指定队列的SIZE
  66. int GetVideoFrameSize(int& nWidth, int& nHeight, Clibvideoqueue* queue);
  67. //从指定队列读取video
  68. bool GetVideoFrame(video_frame* Video, int flags, Clibvideoqueue* queue);
  69. bool IsRecordingCamError(int iRecordingCam, eRvcRecordType eRecordType = eStand2Agent);
  70. bool InitCvxText();
  71. void GetVideoFullName();
  72. bool AddCvxText(unsigned char** pData, int iDataSize, int iWidth, int iHeight);
  73. bool InitRecordWriter(char* filename, int width, int height, int colorbit, int nfps,
  74. int nSamplePsec, int nchannels, int nBitPerSample, int nmaxspacing, int nquality, int nOutBitRate, int iAudioType);
  75. bool StartRecordWrite();
  76. bool ReceiveRecordVideoData(unsigned char* pData, unsigned long len);
  77. bool ReceiveRecordAudioData(unsigned char* pData, unsigned long len);
  78. bool EndRecordWrite();
  79. bool ReNameVideo();
  80. bool InitRecordParams();
  81. bool CalcuRecordAudioParams(audio_frame* paudio);
  82. bool InitRecordAudioBuffer();
  83. bool Rvc_Timeout(int ms);
  84. bool InitAudioQueue();
  85. bool InitVideoQueue();
  86. bool InitMediaQueueInfos(const char* audioqueuename, const char* videoqueuename, const char* videoqueue2name, const char* salesaudioqueuename, const char* remotevideoqueuename, const char* remoteaudioqueuename);
  87. bool GetRecordVideoFrameSize();
  88. bool GetRecordAudioFrameSize();
  89. bool InitRecordVideoFrameBuffer();
  90. bool GetRecordAudioParams(audio_frame** paudio);
  91. int GetSingleSideVideoFrameSize(int& iWidth, int& iHeight);
  92. int GetDoubleSideVideoFrameSize(int& iWidth, int& iHeight);
  93. int GetDestTypeVideoFrameSize(int& iWidth, int& iHeight, eRvcRecordType eRecordType);
  94. int GetSingleCameraVideoFrameData(videoq_frame* Video, int flags, eRvcRecordType eRecordType);
  95. int GetDoubleCameraVideoFrameData(videoq_frame* Video, int flags, const bool bSwitchCam, int iInitCam, eRvcRecordType eRecordType);
  96. int GetStand2SVideoFrameData(videoq_frame* Video, int flags, const bool bSwitchCam, int iInitCam, eRvcRecordType eRecordType);
  97. int GetDestTypeVideoFrameData(videoq_frame* Video, int flags, const bool bSwitchCam, int iInitCam, eRvcRecordType eRecordType);
  98. int GetSingleSideVideoFrameData(videoq_frame* Video, int flags, const bool bSwitchCam, int iInitCam);
  99. void LogFailedEvent(eRvcRecordFailedCase eCase, const char* pszMessage, bool bRecordDevFault = false);
  100. void MediaSynchronous();
  101. int WriteVideoFrame();
  102. int WriteAudioFrame();
  103. bool GetRecordAudioFrame();
  104. bool GetSingleSideAudioFrame();
  105. bool GetLocalAudioFrame(int iAudioLens);
  106. bool GetRemoteAudioFrame(int iAudioLens);
  107. bool ReSetRecordParams();
  108. bool InitVideoRecordParams();
  109. int HandleLeftAudioData();
  110. bool AddMuteAudioFrame(bool bLocal, int iAudioFrameSize);
  111. bool InitDefaultAudioParams(audio_frame* paudio);
  112. bool ResetMediaQueue();
  113. private:
  114. bool m_bStopRecord;
  115. eRvcRecordType m_eRecordType; //录制双向的视频类型,0:单向录制,1:坐席<->大机双录
  116. eAudioOutPutType m_eAudioType;
  117. bool m_bIsAudioNsOn; //是否开启音频降噪功能开关
  118. int m_iNsPolicy; //音频降噪等级
  119. bool m_bIsAudioTransOn; //是否开始音频传输到识别服务器开关
  120. int m_iAudioChannels; //录制音频通道数
  121. bool m_bPauseRecord;
  122. bool m_bMuteAudio;
  123. int m_nFps;
  124. int m_videoquality;
  125. eVideoFormat m_eFormat;
  126. SubtitleParam m_SubtitleParam;
  127. bool m_bWholeSection;
  128. bool m_bSessionManage;
  129. /************************************************************************/
  130. videoq_frame* m_videoframe;
  131. audio_frame* m_audioframe;
  132. Clibaudioqueue* m_audioqueue;
  133. Clibvideoqueue* m_env_videoqueue;
  134. Clibvideoqueue* m_opt_videoqueue;
  135. Clibvideoqueue* m_remote_videoqueue; //远端视频,用于双向录像
  136. Clibaudioqueue* m_remote_audioqueue; //远端音频,用于双向录像
  137. Clibaudioqueue* m_local_audioqueue; //本地音频
  138. Clibaudioqueue* m_sales_audioqueue; //现场销售音频
  139. #ifdef _WIN32
  140. HANDLE m_hEventWait; // CreateEvent
  141. HANDLE m_hRecordThread;
  142. unsigned int m_nRecordthreadId;
  143. #else
  144. sem_t m_semt;
  145. pthread_t m_nRecordthreadId;
  146. #endif
  147. char m_audioqueuename[MAX_PATH];
  148. char m_env_videoqueuename[MAX_PATH];
  149. char m_opt_videoqueuename[MAX_PATH];
  150. char m_salesaudioqueuename[MAX_PATH];
  151. char m_remotevideoqueuename[MAX_PATH];
  152. char m_remoteaudioqueuename[MAX_PATH];
  153. char m_FileName[MAX_PATH];
  154. char m_PathName[MAX_PATH];
  155. char m_VideoFileName[MAX_PATH];
  156. char m_VideoFomat[MAX_PATH];
  157. bool* m_bResult;
  158. char* m_pRecordAudioBuffer; //音频录制拼接BUFFER
  159. int m_iRecordAudioBufSize; //音频拼接的缓冲区大小;
  160. FFmpegWriter* m_pFFmpegWriter;
  161. IAudioNs* m_pAudioNsObj; //audio noise suppression
  162. CvxText* m_pText;
  163. int m_iFileSerialNum;
  164. int m_iAudioFrameSize; //录制音频帧的大小
  165. int m_iVideoFrameSize; //录制视频帧的大小
  166. int m_iWidth; //录制视频帧的长度
  167. int m_iHeight; //录制视频帧的高度
  168. int m_iAudioPerSecBufLen; //每秒音频的长度
  169. int m_iLocalAudioFps; //本地音频频率
  170. int m_iRemoteAudioFps; //远端音频频率
  171. char* m_pRemoteAudioBuffer; //远端音频BUFFER
  172. int m_iRecordedTime; //本段视频已录制时间
  173. int m_iRemoteAudioNum; //远端音频数量
  174. int m_iAudioFromQueNum; //从音频队列中取出的音频数量
  175. int m_iRecordVideoNum; //已录制的视频帧数
  176. int m_iRecordAudioNum ; //已录制的音频数
  177. int m_iSubTitleTime; //本段视频添加字幕耗时
  178. int m_iRemoteAudioBufLen; //远端音频拼接BUFFER的当前长度
  179. int m_iAudioBufferLen; //音频录制拼接BUFFER的当前长度
  180. int m_iRemoteVideoFailedTimes; //获取远端视频失败次数
  181. bool m_bSwitchCam; //录制时是否切换摄像头
  182. };