PlayerBase.cpp 3.52 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
25 26
 */

xiongzilaing committed
27
#include <algorithm>
xzl committed
28
#include "PlayerBase.h"
xiongzilaing committed
29 30
#include "Rtsp/RtspPlayerImp.h"
#include "Rtmp/RtmpPlayerImp.h"
xiongziliang committed
31
using namespace toolkit;
xzl committed
32

xiongziliang committed
33
namespace mediakit {
xzl committed
34

35
PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller,const string &strUrl) {
36 37 38 39 40 41
	static auto releasePlayer = [](PlayerBase *ptr){
		onceToken token(nullptr,[&](){
			delete  ptr;
		});
		ptr->teardown();
	};
xiongziliang committed
42
	string prefix = FindField(strUrl.data(), NULL, "://");
43 44 45 46 47

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

xiongziliang committed
48
	if (strcasecmp("rtsp",prefix.data()) == 0) {
49
		return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer);
xzl committed
50
	}
51 52 53 54 55

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

xiongziliang committed
56
	if (strcasecmp("rtmp",prefix.data()) == 0) {
57
		return PlayerBase::Ptr(new RtmpPlayerImp(poller),releasePlayer);
xzl committed
58
	}
59

60
	return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer);
xzl committed
61 62
}

63
PlayerBase::PlayerBase() {
xiongziliang committed
64
	this->mINI::operator[](kTimeoutMS) = 10000;
65 66
	this->mINI::operator[](kMediaTimeoutMS) = 5000;
	this->mINI::operator[](kBeatIntervalMS) = 5000;
67
	this->mINI::operator[](kMaxAnalysisMS) = 5000;
68 69
}

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

87
vector<Track::Ptr> Demuxer::getTracks(bool trackReady) const {
88 89
	vector<Track::Ptr> ret;
	if(_videoTrack){
90 91 92 93 94 95 96
		if(trackReady){
			if(_videoTrack->ready()){
				ret.emplace_back(_videoTrack);
			}
		}else{
			ret.emplace_back(_videoTrack);
		}
97 98
	}
	if(_audioTrack){
99 100 101 102 103 104 105
		if(trackReady){
			if(_audioTrack->ready()){
				ret.emplace_back(_audioTrack);
			}
		}else{
			ret.emplace_back(_audioTrack);
		}
106
	}
xiongziliang committed
107
	return std::move(ret);
108 109 110 111 112
}

float Demuxer::getDuration() const {
	return _fDuration;
}
113 114 115 116 117 118 119 120 121 122 123

void Demuxer::onAddTrack(const Track::Ptr &track){
	if(_listener){
		_listener->onAddTrack(track);
	}
}

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

xiongziliang committed
124
} /* namespace mediakit */