FFmpegVideoProvider is used to store, obtain, and control stream data. It only supports storing data of the AVPacket type; The data in AVPacket can be YUV, H264, or MJPG. All of its APIs are thread-safe.
API Instructions
namespace com { namespace sunplus { namespace media { class FFmpegVideoProvider { public: FFmpegVideoProvider(AVStream* avStream, int maxQueueSize = 4); ~FFmpegVideoProvider(); public: void prepare(); void destroy(); AVStream* getAVStream(); void setPutFrameInterval(int interval); int putFrame(AVPacket* packet); int getFrame(AVPacket*& packet); }; }}}
Constructors
FFmpegVideoProvider(AVStream* avStream, int maxQueueSize = 4);
prepare
Initialize the internal queue. You can only putFrame/getFrame after preparing.
/** * Initialize the internal queue. * You can only putFrame/getFrame after preparing. */ void prepare();
destroy
Empty the cached data and release the related resources. After destroying, the putFrame/getFrame returns a failure.
/** * Empty the cached data and release the related resources. * After destroy, the putFrame/getFrame returns a failure. */ void destroy();
getAVStream
Get AVStream to get the stream info of the cached data. Please refer to the getAVStream of FFmpegV4L2VideoSource.
/** * Get the AVStream for AVFormatContext, It can be used to get stream information. * Such as time_base, start_time, codec, etc. */ AVStream* getAVStream();
setPutFrameInterval
Set the put frame interval, which can be used to implement frame rate control. It is invalid for H264 data.
/* set the put frame interval. It is invalid for H264 data. * This way, you can change the frame rate of the stream. * For example, if the frame rate of the Video Source is 30 * and you set the interval of the put frame to 3, * the frame rate of this provider will become 10. * */ void setPutFrameInterval(int interval);
putFrame
Create a new packet that references the same data as the src packet, then push the new packet into the queue. The src packet must be freed with av_packet_unref()+av_packet_free() when it is no longer needed.
/** * Cache frame into the queue. * the packet must be freed with av_packet_unref()+av_packet_free() when * it is no longer needed. * @return 0 if OK, < 0 on error. */ int putFrame(AVPacket* packet);
Sample
AVPacket* packet; auto ret = av_read_frame(fmtCtx, packet); if (ret < 0) { return ret; } ret = provider->putFrame(packet); /* it is no longer needed, free it */ /* as below, is only unreferenced the buffer referenced by the packet */ av_packet_unref(packet); /* as below, free the packet, the packet will be set to NULL.*/ av_packet_free(&packet);
getFrame
Get cached frame from the queue, which is a thread-safe API. After successfully obtaining the packet, it must be freed with av_packet_unref()+av_packet_free() when it is no longer needed.
/** * Get cached frame from the queue. * On success, the packet must be freed with av_packet_unref()+av_packet_free() when * it is no longer needed. * @return 0 if OK, < 0 on error. */ int getFrame(AVPacket*& packet);
Sample
AVPacket* packet; auto ret = provider->getFrame(packet); /* copy frame or write frame to file */ ...... /* it is no longer needed, free it */ /* as below, is only unreferenced the buffer referenced by the packet */ av_packet_unref(packet); /* as below, free the packet, the packet will be set to NULL.*/ av_packet_free(&packet);
Sample Code
Please refer to the sample code of FFmpegV4L2VideoSource.