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音频轨道 * g711音频轨道
* @param ctx * @param ctx
* @param au 1.G711A 2.G711U * @param au 3 : G711A 4: G711U
* @param channel * @param channel
* @param sample_bit 16 * @param sample_bit 16
* @param sample_rate * @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 user_data
* @param track_type 01 * @param track_type 01
* @param codec_id 0H2641H2652AAC * @param codec_id 0H2641H2652AAC 3.G711A 4.G711U
* @param data * @param data
* @param len * @param len
* @param dts * @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 ctx
* @param cb ,null * @param cb ,null
* @param user_data * @param user_data
*/ */
API_EXPORT void API_CALL mk_player_set_on_data(mk_player ctx, on_mk_play_data cb, void *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 * codec_id -1 0H2641H2652AAC 3.G711A 4.G711U
* @param ctx * @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); assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
VideoInfo info; VideoInfo info;
info.codecId = CodecH264;
info.iFrameRate = frameRate; info.iFrameRate = frameRate;
info.iWidth = width; info.iWidth = width;
info.iHeight = height; 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); assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
VideoInfo info; VideoInfo info;
info.codecId = CodecH265;
info.iFrameRate = frameRate; info.iFrameRate = frameRate;
info.iWidth = width; info.iWidth = width;
info.iHeight = height; 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) { API_EXPORT void API_CALL mk_media_init_aac(mk_media ctx, int channel, int sample_bit, int sample_rate, int profile) {
assert(ctx); assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
AudioInfo info; AudioInfo info;
info.codecId = CodecAAC;
info.iSampleRate = sample_rate; info.iSampleRate = sample_rate;
info.iChannel = channel; info.iChannel = channel;
info.iSampleBit = sample_bit; 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); (*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(ctx);
assert(au == CodecG711A || au == CodecG711U);
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx; MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
AudioInfo info; AudioInfo info;
info.codecId = (CodecId)au;
info.iSampleRate = sample_rate; info.iSampleRate = sample_rate;
info.iChannel = 1; info.iChannel = 1;
info.iSampleBit = sample_bit; info.iSampleBit = sample_bit;
info.iProfile = 0; info.iProfile = 0;
info.codecId = (CodecId)au;
(*obj)->getChannel()->initAudio(info); (*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); (*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); assert(ctx && data && len > 0);
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx; MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
(*obj)->getChannel()->inputG711((char*)data, len, dts); (*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); assert(ctx);
MediaPlayer::Ptr& player = *((MediaPlayer::Ptr*)ctx); MediaPlayer::Ptr& player = *((MediaPlayer::Ptr*)ctx);
auto track = dynamic_pointer_cast<VideoTrack>(player->getTrack(TrackVideo)); 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; 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); assert(ctx);
MediaPlayer::Ptr& player = *((MediaPlayer::Ptr*)ctx); MediaPlayer::Ptr& player = *((MediaPlayer::Ptr*)ctx);
auto track = dynamic_pointer_cast<AudioTrack>(player->getTrack(TrackAudio)); 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. * may be found in the AUTHORS file in the root of the source tree.
*/ */
#include <stdio.h>
#include <stdio.h>
#include "Device.h" #include "Device.h"
#include "Util/logger.h" #include "Util/logger.h"
#include "Util/util.h"
#include "Util/base64.h" #include "Util/base64.h"
#include "Util/TimeTicker.h"
#include "Extension/AAC.h" #include "Extension/AAC.h"
#include "Extension/G711.h" #include "Extension/G711.h"
#include "Extension/H264.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) { if (uiStamp == 0) {
uiStamp = (uint32_t)_aTicker[1].elapsedTime(); uiStamp = (uint32_t)_aTicker[1].elapsedTime();
} }
@ -159,47 +153,51 @@ void DevChannel::inputG711(const char* pcData, int iDataLen, uint32_t uiStamp)
void DevChannel::initVideo(const VideoInfo& info) { void DevChannel::initVideo(const VideoInfo& info) {
_video = std::make_shared<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){ static void makeAdtsHeader(int profile, int sampleRate, int channels,uint8_t *out){
_video = std::make_shared<VideoInfo>(info); AACFrame adtsHeader;
addTrack(std::make_shared<H265Track>()); adtsHeader.syncword = 0x0FFF;
adtsHeader.id = 0;
adtsHeader.layer = 0;
adtsHeader.protection_absent = 1;
adtsHeader.profile = profile;//audioObjectType - 1;
int i = 0;
for (auto rate : samplingFrequencyTable) {
if (rate == sampleRate) {
adtsHeader.sf_index = i;
};
++i;
}
adtsHeader.private_bit = 0;
adtsHeader.channel_configuration = channels;
adtsHeader.original = 0;
adtsHeader.home = 0;
adtsHeader.copyright_identification_bit = 0;
adtsHeader.copyright_identification_start = 0;
adtsHeader.aac_frame_length = 7;
adtsHeader.adts_buffer_fullness = 2047;
adtsHeader.no_raw_data_blocks_in_frame = 0;
writeAdtsHeader(adtsHeader, out);
} }
void DevChannel::initAudio(const AudioInfo& info) { void DevChannel::initAudio(const AudioInfo& info) {
_audio = std::make_shared<AudioInfo>(info); _audio = std::make_shared<AudioInfo>(info);
if (info.codecId == CodecAAC) switch (info.codecId) {
{ case CodecAAC : {
addTrack(std::make_shared<AACTrack>()); addTrack(std::make_shared<AACTrack>());
makeAdtsHeader(info.iProfile, info.iSampleBit, info.iChannel, _adtsHeader);
AACFrame adtsHeader; break;
adtsHeader.syncword = 0x0FFF;
adtsHeader.id = 0;
adtsHeader.layer = 0;
adtsHeader.protection_absent = 1;
adtsHeader.profile = info.iProfile;//audioObjectType - 1;
int i = 0;
for (auto rate : samplingFrequencyTable) {
if (rate == info.iSampleRate) {
adtsHeader.sf_index = i;
};
++i;
} }
adtsHeader.private_bit = 0;
adtsHeader.channel_configuration = info.iChannel; case CodecG711A :
adtsHeader.original = 0; case CodecG711U : addTrack(std::make_shared<G711Track>(info.codecId)); break;
adtsHeader.home = 0; default: WarnL << "不支持该类型的音频编码类型:" << info.codecId; break;
adtsHeader.copyright_identification_bit = 0;
adtsHeader.copyright_identification_start = 0;
adtsHeader.aac_frame_length = 7;
adtsHeader.adts_buffer_fullness = 2047;
adtsHeader.no_raw_data_blocks_in_frame = 0;
writeAdtsHeader(adtsHeader, _adtsHeader);
}
else if (info.codecId == CodecG711A || info.codecId == CodecG711U)
{
addTrack(std::make_shared<G711Track>(info.codecId, info.iSampleBit, info.iSampleRate));
} }
} }

View File

@ -35,13 +35,14 @@ namespace mediakit {
class VideoInfo { class VideoInfo {
public: public:
CodecId codecId = CodecH264;
int iWidth; int iWidth;
int iHeight; int iHeight;
float iFrameRate; float iFrameRate;
}; };
class AudioInfo { class AudioInfo {
public: public:
CodecId codecId; CodecId codecId = CodecAAC;
int iChannel; int iChannel;
int iSampleBit; int iSampleBit;
int iSampleRate; int iSampleRate;
@ -69,20 +70,14 @@ public:
/** /**
* h264视频Track * h264视频Track
* MultiMediaSourceMuxer::addTrack(H264Track::Ptr ); * MultiMediaSourceMuxer::addTrack(H264Track::Ptr );
* @param info * @param info
*/ */
void initVideo(const VideoInfo &info); void initVideo(const VideoInfo &info);
/**
* h265视频Track
* @param info
*/
void initH265Video(const VideoInfo &info);
/** /**
* aac音频Track * aac音频Track
* MultiMediaSourceMuxer::addTrack(AACTrack::Ptr ); * MultiMediaSourceMuxer::addTrack(AACTrack::Ptr );
* @param info * @param info
*/ */
void initAudio(const AudioInfo &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) { track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
if (_allTrackReady) { if (_allTrackReady) {
onTrackFrame(frame); onTrackFrame(frame);
return;
} }
else
{
if (frame->getTrackType() == TrackVideo)
{
checkTrackIfReady(nullptr);
if (_allTrackReady) { //还有track未准备好如果是视频的话如果直接丢帧可能导致丢失I帧
onTrackFrame(frame); checkTrackIfReady(nullptr);
} if (_allTrackReady) {
else //运行至这里说明Track状态由未就绪切换为已就绪状态,那么这帧就不应该丢弃
{ onTrackFrame(frame);
ErrorL << " 还有track未准备好丢帧 codecName: " << frame->getCodecName(); } else {
} ErrorL << "some track is unreadydrop frame of: " << frame->getCodecName();
}else
ErrorL << " 还有track未准备好丢帧 codecName: " << frame->getCodecName();
} }
})); }));
} }
@ -140,7 +133,7 @@ void MediaSink::emitAllTrackReady() {
//移除未准备好的Track //移除未准备好的Track
for (auto it = _track_map.begin(); it != _track_map.end();) { for (auto it = _track_map.begin(); it != _track_map.end();) {
if (!it->second->ready()) { 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); it = _track_map.erase(it);
continue; continue;
} }

View File

@ -98,10 +98,9 @@ void getAACInfo(const AACFrame &adts,int &iSampleRate,int &iChannel){
iChannel = adts.channel_configuration; iChannel = adts.channel_configuration;
} }
Sdp::Ptr AACTrack::getSdp() { Sdp::Ptr AACTrack::getSdp() {
if(!ready()){ if(!ready()){
WarnL << "AAC Track未准备好"; WarnL << getCodecName() << " Track未准备好";
return nullptr; return nullptr;
} }
return std::make_shared<AACSdp>(getAacCfg(),getAudioSampleRate()); 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(); RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
rtmpPkt->strBuf.clear(); rtmpPkt->strBuf.clear();
//////////header //header
uint8_t is_config = false; uint8_t is_config = false;
rtmpPkt->strBuf.push_back(_ui8AudioFlags); rtmpPkt->strBuf.push_back(_ui8AudioFlags);
rtmpPkt->strBuf.push_back(!is_config); rtmpPkt->strBuf.push_back(!is_config);
//aac data
rtmpPkt->strBuf.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize()); rtmpPkt->strBuf.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
rtmpPkt->bodySize = rtmpPkt->strBuf.size(); rtmpPkt->bodySize = rtmpPkt->strBuf.size();
@ -150,10 +152,11 @@ void AACRtmpEncoder::makeAudioConfigPkt() {
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj(); RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
rtmpPkt->strBuf.clear(); rtmpPkt->strBuf.clear();
//////////header //header
uint8_t is_config = true; uint8_t is_config = true;
rtmpPkt->strBuf.push_back(_ui8AudioFlags); rtmpPkt->strBuf.push_back(_ui8AudioFlags);
rtmpPkt->strBuf.push_back(!is_config); rtmpPkt->strBuf.push_back(!is_config);
//aac config
rtmpPkt->strBuf.append(_aac_cfg); rtmpPkt->strBuf.append(_aac_cfg);
rtmpPkt->bodySize = rtmpPkt->strBuf.size(); rtmpPkt->bodySize = rtmpPkt->strBuf.size();

View File

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

View File

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

View File

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

View File

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

View File

@ -12,57 +12,90 @@
namespace mediakit{ namespace mediakit{
G711RtmpDecoder::G711RtmpDecoder() { G711RtmpDecoder::G711RtmpDecoder(CodecId codecId) {
_adts = obtainFrame(); _frame = obtainFrame();
_codecId = codecId;
} }
G711Frame::Ptr G711RtmpDecoder::obtainFrame() { G711Frame::Ptr G711RtmpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象 //从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = ResourcePoolHelper<G711Frame>::obtainObj(); auto frame = ResourcePoolHelper<G711Frame>::obtainObj();
frame->frameLength = 0; frame->buffer.clear();
frame->iPrefixSize = 0; frame->_codecId = _codecId;
return frame; return frame;
} }
bool G711RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool key_pos) { bool G711RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool) {
onGetG711(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp); //拷贝G711负载
_frame->buffer.assign(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2);
_frame->timeStamp = pkt->timeStamp;
//写入环形缓存
RtmpCodec::inputFrame(_frame);
_frame = obtainFrame();
return false; 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; return;
} }
//拷贝aac负载 auto iSampleRate = g711_track->getAudioSampleRate() ;
memcpy(_adts->buffer, pcData, iLen); auto iChannel = g711_track->getAudioChannel();
_adts->frameLength = iLen; auto iSampleBit = g711_track->getAudioSampleBit();
_adts->timeStamp = ui32TimeStamp; 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 ;
}
//写入环形缓存 _g711_flags = (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono;
RtmpCodec::inputFrame(_adts);
_adts = obtainFrame();
}
/////////////////////////////////////////////////////////////////////////////////////
G711RtmpEncoder::G711RtmpEncoder(const Track::Ptr &track) {
_track = dynamic_pointer_cast<G711Track>(track);
} }
void G711RtmpEncoder::inputFrame(const Frame::Ptr& frame) { void G711RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
if(!_g711_flags){
return;
}
RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj(); RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
rtmpPkt->strBuf.clear(); 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->bodySize = rtmpPkt->strBuf.size();
rtmpPkt->chunkId = CHUNK_AUDIO; rtmpPkt->chunkId = CHUNK_AUDIO;
rtmpPkt->streamId = STREAM_MEDIA; rtmpPkt->streamId = STREAM_MEDIA;
rtmpPkt->timeStamp = frame->dts(); rtmpPkt->timeStamp = frame->dts();
rtmpPkt->typeId = MSG_AUDIO; rtmpPkt->typeId = MSG_AUDIO;
RtmpCodec::inputRtmp(rtmpPkt, false); RtmpCodec::inputRtmp(rtmpPkt, false);
} }
}//namespace mediakit }//namespace mediakit

