mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-10-31 00:37:39 +08:00
优化播放器接口
This commit is contained in:
parent
fcfb434cf2
commit
afc36f5ef4
@ -18,21 +18,16 @@ using namespace ZL::Thread;
|
|||||||
namespace ZL {
|
namespace ZL {
|
||||||
namespace DEV {
|
namespace DEV {
|
||||||
|
|
||||||
|
const char PlayerProxy::kAliveSecond[] = "alive_second";
|
||||||
|
|
||||||
PlayerProxy::PlayerProxy(const char *strApp,const char *strSrc){
|
PlayerProxy::PlayerProxy(const char *strApp,const char *strSrc){
|
||||||
m_strApp = strApp;
|
m_strApp = strApp;
|
||||||
m_strSrc = strSrc;
|
m_strSrc = strSrc;
|
||||||
}
|
}
|
||||||
|
void PlayerProxy::play(const char* strUrl) {
|
||||||
void PlayerProxy::play(const char* strUrl, const char *strUser,
|
m_aliveSecond = (*this)[kAliveSecond];
|
||||||
const char *strPwd, PlayerBase::eRtpType eType, uint32_t iSecond) {
|
|
||||||
m_aliveSecond = iSecond;
|
|
||||||
string strUrlTmp(strUrl);
|
|
||||||
string strUserTmp(strUser);
|
|
||||||
string strPwdTmp(strPwd);
|
|
||||||
|
|
||||||
m_pPlayer.reset(new MediaPlayer());
|
|
||||||
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
||||||
m_pPlayer->setOnVideoCB( [weakSelf,strUrlTmp](const H264Frame &data ) {
|
setOnVideoCB( [weakSelf](const H264Frame &data ) {
|
||||||
auto strongSelf = weakSelf.lock();
|
auto strongSelf = weakSelf.lock();
|
||||||
if(!strongSelf){
|
if(!strongSelf){
|
||||||
return;
|
return;
|
||||||
@ -44,7 +39,7 @@ void PlayerProxy::play(const char* strUrl, const char *strUser,
|
|||||||
}
|
}
|
||||||
strongSelf->checkExpired();
|
strongSelf->checkExpired();
|
||||||
});
|
});
|
||||||
m_pPlayer->setOnAudioCB( [weakSelf,strUrlTmp](const AdtsFrame &data ) {
|
setOnAudioCB( [weakSelf](const AdtsFrame &data ) {
|
||||||
auto strongSelf = weakSelf.lock();
|
auto strongSelf = weakSelf.lock();
|
||||||
if(!strongSelf){
|
if(!strongSelf){
|
||||||
return;
|
return;
|
||||||
@ -58,7 +53,8 @@ void PlayerProxy::play(const char* strUrl, const char *strUser,
|
|||||||
});
|
});
|
||||||
|
|
||||||
std::shared_ptr<uint64_t> piFailedCnt(new uint64_t(0)); //连续播放失败次数
|
std::shared_ptr<uint64_t> piFailedCnt(new uint64_t(0)); //连续播放失败次数
|
||||||
m_pPlayer->setOnPlayResult([weakSelf,strUrlTmp,strUserTmp,strPwdTmp,eType,piFailedCnt](const SockException &err) {
|
string strUrlTmp(strUrl);
|
||||||
|
setOnPlayResult([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
|
||||||
auto strongSelf = weakSelf.lock();
|
auto strongSelf = weakSelf.lock();
|
||||||
if(!strongSelf) {
|
if(!strongSelf) {
|
||||||
return;
|
return;
|
||||||
@ -69,13 +65,12 @@ void PlayerProxy::play(const char* strUrl, const char *strUser,
|
|||||||
*piFailedCnt = 0;//连续播放失败次数清0
|
*piFailedCnt = 0;//连续播放失败次数清0
|
||||||
}else if(*piFailedCnt < replayCnt) {
|
}else if(*piFailedCnt < replayCnt) {
|
||||||
// 播放失败,延时重试播放
|
// 播放失败,延时重试播放
|
||||||
strongSelf->rePlay(strUrlTmp, strUserTmp, strPwdTmp, eType,(*piFailedCnt)++);
|
strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
|
||||||
}else{
|
}else{
|
||||||
strongSelf->expired();
|
strongSelf->expired();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
weak_ptr<MediaPlayer> weakPtr= m_pPlayer;
|
setOnShutdown([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
|
||||||
m_pPlayer->setOnShutdown([weakSelf,weakPtr,strUrlTmp,strUserTmp,strPwdTmp,eType,piFailedCnt](const SockException &err) {
|
|
||||||
auto strongSelf = weakSelf.lock();
|
auto strongSelf = weakSelf.lock();
|
||||||
if(!strongSelf) {
|
if(!strongSelf) {
|
||||||
return;
|
return;
|
||||||
@ -86,52 +81,52 @@ void PlayerProxy::play(const char* strUrl, const char *strUser,
|
|||||||
//播放异常中断,延时重试播放
|
//播放异常中断,延时重试播放
|
||||||
static uint64_t replayCnt = mINI::Instance()[Config::Proxy::kReplayCount].as<uint64_t>();
|
static uint64_t replayCnt = mINI::Instance()[Config::Proxy::kReplayCount].as<uint64_t>();
|
||||||
if(*piFailedCnt < replayCnt) {
|
if(*piFailedCnt < replayCnt) {
|
||||||
strongSelf->rePlay(strUrlTmp, strUserTmp, strPwdTmp, eType,(*piFailedCnt)++);
|
strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
|
||||||
}else{
|
}else{
|
||||||
strongSelf->expired();
|
strongSelf->expired();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
m_pPlayer->play(strUrl, strUser, strPwd, eType);
|
MediaPlayer::play(strUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerProxy::~PlayerProxy() {
|
PlayerProxy::~PlayerProxy() {
|
||||||
auto iTaskId = reinterpret_cast<uint64_t>(this);
|
auto iTaskId = reinterpret_cast<uint64_t>(this);
|
||||||
AsyncTaskThread::Instance().CancelTask(iTaskId);
|
AsyncTaskThread::Instance().CancelTask(iTaskId);
|
||||||
}
|
}
|
||||||
void PlayerProxy::rePlay(const string &strUrl, const string &strUser, const string &strPwd, PlayerBase::eRtpType eType, uint64_t iFailedCnt){
|
void PlayerProxy::rePlay(const string &strUrl,uint64_t iFailedCnt){
|
||||||
checkExpired();
|
checkExpired();
|
||||||
auto iTaskId = reinterpret_cast<uint64_t>(this);
|
auto iTaskId = reinterpret_cast<uint64_t>(this);
|
||||||
auto iDelay = MAX((uint64_t)2 * 1000, MIN(iFailedCnt * 3000,(uint64_t)60*1000));
|
auto iDelay = MAX((uint64_t)2 * 1000, MIN(iFailedCnt * 3000,(uint64_t)60*1000));
|
||||||
weak_ptr<MediaPlayer> weakPtr = m_pPlayer;
|
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
||||||
AsyncTaskThread::Instance().CancelTask(iTaskId);
|
AsyncTaskThread::Instance().CancelTask(iTaskId);
|
||||||
AsyncTaskThread::Instance().DoTaskDelay(iTaskId, iDelay, [weakPtr,strUrl,strUser,strPwd,eType,iFailedCnt]() {
|
AsyncTaskThread::Instance().DoTaskDelay(iTaskId, iDelay, [weakSelf,strUrl,iFailedCnt]() {
|
||||||
//播放失败次数越多,则延时越长
|
//播放失败次数越多,则延时越长
|
||||||
auto strongPlayer = weakPtr.lock();
|
auto strongPlayer = weakSelf.lock();
|
||||||
if(!strongPlayer) {
|
if(!strongPlayer) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
|
WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
|
||||||
strongPlayer->play(strUrl.data(), strUser.data(), strPwd.data(), eType);
|
strongPlayer->MediaPlayer::play(strUrl.data());
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
void PlayerProxy::initMedia() {
|
void PlayerProxy::initMedia() {
|
||||||
if (!m_pPlayer->isInited()) {
|
if (!isInited()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_pChn.reset(new DevChannel(m_strApp.data(),m_strSrc.data(),m_pPlayer->getDuration()));
|
m_pChn.reset(new DevChannel(m_strApp.data(),m_strSrc.data(),getDuration()));
|
||||||
if (m_pPlayer->containVideo()) {
|
if (containVideo()) {
|
||||||
VideoInfo info;
|
VideoInfo info;
|
||||||
info.iFrameRate = m_pPlayer->getVideoFps();
|
info.iFrameRate = getVideoFps();
|
||||||
info.iWidth = m_pPlayer->getVideoWidth();
|
info.iWidth = getVideoWidth();
|
||||||
info.iHeight = m_pPlayer->getVideoHeight();
|
info.iHeight = getVideoHeight();
|
||||||
m_pChn->initVideo(info);
|
m_pChn->initVideo(info);
|
||||||
}
|
}
|
||||||
if (m_pPlayer->containAudio()) {
|
if (containAudio()) {
|
||||||
AudioInfo info;
|
AudioInfo info;
|
||||||
info.iSampleRate = m_pPlayer->getAudioSampleRate();
|
info.iSampleRate = getAudioSampleRate();
|
||||||
info.iChannel = m_pPlayer->getAudioChannel();
|
info.iChannel = getAudioChannel();
|
||||||
info.iSampleBit = m_pPlayer->getAudioSampleBit();
|
info.iSampleBit = getAudioSampleBit();
|
||||||
m_pChn->initAudio(info);
|
m_pChn->initAudio(info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,17 +19,20 @@ using namespace ZL::Player;
|
|||||||
namespace ZL {
|
namespace ZL {
|
||||||
namespace DEV {
|
namespace DEV {
|
||||||
|
|
||||||
class PlayerProxy : public std::enable_shared_from_this<PlayerProxy>{
|
class PlayerProxy :public MediaPlayer, public std::enable_shared_from_this<PlayerProxy>{
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<PlayerProxy> Ptr;
|
typedef std::shared_ptr<PlayerProxy> Ptr;
|
||||||
|
//设置代理时间,0为永久,其他为代理秒数
|
||||||
|
//设置方法:proxy[PlayerProxy::kAliveSecond] = 100;
|
||||||
|
static const char kAliveSecond[];
|
||||||
|
|
||||||
PlayerProxy(const char *strApp, const char *strSrc);
|
PlayerProxy(const char *strApp, const char *strSrc);
|
||||||
void play(const char* strUrl, const char *strUser = "", const char *strPwd = "",PlayerBase::eRtpType eType = PlayerBase::RTP_TCP,uint32_t iSecond = 0);
|
|
||||||
virtual ~PlayerProxy();
|
virtual ~PlayerProxy();
|
||||||
|
void play(const char* strUrl) override;
|
||||||
void setOnExpired(const function<void()> &cb){
|
void setOnExpired(const function<void()> &cb){
|
||||||
onExpired = cb;
|
onExpired = cb;
|
||||||
}
|
}
|
||||||
private :
|
private :
|
||||||
MediaPlayer::Ptr m_pPlayer;
|
|
||||||
DevChannel::Ptr m_pChn;
|
DevChannel::Ptr m_pChn;
|
||||||
Ticker m_aliveTicker;
|
Ticker m_aliveTicker;
|
||||||
uint32_t m_aliveSecond = 0;
|
uint32_t m_aliveSecond = 0;
|
||||||
@ -37,7 +40,7 @@ private :
|
|||||||
string m_strApp;
|
string m_strApp;
|
||||||
string m_strSrc;
|
string m_strSrc;
|
||||||
void initMedia();
|
void initMedia();
|
||||||
void rePlay(const string &strUrl, const string &strUser, const string &strPwd, PlayerBase::eRtpType eType,uint64_t iFailedCnt);
|
void rePlay(const string &strUrl,uint64_t iFailedCnt);
|
||||||
void checkExpired();
|
void checkExpired();
|
||||||
void expired();
|
void expired();
|
||||||
};
|
};
|
||||||
|
@ -22,8 +22,7 @@ MediaPlayer::MediaPlayer() {
|
|||||||
MediaPlayer::~MediaPlayer() {
|
MediaPlayer::~MediaPlayer() {
|
||||||
teardown();
|
teardown();
|
||||||
}
|
}
|
||||||
|
void MediaPlayer::play(const char* strUrl) {
|
||||||
void MediaPlayer::play(const char* strUrl, const char* strUser, const char* strPwd, eRtpType eType) {
|
|
||||||
string strPrefix = FindField(strUrl, NULL, "://");
|
string strPrefix = FindField(strUrl, NULL, "://");
|
||||||
if ((strcasecmp(m_strPrefix.data(),strPrefix.data()) != 0) || strPrefix.empty()) {
|
if ((strcasecmp(m_strPrefix.data(),strPrefix.data()) != 0) || strPrefix.empty()) {
|
||||||
//协议切换
|
//协议切换
|
||||||
@ -34,9 +33,11 @@ void MediaPlayer::play(const char* strUrl, const char* strUser, const char* strP
|
|||||||
m_parser->setOnAudioCB(m_onGetAudioCB);
|
m_parser->setOnAudioCB(m_onGetAudioCB);
|
||||||
}
|
}
|
||||||
m_parser->setOnPlayResult(m_playResultCB);
|
m_parser->setOnPlayResult(m_playResultCB);
|
||||||
m_parser->play(strUrl, strUser, strPwd, eType);
|
m_parser->mINI::operator=(*this);
|
||||||
|
m_parser->play(strUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MediaPlayer::pause(bool bPause) {
|
void MediaPlayer::pause(bool bPause) {
|
||||||
if (m_parser) {
|
if (m_parser) {
|
||||||
m_parser->pause(bPause);
|
m_parser->pause(bPause);
|
||||||
|
@ -12,8 +12,12 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
#include "PlayerBase.h"
|
#include "PlayerBase.h"
|
||||||
|
#include "Rtsp/RtspPlayer.h"
|
||||||
|
#include "Rtmp/RtmpPlayer.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
using namespace ZL::Rtsp;
|
||||||
|
using namespace ZL::Rtmp;
|
||||||
|
|
||||||
namespace ZL {
|
namespace ZL {
|
||||||
namespace Player {
|
namespace Player {
|
||||||
@ -24,8 +28,7 @@ public:
|
|||||||
|
|
||||||
MediaPlayer();
|
MediaPlayer();
|
||||||
virtual ~MediaPlayer();
|
virtual ~MediaPlayer();
|
||||||
|
void play(const char* strUrl) override;
|
||||||
void play(const char* strUrl, const char *strUser = "", const char *strPwd = "", eRtpType eType = RTP_TCP) override;
|
|
||||||
void pause(bool bPause) override;
|
void pause(bool bPause) override;
|
||||||
void teardown() override;
|
void teardown() override;
|
||||||
private:
|
private:
|
||||||
|
@ -8,19 +8,22 @@
|
|||||||
#ifndef SRC_PLAYER_PLAYERBASE_H_
|
#ifndef SRC_PLAYER_PLAYERBASE_H_
|
||||||
#define SRC_PLAYER_PLAYERBASE_H_
|
#define SRC_PLAYER_PLAYERBASE_H_
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
#include "Network/Socket.h"
|
#include "Network/Socket.h"
|
||||||
|
#include "Util/mini.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
using namespace ZL::Util;
|
||||||
using namespace ZL::Network;
|
using namespace ZL::Network;
|
||||||
|
|
||||||
namespace ZL {
|
namespace ZL {
|
||||||
namespace Player {
|
namespace Player {
|
||||||
|
|
||||||
class PlayerBase{
|
class PlayerBase : public mINI{
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<PlayerBase> Ptr;
|
typedef std::shared_ptr<PlayerBase> Ptr;
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@ -32,8 +35,7 @@ public:
|
|||||||
|
|
||||||
PlayerBase(){};
|
PlayerBase(){};
|
||||||
virtual ~PlayerBase(){};
|
virtual ~PlayerBase(){};
|
||||||
|
virtual void play(const char* strUrl) {};
|
||||||
virtual void play(const char* strUrl, const char *strUser = "", const char *strPwd = "", eRtpType eType = RTP_TCP) {};
|
|
||||||
virtual void pause(bool bPause) {};
|
virtual void pause(bool bPause) {};
|
||||||
virtual void teardown() {};
|
virtual void teardown() {};
|
||||||
|
|
||||||
|
@ -52,12 +52,12 @@ void RtmpPlayer::teardown() {
|
|||||||
m_fSeekTo = 0;
|
m_fSeekTo = 0;
|
||||||
CLEAR_ARR(m_adFistStamp);
|
CLEAR_ARR(m_adFistStamp);
|
||||||
CLEAR_ARR(m_adNowStamp);
|
CLEAR_ARR(m_adNowStamp);
|
||||||
clear();
|
reset();
|
||||||
shutdown();
|
shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RtmpPlayer::play(const char* strUrl, const char * , const char *, eRtpType) {
|
void RtmpPlayer::play(const char* strUrl) {
|
||||||
teardown();
|
teardown();
|
||||||
string strHost = FindField(strUrl, "://", "/");
|
string strHost = FindField(strUrl, "://", "/");
|
||||||
m_strApp = FindField(strUrl, (strHost + "/").data(), "/");
|
m_strApp = FindField(strUrl, (strHost + "/").data(), "/");
|
||||||
|
@ -36,8 +36,7 @@ public:
|
|||||||
RtmpPlayer();
|
RtmpPlayer();
|
||||||
virtual ~RtmpPlayer();
|
virtual ~RtmpPlayer();
|
||||||
|
|
||||||
void play(const char* strUrl, const char *strUser, const char *strPwd,
|
void play(const char* strUrl) override;
|
||||||
eRtpType eType) override;
|
|
||||||
void pause(bool bPause) override;
|
void pause(bool bPause) override;
|
||||||
void teardown() override;
|
void teardown() override;
|
||||||
protected:
|
protected:
|
||||||
|
@ -48,9 +48,9 @@ RtmpProtocol::RtmpProtocol() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
RtmpProtocol::~RtmpProtocol() {
|
RtmpProtocol::~RtmpProtocol() {
|
||||||
clear();
|
reset();
|
||||||
}
|
}
|
||||||
void RtmpProtocol::clear() {
|
void RtmpProtocol::reset() {
|
||||||
////////////ChunkSize////////////
|
////////////ChunkSize////////////
|
||||||
m_iChunkLenIn = DEFAULT_CHUNK_LEN;
|
m_iChunkLenIn = DEFAULT_CHUNK_LEN;
|
||||||
m_iChunkLenOut = DEFAULT_CHUNK_LEN;
|
m_iChunkLenOut = DEFAULT_CHUNK_LEN;
|
||||||
|
@ -33,7 +33,7 @@ public:
|
|||||||
//作为客户端发送c0c1,等待s0s1s2并且回调
|
//作为客户端发送c0c1,等待s0s1s2并且回调
|
||||||
void startClientSession(const function<void()> &cb);
|
void startClientSession(const function<void()> &cb);
|
||||||
void onParseRtmp(const char *pcRawData,int iSize);
|
void onParseRtmp(const char *pcRawData,int iSize);
|
||||||
void clear();
|
void reset();
|
||||||
protected:
|
protected:
|
||||||
virtual void onSendRawData(const char *pcRawData,int iSize) = 0;
|
virtual void onSendRawData(const char *pcRawData,int iSize) = 0;
|
||||||
virtual void onRtmpChunk(RtmpPacket &chunkData) = 0;
|
virtual void onRtmpChunk(RtmpPacket &chunkData) = 0;
|
||||||
|
@ -50,7 +50,7 @@ void RtmpPusher::teardown() {
|
|||||||
m_dqOnStatusCB.clear();
|
m_dqOnStatusCB.clear();
|
||||||
}
|
}
|
||||||
m_pPublishTimer.reset();
|
m_pPublishTimer.reset();
|
||||||
clear();
|
reset();
|
||||||
shutdown();
|
shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,8 @@ namespace Rtsp {
|
|||||||
_onRecvRTP(it->second, trackidx); \
|
_onRecvRTP(it->second, trackidx); \
|
||||||
m_amapRtpSort[trackidx].erase(it);
|
m_amapRtpSort[trackidx].erase(it);
|
||||||
|
|
||||||
|
const char RtspPlayer::kRtpType[] = "rtp_type";
|
||||||
|
|
||||||
RtspPlayer::RtspPlayer(void){
|
RtspPlayer::RtspPlayer(void){
|
||||||
}
|
}
|
||||||
RtspPlayer::~RtspPlayer(void) {
|
RtspPlayer::~RtspPlayer(void) {
|
||||||
@ -67,13 +69,34 @@ void RtspPlayer::teardown(){
|
|||||||
shutdown();
|
shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RtspPlayer::play(const char* strUrl){
|
||||||
|
auto userAndPwd = FindField(strUrl,"://","@");
|
||||||
|
eRtpType eType = (eRtpType)(int)(*this)[kRtpType];
|
||||||
|
if(userAndPwd.empty()){
|
||||||
|
play(strUrl,nullptr,nullptr,eType);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto suffix = FindField(strUrl,"@",nullptr);
|
||||||
|
auto url = StrPrinter << "rtsp://" << suffix << endl;
|
||||||
|
if(userAndPwd.find(":") == string::npos){
|
||||||
|
play(url.data(),userAndPwd.data(),nullptr,eType);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto user = FindField(userAndPwd.data(),nullptr,":");
|
||||||
|
auto pwd = FindField(userAndPwd.data(),":",nullptr);
|
||||||
|
play(url.data(),user.data(),pwd.data(),eType);
|
||||||
|
}
|
||||||
//播放,指定是否走rtp over tcp
|
//播放,指定是否走rtp over tcp
|
||||||
void RtspPlayer::play(const char* strUrl, const char *strUser, const char *strPwd, eRtpType eType ) {
|
void RtspPlayer::play(const char* strUrl, const char *strUser, const char *strPwd, eRtpType eType ) {
|
||||||
InfoL <<strUrl <<" "<< eType;
|
DebugL << strUrl << " "
|
||||||
|
<< (strUser ? strUser : "null") << " "
|
||||||
|
<< (strPwd ? strPwd:"null") << " "
|
||||||
|
<< eType;
|
||||||
teardown();
|
teardown();
|
||||||
if(strUser && strPwd){
|
if(strUser){
|
||||||
char _authorization[30];
|
char _authorization[30];
|
||||||
string tmp = StrPrinter << strUser << ":" << strPwd << endl;
|
string tmp = StrPrinter << strUser << ":" << (strPwd ? strPwd : "") << endl;
|
||||||
av_base64_encode(_authorization, sizeof(_authorization), (const unsigned char *) tmp.c_str(), tmp.size());
|
av_base64_encode(_authorization, sizeof(_authorization), (const unsigned char *) tmp.c_str(), tmp.size());
|
||||||
m_strAuthorization = _authorization;
|
m_strAuthorization = _authorization;
|
||||||
}
|
}
|
||||||
|
@ -36,9 +36,13 @@ namespace Rtsp {
|
|||||||
class RtspPlayer: public PlayerBase,public TcpClient {
|
class RtspPlayer: public PlayerBase,public TcpClient {
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<RtspPlayer> Ptr;
|
typedef std::shared_ptr<RtspPlayer> Ptr;
|
||||||
|
//设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播)
|
||||||
|
//设置方法:player[RtspPlayer::kRtpType] = 0/1/2;
|
||||||
|
static const char kRtpType[];
|
||||||
|
|
||||||
RtspPlayer();
|
RtspPlayer();
|
||||||
virtual ~RtspPlayer(void);
|
virtual ~RtspPlayer(void);
|
||||||
void play(const char* strUrl, const char *strUser, const char *strPwd, eRtpType eType) override;
|
void play(const char* strUrl) override;
|
||||||
void pause(bool bPause) override;
|
void pause(bool bPause) override;
|
||||||
void teardown() override;
|
void teardown() override;
|
||||||
float getRtpLossRate(int iTrackId) const override;
|
float getRtpLossRate(int iTrackId) const override;
|
||||||
@ -84,6 +88,7 @@ private:
|
|||||||
onPlayResult(ex);
|
onPlayResult(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void play(const char* strUrl, const char *strUser, const char *strPwd, eRtpType eType);
|
||||||
void onConnect(const SockException &err) override;
|
void onConnect(const SockException &err) override;
|
||||||
void onRecv(const Socket::Buffer::Ptr &pBuf) override;
|
void onRecv(const Socket::Buffer::Ptr &pBuf) override;
|
||||||
void onErr(const SockException &ex) override;
|
void onErr(const SockException &ex) override;
|
||||||
|
@ -52,7 +52,8 @@ int main(int argc, char *argv[]){
|
|||||||
player->setOnShutdown([&](const SockException &ex){
|
player->setOnShutdown([&](const SockException &ex){
|
||||||
--alivePlayerCnt;
|
--alivePlayerCnt;
|
||||||
});
|
});
|
||||||
player->play(argv[3], "", "", (PlayerBase::eRtpType)atoi(argv[4]));
|
(*player)[RtspPlayer::kRtpType] = atoi(argv[4]);
|
||||||
|
player->play(argv[3]);
|
||||||
playerList.push_back(player);
|
playerList.push_back(player);
|
||||||
return playerCnt--;
|
return playerCnt--;
|
||||||
});
|
});
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Poller/EventPoller.h"
|
#include "Poller/EventPoller.h"
|
||||||
#include "Rtsp/UDPServer.h"
|
#include "Rtsp/UDPServer.h"
|
||||||
|
#include "Player/MediaPlayer.h"
|
||||||
#include "Util/onceToken.h"
|
#include "Util/onceToken.h"
|
||||||
#include "Device/PlayerProxy.h"
|
|
||||||
#include "H264Decoder.h"
|
#include "H264Decoder.h"
|
||||||
#include "YuvDisplayer.h"
|
#include "YuvDisplayer.h"
|
||||||
#include "Network/sockutil.h"
|
#include "Network/sockutil.h"
|
||||||
@ -18,7 +18,7 @@ using namespace ZL::Util;
|
|||||||
using namespace ZL::Thread;
|
using namespace ZL::Thread;
|
||||||
using namespace ZL::Network;
|
using namespace ZL::Network;
|
||||||
using namespace ZL::Rtsp;
|
using namespace ZL::Rtsp;
|
||||||
using namespace ZL::DEV;
|
using namespace ZL::Player;
|
||||||
|
|
||||||
void programExit(int arg) {
|
void programExit(int arg) {
|
||||||
EventPoller::Instance().shutdown();
|
EventPoller::Instance().shutdown();
|
||||||
@ -30,9 +30,9 @@ int main(int argc, char *argv[]){
|
|||||||
//Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
//Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||||
signal(SIGINT, programExit);
|
signal(SIGINT, programExit);
|
||||||
|
|
||||||
if(argc != 5){
|
if(argc != 3){
|
||||||
FatalL << "\r\n测试方法:./test_player rtxp_url rtsp_user rtsp_pwd rtp_type\r\n"
|
FatalL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n"
|
||||||
<< "例如:./test_player rtsp://127.0.0.1/live/0 admin 123456 0\r\n"
|
<< "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n"
|
||||||
<<endl;
|
<<endl;
|
||||||
Logger::Destory();
|
Logger::Destory();
|
||||||
return 0;
|
return 0;
|
||||||
@ -46,9 +46,8 @@ int main(int argc, char *argv[]){
|
|||||||
player->setOnShutdown([](const SockException &ex) {
|
player->setOnShutdown([](const SockException &ex) {
|
||||||
ErrorL << "OnShutdown:" << ex.what();
|
ErrorL << "OnShutdown:" << ex.what();
|
||||||
});
|
});
|
||||||
|
(*player)[RtspPlayer::kRtpType] = atoi(argv[2]);
|
||||||
//DebugL << argv[1] << " " << argv[2] << " " << argv[3] << " " << argv[4] << endl;
|
player->play(argv[1]);
|
||||||
player->play(argv[1],argv[2],argv[3],(PlayerBase::eRtpType)atoi(argv[4]));
|
|
||||||
|
|
||||||
H264Decoder decoder;
|
H264Decoder decoder;
|
||||||
YuvDisplayer displayer;
|
YuvDisplayer displayer;
|
||||||
|
@ -49,7 +49,7 @@ int main(int argc,char *argv[]){
|
|||||||
//support rtmp and rtsp url
|
//support rtmp and rtsp url
|
||||||
//just support H264+AAC
|
//just support H264+AAC
|
||||||
auto urlList = {"rtmp://live.hkstv.hk.lxdns.com/live/hks",
|
auto urlList = {"rtmp://live.hkstv.hk.lxdns.com/live/hks",
|
||||||
"rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov"};
|
"rtsp://admin:jzan123456@192.168.0.122/"};
|
||||||
map<string , PlayerProxy::Ptr> proxyMap;
|
map<string , PlayerProxy::Ptr> proxyMap;
|
||||||
int i=0;
|
int i=0;
|
||||||
for(auto url : urlList){
|
for(auto url : urlList){
|
||||||
@ -63,6 +63,7 @@ int main(int argc,char *argv[]){
|
|||||||
//rtsp://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
|
//rtsp://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
|
||||||
//rtmp://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
|
//rtmp://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
|
||||||
PlayerProxy::Ptr player(new PlayerProxy("live",to_string(i++).data()));
|
PlayerProxy::Ptr player(new PlayerProxy("live",to_string(i++).data()));
|
||||||
|
(*player)[PlayerProxy::kAliveSecond] = 10;//录制10秒
|
||||||
player->play(url);
|
player->play(url);
|
||||||
proxyMap.emplace(string(url),player);
|
proxyMap.emplace(string(url),player);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user