mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-02 08:09:02 +08:00
整理优化代码
This commit is contained in:
162
src/Extension/AACRtmpCodec.cpp
Normal file
162
src/Extension/AACRtmpCodec.cpp
Normal file
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "AACRtmpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
AACRtmpDecoder::AACRtmpDecoder() {
|
||||
_adts = obtainFrame();
|
||||
}
|
||||
|
||||
AACFrame::Ptr AACRtmpDecoder::obtainFrame() {
|
||||
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
||||
auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
|
||||
frame->aac_frame_length = 7;
|
||||
frame->iPrefixSize = 7;
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool key_pos) {
|
||||
RtmpCodec::inputRtmp(pkt, false);
|
||||
|
||||
if (pkt->isCfgFrame()) {
|
||||
_aac_cfg = pkt->getAacCfg();
|
||||
return false;
|
||||
}
|
||||
if (!_aac_cfg.empty()) {
|
||||
onGetAAC(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void AACRtmpDecoder::onGetAAC(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
|
||||
if(iLen + 7 > sizeof(_adts->buffer)){
|
||||
WarnL << "Illegal adts data, exceeding the length limit.";
|
||||
return;
|
||||
}
|
||||
//写adts结构头
|
||||
makeAdtsHeader(_aac_cfg,*_adts);
|
||||
|
||||
//拷贝aac负载
|
||||
memcpy(_adts->buffer + 7, pcData, iLen);
|
||||
_adts->aac_frame_length = 7 + iLen;
|
||||
_adts->timeStamp = ui32TimeStamp;
|
||||
|
||||
//adts结构头转成头7个字节
|
||||
writeAdtsHeader(*_adts, _adts->buffer);
|
||||
|
||||
//写入环形缓存
|
||||
RtmpCodec::inputFrame(_adts);
|
||||
_adts = obtainFrame();
|
||||
}
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
AACRtmpEncoder::AACRtmpEncoder(const Track::Ptr &track) {
|
||||
_track = dynamic_pointer_cast<AACTrack>(track);
|
||||
}
|
||||
|
||||
void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
RtmpCodec::inputFrame(frame);
|
||||
|
||||
if(_aac_cfg.empty()){
|
||||
if(frame->prefixSize() >= 7){
|
||||
//包含adts头,从adts头获取aac配置信息
|
||||
_aac_cfg = makeAdtsConfig(reinterpret_cast<const uint8_t *>(frame->data()));
|
||||
makeAudioConfigPkt();
|
||||
} else if(_track && _track->ready()){
|
||||
//从track中和获取aac配置信息
|
||||
_aac_cfg = _track->getAacCfg();
|
||||
makeAudioConfigPkt();
|
||||
}
|
||||
}
|
||||
|
||||
if(!_aac_cfg.empty()){
|
||||
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
||||
rtmpPkt->strBuf.clear();
|
||||
|
||||
//////////header
|
||||
uint8_t is_config = false;
|
||||
rtmpPkt->strBuf.push_back(_ui8AudioFlags);
|
||||
rtmpPkt->strBuf.push_back(!is_config);
|
||||
rtmpPkt->strBuf.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
|
||||
|
||||
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
|
||||
rtmpPkt->chunkId = CHUNK_AUDIO;
|
||||
rtmpPkt->streamId = STREAM_MEDIA;
|
||||
rtmpPkt->timeStamp = frame->stamp();
|
||||
rtmpPkt->typeId = MSG_AUDIO;
|
||||
RtmpCodec::inputRtmp(rtmpPkt, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void AACRtmpEncoder::makeAudioConfigPkt() {
|
||||
makeAdtsHeader(_aac_cfg,*_adts);
|
||||
int iSampleRate , iChannel , iSampleBit = 16;
|
||||
getAACInfo(*_adts,iSampleRate,iChannel);
|
||||
|
||||
uint8_t flvStereoOrMono = (iChannel > 1);
|
||||
uint8_t flvSampleRate;
|
||||
switch (iSampleRate) {
|
||||
case 48000:
|
||||
case 44100:
|
||||
flvSampleRate = 3;
|
||||
break;
|
||||
case 24000:
|
||||
case 22050:
|
||||
flvSampleRate = 2;
|
||||
break;
|
||||
case 12000:
|
||||
case 11025:
|
||||
flvSampleRate = 1;
|
||||
break;
|
||||
default:
|
||||
flvSampleRate = 0;
|
||||
break;
|
||||
}
|
||||
uint8_t flvSampleBit = iSampleBit == 16;
|
||||
uint8_t flvAudioType = 10; //aac
|
||||
|
||||
_ui8AudioFlags = (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono;
|
||||
|
||||
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
||||
rtmpPkt->strBuf.clear();
|
||||
|
||||
//////////header
|
||||
uint8_t is_config = true;
|
||||
rtmpPkt->strBuf.push_back(_ui8AudioFlags);
|
||||
rtmpPkt->strBuf.push_back(!is_config);
|
||||
rtmpPkt->strBuf.append(_aac_cfg);
|
||||
|
||||
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
|
||||
rtmpPkt->chunkId = CHUNK_AUDIO;
|
||||
rtmpPkt->streamId = STREAM_MEDIA;
|
||||
rtmpPkt->timeStamp = 0;
|
||||
rtmpPkt->typeId = MSG_AUDIO;
|
||||
RtmpCodec::inputRtmp(rtmpPkt, false);
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
100
src/Extension/AACRtmpCodec.h
Normal file
100
src/Extension/AACRtmpCodec.h
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ZLMEDIAKIT_AACRTMPCODEC_H
|
||||
#define ZLMEDIAKIT_AACRTMPCODEC_H
|
||||
|
||||
#include "Rtmp/RtmpCodec.h"
|
||||
#include "Extension/Track.h"
|
||||
#include "Extension/AAC.h"
|
||||
|
||||
namespace mediakit{
|
||||
/**
|
||||
* aac Rtmp转adts类
|
||||
*/
|
||||
class AACRtmpDecoder : public RtmpCodec , public ResourcePoolHelper<AACFrame> {
|
||||
public:
|
||||
typedef std::shared_ptr<AACRtmpDecoder> Ptr;
|
||||
|
||||
AACRtmpDecoder();
|
||||
~AACRtmpDecoder() {}
|
||||
|
||||
/**
|
||||
* 输入Rtmp并解码
|
||||
* @param Rtmp Rtmp数据包
|
||||
* @param key_pos 此参数内部强制转换为false,请忽略之
|
||||
*/
|
||||
bool inputRtmp(const RtmpPacket::Ptr &Rtmp, bool key_pos = false) override;
|
||||
|
||||
TrackType getTrackType() const override{
|
||||
return TrackAudio;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const override{
|
||||
return CodecAAC;
|
||||
}
|
||||
|
||||
protected:
|
||||
void onGetAAC(const char* pcData, int iLen, uint32_t ui32TimeStamp);
|
||||
AACFrame::Ptr obtainFrame();
|
||||
protected:
|
||||
AACFrame::Ptr _adts;
|
||||
string _aac_cfg;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* aac adts转Rtmp类
|
||||
*/
|
||||
class AACRtmpEncoder : public AACRtmpDecoder , public ResourcePoolHelper<RtmpPacket> {
|
||||
public:
|
||||
typedef std::shared_ptr<AACRtmpEncoder> Ptr;
|
||||
|
||||
/**
|
||||
* 构造函数,track可以为空,此时则在inputFrame时输入adts头
|
||||
* 如果track不为空且包含adts头相关信息,
|
||||
* 那么inputFrame时可以不输入adts头
|
||||
* @param track
|
||||
*/
|
||||
AACRtmpEncoder(const Track::Ptr &track);
|
||||
~AACRtmpEncoder() {}
|
||||
|
||||
/**
|
||||
* 输入aac 数据,可以不带adts头
|
||||
* @param frame aac数据
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
|
||||
private:
|
||||
void makeAudioConfigPkt();
|
||||
private:
|
||||
uint8_t _ui8AudioFlags;
|
||||
AACTrack::Ptr _track;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_AACRTMPCODEC_H
|
||||
179
src/Extension/AACRtpCodec.cpp
Normal file
179
src/Extension/AACRtpCodec.cpp
Normal file
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "AACRtpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
AACRtpEncoder::AACRtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize,
|
||||
uint32_t ui32SampleRate,
|
||||
uint8_t ui8PlayloadType,
|
||||
uint8_t ui8Interleaved) :
|
||||
RtpInfo(ui32Ssrc,
|
||||
ui32MtuSize,
|
||||
ui32SampleRate,
|
||||
ui8PlayloadType,
|
||||
ui8Interleaved){
|
||||
}
|
||||
|
||||
void AACRtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
RtpCodec::inputFrame(frame);
|
||||
|
||||
GET_CONFIG(uint32_t, cycleMS, Rtp::kCycleMS);
|
||||
auto uiStamp = frame->stamp();
|
||||
auto pcData = frame->data() + frame->prefixSize();
|
||||
auto iLen = frame->size() - frame->prefixSize();
|
||||
|
||||
uiStamp %= cycleMS;
|
||||
char *ptr = (char *) pcData;
|
||||
int iSize = iLen;
|
||||
while (iSize > 0) {
|
||||
if (iSize <= _ui32MtuSize - 20) {
|
||||
_aucSectionBuf[0] = 0;
|
||||
_aucSectionBuf[1] = 16;
|
||||
_aucSectionBuf[2] = iLen >> 5;
|
||||
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
||||
memcpy(_aucSectionBuf + 4, ptr, iSize);
|
||||
makeAACRtp(_aucSectionBuf, iSize + 4, true, uiStamp);
|
||||
break;
|
||||
}
|
||||
_aucSectionBuf[0] = 0;
|
||||
_aucSectionBuf[1] = 16;
|
||||
_aucSectionBuf[2] = (iLen) >> 5;
|
||||
_aucSectionBuf[3] = (iLen & 0x1F) << 3;
|
||||
memcpy(_aucSectionBuf + 4, ptr, _ui32MtuSize - 20);
|
||||
makeAACRtp(_aucSectionBuf, _ui32MtuSize - 16, false, uiStamp);
|
||||
ptr += (_ui32MtuSize - 20);
|
||||
iSize -= (_ui32MtuSize - 20);
|
||||
}
|
||||
}
|
||||
|
||||
void AACRtpEncoder::makeAACRtp(const void *data, unsigned int len, bool mark, uint32_t uiStamp) {
|
||||
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp), false);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
AACRtpDecoder::AACRtpDecoder(const Track::Ptr &track){
|
||||
auto aacTrack = dynamic_pointer_cast<AACTrack>(track);
|
||||
if(!aacTrack || !aacTrack->ready()){
|
||||
WarnL << "该aac track无效!";
|
||||
}else{
|
||||
_aac_cfg = aacTrack->getAacCfg();
|
||||
}
|
||||
_adts = obtainFrame();
|
||||
}
|
||||
AACRtpDecoder::AACRtpDecoder() {
|
||||
_adts = obtainFrame();
|
||||
}
|
||||
|
||||
AACFrame::Ptr AACRtpDecoder::obtainFrame() {
|
||||
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
||||
auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
|
||||
frame->aac_frame_length = 7;
|
||||
frame->iPrefixSize = 7;
|
||||
if(frame->syncword == 0 && !_aac_cfg.empty()) {
|
||||
makeAdtsHeader(_aac_cfg,*frame);
|
||||
}
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
|
||||
RtpCodec::inputRtp(rtppack, false);
|
||||
|
||||
// 获取rtp数据长度
|
||||
int length = rtppack->size() - rtppack->offset;
|
||||
|
||||
// 获取rtp数据
|
||||
const uint8_t *rtp_packet_buf = (uint8_t *)rtppack->data() + rtppack->offset;
|
||||
|
||||
do
|
||||
{
|
||||
// 查询头部的偏移,每次2字节
|
||||
uint32_t au_header_offset = 0;
|
||||
//首2字节表示Au-Header的长度,单位bit,所以除以16得到Au-Header字节数
|
||||
const uint16_t au_header_length = (((rtp_packet_buf[au_header_offset] << 8) | rtp_packet_buf[au_header_offset + 1]) >> 4);
|
||||
au_header_offset += 2;
|
||||
|
||||
//assert(length > (2 + au_header_length * 2));
|
||||
if (length < (2 + au_header_length * 2))
|
||||
break;
|
||||
|
||||
// 存放每一个aac帧长度
|
||||
std::vector<uint32_t > vec_aac_len;
|
||||
for (int i = 0; i < au_header_length; ++i)
|
||||
{
|
||||
// 之后的2字节是AU_HEADER
|
||||
const uint16_t au_header = ((rtp_packet_buf[au_header_offset] << 8) | rtp_packet_buf[au_header_offset + 1]);
|
||||
// 其中高13位表示一帧AAC负载的字节长度,低3位无用
|
||||
uint32_t nAac = (au_header >> 3);
|
||||
vec_aac_len.push_back(nAac);
|
||||
au_header_offset += 2;
|
||||
}
|
||||
|
||||
// 真正aac负载开始处
|
||||
const uint8_t *rtp_packet_payload = rtp_packet_buf + au_header_offset;
|
||||
// 载荷查找
|
||||
uint32_t next_aac_payload_offset = 0;
|
||||
for (int j = 0; j < au_header_length; ++j)
|
||||
{
|
||||
// 当前aac包长度
|
||||
const uint32_t cur_aac_payload_len = vec_aac_len.at(j);
|
||||
|
||||
if (_adts->aac_frame_length + cur_aac_payload_len > sizeof(AACFrame::buffer)) {
|
||||
_adts->aac_frame_length = 7;
|
||||
WarnL << "aac负载数据太长";
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取每一包aac载荷数据
|
||||
memcpy(_adts->buffer + _adts->aac_frame_length, rtp_packet_payload + next_aac_payload_offset, cur_aac_payload_len);
|
||||
_adts->aac_frame_length += (cur_aac_payload_len);
|
||||
if (rtppack->mark == true) {
|
||||
_adts->sequence = rtppack->sequence;
|
||||
_adts->timeStamp = rtppack->timeStamp;
|
||||
writeAdtsHeader(*_adts, _adts->buffer);
|
||||
onGetAAC(_adts);
|
||||
}
|
||||
|
||||
next_aac_payload_offset += cur_aac_payload_len;
|
||||
}
|
||||
} while (0);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void AACRtpDecoder::onGetAAC(const AACFrame::Ptr &frame) {
|
||||
//写入环形缓存
|
||||
RtpCodec::inputFrame(frame);
|
||||
_adts = obtainFrame();
|
||||
}
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
|
||||
|
||||
101
src/Extension/AACRtpCodec.h
Normal file
101
src/Extension/AACRtpCodec.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ZLMEDIAKIT_AACRTPCODEC_H
|
||||
#define ZLMEDIAKIT_AACRTPCODEC_H
|
||||
|
||||
#include "Rtsp/RtpCodec.h"
|
||||
#include "Extension/AAC.h"
|
||||
namespace mediakit{
|
||||
/**
|
||||
* aac rtp转adts类
|
||||
*/
|
||||
class AACRtpDecoder : public RtpCodec , public ResourcePoolHelper<AACFrame> {
|
||||
public:
|
||||
typedef std::shared_ptr<AACRtpDecoder> Ptr;
|
||||
|
||||
AACRtpDecoder(const Track::Ptr &track);
|
||||
~AACRtpDecoder() {}
|
||||
|
||||
/**
|
||||
* 输入rtp并解码
|
||||
* @param rtp rtp数据包
|
||||
* @param key_pos 此参数内部强制转换为false,请忽略之
|
||||
*/
|
||||
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = false) override;
|
||||
|
||||
TrackType getTrackType() const override{
|
||||
return TrackAudio;
|
||||
}
|
||||
CodecId getCodecId() const override{
|
||||
return CodecAAC;
|
||||
}
|
||||
protected:
|
||||
AACRtpDecoder();
|
||||
private:
|
||||
void onGetAAC(const AACFrame::Ptr &frame);
|
||||
AACFrame::Ptr obtainFrame();
|
||||
private:
|
||||
AACFrame::Ptr _adts;
|
||||
string _aac_cfg;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* aac adts转rtp类
|
||||
*/
|
||||
class AACRtpEncoder : public AACRtpDecoder , public RtpInfo {
|
||||
public:
|
||||
typedef std::shared_ptr<AACRtpEncoder> Ptr;
|
||||
|
||||
/**
|
||||
* @param ui32Ssrc ssrc
|
||||
* @param ui32MtuSize mtu 大小
|
||||
* @param ui32SampleRate 采样率
|
||||
* @param ui8PlayloadType pt类型
|
||||
* @param ui8Interleaved rtsp interleaved 值
|
||||
*/
|
||||
AACRtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize,
|
||||
uint32_t ui32SampleRate,
|
||||
uint8_t ui8PlayloadType = 97,
|
||||
uint8_t ui8Interleaved = TrackAudio * 2);
|
||||
~AACRtpEncoder() {}
|
||||
|
||||
/**
|
||||
* 输入aac 数据,必须带dats头
|
||||
* @param frame 带dats头的aac数据
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
private:
|
||||
void makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
|
||||
private:
|
||||
unsigned char _aucSectionBuf[1600];
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_AACRTPCODEC_H
|
||||
@@ -25,11 +25,11 @@
|
||||
*/
|
||||
|
||||
#include "Factory.h"
|
||||
#include "RtmpMuxer/H264RtmpCodec.h"
|
||||
#include "RtmpMuxer/AACRtmpCodec.h"
|
||||
#include "RtspMuxer/H264RtpCodec.h"
|
||||
#include "RtspMuxer/AACRtpCodec.h"
|
||||
#include "RtspMuxer/H265RtpCodec.h"
|
||||
#include "H264RtmpCodec.h"
|
||||
#include "AACRtmpCodec.h"
|
||||
#include "H264RtpCodec.h"
|
||||
#include "AACRtpCodec.h"
|
||||
#include "H265RtpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
|
||||
@@ -30,8 +30,8 @@
|
||||
#include <string>
|
||||
#include "Rtmp/amf.h"
|
||||
#include "Extension/Track.h"
|
||||
#include "RtspMuxer/RtpCodec.h"
|
||||
#include "RtmpMuxer/RtmpCodec.h"
|
||||
#include "Rtsp/RtpCodec.h"
|
||||
#include "Rtmp/RtmpCodec.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
260
src/Extension/H264RtmpCodec.cpp
Normal file
260
src/Extension/H264RtmpCodec.cpp
Normal file
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "H264RtmpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
H264RtmpDecoder::H264RtmpDecoder() {
|
||||
_h264frame = obtainFrame();
|
||||
}
|
||||
|
||||
H264Frame::Ptr H264RtmpDecoder::obtainFrame() {
|
||||
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
||||
auto frame = obtainObj();
|
||||
frame->buffer.clear();
|
||||
frame->iPrefixSize = 4;
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) {
|
||||
key_pos = decodeRtmp(rtmp);
|
||||
RtmpCodec::inputRtmp(rtmp, key_pos);
|
||||
return key_pos;
|
||||
}
|
||||
|
||||
bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
if (pkt->isCfgFrame()) {
|
||||
//缓存sps pps,后续插入到I帧之前
|
||||
_sps = pkt->getH264SPS();
|
||||
_pps = pkt->getH264PPS();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pkt->strBuf.size() > 9) {
|
||||
uint32_t iTotalLen = pkt->strBuf.size();
|
||||
uint32_t iOffset = 5;
|
||||
uint8_t *cts_ptr = (uint8_t *) (pkt->strBuf.data() + 2);
|
||||
int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
|
||||
auto pts = pkt->timeStamp + cts;
|
||||
|
||||
while(iOffset + 4 < iTotalLen){
|
||||
uint32_t iFrameLen;
|
||||
memcpy(&iFrameLen, pkt->strBuf.data() + iOffset, 4);
|
||||
iFrameLen = ntohl(iFrameLen);
|
||||
iOffset += 4;
|
||||
if(iFrameLen + iOffset > iTotalLen){
|
||||
break;
|
||||
}
|
||||
onGetH264_l(pkt->strBuf.data() + iOffset, iFrameLen, pkt->timeStamp , pts);
|
||||
iOffset += iFrameLen;
|
||||
}
|
||||
}
|
||||
return pkt->isVideoKeyFrame();
|
||||
}
|
||||
|
||||
|
||||
inline void H264RtmpDecoder::onGetH264_l(const char* pcData, int iLen, uint32_t dts,uint32_t pts) {
|
||||
switch (H264_TYPE(pcData[0])) {
|
||||
case H264Frame::NAL_IDR: {
|
||||
//I frame
|
||||
if(_sps.length()){
|
||||
onGetH264(_sps.data(), _sps.length(), dts , pts);
|
||||
}
|
||||
if(_pps.length()){
|
||||
onGetH264(_pps.data(), _pps.length(), dts , pts);
|
||||
}
|
||||
onGetH264(pcData, iLen, dts , pts);
|
||||
}
|
||||
break;
|
||||
case H264Frame::NAL_B_P: {
|
||||
//I or P or B frame
|
||||
onGetH264(pcData, iLen, dts , pts);
|
||||
}
|
||||
break;
|
||||
case H264Frame::NAL_SPS: {
|
||||
_sps.assign(pcData, iLen);
|
||||
}
|
||||
break;
|
||||
case H264Frame::NAL_PPS:{
|
||||
_pps.assign(pcData, iLen);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dts,uint32_t pts) {
|
||||
_h264frame->type = H264_TYPE(pcData[0]);
|
||||
_h264frame->timeStamp = dts;
|
||||
_h264frame->ptsStamp = pts;
|
||||
_h264frame->buffer.assign("\x0\x0\x0\x1", 4); //添加264头
|
||||
_h264frame->buffer.append(pcData, iLen);
|
||||
|
||||
//写入环形缓存
|
||||
RtmpCodec::inputFrame(_h264frame);
|
||||
_h264frame = obtainFrame();
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
H264RtmpEncoder::H264RtmpEncoder(const Track::Ptr &track) {
|
||||
_track = dynamic_pointer_cast<H264Track>(track);
|
||||
|
||||
}
|
||||
|
||||
void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
RtmpCodec::inputFrame(frame);
|
||||
|
||||
auto pcData = frame->data() + frame->prefixSize();
|
||||
auto iLen = frame->size() - frame->prefixSize();
|
||||
auto type = H264_TYPE(((uint8_t*)pcData)[0]);
|
||||
|
||||
if(!_gotSpsPps){
|
||||
//尝试从frame中获取sps pps
|
||||
switch (type){
|
||||
case H264Frame::NAL_SPS:{
|
||||
//sps
|
||||
if(_sps.empty()){
|
||||
_sps = string(pcData,iLen);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case H264Frame::NAL_PPS:{
|
||||
//pps
|
||||
if(_pps.empty()){
|
||||
_pps = string(pcData,iLen);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if(_track && _track->ready()){
|
||||
//尝试从track中获取sps pps信息
|
||||
_sps = _track->getSps();
|
||||
_pps = _track->getPps();
|
||||
}
|
||||
|
||||
if(!_sps.empty() && !_pps.empty()){
|
||||
_gotSpsPps = true;
|
||||
makeVideoConfigPkt();
|
||||
}
|
||||
}
|
||||
|
||||
switch (type){
|
||||
case H264Frame::NAL_IDR:
|
||||
case H264Frame::NAL_B_P:{
|
||||
if(_lastPacket && _lastPacket->timeStamp != frame->stamp()) {
|
||||
RtmpCodec::inputRtmp(_lastPacket, _lastPacket->isVideoKeyFrame());
|
||||
_lastPacket = nullptr;
|
||||
}
|
||||
|
||||
if(!_lastPacket) {
|
||||
//I or P or B frame
|
||||
int8_t flags = 7; //h.264
|
||||
bool is_config = false;
|
||||
flags |= ((frame->keyFrame() ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4);
|
||||
|
||||
_lastPacket = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
||||
_lastPacket->strBuf.clear();
|
||||
_lastPacket->strBuf.push_back(flags);
|
||||
_lastPacket->strBuf.push_back(!is_config);
|
||||
auto cts = frame->pts() - frame->dts();
|
||||
cts = htonl(cts);
|
||||
_lastPacket->strBuf.append((char *)&cts + 1, 3);
|
||||
|
||||
_lastPacket->chunkId = CHUNK_VIDEO;
|
||||
_lastPacket->streamId = STREAM_MEDIA;
|
||||
_lastPacket->timeStamp = frame->stamp();
|
||||
_lastPacket->typeId = MSG_VIDEO;
|
||||
|
||||
}
|
||||
auto size = htonl(iLen);
|
||||
_lastPacket->strBuf.append((char *) &size, 4);
|
||||
_lastPacket->strBuf.append(pcData, iLen);
|
||||
_lastPacket->bodySize = _lastPacket->strBuf.size();
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void H264RtmpEncoder::makeVideoConfigPkt() {
|
||||
int8_t flags = 7; //h.264
|
||||
flags |= (FLV_KEY_FRAME << 4);
|
||||
bool is_config = true;
|
||||
|
||||
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
|
||||
rtmpPkt->strBuf.clear();
|
||||
|
||||
//////////header
|
||||
rtmpPkt->strBuf.push_back(flags);
|
||||
rtmpPkt->strBuf.push_back(!is_config);
|
||||
rtmpPkt->strBuf.append("\x0\x0\x0", 3);
|
||||
|
||||
////////////sps
|
||||
rtmpPkt->strBuf.push_back(1); // version
|
||||
|
||||
//DebugL<<hexdump(_sps.data(), _sps.size());
|
||||
rtmpPkt->strBuf.push_back(_sps[1]); // profile
|
||||
rtmpPkt->strBuf.push_back(_sps[2]); // compat
|
||||
rtmpPkt->strBuf.push_back(_sps[3]); // level
|
||||
rtmpPkt->strBuf.push_back(0xff); // 6 bits reserved + 2 bits nal size length - 1 (11)
|
||||
rtmpPkt->strBuf.push_back(0xe1); // 3 bits reserved + 5 bits number of sps (00001)
|
||||
uint16_t size = _sps.size();
|
||||
size = htons(size);
|
||||
rtmpPkt->strBuf.append((char *) &size, 2);
|
||||
rtmpPkt->strBuf.append(_sps);
|
||||
|
||||
/////////////pps
|
||||
rtmpPkt->strBuf.push_back(1); // version
|
||||
size = _pps.size();
|
||||
size = htons(size);
|
||||
rtmpPkt->strBuf.append((char *) &size, 2);
|
||||
rtmpPkt->strBuf.append(_pps);
|
||||
|
||||
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
|
||||
rtmpPkt->chunkId = CHUNK_VIDEO;
|
||||
rtmpPkt->streamId = STREAM_MEDIA;
|
||||
rtmpPkt->timeStamp = 0;
|
||||
rtmpPkt->typeId = MSG_VIDEO;
|
||||
RtmpCodec::inputRtmp(rtmpPkt, false);
|
||||
}
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
103
src/Extension/H264RtmpCodec.h
Normal file
103
src/Extension/H264RtmpCodec.h
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ZLMEDIAKIT_H264RTMPCODEC_H
|
||||
#define ZLMEDIAKIT_H264RTMPCODEC_H
|
||||
|
||||
#include "Rtmp/RtmpCodec.h"
|
||||
#include "Extension/Track.h"
|
||||
#include "Util/ResourcePool.h"
|
||||
#include "Extension/H264.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
/**
|
||||
* h264 Rtmp解码类
|
||||
*/
|
||||
class H264RtmpDecoder : public RtmpCodec ,public ResourcePoolHelper<H264Frame> {
|
||||
public:
|
||||
typedef std::shared_ptr<H264RtmpDecoder> Ptr;
|
||||
|
||||
H264RtmpDecoder();
|
||||
~H264RtmpDecoder() {}
|
||||
|
||||
/**
|
||||
* 输入264 Rtmp包
|
||||
* @param rtmp Rtmp包
|
||||
* @param key_pos 此参数忽略之
|
||||
*/
|
||||
bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos = true) override;
|
||||
|
||||
TrackType getTrackType() const override{
|
||||
return TrackVideo;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const override{
|
||||
return CodecH264;
|
||||
}
|
||||
protected:
|
||||
bool decodeRtmp(const RtmpPacket::Ptr &Rtmp);
|
||||
void onGetH264_l(const char *pcData, int iLen, uint32_t dts,uint32_t pts);
|
||||
void onGetH264(const char *pcData, int iLen, uint32_t dts,uint32_t pts);
|
||||
H264Frame::Ptr obtainFrame();
|
||||
protected:
|
||||
H264Frame::Ptr _h264frame;
|
||||
string _sps;
|
||||
string _pps;
|
||||
};
|
||||
|
||||
/**
|
||||
* 264 Rtmp打包类
|
||||
*/
|
||||
class H264RtmpEncoder : public H264RtmpDecoder, public ResourcePoolHelper<RtmpPacket> {
|
||||
public:
|
||||
typedef std::shared_ptr<H264RtmpEncoder> Ptr;
|
||||
|
||||
/**
|
||||
* 构造函数,track可以为空,此时则在inputFrame时输入sps pps
|
||||
* 如果track不为空且包含sps pps信息,
|
||||
* 那么inputFrame时可以不输入sps pps
|
||||
* @param track
|
||||
*/
|
||||
H264RtmpEncoder(const Track::Ptr &track);
|
||||
~H264RtmpEncoder() {}
|
||||
|
||||
/**
|
||||
* 输入264帧,可以不带sps pps
|
||||
* @param frame 帧数据
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
private:
|
||||
void makeVideoConfigPkt();
|
||||
private:
|
||||
H264Track::Ptr _track;
|
||||
bool _gotSpsPps = false;
|
||||
RtmpPacket::Ptr _lastPacket;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_H264RTMPCODEC_H
|
||||
269
src/Extension/H264RtpCodec.cpp
Normal file
269
src/Extension/H264RtpCodec.cpp
Normal file
@@ -0,0 +1,269 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
* Copyright (c) 2019 火宣 <459502659@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "H264RtpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
|
||||
typedef struct {
|
||||
unsigned forbidden_zero_bit :1;
|
||||
unsigned nal_ref_idc :2;
|
||||
unsigned type :5;
|
||||
} NALU;
|
||||
|
||||
typedef struct {
|
||||
unsigned S :1;
|
||||
unsigned E :1;
|
||||
unsigned R :1;
|
||||
unsigned type :5;
|
||||
} FU;
|
||||
|
||||
static bool MakeNalu(uint8_t in, NALU &nal) {
|
||||
nal.forbidden_zero_bit = in >> 7;
|
||||
if (nal.forbidden_zero_bit) {
|
||||
return false;
|
||||
}
|
||||
nal.nal_ref_idc = (in & 0x60) >> 5;
|
||||
nal.type = in & 0x1f;
|
||||
return true;
|
||||
}
|
||||
static bool MakeFU(uint8_t in, FU &fu) {
|
||||
fu.S = in >> 7;
|
||||
fu.E = (in >> 6) & 0x01;
|
||||
fu.R = (in >> 5) & 0x01;
|
||||
fu.type = in & 0x1f;
|
||||
if (fu.R != 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
H264RtpDecoder::H264RtpDecoder() {
|
||||
_h264frame = obtainFrame();
|
||||
}
|
||||
|
||||
H264Frame::Ptr H264RtpDecoder::obtainFrame() {
|
||||
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
||||
auto frame = ResourcePoolHelper<H264Frame>::obtainObj();
|
||||
frame->buffer.clear();
|
||||
frame->iPrefixSize = 4;
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool H264RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
|
||||
key_pos = decodeRtp(rtp);
|
||||
RtpCodec::inputRtp(rtp, key_pos);
|
||||
return key_pos;
|
||||
}
|
||||
|
||||
bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
|
||||
/**
|
||||
* h264帧类型
|
||||
* Type==1:P/B frame
|
||||
* Type==5:IDR frame
|
||||
* Type==6:SEI frame
|
||||
* Type==7:SPS frame
|
||||
* Type==8:PPS frame
|
||||
*/
|
||||
|
||||
const uint8_t *frame = (uint8_t *) rtppack->data() + rtppack->offset;
|
||||
int length = rtppack->size() - rtppack->offset;
|
||||
NALU nal;
|
||||
MakeNalu(*frame, nal);
|
||||
|
||||
if (nal.type >= 0 && nal.type < 24) {
|
||||
//a full frame
|
||||
_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
|
||||
_h264frame->buffer.append((char *)frame, length);
|
||||
_h264frame->type = nal.type;
|
||||
_h264frame->timeStamp = rtppack->timeStamp;
|
||||
_h264frame->sequence = rtppack->sequence;
|
||||
auto isIDR = _h264frame->type == H264Frame::NAL_IDR;
|
||||
onGetH264(_h264frame);
|
||||
return (isIDR); //i frame
|
||||
}
|
||||
|
||||
switch (nal.type){
|
||||
case 24:{
|
||||
// 24 STAP-A 单一时间的组合包
|
||||
bool haveIDR = false;
|
||||
auto ptr = frame + 1;
|
||||
while(true){
|
||||
int off = ptr - frame;
|
||||
if (off >= length) {
|
||||
break;
|
||||
}
|
||||
//获取当前nalu的大小
|
||||
uint16_t len = *ptr++;
|
||||
len <<= 8;
|
||||
len |= *ptr++;
|
||||
if (off + len > length) {
|
||||
break;
|
||||
}
|
||||
if(len >= 10){
|
||||
//过小的帧丢弃
|
||||
NALU nal;
|
||||
MakeNalu(ptr[0], nal);
|
||||
_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
|
||||
_h264frame->buffer.append((char *)ptr, len);
|
||||
_h264frame->type = nal.type;
|
||||
_h264frame->timeStamp = rtppack->timeStamp;
|
||||
_h264frame->sequence = rtppack->sequence;
|
||||
if(nal.type == H264Frame::NAL_IDR){
|
||||
haveIDR = true;
|
||||
}
|
||||
onGetH264(_h264frame);
|
||||
}
|
||||
ptr += len;
|
||||
}
|
||||
return haveIDR;
|
||||
}
|
||||
|
||||
case 28:{
|
||||
//FU-A
|
||||
FU fu;
|
||||
MakeFU(frame[1], fu);
|
||||
if (fu.S == 1) {
|
||||
//FU-A start
|
||||
char tmp = (nal.forbidden_zero_bit << 7 | nal.nal_ref_idc << 5 | fu.type);
|
||||
_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
|
||||
_h264frame->buffer.push_back(tmp);
|
||||
_h264frame->buffer.append((char *)frame + 2, length - 2);
|
||||
_h264frame->type = fu.type;
|
||||
_h264frame->timeStamp = rtppack->timeStamp;
|
||||
_h264frame->sequence = rtppack->sequence;
|
||||
return (_h264frame->type == H264Frame::NAL_IDR); //i frame
|
||||
}
|
||||
|
||||
if (rtppack->sequence != (uint16_t)(_h264frame->sequence + 1)) {
|
||||
_h264frame->buffer.clear();
|
||||
WarnL << "丢包,帧废弃:" << rtppack->sequence << "," << _h264frame->sequence;
|
||||
return false;
|
||||
}
|
||||
_h264frame->sequence = rtppack->sequence;
|
||||
if (fu.E == 1) {
|
||||
//FU-A end
|
||||
_h264frame->buffer.append((char *)frame + 2, length - 2);
|
||||
_h264frame->timeStamp = rtppack->timeStamp;
|
||||
auto isIDR = _h264frame->type == H264Frame::NAL_IDR;
|
||||
onGetH264(_h264frame);
|
||||
return isIDR;
|
||||
}
|
||||
//FU-A mid
|
||||
_h264frame->buffer.append((char *)frame + 2, length - 2);
|
||||
return false;
|
||||
}
|
||||
|
||||
default:{
|
||||
// 29 FU-B 单NAL单元B模式
|
||||
// 25 STAP-B 单一时间的组合包
|
||||
// 26 MTAP16 多个时间的组合包
|
||||
// 27 MTAP24 多个时间的组合包
|
||||
// 0 udef
|
||||
// 30 udef
|
||||
// 31 udef
|
||||
WarnL << "不支持的rtp类型:" << (int)nal.type << " " << rtppack->sequence;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void H264RtpDecoder::onGetH264(const H264Frame::Ptr &frame) {
|
||||
//写入环形缓存
|
||||
auto lastSeq = _h264frame->sequence;
|
||||
RtpCodec::inputFrame(frame);
|
||||
_h264frame = obtainFrame();
|
||||
_h264frame->sequence = lastSeq;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize,
|
||||
uint32_t ui32SampleRate,
|
||||
uint8_t ui8PlayloadType,
|
||||
uint8_t ui8Interleaved) :
|
||||
RtpInfo(ui32Ssrc,
|
||||
ui32MtuSize,
|
||||
ui32SampleRate,
|
||||
ui8PlayloadType,
|
||||
ui8Interleaved) {
|
||||
}
|
||||
|
||||
void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
RtpCodec::inputFrame(frame);
|
||||
|
||||
GET_CONFIG(uint32_t,cycleMS,Rtp::kCycleMS);
|
||||
auto pcData = frame->data() + frame->prefixSize();
|
||||
auto uiStamp = frame->stamp();
|
||||
auto iLen = frame->size() - frame->prefixSize();
|
||||
unsigned char naluType = H264_TYPE(pcData[0]); //获取NALU的5bit 帧类型
|
||||
|
||||
uiStamp %= cycleMS;
|
||||
int iSize = _ui32MtuSize - 2;
|
||||
if (iLen > iSize) { //超过MTU
|
||||
const unsigned char s_e_r_Start = 0x80;
|
||||
const unsigned char s_e_r_Mid = 0x00;
|
||||
const unsigned char s_e_r_End = 0x40;
|
||||
//获取帧头数据,1byte
|
||||
unsigned char nal_ref_idc = *((unsigned char *) pcData) & 0x60; //获取NALU的2bit 帧重要程度 00 可以丢 11不能丢
|
||||
//nal_ref_idc = 0x60;
|
||||
//组装FU-A帧头数据 2byte
|
||||
unsigned char f_nri_type = nal_ref_idc + 28;//F为0 1bit,nri上面获取到2bit,28为FU-A分片类型5bit
|
||||
unsigned char s_e_r_type = naluType;
|
||||
bool bFirst = true;
|
||||
bool mark = false;
|
||||
int nOffset = 1;
|
||||
while (!mark) {
|
||||
if (iLen < nOffset + iSize) { //是否拆分结束
|
||||
iSize = iLen - nOffset;
|
||||
mark = true;
|
||||
s_e_r_type = s_e_r_End + naluType;
|
||||
} else if (bFirst) {
|
||||
s_e_r_type = s_e_r_Start + naluType;
|
||||
} else {
|
||||
s_e_r_type = s_e_r_Mid + naluType;
|
||||
}
|
||||
memcpy(_aucSectionBuf, &f_nri_type, 1);
|
||||
memcpy(_aucSectionBuf + 1, &s_e_r_type, 1);
|
||||
memcpy(_aucSectionBuf + 2, (unsigned char *) pcData + nOffset, iSize);
|
||||
nOffset += iSize;
|
||||
makeH264Rtp(naluType,_aucSectionBuf, iSize + 2, mark,bFirst, uiStamp);
|
||||
bFirst = false;
|
||||
}
|
||||
} else {
|
||||
makeH264Rtp(naluType,pcData, iLen, true, true, uiStamp);
|
||||
}
|
||||
}
|
||||
|
||||
void H264RtpEncoder::makeH264Rtp(int nal_type,const void* data, unsigned int len, bool mark, bool first_packet, uint32_t uiStamp) {
|
||||
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp),first_packet && nal_type == H264Frame::NAL_IDR);
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
103
src/Extension/H264RtpCodec.h
Normal file
103
src/Extension/H264RtpCodec.h
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ZLMEDIAKIT_H264RTPCODEC_H
|
||||
#define ZLMEDIAKIT_H264RTPCODEC_H
|
||||
|
||||
#include "Rtsp/RtpCodec.h"
|
||||
#include "Util/ResourcePool.h"
|
||||
#include "Extension/H264.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
/**
|
||||
* h264 rtp解码类
|
||||
*/
|
||||
class H264RtpDecoder : public RtpCodec , public ResourcePoolHelper<H264Frame> {
|
||||
public:
|
||||
typedef std::shared_ptr<H264RtpDecoder> Ptr;
|
||||
|
||||
H264RtpDecoder();
|
||||
~H264RtpDecoder() {}
|
||||
|
||||
/**
|
||||
* 输入264 rtp包
|
||||
* @param rtp rtp包
|
||||
* @param key_pos 此参数忽略之
|
||||
*/
|
||||
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override;
|
||||
|
||||
TrackType getTrackType() const override{
|
||||
return TrackVideo;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const override{
|
||||
return CodecH264;
|
||||
}
|
||||
private:
|
||||
bool decodeRtp(const RtpPacket::Ptr &rtp);
|
||||
void onGetH264(const H264Frame::Ptr &frame);
|
||||
H264Frame::Ptr obtainFrame();
|
||||
private:
|
||||
H264Frame::Ptr _h264frame;
|
||||
};
|
||||
|
||||
/**
|
||||
* 264 rtp打包类
|
||||
*/
|
||||
class H264RtpEncoder : public H264RtpDecoder ,public RtpInfo{
|
||||
public:
|
||||
typedef std::shared_ptr<H264RtpEncoder> Ptr;
|
||||
|
||||
/**
|
||||
* @param ui32Ssrc ssrc
|
||||
* @param ui32MtuSize mtu大小
|
||||
* @param ui32SampleRate 采样率,强制为90000
|
||||
* @param ui8PlayloadType pt类型
|
||||
* @param ui8Interleaved rtsp interleaved
|
||||
*/
|
||||
H264RtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize = 1400,
|
||||
uint32_t ui32SampleRate = 90000,
|
||||
uint8_t ui8PlayloadType = 96,
|
||||
uint8_t ui8Interleaved = TrackVideo * 2);
|
||||
~H264RtpEncoder() {}
|
||||
|
||||
/**
|
||||
* 输入264帧
|
||||
* @param frame 帧数据,必须
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
private:
|
||||
void makeH264Rtp(int nal_type,const void *pData, unsigned int uiLen, bool bMark, bool first_packet, uint32_t uiStamp);
|
||||
private:
|
||||
unsigned char _aucSectionBuf[1600];
|
||||
};
|
||||
|
||||
}//namespace mediakit{
|
||||
|
||||
#endif //ZLMEDIAKIT_H264RTPCODEC_H
|
||||
215
src/Extension/H265RtpCodec.cpp
Normal file
215
src/Extension/H265RtpCodec.cpp
Normal file
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "H265RtpCodec.h"
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
//41
|
||||
//42 0 1
|
||||
//43 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
|
||||
//44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
//45 |F| Type | LayerId | TID |
|
||||
//46 +-------------+-----------------+
|
||||
//48 F = 0
|
||||
//49 Type = 49 (fragmentation unit (FU))
|
||||
//50 LayerId = 0
|
||||
//51 TID = 1
|
||||
//56 /*
|
||||
//57 create the FU header
|
||||
//58
|
||||
//59 0 1 2 3 4 5 6 7
|
||||
//60 +-+-+-+-+-+-+-+-+
|
||||
//61 |S|E| FuType |
|
||||
//62 +---------------+
|
||||
//63
|
||||
//64 S = variable
|
||||
//65 E = variable
|
||||
//66 FuType = NAL unit type
|
||||
//67
|
||||
|
||||
typedef struct {
|
||||
unsigned S :1;
|
||||
unsigned E :1;
|
||||
unsigned type :6;
|
||||
} FU;
|
||||
|
||||
static void MakeFU(uint8_t in, FU &fu) {
|
||||
fu.S = in >> 7;
|
||||
fu.E = (in >> 6) & 0x01;
|
||||
fu.type = in & 0x3f;
|
||||
}
|
||||
|
||||
H265RtpDecoder::H265RtpDecoder() {
|
||||
_h265frame = obtainFrame();
|
||||
}
|
||||
|
||||
H265Frame::Ptr H265RtpDecoder::obtainFrame() {
|
||||
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
||||
auto frame = ResourcePoolHelper<H265Frame>::obtainObj();
|
||||
frame->buffer.clear();
|
||||
frame->iPrefixSize = 4;
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool H265RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
|
||||
key_pos = decodeRtp(rtp);
|
||||
RtpCodec::inputRtp(rtp, key_pos);
|
||||
return key_pos;
|
||||
}
|
||||
|
||||
bool H265RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
|
||||
const uint8_t *frame = (uint8_t *) rtppack->data() + rtppack->offset;
|
||||
int length = rtppack->size() - rtppack->offset;
|
||||
int nal = H265_TYPE(frame[0]);
|
||||
|
||||
if (nal > 50){
|
||||
WarnL << "不支持该类型的265 RTP包" << nal;
|
||||
return false; // packet discard, Unsupported (HEVC) NAL type
|
||||
}
|
||||
switch (nal) {
|
||||
case 50:
|
||||
case 48: // aggregated packet (AP) - with two or more NAL units
|
||||
WarnL << "不支持该类型的265 RTP包" << nal;
|
||||
return false;
|
||||
case 49: {
|
||||
// fragmentation unit (FU)
|
||||
FU fu;
|
||||
MakeFU(frame[2], fu);
|
||||
if (fu.S == 1) {
|
||||
//FU-A start
|
||||
_h265frame->buffer.assign("\x0\x0\x0\x1", 4);
|
||||
_h265frame->buffer.push_back(fu.type << 1);
|
||||
_h265frame->buffer.push_back(0x01);
|
||||
_h265frame->buffer.append((char *) frame + 3, length - 3);
|
||||
_h265frame->type = fu.type;
|
||||
_h265frame->timeStamp = rtppack->timeStamp;
|
||||
_h265frame->sequence = rtppack->sequence;
|
||||
return (_h265frame->keyFrame()); //i frame
|
||||
}
|
||||
|
||||
if (rtppack->sequence != (uint16_t) (_h265frame->sequence + 1)) {
|
||||
_h265frame->buffer.clear();
|
||||
WarnL << "丢包,帧废弃:" << rtppack->sequence << "," << _h265frame->sequence;
|
||||
return false;
|
||||
}
|
||||
_h265frame->sequence = rtppack->sequence;
|
||||
if (fu.E == 1) {
|
||||
//FU-A end
|
||||
_h265frame->buffer.append((char *) frame + 3, length - 3);
|
||||
_h265frame->timeStamp = rtppack->timeStamp;
|
||||
auto isIDR = _h265frame->keyFrame();
|
||||
onGetH265(_h265frame);
|
||||
return isIDR;
|
||||
}
|
||||
//FU-A mid
|
||||
_h265frame->buffer.append((char *) frame + 3, length - 3);
|
||||
return false;
|
||||
}
|
||||
|
||||
default: // 4.4.1. Single NAL Unit Packets (p24)
|
||||
//a full frame
|
||||
_h265frame->buffer.assign("\x0\x0\x0\x1", 4);
|
||||
_h265frame->buffer.append((char *)frame, length);
|
||||
_h265frame->type = nal;
|
||||
_h265frame->timeStamp = rtppack->timeStamp;
|
||||
_h265frame->sequence = rtppack->sequence;
|
||||
auto isIDR = _h265frame->keyFrame();
|
||||
onGetH265(_h265frame);
|
||||
return (isIDR); //i frame
|
||||
}
|
||||
}
|
||||
|
||||
void H265RtpDecoder::onGetH265(const H265Frame::Ptr &frame) {
|
||||
//写入环形缓存
|
||||
auto lastSeq = _h265frame->sequence;
|
||||
RtpCodec::inputFrame(frame);
|
||||
_h265frame = obtainFrame();
|
||||
_h265frame->sequence = lastSeq;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
H265RtpEncoder::H265RtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize,
|
||||
uint32_t ui32SampleRate,
|
||||
uint8_t ui8PlayloadType,
|
||||
uint8_t ui8Interleaved) :
|
||||
RtpInfo(ui32Ssrc,
|
||||
ui32MtuSize,
|
||||
ui32SampleRate,
|
||||
ui8PlayloadType,
|
||||
ui8Interleaved) {
|
||||
}
|
||||
|
||||
void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
RtpCodec::inputFrame(frame);
|
||||
|
||||
GET_CONFIG(uint32_t,cycleMS,Rtp::kCycleMS);
|
||||
uint8_t *pcData = (uint8_t*)frame->data() + frame->prefixSize();
|
||||
auto uiStamp = frame->stamp();
|
||||
auto iLen = frame->size() - frame->prefixSize();
|
||||
unsigned char naluType = H265_TYPE(pcData[0]); //获取NALU的5bit 帧类型
|
||||
uiStamp %= cycleMS;
|
||||
|
||||
int maxSize = _ui32MtuSize - 3;
|
||||
if (iLen > maxSize) { //超过MTU
|
||||
//获取帧头数据,1byte
|
||||
unsigned char s_e_type;
|
||||
bool bFirst = true;
|
||||
bool mark = false;
|
||||
int nOffset = 2;
|
||||
while (!mark) {
|
||||
if (iLen < nOffset + maxSize) { //是否拆分结束
|
||||
maxSize = iLen - nOffset;
|
||||
mark = true;
|
||||
s_e_type = 1 << 6 | naluType;
|
||||
} else if (bFirst) {
|
||||
s_e_type = 1 << 7 | naluType;
|
||||
} else {
|
||||
s_e_type = naluType;
|
||||
}
|
||||
|
||||
//FU type
|
||||
_aucSectionBuf[0] = 49 << 1;
|
||||
_aucSectionBuf[1] = 1;
|
||||
_aucSectionBuf[2] = s_e_type;
|
||||
memcpy(_aucSectionBuf + 3, pcData + nOffset, maxSize);
|
||||
nOffset += maxSize;
|
||||
makeH265Rtp(naluType,_aucSectionBuf, maxSize + 3, mark,bFirst, uiStamp);
|
||||
bFirst = false;
|
||||
}
|
||||
} else {
|
||||
makeH265Rtp(naluType,pcData, iLen, true, true, uiStamp);
|
||||
}
|
||||
}
|
||||
|
||||
void H265RtpEncoder::makeH265Rtp(int nal_type,const void* data, unsigned int len, bool mark, bool first_packet, uint32_t uiStamp) {
|
||||
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp),first_packet && H265Frame::isKeyFrame(nal_type));
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
104
src/Extension/H265RtpCodec.h
Normal file
104
src/Extension/H265RtpCodec.h
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ZLMEDIAKIT_H265RTPCODEC_H
|
||||
#define ZLMEDIAKIT_H265RTPCODEC_H
|
||||
|
||||
#include "Rtsp/RtpCodec.h"
|
||||
#include "Util/ResourcePool.h"
|
||||
#include "Extension/H265.h"
|
||||
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
/**
|
||||
* h265 rtp解码类
|
||||
*/
|
||||
class H265RtpDecoder : public RtpCodec , public ResourcePoolHelper<H265Frame> {
|
||||
public:
|
||||
typedef std::shared_ptr<H265RtpDecoder> Ptr;
|
||||
|
||||
H265RtpDecoder();
|
||||
~H265RtpDecoder() {}
|
||||
|
||||
/**
|
||||
* 输入265 rtp包
|
||||
* @param rtp rtp包
|
||||
* @param key_pos 此参数忽略之
|
||||
*/
|
||||
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override;
|
||||
|
||||
TrackType getTrackType() const override{
|
||||
return TrackVideo;
|
||||
}
|
||||
|
||||
CodecId getCodecId() const override{
|
||||
return CodecH265;
|
||||
}
|
||||
private:
|
||||
bool decodeRtp(const RtpPacket::Ptr &rtp);
|
||||
void onGetH265(const H265Frame::Ptr &frame);
|
||||
H265Frame::Ptr obtainFrame();
|
||||
private:
|
||||
H265Frame::Ptr _h265frame;
|
||||
};
|
||||
|
||||
/**
|
||||
* 265 rtp打包类
|
||||
*/
|
||||
class H265RtpEncoder : public H265RtpDecoder ,public RtpInfo{
|
||||
public:
|
||||
typedef std::shared_ptr<H265RtpEncoder> Ptr;
|
||||
|
||||
/**
|
||||
* @param ui32Ssrc ssrc
|
||||
* @param ui32MtuSize mtu大小
|
||||
* @param ui32SampleRate 采样率,强制为90000
|
||||
* @param ui8PlayloadType pt类型
|
||||
* @param ui8Interleaved rtsp interleaved
|
||||
*/
|
||||
H265RtpEncoder(uint32_t ui32Ssrc,
|
||||
uint32_t ui32MtuSize = 1400,
|
||||
uint32_t ui32SampleRate = 90000,
|
||||
uint8_t ui8PlayloadType = 96,
|
||||
uint8_t ui8Interleaved = TrackVideo * 2);
|
||||
~H265RtpEncoder() {}
|
||||
|
||||
/**
|
||||
* 输入265帧
|
||||
* @param frame 帧数据,必须
|
||||
*/
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
private:
|
||||
void makeH265Rtp(int nal_type,const void *pData, unsigned int uiLen, bool bMark, bool first_packet,uint32_t uiStamp);
|
||||
private:
|
||||
unsigned char _aucSectionBuf[1600];
|
||||
};
|
||||
|
||||
}//namespace mediakit{
|
||||
|
||||
#endif //ZLMEDIAKIT_H265RTPCODEC_H
|
||||
Reference in New Issue
Block a user