mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
完成播放器适配
This commit is contained in:
parent
274dab6e1d
commit
5dc1859db4
@ -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,48 +65,44 @@ public:
|
|||||||
SDL_PushEvent(&event);
|
SDL_PushEvent(&event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void runLoop(){
|
||||||
|
bool flag = true;
|
||||||
|
std::function<bool ()> task;
|
||||||
|
SDL_Event event;
|
||||||
|
while(flag){
|
||||||
|
SDL_WaitEvent(&event);
|
||||||
|
if (event.type == REFRESH_EVENT)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
lock_guard<mutex> lck(_mtxTask);
|
||||||
|
if(_taskList.empty()){
|
||||||
|
//not reachable
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
task = _taskList.front();
|
||||||
|
_taskList.pop_front();
|
||||||
|
}
|
||||||
|
flag = task();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void shutdown(){
|
||||||
|
doTask([](){return false;});
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
SDLDisplayerHelper(){
|
SDLDisplayerHelper(){
|
||||||
_loopThread.reset(new std::thread(&SDLDisplayerHelper::runLoop,this));
|
|
||||||
};
|
};
|
||||||
~SDLDisplayerHelper(){
|
~SDLDisplayerHelper(){
|
||||||
doTask([](){return false;});
|
shutdown();
|
||||||
_loopThread->join();
|
|
||||||
_loopThread.reset();
|
|
||||||
};
|
};
|
||||||
|
private:
|
||||||
void runLoop(){
|
|
||||||
bool flag = true;
|
|
||||||
std::function<bool ()> task;
|
|
||||||
SDL_Event event;
|
|
||||||
while(flag){
|
|
||||||
SDL_WaitEvent(&event);
|
|
||||||
if (event.type == REFRESH_EVENT)
|
|
||||||
{
|
|
||||||
{
|
|
||||||
lock_guard<mutex> lck(_mtxTask);
|
|
||||||
if(_taskList.empty()){
|
|
||||||
//not reachable
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
task = _taskList.front();
|
|
||||||
_taskList.pop_front();
|
|
||||||
}
|
|
||||||
flag = task();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
Loading…
Reference in New Issue
Block a user