在网上找了一圈关于ffmpeg如何接收rtp流的Example,都是使用ffmpeg 命令行来实现的,还没看到用相关ffmpeg库和API来实现的,于是自己写了个小程序,来验证ffmpeg在本地监听rtp流,代码在ffmpeg4.1+win10环境下测试可行:
有几个关键点:
1)需要提供video/audio sdp信息,用于描述基本媒体信息
2)需要自动检测一个可用的端口
#include <iostream>
#include <sstream>
#include <fstream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/mathematics.h>
#include <libavutil/audio_fifo.h>
#include <libavutil/opt.h>
#include <libswresample/swresample.h>
}
#define WIN32
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <net/if.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/select.h>
#endif
#pragma warning(disable : 4996)
#pragma comment(lib, "WS2_32")
bool getAvailableListenPort(unsigned short *port)
{
std::string CLIENT_IP("127.0.0.1");
bool result = true;
#ifdef WIN32
WSADATA wsa;
/*初始化socket资源*/
if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0)
{
return false; //代表失败
}
#endif
// 1. 创建一个socket
//int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_UDP);
int sock = socket(AF_INET, SOCK_DGRAM, 0);
// 2. 创建一个sockaddr,并将它的端口号设为0
struct sockaddr_in addrto;
memset(&addrto, 0, sizeof(struct sockaddr_in));
addrto.sin_family = AF_INET;
addrto.sin_addr.s_addr = inet_addr(CLIENT_IP.c_str());
addrto.sin_port = 0;
// 3. 绑定
int ret = ::bind(sock, (struct sockaddr*) & (addrto), sizeof(struct sockaddr_in));
if (0 != ret)
{
return false;
}
// 4. 利用getsockname获取
struct sockaddr_in connAddr;
memset(&connAddr, 0, sizeof(struct sockaddr_in));
#ifdef WIN32
int len = sizeof(connAddr);
#else
unsigned int len = sizeof(connAddr);
#endif
ret = ::getsockname(sock, (sockaddr*)&connAddr, &len);
if (0 != ret)
{
return false;
}
*port = ntohs(connAddr.sin_port); // 获取端口号
#ifdef WIN32
if (0 != closesocket(sock))
#else
if (0 != close(sock))
#endif
{
result = false;
}
return result;
}
int receiveVideo()
{
printf("receiveVideo() start ");
int ret, i;
std::string ipaddr("127.0.0.1");
unsigned short port;
ret = getAvailableListenPort(&port);
std::string ifsdp("input.sdp");
std::ofstream sdp(ifsdp);
sdp << "v=0\r\n"
<< "o=- 0 0 IN IP4 " << ipaddr << "\r\n"
<< "s=No Name\r\n"
<< "c=IN IP4 " << ipaddr << "\r\n"
<< "t=0 0\r\n"
<< "m=" << "video" << " " << port << " RTP/AVP 96\r\n"
<< "a=rtpmap:96 H264/90000\r\n";
sdp.close();
av_register_all();
avcodec_register_all();
avformat_network_init();
AVFormatContext* fmtCtx(NULL);
fmtCtx = avformat_alloc_context();
fmtCtx->flags |= AVFMT_NOFILE;
fmtCtx->protocol_whitelist =(char*)"file,udp,rtp";
AVInputFormat* ifmt = av_find_input_format("sdp");
if (!ifmt) {
std::cerr << "can't find input format" << std::endl;
return 1;
}
printf("step1: open rtp input accroding to sdp, listen port:%u \n", port);
//if (avformat_open_input(&fmtCtx, "rtp://@127.0.0.1:9999", ifmt, NULL) != 0) {
if (avformat_open_input(&fmtCtx, ifsdp.c_str(), ifmt, NULL) != 0) {
printf("Failed to open input stream information\n");
return false;
}
printf("avformat_open_input done\n");
printf("step2: find stream info, block until key frame arrived \n");
if ((ret = avformat_find_stream_info(fmtCtx, 0)) < 0) {
printf("Failed to retrieve input stream information");
return false;
}
AVStream* st = fmtCtx->streams[0];
AVCodecID videoCodecId = st->codec->codec_id;
int width = st->codec->width;
int height = st->codec->height;
printf("step3: find stream info done: time base: %d/%d, type:%s, width:%d, height:%d \n",
st->time_base.num,
st->time_base.den,
avcodec_get_name(st->codec->codec_id),
width,
height);
if (videoCodecId != AV_CODEC_ID_H264) {
printf("video codec info not correct!");
return false;
}
printf("step4: start to receive video frame\n");
av_read_play(fmtCtx);
AVPacket m_avPacket;
int count = 0;
memset(&m_avPacket, 0, sizeof(m_avPacket));
while (count++ <50) {
av_init_packet(&m_avPacket);
ret = av_read_frame(fmtCtx, &m_avPacket);
if (ret < 0) {
printf("av_read_frame error\n");
continue;
}
printf("Receive video frame packet(%d th), dts %ld, size %d\n",count, m_avPacket.dts, m_avPacket.size);
av_packet_unref(&m_avPacket);
}
printf("program finished");
}
int receiveAudio()
{
printf("receiveAudio() start ");
int ret, i;
std::string ipaddr("127.0.0.1");
unsigned short port;
ret = getAvailableListenPort(&port);
std::string ifsdp("inputAudio.sdp");
std::ofstream sdp(ifsdp);
sdp << "v=0\r\n"
<< "o=- 0 0 IN IP4 " << ipaddr << "\r\n"
<< "s=No Name\r\n"
<< "c=IN IP4 " << ipaddr << "\r\n"
<< "t=0 0\r\n"
<< "m=" << "audio" << " " << port << " RTP/AVP 97\r\n"
<< "a=rtpmap:97 opus/48000/2\r\n";
sdp.close();
av_register_all();
avcodec_register_all();
avformat_network_init();
AVFormatContext* fmtCtx(NULL);
fmtCtx = avformat_alloc_context();
fmtCtx->flags |= AVFMT_NOFILE;
fmtCtx->protocol_whitelist = (char*)"file,udp,rtp";
AVInputFormat* ifmt = av_find_input_format("sdp");
if (!ifmt) {
std::cerr << "can't find input format" << std::endl;
return 1;
}
printf("step1: open rtp input accroding to sdp, listen port:%u \n", port);
//if (avformat_open_input(&fmtCtx, "rtp://@127.0.0.1:9999", ifmt, NULL) != 0) {
if (avformat_open_input(&fmtCtx, ifsdp.c_str(), ifmt, NULL) != 0) {
printf("Failed to open input stream information\n");
return false;
}
printf("avformat_open_input done\n");
printf("step2: find stream info, block until key frame arrived \n");
if ((ret = avformat_find_stream_info(fmtCtx, 0)) < 0) {
printf("Failed to retrieve input stream information");
return false;
}
AVStream* st = fmtCtx->streams[0];
AVCodecID audioCodecId = st->codec->codec_id;
int sampleRate = st->codec->sample_rate;
int channels = st->codec->channels;
printf("step3: find stream info done: time base: %d/%d, type:%s, channels:%d, sampleRate:%d \n",
st->time_base.num,
st->time_base.den,
avcodec_get_name(st->codec->codec_id),
channels,
sampleRate);
if (audioCodecId != AV_CODEC_ID_OPUS) {
printf("audio codec info not correct!");
return false;
}
printf("step4: start to receive audio frame\n");
av_read_play(fmtCtx);
AVPacket m_avPacket;
int count = 0;
memset(&m_avPacket, 0, sizeof(m_avPacket));
while (count++ < 50) {
av_init_packet(&m_avPacket);
ret = av_read_frame(fmtCtx, &m_avPacket);
if (ret < 0) {
printf("av_read_frame error\n");
continue;
}
printf("Receive audio frame packet(%d th), dts %ld, size %d\n", count, m_avPacket.dts, m_avPacket.size);
av_packet_unref(&m_avPacket);
}
avformat_close_input(&fmtCtx);
avformat_free_context(fmtCtx);
printf("program finished");
}
int main() {
//receiveVideo();
receiveAudio();
}