PlayerBase.h 7.49 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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
25 26 27 28 29
 */

#ifndef SRC_PLAYER_PLAYERBASE_H_
#define SRC_PLAYER_PLAYERBASE_H_

xiongziliang committed
30
#include <map>
xzl committed
31 32
#include <memory>
#include <string>
xiongzilaing committed
33 34
#include <functional>
#include "Network/Socket.h"
xiongziliang committed
35
#include "Util/mini.h"
36
#include "Util/RingBuffer.h"
xiongziliang committed
37
#include "Common/MediaSource.h"
38 39
#include "Extension/Frame.h"
#include "Extension/Track.h"
xiongziliang committed
40
using namespace toolkit;
xiongzilaing committed
41

xiongziliang committed
42
namespace mediakit {
xzl committed
43

xiongziliang committed
44 45 46 47
class DemuxerBase {
public:
	typedef std::shared_ptr<DemuxerBase> Ptr;

48 49 50 51
	/**
	 * 获取节目总时长,单位秒
	 * @return
	 */
xiongziliang committed
52
	virtual float getDuration() const { return 0;}
53 54 55

	/**
	 * 是否初始化完毕,完毕后方可调用getTrack方法
56
	 * @param analysisMs 数据流最大分析时间 单位毫秒
57 58
	 * @return
	 */
59
	virtual bool isInited(int analysisMs) { return true; }
60 61 62

	/**
	 * 获取全部的Track
63
	 * @param trackReady 是否获取全部已经准备好的Track
64 65
	 * @return
	 */
66
	virtual vector<Track::Ptr> getTracks(bool trackReady = true) const { return vector<Track::Ptr>();}
67 68 69

	/**
	 * 获取特定Track
70 71
	 * @param type track类型
	 * @param trackReady 是否获取全部已经准备好的Track
72 73
	 * @return
	 */
74 75
	virtual Track::Ptr getTrack(TrackType type , bool trackReady = true) const {
		auto tracks = getTracks(trackReady);
xiongziliang committed
76 77 78 79 80 81 82
		for(auto &track : tracks){
			if(track->getTrackType() == type){
				return track;
			}
		}
		return nullptr;
	}
xiongziliang committed
83 84 85 86
};


class PlayerBase : public DemuxerBase, public mINI{
xzl committed
87 88
public:
	typedef std::shared_ptr<PlayerBase> Ptr;
89
    static Ptr createPlayer(const EventPoller::Ptr &poller,const string &strUrl);
90

91
	PlayerBase();
xiongziliang committed
92
	virtual ~PlayerBase(){}
93 94 95 96 97

	/**
	 * 开始播放
	 * @param strUrl 视频url,支持rtsp/rtmp
	 */
xiongziliang committed
98
	virtual void play(const string &strUrl) {}
99 100 101 102 103

	/**
	 * 暂停或恢复
	 * @param bPause
	 */
xiongziliang committed
104
	virtual void pause(bool bPause) {}
105 106 107 108

	/**
	 * 中断播放
	 */
xiongziliang committed
109
	virtual void teardown() {}
xzl committed
110

111 112 113 114
	/**
	 * 设置异常中断回调
	 * @param cb
	 */
xiongziliang committed
115
	virtual void setOnShutdown( const function<void(const SockException &)> &cb) {}
116 117 118 119 120

	/**
	 * 设置播放结果回调
	 * @param cb
	 */
xiongziliang committed
121
	virtual void setOnPlayResult( const function<void(const SockException &ex)> &cb) {}
122

123 124 125 126
	/**
	 * 获取播放进度,取值 0.0 ~ 1.0
	 * @return
	 */
xiongziliang committed
127
    virtual float getProgress() const { return 0;}
128 129 130 131 132

    /**
     * 拖动进度条
     * @param fProgress 进度,取值 0.0 ~ 1.0
     */
xiongziliang committed
133
    virtual void seekTo(float fProgress) {}
xiongziliang committed
134

135 136 137 138
    /**
     * 设置一个MediaSource,直接生产rtsp/rtmp代理
     * @param src
     */
xiongziliang committed
139
    virtual void setMediaSouce(const MediaSource::Ptr & src) {}
140 141 142 143 144 145 146

    /**
     * 获取丢包率,只支持rtsp
     * @param trackType 音频或视频,TrackInvalid时为总丢包率
     * @return
     */
	virtual float getPacketLossRate(TrackType trackType) const {return 0; }
xzl committed
147
protected:
xiongziliang committed
148 149
    virtual void onShutdown(const SockException &ex) {}
    virtual void onPlayResult(const SockException &ex) {}
xzl committed
150 151 152 153 154 155 156
};

template<typename Parent,typename Parser>
class PlayerImp : public Parent
{
public:
	typedef std::shared_ptr<PlayerImp> Ptr;
157 158 159 160

