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

#ifndef __STREAM_LIVE_ENCODER_HEADER__
#define __STREAM_LIVE_ENCODER_HEADER__
#ifndef LRF_PROXY_ONLY
/*
Live view of the processed frames (kernelModuleEmbedVideo live_mode EVE_LIVE_ENCODE)
streamHandler hands every processed frame (scaled, flipped, with the time stamp / label / logo / event
marks) to push(); the encoder thread encodes the newest one to H264 (libx264 when available, zero
latency, no B-frames, constrained baseline: every browser decodes it) and feeds the path (WebRTC
readers, recorder). A slow encoder drops frames, it never blocks the source.
Nothing is encoded while the path has no reader and does not record. A new reader gets an IDR at once.
*/
#include <condition_variable>
#include <memory>
#include <mutex>
#include <thread>
#include "../../components/base/basicLIBAV.h"
#include "../../components/base/basicLogCollection.h"
#include "../../data/media/mtx/core/mediaPath.h"
#include "../../data/media/mtx/feeder/h264Feeder.h"
class streamLiveEncoder
{
public:
streamLiveEncoder(std::shared_ptr<mtx::MediaPath> path, int fps, int bitrateKbps);
~streamLiveEncoder();
// From the frame thread: copies the frame when the encoder wants one (frame rate, readers)
void push(const cv::Mat & bgr);
private:
void run();
bool openEncoder(int width, int height);
void closeEncoder();
bool encode(const cv::Mat & bgr, bool forceIdr);
basicLogCollection * m_pLog;
std::shared_ptr<mtx::MediaPath> m_pPath;
mtx::H264Feeder m_oFeeder;
int m_nFps;
int m_nBitrateKbps;
std::mutex m_mutex;
std::condition_variable m_cond;
cv::Mat m_mPending;
bool m_bHavePending;
bool m_bRun;
int64_t m_nNextFrameUs; // frame rate limit
std::thread m_thread;
// encoder (encoder thread only)
AVCodecContext * m_pCtx;
AVFrame * m_pFrame;
AVPacket * m_pPacket;
SwsContext * m_pSws;
int m_nWidth;
int m_nHeight;
int64_t m_nFrameIndex;
size_t m_nLastReaders;
bool m_bOpenFailed;
};
#endif // !LRF_PROXY_ONLY
#endif