test_player.cpp 1.89 KB
Newer Older
xzl committed
1 2 3 4 5 6 7

#include <signal.h>
#include <unistd.h>
#include "Util/logger.h"
#include <iostream>
#include "Poller/EventPoller.h"
#include "Rtsp/UDPServer.h"
xiongziliang committed
8
#include "Player/MediaPlayer.h"
xzl committed
9 10 11 12 13 14 15 16 17 18 19 20
#include "Util/onceToken.h"
#include "H264Decoder.h"
#include "YuvDisplayer.h"
#include "Network/sockutil.h"

using namespace std;
using namespace ZL::Screen;
using namespace ZL::Codec;
using namespace ZL::Util;
using namespace ZL::Thread;
using namespace ZL::Network;
using namespace ZL::Rtsp;
xiongziliang committed
21
using namespace ZL::Player;
xzl committed
22 23 24 25 26 27 28 29 30 31 32

void programExit(int arg) {
	EventPoller::Instance().shutdown();
}

int main(int argc, char *argv[]){
	
	Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
	//Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
	signal(SIGINT, programExit);

xiongziliang committed
33 34 35
	if(argc != 3){
		FatalL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n"
		       << "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n"
xzl committed
36 37 38 39 40 41 42 43 44 45 46 47 48
		       <<endl;
		Logger::Destory();
		return 0;

	}

	MediaPlayer::Ptr player(new MediaPlayer());
	player->setOnPlayResult([](const SockException &ex) {
		InfoL << "OnPlayResult:" << ex.what();
	});
	player->setOnShutdown([](const SockException &ex) {
		ErrorL << "OnShutdown:" << ex.what();
	});
xiongziliang committed
49 50
	(*player)[RtspPlayer::kRtpType] = atoi(argv[2]);
	player->play(argv[1]);
xzl committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

	H264Decoder decoder;
	YuvDisplayer displayer;
	ThreadPool pool(1);
	player->setOnVideoCB([&](const H264Frame &frame){
		pool.async([&,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;
						displayer.displayYUV(pFrame);
					}
				});
	});

	EventPoller::Instance().runLoop();


	static onceToken token(nullptr, []() {
		UDPServer::Destory();
        	AsyncTaskThread::Destory();
		EventPoller::Destory();
		Logger::Destory();
	});
	return 0;
}