c++ ffmpeg如何获取RTP包中的Seq和timestamp
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++ ffmpeg如何获取RTP包中的Seq和timestamp相关的知识,希望对你有一定的参考价值。
在c++程序中,使用ffmpeg拉取rtsp视频流时,如何能获取到网络传输的原始RTP包,我需要取得包中的sequence number和timestamp这两个值。
参考技术A avformat_open_input这些代码打开文件然后av_read_frame不停的读文件内容
然后decode,然后encode。然后avio_open打开输出文件,然后不停的将encode结果av_interleaved_write_frame到文件里。追问
答非所问,回答前可否先看看提问内容呢?我问的是如何获取网络传输的RTP包中的sequence number和timestamp这两个值。你却回答视频文件转码另存其他文件
参考技术B 请问楼主现在解决了吗,是怎么解决的呢c++ h264RTP接收和发送程序
目的
制作最为简单的h264rtp接收,掌握h264解码和RTP接收
2 基本架构
2.1 libRtpReceive 静态库
静态库包含decode 解码程序,drawpkt 渲染画面,drawrgb24 画面, ffmpeg封装, h264frame 解封包,udp服务程序, 而LiveRtpDecode则是界面接收显示,负责渲染,liveRTPEncoder是发送界面。
2.2 libRtpDecode 可执行程序
拉一个界面负责显示用
开始和结束接收数据线程,udp端口6666
void CDrawHwndDlg::OnBnClickedButton1()
DP0("start to receive");
HWND hwnd = this->GetDlgItem(IDC_PIC0)->GetSafeHwnd();
v_draw_server.StartServer(hwnd,6666);
//server1.StartReceive(9200,hwnd);
void CDrawHwndDlg::OnBnClickedButton2()
v_draw_server.StopServer();
class c_drawpkt:public c_thread
public:
c_drawpkt(void);
~c_drawpkt(void);
void Run();
void StartServer(HWND hWnd,unsigned short port);
void StopServer();
private:
AVFrame * _pFrameRGB;
uint8_t * _RGBBuffer;
struct SwsContext *_img_convert_ctx;
HWND _hWnd0;
// HWND _hWnd1;
c_drawrgb24 _Draw;
H264DecoderContext _decoder;
c_udp v_udpserver;
//frame
int _fps;
;
以下为cpp文件
#include "common.h"
#include "c_drawpkt.h"
#include <thread>
c_drawpkt::c_drawpkt(void)
_RGBBuffer = NULL;
_pFrameRGB = NULL;
_hWnd0 = NULL;
_fps = 10;
c_drawpkt::~c_drawpkt(void)
void c_drawpkt::StartServer(HWND hWnd, unsigned short port)
_hWnd0 = hWnd;
if(!_decoder.Initialize())
return ;
v_udpserver.StartReceive(port,&_decoder);
Start();
void c_drawpkt::StopServer()
//1 stop the draw thread
Stop();
Join();
//2 stop udp server
v_udpserver.StopServer();
//3 delete all the memory
memory_cache * mc = v_udpserver.GetBufferToDecode();
while(mc!=NULL)
delete []mc->buf;
delete mc;
mc = v_udpserver.GetBufferToDecode();
void c_drawpkt::Run()
//第一次解码时间
//int64_t firstDecodeTime = 0;
//int TickCountStart = 0;
//画帧计数
unsigned int FrameCount = 1;
//int fpsTick = 1000/_fps;
//int b =10;
int first_keyframe = 0;
while(!IsStop())
//WaitForSignal();
memory_cache * mc = v_udpserver.GetBufferToDecode();
if(mc == NULL)
std::this_thread::sleep_for(std::chrono::milliseconds(20));
else
AVFrame * frame = _decoder.DecodeFrames_2((const u_char*)mc->buf, mc->size);
if (frame != NULL)
int dw = _decoder.GetContext()->width;
int dh = _decoder.GetContext()->height;//_pDecode->GetContext()->height;
if (dw > 0 && dh > 0)
if (_RGBBuffer == NULL)
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_BGR24, dw, dh, 1);
_RGBBuffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
if (_pFrameRGB == NULL)
_pFrameRGB = av_frame_alloc();
av_image_fill_arrays(_pFrameRGB->data, _pFrameRGB->linesize, _RGBBuffer
, AV_PIX_FMT_BGR24, dw, dh, 1);
_img_convert_ctx = sws_getContext(dw, dh,
_decoder.GetContext()->pix_fmt,//PIX_FMT_YUV420P,
dw,
dh,
AV_PIX_FMT_BGR24,
SWS_BICUBIC,
NULL,
NULL,
NULL);
sws_scale(_img_convert_ctx, frame->data, frame->linesize, 0, dh, _pFrameRGB->data, _pFrameRGB->linesize);
if (_hWnd0 != NULL)
_Draw.Draw2(_hWnd0, NULL, _RGBBuffer, dw, dh);
FrameCount++;
delete[]mc->buf;
delete mc;
2.3 liveRTPEncoder 测试发送程序
拉一个界面负责发送,找到视频设备和音频设备,分辨率默认,慢慢改进
程序会开源,关注后期会发送源码地址,此篇文章会修改
以上是关于c++ ffmpeg如何获取RTP包中的Seq和timestamp的主要内容,如果未能解决你的问题,请参考以下文章
使用命名管道使用 FFMPEG 记录 RTP VP8 数据包