无音频时生成静音音频,防止播放器加载画面太慢

修复MAC下SDL必须在主线程渲染的问题
This commit is contained in:
xiongziliang 2018-06-30 23:02:16 +08:00
parent ae5482ec61
commit 58b58418d6
5 changed files with 82 additions and 10 deletions

View File

@ -38,6 +38,36 @@ using namespace ZL::Thread;
namespace ZL {
namespace DEV {
static uint8_t s_mute_adts[] = {0xff, 0xf1, 0x6c, 0x40, 0x2d, 0x3f, 0xfc, 0x00, 0xe0, 0x34, 0x20, 0xad, 0xf2, 0x3f, 0xb5, 0xdd,
0x73, 0xac, 0xbd, 0xca, 0xd7, 0x7d, 0x4a, 0x13, 0x2d, 0x2e, 0xa2, 0x62, 0x02, 0x70, 0x3c, 0x1c,
0xc5, 0x63, 0x55, 0x69, 0x94, 0xb5, 0x8d, 0x70, 0xd7, 0x24, 0x6a, 0x9e, 0x2e, 0x86, 0x24, 0xea,
0x4f, 0xd4, 0xf8, 0x10, 0x53, 0xa5, 0x4a, 0xb2, 0x9a, 0xf0, 0xa1, 0x4f, 0x2f, 0x66, 0xf9, 0xd3,
0x8c, 0xa6, 0x97, 0xd5, 0x84, 0xac, 0x09, 0x25, 0x98, 0x0b, 0x1d, 0x77, 0x04, 0xb8, 0x55, 0x49,
0x85, 0x27, 0x06, 0x23, 0x58, 0xcb, 0x22, 0xc3, 0x20, 0x3a, 0x12, 0x09, 0x48, 0x24, 0x86, 0x76,
0x95, 0xe3, 0x45, 0x61, 0x43, 0x06, 0x6b, 0x4a, 0x61, 0x14, 0x24, 0xa9, 0x16, 0xe0, 0x97, 0x34,
0xb6, 0x58, 0xa4, 0x38, 0x34, 0x90, 0x19, 0x5d, 0x00, 0x19, 0x4a, 0xc2, 0x80, 0x4b, 0xdc, 0xb7,
0x00, 0x18, 0x12, 0x3d, 0xd9, 0x93, 0xee, 0x74, 0x13, 0x95, 0xad, 0x0b, 0x59, 0x51, 0x0e, 0x99,
0xdf, 0x49, 0x98, 0xde, 0xa9, 0x48, 0x4b, 0xa5, 0xfb, 0xe8, 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77, 0x92, 0x9b, 0xff, 0xc6, 0xae, 0xf8, 0x36, 0xba, 0xa8, 0xaa, 0x6b, 0x1e, 0x8c,
0xc5, 0x97, 0x39, 0x6a, 0xb8, 0xa2, 0x55, 0xa8, 0xf8};
#define MUTE_ADTS_CHN_CNT 1
#define MUTE_ADTS_SAMPLE_BIT 16
#define MUTE_ADTS_SAMPLE_RATE 8000
#define MUTE_ADTS_DATA s_mute_adts
#define MUTE_ADTS_DATA_LEN sizeof(s_mute_adts)
#define MUTE_ADTS_DATA_MS 120
PlayerProxy::PlayerProxy(const char *strVhost,
const char *strApp,
const char *strSrc,
@ -60,6 +90,9 @@ void PlayerProxy::play(const char* strUrl) {
}
if(strongSelf->m_pChn){
strongSelf->m_pChn->inputH264((char *)data.data.data(), data.data.size(), data.timeStamp);
if(!strongSelf->m_haveAudio){
strongSelf->makeMuteAudio(data.timeStamp);
}
}else{
strongSelf->initMedia();
}
@ -140,12 +173,20 @@ void PlayerProxy::initMedia() {
info.iHeight = getVideoHeight();
m_pChn->initVideo(info);
}
m_haveAudio = containAudio();
if (containAudio()) {
AudioInfo info;
info.iSampleRate = getAudioSampleRate();
info.iChannel = getAudioChannel();
info.iSampleBit = getAudioSampleBit();
m_pChn->initAudio(info);
}else{
AudioInfo info;
info.iSampleRate = MUTE_ADTS_SAMPLE_RATE;
info.iChannel = MUTE_ADTS_CHN_CNT;
info.iSampleBit = MUTE_ADTS_SAMPLE_BIT;
m_pChn->initAudio(info);
}
}
bool PlayerProxy::shutDown() {
@ -161,6 +202,15 @@ bool PlayerProxy::shutDown() {
return true;
}
void PlayerProxy::makeMuteAudio(uint32_t stamp) {
auto iAudioIndex = stamp / MUTE_ADTS_DATA_MS;
if(m_iAudioIndex != iAudioIndex){
m_iAudioIndex = iAudioIndex;
m_pChn->inputAAC((char *)MUTE_ADTS_DATA,MUTE_ADTS_DATA_LEN, m_iAudioIndex * MUTE_ADTS_DATA_MS);
//DebugL << m_iAudioIndex * MUTE_ADTS_DATA_MS << " " << stamp;
}
}
} /* namespace Player */
} /* namespace Player */
} /* namespace ZL */

