c++ h264RTP接收和发送程序
Posted qianbo_insist
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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++ h264RTP接收和发送程序的主要内容,如果未能解决你的问题,请参考以下文章