PlayerBase.h 7.32 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
class DemuxerBase : public TrackSource{
xiongziliang committed
45 46 47
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; }
xiongziliang committed
60 61 62 63
};


class PlayerBase : public DemuxerBase, public mINI{
xzl committed
64 65
public:
	typedef std::shared_ptr<PlayerBase> Ptr;
66
    static Ptr createPlayer(const EventPoller::Ptr &poller,const string &strUrl);
67

68
	PlayerBase();
xiongziliang committed
69
	virtual ~PlayerBase(){}
70 71 72 73 74

	/**
	 * 开始播放
	 * @param strUrl 视频url,支持rtsp/rtmp
	 */
xiongziliang committed
75
	virtual void play(const string &strUrl) {}
76 77 78 79 80

	/**
	 * 暂停或恢复
	 * @param bPause
	 */
xiongziliang committed
81
	virtual void pause(bool bPause) {}
82 83 84 85

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

88 89 90 91
	/**
	 * 设置异常中断回调
	 * @param cb
	 */
xiongziliang committed
92
	virtual void setOnShutdown( const function<void(const SockException &)> &cb) {}
93 94 95 96 97

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

100 101 102 103 104 105
    /**
     * 设置播放恢复回调
     * @param cb
     */
    virtual void setOnResume( const function<void()> &cb) {}

106 107 108 109
	/**
	 * 获取播放进度,取值 0.0 ~ 1.0
	 * @return
	 */
xiongziliang committed
110
    virtual float getProgress() const { return 0;}
111 112 113 114 115

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

118 119 120 121
    /**
     * 设置一个MediaSource,直接生产rtsp/rtmp代理
     * @param src
     */
xiongziliang committed
122
    virtual void setMediaSouce(const MediaSource::Ptr & src) {}
123 124 125 126 127 128 129

    /**
     * 获取丢包率,只支持rtsp
     * @param trackType 音频或视频,TrackInvalid时为总丢包率
     * @return
     */
	virtual float getPacketLossRate(TrackType trackType) const {return 0; }
xiongziliang committed
130 131 132 133 134 135 136

    /**
     * 获取所有track
     */
    vector<Track::Ptr> getTracks(bool trackReady = true) const override{
        return vector<Track::Ptr>();
    }
xzl committed
137
protected:
xiongziliang committed
138 139
    virtual void onShutdown(const SockException &ex) {}
    virtual void onPlayResult(const SockException &ex) {}
140 141 142 143
    /**
     * 暂停后恢复播放时间
     */
    virtual void onResume(){};
xzl committed
144 145
};

xiongziliang committed
146 147
template<typename Parent,typename Delegate>
class PlayerImp : public Parent {
xzl committed
148 149
public:
	typedef std::shared_ptr<PlayerImp> Ptr;
150 151 152 153

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

xiongziliang committed
154
	virtual ~PlayerImp(){}
xzl committed
155
	void setOnShutdown(const function<void(const SockException &)> &cb) override {
xiongziliang committed
156 157
		if (_delegate) {
			_delegate->setOnShutdown(cb);
xzl committed
158
		}
159
		_shutdownCB = cb;
xzl committed
160 161
	}
	void setOnPlayResult(const function<void(const SockException &ex)> &cb) override {
xiongziliang committed
162 163
		if (_delegate) {
			_delegate->setOnPlayResult(cb);
xzl committed
164
		}
165
		_playResultCB = cb;
xzl committed
166 167
	}

168
    void setOnResume(const function<void()> &cb) override {
xiongziliang committed
169 170
        if (_delegate) {
            _delegate->setOnResume(cb);
171 172 173 174
        }
        _resumeCB = cb;
    }

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

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

207
    vector<Track::Ptr> getTracks(bool trackReady = true) const override{
xiongziliang committed
208 209
		if (_delegate) {
			return _delegate->getTracks(trackReady);
xiongziliang committed
210
		}
xiongziliang committed
211
		return Parent::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
		if(_playResultCB) {
223 224
			_playResultCB(ex);
			_playResultCB = nullptr;
225 226
		}
	}
227 228 229 230 231 232

	void onResume() override{
        if(_resumeCB){
            _resumeCB();
        }
    }
xiongziliang committed
233
protected:
234 235
	function<void(const SockException &ex)> _shutdownCB;
	function<void(const SockException &ex)> _playResultCB;
236
    function<void()> _resumeCB;
xiongziliang committed
237
    std::shared_ptr<Delegate> _delegate;
238
	MediaSource::Ptr _pMediaSrc;
xzl committed
239
};
xiongziliang committed
240

241 242 243

class Demuxer : public PlayerBase{
public:
244 245 246 247 248 249 250
	class Listener{
	public:
		Listener() = default;
		virtual ~Listener() = default;
		virtual void onAddTrack(const Track::Ptr &track) = 0;
	};

251 252 253 254 255 256 257 258 259 260
	Demuxer(){};
	virtual ~Demuxer(){};

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

	/**
xiongziliang committed
267 268
	 * 获取所有Track
	 * @return 所有Track
269
	 */
270
	vector<Track::Ptr> getTracks(bool trackReady = true) const override;
271 272 273

	/**
	 * 获取节目总时长
xiongziliang committed
274
	 * @return 节目总时长,单位秒
275 276
	 */
	float getDuration() const override;
277 278 279 280 281 282 283

	/**
	 * 设置track监听器
	 */
	void setTrackListener(Listener *listener);
protected:
	void onAddTrack(const Track::Ptr &track);
284
protected:
285
	Listener *_listener;
286 287 288 289 290 291
	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_ */