PlayerBase.cpp 3.05 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 29
#include "PlayerBase.h"
#include "Rtsp/Rtsp.h"
xiongzilaing committed
30 31
#include "Rtsp/RtspPlayerImp.h"
#include "Rtmp/RtmpPlayerImp.h"
xiongziliang committed
32
using namespace toolkit;
xzl committed
33

xiongziliang committed
34
namespace mediakit {
xzl committed
35

36
PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller,const string &strUrl) {
37 38 39 40 41 42
	static auto releasePlayer = [](PlayerBase *ptr){
		onceToken token(nullptr,[&](){
			delete  ptr;
		});
		ptr->teardown();
	};
xiongziliang committed
43
	string prefix = FindField(strUrl.data(), NULL, "://");
xiongziliang committed
44
	if (strcasecmp("rtsp",prefix.data()) == 0) {
45
		return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer);
xzl committed
46
	}
xiongziliang committed
47
	if (strcasecmp("rtmp",prefix.data()) == 0) {
48
		return PlayerBase::Ptr(new RtmpPlayerImp(poller),releasePlayer);
xzl committed
49
	}
50
	return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer);
xzl committed
51 52
}

53
PlayerBase::PlayerBase() {
xiongziliang committed
54
	this->mINI::operator[](kTimeoutMS) = 10000;
55 56 57 58 59
	this->mINI::operator[](kMediaTimeoutMS) = 5000;
	this->mINI::operator[](kBeatIntervalMS) = 5000;
	this->mINI::operator[](kMaxAnalysisMS) = 2000;
}

60
///////////////////////////Demuxer//////////////////////////////
61 62 63
bool Demuxer::isInited(int analysisMs) {
	if(_ticker.createdTime() < analysisMs){
		//analysisMs毫秒内判断条件
64 65 66
		//如果音视频都准备好了 ,说明Track全部就绪
		return (_videoTrack &&  _videoTrack->ready() && _audioTrack && _audioTrack->ready());
	}
67
	//analysisMs毫秒后强制初始化完毕
68 69 70
	return true;
}

71
vector<Track::Ptr> Demuxer::getTracks(bool trackReady) const {
72 73
	vector<Track::Ptr> ret;
	if(_videoTrack){
74 75 76 77 78 79 80
		if(trackReady){
			if(_videoTrack->ready()){
				ret.emplace_back(_videoTrack);
			}
		}else{
			ret.emplace_back(_videoTrack);
		}
81 82
	}
	if(_audioTrack){
83 84 85 86 87 88 89
		if(trackReady){
			if(_audioTrack->ready()){
				ret.emplace_back(_audioTrack);
			}
		}else{
			ret.emplace_back(_audioTrack);
		}
90 91 92 93 94 95 96
	}
	return ret;
}

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