2020-05-18 15:31:49 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*/
|
2020-04-04 20:30:09 +08:00
|
|
|
|
|
2017-05-08 18:03:43 +08:00
|
|
|
|
#include <signal.h>
|
2019-07-09 14:53:23 +08:00
|
|
|
|
#include "Util/util.h"
|
2017-05-08 18:03:43 +08:00
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#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"
|
2020-04-30 12:52:13 +08:00
|
|
|
|
#include "FFMpegDecoder.h"
|
2017-05-08 18:03:43 +08:00
|
|
|
|
#include "YuvDisplayer.h"
|
2020-04-30 12:52:13 +08:00
|
|
|
|
#include "Extension/H265.h"
|
2021-06-28 17:34:07 +08:00
|
|
|
|
#include "Extension/AAC.h"
|
|
|
|
|
#include "Audio/AudioPlayer.h"
|
2017-05-08 18:03:43 +08:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-07-26 22:56:17 +08:00
|
|
|
|
#ifdef WIN32
|
|
|
|
|
#include <TCHAR.h>
|
|
|
|
|
|
|
|
|
|
extern int __argc;
|
|
|
|
|
extern TCHAR** __targv;
|
|
|
|
|
|
|
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstanc, LPSTR lpCmdLine, int nShowCmd) {
|
|
|
|
|
int argc = __argc;
|
|
|
|
|
char **argv = __targv;
|
|
|
|
|
|
|
|
|
|
//1. 首先调用AllocConsole创建一个控制台窗口
|
|
|
|
|
AllocConsole();
|
|
|
|
|
|
|
|
|
|
//2. 但此时调用cout或者printf都不能正常输出文字到窗口(包括输入流cin和scanf), 所以需要如下重定向输入输出流:
|
|
|
|
|
FILE* stream;
|
|
|
|
|
freopen_s(&stream, "CON", "r", stdin);//重定向输入流
|
|
|
|
|
freopen_s(&stream, "CON", "w", stdout);//重定向输入流
|
|
|
|
|
|
|
|
|
|
//3. 如果我们需要用到控制台窗口句柄,可以调用FindWindow取得:
|
|
|
|
|
HWND _consoleHwnd;
|
|
|
|
|
SetConsoleTitleA("test_player");//设置窗口名
|
|
|
|
|
#else
|
|
|
|
|
#include <unistd.h>
|
2018-04-17 21:38:13 +08:00
|
|
|
|
int main(int argc, char *argv[]) {
|
2019-07-26 22:56:17 +08:00
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-10-08 10:22:59 +08:00
|
|
|
|
static char *url = argv[1];
|
2018-04-17 21:38:13 +08:00
|
|
|
|
//设置退出信号处理函数
|
2018-10-29 13:56:10 +08:00
|
|
|
|
signal(SIGINT, [](int) { SDLDisplayerHelper::Instance().shutdown(); });
|
2018-04-17 21:38:13 +08:00
|
|
|
|
//设置日志
|
2019-08-22 16:57:50 +08:00
|
|
|
|
Logger::Instance().add(std::make_shared<ConsoleChannel>());
|
2018-12-28 16:47:50 +08:00
|
|
|
|
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;
|
|
|
|
|
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-12-28 16:47:50 +08:00
|
|
|
|
MediaPlayer::Ptr player(new MediaPlayer());
|
|
|
|
|
weak_ptr<MediaPlayer> weakPlayer = player;
|
|
|
|
|
player->setOnPlayResult([weakPlayer](const SockException &ex) {
|
|
|
|
|
InfoL << "OnPlayResult:" << ex.what();
|
|
|
|
|
auto strongPlayer = weakPlayer.lock();
|
|
|
|
|
if (ex || !strongPlayer) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-08 18:03:43 +08:00
|
|
|
|
|
2020-08-01 10:17:54 +08:00
|
|
|
|
auto viedoTrack = strongPlayer->getTrack(TrackVideo, false);
|
2020-04-30 12:52:13 +08:00
|
|
|
|
if (!viedoTrack) {
|
|
|
|
|
WarnL << "没有视频!";
|
2018-12-28 16:47:50 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-09 14:53:23 +08:00
|
|
|
|
|
2021-06-28 17:34:07 +08:00
|
|
|
|
auto audioTrack = strongPlayer->getTrack(TrackAudio,false);
|
|
|
|
|
if( !audioTrack){
|
|
|
|
|
WarnL << "没有音频!";
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 14:53:23 +08:00
|
|
|
|
AnyStorage::Ptr storage(new AnyStorage);
|
2020-05-17 19:06:14 +08:00
|
|
|
|
viedoTrack->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([storage](const Frame::Ptr &frame_in) {
|
|
|
|
|
auto frame = Frame::getCacheAbleFrame(frame_in);
|
2019-07-09 14:53:23 +08:00
|
|
|
|
SDLDisplayerHelper::Instance().doTask([frame,storage]() {
|
|
|
|
|
auto &decoder = (*storage)["decoder"];
|
|
|
|
|
auto &displayer = (*storage)["displayer"];
|
2019-09-05 11:15:30 +08:00
|
|
|
|
auto &merger = (*storage)["merger"];
|
2019-07-09 14:53:23 +08:00
|
|
|
|
if(!decoder){
|
2020-04-30 12:52:13 +08:00
|
|
|
|
decoder.set<FFMpegDecoder>(frame->getCodecId());
|
2019-07-09 14:53:23 +08:00
|
|
|
|
}
|
|
|
|
|
if(!displayer){
|
2019-10-08 10:22:59 +08:00
|
|
|
|
displayer.set<YuvDisplayer>(nullptr,url);
|
2019-07-09 14:53:23 +08:00
|
|
|
|
}
|
2019-09-05 11:15:30 +08:00
|
|
|
|
if(!merger){
|
2021-04-26 18:26:07 +08:00
|
|
|
|
merger.set<FrameMerger>(FrameMerger::h264_prefix);
|
2020-04-30 12:52:13 +08:00
|
|
|
|
}
|
2021-04-27 23:05:04 +08:00
|
|
|
|
merger.get<FrameMerger>().inputFrame(frame,[&](uint32_t dts,uint32_t pts,const Buffer::Ptr &buffer, bool have_idr){
|
2019-09-05 11:15:30 +08:00
|
|
|
|
AVFrame *pFrame = nullptr;
|
2020-04-30 12:52:13 +08:00
|
|
|
|
bool flag = decoder.get<FFMpegDecoder>().inputVideo((unsigned char *) buffer->data(), buffer->size(), dts, &pFrame);
|
2019-09-05 11:15:30 +08:00
|
|
|
|
if (flag) {
|
|
|
|
|
displayer.get<YuvDisplayer>().displayYUV(pFrame);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-07-09 14:53:23 +08:00
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}));
|
2021-06-28 17:34:07 +08:00
|
|
|
|
|
|
|
|
|
auto audio_delagate = std::make_shared<FrameWriterInterfaceHelper>([storage, audioTrack](const Frame::Ptr& frame_in) {
|
|
|
|
|
auto frame = Frame::getCacheAbleFrame(frame_in);
|
|
|
|
|
AACTrack::Ptr aacTrack = dynamic_pointer_cast<AACTrack>(audioTrack);
|
|
|
|
|
|
|
|
|
|
std::string codename = frame->getCodecName();
|
|
|
|
|
int out_sample_fmt = aacTrack->getAudioSampleBit();
|
|
|
|
|
int channelnum = aacTrack->getAudioChannel();
|
|
|
|
|
int sample_rate = aacTrack->getAudioSampleRate();
|
|
|
|
|
|
|
|
|
|
auto &audioplayer = (*storage)["audioplayer"];
|
|
|
|
|
|
|
|
|
|
if(!audioplayer){
|
|
|
|
|
audioplayer.set<AudioPlayer>(sample_rate,channelnum,out_sample_fmt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
audioplayer.get<AudioPlayer>().inputFrame( frame );
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if( audioTrack ) {
|
|
|
|
|
audioTrack->addDelegate(audio_delagate);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 16:47:50 +08:00
|
|
|
|
});
|
2018-06-30 23:02:16 +08:00
|
|
|
|
|
2018-10-29 13:56:10 +08:00
|
|
|
|
|
2018-12-28 16:47:50 +08:00
|
|
|
|
player->setOnShutdown([](const SockException &ex) {
|
|
|
|
|
ErrorL << "OnShutdown:" << ex.what();
|
|
|
|
|
});
|
2019-03-27 18:58:43 +08:00
|
|
|
|
(*player)[kRtpType] = atoi(argv[2]);
|
2018-12-28 16:47:50 +08:00
|
|
|
|
player->play(argv[1]);
|
|
|
|
|
|
|
|
|
|
SDLDisplayerHelper::Instance().runLoop();
|
2018-04-17 21:38:13 +08:00
|
|
|
|
return 0;
|
2017-05-08 18:03:43 +08:00
|
|
|
|
}
|
|
|
|
|
|