ZLMediaKit/src/Player/PlayerBase.h

372 lines
9.9 KiB
C
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
2017-04-01 16:35:56 +08:00
*
2017-09-27 16:20:30 +08:00
* 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.
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 "Player.h"
#include "Network/Socket.h"
2017-08-03 13:55:46 +08:00
#include "Util/mini.h"
2018-09-18 21:40:26 +08:00
#include "Common/MediaSource.h"
2018-09-19 12:34:29 +08:00
#include "Util/RingBuffer.h"
2017-04-25 11:35:41 +08:00
2017-04-01 16:35:56 +08:00
using namespace std;
2017-08-03 13:55:46 +08:00
using namespace ZL::Util;
2018-09-18 21:40:26 +08:00
using namespace ZL::Media;
2017-04-01 16:35:56 +08:00
using namespace ZL::Network;
namespace ZL {
namespace Player {
2018-09-19 12:34:29 +08:00
class TrackFrame : public Buffer {
public:
typedef std::shared_ptr<TrackFrame> Ptr;
virtual ~TrackFrame(){}
virtual uint32_t stamp() = 0;
};
class TrackFormat {
public:
typedef std::shared_ptr<TrackFormat> Ptr;
typedef RingBuffer<TrackFrame::Ptr> RingType;
typedef RingType::RingReader::Ptr ReaderType;
typedef enum {
VideoCodecInvalid = -1,
VideoCodecH264 = 0,
VideoCodecMax
} VideoCodecID;
typedef enum {
AudioCodecInvalid = -1,
AudioCodecAAC = 0,
AudioCodecMax
} AudioCodecID;
TrackFormat(){
_ring = std::make_shared<RingType>();
}
virtual ~TrackFormat(){}
virtual TrackType getTrackType() const = 0;
virtual int getCodecId() const = 0;
ReaderType attachReader(bool useBuffer = false){
return _ring->attach(useBuffer);
}
void writeFrame(const TrackFrame::Ptr &frame,bool keypos = true){
_ring->write(frame, keypos);
}
private:
RingType::Ptr _ring;
};
class VideoTrackFormat : public TrackFormat {
public:
TrackType getTrackType() const override { return TrackVideo;};
virtual int getVideoHeight() const = 0;
virtual int getVideoWidth() const = 0;
virtual float getVideoFps() const = 0;
};
class AudioTrackFormat : public TrackFormat {
public:
TrackType getTrackType() const override { return TrackAudio;};
virtual int getAudioSampleRate() const = 0;
virtual int getAudioSampleBit() const = 0;
virtual int getAudioChannel() const = 0;
};
class H264TrackFormat : public VideoTrackFormat{
public:
H264TrackFormat(const string &sps,const string &pps){
_sps = sps;
_pps = pps;
}
const string &getSps() const{
return _sps;
}
const string &getPps() const{
return _pps;
}
int getCodecId() const override{
return TrackFormat::VideoCodecH264;
}
private:
string _sps;
string _pps;
};
class AACTrackFormat : public AudioTrackFormat{
public:
AACTrackFormat(const string &aac_cfg){
_cfg = aac_cfg;
}
const string &getAacCfg() const{
return _cfg;
}
int getCodecId() const override{
return TrackFormat::AudioCodecAAC;
}
private:
string _cfg;
};
2018-09-18 23:49:48 +08:00
class MediaFormat {
public:
virtual ~MediaFormat(){};
virtual int getVideoHeight() const { return 0; };
virtual int getVideoWidth() const { return 0; };
virtual float getVideoFps() const { return 0; };
virtual int getAudioSampleRate() const { return 0; };
virtual int getAudioSampleBit() const { return 0; };
virtual int getAudioChannel() const { return 0; };
virtual const string& getPps() const { static string null;return null; };
virtual const string& getSps() const { static string null;return null; };
virtual const string& getAudioCfg() const { static string null;return null; };
};
class PlayerBase : public MediaFormat,public mINI{
2017-04-01 16:35:56 +08:00
public:
typedef std::shared_ptr<PlayerBase> Ptr;
typedef enum {
RTP_TCP = 0,
RTP_UDP = 1,
RTP_MULTICAST = 2,
} eRtpType;
static Ptr createPlayer(const char* strUrl);
//指定网卡ip
2018-05-21 23:18:17 +08:00
static const char kNetAdapter[];
//设置rtp传输类型可选项有0(tcp默认)、1(udp)、2(组播)
//设置方法:player[PlayerBase::kRtpType] = 0/1/2;
static const char kRtpType[];
//rtsp认证用户名
static const char kRtspUser[];
//rtsp认证用用户密码可以是明文也可以是md5,md5密码生成方式 md5(username:realm:password)
static const char kRtspPwd[];
//rtsp认证用用户密码是否为md5
static const char kRtspPwdIsMD5[];
2018-05-21 23:18:17 +08:00
2017-04-01 16:35:56 +08:00
PlayerBase(){};
virtual ~PlayerBase(){};
2017-08-03 13:55:46 +08:00
virtual void play(const char* strUrl) {};
2017-04-01 16:35:56 +08:00
virtual void pause(bool bPause) {};
virtual void teardown() {};
virtual void setOnShutdown( const function<void(const SockException &)> &cb) {};
virtual void setOnPlayResult( const function<void(const SockException &ex)> &cb) {};
virtual void setOnVideoCB( const function<void(const H264Frame &frame)> &cb) {};
virtual void setOnAudioCB( const function<void(const AdtsFrame &frame)> &cb) {};
2017-08-14 22:20:53 +08:00
virtual float getProgress() const { return 0;};
2017-04-01 16:35:56 +08:00
virtual void seekTo(float fProgress) {};
2018-09-18 21:40:26 +08:00
virtual void setMediaSouce(const MediaSource::Ptr & src) {};
2018-09-18 23:49:48 +08:00
virtual bool isInited() const { return true; };
//TrackVideo = 0, TrackAudio = 1
virtual float getRtpLossRate(int trackType) const {return 0; };
2018-09-19 12:34:29 +08:00
virtual float getDuration() const { return 0;};
virtual bool containAudio() const { return false; };
virtual bool containVideo() const { return false; };
virtual int getTrackCount() const { return 0;};
virtual TrackFormat::Ptr getTrack(int index) const {return nullptr;};
2017-04-01 16:35:56 +08:00
protected:
virtual void onShutdown(const SockException &ex) {};
virtual void onPlayResult(const SockException &ex) {};
};
template<typename Parent,typename Parser>
class PlayerImp : public Parent
{
public:
typedef std::shared_ptr<PlayerImp> Ptr;
PlayerImp(){};
virtual ~PlayerImp(){};
void setOnShutdown(const function<void(const SockException &)> &cb) override {
if (m_parser) {
m_parser->setOnShutdown(cb);
}
m_shutdownCB = cb;
}
void setOnPlayResult(const function<void(const SockException &ex)> &cb) override {
if (m_parser) {
m_parser->setOnPlayResult(cb);
}
m_playResultCB = cb;
}
void setOnVideoCB(const function<void(const H264Frame &frame)> &cb) override{
if (m_parser) {
m_parser->setOnVideoCB(cb);
}
m_onGetVideoCB = cb;
}
void setOnAudioCB(const function<void(const AdtsFrame &frame)> &cb) override{
if (m_parser) {
m_parser->setOnAudioCB(cb);
}
m_onGetAudioCB = cb;
}
int getVideoHeight() const override{
if (m_parser) {
return m_parser->getVideoHeight();
}
return PlayerBase::getVideoHeight();
}
int getVideoWidth() const override{
if (m_parser) {
return m_parser->getVideoWidth();
}
return PlayerBase::getVideoWidth();
}
float getVideoFps() const override{
if (m_parser) {
return m_parser->getVideoFps();
}
return PlayerBase::getVideoFps();
}
int getAudioSampleRate() const override{
if (m_parser) {
return m_parser->getAudioSampleRate();
}
return PlayerBase::getAudioSampleRate();
}
int getAudioSampleBit() const override{
if (m_parser) {
return m_parser->getAudioSampleBit();
}
return PlayerBase::getAudioSampleBit();
}
int getAudioChannel() const override{
if (m_parser) {
return m_parser->getAudioChannel();
}
return PlayerBase::getAudioChannel();
}
const string& getPps() const override{
if (m_parser) {
return m_parser->getPps();
}
return PlayerBase::getPps();
}
const string& getSps() const override{
if (m_parser) {
return m_parser->getSps();
}
return PlayerBase::getSps();
}
const string& getAudioCfg() const override{
if (m_parser) {
return m_parser->getAudioCfg();
}
return PlayerBase::getAudioCfg();
}
bool containAudio() const override{
if (m_parser) {
return m_parser->containAudio();
}
return PlayerBase::containAudio();
}
bool containVideo() const override{
if (m_parser) {
return m_parser->containVideo();
}
return PlayerBase::containVideo();
}
bool isInited() const override{
if (m_parser) {
return m_parser->isInited();
}
return PlayerBase::isInited();
}
float getDuration() const override {
if (m_parser) {
return m_parser->getDuration();
}
return PlayerBase::getDuration();
}
2017-08-14 22:20:53 +08:00
float getProgress() const override{
2017-04-01 16:35:56 +08:00
if (m_parser) {
2017-08-14 22:20:53 +08:00
return m_parser->getProgress();
2017-04-01 16:35:56 +08:00
}
2017-08-14 22:20:53 +08:00
return PlayerBase::getProgress();
2017-04-01 16:35:56 +08:00
};
void seekTo(float fProgress) override{
if (m_parser) {
return m_parser->seekTo(fProgress);
}
return PlayerBase::seekTo(fProgress);
};
2018-09-18 21:40:26 +08:00
void setMediaSouce(const MediaSource::Ptr & src) override {
if (m_parser) {
return m_parser->setMediaSouce(src);
}
m_pMediaSrc = src;
};
2017-04-01 16:35:56 +08:00
protected:
void onShutdown(const SockException &ex) override {
if (m_shutdownCB) {
m_shutdownCB(ex);
}
}
void onPlayResult(const SockException &ex) override {
if (m_playResultCB) {
m_playResultCB(ex);
m_playResultCB = nullptr;
}
}
2018-09-18 21:40:26 +08:00
protected:
2017-04-01 16:35:56 +08:00
function<void(const SockException &ex)> m_shutdownCB;
function<void(const SockException &ex)> m_playResultCB;
std::shared_ptr<Parser> m_parser;
function<void(const H264Frame &frame)> m_onGetVideoCB;
function<void(const AdtsFrame &frame)> m_onGetAudioCB;
2018-09-18 21:40:26 +08:00
MediaSource::Ptr m_pMediaSrc;
2017-04-01 16:35:56 +08:00
};
} /* namespace Player */
} /* namespace ZL */
#endif /* SRC_PLAYER_PLAYERBASE_H_ */