支持自动生成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/////////////////////////// //////////////////////////Rtsp media///////////////////////////
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) {
DevChannel::Ptr ret(new DevChannel(DEFAULT_VHOST,appName,mediaName)); DevChannel::Ptr ret(new DevChannel(DEFAULT_VHOST,appName,mediaName,0,bEanbleHls,bEnableMp4));
lock_guard<recursive_mutex> lck(s_mtxMapMedia); lock_guard<recursive_mutex> lck(s_mtxMapMedia);
s_mapMedia.emplace((void *) (ret.get()), ret); s_mapMedia.emplace((void *) (ret.get()), ret);
return ret.get(); return ret.get();
@ -68,12 +68,13 @@ API_EXPORT void API_CALL media_initVideo(MediaContext ctx, int width, int height
info.iHeight = height; info.iHeight = height;
ptr->initVideo(info); 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; DevChannel *ptr = (DevChannel *) ctx;
AudioInfo info; AudioInfo info;
info.iSampleRate = sampleRate; info.iSampleRate = sampleRate;
info.iChannel = channel; info.iChannel = channel;
info.iSampleBit = sampleBit; info.iSampleBit = sampleBit;
info.iProfile = profile;
ptr->initAudio(info); ptr->initAudio(info);
} }
API_EXPORT void API_CALL media_inputH264(MediaContext ctx, void *data, int len, unsigned long stamp) { 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; DevChannel *ptr = (DevChannel *) ctx;
ptr->inputH264((char *) data, len, stamp); 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(); //TimeTicker();
DevChannel *ptr = (DevChannel *) ctx; 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){ 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; 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 * :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头) * :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 #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) { if (!m_pRtpMaker_h264) {
uint32_t ui32Ssrc; uint32_t ui32Ssrc;
memcpy(&ui32Ssrc, makeRandStr(4, false).data(), 4); 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); m_pRtpMaker_h264->makeRtp(pcData + iOffset, iDataLen - iOffset, uiStamp);
} }
void DevChannel::inputAAC(char* pcData, int iDataLen, uint32_t uiStamp) { void DevChannel::inputAAC(const char* pcData, int iDataLen, uint32_t uiStamp,bool withAdtsHeader) {
if(withAdtsHeader){
inputAAC(pcData+7,iDataLen-7,uiStamp,pcData); 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) { if (!m_pRtpMaker_aac) {
uint32_t ssrc; uint32_t ssrc;
memcpy(&ssrc, makeRandStr(8, false).data() + 4, 4); memcpy(&ssrc, makeRandStr(8, false).data() + 4, 4);
@ -273,6 +279,33 @@ void DevChannel::initVideo(const VideoInfo& info) {
void DevChannel::initAudio(const AudioInfo& info) { void DevChannel::initAudio(const AudioInfo& info) {
m_audio.reset(new 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 DEV */
} /* namespace ZL */ } /* namespace ZL */

View File

@ -31,6 +31,7 @@
#include <string> #include <string>
#include <functional> #include <functional>
#include "Util/util.h" #include "Util/util.h"
#include "Player/Player.h"
#include "RTP/RtpMakerAAC.h" #include "RTP/RtpMakerAAC.h"
#include "RTP/RtpMakerH264.h" #include "RTP/RtpMakerH264.h"
#include "Rtsp/RtspToRtmpMediaSource.h" #include "Rtsp/RtspToRtmpMediaSource.h"
@ -65,6 +66,7 @@ public:
int iChannel; int iChannel;
int iSampleBit; int iSampleBit;
int iSampleRate; int iSampleRate;
int iProfile;
}; };
class DevChannel : public RtspToRtmpMediaSource{ class DevChannel : public RtspToRtmpMediaSource{
@ -82,9 +84,9 @@ public:
void initVideo(const VideoInfo &info); void initVideo(const VideoInfo &info);
void initAudio(const AudioInfo &info); void initAudio(const AudioInfo &info);
void inputH264(char *pcData, int iDataLen, uint32_t uiStamp); void inputH264(const char *pcData, int iDataLen, uint32_t uiStamp);
void inputAAC(char *pcDataWithAdts, int iDataLen, uint32_t uiStamp); void inputAAC(const char *pcDataWithAdts, int iDataLen, uint32_t uiStamp, bool withAdtsHeader = true);
void inputAAC(char *pcDataWithoutAdts,int iDataLen, uint32_t uiStamp,char *pcAdtsHeader); void inputAAC(const char *pcDataWithoutAdts,int iDataLen, uint32_t uiStamp,const char *pcAdtsHeader);
#ifdef ENABLE_X264 #ifdef ENABLE_X264
void inputYUV(char *apcYuv[3], int aiYuvLen[3], uint32_t uiStamp); 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<VideoInfo> m_video;
std::shared_ptr<AudioInfo> m_audio; std::shared_ptr<AudioInfo> m_audio;
SmoothTicker m_aTicker[2]; 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后缀 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)); 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源不存在 //该rtmp源不存在
sendNotFound(true); sendNotFound(true);
shutdown(); shutdown();
return true; return true;
} }
if(!mediaSrc->ready()){
//未准备好
sendNotFound(true);
shutdown();
return true;
}
auto onRes = [this,mediaSrc](const string &err){ auto onRes = [this,mediaSrc](const string &err){
bool authSuccess = err.empty(); bool authSuccess = err.empty();

View File

@ -31,9 +31,6 @@
using namespace ZL::Util; 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) { void writeAdtsHeader(const AdtsFrame &hed, uint8_t *pcAdts) {
pcAdts[0] = (hed.syncword >> 4 & 0xFF); //8bit pcAdts[0] = (hed.syncword >> 4 & 0xFF); //8bit

View File

@ -63,6 +63,14 @@ typedef struct {
uint32_t timeStamp; uint32_t timeStamp;
} AdtsFrame; } 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 makeAdtsHeader(const string &strAudioCfg,AdtsFrame &adts);
void writeAdtsHeader(const AdtsFrame &adts, uint8_t *pcAdts) ; void writeAdtsHeader(const AdtsFrame &adts, uint8_t *pcAdts) ;
string makeAdtsConfig(const uint8_t *pcAdts); string makeAdtsConfig(const uint8_t *pcAdts);