PlayerBase.h 7.54 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 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 127 128
    /**
     * 设置播放恢复回调
     * @param cb
     */
    virtual void setOnResume( const function<void()> &cb) {}

129 130 131 132
	/**
	 * 获取播放进度,取值 0.0 ~ 1.0
	 * @return
	 */
xiongziliang committed
133
    virtual float getProgress() const { return 0;}
134 135 136 137 138

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

141 142 143 144
    /**
     * 设置一个MediaSource,直接生产rtsp/rtmp代理
     * @param src
     */
xiongziliang committed
145
    virtual void setMediaSouce(const MediaSource::Ptr & src) {}
146 147 148 149 150 151 152

    /**
     * 获取丢包率,只支持rtsp
     * @param trackType 音频或视频,TrackInvalid时为总丢包率
     * @return
     */
	virtual float getPacketLossRate(TrackType trackType) const {return 0; }
xzl committed
153
protected:
xiongziliang committed
154 155
    virtual void onShutdown(const SockException &ex) {}
    virtual void onPlayResult(const SockException &ex) {}
156 157 158 159
    /**
     * 暂停后恢复播放时间
     */
    virtual void onResume(){};
xzl committed
160 161 162 163 164 165 166
};

template<typename Parent,typename Parser>
class PlayerImp : public Parent
{
public:
	typedef std::shared_ptr<PlayerImp> Ptr;
167 168 169 170

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

xiongziliang committed
171
	virtual ~PlayerImp(){}
xzl committed
172
	void setOnShutdown(const function<void(const SockException &)> &cb) override {
173 174
		if (_parser) {
			_parser->setOnShutdown(cb);
xzl committed
175
		}
176
		_shutdownCB = cb;
xzl committed
177 178
	}
	void setOnPlayResult(const function<void(const SockException &ex)> &cb) override {
179 180
		if (_parser) {
			_parser->setOnPlayResult(cb);
xzl committed
181
		}
182
		_playResultCB = cb;
xzl committed
183 184
	}

185 186 187 188 189 190 191
    void setOnResume(const function<void()> &cb) override {
        if (_parser) {
            _parser->setOnResume(cb);
        }
        _resumeCB = cb;
    }

192
    bool isInited(int analysisMs) override{
193
        if (_parser) {
194
            return _parser->isInited(analysisMs);
xzl committed
195
        }
196
        return PlayerBase::isInited(analysisMs);
xzl committed
197 198
    }
	float getDuration() const override {
199 200
		if (_parser) {
			return _parser->getDuration();
xzl committed
201 202 203
		}
		return PlayerBase::getDuration();
	}
xiongziliang committed
204
    float getProgress() const override{
205 206
        if (_parser) {
            return _parser->getProgress();
xzl committed
207
        }
xiongziliang committed
208
        return PlayerBase::getProgress();
xiongziliang committed
209
    }
xzl committed
210
    void seekTo(float fProgress) override{
211 212
        if (_parser) {
            return _parser->seekTo(fProgress);
xzl committed
213 214
        }
        return PlayerBase::seekTo(fProgress);
xiongziliang committed
215
    }
xiongziliang committed
216 217

    void setMediaSouce(const MediaSource::Ptr & src) override {
218
		if (_parser) {
219
			_parser->setMediaSouce(src);
xiongziliang committed
220
		}
221
		_pMediaSrc = src;
xiongziliang committed
222
    }
xiongziliang committed
223

224
    vector<Track::Ptr> getTracks(bool trackReady = true) const override{
225
		if (_parser) {
226
			return _parser->getTracks(trackReady);
xiongziliang committed
227
		}
228
		return PlayerBase::getTracks(trackReady);
xiongziliang committed
229
	}
xzl committed
230 231
protected:
	void onShutdown(const SockException &ex) override {
232 233
		if (_shutdownCB) {
			_shutdownCB(ex);
234
			_shutdownCB = nullptr;
xzl committed
235 236
		}
	}
237

xzl committed
238
	void onPlayResult(const SockException &ex) override {
239 240 241 242 243
		if(!_playResultCB){
			return;
		}
		if(ex){
			//播放失败,则立即回调
244 245
			_playResultCB(ex);
			_playResultCB = nullptr;
246 247
			return;
		}
248 249 250
		//播放成功
		_playResultCB(ex);
		_playResultCB = nullptr;
251
	}
252 253 254 255 256 257

	void onResume() override{
        if(_resumeCB){
            _resumeCB();
        }
    }
xiongziliang committed
258
protected:
259 260
	function<void(const SockException &ex)> _shutdownCB;
	function<void(const SockException &ex)> _playResultCB;
261 262
    function<void()> _resumeCB;
    std::shared_ptr<Parser> _parser;
263
	MediaSource::Ptr _pMediaSrc;
xzl committed
264
};
xiongziliang committed
265

266 267 268 269 270 271 272 273 274 275 276 277 278

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

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

	/**
	 * 获取所有可用Track,请在isInited()返回true时调用
	 * @return
	 */
288
	vector<Track::Ptr> getTracks(bool trackReady = true) const override;
289 290 291 292 293 294 295 296 297 298 299 300 301

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

xiongziliang committed
302
} /* namespace mediakit */
xzl committed
303 304

#endif /* SRC_PLAYER_PLAYERBASE_H_ */