mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-25 03:02:22 +08:00
添加rtsp推流器
整理代码
This commit is contained in:
@@ -84,6 +84,7 @@ using namespace toolkit;
|
||||
#define FLV_KEY_FRAME 1
|
||||
#define FLV_INTER_FRAME 2
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
#if defined(_WIN32)
|
||||
#pragma pack(push, 1)
|
||||
@@ -291,7 +292,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -72,11 +72,11 @@ void RtmpPlayer::teardown() {
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
void RtmpPlayer::play(const char* strUrl) {
|
||||
void RtmpPlayer::play(const string &strUrl) {
|
||||
teardown();
|
||||
string strHost = FindField(strUrl, "://", "/");
|
||||
_strApp = FindField(strUrl, (strHost + "/").data(), "/");
|
||||
_strStream = FindField(strUrl, (strHost + "/" + _strApp + "/").data(), NULL);
|
||||
string strHost = FindField(strUrl.data(), "://", "/");
|
||||
_strApp = FindField(strUrl.data(), (strHost + "/").data(), "/");
|
||||
_strStream = FindField(strUrl.data(), (strHost + "/" + _strApp + "/").data(), NULL);
|
||||
_strTcUrl = string("rtmp://") + strHost + "/" + _strApp;
|
||||
|
||||
if (!_strApp.size() || !_strStream.size()) {
|
||||
@@ -85,13 +85,13 @@ void RtmpPlayer::play(const char* strUrl) {
|
||||
}
|
||||
DebugL << strHost << " " << _strApp << " " << _strStream;
|
||||
|
||||
auto iPort = atoi(FindField(strHost.c_str(), ":", NULL).c_str());
|
||||
auto iPort = atoi(FindField(strHost.data(), ":", NULL).data());
|
||||
if (iPort <= 0) {
|
||||
//rtmp 默认端口1935
|
||||
iPort = 1935;
|
||||
} else {
|
||||
//服务器域名
|
||||
strHost = FindField(strHost.c_str(), NULL, ":");
|
||||
strHost = FindField(strHost.data(), NULL, ":");
|
||||
}
|
||||
if(!(*this)[PlayerBase::kNetAdapter].empty()){
|
||||
setNetAdapter((*this)[PlayerBase::kNetAdapter]);
|
||||
|
||||
@@ -49,7 +49,7 @@ public:
|
||||
RtmpPlayer();
|
||||
virtual ~RtmpPlayer();
|
||||
|
||||
void play(const char* strUrl) override;
|
||||
void play(const string &strUrl) override;
|
||||
void pause(bool bPause) override;
|
||||
void teardown() override;
|
||||
protected:
|
||||
|
||||
@@ -56,7 +56,7 @@ public:
|
||||
fProgress = MAX(float(0),MIN(fProgress,float(1.0)));
|
||||
seekToMilliSecond(fProgress * getDuration() * 1000);
|
||||
};
|
||||
void play(const char* strUrl) override {
|
||||
void play(const string &strUrl) override {
|
||||
_analysisMs = (*this)[PlayerBase::kMaxAnalysisMS].as<int>();
|
||||
PlayerImp<RtmpPlayer,RtmpDemuxer>::play(strUrl);
|
||||
}
|
||||
|
||||
@@ -325,7 +325,7 @@ void RtmpProtocol::handle_C0C1() {
|
||||
if (_strRcvBuf[0] != HANDSHAKE_PLAINTEXT) {
|
||||
throw std::runtime_error("only plaintext[0x03] handshake supported");
|
||||
}
|
||||
if(memcmp(_strRcvBuf.c_str() + 5,"\x00\x00\x00\x00",4) ==0 ){
|
||||
if(memcmp(_strRcvBuf.data() + 5,"\x00\x00\x00\x00",4) ==0 ){
|
||||
//simple handsharke
|
||||
handle_C1_simple();
|
||||
}else{
|
||||
@@ -347,7 +347,7 @@ void RtmpProtocol::handle_C1_simple(){
|
||||
RtmpHandshake s1(0);
|
||||
onSendRawData(obtainBuffer((char *) &s1, C1_HANDSHARK_SIZE));
|
||||
//发送S2
|
||||
onSendRawData(obtainBuffer(_strRcvBuf.c_str() + 1, C1_HANDSHARK_SIZE));
|
||||
onSendRawData(obtainBuffer(_strRcvBuf.data() + 1, C1_HANDSHARK_SIZE));
|
||||
//等待C2
|
||||
_nextHandle = [this]() {
|
||||
handle_C2();
|
||||
|
||||
@@ -35,14 +35,6 @@ namespace mediakit {
|
||||
|
||||
static int kSockFlags = SOCKET_DEFAULE_FLAGS | FLAG_MORE;
|
||||
|
||||
RtmpPusher::RtmpPusher(const char *strVhost,const char *strApp,const char *strStream) {
|
||||
auto src = dynamic_pointer_cast<RtmpMediaSource>(MediaSource::find(RTMP_SCHEMA,strVhost,strApp,strStream));
|
||||
if (!src) {
|
||||
auto strErr = StrPrinter << "media source:" << strVhost << "/" << strApp << "/" << strStream << "not found!" << endl;
|
||||
throw std::runtime_error(strErr);
|
||||
}
|
||||
_pMediaSrc=src;
|
||||
}
|
||||
RtmpPusher::RtmpPusher(const RtmpMediaSource::Ptr &src){
|
||||
_pMediaSrc=src;
|
||||
}
|
||||
@@ -70,11 +62,11 @@ void RtmpPusher::teardown() {
|
||||
}
|
||||
}
|
||||
|
||||
void RtmpPusher::publish(const char* strUrl) {
|
||||
void RtmpPusher::publish(const string &strUrl) {
|
||||
teardown();
|
||||
string strHost = FindField(strUrl, "://", "/");
|
||||
_strApp = FindField(strUrl, (strHost + "/").data(), "/");
|
||||
_strStream = FindField(strUrl, (strHost + "/" + _strApp + "/").data(), NULL);
|
||||
string strHost = FindField(strUrl.data(), "://", "/");
|
||||
_strApp = FindField(strUrl.data(), (strHost + "/").data(), "/");
|
||||
_strStream = FindField(strUrl.data(), (strHost + "/" + _strApp + "/").data(), NULL);
|
||||
_strTcUrl = string("rtmp://") + strHost + "/" + _strApp;
|
||||
|
||||
if (!_strApp.size() || !_strStream.size()) {
|
||||
@@ -83,14 +75,31 @@ void RtmpPusher::publish(const char* strUrl) {
|
||||
}
|
||||
DebugL << strHost << " " << _strApp << " " << _strStream;
|
||||
|
||||
auto iPort = atoi(FindField(strHost.c_str(), ":", NULL).c_str());
|
||||
auto iPort = atoi(FindField(strHost.data(), ":", NULL).data());
|
||||
if (iPort <= 0) {
|
||||
//rtmp 默认端口1935
|
||||
iPort = 1935;
|
||||
} else {
|
||||
//服务器域名
|
||||
strHost = FindField(strHost.c_str(), NULL, ":");
|
||||
strHost = FindField(strHost.data(), NULL, ":");
|
||||
}
|
||||
|
||||
weak_ptr<RtmpPusher> weakSelf = dynamic_pointer_cast<RtmpPusher>(shared_from_this());
|
||||
float playTimeOutSec = (*this)[kPlayTimeoutMS].as<int>() / 1000.0;
|
||||
_pPublishTimer.reset( new Timer(playTimeOutSec, [weakSelf]() {
|
||||
auto strongSelf=weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
return false;
|
||||
}
|
||||
strongSelf->onPublishResult(SockException(Err_timeout,"publish rtmp timeout"));
|
||||
strongSelf->teardown();
|
||||
return false;
|
||||
},getPoller()));
|
||||
|
||||
if(!(*this)[kNetAdapter].empty()){
|
||||
setNetAdapter((*this)[kNetAdapter]);
|
||||
}
|
||||
|
||||
startConnect(strHost, iPort);
|
||||
}
|
||||
|
||||
@@ -98,26 +107,18 @@ void RtmpPusher::onErr(const SockException &ex){
|
||||
onShutdown(ex);
|
||||
}
|
||||
void RtmpPusher::onConnect(const SockException &err){
|
||||
if(err.getErrCode()!=Err_success) {
|
||||
if(err) {
|
||||
onPublishResult(err);
|
||||
return;
|
||||
}
|
||||
weak_ptr<RtmpPusher> weakSelf = dynamic_pointer_cast<RtmpPusher>(shared_from_this());
|
||||
_pPublishTimer.reset( new Timer(10, [weakSelf]() {
|
||||
auto strongSelf=weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
return false;
|
||||
}
|
||||
strongSelf->onPublishResult(SockException(Err_timeout,"publish rtmp timeout"));
|
||||
strongSelf->teardown();
|
||||
return false;
|
||||
},getPoller()));
|
||||
startClientSession([weakSelf](){
|
||||
auto strongSelf=weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
return;
|
||||
}
|
||||
//strongSelf->sendChunkSize(60000);
|
||||
|
||||
strongSelf->sendChunkSize(60000);
|
||||
strongSelf->send_connect();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -30,28 +30,27 @@
|
||||
#include "RtmpProtocol.h"
|
||||
#include "RtmpMediaSource.h"
|
||||
#include "Network/TcpClient.h"
|
||||
#include "Pusher/PusherBase.h"
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class RtmpPusher: public RtmpProtocol , public TcpClient{
|
||||
class RtmpPusher: public RtmpProtocol , public TcpClient , public PusherBase{
|
||||
public:
|
||||
typedef std::shared_ptr<RtmpPusher> Ptr;
|
||||
typedef std::function<void(const SockException &ex)> Event;
|
||||
RtmpPusher(const char *strVhost,const char *strApp,const char *strStream);
|
||||
RtmpPusher(const RtmpMediaSource::Ptr &src);
|
||||
virtual ~RtmpPusher();
|
||||
|
||||
void publish(const char* strUrl);
|
||||
void teardown();
|
||||
void publish(const string &strUrl) override ;
|
||||
|
||||
void setOnPublished(Event onPublished) {
|
||||
_onPublished = onPublished;
|
||||
void teardown() override;
|
||||
|
||||
void setOnPublished(const Event &cb) override {
|
||||
_onPublished = cb;
|
||||
}
|
||||
|
||||
void setOnShutdown(Event onShutdown) {
|
||||
_onShutdown = onShutdown;
|
||||
void setOnShutdown(const Event &cb) override{
|
||||
_onShutdown = cb;
|
||||
}
|
||||
|
||||
protected:
|
||||
//for Tcpclient override
|
||||
void onRecv(const Buffer::Ptr &pBuf) override;
|
||||
|
||||
Reference in New Issue
Block a user