video_recorder.cpp 8.2 KB

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