test_player.cpp 3.82 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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.
 */
xzl committed
26 27 28 29 30 31
#include <signal.h>
#include <unistd.h>
#include "Util/logger.h"
#include <iostream>
#include "Poller/EventPoller.h"
#include "Rtsp/UDPServer.h"
xiongziliang committed
32
#include "Player/MediaPlayer.h"
xzl committed
33 34 35 36 37 38 39 40 41 42 43
#include "Util/onceToken.h"
#include "H264Decoder.h"
#include "YuvDisplayer.h"
#include "Network/sockutil.h"

using namespace std;
using namespace ZL::Codec;
using namespace ZL::Util;
using namespace ZL::Thread;
using namespace ZL::Network;
using namespace ZL::Rtsp;
xiongziliang committed
44
using namespace ZL::Player;
xzl committed
45

xiongziliang committed
46 47 48 49 50 51
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>());
xzl committed
52

xiongziliang committed
53 54 55 56 57 58
    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;
xzl committed
59

xiongziliang committed
60
    }
xzl committed
61

xiongziliang committed
62 63 64 65
    {
        MediaPlayer::Ptr player(new MediaPlayer());
        player->setOnPlayResult([](const SockException &ex) {
            InfoL << "OnPlayResult:" << ex.what();
xiongziliang committed
66
        });
xiongziliang committed
67 68 69 70 71
        player->setOnShutdown([](const SockException &ex) {
            ErrorL << "OnShutdown:" << ex.what();
        });
        (*player)[RtspPlayer::kRtpType] = atoi(argv[2]);
        player->play(argv[1]);
xzl committed
72

xiongziliang committed
73 74 75
        H264Decoder decoder;
        YuvDisplayer displayer;
        player->setOnVideoCB([&](const H264Frame &frame) {
76
#ifndef __MACH__
xiongziliang committed
77 78 79 80 81 82
            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;
83 84 85
                    EventPoller::Instance().sync([&](){
                        displayer.displayYUV(pFrame);
                    });
xiongziliang committed
86 87 88
                }
                return true;
            });
89 90 91 92 93 94 95 96 97 98
#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

xiongziliang committed
99
        });
xzl committed
100

xiongziliang committed
101 102 103 104 105 106
        EventPoller::Instance().runLoop();
    }
    UDPServer::Destory();
    EventPoller::Destory();
    AsyncTaskThread::Destory();
    Logger::Destory();
xiongziliang committed
107
    return 0;
xzl committed
108 109
}