Fix Enhanced RTMP AV1 metadata and VPx packet framing (#4840)

Co-authored-by: xiongziliang <771730766@qq.com>
This commit is contained in:
pannal
2026-10-08 09:06:05 +02:00
committed by GitHub
parent de04a56c2b
commit 46220e6a86
12 changed files with 968 additions and 36 deletions

View File

@@ -40,16 +40,11 @@ void VpxRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
case RtmpPacketType::PacketTypeCodedFramesX:
case RtmpPacketType::PacketTypeCodedFrames: {
auto pts = pkt->time_stamp;
if (RtmpPacketType::PacketTypeCodedFrames == _info.video.pkt_type) {
CHECK_RET(size > 3);
// SI24 = [CompositionTime Offset]
int32_t cts = (load_be24(data) + 0xff800000) ^ 0xff800000;
pts += cts;
data += 3;
size -= 3;
}
outputFrame((char*)data, size, pkt->time_stamp, pts);
// Enhanced AV1/VP8/VP9 carry coded data immediately after FourCC.
// Only AVC/HEVC/VVC CodedFrames have an SI24 composition offset.
// Older ZLM CodedFrames with an SI24 prefix are intentionally
// unsupported: the wire format cannot identify that legacy layout.
outputFrame((char*)data, size, pkt->time_stamp, pkt->time_stamp);
break;
}
default:
@@ -81,22 +76,19 @@ VpxRtmpEncoder::VpxRtmpEncoder(const Track::Ptr &track) : RtmpCodec(track) {
bool VpxRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
auto packet = RtmpPacket::create();
packet->buffer.resize(8 + frame->size());
char *buff = packet->data();
int32_t cts = frame->pts() - frame->dts();
auto payload = frame->data() + frame->prefixSize();
auto payload_size = frame->size() - frame->prefixSize();
// 一次性按"头部 + 载荷"的实际长度分配,全程一次分配、一次 memcpy。
auto header_size = _enhanced ? (size_t)RtmpPacketInfo::kEnhancedRtmpHeaderSize : (size_t)5;
packet->buffer.resize(header_size + payload_size);
auto buff = packet->data();
if (_enhanced) {
auto header = (RtmpVideoHeaderEnhanced *)buff;
header->enhanced = 1;
header->frame_type = frame->keyFrame() ? (int)RtmpFrameType::key_frame : (int)RtmpFrameType::inter_frame;
header->fourcc = htonl(getCodecFourCC(frame->getCodecId()));
buff += RtmpPacketInfo::kEnhancedRtmpHeaderSize;
if (cts) {
header->pkt_type = (uint8_t)RtmpPacketType::PacketTypeCodedFrames;
set_be24(buff, cts);
buff += 3;
} else {
header->pkt_type = (uint8_t)RtmpPacketType::PacketTypeCodedFramesX;
}
// Enhanced AV1/VP8/VP9 have no composition offset, so always use CodedFrames.
header->pkt_type = (uint8_t)RtmpPacketType::PacketTypeCodedFrames;
} else {
// flags
uint8_t flags = getCodecFlags(frame->getCodecId());
@@ -104,15 +96,15 @@ bool VpxRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
buff[0] = flags;
buff[1] = (uint8_t)RtmpH264PacketType::h264_nalu;
// cts
// Preserve the low 24 bits of the signed composition offset, including
// negative offsets, without narrowing an unsigned difference to int32_t.
auto cts = static_cast<uint32_t>(frame->pts() - frame->dts());
set_be24(&buff[2], cts);
buff += 5;
}
memcpy(buff + header_size, payload, payload_size);
packet->time_stamp = frame->dts();
memcpy(buff, frame->data(), frame->size());
buff += frame->size();
packet->body_size = buff - packet->data();
packet->time_stamp = _enhanced ? frame->pts() : frame->dts();
packet->body_size = packet->buffer.size();
packet->chunk_id = CHUNK_VIDEO;
packet->stream_index = STREAM_MEDIA;
packet->type_id = MSG_VIDEO;