mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-04 01:37:33 +08:00
初始提交
This commit is contained in:
57
src/Player/MediaPlayer.cpp
Normal file
57
src/Player/MediaPlayer.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* MediaPlayer.cpp
|
||||
*
|
||||
* Created on: 2016年12月5日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#include "Rtmp/RtmpPlayerImp.h"
|
||||
#include "Rtsp/RtspPlayerImp.h"
|
||||
#include "MediaPlayer.h"
|
||||
#include <algorithm>
|
||||
|
||||
using namespace ZL::Rtmp;
|
||||
using namespace ZL::Rtsp;
|
||||
|
||||
namespace ZL {
|
||||
namespace Player {
|
||||
|
||||
MediaPlayer::MediaPlayer() {
|
||||
}
|
||||
|
||||
MediaPlayer::~MediaPlayer() {
|
||||
if(!EventPoller::Instance().isMainThread()){
|
||||
FatalL << "未在主线程释放";
|
||||
}
|
||||
teardown();
|
||||
}
|
||||
|
||||
void MediaPlayer::play(const char* strUrl, const char* strUser, const char* strPwd, eRtpType eType) {
|
||||
string strPrefix = FindField(strUrl, NULL, "://");
|
||||
if ((strcasecmp(m_strPrefix.data(),strPrefix.data()) != 0) || strPrefix.empty()) {
|
||||
//协议切换
|
||||
m_strPrefix = strPrefix;
|
||||
m_parser = PlayerBase::createPlayer(strUrl);
|
||||
m_parser->setOnShutdown(m_shutdownCB);
|
||||
m_parser->setOnVideoCB(m_onGetVideoCB);
|
||||
m_parser->setOnAudioCB(m_onGetAudioCB);
|
||||
}
|
||||
m_parser->setOnPlayResult(m_playResultCB);
|
||||
m_parser->play(strUrl, strUser, strPwd, eType);
|
||||
}
|
||||
|
||||
void MediaPlayer::pause(bool bPause) {
|
||||
if (m_parser) {
|
||||
m_parser->pause(bPause);
|
||||
}
|
||||
}
|
||||
|
||||
void MediaPlayer::teardown() {
|
||||
if (m_parser) {
|
||||
m_parser->teardown();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} /* namespace Player */
|
||||
} /* namespace ZL */
|
||||
39
src/Player/MediaPlayer.h
Normal file
39
src/Player/MediaPlayer.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* MediaPlayer.h
|
||||
*
|
||||
* Created on: 2016年12月5日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#ifndef SRC_PLAYER_MEDIAPLAYER_H_
|
||||
#define SRC_PLAYER_MEDIAPLAYER_H_
|
||||
|
||||
#include "Player.h"
|
||||
#include "PlayerBase.h"
|
||||
#include <string>
|
||||
#include <memory>
|
||||
using namespace std;
|
||||
|
||||
|
||||
namespace ZL {
|
||||
namespace Player {
|
||||
|
||||
class MediaPlayer : public PlayerImp<PlayerBase,PlayerBase> {
|
||||
public:
|
||||
typedef std::shared_ptr<MediaPlayer> Ptr;
|
||||
|
||||
MediaPlayer();
|
||||
virtual ~MediaPlayer();
|
||||
|
||||
void play(const char* strUrl, const char *strUser = "", const char *strPwd = "", eRtpType eType = RTP_TCP) override;
|
||||
void pause(bool bPause) override;
|
||||
void teardown() override;
|
||||
private:
|
||||
string m_strPrefix;
|
||||
|
||||
};
|
||||
|
||||
} /* namespace Player */
|
||||
} /* namespace ZL */
|
||||
|
||||
#endif /* SRC_PLAYER_MEDIAPLAYER_H_ */
|
||||
118
src/Player/Player.cpp
Normal file
118
src/Player/Player.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Player.cpp
|
||||
*
|
||||
* Created on: 2016年12月2日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#include "Player.h"
|
||||
#include "H264/SPSParser.h"
|
||||
#include <cstring>
|
||||
#include "Util/logger.h"
|
||||
using namespace ZL::Util;
|
||||
|
||||
|
||||
static unsigned const samplingFrequencyTable[16] = { 96000, 88200,
|
||||
64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025,
|
||||
8000, 7350, 0, 0, 0 };
|
||||
|
||||
void writeAdtsHeader(const AdtsFrame &hed, uint8_t *pcAdts) {
|
||||
pcAdts[0] = (hed.syncword >> 4 & 0xFF); //8bit
|
||||
pcAdts[1] = (hed.syncword << 4 & 0xF0); //4 bit
|
||||
pcAdts[1] |= (hed.id << 3 & 0x08); //1 bit
|
||||
pcAdts[1] |= (hed.layer << 1 & 0x06); //2bit
|
||||
pcAdts[1] |= (hed.protection_absent & 0x01); //1 bit
|
||||
|
||||
pcAdts[2] = (hed.profile << 6 & 0xC0); // 2 bit
|
||||
pcAdts[2] |= (hed.sf_index << 2 & 0x3C); //4bit
|
||||
pcAdts[2] |= (hed.private_bit << 1 & 0x02); //1 bit
|
||||
pcAdts[2] |= (hed.channel_configuration >> 2 & 0x03); //1 bit
|
||||
|
||||
pcAdts[3] = (hed.channel_configuration << 6 & 0xC0); // 2 bit
|
||||
pcAdts[3] |= (hed.original << 5 & 0x20); //1 bit
|
||||
pcAdts[3] |= (hed.home << 4 & 0x10); //1 bit
|
||||
pcAdts[3] |= (hed.copyright_identification_bit << 3 & 0x08); //1 bit
|
||||
pcAdts[3] |= (hed.copyright_identification_start << 2 & 0x04); //1 bit
|
||||
pcAdts[3] |= (hed.aac_frame_length >> 11 & 0x03); //2 bit
|
||||
|
||||
pcAdts[4] = (hed.aac_frame_length >> 3 & 0xFF); //8 bit
|
||||
|
||||
pcAdts[5] = (hed.aac_frame_length << 5 & 0xE0); //3 bit
|
||||
pcAdts[5] |= (hed.adts_buffer_fullness >> 6 & 0x1F); //5 bit
|
||||
|
||||
pcAdts[6] = (hed.adts_buffer_fullness << 2 & 0xFC); //6 bit
|
||||
pcAdts[6] |= (hed.no_raw_data_blocks_in_frame & 0x03); //2 bit
|
||||
}
|
||||
string makeAdtsConfig(const uint8_t *pcAdts){
|
||||
if (!(pcAdts[0] == 0xFF && (pcAdts[1] & 0xF0) == 0xF0)) {
|
||||
return "";
|
||||
}
|
||||
// Get and check the 'profile':
|
||||
unsigned char profile = (pcAdts[2] & 0xC0) >> 6; // 2 bits
|
||||
if (profile == 3) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Get and check the 'sampling_frequency_index':
|
||||
unsigned char sampling_frequency_index = (pcAdts[2] & 0x3C) >> 2; // 4 bits
|
||||
if (samplingFrequencyTable[sampling_frequency_index] == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Get and check the 'channel_configuration':
|
||||
unsigned char channel_configuration = ((pcAdts[2] & 0x01) << 2)
|
||||
| ((pcAdts[3] & 0xC0) >> 6); // 3 bits
|
||||
|
||||
unsigned char audioSpecificConfig[2];
|
||||
unsigned char const audioObjectType = profile + 1;
|
||||
audioSpecificConfig[0] = (audioObjectType << 3) | (sampling_frequency_index >> 1);
|
||||
audioSpecificConfig[1] = (sampling_frequency_index << 7) | (channel_configuration << 3);
|
||||
return string((char *)audioSpecificConfig,2);
|
||||
}
|
||||
void makeAdtsHeader(const string &strAudioCfg,AdtsFrame &adts) {
|
||||
uint8_t cfg1 = strAudioCfg[0];
|
||||
uint8_t cfg2 = strAudioCfg[1];
|
||||
|
||||
int audioObjectType;
|
||||
int sampling_frequency_index;
|
||||
int channel_configuration;
|
||||
|
||||
audioObjectType = cfg1 >> 3;
|
||||
sampling_frequency_index = ((cfg1 & 0x07) << 1) | (cfg2 >> 7);
|
||||
channel_configuration = (cfg2 & 0x7F) >> 3;
|
||||
|
||||
adts.syncword = 0x0FFF;
|
||||
adts.id = 0;
|
||||
adts.layer = 0;
|
||||
adts.protection_absent = 1;
|
||||
adts.profile = audioObjectType - 1;
|
||||
adts.sf_index = sampling_frequency_index;
|
||||
adts.private_bit = 0;
|
||||
adts.channel_configuration = channel_configuration;
|
||||
adts.original = 0;
|
||||
adts.home = 0;
|
||||
adts.copyright_identification_bit = 0;
|
||||
adts.copyright_identification_start = 0;
|
||||
adts.aac_frame_length = 7;
|
||||
adts.adts_buffer_fullness = 2047;
|
||||
adts.no_raw_data_blocks_in_frame = 0;
|
||||
}
|
||||
void getAACInfo(const AdtsFrame &adts,int &iSampleRate,int &iChannel){
|
||||
iSampleRate = samplingFrequencyTable[adts.sf_index];
|
||||
iChannel = adts.channel_configuration;
|
||||
}
|
||||
bool getAVCInfo(const string& strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps) {
|
||||
T_GetBitContext tGetBitBuf;
|
||||
T_SPS tH264SpsInfo;
|
||||
memset(&tGetBitBuf,0,sizeof(tGetBitBuf));
|
||||
memset(&tH264SpsInfo,0,sizeof(tH264SpsInfo));
|
||||
tGetBitBuf.pu8Buf = (uint8_t *)strSps.data() + 1;
|
||||
tGetBitBuf.iBufSize = strSps.size() - 1;
|
||||
if(0 != h264DecSeqParameterSet((void *) &tGetBitBuf, &tH264SpsInfo)){
|
||||
return false;
|
||||
}
|
||||
h264GetWidthHeight(&tH264SpsInfo, &iVideoWidth, &iVideoHeight);
|
||||
h264GeFramerate(&tH264SpsInfo, &iVideoFps);
|
||||
//FatalL << iVideoWidth << " " << iVideoHeight << " " << iVideoFps;
|
||||
return true;
|
||||
}
|
||||
53
src/Player/Player.h
Normal file
53
src/Player/Player.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Player.h
|
||||
*
|
||||
* Created on: 2016年12月2日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#ifndef SRC_PLAYER_PLAYER_H_
|
||||
#define SRC_PLAYER_PLAYER_H_
|
||||
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
typedef struct {
|
||||
uint16_t sequence;
|
||||
uint32_t timeStamp;
|
||||
unsigned char type;
|
||||
string data;
|
||||
} H264Frame;
|
||||
|
||||
//ADTS 头中相对有用的信息 采样率、声道数、帧长度
|
||||
typedef struct {
|
||||
unsigned int syncword; //12 bslbf 同步字The bit string ‘1111 1111 1111’,说明一个ADTS帧的开始
|
||||
unsigned int id; //1 bslbf MPEG 标示符, 设置为1
|
||||
unsigned int layer; //2 uimsbf Indicates which layer is used. Set to ‘00’
|
||||
unsigned int protection_absent; //1 bslbf 表示是否误码校验
|
||||
unsigned int profile; //2 uimsbf 表示使用哪个级别的AAC,如01 Low Complexity(LC)--- AACLC
|
||||
unsigned int sf_index; //4 uimsbf 表示使用的采样率下标
|
||||
unsigned int private_bit; //1 bslbf
|
||||
unsigned int channel_configuration; //3 uimsbf 表示声道数
|
||||
unsigned int original; //1 bslbf
|
||||
unsigned int home; //1 bslbf
|
||||
//下面的为改变的参数即每一帧都不同
|
||||
unsigned int copyright_identification_bit; //1 bslbf
|
||||
unsigned int copyright_identification_start; //1 bslbf
|
||||
unsigned int aac_frame_length; // 13 bslbf 一个ADTS帧的长度包括ADTS头和raw data block
|
||||
unsigned int adts_buffer_fullness; //11 bslbf 0x7FF 说明是码率可变的码流
|
||||
//no_raw_data_blocks_in_frame 表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧.
|
||||
//所以说number_of_raw_data_blocks_in_frame == 0
|
||||
//表示说ADTS帧中有一个AAC数据块并不是说没有。(一个AAC原始帧包含一段时间内1024个采样及相关数据)
|
||||
unsigned int no_raw_data_blocks_in_frame; //2 uimsfb
|
||||
unsigned char data[2 * 1024 + 7];
|
||||
uint16_t sequence;
|
||||
uint32_t timeStamp;
|
||||
} AdtsFrame;
|
||||
|
||||
void makeAdtsHeader(const string &strAudioCfg,AdtsFrame &adts);
|
||||
void writeAdtsHeader(const AdtsFrame &adts, uint8_t *pcAdts) ;
|
||||
string makeAdtsConfig(const uint8_t *pcAdts);
|
||||
void getAACInfo(const AdtsFrame &adts,int &iSampleRate,int &iChannel);
|
||||
bool getAVCInfo(const string &strSps,int &iVideoWidth, int &iVideoHeight, float &iVideoFps);
|
||||
|
||||
#endif /* SRC_PLAYER_PLAYER_H_ */
|
||||
34
src/Player/PlayerBase.cpp
Normal file
34
src/Player/PlayerBase.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* PlayerBase.cpp
|
||||
*
|
||||
* Created on: 2016年12月1日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#include "PlayerBase.h"
|
||||
#include "Rtmp/RtmpPlayerImp.h"
|
||||
#include "Rtsp/RtspPlayerImp.h"
|
||||
#include "Rtsp/Rtsp.h"
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
using namespace ZL::Rtmp;
|
||||
using namespace ZL::Rtsp;
|
||||
|
||||
namespace ZL {
|
||||
namespace Player {
|
||||
|
||||
|
||||
PlayerBase::Ptr PlayerBase::createPlayer(const char* strUrl) {
|
||||
string prefix = FindField(strUrl, NULL, "://");
|
||||
if (strcasecmp("rtsp",prefix.data()) == 0) {
|
||||
return PlayerBase::Ptr(new RtspPlayerImp());
|
||||
}
|
||||
if (strcasecmp("rtmp",prefix.data()) == 0) {
|
||||
return PlayerBase::Ptr(new RtmpPlayerImp());
|
||||
}
|
||||
return PlayerBase::Ptr(new RtspPlayerImp());
|
||||
}
|
||||
|
||||
} /* namespace Player */
|
||||
} /* namespace ZL */
|
||||
219
src/Player/PlayerBase.h
Normal file
219
src/Player/PlayerBase.h
Normal file
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
* PlayerBase.h
|
||||
*
|
||||
* Created on: 2016年12月1日
|
||||
* Author: xzl
|
||||
*/
|
||||
|
||||
#ifndef SRC_PLAYER_PLAYERBASE_H_
|
||||
#define SRC_PLAYER_PLAYERBASE_H_
|
||||
|
||||
#include "Player.h"
|
||||
#include "Network/Socket.hpp"
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
using namespace ZL::Network;
|
||||
|
||||
namespace ZL {
|
||||
namespace Player {
|
||||
|
||||
|
||||
class PlayerBase{
|
||||
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);
|
||||
|
||||
PlayerBase(){};
|
||||
virtual ~PlayerBase(){};
|
||||
|
||||
virtual void play(const char* strUrl, const char *strUser = "", const char *strPwd = "", eRtpType eType = RTP_TCP) {};
|
||||
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) {};
|
||||
|
||||
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 float getRtpLossRate(int iTrackId) 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; };
|
||||
virtual bool containAudio() const { return false; };
|
||||
virtual bool containVideo() const { return false; };
|
||||
virtual bool isInited() const { return true; };
|
||||
virtual float getDuration() const { return 0;};
|
||||
virtual float getProgresss() const { return 0;};
|
||||
virtual void seekTo(float fProgress) {};
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
float getProgresss() const override{
|
||||
if (m_parser) {
|
||||
return m_parser->getProgresss();
|
||||
}
|
||||
return PlayerBase::getProgresss();
|
||||
};
|
||||
void seekTo(float fProgress) override{
|
||||
if (m_parser) {
|
||||
return m_parser->seekTo(fProgress);
|
||||
}
|
||||
return PlayerBase::seekTo(fProgress);
|
||||
};
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
};
|
||||
} /* namespace Player */
|
||||
} /* namespace ZL */
|
||||
|
||||
#endif /* SRC_PLAYER_PLAYERBASE_H_ */
|
||||
Reference in New Issue
Block a user