前言
rtmp即Real Time Messaging Protocol(实时消息传输协议)的首字母缩写,它是由Adobe公司提出的一种应用层的协议,用来解决多媒体数据传输流的多路复用(Multiplexing)和分包(packetizing)的问题。
目前现在流行的是直播技术, 大部分都是采用该协议;本文档重点用C++开发实现了RTMP直播推流sdk部分,并使之跨平台通用。
简单的结构如下:
主要接口设计
class SRLibRtmpInterface
{
public:
virtual int Connect(const char* url) = 0;
virtual int StartRtmpSend(int EnableVideo, int EnableAudio) = 0;
virtual int InitVideoParams(SR_VideoCodecSetting videoCodecSetting) = 0;
virtual int InitAudioParams(SR_AudioCodecSetting audioCodecSetting) = 0;
virtual int SetAudioDelay(int msecs) = 0;
virtual int StartRecordMp4File() = 0;
virtual int StopRecordMp4File() = 0;
virtual int SendYUVCapture(unsigned char * yuvframe, int yuvframeLen, unsigned long nTimeStamp) = 0;
virtual int SendPCMFrame(unsigned char * pcmframe, int frameLen, unsigned long nTimeStamp) = 0;
virtual int Close() = 0;
SRLibRtmpInterface(){};
virtual ~SRLibRtmpInterface() = 0;
}
- 用户直接设置需要连接的url 例如腾讯云的推流地址
- 用户直接输入yuv和pcm数据,推流sdk打包推送到各个平台
- 用户可以录制本地推送的流到mp4 文件
H264编码
int i_frame_size_byte = x264_encoder_encode(_enc, &nalu, &num_nals, &xpic, &oxpic);
if(i_frame_size_byte < 0)
{
xbase::XLog::Printf("EncodeX264", xbase::XLOG_ERR, "x264 encode failed");
return -1;
}
if(num_nals <= 0)
{
xbase::XLog::Printf("EncodeX264", xbase::XLOG_ERR, "frame delayed in encoder.");
return -2;
}
_frameNbr++;
for(int i = 0; i < num_nals; i++)
{
//unsigned char nalu_header[4] = { 0, 0, 0, 1 };
//fwrite(nalu_header, 1, 4, fp_h264);
//fwrite(nalu[i].p_payload, 1, nalu[i].i_payload, fp_h264);
x264_nal_t &nal = nalu[i];
if (nal.i_type == NAL_SPS)
{
uint8_t * h264Data = nal.p_payload;
if (video_broadcaster)
{
base::DataBuffer* RecvdataBuf = new base::DataBuffer(h264Data, nal.i_payload);
video_broadcaster->PreSendVideoDataPacket(RecvdataBuf, nTimeStamp, true);
}
}
if (nal.i_type == NAL_PPS)
{
uint8_t * h264Data = nal.p_payload;
if (video_broadcaster)
{
base::DataBuffer* RecvdataBuf = new base::DataBuffer(h264Data, nal.i_payload);
video_broadcaster->PreSendVideoDataPacket(RecvdataBuf, nTimeStamp, true);
}
}
if (nal.i_type == NAL_SLICE_IDR)
{
uint8_t * h264Data = nal.p_payload;
if (video_broadcaster)
{
base::DataBuffer* RecvdataBuf = new base::DataBuffer(h264Data, nal.i_payload);
video_broadcaster->PreSendVideoDataPacket(RecvdataBuf, nTimeStamp, true);
}
}
if (nal.i_type == NAL_SLICE)
{
uint8_t * h264Data = nal.p_payload;
if (video_broadcaster)
{
base::DataBuffer* RecvdataBuf = new base::DataBuffer(h264Data, nal.i_payload);
video_broadcaster->PreSendVideoDataPacket(RecvdataBuf, nTimeStamp, false);
}
}
}
分发器
本sdk加入了分发器的概念, 同一个流可以推动多个平台
4.pngrtmp打包
RTMP 协议栈的实现,这里使用的 srs-libRTMP,它提供的使用demo很多,代码设计也很精炼
联系
点击链接加入群【rtmp直播交流学习合作】:https://jq.qq.com/?_wv=1027&k=4Aqm21F
qq群号:559425694
email:zhanchonglongba@126.com/690759587
网友评论