ZLMediaKit/tests/test_player.cpp

108 lines
3.8 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2017-05-08 18:03:43 +08:00
#include <signal.h>
#include <unistd.h>
#include "Util/logger.h"
#include <iostream>
#include "Poller/EventPoller.h"
#include "Rtsp/UDPServer.h"
2017-08-03 13:55:46 +08:00
#include "Player/MediaPlayer.h"
2017-05-08 18:03:43 +08:00
#include "Util/onceToken.h"
#include "H264Decoder.h"
#include "YuvDisplayer.h"
#include "Network/sockutil.h"
using namespace std;
2018-10-24 17:17:55 +08:00
using namespace toolkit;
using namespace mediakit;
2017-05-08 18:03:43 +08:00
2018-04-17 21:38:13 +08:00
int main(int argc, char *argv[]) {
//设置退出信号处理函数
signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); });
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
2017-05-08 18:03:43 +08:00
2018-04-17 21:38:13 +08:00
if (argc != 3) {
ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n"
<< "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n"
<< endl;
Logger::Destory();
return 0;
2017-05-08 18:03:43 +08:00
2018-04-17 21:38:13 +08:00
}
2017-05-08 18:03:43 +08:00
2018-04-17 21:44:18 +08:00
{
MediaPlayer::Ptr player(new MediaPlayer());
player->setOnPlayResult([](const SockException &ex) {
InfoL << "OnPlayResult:" << ex.what();
2018-04-17 21:38:13 +08:00
});
2018-04-17 21:44:18 +08:00
player->setOnShutdown([](const SockException &ex) {
ErrorL << "OnShutdown:" << ex.what();
});
(*player)[RtspPlayer::kRtpType] = atoi(argv[2]);
player->play(argv[1]);
2017-05-08 18:03:43 +08:00
2018-04-17 21:44:18 +08:00
H264Decoder decoder;
YuvDisplayer displayer;
2018-10-25 17:39:19 +08:00
//todo(xzl) 修复此处
#if 0
2018-04-17 21:44:18 +08:00
player->setOnVideoCB([&](const H264Frame &frame) {
#ifndef __MACH__
2018-04-17 21:44:18 +08:00
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);
});
2018-04-17 21:44:18 +08:00
}
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
2018-04-17 21:44:18 +08:00
});
2018-10-25 17:39:19 +08:00
#endif
2018-04-17 21:44:18 +08:00
EventPoller::Instance().runLoop();
}
UDPServer::Destory();
EventPoller::Destory();
AsyncTaskThread::Destory();
Logger::Destory();
2018-04-17 21:38:13 +08:00
return 0;
2017-05-08 18:03:43 +08:00
}