使用ffmpeg直接打开sdp文件播放里面流媒体.
0x0 使用Custom I/O
ffmpeg 有一个非常好用的模块: AVIO, 这个模块是用ffmpeg Custom I/O 的主要接口,来看API;
结构体
- AVIOInterruptCB : Callback for checking whether to abort blocking functions
- AVIODirEntry
- AVIODirContext
- AVIOContext : Bytestream IO Context,
常用函数
- avio_open, avio_open2
- avio_alloc_context,
- avio_read
- avio_write
这些函数的详细使用可以参考官方示例:http_multiclinet.c 和 官方文档
解析SDP
初始话必要的网络和解码模块
av_register_all()
avformat_network_init()
Alloc AVIO Context
unsigned char *iobuffer; // sdp
AVIOContext *avio = avio_alloc_context(iobuffer, sdp_size, 0, (void*)NULL, NULL, NULL,NULL);
// 把io context 设置到 avformat context 上去,
//这样ffmpeg就进入了Custom模式;
//详细查看源码里面类是 if (formatCtx->pb != NULL) { //custom io }
pFormatCtx->pb = avio
Demuxer, Decoder
pFormatCtx->iformat = av_find_input_format("sdp");
int err = avformat_open_input(&pFormatCtx, "nothing", NULL, NULL);
if(err) {
printf("avformat_open_input error: %d\n", err);
return -1;
}
// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx, NULL)<0) {
printf("avformat_find_stream_info error!!!\n");
return -1; // Couldn't find stream information
}
完整示例
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <stdio.h>