当前位置: 首页 > news >正文

RTMP推流H264和AAC

使用 librtmp 库实现推流h264和aac文件,rtmp服务器使用SRS搭建,拉流端使用VLC。其中用到的h264和aac文件解析部分代码在我其它博客中有写:C/C++ AAC文件解析-CSDN博客、C/C++ H264文件解析-CSDN博客。

推流部分源码(C++)如下:

rtmp_ah_publish.h:

#ifndef _RTMP_AH_PUBLISH_H_
#define _RTMP_AH_PUBLISH_H_#include <string>
#include "librtmp/rtmp.h"class Rtmp_ah_publish
{
private:RTMP *rtmp;uint8_t *dataBuffer;uint32_t bufSize;public:Rtmp_ah_publish(size_t size = 1024 * 1024);~Rtmp_ah_publish();// 连接rtmp服务器int connect(const std::string url);// 断开连接void close();/*** @brief 发送AAC序列头** @param profile      AAC规格* @param freq_index   AAC采样频率索引* @param chan_config  AAC声道配置** @return 成功返回 0,失败返回 -1*/int send_aac_sequence_header(uint8_t profile, uint8_t freq_index, uint8_t chan_config);/*** @brief 发送AAC数据** @param data   一帧AAC数据,不含ADTS头* @param len    数据长度* @param dts_ms 时间戳** @return 成功返回 0,失败返回 -1*/int send_aacData(const uint8_t *data, uint32_t len, uint32_t dts_ms);/*** @brief 发送H264序列头** @param sps    sps数据,不含起始码* @param spslen sps数据长度* @param pps    pps数据,不含起始码* @param ppslen pps数据长度** @return 成功返回 0,失败返回 -1*/int send_avc_sequence_header(const uint8_t *sps, uint32_t spslen,const uint8_t *pps, uint32_t ppslen);/*** @brief 发送H264数据** @param data   一个NALU数据,不含起始码* @param len    数据长度* @param dts_ms 时间戳** @return 成功返回 0,失败返回 -1,丢弃该NALU返回 -2*/int send_h264Data(const uint8_t *data, uint32_t len, uint32_t dts_ms);
};#endif // _RTMP_AH_PUBLISH_H_

rtmp_ah_publish.cpp:

