2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_PLAYER_PLAYERBASE_H_
|
|
|
|
|
#define SRC_PLAYER_PLAYERBASE_H_
|
|
|
|
|
|
2017-08-03 13:55:46 +08:00
|
|
|
|
#include <map>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <functional>
|
|
|
|
|
#include "Network/Socket.h"
|
2017-08-03 13:55:46 +08:00
|
|
|
|
#include "Util/mini.h"
|
2018-09-19 12:34:29 +08:00
|
|
|
|
#include "Util/RingBuffer.h"
|
2018-10-21 21:21:14 +08:00
|
|
|
|
#include "Common/MediaSource.h"
|
2021-04-20 17:53:43 +08:00
|
|
|
|
#include "Common/MediaSink.h"
|
2018-10-30 14:59:42 +08:00
|
|
|
|
#include "Extension/Frame.h"
|
|
|
|
|
#include "Extension/Track.h"
|
2021-11-10 10:58:43 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-04-25 11:35:41 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
class PlayerBase : public TrackSource, public mINI {
|
2018-10-25 15:45:38 +08:00
|
|
|
|
public:
|
2021-11-10 10:58:43 +08:00
|
|
|
|
using Ptr = std::shared_ptr<PlayerBase>;
|
|
|
|
|
using Event = std::function<void(const SockException &ex)>;
|
2018-10-25 15:45:38 +08:00
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
static Ptr createPlayer(const EventPoller::Ptr &poller, const string &strUrl);
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
PlayerBase();
|
2021-11-10 10:58:43 +08:00
|
|
|
|
~PlayerBase() override = default;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开始播放
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* @param url 视频url,支持rtsp/rtmp
|
2020-03-20 11:51:24 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void play(const string &url) {};
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 暂停或恢复
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* @param flag true:暂停,false:恢复
|
|
|
|
|
*/
|
|
|
|
|
virtual void pause(bool flag) {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取节目总时长,单位秒
|
2020-03-20 11:51:24 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual float getDuration() const { return 0; };
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
2021-08-09 18:28:43 +08:00
|
|
|
|
/**
|
|
|
|
|
* 倍数播放
|
|
|
|
|
* @param speed 1.0 2.0 0.5
|
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void speed(float speed) {};
|
2021-08-09 18:28:43 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* 中断播放
|
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void teardown() {};
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 获取播放进度,取值 0.0 ~ 1.0
|
2020-03-20 11:51:24 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual float getProgress() const { return 0; };
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 获取播放进度pos,取值 相对开始时间增量 单位秒
|
2020-03-20 11:51:24 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual uint32_t getProgressPos() const { return 0; };
|
2018-09-18 23:49:48 +08:00
|
|
|
|
|
2019-05-08 15:27:37 +08:00
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 拖动进度条
|
|
|
|
|
* @param progress 进度,取值 0.0 ~ 1.0
|
2019-05-08 15:27:37 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void seekTo(float progress) {};
|
2019-05-08 15:27:37 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 拖动进度条
|
|
|
|
|
* @param pos 进度,取值 相对于开始时间的增量 单位秒
|
2020-03-20 11:51:24 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void seekTo(uint32_t pos) {};
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
2021-08-09 18:28:43 +08:00
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 获取丢包率,只支持rtsp
|
|
|
|
|
* @param type 音频或视频,TrackInvalid时为总丢包率
|
2021-08-09 18:28:43 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual float getPacketLossRate(TrackType type) const { return 0; };
|
2021-08-09 18:28:43 +08:00
|
|
|
|
|
2018-10-26 14:12:16 +08:00
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 获取所有track
|
2018-10-26 14:12:16 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
vector<Track::Ptr> getTracks(bool ready = true) const override { return vector<Track::Ptr>(); };
|
2018-10-26 11:03:53 +08:00
|
|
|
|
|
2021-08-09 18:28:43 +08:00
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 设置一个MediaSource,直接生产rtsp/rtmp代理
|
2021-08-09 18:28:43 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void setMediaSource(const MediaSource::Ptr &src) = 0;
|
2021-08-09 18:28:43 +08:00
|
|
|
|
|
2018-10-26 14:12:16 +08:00
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 设置异常中断回调
|
2018-10-26 14:12:16 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void setOnShutdown(const Event &cb) = 0;
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 设置播放结果回调
|
2018-10-26 14:12:16 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void setOnPlayResult(const Event &cb) = 0;
|
2019-12-03 16:10:02 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2021-11-10 10:58:43 +08:00
|
|
|
|
* 设置播放恢复回调
|
2019-12-03 16:10:02 +08:00
|
|
|
|
*/
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void setOnResume(const function<void()> &cb) = 0;
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
protected:
|
2021-11-10 10:58:43 +08:00
|
|
|
|
virtual void onResume() = 0;
|
|
|
|
|
virtual void onShutdown(const SockException &ex) = 0;
|
|
|
|
|
virtual void onPlayResult(const SockException &ex) = 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
template<typename Parent, typename Delegate>
|
2019-12-03 16:10:02 +08:00
|
|
|
|
class PlayerImp : public Parent {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2021-11-10 10:58:43 +08:00
|
|
|
|
using Ptr = std::shared_ptr<PlayerImp>;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
template<typename ...ArgsType>
|
2021-11-10 10:58:43 +08:00
|
|
|
|
PlayerImp(ArgsType &&...args) : Parent(std::forward<ArgsType>(args)...) {}
|
|
|
|
|
~PlayerImp() override = default;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
void play(const string &url) override {
|
|
|
|
|
return _delegate ? _delegate->play(url) : Parent::play(url);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-08-12 16:07:31 +08:00
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
void pause(bool flag) override {
|
|
|
|
|
return _delegate ? _delegate->pause(flag) : Parent::pause(flag);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
void speed(float speed) override {
|
|
|
|
|
return _delegate ? _delegate->speed(speed) : Parent::speed(speed);
|
2019-05-08 15:27:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
void teardown() override {
|
|
|
|
|
return _delegate ? _delegate->teardown() : Parent::teardown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float getPacketLossRate(TrackType type) const override {
|
|
|
|
|
return _delegate ? _delegate->getPacketLossRate(type) : Parent::getPacketLossRate(type);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2021-08-12 16:07:31 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
float getDuration() const override {
|
2021-11-10 10:58:43 +08:00
|
|
|
|
return _delegate ? _delegate->getDuration() : Parent::getDuration();
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-08-12 16:07:31 +08:00
|
|
|
|
|
|
|
|
|
float getProgress() const override {
|
2021-11-10 10:58:43 +08:00
|
|
|
|
return _delegate ? _delegate->getProgress() : Parent::getProgress();
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
2021-08-12 16:07:31 +08:00
|
|
|
|
|
2021-08-09 18:28:43 +08:00
|
|
|
|
uint32_t getProgressPos() const override {
|
2021-11-10 10:58:43 +08:00
|
|
|
|
return _delegate ? _delegate->getProgressPos() : Parent::getProgressPos();
|
2021-08-09 18:28:43 +08:00
|
|
|
|
}
|
2021-08-12 16:07:31 +08:00
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
void seekTo(float progress) override {
|
|
|
|
|
return _delegate ? _delegate->seekTo(progress) : Parent::seekTo(progress);
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
2018-09-18 21:40:26 +08:00
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
void seekTo(uint32_t pos) override {
|
|
|
|
|
return _delegate ? _delegate->seekTo(pos) : Parent::seekTo(pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector<Track::Ptr> getTracks(bool ready = true) const override {
|
|
|
|
|
return _delegate ? _delegate->getTracks(ready) : Parent::getTracks(ready);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<SockInfo> getSockInfo() const {
|
|
|
|
|
return dynamic_pointer_cast<SockInfo>(_delegate);
|
2021-08-09 18:28:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
void setMediaSource(const MediaSource::Ptr &src) override {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if (_delegate) {
|
2020-10-24 23:30:06 +08:00
|
|
|
|
_delegate->setMediaSource(src);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-11-10 10:58:43 +08:00
|
|
|
|
_media_src = src;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setOnShutdown(const function<void(const SockException &)> &cb) override {
|
|
|
|
|
if (_delegate) {
|
|
|
|
|
_delegate->setOnShutdown(cb);
|
|
|
|
|
}
|
|
|
|
|
_on_shutdown = cb;
|
2018-10-23 16:41:25 +08:00
|
|
|
|
}
|
2018-09-18 21:40:26 +08:00
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
void setOnPlayResult(const function<void(const SockException &ex)> &cb) override {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
if (_delegate) {
|
2021-11-10 10:58:43 +08:00
|
|
|
|
_delegate->setOnPlayResult(cb);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-11-10 10:58:43 +08:00
|
|
|
|
_on_play_result = cb;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2020-09-27 11:32:49 +08:00
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
void setOnResume(const function<void()> &cb) override {
|
|
|
|
|
if (_delegate) {
|
|
|
|
|
_delegate->setOnResume(cb);
|
|
|
|
|
}
|
|
|
|
|
_on_resume = cb;
|
2020-09-27 11:32:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
protected:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void onShutdown(const SockException &ex) override {
|
2021-11-10 10:58:43 +08:00
|
|
|
|
if (_on_shutdown) {
|
|
|
|
|
_on_shutdown(ex);
|
|
|
|
|
_on_shutdown = nullptr;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onPlayResult(const SockException &ex) override {
|
2021-11-10 10:58:43 +08:00
|
|
|
|
if (_on_play_result) {
|
|
|
|
|
_on_play_result(ex);
|
|
|
|
|
_on_play_result = nullptr;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
void onResume() override {
|
|
|
|
|
if (_on_resume) {
|
|
|
|
|
_on_resume();
|
2019-05-08 15:27:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-10 10:58:43 +08:00
|
|
|
|
|
2018-09-18 21:40:26 +08:00
|
|
|
|
protected:
|
2021-11-10 10:58:43 +08:00
|
|
|
|
function<void()> _on_resume;
|
|
|
|
|
PlayerBase::Event _on_shutdown;
|
|
|
|
|
PlayerBase::Event _on_play_result;
|
|
|
|
|
MediaSource::Ptr _media_src;
|
2019-12-03 16:10:02 +08:00
|
|
|
|
std::shared_ptr<Delegate> _delegate;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
2018-10-23 16:41:25 +08:00
|
|
|
|
|
2021-12-01 21:38:31 +08:00
|
|
|
|
class MediaSinkDelegate : public MediaSink {
|
|
|
|
|
public:
|
|
|
|
|
MediaSinkDelegate() = default;
|
|
|
|
|
~MediaSinkDelegate() override = default;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置track监听器
|
|
|
|
|
*/
|
|
|
|
|
void setTrackListener(TrackListener *listener);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void resetTracks() override;
|
|
|
|
|
bool onTrackReady(const Track::Ptr & track) override;
|
|
|
|
|
void onAllTrackReady() override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
TrackListener *_listener = nullptr;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-10 13:44:29 +08:00
|
|
|
|
class Demuxer : protected TrackListener, public TrackSource {
|
2018-11-15 15:14:05 +08:00
|
|
|
|
public:
|
2021-04-20 17:53:43 +08:00
|
|
|
|
Demuxer() = default;
|
|
|
|
|
~Demuxer() override = default;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
2021-11-10 13:44:29 +08:00
|
|
|
|
void setTrackListener(TrackListener *listener, bool wait_track_ready = false);
|
2021-12-01 21:38:31 +08:00
|
|
|
|
vector<Track::Ptr> getTracks(bool trackReady = true) const override;
|
|
|
|
|
|
|
|
|
|
protected:
|
2021-11-10 13:44:29 +08:00
|
|
|
|
bool addTrack(const Track::Ptr &track) override;
|
|
|
|
|
void addTrackCompleted() override;
|
2021-04-20 17:53:43 +08:00
|
|
|
|
void resetTracks() override;
|
|
|
|
|
|
2021-11-10 10:58:43 +08:00
|
|
|
|
private:
|
2021-11-10 13:44:29 +08:00
|
|
|
|
MediaSink::Ptr _sink;
|
2021-04-20 17:53:43 +08:00
|
|
|
|
TrackListener *_listener = nullptr;
|
2021-11-10 13:44:29 +08:00
|
|
|
|
vector<Track::Ptr> _origin_track;
|
2018-11-15 15:14:05 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif /* SRC_PLAYER_PLAYERBASE_H_ */
|