ZLMediaKit/src/Player/PlayerBase.h

282 lines
7.2 KiB
C++
Raw Normal View History

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
*
* 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"
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
class PlayerBase : public TrackSource, public mINI {
2018-10-25 15:45:38 +08:00
public:
using Ptr = std::shared_ptr<PlayerBase>;
using Event = std::function<void(const SockException &ex)>;
2018-10-25 15:45:38 +08:00
static Ptr createPlayer(const EventPoller::Ptr &poller, const string &strUrl);
2020-03-20 11:51:24 +08:00
PlayerBase();
~PlayerBase() override = default;
2020-03-20 11:51:24 +08:00
/**
*
* @param url urlrtsp/rtmp
2020-03-20 11:51:24 +08:00
*/
virtual void play(const string &url) {};
2020-03-20 11:51:24 +08:00
/**
*
* @param flag true:false:
*/
virtual void pause(bool flag) {};
/**
*
2020-03-20 11:51:24 +08:00
*/
virtual float getDuration() const { return 0; };
2020-03-20 11:51:24 +08:00
/**
*
* @param speed 1.0 2.0 0.5
*/
virtual void speed(float speed) {};
2020-03-20 11:51:24 +08:00
/**
*
*/
virtual void teardown() {};
2020-03-20 11:51:24 +08:00
/**
* 0.0 ~ 1.0
2020-03-20 11:51:24 +08:00
*/
virtual float getProgress() const { return 0; };
2020-03-20 11:51:24 +08:00
/**
* pos
2020-03-20 11:51:24 +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
/**
*
* @param progress 0.0 ~ 1.0
2019-05-08 15:27:37 +08:00
*/
virtual void seekTo(float progress) {};
2019-05-08 15:27:37 +08:00
2020-03-20 11:51:24 +08:00
/**
*
* @param pos
2020-03-20 11:51:24 +08:00
*/
virtual void seekTo(uint32_t pos) {};
2018-10-26 14:12:16 +08:00
/**
* rtsp
* @param type TrackInvalid时为总丢包率
*/
virtual float getPacketLossRate(TrackType type) const { return 0; };
2018-10-26 14:12:16 +08:00
/**
* track
2018-10-26 14:12:16 +08:00
*/
vector<Track::Ptr> getTracks(bool ready = true) const override { return vector<Track::Ptr>(); };
2018-10-26 11:03:53 +08:00
/**
* MediaSourcertsp/rtmp代理
*/
virtual void setMediaSource(const MediaSource::Ptr &src) = 0;
2018-10-26 14:12:16 +08:00
/**
*
2018-10-26 14:12:16 +08:00
*/
virtual void setOnShutdown(const Event &cb) = 0;
2018-10-26 14:12:16 +08:00
/**
*
2018-10-26 14:12:16 +08:00
*/
virtual void setOnPlayResult(const Event &cb) = 0;
2019-12-03 16:10:02 +08:00
/**
*
2019-12-03 16:10:02 +08:00
*/
virtual void setOnResume(const function<void()> &cb) = 0;
2017-04-01 16:35:56 +08:00
protected:
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
};
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:
using Ptr = std::shared_ptr<PlayerImp>;
2020-03-20 11:51:24 +08:00
template<typename ...ArgsType>
PlayerImp(ArgsType &&...args) : Parent(std::forward<ArgsType>(args)...) {}
~PlayerImp() override = default;
2020-03-20 11:51:24 +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
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
void speed(float speed) override {
return _delegate ? _delegate->speed(speed) : Parent::speed(speed);
2019-05-08 15:27:37 +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 {
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 {
return _delegate ? _delegate->getProgress() : Parent::getProgress();
2018-10-23 16:41:25 +08:00
}
2021-08-12 16:07:31 +08:00
uint32_t getProgressPos() const override {
return _delegate ? _delegate->getProgressPos() : Parent::getProgressPos();
}
2021-08-12 16:07:31 +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
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-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
}
_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
void setOnPlayResult(const function<void(const SockException &ex)> &cb) override {
2020-03-20 11:51:24 +08:00
if (_delegate) {
_delegate->setOnPlayResult(cb);
2020-03-20 11:51:24 +08:00
}
_on_play_result = cb;
2020-03-20 11:51:24 +08:00
}
2020-09-27 11:32:49 +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 {
if (_on_shutdown) {
_on_shutdown(ex);
_on_shutdown = nullptr;
2020-03-20 11:51:24 +08:00
}
}
void onPlayResult(const SockException &ex) override {
if (_on_play_result) {
_on_play_result(ex);
_on_play_result = nullptr;
2020-03-20 11:51:24 +08:00
}
}
void onResume() override {
if (_on_resume) {
_on_resume();
2019-05-08 15:27:37 +08:00
}
}
2018-09-18 21:40:26 +08:00
protected:
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
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;
};
class Demuxer : protected TrackListener, public TrackSource {
public:
2021-04-20 17:53:43 +08:00
Demuxer() = default;
~Demuxer() override = default;
2020-03-20 11:51:24 +08:00
void setTrackListener(TrackListener *listener, bool wait_track_ready = false);
vector<Track::Ptr> getTracks(bool trackReady = true) const override;
protected:
bool addTrack(const Track::Ptr &track) override;
void addTrackCompleted() override;
2021-04-20 17:53:43 +08:00
void resetTracks() override;
private:
MediaSink::Ptr _sink;
2021-04-20 17:53:43 +08:00
TrackListener *_listener = nullptr;
vector<Track::Ptr> _origin_track;
};
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2017-04-01 16:35:56 +08:00
#endif /* SRC_PLAYER_PLAYERBASE_H_ */