mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-30 06:42:22 +08:00
支持http api动态添加或关闭rtp服务器
This commit is contained in:
64
src/Rtp/RtpServer.cpp
Normal file
64
src/Rtp/RtpServer.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if defined(ENABLE_RTPPROXY)
|
||||
#include "RtpServer.h"
|
||||
#include "RtpSelector.h"
|
||||
namespace mediakit{
|
||||
|
||||
RtpServer::RtpServer() {
|
||||
}
|
||||
|
||||
RtpServer::~RtpServer() {
|
||||
if(_udp_server){
|
||||
_udp_server->setOnRead(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void RtpServer::start(uint16_t local_port, bool enable_tcp, const char *local_ip) {
|
||||
_udp_server.reset(new Socket(nullptr, false));
|
||||
auto &ref = RtpSelector::Instance();
|
||||
auto sock = _udp_server;
|
||||
_udp_server->setOnRead([&ref, sock](const Buffer::Ptr &buf, struct sockaddr *addr, int) {
|
||||
ref.inputRtp(sock, buf->data(), buf->size(), addr);
|
||||
});
|
||||
|
||||
//创建udp服务器
|
||||
if (!_udp_server->bindUdpSock(local_port, local_ip)) {
|
||||
_udp_server = nullptr;
|
||||
string err = (StrPrinter << "bindUdpSock on " << local_ip << ":" << local_port << " failed:" << get_uv_errmsg(true));
|
||||
throw std::runtime_error(err);
|
||||
}
|
||||
//设置udp socket读缓存
|
||||
SockUtil::setRecvBuf(_udp_server->rawFD(), 4 * 1024 * 1024);
|
||||
|
||||
if (enable_tcp) {
|
||||
try {
|
||||
//创建tcp服务器
|
||||
_tcp_server = std::make_shared<TcpServer>(_udp_server->getPoller());
|
||||
_tcp_server->start<RtpSession>(_udp_server->get_local_port(), local_ip);
|
||||
} catch (...) {
|
||||
_tcp_server = nullptr;
|
||||
_udp_server = nullptr;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EventPoller::Ptr RtpServer::getPoller() {
|
||||
return _udp_server->getPoller();
|
||||
}
|
||||
|
||||
uint16_t RtpServer::getPort() {
|
||||
return _udp_server ? _udp_server->get_local_port() : 0;
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
#endif//defined(ENABLE_RTPPROXY)
|
||||
61
src/Rtp/RtpServer.h
Normal file
61
src/Rtp/RtpServer.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef ZLMEDIAKIT_RTPSERVER_H
|
||||
#define ZLMEDIAKIT_RTPSERVER_H
|
||||
|
||||
#if defined(ENABLE_RTPPROXY)
|
||||
#include <memory>
|
||||
#include "Network/Socket.h"
|
||||
#include "Network/TcpServer.h"
|
||||
#include "RtpSession.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
/**
|
||||
* RTP服务器,支持UDP/TCP
|
||||
*/
|
||||
class RtpServer {
|
||||
public:
|
||||
typedef std::shared_ptr<RtpServer> Ptr;
|
||||
typedef function<void(const Buffer::Ptr &buf)> onRecv;
|
||||
|
||||
RtpServer();
|
||||
~RtpServer();
|
||||
|
||||
/**
|
||||
* 开启服务器,可能抛异常
|
||||
* @param local_port 本地端口,0时为随机端口
|
||||
* @param enable_tcp 是否启用tcp服务器
|
||||
* @param local_ip 绑定的本地网卡ip
|
||||
*/
|
||||
void start(uint16_t local_port, bool enable_tcp = true, const char *local_ip = "0.0.0.0");
|
||||
|
||||
/**
|
||||
* 获取绑定的本地端口
|
||||
*/
|
||||
uint16_t getPort();
|
||||
|
||||
/**
|
||||
* 获取绑定的线程
|
||||
*/
|
||||
EventPoller::Ptr getPoller();
|
||||
|
||||
protected:
|
||||
Socket::Ptr _udp_server;
|
||||
TcpServer::Ptr _tcp_server;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
#endif//defined(ENABLE_RTPPROXY)
|
||||
#endif //ZLMEDIAKIT_RTPSERVER_H
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if defined(ENABLE_RTPPROXY)
|
||||
#include "UdpRecver.h"
|
||||
#include "RtpSelector.h"
|
||||
namespace mediakit{
|
||||
|
||||
UdpRecver::UdpRecver() {
|
||||
}
|
||||
|
||||
UdpRecver::~UdpRecver() {
|
||||
if(_sock){
|
||||
_sock->setOnRead(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
bool UdpRecver::initSock(uint16_t local_port,const char *local_ip) {
|
||||
_sock.reset(new Socket(nullptr, false));
|
||||
onceToken token(nullptr,[&](){
|
||||
SockUtil::setRecvBuf(_sock->rawFD(),4 * 1024 * 1024);
|
||||
});
|
||||
|
||||
auto &ref = RtpSelector::Instance();
|
||||
auto sock = _sock;
|
||||
_sock->setOnRead([&ref, sock](const Buffer::Ptr &buf, struct sockaddr *addr, int ){
|
||||
ref.inputRtp(sock, buf->data(),buf->size(),addr);
|
||||
});
|
||||
return _sock->bindUdpSock(local_port,local_ip);
|
||||
}
|
||||
|
||||
EventPoller::Ptr UdpRecver::getPoller() {
|
||||
return _sock->getPoller();
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
#endif//defined(ENABLE_RTPPROXY)
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef ZLMEDIAKIT_UDPRECVER_H
|
||||
#define ZLMEDIAKIT_UDPRECVER_H
|
||||
|
||||
#if defined(ENABLE_RTPPROXY)
|
||||
#include <memory>
|
||||
#include "Network/Socket.h"
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
/**
|
||||
* 组播接收器
|
||||
*/
|
||||
class UdpRecver {
|
||||
public:
|
||||
typedef std::shared_ptr<UdpRecver> Ptr;
|
||||
typedef function<void(const Buffer::Ptr &buf)> onRecv;
|
||||
|
||||
UdpRecver();
|
||||
virtual ~UdpRecver();
|
||||
bool initSock(uint16_t local_port,const char *local_ip = "0.0.0.0");
|
||||
EventPoller::Ptr getPoller();
|
||||
protected:
|
||||
Socket::Ptr _sock;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
#endif//defined(ENABLE_RTPPROXY)
|
||||
#endif //ZLMEDIAKIT_UDPRECVER_H
|
||||
Reference in New Issue
Block a user