video_recorder.cpp 7.4 KB

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