2020-07-02 22:23:43 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2020-07-02 22:23:43 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2020-07-02 22:23:43 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-07-02 22:23:43 +08:00
|
|
|
|
* 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"
|
2021-06-08 14:03:25 +08:00
|
|
|
|
#include "Network/UdpServer.h"
|
2020-07-02 22:23:43 +08:00
|
|
|
|
#include "RtpSession.h"
|
|
|
|
|
|
|
2022-11-01 17:27:27 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
|
|
class RtcpHelper;
|
2020-07-02 22:23:43 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* RTP服务器,支持UDP/TCP
|
|
|
|
|
|
*/
|
2022-09-09 10:56:28 +08:00
|
|
|
|
class RtpServer : public std::enable_shared_from_this<RtpServer> {
|
2020-07-02 22:23:43 +08:00
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using Ptr = std::shared_ptr<RtpServer>;
|
|
|
|
|
|
using onRecv = std::function<void(const toolkit::Buffer::Ptr &buf)>;
|
2022-09-09 10:56:28 +08:00
|
|
|
|
enum TcpMode { NONE = 0, PASSIVE, ACTIVE };
|
2020-07-02 22:23:43 +08:00
|
|
|
|
|
|
|
|
|
|
~RtpServer();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 开启服务器,可能抛异常
|
|
|
|
|
|
* @param local_port 本地端口,0时为随机端口
|
2020-07-07 10:01:12 +08:00
|
|
|
|
* @param stream_id 流id,置空则使用ssrc
|
2022-09-09 10:56:28 +08:00
|
|
|
|
* @param tcp_mode tcp服务模式
|
2020-07-02 22:23:43 +08:00
|
|
|
|
* @param local_ip 绑定的本地网卡ip
|
2022-04-09 10:26:15 +08:00
|
|
|
|
* @param re_use_port 是否设置socket为re_use属性
|
2022-09-09 10:56:28 +08:00
|
|
|
|
* @param ssrc 指定的ssrc
|
2023-11-09 11:26:13 +08:00
|
|
|
|
* @param multiplex 多路复用
|
2020-07-02 22:23:43 +08:00
|
|
|
|
*/
|
2024-07-09 10:42:10 +08:00
|
|
|
|
void start(uint16_t local_port, const MediaTuple &tuple = MediaTuple{DEFAULT_VHOST, kRtpAppName, "", ""}, TcpMode tcp_mode = PASSIVE,
|
2024-03-05 17:06:31 +08:00
|
|
|
|
const char *local_ip = "::", bool re_use_port = true, uint32_t ssrc = 0, int only_track = 0, bool multiplex = false);
|
2020-07-02 22:23:43 +08:00
|
|
|
|
|
2022-09-09 10:56:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 连接到tcp服务(tcp主动模式)
|
|
|
|
|
|
* @param url 服务器地址
|
|
|
|
|
|
* @param port 服务器端口
|
|
|
|
|
|
* @param cb 连接服务器是否成功的回调
|
|
|
|
|
|
*/
|
|
|
|
|
|
void connectToServer(const std::string &url, uint16_t port, const std::function<void(const toolkit::SockException &ex)> &cb);
|
|
|
|
|
|
|
2020-07-02 22:23:43 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取绑定的本地端口
|
|
|
|
|
|
*/
|
|
|
|
|
|
uint16_t getPort();
|
|
|
|
|
|
|
2020-07-08 10:25:30 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 设置RtpProcess onDetach事件回调
|
|
|
|
|
|
*/
|
2024-06-09 10:52:10 +08:00
|
|
|
|
void setOnDetach(RtpProcess::onDetachCB cb);
|
2020-12-27 21:21:31 +08:00
|
|
|
|
|
2023-04-21 23:08:48 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更新ssrc
|
|
|
|
|
|
*/
|
|
|
|
|
|
void updateSSRC(uint32_t ssrc);
|
|
|
|
|
|
|
2022-09-09 10:56:28 +08:00
|
|
|
|
private:
|
|
|
|
|
|
// tcp主动模式连接服务器成功回调
|
|
|
|
|
|
void onConnect();
|
|
|
|
|
|
|
2020-07-02 22:23:43 +08:00
|
|
|
|
protected:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Socket::Ptr _rtp_socket;
|
|
|
|
|
|
toolkit::UdpServer::Ptr _udp_server;
|
|
|
|
|
|
toolkit::TcpServer::Ptr _tcp_server;
|
2023-04-21 23:08:48 +08:00
|
|
|
|
std::shared_ptr<uint32_t> _ssrc;
|
2022-11-01 17:27:27 +08:00
|
|
|
|
std::shared_ptr<RtcpHelper> _rtcp_helper;
|
2022-09-09 10:56:28 +08:00
|
|
|
|
std::function<void()> _on_cleanup;
|
|
|
|
|
|
|
2024-03-05 17:06:31 +08:00
|
|
|
|
int _only_track = 0;
|
2022-09-09 10:56:28 +08:00
|
|
|
|
//用于tcp主动模式
|
|
|
|
|
|
TcpMode _tcp_mode = NONE;
|
2020-07-02 22:23:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|
#endif//defined(ENABLE_RTPPROXY)
|
|
|
|
|
|
#endif //ZLMEDIAKIT_RTPSERVER_H
|