View File

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

View File

@ -12,16 +12,63 @@
namespace mediakit{ 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, G711RtpEncoder::G711RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize, uint32_t ui32MtuSize,
uint32_t ui32SampleRate, uint32_t ui32SampleRate,
uint8_t ui8PlayloadType, uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) : uint8_t ui8Interleaved) :
RtpInfo(ui32Ssrc, RtpInfo(ui32Ssrc,
ui32MtuSize, ui32MtuSize,
ui32SampleRate, ui32SampleRate,
ui8PlayloadType, ui8PlayloadType,
ui8Interleaved){ ui8Interleaved) {
} }
void G711RtpEncoder::inputFrame(const Frame::Ptr &frame) { void G711RtpEncoder::inputFrame(const Frame::Ptr &frame) {
@ -45,56 +92,9 @@ void G711RtpEncoder::inputFrame(const Frame::Ptr &frame) {
} }
void G711RtpEncoder::makeG711Rtp(const void *data, unsigned int len, bool mark, uint32_t uiStamp) { void G711RtpEncoder::makeG711Rtp(const void *data, unsigned int len, bool mark, uint32_t uiStamp) {
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp), false); 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 }//namespace mediakit

View File

@ -10,12 +10,12 @@
#ifndef ZLMEDIAKIT_G711RTPCODEC_H #ifndef ZLMEDIAKIT_G711RTPCODEC_H
#define ZLMEDIAKIT_G711RTPCODEC_H #define ZLMEDIAKIT_G711RTPCODEC_H
#include "Rtsp/RtpCodec.h" #include "Rtsp/RtpCodec.h"
#include "Extension/G711.h" #include "Extension/G711.h"
namespace mediakit{ namespace mediakit{
/** /**
* G711 rtp转adts * rtp转G711类
*/ */
class G711RtpDecoder : public RtpCodec , public ResourcePoolHelper<G711Frame> { class G711RtpDecoder : public RtpCodec , public ResourcePoolHelper<G711Frame> {
public: public:
@ -34,19 +34,22 @@ public:
TrackType getTrackType() const override{ TrackType getTrackType() const override{
return TrackAudio; return TrackAudio;
} }
CodecId getCodecId() const override{ CodecId getCodecId() const override{
return _codecid; return _codecid;
} }
protected: protected:
G711RtpDecoder(); G711RtpDecoder() {}
private: private:
void onGetG711(const G711Frame::Ptr &frame); void onGetG711(const G711Frame::Ptr &frame);
G711Frame::Ptr obtainFrame(); G711Frame::Ptr obtainFrame();
private:
G711Frame::Ptr _adts;
CodecId _codecid = CodecInvalid;
};
private:
G711Frame::Ptr _frame;
CodecId _codecid;
};
/** /**
* g711 rtp类 * g711 rtp类
@ -63,10 +66,10 @@ public:
* @param ui8Interleaved rtsp interleaved * @param ui8Interleaved rtsp interleaved
*/ */
G711RtpEncoder(uint32_t ui32Ssrc, G711RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize, uint32_t ui32MtuSize,
uint32_t ui32SampleRate, uint32_t ui32SampleRate,
uint8_t ui8PlayloadType = 0, uint8_t ui8PlayloadType = 0,
uint8_t ui8Interleaved = TrackAudio * 2); uint8_t ui8Interleaved = TrackAudio * 2);
~G711RtpEncoder() {} ~G711RtpEncoder() {}
/** /**
@ -75,8 +78,6 @@ public:
void inputFrame(const Frame::Ptr &frame) override; void inputFrame(const Frame::Ptr &frame) override;
private: private:
void makeG711Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp); void makeG711Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
private:
unsigned char _aucSectionBuf[1600];
}; };
}//namespace mediakit }//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() { Sdp::Ptr H264Track::getSdp() {
if(!ready()){ if(!ready()){
WarnL << "H264 Track未准备好"; WarnL << getCodecName() << " Track未准备好";
return nullptr; return nullptr;
} }
return std::make_shared<H264Sdp>(getSps(),getPps()); 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() { Sdp::Ptr H265Track::getSdp() {
if(!ready()){ if(!ready()){
WarnL << "H265 Track未准备好"; WarnL << getCodecName() << " Track未准备好";
return nullptr; return nullptr;
} }
return std::make_shared<H265Sdp>(getVps(),getSps(),getPps()); return std::make_shared<H265Sdp>(getVps(),getSps(),getPps());

View File

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