#include "rtmp_ah_publish.h"
#include <iostream>
#include <cstring>Rtmp_ah_publish::Rtmp_ah_publish(size_t size)
{dataBuffer = nullptr;bufSize = 0;try{dataBuffer = new uint8_t[size];}catch (const std::bad_alloc &e){std::cout << "Memory allocation failed: " << e.what() << std::endl;}bufSize = size;
}Rtmp_ah_publish::~Rtmp_ah_publish()
{delete[] dataBuffer;dataBuffer = nullptr;bufSize = 0;
}int Rtmp_ah_publish::connect(const std::string url)
{rtmp = nullptr;rtmp = RTMP_Alloc();if (!rtmp){std::cout << "RTMP_Alloc failed" << std::endl;return -1;}RTMP_Init(rtmp);rtmp->Link.timeout = 10; // 10秒超时// rtmp->Link.lFlags |= RTMP_LF_LIVE; // 实时流标识if (!RTMP_SetupURL(rtmp, (char *)url.c_str())){std::cout << "RTMP_SetupURL failed" << std::endl;goto err;}// 设置为推流模式RTMP_EnableWrite(rtmp);// 建立连接if (!RTMP_Connect(rtmp, NULL)){std::cout << "RTMP_Connect failed" << std::endl;goto err;}// 创建流if (!RTMP_ConnectStream(rtmp, 0)){std::cout << "RTMP_ConnectStream failed" << std::endl;goto err;}return 0;err:if (rtmp){RTMP_Close(rtmp);RTMP_Free(rtmp);}return -1;
}void Rtmp_ah_publish::close()
{RTMP_Close(rtmp);RTMP_Free(rtmp);rtmp = nullptr;
}int Rtmp_ah_publish::send_aac_sequence_header(uint8_t profile, uint8_t freq_index, uint8_t chan_config)
{int ret = -1;// 组装 AudioSpecificConfig(2 字节)// AudioSpecificConfig 格式:// - 5 位:音频对象类型(Audio Object Type)// - 4 位:采样频率索引// - 4 位:声道配置uint8_t asc[2] = {0};asc[0] = ((profile + 1) << 3) | (freq_index >> 1);asc[1] = ((freq_index & 0x1) << 7) | (chan_config << 3);// 创建 FLV Audio Tag Data// FLV Audio Tag Data 结构:// - 1 字节:[SoundFormat][SoundRate][SoundSize][SoundType]// - 1 字节:AACPacketType// - n 字节:AudioSpecificConfiguint8_t audio_tag[4] = {0};// 配置第一个字节:// - 4位 SoundFormat = 10(AAC)// - 2位 SoundRate  = 3(44kHz,对于AAC总是3)// - 1位 SoundSize = 1(16 位,对于AAC总是1)// - 1位 SoundType = 1(立体声,对于AAC总是1)audio_tag[0] = (10 << 4) | (3 << 2) | (1 << 1) | 1;// printf("audio_tag[0]: 0x%02X\n", audio_tag[0]);audio_tag[1] = 0; // AACPacketType = 0(序列头)audio_tag[2] = asc[0];audio_tag[3] = asc[1];RTMPPacket packet;RTMPPacket_Reset(&packet);RTMPPacket_Alloc(&packet, sizeof(audio_tag)); // 给 packet.m_body 分配空间if (!packet.m_body){std::cout << "RTMPPacket_Alloc failed" << std::endl;return -1;}memcpy(packet.m_body, audio_tag, sizeof(audio_tag));packet.m_nBodySize = sizeof(audio_tag);packet.m_packetType = RTMP_PACKET_TYPE_AUDIO;packet.m_nChannel = 0x04;     // 通道ID,音频或视频为0x04packet.m_nTimeStamp = 0;      // 序列头的时间戳为 0packet.m_hasAbsTimestamp = 0; // 0: 相对时间戳 1: 绝对时间戳packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;packet.m_nInfoField2 = rtmp->m_stream_id;// 检查连接的状态if (!RTMP_IsConnected(rtmp)){printf("rtmp disconnect!\n");goto end;}// 发送,0表示直接发送,1表示放进发送队列if (!RTMP_SendPacket(rtmp, &packet, 1)){std::cout << "RTMP_SendPacket failed" << std::endl;goto end;}ret = 0;end:RTMPPacket_Free(&packet);return ret;
}int Rtmp_ah_publish::send_aacData(const uint8_t *data, uint32_t len, uint32_t dts_ms)
{if (len + 2 > bufSize - RTMP_MAX_HEADER_SIZE){std::cout << "error: exceeds buffer size." << std::endl;return -1;}// 创建 FLV Audio Tag Datauint8_t audio_tag[2] = {0};audio_tag[0] = (10 << 4) | (3 << 2) | (1 << 1) | 1;audio_tag[1] = 1; // AACPacketType = 1(aac原始数据)RTMPPacket packet;RTMPPacket_Reset(&packet);// 根据 RTMPPacket_Alloc 函数内部实现,m_body 是指向RTMP头后的内存packet.m_body = (char *)dataBuffer + RTMP_MAX_HEADER_SIZE;packet.m_nBodySize = len + 2;packet.m_packetType = RTMP_PACKET_TYPE_AUDIO;packet.m_nChannel = 0x04;packet.m_nTimeStamp = dts_ms;packet.m_hasAbsTimestamp = 0;packet.m_headerType = RTMP_PACKET_SIZE_LARGE;packet.m_nInfoField2 = rtmp->m_stream_id;memcpy(packet.m_body, audio_tag, 2);memcpy(packet.m_body + 2, data, len);// 检查连接的状态if (!RTMP_IsConnected(rtmp)){printf("rtmp disconnect!\n");return -1;}// 发送,0表示直接发送,1表示放进发送队列if (!RTMP_SendPacket(rtmp, &packet, 1)){std::cout << "RTMP_SendPacket failed" << std::endl;return -1;}return 0;
}int Rtmp_ah_publish::send_avc_sequence_header(const uint8_t *sps, uint32_t spslen,const uint8_t *pps, uint32_t ppslen)
{int ret = -1;// 构建 AVCDecoderConfigurationRecord// 参考 ISO/IEC 14496-15uint32_t avc_config_size = 11 + spslen + ppslen;uint8_t avc_config[avc_config_size] = {0};uint8_t *p = avc_config;// configurationVersion (版本号,通常为1)*p++ = 0x01;// AVCProfileIndication*p++ = sps[1];// profile_compatibility*p++ = sps[2];// AVCLevelIndication*p++ = sps[3];// reserved (6 bits,保留字节,都为1) +// lengthSizeMinusOne (2 bits,表示在视频流中每个NAL单元长度字段的实际字节数减一,// 该字段值一般为3)*p++ = 0xFF;// reserved (3 bits,保留字节,都为1) +// numOfSequenceParameterSets (5 bits,SPS个数,一般为1)*p++ = 0xE1;// sequenceParameterSetLength (SPS长度)*p++ = (spslen >> 8) & 0xFF;*p++ = spslen & 0xFF;// sequenceParameterSetNALUnit (SPS数据)memcpy(p, sps, spslen);p += spslen;// numOfPictureParameterSets (PPS的个数)*p++ = 0x01;// pictureParameterSetLength (PPS长度)*p++ = (ppslen >> 8) & 0xFF;*p++ = ppslen & 0xFF;// pictureParameterSetNALUnit (PPS数据)memcpy(p, pps, ppslen);// 构建 AVCVIDEOPACKETuint32_t avcvideopacket_size = 4 + avc_config_size;uint8_t avcvideopacket[avcvideopacket_size] = {0};// AVCPacketType = 0 (序列头)avcvideopacket[0] = 0x00;// CompositionTime = 0 (PTS 和 DTS 之间的差值,PTS = DTS + CompositionTime)avcvideopacket[1] = 0x00;avcvideopacket[2] = 0x00;avcvideopacket[3] = 0x00;// AVCDecoderConfigurationRecordmemcpy(avcvideopacket + 4, avc_config, avc_config_size);// 创建 FLV Video Tag Data// FLV Video Tag Data 结构:// - 1 字节:[FrameType][CodecID]// - n 字节:AVCVIDEOPACKETuint32_t video_tag_size = 1 + avcvideopacket_size;uint8_t video_tag[video_tag_size] = {0};// FrameType = 1 (关键帧), CodecID = 7 (AVC), 各占 4 bitsvideo_tag[0] = 0x17;memcpy(video_tag + 1, avcvideopacket, avcvideopacket_size);// 创建 RTMPPacketRTMPPacket packet;RTMPPacket_Reset(&packet);RTMPPacket_Alloc(&packet, video_tag_size);if (!packet.m_body){std::cout << "RTMPPacket_Alloc failed" << std::endl;return -1;}memcpy(packet.m_body, video_tag, video_tag_size);packet.m_nBodySize = video_tag_size;packet.m_packetType = RTMP_PACKET_TYPE_VIDEO;packet.m_nChannel = 0x04; // 通道ID,音频或视频为0x04packet.m_nTimeStamp = 0;  // 序列头的时间戳为 0packet.m_hasAbsTimestamp = 0;packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;packet.m_nInfoField2 = rtmp->m_stream_id;// 检查连接的状态if (!RTMP_IsConnected(rtmp)){printf("rtmp disconnect!\n");goto end;}// 发送,0表示直接发送,1表示放进发送队列if (!RTMP_SendPacket(rtmp, &packet, 1)){std::cout << "RTMP_SendPacket failed" << std::endl;goto end;}ret = 0;end:RTMPPacket_Free(&packet);return ret;
}int Rtmp_ah_publish::send_h264Data(const uint8_t *data, uint32_t len, uint32_t dts_ms)
{if (len + 9 > bufSize - RTMP_MAX_HEADER_SIZE){std::cout << "error: exceeds buffer size." << std::endl;return -1;}// 创建 FLV Video Tag Datauint8_t video_tag[9] = {0};uint8_t nal_type = data[0] & 0x1f; // 帧类型// FrameType = 1 (关键帧) 或 2 (非关键帧), CodecID = 7 (AVC)switch (nal_type){case 0x01: // 非关键帧video_tag[0] = 0x27;break;case 0x05: // 关键帧video_tag[0] = 0x17;break;default:// std::cout << "warn: Discard nal, nal type: " << static_cast<uint16_t>(nal_type) << std::endl;return -2;}// AVCPacketType = 1 (AVC NALU)video_tag[1] = 0x01;// CompositionTime = 0video_tag[2] = 0x00;video_tag[3] = 0x00;video_tag[4] = 0x00;// Nalu lenvideo_tag[5] = (len >> 24) & 0xff;video_tag[6] = (len >> 16) & 0xff;video_tag[7] = (len >> 8) & 0xff;video_tag[8] = len & 0xff;// 创建 RTMPPacketRTMPPacket packet;RTMPPacket_Reset(&packet);packet.m_body = (char *)dataBuffer + RTMP_MAX_HEADER_SIZE;packet.m_nBodySize = 9 + len;packet.m_packetType = RTMP_PACKET_TYPE_VIDEO;packet.m_nChannel = 0x04; // 通道ID,音频或视频为0x04packet.m_nTimeStamp = dts_ms;packet.m_hasAbsTimestamp = 0;packet.m_headerType = RTMP_PACKET_SIZE_LARGE;packet.m_nInfoField2 = rtmp->m_stream_id;memcpy(packet.m_body, video_tag, 9);memcpy(packet.m_body + 9, data, len);// 检查连接的状态if (!RTMP_IsConnected(rtmp)){printf("rtmp disconnect!\n");return -1;}// 发送,0表示直接发送,1表示放进发送队列if (!RTMP_SendPacket(rtmp, &packet, 1)){std::cout << "RTMP_SendPacket failed" << std::endl;return -1;}return 0;
}