View File

@ -55,6 +55,10 @@ public:
void play(const char* strUrl) override;
bool shutDown() override;
private:
void initMedia();
void rePlay(const string &strUrl,int iFailedCnt);
void makeMuteAudio(uint32_t stamp);
private:
bool m_bEnableHls;
bool m_bEnableMp4;
@ -63,9 +67,8 @@ private:
string m_strVhost;
string m_strApp;
string m_strSrc;
private:
void initMedia();
void rePlay(const string &strUrl,int iFailedCnt);
bool m_haveAudio = false;
int m_iAudioIndex = 0;
};
} /* namespace Player */

View File

@ -11,7 +11,7 @@ find_package(AVUTIL QUIET)
if(AVUTIL_FOUND)
include_directories(${AVUTIL_INCLUDE_DIR})
list(APPEND LINK_LIB_LIST ${AVUTIL_LIBRARIES})
message(STATUS " found libutil")
message(STATUS "found libutil ${AVUTIL_LIBRARIES}")
endif()
#ffmpeg/libavcodec
@ -19,7 +19,7 @@ find_package(AVCODEC QUIET)
if(AVCODEC_FOUND)
include_directories(${AVCODEC_INCLUDE_DIR})
list(APPEND LINK_LIB_LIST ${AVCODEC_LIBRARIES})
message(STATUS " found libavcodec")
message(STATUS "found libavcodec ${AVCODEC_LIBRARIES}")
endif()
aux_source_directory(. TEST_SRC_LIST)

View File

@ -42,6 +42,8 @@ using namespace ZL::Util;
#define REFRESH_EVENT (SDL_USEREVENT + 1)
#ifndef __MACH__
class SDLDisplayerHelper
{
public:
@ -104,6 +106,8 @@ private:
};
#endif //__MACH__
class YuvDisplayer {
public:
YuvDisplayer(void *hwnd = nullptr,const char *title = "untitled"){
@ -121,7 +125,9 @@ public:
},nullptr);
InfoL << "SDL_Init";
}, []() {
#ifndef __MACH__
SDLDisplayerHelper::Destory();
#endif
SDL_Quit();
});

View File

@ -73,7 +73,20 @@ int main(int argc, char *argv[]) {
H264Decoder decoder;
YuvDisplayer displayer;
player->setOnVideoCB([&](const H264Frame &frame) {
#ifndef __MACH__
SDLDisplayerHelper::Instance().doTask([&, frame]() {
AVFrame *pFrame = nullptr;
bool flag = decoder.inputVideo((unsigned char *) frame.data.data(), frame.data.size(), frame.timeStamp,
&pFrame);
if (flag) {
//DebugL << pFrame->pkt_pts;
EventPoller::Instance().sync([&](){
displayer.displayYUV(pFrame);
});
}
return true;
});
#else
AVFrame *pFrame = nullptr;
bool flag = decoder.inputVideo((unsigned char *) frame.data.data(), frame.data.size(), frame.timeStamp,
&pFrame);
@ -81,8 +94,8 @@ int main(int argc, char *argv[]) {
//DebugL << pFrame->pkt_pts;
displayer.displayYUV(pFrame);
}
return true;
});
#endif
});
EventPoller::Instance().runLoop();