video_recorder.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef __VIDEO_RECORDER_H__
  2. #define __VIDEO_RECORDER_H__
  3. #include "basic/timer.h"
  4. #include "avrecorder/capturer/video/VideoCaptureManager.h"
  5. using namespace avrecorder::video;
  6. #include "muxer/av_muxer.h"
  7. #include <deque>
  8. #include <string>
  9. // #include <condition_variable>
  10. // #include <queue>
  11. class VideoRecorder {
  12. public:
  13. bool Open(HWND srcHwnd, Encoder<MediaType::VIDEO>::Param& param, CaptureMethod method);
  14. bool Open(int monitorIdx, Encoder<MediaType::VIDEO>::Param& param, CaptureMethod method);
  15. bool LoadMuxer(AvMuxer& muxer);
  16. bool UnloadMuxer(AvMuxer& muxer);
  17. bool StartRecord();
  18. void StopRecord();
  19. AVFrame* GetRenderFrame();
  20. void Close();
  21. void SetIsDrawCursor(bool isDraw) { _capturer.setDrawCursor(isDraw); }
  22. bool IsCaptureOverload() const { return _captureTimer.IsOverload(); }
  23. double GetLossRate();
  24. void SetCaptureSource(HWND srcHwnd, CaptureMethod method);
  25. void SetCaptureSource(int monitorIdx, CaptureMethod method);
  26. // 新增:获取指定muxer对应的视频编码器名称
  27. std::string GetEncoderNameForMuxer(AvMuxer& muxer);
  28. private:
  29. struct MuxerInfo {
  30. AvMuxer* muxer;
  31. int streamIndex;
  32. MuxerInfo(AvMuxer* m, int idx) : muxer(m), streamIndex(idx) {}
  33. };
  34. static constexpr int kCanvasWidth = 1920;
  35. static constexpr int kCanvasHeight = 1080;
  36. bool _Open(Encoder<MediaType::VIDEO>::Param& param);
  37. VideoCaptureManager _capturer;
  38. std::vector<MuxerInfo> _muxers;
  39. std::mutex _muxersMtx;
  40. bool _isRecord = false;
  41. AVFrame* _encodeFrame = nullptr;
  42. AVFrame* _renderFrame = nullptr;
  43. Encoder<MediaType::VIDEO>::Param _param;
  44. Timer _captureTimer;
  45. Timer _muxTimer;
  46. std::mutex _renderMtx;
  47. std::mutex _lossMtx;
  48. uint64_t _totalPts = 0;
  49. uint64_t _lossPts = 0;
  50. // 滑动窗口丢帧统计
  51. static constexpr int LOSS_WINDOW = 100;
  52. std::deque<bool> _lossHistory;
  53. };
  54. #endif