mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-05-06 23:33:47 +08:00
完善ertmp相关代码 (#4505)
增加多种Codec支持,并修复一些bug: - opus 非标准实现,不输出config frame,与旧的实现保持一致 - 添加RTMP_CODEC_MAP宏,精简代码
This commit is contained in:
@@ -35,6 +35,14 @@ void OpusRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
outputFrame(data, size, pkt->time_stamp, pkt->time_stamp);
|
||||
}
|
||||
} else {
|
||||
if (codec == RtmpAudioCodec::aac) {
|
||||
uint8_t pkt_type = *data;
|
||||
data++; size--;
|
||||
if (pkt_type == (uint8_t)RtmpAACPacketType::aac_config_header) {
|
||||
getTrack()->setExtraData((uint8_t *)data, size);
|
||||
return;
|
||||
}
|
||||
}
|
||||
outputFrame(data, size, pkt->time_stamp, pkt->time_stamp);
|
||||
}
|
||||
}
|
||||
@@ -53,11 +61,14 @@ bool OpusRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
if (_enhanced) {
|
||||
uint8_t flags = ((uint8_t)RtmpAudioCodec::ex_header << 4) | (uint8_t)RtmpPacketType::PacketTypeCodedFrames;
|
||||
packet->buffer.push_back(flags);
|
||||
uint32_t fourcc = static_cast<uint32_t>(RtmpAudioCodec::fourcc_opus);
|
||||
uint32_t fourcc = htonl(getCodecFourCC(getTrack()->getCodecId()));
|
||||
packet->buffer.append(reinterpret_cast<char *>(&fourcc), 4);
|
||||
} else {
|
||||
uint8_t flags = getAudioRtmpFlags(getTrack());
|
||||
packet->buffer.push_back(flags);
|
||||
if (getTrack()->getCodecId() == CodecAAC) {
|
||||
packet->buffer.push_back((uint8_t)RtmpAACPacketType::aac_raw);
|
||||
}
|
||||
}
|
||||
packet->buffer.append(frame->data(), frame->size());
|
||||
packet->body_size = packet->buffer.size();
|
||||
@@ -74,23 +85,29 @@ void OpusRtmpEncoder::makeConfigPacket() {
|
||||
auto extra_data = getTrack()->getExtraData();
|
||||
if (!extra_data || !extra_data->size())
|
||||
return;
|
||||
auto pkt = RtmpPacket::create();
|
||||
auto packet = RtmpPacket::create();
|
||||
if (_enhanced) {
|
||||
uint8_t flags = ((uint8_t)RtmpAudioCodec::ex_header << 4) | (uint8_t)RtmpPacketType::PacketTypeSequenceStart;
|
||||
pkt->buffer.push_back(flags);
|
||||
uint32_t fourcc = static_cast<uint32_t>(RtmpAudioCodec::fourcc_opus);
|
||||
pkt->buffer.append(reinterpret_cast<char *>(&fourcc), 4);
|
||||
packet->buffer.push_back(flags);
|
||||
uint32_t fourcc = htonl(getCodecFourCC(getTrack()->getCodecId()));
|
||||
packet->buffer.append(reinterpret_cast<char *>(&fourcc), 4);
|
||||
} else {
|
||||
uint8_t flags = getAudioRtmpFlags(getTrack());
|
||||
pkt->buffer.push_back(flags);
|
||||
packet->buffer.push_back(flags);
|
||||
if (getTrack()->getCodecId() == CodecAAC) {
|
||||
packet->buffer.push_back((uint8_t)RtmpAACPacketType::aac_config_header);
|
||||
}
|
||||
else{
|
||||
return ;
|
||||
}
|
||||
}
|
||||
pkt->buffer.append(extra_data->data(), extra_data->size());
|
||||
pkt->body_size = pkt->buffer.size();
|
||||
pkt->chunk_id = CHUNK_AUDIO;
|
||||
pkt->stream_index = STREAM_MEDIA;
|
||||
pkt->time_stamp = 0;
|
||||
pkt->type_id = MSG_AUDIO;
|
||||
RtmpCodec::inputRtmp(pkt);
|
||||
packet->buffer.append(extra_data->data(), extra_data->size());
|
||||
packet->body_size = packet->buffer.size();
|
||||
packet->chunk_id = CHUNK_AUDIO;
|
||||
packet->stream_index = STREAM_MEDIA;
|
||||
packet->time_stamp = 0;
|
||||
packet->type_id = MSG_AUDIO;
|
||||
RtmpCodec::inputRtmp(packet);
|
||||
}
|
||||
|
||||
} // namespace mediakit
|
||||
|
||||
@@ -87,12 +87,7 @@ bool VpxRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
auto header = (RtmpVideoHeaderEnhanced *)buff;
|
||||
header->enhanced = 1;
|
||||
header->frame_type = frame->keyFrame() ? (int)RtmpFrameType::key_frame : (int)RtmpFrameType::inter_frame;
|
||||
switch (frame->getCodecId()) {
|
||||
case CodecVP8: header->fourcc = htonl((uint32_t)RtmpVideoCodec::fourcc_vp8); break;
|
||||
case CodecVP9: header->fourcc = htonl((uint32_t)RtmpVideoCodec::fourcc_vp9); break;
|
||||
case CodecAV1: header->fourcc = htonl((uint32_t)RtmpVideoCodec::fourcc_av1); break;
|
||||
default: break;
|
||||
}
|
||||
header->fourcc = htonl(getCodecFourCC(frame->getCodecId()));
|
||||
buff += RtmpPacketInfo::kEnhancedRtmpHeaderSize;
|
||||
if (cts) {
|
||||
header->pkt_type = (uint8_t)RtmpPacketType::PacketTypeCodedFrames;
|
||||
@@ -103,13 +98,7 @@ bool VpxRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
}
|
||||
} else {
|
||||
// flags
|
||||
uint8_t flags = 0;
|
||||
switch (getTrack()->getCodecId()) {
|
||||
case CodecVP8: flags = (uint8_t)RtmpVideoCodec::vp8; break;
|
||||
case CodecVP9: flags = (uint8_t)RtmpVideoCodec::vp9; break;
|
||||
case CodecAV1: flags = (uint8_t)RtmpVideoCodec::av1; break;
|
||||
default: break;
|
||||
}
|
||||
uint8_t flags = getCodecFlags(frame->getCodecId());
|
||||
flags |= (uint8_t)(frame->keyFrame() ? RtmpFrameType::key_frame : RtmpFrameType::inter_frame) << 4;
|
||||
|
||||
buff[0] = flags;
|
||||
@@ -144,20 +133,9 @@ void VpxRtmpEncoder::makeConfigPacket() {
|
||||
header->enhanced = 1;
|
||||
header->pkt_type = (int)RtmpPacketType::PacketTypeSequenceStart;
|
||||
header->frame_type = (int)RtmpFrameType::key_frame;
|
||||
switch (getTrack()->getCodecId()) {
|
||||
case CodecVP8: header->fourcc = htonl((uint32_t)RtmpVideoCodec::fourcc_vp8); break;
|
||||
case CodecVP9: header->fourcc = htonl((uint32_t)RtmpVideoCodec::fourcc_vp9); break;
|
||||
case CodecAV1: header->fourcc = htonl((uint32_t)RtmpVideoCodec::fourcc_av1); break;
|
||||
default: break;
|
||||
}
|
||||
header->fourcc = htonl(getCodecFourCC(getTrack()->getCodecId()));
|
||||
} else {
|
||||
uint8_t flags = 0;
|
||||
switch (getTrack()->getCodecId()) {
|
||||
case CodecVP8: flags = (uint8_t)RtmpVideoCodec::vp8; break;
|
||||
case CodecVP9: flags = (uint8_t)RtmpVideoCodec::vp9; break;
|
||||
case CodecAV1: flags = (uint8_t)RtmpVideoCodec::av1; break;
|
||||
default: break;
|
||||
}
|
||||
uint8_t flags = getCodecFlags(getTrack()->getCodecId());
|
||||
flags |= ((uint8_t)RtmpFrameType::key_frame << 4);
|
||||
buff[0] = flags;
|
||||
buff[1] = (uint8_t)RtmpH264PacketType::h264_config_header;
|
||||
|
||||
Reference in New Issue
Block a user