FFmpegPreRecordH264Provider

FFmpegPreRecordH264Provider is dependent on FFmpegH264Provider. It is designed for pre-recording. It supports setting the cache duration in milliseconds.

API Instructions

class FFmpegPreRecordH264Provider { public: FFmpegPreRecordH264Provider(std::shared_ptr<FFmpegVideoSource> yuvSource, int maxCacheTimeMs = 5000); ~FFmpegPreRecordH264Provider(); public: int prepare(VideoStreamParam_t param); void destroy(); AVCodecContext* getAVCodecContext(); int getPreRecordFrames(std::queue<AVPacket*>& frameQueue); }; }}}

Constructors

When creating FFmpegPreRecordH264Provider you can set the cache size or duration.

if maxCacheSize > 0, maxCacheTimeMs is valid, otherwise, use the default value of 5000.

/** * @param yuvSource provide yuv data. * * @param maxCacheTimeMs >0 is valid, otherwise, use the default value of 5000. * */ FFmpegPreRecordH264Provider(std::shared_ptr<FFmpegVideoSource> yuvSource, int maxCacheTimeMs = 5000);

prepare

Set encoding parameters, and prepare FFmpegH264Provider.

/** * Set encoding parameters and initialize all required resources. * * @param param set parameters of the encoder. * */ int prepare(VideoStreamParam_t param);

destroy

Destroy FFmpegH264Provider.

getAVCodecContext

Get AVCodecContext to get the info of the encoder, such as width, height, spspps, etc.

getPreRecordFrames

Get all cached h264 frames from the queue. The packet must be freed with av_packet_unref()+av_packet_free() when it is no longer needed.

Sample Code

This is a sample of how to pre-record MP4 dependent on FFmpegPreRecordH264Provider, FFmpegH264Provider, and FFmpegAVMuxer.

the flow of pre-record MP4:

create yuv source --> create pre-record h264 provider --> pre-record provider prepare --> recording 6s --> create h264 provider --> h264 provider prepare -> create MP4 muxer -> init muxer --> put all pre-record frames to muxer --> create the thread of get the frame from h264 provider to put to muxer --> recording 3s --> uninit muxer

Test Result

FFmpegPreRecordH264ProviderTestResult_1.png
FFmpegPreRecordH264ProviderTestResult_2.png

Â