video_recorder.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "video_recorder.h"
  2. #include "avrecorder/capturer/video/VideoCaptureManager.h"
  3. #include <Windows.h>
  4. #include <capturer/finder.h>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <mutex>
  8. using namespace avrecorder::video;
  9. bool VideoRecorder::Open(HWND srcHwnd, Encoder<MediaType::VIDEO>::Param& param, CaptureMethod method)
  10. {
  11. CaptureTarget target;
  12. target.type = CaptureTargetType::Window;
  13. target.hwnd = srcHwnd;
  14. RECT clientRect;
  15. if (!GetClientRect(srcHwnd, &clientRect))
  16. return false;
  17. int width = clientRect.right - clientRect.left;
  18. int height = clientRect.bottom - clientRect.top;
  19. _capturer.open(target, method, width, height);
  20. return _Open(param);
  21. }
  22. bool VideoRecorder::Open(int monitorIdx,
  23. Encoder<MediaType::VIDEO>::Param& param,
  24. CaptureMethod method)
  25. {
  26. CaptureTarget target;
  27. target.type = CaptureTargetType::Monitor;
  28. target.monitorIdx = monitorIdx;
  29. auto monitorInfo = MonitorFinder::GetList()[monitorIdx];
  30. RECT rect = monitorInfo.rect;
  31. int width = rect.right - rect.left;
  32. int height = rect.bottom - rect.top;
  33. _capturer.open(target, method, width, height);
  34. return _Open(param);
  35. }
  36. bool VideoRecorder::_Open(Encoder<MediaType::VIDEO>::Param& param)
  37. {
  38. __CheckBool(_encodeFrame = Frame<MediaType::VIDEO>::Alloc(AV_PIX_FMT_NV12,
  39. _capturer.getFrame()
  40. ? _capturer.getFrame()->width
  41. : param.width,
  42. _capturer.getFrame()
  43. ? _capturer.getFrame()->height
  44. : param.height));
  45. {
  46. std::lock_guard<std::mutex> renderLk(_renderMtx);
  47. __CheckBool(_renderFrame = Frame<MediaType::VIDEO>::Alloc(AV_PIX_FMT_NV12,
  48. _capturer.getFrame()
  49. ? _capturer.getFrame()->width
  50. : param.width,
  51. _capturer.getFrame()
  52. ? _capturer.getFrame()->height
  53. : param.height));
  54. }
  55. // 捕获定时器和帧获取逻辑
  56. _captureTimer.Start(param.fps, [this] {
  57. auto srcFrame = _capturer.getFrame();
  58. if (srcFrame != nullptr) {
  59. std::lock_guard<std::mutex> muxLk(__mtx);
  60. if (srcFrame->format != _encodeFrame->format) {
  61. std::lock_guard<std::mutex> renderLk(_renderMtx);
  62. Free(_encodeFrame, [this] { av_frame_free(&_encodeFrame); });
  63. __CheckNo(
  64. _encodeFrame = Frame<MediaType::VIDEO>::Alloc(AVPixelFormat(srcFrame->format),
  65. srcFrame->width,
  66. srcFrame->height));
  67. }
  68. av_frame_copy(_encodeFrame, srcFrame);
  69. }
  70. });
  71. param.width = _capturer.getFrame() ? _capturer.getFrame()->width : param.width;
  72. param.height = _capturer.getFrame() ? _capturer.getFrame()->height : param.height;
  73. _param = param;
  74. return true;
  75. }
  76. AVFrame* VideoRecorder::GetRenderFrame()
  77. {
  78. std::lock_guard<std::mutex> renderLk(_renderMtx);
  79. if (_encodeFrame == nullptr) {
  80. return nullptr;
  81. }
  82. if (_renderFrame->format != _encodeFrame->format) {
  83. Free(_renderFrame, [this] { av_frame_free(&_renderFrame); });
  84. _renderFrame = Frame<MediaType::VIDEO>::Alloc(AVPixelFormat(_encodeFrame->format),
  85. _encodeFrame->width,
  86. _encodeFrame->height);
  87. }
  88. av_frame_copy(_renderFrame, _encodeFrame);
  89. return _renderFrame;
  90. }
  91. bool VideoRecorder::LoadMuxer(AvMuxer& muxer)
  92. {
  93. std::lock_guard<std::mutex> lock(_muxersMtx);
  94. // 检查是否已经加载过这个muxer
  95. for (const auto& info : _muxers) {
  96. if (info.muxer == &muxer) {
  97. qDebug() << "VideoRecorder::LoadMuxer: Muxer already loaded, streamIndex:" << info.streamIndex;
  98. return true; // 已经加载过,直接返回成功
  99. }
  100. }
  101. qDebug() << "VideoRecorder::LoadMuxer: Adding video stream with params - name:" << QString::fromStdString(_param.name)
  102. << "width:" << _param.width << "height:" << _param.height << "fps:" << _param.fps << "bitRate:" << _param.bitRate;
  103. int streamIndex = muxer.AddVideoStream(_param);
  104. if (streamIndex == -1) {
  105. qDebug() << "VideoRecorder::LoadMuxer: Failed to add video stream to muxer";
  106. return false;
  107. }
  108. qDebug() << "VideoRecorder::LoadMuxer: Successfully added video stream, streamIndex:" << streamIndex;
  109. _muxers.emplace_back(&muxer, streamIndex);
  110. return true;
  111. }
  112. bool VideoRecorder::UnloadMuxer(AvMuxer& muxer)
  113. {
  114. std::lock_guard<std::mutex> lock(_muxersMtx);
  115. auto it = std::find_if(_muxers.begin(), _muxers.end(),
  116. [&muxer](const MuxerInfo& info) {
  117. return info.muxer == &muxer;
  118. });
  119. if (it != _muxers.end()) {
  120. _muxers.erase(it);
  121. return true;
  122. }
  123. return false; // 没有找到对应的muxer
  124. }
  125. bool VideoRecorder::StartRecord()
  126. {
  127. std::lock_guard<std::mutex> lock(_muxersMtx);
  128. if (_muxers.empty()) {
  129. return false; // 没有加载任何muxer
  130. }
  131. _totalPts = 0;
  132. _lossPts = 0;
  133. {
  134. std::lock_guard<std::mutex> lock(_lossMtx);
  135. _lossHistory.clear();
  136. }
  137. _muxTimer.Start(_param.fps, [this] {
  138. ++_totalPts;
  139. bool anySuccess = false;
  140. // 向所有muxer写入数据
  141. {
  142. std::lock_guard<std::mutex> muxerLock(_muxersMtx);
  143. for (const auto& info : _muxers) {
  144. if (info.muxer->Write(_encodeFrame, info.streamIndex)) {
  145. anySuccess = true;
  146. }
  147. }
  148. }
  149. bool loss = !anySuccess;
  150. if (loss)
  151. ++_lossPts;
  152. {
  153. std::lock_guard<std::mutex> lock(_lossMtx);
  154. _lossHistory.push_back(loss);
  155. if (_lossHistory.size() > LOSS_WINDOW)
  156. _lossHistory.pop_front();
  157. }
  158. });
  159. _isRecord = true;
  160. return true;
  161. }
  162. void VideoRecorder::StopRecord()
  163. {
  164. _isRecord = false;
  165. _muxTimer.Stop();
  166. }
  167. double VideoRecorder::GetLossRate()
  168. {
  169. std::lock_guard<std::mutex> lock(_lossMtx);
  170. if (_lossHistory.size() < LOSS_WINDOW)
  171. return -1.0; // 统计中
  172. int lossCount = std::count(_lossHistory.begin(), _lossHistory.end(), true);
  173. if (_lossHistory.empty()) {
  174. return 0.0;
  175. }
  176. return double(lossCount) / _lossHistory.size();
  177. }
  178. void VideoRecorder::Close()
  179. {
  180. StopRecord();
  181. _captureTimer.Stop();
  182. _capturer.close();
  183. Free(_encodeFrame, [this] { av_frame_free(&_encodeFrame); });
  184. Free(_renderFrame, [this] { av_frame_free(&_renderFrame); });
  185. }
  186. void VideoRecorder::SetCaptureSource(HWND srcHwnd, CaptureMethod method)
  187. {
  188. // 只切换采集源,不重启编码器和推流
  189. CaptureTarget target;
  190. target.type = CaptureTargetType::Window;
  191. target.hwnd = srcHwnd;
  192. RECT clientRect;
  193. if (!GetClientRect(srcHwnd, &clientRect))
  194. return;
  195. int width = clientRect.right - clientRect.left;
  196. int height = clientRect.bottom - clientRect.top;
  197. _capturer.open(target, method, width, height);
  198. }
  199. void VideoRecorder::SetCaptureSource(int monitorIdx, CaptureMethod method)
  200. {
  201. // 只切换采集源,不重启编码器和推流
  202. CaptureTarget target;
  203. target.type = CaptureTargetType::Monitor;
  204. target.monitorIdx = monitorIdx;
  205. auto monitorInfo = MonitorFinder::GetList()[monitorIdx];
  206. RECT rect = monitorInfo.rect;
  207. int width = rect.right - rect.left;
  208. int height = rect.bottom - rect.top;
  209. _capturer.open(target, method, width, height);
  210. }