2018-10-25 10:00:17 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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
|
|
|
|
|
2021-08-19 19:52:55 +08:00
|
|
|
|
#include "Rtmp/utils.h"
|
2019-06-28 17:37:11 +08:00
|
|
|
|
#include "H264Rtmp.h"
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2024-01-15 20:32:06 +08:00
|
|
|
|
#define CHECK_RET(...) \
|
|
|
|
|
try { \
|
|
|
|
|
CHECK(__VA_ARGS__); \
|
|
|
|
|
} catch (AssertFailedException & ex) { \
|
|
|
|
|
WarnL << ex.what(); \
|
|
|
|
|
return; \
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
namespace mediakit {
|
2018-10-24 12:01:40 +08:00
|
|
|
|
|
2020-09-06 18:22:04 +08:00
|
|
|
|
void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
2023-07-22 19:40:28 +08:00
|
|
|
|
if (pkt->isConfigFrame()) {
|
2024-01-15 20:32:06 +08:00
|
|
|
|
CHECK_RET(pkt->size() > 5);
|
2023-12-09 16:23:51 +08:00
|
|
|
|
getTrack()->setExtraData((uint8_t *)pkt->data() + 5, pkt->size() - 5);
|
2020-09-06 18:22:04 +08:00
|
|
|
|
return;
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 20:32:06 +08:00
|
|
|
|
CHECK_RET(pkt->size() > 9);
|
2023-12-09 16:23:51 +08:00
|
|
|
|
uint8_t *cts_ptr = (uint8_t *)(pkt->buffer.data() + 2);
|
|
|
|
|
int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
|
|
|
|
|
auto pts = pkt->time_stamp + cts;
|
|
|
|
|
splitFrame((uint8_t *)pkt->data() + 5, pkt->size() - 5, pkt->time_stamp, pts);
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
void H264RtmpDecoder::splitFrame(const uint8_t *data, size_t size, uint32_t dts, uint32_t pts) {
|
|
|
|
|
auto end = data + size;
|
|
|
|
|
while (data + 4 < end) {
|
|
|
|
|
uint32_t frame_len = load_be32(data);
|
|
|
|
|
data += 4;
|
|
|
|
|
if (data + frame_len > end) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
outputFrame((const char *)data, frame_len, dts, pts);
|
|
|
|
|
data += frame_len;
|
2020-04-04 22:54:49 +08:00
|
|
|
|
}
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
void H264RtmpDecoder::outputFrame(const char *data, size_t len, uint32_t dts, uint32_t pts) {
|
|
|
|
|
auto frame = FrameImp::create<H264Frame>();
|
|
|
|
|
frame->_prefix_size = 4;
|
|
|
|
|
frame->_dts = dts;
|
|
|
|
|
frame->_pts = pts;
|
|
|
|
|
frame->_buffer.assign("\x00\x00\x00\x01", 4); // 添加264头
|
|
|
|
|
frame->_buffer.append(data, len);
|
|
|
|
|
RtmpCodec::inputFrame(frame);
|
2020-01-15 11:46:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2018-10-24 12:01:40 +08:00
|
|
|
|
|
2022-10-16 19:49:56 +08:00
|
|
|
|
void H264RtmpEncoder::flush() {
|
|
|
|
|
inputFrame(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
2021-06-28 10:35:08 +08:00
|
|
|
|
if (!_rtmp_packet) {
|
|
|
|
|
_rtmp_packet = RtmpPacket::create();
|
2021-08-19 19:52:55 +08:00
|
|
|
|
//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) {
|
2023-07-22 18:54:59 +08:00
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
void H264RtmpEncoder::makeConfigPacket() {
|
2023-07-22 18:54:59 +08:00
|
|
|
|
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
|
2023-12-09 16:23:51 +08:00
|
|
|
|
auto extra_data = getTrack()->getExtraData();
|
|
|
|
|
CHECK(extra_data);
|
|
|
|
|
pkt->buffer.append(extra_data->data(), extra_data->size());
|
2023-07-22 18:54:59 +08:00
|
|
|
|
|
|
|
|
|
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
|