PlayerBase.cpp 3.3 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 67 68 69
	this->mINI::operator[](kMediaTimeoutMS) = 5000;
	this->mINI::operator[](kBeatIntervalMS) = 5000;
	this->mINI::operator[](kMaxAnalysisMS) = 2000;
}

70
///////////////////////////Demuxer//////////////////////////////
71 72 73
bool Demuxer::isInited(int analysisMs) {
	if(_ticker.createdTime() < analysisMs){
		//analysisMs毫秒内判断条件
74 75 76
		//如果音视频都准备好了 ,说明Track全部就绪
		return (_videoTrack &&  _videoTrack->ready() && _audioTrack && _audioTrack->ready());
	}
77
	//analysisMs毫秒后强制初始化完毕
78 79 80
	return true;
}

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

float Demuxer::getDuration() const {
	return _fDuration;
}
xiongziliang committed
107
} /* namespace mediakit */