main.cpp:

/*
// 推流AAC#include <iostream>
#include <chrono>
#include <thread>
#include "aacParse.h"
#include "rtmp_ah_publish.h"using namespace std;
using namespace std::chrono;int main()
{int ret;AACParse aac;uint8_t profile, sampleRate_index, channel_number;uint8_t buf[1024 * 1024];uint32_t len = 0;Rtmp_ah_publish rtmp_publish;aac.open_file("/home/tl/work/app/res/output.aac");if (rtmp_publish.connect("rtmp://192.168.0.102/live/livestream") == -1){cout << "connect failed." << endl;aac.close_file();return 0;}aac.get_configInfo(profile, sampleRate_index, channel_number);rtmp_publish.send_aac_sequence_header(profile, sampleRate_index, channel_number);// 计算每帧的持续时间(毫秒)uint32_t frame_duration_ms = 1024.0 / aac.get_aacSampleRate(sampleRate_index) * 1000;cout << "frame_duration_ms: " << frame_duration_ms << endl;uint32_t dts = 0;// 记录开始时间auto start_time = high_resolution_clock::now();while ((ret = aac.get_adts(buf, sizeof(buf), len)) != -1){rtmp_publish.send_aacData(buf + aac.get_headLength(), len - aac.get_headLength(), dts);dts += frame_duration_ms;if (ret == 0)break;// 计算发送时间auto target_time = start_time + duration<double, milli>(dts);// 获取当前时间auto now = high_resolution_clock::now();if (target_time > now){// 计算需要等待的持续时间auto sleep_duration = target_time - now;// 转换为毫秒,并休眠this_thread::sleep_for(duration_cast<milliseconds>(sleep_duration));}}if (ret == -1){cout << "get_adts failed." << endl;}cout << "end" << endl;rtmp_publish.close();aac.close_file();return 0;
}
*//*
// 推流H264#include <iostream>
#include <chrono>
#include <thread>
#include <cstring>
#include "h264Parse.h"
#include "rtmp_ah_publish.h"#define FRAME_RATE 60 // 视频帧率using namespace std;
using namespace std::chrono;int main()
{int ret;H264Parse h264;uint8_t buf[1024 * 1024];uint32_t len = 0;Rtmp_ah_publish rtmp_publish;h264.open_file("/home/tl/work/app/res/output.h264");if (rtmp_publish.connect("rtmp://192.168.0.102/live/livestream") == -1){cout << "connect failed." << endl;h264.close_file();return 0;}uint8_t sps[64], pps[64];uint32_t spslen, ppslen;h264.get_sps_pps(sps, sizeof(sps), spslen, pps, sizeof(pps), ppslen);rtmp_publish.send_avc_sequence_header(sps + h264.get_startCode_len(sps), spslen - h264.get_startCode_len(sps),pps + h264.get_startCode_len(pps), ppslen - h264.get_startCode_len(pps));uint32_t frame_duration_ms = 1.0 / FRAME_RATE * 1000;cout << "frame_duration_ms: " << frame_duration_ms << endl;uint32_t dts = 0;int startCode_len = 0;// 记录开始时间auto start_time = high_resolution_clock::now();while ((ret = h264.read_nalu(buf, sizeof(buf), len, 1024 * 50)) != -1){startCode_len = h264.get_startCode_len(buf);if (rtmp_publish.send_h264Data(buf + startCode_len,len - startCode_len, dts) == 0){dts += frame_duration_ms;}if (ret == 0)break;// 计算发送时间auto target_time = start_time + duration<double, milli>(dts);// 获取当前时间auto now = high_resolution_clock::now();if (target_time > now){// 计算需要等待的持续时间auto sleep_duration = target_time - now;// 转换为毫秒,并休眠this_thread::sleep_for(duration_cast<milliseconds>(sleep_duration));}}if (ret == -1){cout << "read_nalu failed." << endl;}cout << "end" << endl;rtmp_publish.close();h264.close_file();return 0;
}
*/// 同时推流AAC和H264#include <iostream>
#include <chrono>
#include <thread>
#include <cstring>
#include <mutex>
#include "aacParse.h"
#include "h264Parse.h"
#include "rtmp_ah_publish.h"#define FRAME_RATE 60 // 视频帧率using namespace std;
using namespace std::chrono;std::mutex rtmp_mutex;void stream_aac(AACParse &aac, Rtmp_ah_publish &rtmp_publish, uint8_t sampleRate_index)
{int ret;uint8_t buf[1024 * 1024];uint32_t len = 0;uint32_t dts = 0;// 计算每帧的持续时间(毫秒)uint32_t frame_duration_ms = 1024.0 / aac.get_aacSampleRate(sampleRate_index) * 1000;cout << "AAC frame_duration_ms: " << frame_duration_ms << endl;while ((ret = aac.get_adts(buf, sizeof(buf), len)) != -1){static auto start_time = high_resolution_clock::now();// 上锁{std::lock_guard<std::mutex> lock(rtmp_mutex);rtmp_publish.send_aacData(buf + aac.get_headLength(), len - aac.get_headLength(), dts);}dts += frame_duration_ms;if (ret == 0)break;auto target_time = start_time + duration<double, milli>(dts);auto now = high_resolution_clock::now();if (target_time > now){auto sleep_duration = target_time - now;this_thread::sleep_for(duration_cast<milliseconds>(sleep_duration));}}if (ret == -1){cout << "AAC get_adts failed." << endl;}
}void stream_h264(H264Parse &h264, Rtmp_ah_publish &rtmp_publish)
{int ret;uint8_t buf[1024 * 1024];uint32_t len = 0;uint32_t dts = 0;uint32_t frame_duration_ms = 1.0 / FRAME_RATE * 1000;cout << "H264 frame_duration_ms: " << frame_duration_ms << endl;while ((ret = h264.read_nalu(buf, sizeof(buf), len, 1024 * 50)) != -1){int startCode_len = h264.get_startCode_len(buf);static auto start_time = high_resolution_clock::now();// 上锁{std::lock_guard<std::mutex> lock(rtmp_mutex);if (rtmp_publish.send_h264Data(buf + startCode_len, len - startCode_len, dts) == 0){dts += frame_duration_ms;}}if (ret == 0)break;auto target_time = start_time + duration<double, milli>(dts);auto now = high_resolution_clock::now();if (target_time > now){auto sleep_duration = target_time - now;this_thread::sleep_for(duration_cast<milliseconds>(sleep_duration));}}if (ret == -1){cout << "H264 read_nalu failed." << endl;}
}int main()
{AACParse aac;H264Parse h264;Rtmp_ah_publish rtmp_publish;// 打开AAC文件aac.open_file("/home/tl/work/app/res/output.aac");// 打开H264文件h264.open_file("/home/tl/work/app/res/output.h264");// 连接RTMP流if (rtmp_publish.connect("rtmp://192.168.0.102/live/livestream") == -1){cout << "connect failed." << endl;aac.close_file();return 0;}uint8_t profile, sampleRate_index, channel_number;aac.get_configInfo(profile, sampleRate_index, channel_number);rtmp_publish.send_aac_sequence_header(profile, sampleRate_index, channel_number);uint8_t sps[64], pps[64];uint32_t spslen, ppslen;h264.get_sps_pps(sps, sizeof(sps), spslen, pps, sizeof(pps), ppslen);rtmp_publish.send_avc_sequence_header(sps + h264.get_startCode_len(sps), spslen - h264.get_startCode_len(sps),pps + h264.get_startCode_len(pps), ppslen - h264.get_startCode_len(pps));// 创建两个线程来同时推流AAC和H264thread aac_thread(stream_aac, ref(aac), ref(rtmp_publish), ref(sampleRate_index));thread h264_thread(stream_h264, ref(h264), ref(rtmp_publish));// 等待两个线程完成aac_thread.join();h264_thread.join();cout << "end" << endl;// 清理rtmp_publish.close();aac.close_file();h264.close_file();return 0;
}

相关文章链接:Flv 格式分析_script tag data-CSDN博客,注意文章中有处错误:

AVCDecoderConfigurationRecord 结构图中有一处错误,pictureParameterSetLength (PPS的长度) 应该是 UI16 ,两个字节。


http://www.mrgr.cn/news/67331.html

相关文章:

  • 论文阅读:DynamicDet: A Unified Dynamic Architecture for Object Detection
  • Flutter鸿蒙next 中的 Drawer 导航栏
  • 数据库管理-第256期 Oracle DB 23.6新特性一览(20241031)
  • Halcon打开多个窗口,指定窗口显示指定内容
  • 速度了解云原生后端!!!
  • RabbitMQ应用问题
  • 计算机网络综合题
  • 【c++语言程序设计】字符串与浅层复制(深拷贝与浅拷贝)
  • jenkins流水线pipeline
  • 使用Rust实现http/https正向代理
  • UE5.4 PCG 创建圆形植被聚落
  • GORM优化器和索引提示
  • C语言 | Leetcode C语言题解之第542题01矩阵
  • 速盾:高防cdn遭受攻击会瘫痪吗?
  • Java Agent使用
  • 网站架构知识之Ansible(day020)
  • 映像?什么是映像
  • 使用 Javascript 停用外部集成的 Javascript 文件
  • C语言常用的宏定义
  • 【LeetCode】【算法】238. 除自身以外数组的乘积
  • Star Tower:开启数据存储新纪元
  • 运动控制 PID算法
  • 掌握 PyQt5:从零开始的桌面应用开发
  • Kubernetes 服务发现:Service、DNS 深度解析
  • 迷你版VFB,极简的Freebasic开发IDE-VB7-vb6编程开发
  • Java——异常