ZLMediaKit/src/Extension/H264Rtmp.cpp

217 lines
6.9 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2018-10-25 10:00:17 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2018-10-25 10:00:17 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2018-10-25 10:00:17 +08:00
*/
2018-10-24 12:01:40 +08:00
#include "Rtmp/utils.h"
2019-06-28 17:37:11 +08:00
#include "H264Rtmp.h"
using namespace std;
using namespace toolkit;
namespace mediakit {
2018-10-24 12:01:40 +08:00
H264RtmpDecoder::H264RtmpDecoder() {
2018-10-24 15:43:52 +08:00
_h264frame = obtainFrame();
2018-10-24 12:01:40 +08:00
}
2021-02-05 11:51:16 +08:00
H264Frame::Ptr H264RtmpDecoder::obtainFrame() {
auto frame = FrameImp::create<H264Frame>();
frame->_prefix_size = 4;
2018-10-24 12:01:40 +08:00
return frame;
}
2020-04-04 22:54:49 +08:00
/**
* 0x00 00 00 01sps pps
2020-04-04 22:54:49 +08:00
*/
static bool getH264Config(const RtmpPacket &thiz, string &sps, string &pps) {
if ((RtmpVideoCodec)thiz.getRtmpCodecId() != RtmpVideoCodec::h264) {
return false;
2020-04-04 22:54:49 +08:00
}
2020-08-30 10:48:34 +08:00
if (thiz.buffer.size() < 13) {
return false;
2020-04-04 22:54:49 +08:00
}
uint16_t sps_size;
2020-08-30 10:48:34 +08:00
memcpy(&sps_size, thiz.buffer.data() + 11, 2);
2020-04-04 22:54:49 +08:00
sps_size = ntohs(sps_size);
2020-08-30 10:48:34 +08:00
if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2) {
return false;
2020-04-04 22:54:49 +08:00
}
uint16_t pps_size;
2020-08-30 10:48:34 +08:00
memcpy(&pps_size, thiz.buffer.data() + 13 + sps_size + 1, 2);
2020-04-04 22:54:49 +08:00
pps_size = ntohs(pps_size);
2020-08-30 10:48:34 +08:00
if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2 + pps_size) {
return false;
2020-04-04 22:54:49 +08:00
}
sps.assign(thiz.buffer.data() + 13, sps_size);
pps.assign(thiz.buffer.data() + 13 + sps_size + 1 + 2, pps_size);
return true;
2020-04-04 22:54:49 +08:00
}
void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
if (pkt->isConfigFrame()) {
2018-10-24 12:01:40 +08:00
//缓存sps pps后续插入到I帧之前
if (!getH264Config(*pkt, _sps, _pps)) {
WarnL << "get h264 sps/pps failed, rtmp packet is: " << hexdump(pkt->data(), pkt->size());
return;
}
onGetH264(_sps.data(), _sps.size(), pkt->time_stamp, pkt->time_stamp);
onGetH264(_pps.data(), _pps.size(), pkt->time_stamp, pkt->time_stamp);
return;
2018-10-24 12:01:40 +08:00
}
2020-08-30 10:48:34 +08:00
if (pkt->buffer.size() > 9) {
2021-06-28 10:35:08 +08:00
auto total_len = pkt->buffer.size();
size_t offset = 5;
2020-08-30 10:48:34 +08:00
uint8_t *cts_ptr = (uint8_t *) (pkt->buffer.data() + 2);
2018-11-17 17:47:43 +08:00
int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
2020-08-30 10:48:34 +08:00
auto pts = pkt->time_stamp + cts;
2021-06-28 10:35:08 +08:00
while (offset + 4 < total_len) {
uint32_t frame_len;
memcpy(&frame_len, pkt->buffer.data() + offset, 4);
frame_len = ntohl(frame_len);
offset += 4;
if (frame_len + offset > total_len) {
2018-10-24 12:01:40 +08:00
break;
}
2021-06-28 10:35:08 +08:00
onGetH264(pkt->buffer.data() + offset, frame_len, pkt->time_stamp, pts);
offset += frame_len;
2018-10-24 12:01:40 +08:00
}
}
}
inline void H264RtmpDecoder::onGetH264(const char* data, size_t len, uint32_t dts, uint32_t pts) {
if (!len) {
2020-04-04 22:54:49 +08:00
return;
}
_h264frame->_dts = dts;
_h264frame->_pts = pts;
2021-02-05 11:51:16 +08:00
_h264frame->_buffer.assign("\x00\x00\x00\x01", 4); //添加264头
_h264frame->_buffer.append(data, len);
2018-10-24 12:01:40 +08:00
//写入环形缓存
2018-10-24 15:43:52 +08:00
RtmpCodec::inputFrame(_h264frame);
_h264frame = obtainFrame();
2018-10-24 12:01:40 +08:00
}
////////////////////////////////////////////////////////////////////////
H264RtmpEncoder::H264RtmpEncoder(const Track::Ptr &track) {
_track = dynamic_pointer_cast<H264Track>(track);
}
void H264RtmpEncoder::makeConfigPacket(){
if (_track && _track->ready()) {
//尝试从track中获取sps pps信息
_sps = _track->getSps();
_pps = _track->getPps();
}
2018-11-17 16:26:43 +08:00
if (!_sps.empty() && !_pps.empty()) {
//获取到sps/pps
makeVideoConfigPkt();
2021-06-28 10:35:08 +08:00
_got_config_frame = true;
}
2018-10-24 12:01:40 +08:00
}
2022-10-16 19:49:56 +08:00
void H264RtmpEncoder::flush() {
inputFrame(nullptr);
}
bool H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
2022-10-16 19:49:56 +08:00
if (frame) {
auto data = frame->data() + frame->prefixSize();
auto len = frame->size() - frame->prefixSize();
auto type = H264_TYPE(data[0]);
switch (type) {
case H264Frame::NAL_SPS: {
if (!_got_config_frame) {
_sps = string(data, len);
makeConfigPacket();
}
break;
2018-10-24 12:01:40 +08:00
}
2022-10-16 19:49:56 +08:00
case H264Frame::NAL_PPS: {
if (!_got_config_frame) {
_pps = string(data, len);
makeConfigPacket();
}
break;
}
2022-10-16 19:49:56 +08:00
default: break;
2018-10-24 12:01:40 +08:00
}
}
2021-06-28 10:35:08 +08:00
if (!_rtmp_packet) {
_rtmp_packet = RtmpPacket::create();
//flags/not config/cts预占位
_rtmp_packet->buffer.resize(5);
}
2022-08-08 17:13:39 +08:00
return _merger.inputFrame(frame, [this](uint64_t dts, uint64_t pts, const Buffer::Ptr &, bool have_key_frame) {
// flags
_rtmp_packet->buffer[0] = (uint8_t)RtmpVideoCodec::h264 | ((uint8_t)(have_key_frame ? RtmpFrameType::key_frame : RtmpFrameType::inter_frame) << 4);
_rtmp_packet->buffer[1] = (uint8_t)RtmpH264PacketType::h264_nalu;
int32_t cts = pts - dts;
// cts
set_be24(&_rtmp_packet->buffer[2], cts);
_rtmp_packet->time_stamp = dts;
_rtmp_packet->body_size = _rtmp_packet->buffer.size();
_rtmp_packet->chunk_id = CHUNK_VIDEO;
_rtmp_packet->stream_index = STREAM_MEDIA;
_rtmp_packet->type_id = MSG_VIDEO;
// 输出rtmp packet
RtmpCodec::inputRtmp(_rtmp_packet);
_rtmp_packet = nullptr;
}, &_rtmp_packet->buffer);
2018-10-24 12:01:40 +08:00
}
void H264RtmpEncoder::makeVideoConfigPkt() {
2021-02-28 20:58:30 +08:00
if (_sps.size() < 4) {
WarnL << "sps长度不足4字节";
return;
}
auto flags = (uint8_t)RtmpVideoCodec::h264;
flags |= ((uint8_t)RtmpFrameType::key_frame << 4);
auto pkt = RtmpPacket::create();
// header
pkt->buffer.push_back(flags);
pkt->buffer.push_back((uint8_t)RtmpH264PacketType::h264_config_header);
// cts
pkt->buffer.append("\x0\x0\x0", 3);
// AVCDecoderConfigurationRecord start
pkt->buffer.push_back(1); // version
pkt->buffer.push_back(_sps[1]); // profile
pkt->buffer.push_back(_sps[2]); // compat
pkt->buffer.push_back(_sps[3]); // level
pkt->buffer.push_back((char)0xff); // 6 bits reserved + 2 bits nal size length - 1 (11)
pkt->buffer.push_back((char)0xe1); // 3 bits reserved + 5 bits number of sps (00001)
// sps
uint16_t size = (uint16_t)_sps.size();
2018-10-24 12:01:40 +08:00
size = htons(size);
pkt->buffer.append((char *)&size, 2);
pkt->buffer.append(_sps);
// pps
pkt->buffer.push_back(1); // version
size = (uint16_t)_pps.size();
2018-10-24 12:01:40 +08:00
size = htons(size);
pkt->buffer.append((char *)&size, 2);
pkt->buffer.append(_pps);
pkt->body_size = pkt->buffer.size();
pkt->chunk_id = CHUNK_VIDEO;
pkt->stream_index = STREAM_MEDIA;
pkt->time_stamp = 0;
pkt->type_id = MSG_VIDEO;
RtmpCodec::inputRtmp(pkt);
2018-10-24 12:01:40 +08:00
}
2018-10-24 17:17:55 +08:00
}//namespace mediakit