因公司项目需求。简单学习了FFMPEG。在这里做个记录。
音频解码的流程如下:
先介绍一下AVFormatContext里面的几个成员:
/**
* Maximum size of the data read from input for determining
* the input container format.
* Demuxing only, set by the caller before avformat_open_input().
*/
int64_t probesize;
/**
* Maximum duration (in AV_TIME_BASE units) of the data read
* from input in avformat_find_stream_info().
* Demuxing only, set by the caller before avformat_find_stream_info().
* Can be set to 0 to let avformat choose using a heuristic.
*/
int64_t max_analyze_duration;
/**
* Custom interrupt callbacks for the I/O layer.
*
* demuxing: set by the user before avformat_open_input().
* muxing: set by the user before avformat_write_header()
* (mainly useful for AVFMT_NOFILE formats). The callback
* should also be passed to avio_open2() if it's used to
* open the file.
*/
AVIOInterruptCB interrupt_callback;
/**
* I/O context.
*
* - demuxing: either set by the user before avformat_open_input() (then
* the user must close it manually) or set by avformat_open_input().
* - muxing: set by the user before avformat_write_header(). The caller must
* take care of closing / freeing the IO context.
*
* Do NOT set this field if AVFMT_NOFILE flag is set in
* iformat/oformat.flags. In such a case, the (de)muxer will handle
* I/O in some other way and this field will be NULL.
*/
AVIOContext *pb;
输入为内存的时候,我们需要设置一个AVIOContext。
而直接用于拉流时,可以直接调用avformat_open_input。第二个参数传url。
ffmpeg也提供了停止拉流的方法。设置AVFormatContext的interrupt_callback。自己写一个回调。当它返回1时,拉流停止。
这里还有一个延迟的问题。ffmpeg解码时,会先猜测解码的数据格式。通过设置AVFormatContext的probeSize和max_analyze_duration。可以明显的降低延迟。
直接上代码:
#ifndef _IDECODE_H_
#define _IDECODE_H_
#include "util.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <