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
|
|
|
|
*
|
2021-01-17 18:31:50 +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
|
|
|
|
|
2019-06-28 17:37:11 +08:00
|
|
|
|
#include "H264Rtmp.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
H264Frame::Ptr H264RtmpDecoder::obtainFrame() {
|
|
|
|
|
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
|
|
|
|
auto frame = obtainObj();
|
2020-01-14 10:04:24 +08:00
|
|
|
|
frame->_buffer.clear();
|
|
|
|
|
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 01头的sps
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
static string getH264SPS(const RtmpPacket &thiz) {
|
|
|
|
|
string ret;
|
|
|
|
|
if (thiz.getMediaType() != FLV_CODEC_H264) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
if (!thiz.isCfgFrame()) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2020-08-30 10:48:34 +08:00
|
|
|
|
if (thiz.buffer.size() < 13) {
|
2020-04-04 22:54:49 +08:00
|
|
|
|
WarnL << "bad H264 cfg!";
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-04-04 22:54:49 +08:00
|
|
|
|
WarnL << "bad H264 cfg!";
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2020-08-30 10:48:34 +08:00
|
|
|
|
ret.assign(thiz.buffer.data() + 13, sps_size);
|
2020-04-04 22:54:49 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回不带0x00 00 00 01头的pps
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
static string getH264PPS(const RtmpPacket &thiz) {
|
|
|
|
|
string ret;
|
|
|
|
|
if (thiz.getMediaType() != FLV_CODEC_H264) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
if (!thiz.isCfgFrame()) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2020-08-30 10:48:34 +08:00
|
|
|
|
if (thiz.buffer.size() < 13) {
|
2020-04-04 22:54:49 +08:00
|
|
|
|
WarnL << "bad H264 cfg!";
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-04-04 22:54:49 +08:00
|
|
|
|
WarnL << "bad H264 cfg!";
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-04-04 22:54:49 +08:00
|
|
|
|
WarnL << "bad H264 cfg!";
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2020-08-30 10:48:34 +08:00
|
|
|
|
ret.assign(thiz.buffer.data() + 13 + sps_size + 1 + 2, pps_size);
|
2020-04-04 22:54:49 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-06 18:22:04 +08:00
|
|
|
|
void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
2018-10-24 12:01:40 +08:00
|
|
|
|
if (pkt->isCfgFrame()) {
|
|
|
|
|
//缓存sps pps,后续插入到I帧之前
|
2020-04-04 22:54:49 +08:00
|
|
|
|
_sps = getH264SPS(*pkt);
|
|
|
|
|
_pps = getH264PPS(*pkt);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
onGetH264(_sps.data(), _sps.size(), pkt->time_stamp , pkt->time_stamp);
|
|
|
|
|
onGetH264(_pps.data(), _pps.size(), pkt->time_stamp , pkt->time_stamp);
|
2020-09-06 18:22:04 +08:00
|
|
|
|
return;
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 10:48:34 +08:00
|
|
|
|
if (pkt->buffer.size() > 9) {
|
2021-01-17 18:31:50 +08:00
|
|
|
|
auto iTotalLen = pkt->buffer.size();
|
|
|
|
|
size_t iOffset = 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;
|
2018-11-17 17:47:43 +08:00
|
|
|
|
|
2018-10-24 12:01:40 +08:00
|
|
|
|
while(iOffset + 4 < iTotalLen){
|
|
|
|
|
uint32_t iFrameLen;
|
2020-08-30 10:48:34 +08:00
|
|
|
|
memcpy(&iFrameLen, pkt->buffer.data() + iOffset, 4);
|
2018-10-24 12:01:40 +08:00
|
|
|
|
iFrameLen = ntohl(iFrameLen);
|
|
|
|
|
iOffset += 4;
|
|
|
|
|
if(iFrameLen + iOffset > iTotalLen){
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-08-30 10:48:34 +08:00
|
|
|
|
onGetH264(pkt->buffer.data() + iOffset, iFrameLen, pkt->time_stamp , pts);
|
2018-10-24 12:01:40 +08:00
|
|
|
|
iOffset += iFrameLen;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
inline void H264RtmpDecoder::onGetH264(const char* pcData, size_t iLen, uint32_t dts,uint32_t pts) {
|
2020-04-04 22:54:49 +08:00
|
|
|
|
if(iLen == 0){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-25 12:18:17 +08:00
|
|
|
|
#if 1
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h264frame->_dts = dts;
|
|
|
|
|
_h264frame->_pts = pts;
|
|
|
|
|
_h264frame->_buffer.assign("\x0\x0\x0\x1", 4); //添加264头
|
|
|
|
|
_h264frame->_buffer.append(pcData, iLen);
|
2018-10-24 12:01:40 +08:00
|
|
|
|
|
|
|
|
|
//写入环形缓存
|
2018-10-24 15:43:52 +08:00
|
|
|
|
RtmpCodec::inputFrame(_h264frame);
|
|
|
|
|
_h264frame = obtainFrame();
|
2019-07-25 12:09:36 +08:00
|
|
|
|
#else
|
|
|
|
|
//防止内存拷贝,这样产生的264帧不会有0x00 00 01头
|
|
|
|
|
auto frame = std::make_shared<H264FrameNoCacheAble>((char *)pcData,iLen,dts,pts,0);
|
|
|
|
|
RtmpCodec::inputFrame(frame);
|
|
|
|
|
#endif
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2018-10-25 14:16:40 +08:00
|
|
|
|
H264RtmpEncoder::H264RtmpEncoder(const Track::Ptr &track) {
|
|
|
|
|
_track = dynamic_pointer_cast<H264Track>(track);
|
2020-01-15 11:46:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void H264RtmpEncoder::makeConfigPacket(){
|
|
|
|
|
if (_track && _track->ready()) {
|
|
|
|
|
//尝试从track中获取sps pps信息
|
|
|
|
|
_sps = _track->getSps();
|
|
|
|
|
_pps = _track->getPps();
|
|
|
|
|
}
|
2018-11-17 16:26:43 +08:00
|
|
|
|
|
2020-01-15 11:46:15 +08:00
|
|
|
|
if (!_sps.empty() && !_pps.empty()) {
|
|
|
|
|
//获取到sps/pps
|
|
|
|
|
makeVideoConfigPkt();
|
|
|
|
|
_gotSpsPps = true;
|
|
|
|
|
}
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
|
|
|
|
auto pcData = frame->data() + frame->prefixSize();
|
|
|
|
|
auto iLen = frame->size() - frame->prefixSize();
|
2018-10-30 15:56:00 +08:00
|
|
|
|
auto type = H264_TYPE(((uint8_t*)pcData)[0]);
|
2020-07-16 16:26:13 +08:00
|
|
|
|
if(type == H264Frame::NAL_SEI){
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-24 12:01:40 +08:00
|
|
|
|
|
2020-01-15 11:46:15 +08:00
|
|
|
|
if (!_gotSpsPps) {
|
2018-10-25 14:23:02 +08:00
|
|
|
|
//尝试从frame中获取sps pps
|
2020-01-15 11:46:15 +08:00
|
|
|
|
switch (type) {
|
|
|
|
|
case H264Frame::NAL_SPS: {
|
2018-10-25 14:23:02 +08:00
|
|
|
|
//sps
|
2020-01-15 11:46:15 +08:00
|
|
|
|
_sps = string(pcData, iLen);
|
|
|
|
|
makeConfigPacket();
|
2018-10-25 14:23:02 +08:00
|
|
|
|
break;
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
2020-01-15 11:46:15 +08:00
|
|
|
|
case H264Frame::NAL_PPS: {
|
|
|
|
|
//pps
|
|
|
|
|
_pps = string(pcData, iLen);
|
|
|
|
|
makeConfigPacket();
|
2018-10-25 14:23:02 +08:00
|
|
|
|
break;
|
2020-01-15 11:46:15 +08:00
|
|
|
|
}
|
2018-10-25 14:23:02 +08:00
|
|
|
|
default:
|
|
|
|
|
break;
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
2018-10-25 14:16:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 10:48:34 +08:00
|
|
|
|
if(_lastPacket && _lastPacket->time_stamp != frame->dts()) {
|
2020-09-06 18:22:04 +08:00
|
|
|
|
RtmpCodec::inputRtmp(_lastPacket);
|
2019-12-13 15:42:58 +08:00
|
|
|
|
_lastPacket = nullptr;
|
|
|
|
|
}
|
2018-11-16 15:46:49 +08:00
|
|
|
|
|
2019-12-13 15:42:58 +08:00
|
|
|
|
if(!_lastPacket) {
|
|
|
|
|
//I or P or B frame
|
2020-04-04 22:54:49 +08:00
|
|
|
|
int8_t flags = FLV_CODEC_H264;
|
2019-12-13 15:42:58 +08:00
|
|
|
|
bool is_config = false;
|
2020-01-13 11:51:29 +08:00
|
|
|
|
flags |= (((frame->configFrame() || frame->keyFrame()) ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4);
|
2019-12-13 15:42:58 +08:00
|
|
|
|
|
|
|
|
|
_lastPacket = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
2020-08-30 10:48:34 +08:00
|
|
|
|
_lastPacket->buffer.clear();
|
|
|
|
|
_lastPacket->buffer.push_back(flags);
|
|
|
|
|
_lastPacket->buffer.push_back(!is_config);
|
2020-11-07 17:44:30 +08:00
|
|
|
|
int32_t cts = frame->pts() - frame->dts();
|
|
|
|
|
if (cts < 0) {
|
|
|
|
|
cts = 0;
|
|
|
|
|
}
|
2019-12-13 15:42:58 +08:00
|
|
|
|
cts = htonl(cts);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
_lastPacket->buffer.append((char *)&cts + 1, 3);
|
2019-12-13 15:42:58 +08:00
|
|
|
|
|
2020-08-30 10:48:34 +08:00
|
|
|
|
_lastPacket->chunk_id = CHUNK_VIDEO;
|
|
|
|
|
_lastPacket->stream_index = STREAM_MEDIA;
|
|
|
|
|
_lastPacket->time_stamp = frame->dts();
|
|
|
|
|
_lastPacket->type_id = MSG_VIDEO;
|
2018-10-24 12:01:40 +08:00
|
|
|
|
|
|
|
|
|
}
|
2021-01-17 18:31:50 +08:00
|
|
|
|
uint32_t size = htonl((uint32_t)iLen);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
_lastPacket->buffer.append((char *) &size, 4);
|
|
|
|
|
_lastPacket->buffer.append(pcData, iLen);
|
|
|
|
|
_lastPacket->body_size = _lastPacket->buffer.size();
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void H264RtmpEncoder::makeVideoConfigPkt() {
|
2020-04-04 22:54:49 +08:00
|
|
|
|
int8_t flags = FLV_CODEC_H264;
|
2018-10-24 12:01:40 +08:00
|
|
|
|
flags |= (FLV_KEY_FRAME << 4);
|
|
|
|
|
bool is_config = true;
|
|
|
|
|
|
|
|
|
|
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
2020-08-30 10:48:34 +08:00
|
|
|
|
rtmpPkt->buffer.clear();
|
2018-10-24 17:50:09 +08:00
|
|
|
|
|
2020-04-04 22:54:49 +08:00
|
|
|
|
//header
|
2020-08-30 10:48:34 +08:00
|
|
|
|
rtmpPkt->buffer.push_back(flags);
|
|
|
|
|
rtmpPkt->buffer.push_back(!is_config);
|
2020-04-04 22:54:49 +08:00
|
|
|
|
//cts
|
2020-08-30 10:48:34 +08:00
|
|
|
|
rtmpPkt->buffer.append("\x0\x0\x0", 3);
|
2018-10-24 12:01:40 +08:00
|
|
|
|
|
2020-04-04 22:54:49 +08:00
|
|
|
|
//AVCDecoderConfigurationRecord start
|
2020-08-30 10:48:34 +08:00
|
|
|
|
rtmpPkt->buffer.push_back(1); // version
|
|
|
|
|
rtmpPkt->buffer.push_back(_sps[1]); // profile
|
|
|
|
|
rtmpPkt->buffer.push_back(_sps[2]); // compat
|
|
|
|
|
rtmpPkt->buffer.push_back(_sps[3]); // level
|
2021-01-17 18:31:50 +08:00
|
|
|
|
rtmpPkt->buffer.push_back((char)0xff); // 6 bits reserved + 2 bits nal size length - 1 (11)
|
|
|
|
|
rtmpPkt->buffer.push_back((char)0xe1); // 3 bits reserved + 5 bits number of sps (00001)
|
2020-04-04 22:54:49 +08:00
|
|
|
|
//sps
|
2021-01-17 18:31:50 +08:00
|
|
|
|
uint16_t size = (uint16_t)_sps.size();
|
2018-10-24 12:01:40 +08:00
|
|
|
|
size = htons(size);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
rtmpPkt->buffer.append((char *) &size, 2);
|
|
|
|
|
rtmpPkt->buffer.append(_sps);
|
2020-04-04 22:54:49 +08:00
|
|
|
|
//pps
|
2020-08-30 10:48:34 +08:00
|
|
|
|
rtmpPkt->buffer.push_back(1); // version
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size = (uint16_t)_pps.size();
|
2018-10-24 12:01:40 +08:00
|
|
|
|
size = htons(size);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
rtmpPkt->buffer.append((char *) &size, 2);
|
|
|
|
|
rtmpPkt->buffer.append(_pps);
|
2018-10-24 12:01:40 +08:00
|
|
|
|
|
2020-08-30 10:48:34 +08:00
|
|
|
|
rtmpPkt->body_size = rtmpPkt->buffer.size();
|
|
|
|
|
rtmpPkt->chunk_id = CHUNK_VIDEO;
|
|
|
|
|
rtmpPkt->stream_index = STREAM_MEDIA;
|
|
|
|
|
rtmpPkt->time_stamp = 0;
|
|
|
|
|
rtmpPkt->type_id = MSG_VIDEO;
|
2020-09-06 18:22:04 +08:00
|
|
|
|
RtmpCodec::inputRtmp(rtmpPkt);
|
2018-10-24 12:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
}//namespace mediakit
|