You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __STREAM_LIVE_ENCODER_HEADER__
  2. #define __STREAM_LIVE_ENCODER_HEADER__
  3. #ifndef LRF_PROXY_ONLY
  4. /*
  5. Live view of the processed frames (kernelModuleEmbedVideo live_mode EVE_LIVE_ENCODE)
  6. streamHandler hands every processed frame (scaled, flipped, with the time stamp / label / logo / event
  7. marks) to push(); the encoder thread encodes the newest one to H264 (libx264 when available, zero
  8. latency, no B-frames, constrained baseline: every browser decodes it) and feeds the path (WebRTC
  9. readers, recorder). A slow encoder drops frames, it never blocks the source.
  10. Nothing is encoded while the path has no reader and does not record. A new reader gets an IDR at once.
  11. */
  12. #include <condition_variable>
  13. #include <memory>
  14. #include <mutex>
  15. #include <thread>
  16. #include "../../components/base/basicLIBAV.h"
  17. #include "../../components/base/basicLogCollection.h"
  18. #include "../../data/media/mtx/core/mediaPath.h"
  19. #include "../../data/media/mtx/feeder/h264Feeder.h"
  20. class streamLiveEncoder
  21. {
  22. public:
  23. streamLiveEncoder(std::shared_ptr<mtx::MediaPath> path, int fps, int bitrateKbps);
  24. ~streamLiveEncoder();
  25. // From the frame thread: copies the frame when the encoder wants one (frame rate, readers)
  26. void push(const cv::Mat & bgr);
  27. private:
  28. void run();
  29. bool openEncoder(int width, int height);
  30. void closeEncoder();
  31. bool encode(const cv::Mat & bgr, bool forceIdr);
  32. basicLogCollection * m_pLog;
  33. std::shared_ptr<mtx::MediaPath> m_pPath;
  34. mtx::H264Feeder m_oFeeder;
  35. int m_nFps;
  36. int m_nBitrateKbps;
  37. std::mutex m_mutex;
  38. std::condition_variable m_cond;
  39. cv::Mat m_mPending;
  40. bool m_bHavePending;
  41. bool m_bRun;
  42. int64_t m_nNextFrameUs; // frame rate limit
  43. std::thread m_thread;
  44. // encoder (encoder thread only)
  45. AVCodecContext * m_pCtx;
  46. AVFrame * m_pFrame;
  47. AVPacket * m_pPacket;
  48. SwsContext * m_pSws;
  49. int m_nWidth;
  50. int m_nHeight;
  51. int64_t m_nFrameIndex;
  52. size_t m_nLastReaders;
  53. bool m_bOpenFailed;
  54. };
  55. #endif // !LRF_PROXY_ONLY
  56. #endif