	template<typename ...ArgsType>
	PlayerImp(ArgsType &&...args):Parent(std::forward<ArgsType>(args)...){}

xiongziliang committed
161
	virtual ~PlayerImp(){}
xzl committed
162
	void setOnShutdown(const function<void(const SockException &)> &cb) override {
163 164
		if (_parser) {
			_parser->setOnShutdown(cb);
xzl committed
165
		}
166
		_shutdownCB = cb;
xzl committed
167 168
	}
	void setOnPlayResult(const function<void(const SockException &ex)> &cb) override {
169 170
		if (_parser) {
			_parser->setOnPlayResult(cb);
xzl committed
171
		}
172
		_playResultCB = cb;
xzl committed
173 174
	}

175
    bool isInited(int analysisMs) override{
176
        if (_parser) {
177
            return _parser->isInited(analysisMs);
xzl committed
178
        }
179
        return PlayerBase::isInited(analysisMs);
xzl committed
180 181
    }
	float getDuration() const override {
182 183
		if (_parser) {
			return _parser->getDuration();
xzl committed
184 185 186
		}
		return PlayerBase::getDuration();
	}
xiongziliang committed
187
    float getProgress() const override{
188 189
        if (_parser) {
            return _parser->getProgress();
xzl committed
190
        }
xiongziliang committed
191
        return PlayerBase::getProgress();
xiongziliang committed
192
    }
xzl committed
193
    void seekTo(float fProgress) override{
194 195
        if (_parser) {
            return _parser->seekTo(fProgress);
xzl committed
196 197
        }
        return PlayerBase::seekTo(fProgress);
xiongziliang committed
198
    }
xiongziliang committed
199 200

    void setMediaSouce(const MediaSource::Ptr & src) override {
201 202
		if (_parser) {
			return _parser->setMediaSouce(src);
xiongziliang committed
203
		}
204
		_pMediaSrc = src;
xiongziliang committed
205
    }
xiongziliang committed
206

207
    vector<Track::Ptr> getTracks(bool trackReady = true) const override{
208
		if (_parser) {
209
			return _parser->getTracks(trackReady);
xiongziliang committed
210
		}
211
		return PlayerBase::getTracks(trackReady);
xiongziliang committed
212
	}
xzl committed
213 214
protected:
	void onShutdown(const SockException &ex) override {
215 216
		if (_shutdownCB) {
			_shutdownCB(ex);
217
			_shutdownCB = nullptr;
xzl committed
218 219
		}
	}
220

xzl committed
221
	void onPlayResult(const SockException &ex) override {
222 223 224 225 226
		if(!_playResultCB){
			return;
		}
		if(ex){
			//播放失败,则立即回调
227 228
			_playResultCB(ex);
			_playResultCB = nullptr;
229 230
			return;
		}
231
		//播放成功后,我们还必须等待各个Track初始化完毕才能回调告知已经初始化完毕
xiongziliang committed
232
		if(isInited(0xFFFF)){
233 234 235 236 237
			//初始化完毕则立即回调
			_playResultCB(ex);
			_playResultCB = nullptr;
			return;
		}
238
		//播放成功却未初始化完毕,这个时候不回调汇报播放成功
239
	}
240
	void checkInited(int analysisMs){
241 242 243
		if(!_playResultCB){
			return;
		}
244
		if(isInited(analysisMs)){
245 246
			_playResultCB(SockException(Err_success,"play success"));
			_playResultCB = nullptr;
xzl committed
247 248
		}
	}
xiongziliang committed
249
protected:
250 251 252 253
	function<void(const SockException &ex)> _shutdownCB;
	function<void(const SockException &ex)> _playResultCB;
	std::shared_ptr<Parser> _parser;
	MediaSource::Ptr _pMediaSrc;
xzl committed
254
};
xiongziliang committed
255

256 257 258 259 260 261 262 263 264 265 266 267 268

class Demuxer : public PlayerBase{
public:
	Demuxer(){};
	virtual ~Demuxer(){};

	/**
	 * 返回是否完成初始化完毕
	 * 在构造RtspDemuxer对象时有些rtsp的sdp不包含sps pps信息
	 * 所以要等待接收到到sps的rtp包后才能完成
	 *
	 * 在构造RtmpDemuxer对象时是无法获取sps pps aac_cfg等这些信息,
	 * 所以要调用inputRtmp后才会获取到这些信息,这时才初始化成功
269
	 * @param analysisMs 数据流最大分析时间 单位毫秒
270 271
	 * @return
	 */
272
	bool isInited(int analysisMs) override;
273 274 275 276 277

	/**
	 * 获取所有可用Track,请在isInited()返回true时调用
	 * @return
	 */
278
	vector<Track::Ptr> getTracks(bool trackReady = true) const override;
279 280 281 282 283 284 285 286 287 288 289 290 291

	/**
	 * 获取节目总时长
	 * @return
	 */
	float getDuration() const override;
protected:
	AudioTrack::Ptr _audioTrack;
	VideoTrack::Ptr _videoTrack;
	Ticker _ticker;
	float _fDuration = 0;
};

xiongziliang committed
292
} /* namespace mediakit */
xzl committed
293 294

#endif /* SRC_PLAYER_PLAYERBASE_H_ */