mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-05 10:58:11 +08:00
新增支持enhanced-rtmp h265 推流 (#2694)
This commit is contained in:
@@ -258,6 +258,55 @@ void RtmpHandshake::random_generate(char *bytes, int size)
|
||||
}
|
||||
}
|
||||
|
||||
CodecId parseVideoRtmpPacket(const uint8_t *data, size_t size, RtmpPacketInfo *info) {
|
||||
RtmpPacketInfo save;
|
||||
info = info ? info : &save;
|
||||
info->codec = CodecInvalid;
|
||||
|
||||
CHECK(size > 0);
|
||||
if (data[0] >> 7 == 1) {
|
||||
// IsExHeader == 1
|
||||
CHECK(size >= 5, "Invalid rtmp buffer size: ", size);
|
||||
info->is_enhanced = true;
|
||||
info->video.frame_type = (RtmpFrameType)((data[0] >> 4) & 0x07);
|
||||
info->video.pkt_type = (RtmpPacketType)(data[0] & 0x0f);
|
||||
if (memcmp(data + 1, "av01", 4) == 0) {
|
||||
// AV1
|
||||
info->codec = CodecAV1;
|
||||
} else if (memcmp(data + 1, "vp09", 4) == 0) {
|
||||
// VP9
|
||||
info->codec = CodecVP9;
|
||||
} else if (memcmp(data + 1, "hvc1", 4) == 0) {
|
||||
// HEVC(H265)
|
||||
info->codec = CodecH265;
|
||||
} else {
|
||||
WarnL << "Rtmp video codec not supported: " << std::string((char *)data + 1, 4);
|
||||
}
|
||||
} else {
|
||||
// IsExHeader == 0
|
||||
info->is_enhanced = false;
|
||||
info->video.frame_type = (RtmpFrameType)(data[0] >> 4);
|
||||
info->video.rtmp_codec = (RtmpVideoCodec)(data[0] & 0x0f);
|
||||
|
||||
switch (info->video.rtmp_codec) {
|
||||
case RtmpVideoCodec::h264: {
|
||||
CHECK(size >= 1, "Invalid rtmp buffer size: ", size);
|
||||
info->codec = CodecH264;
|
||||
info->video.h264_pkt_type = (RtmpH264PacketType)data[1];
|
||||
break;
|
||||
}
|
||||
case RtmpVideoCodec::h265: {
|
||||
CHECK(size >= 1, "Invalid rtmp buffer size: ", size);
|
||||
info->codec = CodecH265;
|
||||
info->video.h264_pkt_type = (RtmpH264PacketType)data[1];
|
||||
break;
|
||||
}
|
||||
default: WarnL << "Rtmp video codec not supported: " << (int)info->video.rtmp_codec; break;
|
||||
}
|
||||
}
|
||||
return info->codec;
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
namespace toolkit {
|
||||
|
||||
@@ -269,5 +269,72 @@ private:
|
||||
//根据音频track获取flags
|
||||
uint8_t getAudioRtmpFlags(const Track::Ptr &track);
|
||||
|
||||
enum class RtmpFrameType : uint8_t {
|
||||
reserved = 0,
|
||||
key_frame = 1, // key frame (for AVC, a seekable frame)
|
||||
inter_frame = 2, // inter frame (for AVC, a non-seekable frame)
|
||||
disposable_inter_frame = 3, // disposable inter frame (H.263 only)
|
||||
generated_key_frame = 4, // generated key frame (reserved for server use only)
|
||||
video_info_frame = 5, // video info/command frame
|
||||
};
|
||||
|
||||
enum class RtmpVideoCodec : uint8_t {
|
||||
h263 = 2, // Sorenson H.263
|
||||
screen_video = 3, // Screen video
|
||||
vp6 = 4, // On2 VP6
|
||||
vp6_alpha = 5, // On2 VP6 with alpha channel
|
||||
screen_video2 = 6, // Screen video version 2
|
||||
h264 = 7, // avc
|
||||
h265 = 12, // 国内扩展
|
||||
};
|
||||
|
||||
enum class RtmpH264PacketType : uint8_t {
|
||||
h264_config_header = 0, // AVC sequence header(sps/pps)
|
||||
h264_nalu = 1, // AVC NALU
|
||||
h264_end_seq = 2, // AVC end of sequence (lower level NALU sequence ender is not REQUIRED or supported)
|
||||
};
|
||||
|
||||
enum class RtmpPacketType : uint8_t {
|
||||
PacketTypeSequenceStart = 0,
|
||||
PacketTypeCodedFrames = 1,
|
||||
PacketTypeSequenceEnd = 2,
|
||||
|
||||
// CompositionTime Offset is implied to equal zero. This is
|
||||
// an optimization to save putting SI24 composition time value of zero on
|
||||
// the wire. See pseudo code below in the VideoTagBody section
|
||||
PacketTypeCodedFramesX = 3,
|
||||
|
||||
// VideoTagBody does not contain video data. VideoTagBody
|
||||
// instead contains an AMF encoded metadata. See Metadata Frame
|
||||
// section for an illustration of its usage. As an example, the metadata
|
||||
// can be HDR information. This is a good way to signal HDR
|
||||
// information. This also opens up future ways to express additional
|
||||
// metadata that is meant for the next video sequence.
|
||||
//
|
||||
// note: presence of PacketTypeMetadata means that FrameType
|
||||
// flags at the top of this table should be ignored
|
||||
PacketTypeMetadata = 4,
|
||||
|
||||
// Carriage of bitstream in MPEG-2 TS format
|
||||
// note: PacketTypeSequenceStart and PacketTypeMPEG2TSSequenceStart
|
||||
// are mutually exclusive
|
||||
PacketTypeMPEG2TSSequenceStart = 5,
|
||||
};
|
||||
|
||||
struct RtmpPacketInfo {
|
||||
CodecId codec = CodecInvalid;
|
||||
bool is_enhanced;
|
||||
union {
|
||||
struct {
|
||||
RtmpFrameType frame_type;
|
||||
RtmpVideoCodec rtmp_codec;
|
||||
RtmpPacketType pkt_type;
|
||||
RtmpH264PacketType h264_pkt_type;
|
||||
} video;
|
||||
};
|
||||
};
|
||||
// https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp-v1.pdf
|
||||
CodecId parseVideoRtmpPacket(const uint8_t *data, size_t size, RtmpPacketInfo *info = nullptr);
|
||||
|
||||
}//namespace mediakit
|
||||
#endif//__rtmp_h
|
||||
|
||||
@@ -19,12 +19,12 @@ size_t RtmpDemuxer::trackCount(const AMFValue &metadata) {
|
||||
size_t ret = 0;
|
||||
metadata.object_for_each([&](const string &key, const AMFValue &val) {
|
||||
if (key == "videocodecid") {
|
||||
//找到视频
|
||||
// 找到视频
|
||||
++ret;
|
||||
return;
|
||||
}
|
||||
if (key == "audiocodecid") {
|
||||
//找到音频
|
||||
// 找到音频
|
||||
++ret;
|
||||
return;
|
||||
}
|
||||
@@ -32,7 +32,7 @@ size_t RtmpDemuxer::trackCount(const AMFValue &metadata) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool RtmpDemuxer::loadMetaData(const AMFValue &val){
|
||||
bool RtmpDemuxer::loadMetaData(const AMFValue &val) {
|
||||
bool ret = false;
|
||||
try {
|
||||
int audiosamplerate = 0;
|
||||
@@ -60,12 +60,12 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){
|
||||
return;
|
||||
}
|
||||
if (key == "videocodecid") {
|
||||
//找到视频
|
||||
// 找到视频
|
||||
videocodecid = &val;
|
||||
return;
|
||||
}
|
||||
if (key == "audiocodecid") {
|
||||
//找到音频
|
||||
// 找到音频
|
||||
audiocodecid = &val;
|
||||
return;
|
||||
}
|
||||
@@ -75,16 +75,22 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){
|
||||
}
|
||||
if (key == "videodatarate") {
|
||||
videodatarate = val.as_integer();
|
||||
_videodatarate = videodatarate * 1024;
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (videocodecid) {
|
||||
//有视频
|
||||
// 有视频
|
||||
ret = true;
|
||||
makeVideoTrack(*videocodecid, videodatarate * 1024);
|
||||
if (videocodecid->type() == AMF_NUMBER && videocodecid->as_integer() == (int)RtmpVideoCodec::h264) {
|
||||
// https://github.com/veovera/enhanced-rtmp/issues/8
|
||||
_complete_delay = true;
|
||||
} else {
|
||||
makeVideoTrack(*videocodecid, videodatarate * 1024);
|
||||
}
|
||||
}
|
||||
if (audiocodecid) {
|
||||
//有音频
|
||||
// 有音频
|
||||
ret = true;
|
||||
makeAudioTrack(*audiocodecid, audiosamplerate, audiochannels, audiosamplesize, audiodatarate * 1024);
|
||||
}
|
||||
@@ -92,8 +98,8 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){
|
||||
WarnL << ex.what();
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
//metadata中存在track相关的描述,那么我们根据metadata判断有多少个track
|
||||
if (ret && !_complete_delay) {
|
||||
// metadata中存在track相关的描述,那么我们根据metadata判断有多少个track
|
||||
addTrackCompleted();
|
||||
}
|
||||
return ret;
|
||||
@@ -108,8 +114,14 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
case MSG_VIDEO: {
|
||||
if (!_try_get_video_track) {
|
||||
_try_get_video_track = true;
|
||||
auto codec = AMFValue(pkt->getMediaType());
|
||||
makeVideoTrack(codec, 0);
|
||||
RtmpPacketInfo info;
|
||||
auto codec_id = parseVideoRtmpPacket((uint8_t *)pkt->data(), pkt->size(), &info);
|
||||
if (codec_id != CodecInvalid) {
|
||||
makeVideoTrack(Factory::getTrackByCodecId(codec_id), _videodatarate);
|
||||
if (_complete_delay) {
|
||||
addTrackCompleted();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_video_rtmp_decoder) {
|
||||
_video_rtmp_decoder->inputRtmp(pkt);
|
||||
@@ -128,51 +140,55 @@ void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
default : break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void RtmpDemuxer::makeVideoTrack(const AMFValue &videoCodec, int bit_rate) {
|
||||
makeVideoTrack(Factory::getVideoTrackByAmf(videoCodec), bit_rate);
|
||||
}
|
||||
|
||||
void RtmpDemuxer::makeVideoTrack(const Track::Ptr &track, int bit_rate) {
|
||||
if (_video_rtmp_decoder) {
|
||||
return;
|
||||
}
|
||||
//生成Track对象
|
||||
_video_track = dynamic_pointer_cast<VideoTrack>(Factory::getVideoTrackByAmf(videoCodec));
|
||||
// 生成Track对象
|
||||
_video_track = dynamic_pointer_cast<VideoTrack>(track);
|
||||
if (!_video_track) {
|
||||
return;
|
||||
}
|
||||
//生成rtmpCodec对象以便解码rtmp
|
||||
// 生成rtmpCodec对象以便解码rtmp
|
||||
_video_rtmp_decoder = Factory::getRtmpCodecByTrack(_video_track, false);
|
||||
if (!_video_rtmp_decoder) {
|
||||
//找不到相应的rtmp解码器,该track无效
|
||||
// 找不到相应的rtmp解码器,该track无效
|
||||
_video_track.reset();
|
||||
return;
|
||||
}
|
||||
_video_track->setBitRate(bit_rate);
|
||||
//设置rtmp解码器代理,生成的frame写入该Track
|
||||
// 设置rtmp解码器代理,生成的frame写入该Track
|
||||
_video_rtmp_decoder->addDelegate(_video_track);
|
||||
addTrack(_video_track);
|
||||
_try_get_video_track = true;
|
||||
}
|
||||
|
||||
void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec,int sample_rate, int channels, int sample_bit, int bit_rate) {
|
||||
void RtmpDemuxer::makeAudioTrack(const AMFValue &audioCodec, int sample_rate, int channels, int sample_bit, int bit_rate) {
|
||||
if (_audio_rtmp_decoder) {
|
||||
return;
|
||||
}
|
||||
//生成Track对象
|
||||
// 生成Track对象
|
||||
_audio_track = dynamic_pointer_cast<AudioTrack>(Factory::getAudioTrackByAmf(audioCodec, sample_rate, channels, sample_bit));
|
||||
if (!_audio_track) {
|
||||
return;
|
||||
}
|
||||
//生成rtmpCodec对象以便解码rtmp
|
||||
// 生成rtmpCodec对象以便解码rtmp
|
||||
_audio_rtmp_decoder = Factory::getRtmpCodecByTrack(_audio_track, false);
|
||||
if (!_audio_rtmp_decoder) {
|
||||
//找不到相应的rtmp解码器,该track无效
|
||||
// 找不到相应的rtmp解码器,该track无效
|
||||
_audio_track.reset();
|
||||
return;
|
||||
}
|
||||
_audio_track->setBitRate(bit_rate);
|
||||
//设置rtmp解码器代理,生成的frame写入该Track
|
||||
// 设置rtmp解码器代理,生成的frame写入该Track
|
||||
_audio_rtmp_decoder->addDelegate(_audio_track);
|
||||
addTrack(_audio_track);
|
||||
_try_get_audio_track = true;
|
||||
|
||||
@@ -45,12 +45,15 @@ public:
|
||||
|
||||
private:
|
||||
void makeVideoTrack(const AMFValue &val, int bit_rate);
|
||||
void makeVideoTrack(const Track::Ptr &val, int bit_rate);
|
||||
void makeAudioTrack(const AMFValue &val, int sample_rate, int channels, int sample_bit, int bit_rate);
|
||||
|
||||
private:
|
||||
bool _try_get_video_track = false;
|
||||
bool _try_get_audio_track = false;
|
||||
bool _complete_delay = false;
|
||||
float _duration = 0;
|
||||
int _videodatarate = 0;
|
||||
AudioTrack::Ptr _audio_track;
|
||||
VideoTrack::Ptr _video_track;
|
||||
RtmpCodec::Ptr _audio_rtmp_decoder;
|
||||
|
||||
@@ -191,6 +191,13 @@ void RtmpPlayer::send_connect() {
|
||||
obj.set("audioCodecs", (double) (0x0400));
|
||||
//只支持H264
|
||||
obj.set("videoCodecs", (double) (0x0080));
|
||||
|
||||
AMFValue fourCcList(AMF_STRICT_ARRAY);
|
||||
fourCcList.add("av01");
|
||||
fourCcList.add("vp09");
|
||||
fourCcList.add("hvc1");
|
||||
obj.set("fourCcList", fourCcList);
|
||||
|
||||
sendInvoke("connect", obj);
|
||||
addOnResultCB([this](AMFDecoder &dec) {
|
||||
//TraceL << "connect result";
|
||||
|
||||
@@ -135,6 +135,13 @@ void RtmpPusher::send_connect() {
|
||||
obj.set("type", "nonprivate");
|
||||
obj.set("tcUrl", _tc_url);
|
||||
obj.set("swfUrl", _tc_url);
|
||||
|
||||
AMFValue fourCcList(AMF_STRICT_ARRAY);
|
||||
fourCcList.add("av01");
|
||||
fourCcList.add("vp09");
|
||||
fourCcList.add("hvc1");
|
||||
obj.set("fourCcList", fourCcList);
|
||||
|
||||
sendInvoke("connect", obj);
|
||||
addOnResultCB([this](AMFDecoder &dec) {
|
||||
//TraceL << "connect result";
|
||||
|
||||
Reference in New Issue
Block a user