This commit is contained in:
xiongziliang 2020-04-18 18:46:20 +08:00
parent 1f43359b31
commit 5c3418a412
21 changed files with 277 additions and 364 deletions

@ -1 +1 @@
Subproject commit 2b4eacb22604f22d459b1c68195ae13e5cb9d62a
Subproject commit 987683f1045613098e2bcd534bc90a13d16df8a4

View File

@ -68,7 +68,7 @@ API_EXPORT void API_CALL mk_media_init_aac(mk_media ctx, int channel, int sample
/**
* g711音频轨道
* @param ctx
* @param au 1.G711A 2.G711U
* @param au 3 : G711A 4: G711U
* @param channel
* @param sample_bit 16
* @param sample_rate

View File

@ -31,7 +31,7 @@ typedef void(API_CALL *on_mk_play_event)(void *user_data,int err_code,const char
*
* @param user_data
* @param track_type 01
* @param codec_id 0H2641H2652AAC
* @param codec_id 0H2641H2652AAC 3.G711A 4.G711U
* @param data
* @param len
* @param dts
@ -98,13 +98,15 @@ API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_eve
/**
*
*
*
* @param ctx
* @param cb ,null
* @param user_data
*/
API_EXPORT void API_CALL mk_player_set_on_data(mk_player ctx, on_mk_play_data cb, void *user_data);
///////////////////////////获取音视频相关信息接口在播放成功回调触发后才有效///////////////////////////////
/**
* codec_id -1 0H2641H2652AAC 3.G711A 4.G711U
* @param ctx

View File

@ -113,6 +113,7 @@ API_EXPORT void API_CALL mk_media_init_h264(mk_media ctx, int width, int height,
assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
VideoInfo info;
info.codecId = CodecH264;
info.iFrameRate = frameRate;
info.iWidth = width;
info.iHeight = height;
@ -123,16 +124,18 @@ API_EXPORT void API_CALL mk_media_init_h265(mk_media ctx, int width, int height,
assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
VideoInfo info;
info.codecId = CodecH265;
info.iFrameRate = frameRate;
info.iWidth = width;
info.iHeight = height;
(*obj)->getChannel()->initH265Video(info);
(*obj)->getChannel()->initVideo(info);
}
API_EXPORT void API_CALL mk_media_init_aac(mk_media ctx, int channel, int sample_bit, int sample_rate, int profile) {
assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
AudioInfo info;
info.codecId = CodecAAC;
info.iSampleRate = sample_rate;
info.iChannel = channel;
info.iSampleBit = sample_bit;
@ -140,17 +143,16 @@ API_EXPORT void API_CALL mk_media_init_aac(mk_media ctx, int channel, int sample
(*obj)->getChannel()->initAudio(info);
}
API_EXPORT void API_CALL mk_media_init_g711(mk_media ctx, int au, int sample_bit, int sample_rate)
{
API_EXPORT void API_CALL mk_media_init_g711(mk_media ctx, int au, int sample_bit, int sample_rate){
assert(ctx);
assert(au == CodecG711A || au == CodecG711U);
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
AudioInfo info;
info.codecId = (CodecId)au;
info.iSampleRate = sample_rate;
info.iChannel = 1;
info.iSampleBit = sample_bit;
info.iProfile = 0;
info.codecId = (CodecId)au;
(*obj)->getChannel()->initAudio(info);
}
@ -184,12 +186,8 @@ API_EXPORT void API_CALL mk_media_input_aac1(mk_media ctx, void *data, int len,
(*obj)->getChannel()->inputAAC((char *) data, len, dts, (char *) adts);
}
API_EXPORT void API_CALL mk_media_input_g711(mk_media ctx, void* data, int len, uint32_t dts)
{
API_EXPORT void API_CALL mk_media_input_g711(mk_media ctx, void* data, int len, uint32_t dts){
assert(ctx && data && len > 0);
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
(*obj)->getChannel()->inputG711((char*)data, len, dts);
}

View File

@ -101,9 +101,7 @@ API_EXPORT void API_CALL mk_player_set_on_data(mk_player ctx, on_mk_play_data cb
});
}
API_EXPORT int API_CALL mk_player_video_codecId(mk_player ctx)
{
API_EXPORT int API_CALL mk_player_video_codecId(mk_player ctx){
assert(ctx);
MediaPlayer::Ptr& player = *((MediaPlayer::Ptr*)ctx);
auto track = dynamic_pointer_cast<VideoTrack>(player->getTrack(TrackVideo));
@ -131,9 +129,7 @@ API_EXPORT int API_CALL mk_player_video_fps(mk_player ctx) {
return track ? track->getVideoFps() : 0;
}
API_EXPORT int API_CALL mk_player_audio_codecId(mk_player ctx)
{
API_EXPORT int API_CALL mk_player_audio_codecId(mk_player ctx){
assert(ctx);
MediaPlayer::Ptr& player = *((MediaPlayer::Ptr*)ctx);
auto track = dynamic_pointer_cast<AudioTrack>(player->getTrack(TrackAudio));

View File

@ -8,13 +8,9 @@
* may be found in the AUTHORS file in the root of the source tree.
*/
#include <stdio.h>
#include <stdio.h>
#include "Device.h"
#include "Util/logger.h"
#include "Util/util.h"
#include "Util/base64.h"
#include "Util/TimeTicker.h"
#include "Extension/AAC.h"
#include "Extension/G711.h"
#include "Extension/H264.h"
@ -148,9 +144,7 @@ void DevChannel::inputAAC(const char *pcDataWithoutAdts,int iDataLen, uint32_t u
}
}
void DevChannel::inputG711(const char* pcData, int iDataLen, uint32_t uiStamp)
{
void DevChannel::inputG711(const char* pcData, int iDataLen, uint32_t uiStamp){
if (uiStamp == 0) {
uiStamp = (uint32_t)_aTicker[1].elapsedTime();
}
@ -159,35 +153,29 @@ void DevChannel::inputG711(const char* pcData, int iDataLen, uint32_t uiStamp)
void DevChannel::initVideo(const VideoInfo& info) {
_video = std::make_shared<VideoInfo>(info);
addTrack(std::make_shared<H264Track>());
switch (info.codecId){
case CodecH265 : addTrack(std::make_shared<H265Track>()); break;
case CodecH264 : addTrack(std::make_shared<H264Track>()); break;
default: WarnL << "不支持该类型的视频编码类型:" << info.codecId; break;
}
}
void DevChannel::initH265Video(const VideoInfo &info){
_video = std::make_shared<VideoInfo>(info);
addTrack(std::make_shared<H265Track>());
}
void DevChannel::initAudio(const AudioInfo& info) {
_audio = std::make_shared<AudioInfo>(info);
if (info.codecId == CodecAAC)
{
addTrack(std::make_shared<AACTrack>());
static void makeAdtsHeader(int profile, int sampleRate, int channels,uint8_t *out){
AACFrame adtsHeader;
adtsHeader.syncword = 0x0FFF;
adtsHeader.id = 0;
adtsHeader.layer = 0;
adtsHeader.protection_absent = 1;
adtsHeader.profile = info.iProfile;//audioObjectType - 1;
adtsHeader.profile = profile;//audioObjectType - 1;
int i = 0;
for (auto rate : samplingFrequencyTable) {
if (rate == info.iSampleRate) {
if (rate == sampleRate) {
adtsHeader.sf_index = i;
};
++i;
}
adtsHeader.private_bit = 0;
adtsHeader.channel_configuration = info.iChannel;
adtsHeader.channel_configuration = channels;
adtsHeader.original = 0;
adtsHeader.home = 0;
adtsHeader.copyright_identification_bit = 0;
@ -195,11 +183,21 @@ void DevChannel::initAudio(const AudioInfo& info) {
adtsHeader.aac_frame_length = 7;
adtsHeader.adts_buffer_fullness = 2047;
adtsHeader.no_raw_data_blocks_in_frame = 0;
writeAdtsHeader(adtsHeader, _adtsHeader);
writeAdtsHeader(adtsHeader, out);
}
else if (info.codecId == CodecG711A || info.codecId == CodecG711U)
{
addTrack(std::make_shared<G711Track>(info.codecId, info.iSampleBit, info.iSampleRate));
void DevChannel::initAudio(const AudioInfo& info) {
_audio = std::make_shared<AudioInfo>(info);
switch (info.codecId) {
case CodecAAC : {
addTrack(std::make_shared<AACTrack>());
makeAdtsHeader(info.iProfile, info.iSampleBit, info.iChannel, _adtsHeader);
break;
}
case CodecG711A :
case CodecG711U : addTrack(std::make_shared<G711Track>(info.codecId)); break;
default: WarnL << "不支持该类型的音频编码类型:" << info.codecId; break;
}
}

View File

@ -35,13 +35,14 @@ namespace mediakit {
class VideoInfo {
public:
CodecId codecId = CodecH264;
int iWidth;
int iHeight;
float iFrameRate;
};
class AudioInfo {
public:
CodecId codecId;
CodecId codecId = CodecAAC;
int iChannel;
int iSampleBit;
int iSampleRate;
@ -69,20 +70,14 @@ public:
/**
* h264视频Track
* MultiMediaSourceMuxer::addTrack(H264Track::Ptr );
* @param info
* @param info
*/
void initVideo(const VideoInfo &info);
/**
* h265视频Track
* @param info
*/
void initH265Video(const VideoInfo &info);
/**
* aac音频Track
* MultiMediaSourceMuxer::addTrack(AACTrack::Ptr );
* @param info
* @param info
*/
void initAudio(const AudioInfo &info);

View File

@ -34,23 +34,16 @@ void MediaSink::addTrack(const Track::Ptr &track_in) {
track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
if (_allTrackReady) {
onTrackFrame(frame);
return;
}
else
{
if (frame->getTrackType() == TrackVideo)
{
//还有track未准备好如果是视频的话如果直接丢帧可能导致丢失I帧
checkTrackIfReady(nullptr);
if (_allTrackReady) {
//运行至这里说明Track状态由未就绪切换为已就绪状态,那么这帧就不应该丢弃
onTrackFrame(frame);
}
else
{
ErrorL << " 还有track未准备好丢帧 codecName: " << frame->getCodecName();
}
}else
ErrorL << " 还有track未准备好丢帧 codecName: " << frame->getCodecName();
} else {
ErrorL << "some track is unreadydrop frame of: " << frame->getCodecName();
}
}));
}
@ -140,7 +133,7 @@ void MediaSink::emitAllTrackReady() {
//移除未准备好的Track
for (auto it = _track_map.begin(); it != _track_map.end();) {
if (!it->second->ready()) {
WarnL << "该track长时间未被初始化,已忽略:" << it->second->getCodecName();
WarnL << "track not ready for a long time, ignored: " << it->second->getCodecName();
it = _track_map.erase(it);
continue;
}

View File

@ -98,10 +98,9 @@ void getAACInfo(const AACFrame &adts,int &iSampleRate,int &iChannel){
iChannel = adts.channel_configuration;
}
Sdp::Ptr AACTrack::getSdp() {
if(!ready()){
WarnL << "AAC Track未准备好";
WarnL << getCodecName() << " Track未准备好";
return nullptr;
}
return std::make_shared<AACSdp>(getAacCfg(),getAudioSampleRate());

View File

@ -102,10 +102,12 @@ void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
rtmpPkt->strBuf.clear();
//////////header
//header
uint8_t is_config = false;
rtmpPkt->strBuf.push_back(_ui8AudioFlags);
rtmpPkt->strBuf.push_back(!is_config);
//aac data
rtmpPkt->strBuf.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
rtmpPkt->bodySize = rtmpPkt->strBuf.size();
@ -150,10 +152,11 @@ void AACRtmpEncoder::makeAudioConfigPkt() {
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
rtmpPkt->strBuf.clear();
//////////header
//header
uint8_t is_config = true;
rtmpPkt->strBuf.push_back(_ui8AudioFlags);
rtmpPkt->strBuf.push_back(!is_config);
//aac config
rtmpPkt->strBuf.append(_aac_cfg);
rtmpPkt->bodySize = rtmpPkt->strBuf.size();

View File

@ -88,7 +88,6 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) {
return nullptr;
}
Track::Ptr Factory::getTrackByCodecId(CodecId codecId) {
switch (codecId){
case CodecH264:{
@ -169,25 +168,7 @@ RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) {
/////////////////////////////rtmp相关///////////////////////////////////////////
Track::Ptr Factory::getVideoTrackByAmf(const AMFValue &amf) {
CodecId codecId = getCodecIdByAmf(amf);
if(codecId == CodecInvalid){
return nullptr;
}
return getTrackByCodecId(codecId);
}
mediakit::Track::Ptr Factory::getAudioTrackByAmf(const AMFValue& amf)
{
CodecId codecId = getAudioCodecIdByAmf(amf);
if (codecId == CodecInvalid) {
return nullptr;
}
return getTrackByCodecId(codecId);
}
CodecId Factory::getCodecIdByAmf(const AMFValue &val){
static CodecId getVideoCodecIdByAmf(const AMFValue &val){
if (val.type() == AMF_STRING){
auto str = val.as_string();
if(str == "avc1"){
@ -213,16 +194,20 @@ CodecId Factory::getCodecIdByAmf(const AMFValue &val){
WarnL << "暂不支持该Amf:" << type_id;
return CodecInvalid;
}
}else{
WarnL << "Metadata不存在相应的Track";
}
return CodecInvalid;
}
Track::Ptr Factory::getVideoTrackByAmf(const AMFValue &amf) {
CodecId codecId = getVideoCodecIdByAmf(amf);
if(codecId == CodecInvalid){
return nullptr;
}
return getTrackByCodecId(codecId);
}
CodecId Factory::getAudioCodecIdByAmf(const AMFValue& val)
{
static CodecId getAudioCodecIdByAmf(const AMFValue &val) {
if (val.type() == AMF_STRING) {
auto str = val.as_string();
if (str == "mp4a") {
@ -238,19 +223,21 @@ CodecId Factory::getAudioCodecIdByAmf(const AMFValue& val)
case FLV_CODEC_AAC : return CodecAAC;
case FLV_CODEC_G711A : return CodecG711A;
case FLV_CODEC_G711U : return CodecG711U;
default:
WarnL << "暂不支持该Amf:" << type_id;
return CodecInvalid;
default : WarnL << "暂不支持该Amf:" << type_id; return CodecInvalid;
}
}
else {
WarnL << "Metadata不存在相应的Track";
}
return CodecInvalid;
}
Track::Ptr Factory::getAudioTrackByAmf(const AMFValue& amf){
CodecId codecId = getAudioCodecIdByAmf(amf);
if (codecId == CodecInvalid) {
return nullptr;
}
return getTrackByCodecId(codecId);
}
RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track) {
switch (track->getCodecId()){
case CodecH264:
@ -270,6 +257,7 @@ RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track) {
AMFValue Factory::getAmfByCodecId(CodecId codecId) {
switch (codecId){
//此处用string标明rtmp编码类型目的是为了兼容某些android系统
case CodecAAC: return AMFValue("mp4a");
case CodecH264: return AMFValue("avc1");
case CodecH265: return AMFValue(FLV_CODEC_H265);
@ -279,6 +267,5 @@ AMFValue Factory::getAmfByCodecId(CodecId codecId) {
}
}
}//namespace mediakit

View File

@ -28,7 +28,6 @@ public:
/**
* CodecId获取TrackTrack的ready()false
* @param codecId id
* @return
*/
static Track::Ptr getTrackByCodecId(CodecId codecId);
@ -41,14 +40,11 @@ public:
/**
* sdp生成rtp编码器
* @param sdp sdp对象
* @return
*/
static RtpCodec::Ptr getRtpEncoderBySdp(const Sdp::Ptr &sdp);
/**
* Track生成Rtp解包器
* @param track
* @return
*/
static RtpCodec::Ptr getRtpDecoderByTrack(const Track::Ptr &track);
@ -58,43 +54,23 @@ public:
/**
* amf对象获取视频相应的Track
* @param amf rtmp metadata中的videocodecid的值
* @return
*/
static Track::Ptr getVideoTrackByAmf(const AMFValue &amf);
/**
* amf对象获取音频相应的Track
* @param amf rtmp metadata中的audiocodecid的值
* @return
*/
static Track::Ptr getAudioTrackByAmf(const AMFValue& amf);
/**
* amf对象获取相应的CodecId
* @param val rtmp metadata中的videocodecid或audiocodecid的值
* @return
*/
static CodecId getCodecIdByAmf(const AMFValue &val);
/**
* amf对象获取音频相应的CodecId
* @param val rtmp metadata中的audiocodecid的值
* @return
*/
static CodecId getAudioCodecIdByAmf(const AMFValue& val);
/**
* Track获取Rtmp的编解码器
* @param track
* @return
*/
static RtmpCodec::Ptr getRtmpCodecByTrack(const Track::Ptr &track);
/**
* codecId获取rtmp的codec描述
* @param codecId
* @return
*/
static AMFValue getAmfByCodecId(CodecId codecId);
};

View File

@ -18,7 +18,7 @@ Sdp::Ptr G711Track::getSdp() {
WarnL << getCodecName() << " Track未准备好";
return nullptr;
}
return std::make_shared<G711Sdp>(getCodecId(), getAudioSampleRate(), getCodecId() == CodecG711A ? 8 : 0, getAudioSampleBit());
return std::make_shared<G711Sdp>(getCodecId());
}
}//namespace mediakit

View File

@ -16,40 +16,27 @@
namespace mediakit{
class G711Frame;
unsigned const samplingFrequencyTableG711[16] = { 96000, 88200,
64000, 48000,
44100, 32000,
24000, 22050,
16000, 12000,
11025, 8000,
7350, 0, 0, 0 };
void makeAdtsHeader(const string &strAudioCfg,G711Frame &adts);
void writeAdtsHeader(const G711Frame &adts, uint8_t *pcAdts) ;
string makeG711AdtsConfig(const uint8_t *pcAdts);
void getAACInfo(const G711Frame &adts,int &iSampleRate,int &iChannel);
/**
* aac帧adts头
* G711帧
*/
class G711Frame : public Frame {
public:
typedef std::shared_ptr<G711Frame> Ptr;
char *data() const override{
return (char *)buffer;
return (char *)buffer.data();
}
uint32_t size() const override {
return frameLength;
return buffer.size();
}
uint32_t dts() const override {
return timeStamp;
}
uint32_t prefixSize() const override{
return iPrefixSize;
return 0;
}
TrackType getTrackType() const override{
@ -69,17 +56,15 @@ public:
}
public:
CodecId _codecId = CodecG711A;
unsigned int frameLength; // 一个帧的长度包括 raw data block
unsigned char buffer[2 * 1024 + 7];
string buffer;
uint32_t timeStamp;
uint32_t iPrefixSize = 0;
} ;
class G711FrameNoCacheAble : public FrameNoCacheAble {
public:
typedef std::shared_ptr<G711FrameNoCacheAble> Ptr;
G711FrameNoCacheAble(CodecId codecId, char *ptr,uint32_t size,uint32_t dts,int prefixeSize = 7){
G711FrameNoCacheAble(CodecId codecId, char *ptr,uint32_t size,uint32_t dts,int prefixeSize = 0){
_codecId = codecId;
_ptr = ptr;
_size = size;
@ -107,89 +92,56 @@ private:
CodecId _codecId;
};
/**
* g711音频通道
* G711音频通道
*/
class G711Track : public AudioTrack{
public:
typedef std::shared_ptr<G711Track> Ptr;
/**
* adts头信息
* inputFrame中获取adts头信息
*/
G711Track(){}
/**
* G711A G711U
*/
G711Track(CodecId codecId, int sampleBit = 16, int sampleRate = 8000){
G711Track(CodecId codecId){
_codecid = codecId;
_sampleBit = sampleBit;
_sampleRate = sampleRate;
onReady();
}
/**
*
* @return
*/
CodecId getCodecId() const override{
return _codecid;
}
/**
* aac_cfg前是无效的Track
* @return
* G711的Track不需要初始化
*/
bool ready() override {
return true;
}
/**
*
* @return
*/
int getAudioSampleRate() const override{
return _sampleRate;
}
/**
* 168
* @return
*/
int getAudioSampleBit() const override{
return _sampleBit;
}
/**
*
* @return
*/
int getAudioChannel() const override{
return _channel;
return 8000;
}
/**
* ,aac_cfg
* @param frame
* 168
*/
void inputFrame(const Frame::Ptr &frame) override{
AudioTrack::inputFrame(frame);
int getAudioSampleBit() const override{
return 16;
}
private:
/**
*
*
*/
void onReady(){
/*
if(_cfg.size() < 2){
return;
}
G711Frame aacFrame;
makeAdtsHeader(_cfg,aacFrame);
getAACInfo(aacFrame,_sampleRate,_channel);*/
int getAudioChannel() const override{
return 1;
}
private:
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
}
@ -197,34 +149,22 @@ private:
//生成sdp
Sdp::Ptr getSdp() override ;
private:
string _cfg;
CodecId _codecid = CodecG711A;
int _sampleRate = 8000;
int _sampleBit = 16;
int _channel = 1;
};
/**
* aac类型SDP
* G711类型SDP
*/
class G711Sdp : public Sdp {
public:
/**
*
* @param aac_codecId G711A G711U
* @param sample_rate
* @param playload_type rtp playload type 0G711U 8G711A
* @param bitrate
* G711采样率固定为8000
* @param codecId G711A G711U
*/
G711Sdp(CodecId codecId,
int sample_rate,
int playload_type = 0,
int bitrate = 128) : Sdp(sample_rate,playload_type), _codecId(codecId){
_printer << "m=audio 0 RTP/AVP " << playload_type << "\r\n";
//_printer << "b=AS:" << bitrate << "\r\n";
_printer << "a=rtpmap:" << playload_type << (codecId == CodecG711A ? " PCMA/" : " PCMU/") << sample_rate << "\r\n";
G711Sdp(CodecId codecId) : Sdp(8000,payloadType(codecId)), _codecId(codecId){
int pt = payloadType(codecId);
_printer << "m=audio 0 RTP/AVP " << pt << "\r\n";
_printer << "a=rtpmap:" << pt << (codecId == CodecG711A ? " PCMA/" : " PCMU/") << 8000 << "\r\n";
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
}
@ -235,12 +175,22 @@ public:
TrackType getTrackType() const override {
return TrackAudio;
}
CodecId getCodecId() const override {
return _codecId;
}
int payloadType(CodecId codecId){
switch (codecId){
case CodecG711A : return 8;
case CodecG711U : return 0;
default : return -1;
}
}
private:
_StrPrinter _printer;
CodecId _codecId = CodecG711A;
CodecId _codecId;
};
}//namespace mediakit

View File

@ -12,57 +12,90 @@
namespace mediakit{
G711RtmpDecoder::G711RtmpDecoder() {
_adts = obtainFrame();
G711RtmpDecoder::G711RtmpDecoder(CodecId codecId) {
_frame = obtainFrame();
_codecId = codecId;
}
G711Frame::Ptr G711RtmpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = ResourcePoolHelper<G711Frame>::obtainObj();
frame->frameLength = 0;
frame->iPrefixSize = 0;
frame->buffer.clear();
frame->_codecId = _codecId;
return frame;
}
bool G711RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool key_pos) {
onGetG711(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp);
bool G711RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool) {
//拷贝G711负载
_frame->buffer.assign(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2);
_frame->timeStamp = pkt->timeStamp;
//写入环形缓存
RtmpCodec::inputFrame(_frame);
_frame = obtainFrame();
return false;
}
void G711RtmpDecoder::onGetG711(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
if(iLen + 7 > sizeof(_adts->buffer)){
WarnL << "Illegal adts data, exceeding the length limit.";
/////////////////////////////////////////////////////////////////////////////////////
G711RtmpEncoder::G711RtmpEncoder(const Track::Ptr &track) : G711RtmpDecoder(track->getCodecId()) {
auto g711_track = dynamic_pointer_cast<AudioTrack>(track);
if(!g711_track){
WarnL << "无效的G711 track, 将忽略打包为RTMP";
return;
}
//拷贝aac负载
memcpy(_adts->buffer, pcData, iLen);
_adts->frameLength = iLen;
_adts->timeStamp = ui32TimeStamp;
//写入环形缓存
RtmpCodec::inputFrame(_adts);
_adts = obtainFrame();
auto iSampleRate = g711_track->getAudioSampleRate() ;
auto iChannel = g711_track->getAudioChannel();
auto iSampleBit = g711_track->getAudioSampleBit();
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 ;
switch (g711_track->getCodecId()){
case CodecG711A : flvAudioType = FLV_CODEC_G711A; break;
case CodecG711U : flvAudioType = FLV_CODEC_G711U; break;
default: WarnL << "无效的G711 track, 将忽略打包为RTMP"; return ;
}
/////////////////////////////////////////////////////////////////////////////////////
G711RtmpEncoder::G711RtmpEncoder(const Track::Ptr &track) {
_track = dynamic_pointer_cast<G711Track>(track);
_g711_flags = (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono;
}
void G711RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
if(!_g711_flags){
return;
}
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
rtmpPkt->strBuf.clear();
rtmpPkt->strBuf.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
//header
uint8_t is_config = false;
rtmpPkt->strBuf.push_back(_g711_flags);
rtmpPkt->strBuf.push_back(!is_config);
//g711 data
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->dts();
rtmpPkt->typeId = MSG_AUDIO;
RtmpCodec::inputRtmp(rtmpPkt, false);
}
}//namespace mediakit

View File

@ -17,13 +17,13 @@
namespace mediakit{
/**
* G711 Rtmp转adts
* G711 Rtmp转G711 Frame
*/
class G711RtmpDecoder : public RtmpCodec , public ResourcePoolHelper<G711Frame> {
public:
typedef std::shared_ptr<G711RtmpDecoder> Ptr;
G711RtmpDecoder();
G711RtmpDecoder(CodecId codecId);
~G711RtmpDecoder() {}
/**
@ -37,48 +37,32 @@ public:
return TrackAudio;
}
void setCodecId(CodecId codecId)
{
_codecid = codecId;
}
CodecId getCodecId() const override{
return _codecid;
return _codecId;
}
protected:
void onGetG711(const char* pcData, int iLen, uint32_t ui32TimeStamp);
private:
G711Frame::Ptr obtainFrame();
protected:
G711Frame::Ptr _adts;
CodecId _codecid = CodecInvalid;
private:
G711Frame::Ptr _frame;
CodecId _codecId;
};
/**
* aac adts转Rtmp
* G711 RTMP打包
*/
class G711RtmpEncoder : public G711RtmpDecoder , public ResourcePoolHelper<RtmpPacket> {
public:
typedef std::shared_ptr<G711RtmpEncoder> Ptr;
/**
* track可以为空inputFrame时输入adts头
* track不为空且包含adts头相关信息
* inputFrame时可以不输入adts头
* @param track
*/
G711RtmpEncoder(const Track::Ptr &track);
~G711RtmpEncoder() {}
/**
* aac adts头
* @param frame aac数据
* G711
*/
void inputFrame(const Frame::Ptr &frame) override;
private:
G711Track::Ptr _track;
uint8_t _g711_flags = 0;
};
}//namespace mediakit

View File

@ -12,6 +12,53 @@
namespace mediakit{
G711RtpDecoder::G711RtpDecoder(const Track::Ptr &track){
_codecid = track->getCodecId();
_frame = obtainFrame();
}
G711Frame::Ptr G711RtpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = ResourcePoolHelper<G711Frame>::obtainObj();
frame->buffer.clear();
frame->_codecId = _codecid;
frame->timeStamp = 0;
return frame;
}
bool G711RtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool) {
// 获取rtp数据长度
int length = rtppack->size() - rtppack->offset;
// 获取rtp数据
const char *rtp_packet_buf = rtppack->data() + rtppack->offset;
if (rtppack->timeStamp != _frame->timeStamp) {
//时间戳变更,清空上一帧
onGetG711(_frame);
}
//追加数据
_frame->buffer.append(rtp_packet_buf, length);
//赋值时间戳
_frame->timeStamp = rtppack->timeStamp;
if (rtppack->mark || _frame->buffer.size() > 10 * 1024) {
//标记为mark时或者内存快溢出时我们认为这是该帧最后一个包
onGetG711(_frame);
}
return false;
}
void G711RtpDecoder::onGetG711(const G711Frame::Ptr &frame) {
if(!frame->buffer.empty()){
//写入环形缓存
RtpCodec::inputFrame(frame);
_frame = obtainFrame();
}
}
/////////////////////////////////////////////////////////////////////////////////////
G711RtpEncoder::G711RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
@ -48,53 +95,6 @@ void G711RtpEncoder::makeG711Rtp(const void *data, unsigned int len, bool mark,
RtpCodec::inputRtp(makeRtp(getTrackType(), data, len, mark, uiStamp), false);
}
/////////////////////////////////////////////////////////////////////////////////////
G711RtpDecoder::G711RtpDecoder(const Track::Ptr &track){
auto g711Track = dynamic_pointer_cast<G711Track>(track);
_codecid = g711Track->getCodecId();
if(!g711Track || !g711Track->ready()){
WarnL << "该g711 track无效!";
}else{
}
_adts = obtainFrame();
}
G711RtpDecoder::G711RtpDecoder() {
_adts = obtainFrame();
}
G711Frame::Ptr G711RtpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = ResourcePoolHelper<G711Frame>::obtainObj();
frame->frameLength = 0;
frame->iPrefixSize = 0;
return frame;
}
bool G711RtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
// 获取rtp数据长度
int length = rtppack->size() - rtppack->offset;
// 获取rtp数据
const uint8_t *rtp_packet_buf = (uint8_t *)rtppack->data() + rtppack->offset;
_adts->frameLength = length;
memcpy(_adts->buffer, rtp_packet_buf, length);
_adts->_codecId = _codecid;
if (rtppack->mark == true) {
_adts->timeStamp = rtppack->timeStamp;
onGetG711(_adts);
}
return false;
}
void G711RtpDecoder::onGetG711(const G711Frame::Ptr &frame) {
//写入环形缓存
RtpCodec::inputFrame(frame);
_adts = obtainFrame();
}
}//namespace mediakit

View File

@ -10,12 +10,12 @@
#ifndef ZLMEDIAKIT_G711RTPCODEC_H
#define ZLMEDIAKIT_G711RTPCODEC_H
#include "Rtsp/RtpCodec.h"
#include "Extension/G711.h"
namespace mediakit{
/**
* G711 rtp转adts
* rtp转G711类
*/
class G711RtpDecoder : public RtpCodec , public ResourcePoolHelper<G711Frame> {
public:
@ -34,19 +34,22 @@ public:
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return _codecid;
}
protected:
G711RtpDecoder();
G711RtpDecoder() {}
private:
void onGetG711(const G711Frame::Ptr &frame);
G711Frame::Ptr obtainFrame();
private:
G711Frame::Ptr _adts;
CodecId _codecid = CodecInvalid;
};
private:
G711Frame::Ptr _frame;
CodecId _codecid;
};
/**
* g711 rtp类
@ -75,8 +78,6 @@ public:
void inputFrame(const Frame::Ptr &frame) override;
private:
void makeG711Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
private:
unsigned char _aucSectionBuf[1600];
};
}//namespace mediakit

View File

@ -62,7 +62,7 @@ void splitH264(const char *ptr, int len, const std::function<void(const char *,
Sdp::Ptr H264Track::getSdp() {
if(!ready()){
WarnL << "H264 Track未准备好";
WarnL << getCodecName() << " Track未准备好";
return nullptr;
}
return std::make_shared<H264Sdp>(getSps(),getPps());

View File

@ -52,7 +52,7 @@ bool getHEVCInfo(const string &strVps, const string &strSps, int &iVideoWidth, i
Sdp::Ptr H265Track::getSdp() {
if(!ready()){
WarnL << "H265 Track未准备好";
WarnL << getCodecName() << " Track未准备好";
return nullptr;
}
return std::make_shared<H265Sdp>(getVps(),getSps(),getPps());

View File

@ -209,8 +209,6 @@ public:
}
};
/**
* rtmp metadata基类rtmp格式信息
*/