完成播放器适配

This commit is contained in:
xiongziliang 2018-10-29 13:56:10 +08:00
parent 274dab6e1d
commit 5dc1859db4
2 changed files with 59 additions and 69 deletions

View File

@ -44,7 +44,6 @@ using namespace mediakit;
#define REFRESH_EVENT (SDL_USEREVENT + 1) #define REFRESH_EVENT (SDL_USEREVENT + 1)
#ifndef __MACH__
class SDLDisplayerHelper class SDLDisplayerHelper
{ {
public: public:
@ -66,17 +65,6 @@ public:
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
private:
SDLDisplayerHelper(){
_loopThread.reset(new std::thread(&SDLDisplayerHelper::runLoop,this));
};
~SDLDisplayerHelper(){
doTask([](){return false;});
_loopThread->join();
_loopThread.reset();
};
void runLoop(){ void runLoop(){
bool flag = true; bool flag = true;
std::function<bool ()> task; std::function<bool ()> task;
@ -100,14 +88,21 @@ private:
} }
} }
void shutdown(){
doTask([](){return false;});
}
private:
SDLDisplayerHelper(){
};
~SDLDisplayerHelper(){
shutdown();
};
private:
std::deque<std::function<bool ()> > _taskList; std::deque<std::function<bool ()> > _taskList;
std::shared_ptr<thread> _loopThread;
std::mutex _mtxTask; std::mutex _mtxTask;
}; };
#endif //__MACH__
class YuvDisplayer { class YuvDisplayer {
public: public:
@ -126,9 +121,7 @@ public:
},nullptr); },nullptr);
InfoL << "SDL_Init"; InfoL << "SDL_Init";
}, []() { }, []() {
#ifndef __MACH__
SDLDisplayerHelper::Destory(); SDLDisplayerHelper::Destory();
#endif
SDL_Quit(); SDL_Quit();
}); });

View File

@ -41,10 +41,9 @@ using namespace mediakit;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
//设置退出信号处理函数 //设置退出信号处理函数
signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); }); signal(SIGINT, [](int) { SDLDisplayerHelper::Instance().shutdown(); });
//设置日志 //设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace)); Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
if (argc != 3) { if (argc != 3) {
ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n" ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n"
@ -57,46 +56,44 @@ int main(int argc, char *argv[]) {
{ {
MediaPlayer::Ptr player(new MediaPlayer()); MediaPlayer::Ptr player(new MediaPlayer());
player->setOnPlayResult([](const SockException &ex) { weak_ptr<MediaPlayer> weakPlayer = player;
player->setOnPlayResult([weakPlayer](const SockException &ex) {
InfoL << "OnPlayResult:" << ex.what(); InfoL << "OnPlayResult:" << ex.what();
auto strongPlayer = weakPlayer.lock();
if (ex || !strongPlayer) {
return;
}
auto viedoTrack = strongPlayer->getTrack(TrackVideo);
if (!viedoTrack || viedoTrack->getCodecId() != CodecH264) {
WarnL << "没有视频或者视频不是264编码!";
return;
}
SDLDisplayerHelper::Instance().doTask([viedoTrack]() {
std::shared_ptr<H264Decoder> decoder(new H264Decoder);
std::shared_ptr<YuvDisplayer> displayer(new YuvDisplayer);
viedoTrack->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([decoder, displayer](const Frame::Ptr &frame) {
SDLDisplayerHelper::Instance().doTask([decoder, displayer, frame]() {
AVFrame *pFrame = nullptr;
bool flag = decoder->inputVideo((unsigned char *) frame->data(), frame->size(),
frame->stamp(), &pFrame);
if (flag) {
displayer->displayYUV(pFrame);
}
return true;
}); });
}));
return true;
});
});
player->setOnShutdown([](const SockException &ex) { player->setOnShutdown([](const SockException &ex) {
ErrorL << "OnShutdown:" << ex.what(); ErrorL << "OnShutdown:" << ex.what();
}); });
(*player)[RtspPlayer::kRtpType] = atoi(argv[2]); (*player)[RtspPlayer::kRtpType] = atoi(argv[2]);
player->play(argv[1]); player->play(argv[1]);
SDLDisplayerHelper::Instance().runLoop();
H264Decoder decoder;
YuvDisplayer displayer;
//todo(xzl) 修复此处
#if 0
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);
if (flag) {
//DebugL << pFrame->pkt_pts;
displayer.displayYUV(pFrame);
}
#endif
});
#endif
EventPoller::Instance().runLoop();
} }
UDPServer::Destory(); UDPServer::Destory();
EventPoller::Destory(); EventPoller::Destory();