video_recorder.cpp 8.3 KB

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