支持自动生成adts头

This commit is contained in:
xiongziliang 2018-08-29 11:19:00 +08:00
parent d731454531
commit 370d31121c
7 changed files with 64 additions and 28 deletions

View File

@ -49,8 +49,8 @@ static onceToken s_token([](){
//////////////////////////Rtsp media///////////////////////////
API_EXPORT MediaContext API_CALL createMedia(const char *appName,const char *mediaName) {
DevChannel::Ptr ret(new DevChannel(DEFAULT_VHOST,appName,mediaName));
API_EXPORT MediaContext API_CALL createMedia(const char *appName,const char *mediaName , int bEanbleHls, int bEnableMp4) {
DevChannel::Ptr ret(new DevChannel(DEFAULT_VHOST,appName,mediaName,0,bEanbleHls,bEnableMp4));
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
s_mapMedia.emplace((void *) (ret.get()), ret);
return ret.get();
@ -68,12 +68,13 @@ API_EXPORT void API_CALL media_initVideo(MediaContext ctx, int width, int height
info.iHeight = height;
ptr->initVideo(info);
}
API_EXPORT void API_CALL media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate) {
API_EXPORT void API_CALL media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate,int profile) {
DevChannel *ptr = (DevChannel *) ctx;
AudioInfo info;
info.iSampleRate = sampleRate;
info.iChannel = channel;
info.iSampleBit = sampleBit;
info.iProfile = profile;
ptr->initAudio(info);
}
API_EXPORT void API_CALL media_inputH264(MediaContext ctx, void *data, int len, unsigned long stamp) {
@ -81,10 +82,10 @@ API_EXPORT void API_CALL media_inputH264(MediaContext ctx, void *data, int len,
DevChannel *ptr = (DevChannel *) ctx;
ptr->inputH264((char *) data, len, stamp);
}
API_EXPORT void API_CALL media_inputAAC(MediaContext ctx, void *data, int len,unsigned long stamp) {
API_EXPORT void API_CALL media_inputAAC(MediaContext ctx, void *data, int len,unsigned long stamp,int withAdtsHeader) {
//TimeTicker();
DevChannel *ptr = (DevChannel *) ctx;
ptr->inputAAC((char *) data, len, stamp);
ptr->inputAAC((char *) data, len, stamp,withAdtsHeader);
}
API_EXPORT void API_CALL media_inputAAC1(MediaContext ctx, void *data, int len, unsigned long stamp,void *adts){

View File

@ -39,10 +39,10 @@ extern "C" {
typedef void* MediaContext;
/*
* :
* :mediaName:,url地址的一部分
* :mediaName:,url地址的一部分,bEanbleHls:hlsbEnableMp4:mp4
* :
*/
API_EXPORT MediaContext API_CALL createMedia(const char *appName,const char *mediaName);
API_EXPORT MediaContext API_CALL createMedia(const char *appName,const char *mediaName,int bEanbleHls, int bEnableMp4);
/*
* :
@ -60,10 +60,10 @@ API_EXPORT void API_CALL media_initVideo(MediaContext ctx, int width, int height
/*
* :
* :ctx:;channel:;sampleBit:,16bit;sampleRate:
* :ctx:;channel:;sampleBit:,16bit;sampleRate:;profile:aac编码profileadts头时用于生产adts头
* :
*/
API_EXPORT void API_CALL media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate);
API_EXPORT void API_CALL media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate , int profile);
/*
* :H264视频SPS和PPS帧,00 00 01,00 00 00 01
@ -74,10 +74,10 @@ API_EXPORT void API_CALL media_inputH264(MediaContext ctx, void *data, int len,
/*
* :AAC音频(adts头)
* :ctx:;data:AAC数据;len:AAC数据字节数;stamp:
* :ctx:;data:AAC数据;len:AAC数据字节数;stamp: ,withAdtsHeader:adts头
* :
*/
API_EXPORT void API_CALL media_inputAAC(MediaContext ctx, void *data, int len, unsigned long stamp);
API_EXPORT void API_CALL media_inputAAC(MediaContext ctx, void *data, int len, unsigned long stamp,int withAdtsHeader);
/*

View File

@ -100,7 +100,7 @@ void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
}
#endif //ENABLE_FAAC
void DevChannel::inputH264(char* pcData, int iDataLen, uint32_t uiStamp) {
void DevChannel::inputH264(const char* pcData, int iDataLen, uint32_t uiStamp) {
if (!m_pRtpMaker_h264) {
uint32_t ui32Ssrc;
memcpy(&ui32Ssrc, makeRandStr(4, false).data(), 4);
@ -123,10 +123,16 @@ void DevChannel::inputH264(char* pcData, int iDataLen, uint32_t uiStamp) {
m_pRtpMaker_h264->makeRtp(pcData + iOffset, iDataLen - iOffset, uiStamp);
}
void DevChannel::inputAAC(char* pcData, int iDataLen, uint32_t uiStamp) {
inputAAC(pcData+7,iDataLen-7,uiStamp,pcData);
void DevChannel::inputAAC(const char* pcData, int iDataLen, uint32_t uiStamp,bool withAdtsHeader) {
if(withAdtsHeader){
inputAAC(pcData+7,iDataLen-7,uiStamp,pcData);
} else if(m_pAdtsHeader){
m_pAdtsHeader->aac_frame_length = iDataLen;
writeAdtsHeader(*m_pAdtsHeader,m_pAdtsHeader->data);
inputAAC(pcData,iDataLen,uiStamp,(const char *)m_pAdtsHeader->data);
}
}
void DevChannel::inputAAC(char *pcDataWithoutAdts,int iDataLen, uint32_t uiStamp,char *pcAdtsHeader){
void DevChannel::inputAAC(const char *pcDataWithoutAdts,int iDataLen, uint32_t uiStamp,const char *pcAdtsHeader){
if (!m_pRtpMaker_aac) {
uint32_t ssrc;
memcpy(&ssrc, makeRandStr(8, false).data() + 4, 4);
@ -273,6 +279,33 @@ void DevChannel::initVideo(const VideoInfo& info) {
void DevChannel::initAudio(const AudioInfo& info) {
m_audio.reset(new AudioInfo(info));
m_pAdtsHeader.reset((AdtsFrame *)malloc(sizeof(AdtsFrame) - sizeof(AdtsFrame::data) + 7),[](AdtsFrame *ptr){
free(ptr);
});
m_pAdtsHeader->syncword = 0x0FFF;
m_pAdtsHeader->id = 0;
m_pAdtsHeader->layer = 0;
m_pAdtsHeader->protection_absent = 1;
m_pAdtsHeader->profile = info.iProfile;//audioObjectType - 1;
int i = 0;
for(auto rate : samplingFrequencyTable){
if(rate == info.iSampleRate){
m_pAdtsHeader->sf_index = i;
};
++i;
}
m_pAdtsHeader->private_bit = 0;
m_pAdtsHeader->channel_configuration = info.iChannel;
m_pAdtsHeader->original = 0;
m_pAdtsHeader->home = 0;
m_pAdtsHeader->copyright_identification_bit = 0;
m_pAdtsHeader->copyright_identification_start = 0;
m_pAdtsHeader->aac_frame_length = 7;
m_pAdtsHeader->adts_buffer_fullness = 2047;
m_pAdtsHeader->no_raw_data_blocks_in_frame = 0;
}
} /* namespace DEV */
} /* namespace ZL */

View File

@ -31,6 +31,7 @@
#include <string>
#include <functional>
#include "Util/util.h"
#include "Player/Player.h"
#include "RTP/RtpMakerAAC.h"
#include "RTP/RtpMakerH264.h"
#include "Rtsp/RtspToRtmpMediaSource.h"
@ -65,6 +66,7 @@ public:
int iChannel;
int iSampleBit;
int iSampleRate;
int iProfile;
};
class DevChannel : public RtspToRtmpMediaSource{
@ -82,9 +84,9 @@ public:
void initVideo(const VideoInfo &info);
void initAudio(const AudioInfo &info);
void inputH264(char *pcData, int iDataLen, uint32_t uiStamp);
void inputAAC(char *pcDataWithAdts, int iDataLen, uint32_t uiStamp);
void inputAAC(char *pcDataWithoutAdts,int iDataLen, uint32_t uiStamp,char *pcAdtsHeader);
void inputH264(const char *pcData, int iDataLen, uint32_t uiStamp);
void inputAAC(const char *pcDataWithAdts, int iDataLen, uint32_t uiStamp, bool withAdtsHeader = true);
void inputAAC(const char *pcDataWithoutAdts,int iDataLen, uint32_t uiStamp,const char *pcAdtsHeader);
#ifdef ENABLE_X264
void inputYUV(char *apcYuv[3], int aiYuvLen[3], uint32_t uiStamp);
@ -117,6 +119,7 @@ private:
std::shared_ptr<VideoInfo> m_video;
std::shared_ptr<AudioInfo> m_audio;
SmoothTicker m_aTicker[2];
std::shared_ptr<AdtsFrame> m_pAdtsHeader;
};

View File

@ -208,18 +208,12 @@ inline bool HttpSession::checkLiveFlvStream(){
m_mediaInfo.m_streamid.erase(m_mediaInfo.m_streamid.size() - 4);//去除.flv后缀
auto mediaSrc = dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,m_mediaInfo.m_vhost,m_mediaInfo.m_app,m_mediaInfo.m_streamid));
if(!mediaSrc){
if(!mediaSrc || !mediaSrc->ready()){
//该rtmp源不存在
sendNotFound(true);
shutdown();
return true;
}
if(!mediaSrc->ready()){
//未准备好
sendNotFound(true);
shutdown();
return true;
}
auto onRes = [this,mediaSrc](const string &err){
bool authSuccess = err.empty();

View File

@ -31,9 +31,6 @@
using namespace ZL::Util;
static unsigned const samplingFrequencyTable[16] = { 96000, 88200,
64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025,
8000, 7350, 0, 0, 0 };
void writeAdtsHeader(const AdtsFrame &hed, uint8_t *pcAdts) {
pcAdts[0] = (hed.syncword >> 4 & 0xFF); //8bit

View File

@ -63,6 +63,14 @@ typedef struct {
uint32_t timeStamp;
} AdtsFrame;
unsigned const samplingFrequencyTable[16] = { 96000, 88200,
64000, 48000,
44100, 32000,
24000, 22050,
16000, 12000,
11025, 8000,
7350, 0, 0, 0 };
void makeAdtsHeader(const string &strAudioCfg,AdtsFrame &adts);
void writeAdtsHeader(const AdtsFrame &adts, uint8_t *pcAdts) ;
string makeAdtsConfig(const uint8_t *pcAdts);