PlayerBase.cpp 3.36 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
xzl committed
9 10
 */

xiongzilaing committed
11
#include <algorithm>
xzl committed
12
#include "PlayerBase.h"
xiongzilaing committed
13 14
#include "Rtsp/RtspPlayerImp.h"
#include "Rtmp/RtmpPlayerImp.h"
15
#include "Http/HlsPlayer.h"
xiongziliang committed
16
using namespace toolkit;
xzl committed
17

xiongziliang committed
18
namespace mediakit {
xzl committed
19

20
PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller,const string &url_in) {
21 22 23 24 25 26
    static auto releasePlayer = [](PlayerBase *ptr){
        onceToken token(nullptr,[&](){
            delete  ptr;
        });
        ptr->teardown();
    };
27 28 29 30 31 32 33
    string url = url_in;
    string prefix = FindField(url.data(), NULL, "://");
    auto pos = url.find('?');
    if (pos != string::npos) {
        //去除?后面的字符串
        url = url.substr(0, pos);
    }
34

35 36 37
    if (strcasecmp("rtsps",prefix.data()) == 0) {
        return PlayerBase::Ptr(new TcpClientWithSSL<RtspPlayerImp>(poller),releasePlayer);
    }
38

39 40 41
    if (strcasecmp("rtsp",prefix.data()) == 0) {
        return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer);
    }
42

43 44 45
    if (strcasecmp("rtmps",prefix.data()) == 0) {
        return PlayerBase::Ptr(new TcpClientWithSSL<RtmpPlayerImp>(poller),releasePlayer);
    }
46

47 48 49
    if (strcasecmp("rtmp",prefix.data()) == 0) {
        return PlayerBase::Ptr(new RtmpPlayerImp(poller),releasePlayer);
    }
50

xiongziliang committed
51
    if ((strcasecmp("http",prefix.data()) == 0 || strcasecmp("https",prefix.data()) == 0) && end_with(url, ".m3u8")) {
52 53 54
        return PlayerBase::Ptr(new HlsPlayerImp(poller),releasePlayer);
    }

55
    return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer);
xzl committed
56 57
}

58
PlayerBase::PlayerBase() {
59 60 61 62
    this->mINI::operator[](kTimeoutMS) = 10000;
    this->mINI::operator[](kMediaTimeoutMS) = 5000;
    this->mINI::operator[](kBeatIntervalMS) = 5000;
    this->mINI::operator[](kMaxAnalysisMS) = 5000;
63 64
}

65
///////////////////////////Demuxer//////////////////////////////
66
bool Demuxer::isInited(int analysisMs) {
67 68 69 70 71 72 73 74 75 76 77 78 79
    if(analysisMs && _ticker.createdTime() > analysisMs){
        //analysisMs毫秒后强制初始化完毕
        return true;
    }
    if (_videoTrack && !_videoTrack->ready()) {
        //视频未准备好
        return false;
    }
    if (_audioTrack && !_audioTrack->ready()) {
        //音频未准备好
        return false;
    }
    return true;
80 81
}

82
vector<Track::Ptr> Demuxer::getTracks(bool trackReady) const {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    vector<Track::Ptr> ret;
    if(_videoTrack){
        if(trackReady){
            if(_videoTrack->ready()){
                ret.emplace_back(_videoTrack);
            }
        }else{
            ret.emplace_back(_videoTrack);
        }
    }
    if(_audioTrack){
        if(trackReady){
            if(_audioTrack->ready()){
                ret.emplace_back(_audioTrack);
            }
        }else{
            ret.emplace_back(_audioTrack);
        }
    }
102
    return ret;
103 104 105
}

float Demuxer::getDuration() const {
106
    return _fDuration;
107
}
108 109

void Demuxer::onAddTrack(const Track::Ptr &track){
110 111 112
    if(_listener){
        _listener->onAddTrack(track);
    }
113 114 115
}

void Demuxer::setTrackListener(Demuxer::Listener *listener) {
116
    _listener = listener;
117 118
}

xiongziliang committed
119
} /* namespace mediakit */