Distribution_Service/CC_SDK/Include/Module/Comm/RTSP/CCRTSPServer.h
2026-03-24 14:43:26 +08:00

153 lines
4.5 KiB
C++

#ifndef SRCCTL_CC_RTSP_SERVER_H
#define SRCCTL_CC_RTSP_SERVER_H
#include "CCRTP.h"
#include "EventLoop.h"
#include "CCThreadPool.h"
#include "CCRTSPDataChannel.h"
#include "CCSDP.h"
#include "CCRTCP.h"
#include "rtpsessionparams.h"
#include "rtpudpv4transmitter.h"
#include "rtpsession.h"
#include "rtppacket.h"
//OPTIONS,DESCRIBE,SETUP,TEARDOWN,PLAY,PAUSE,ANNOUNCE,RECORD
#define RTSP_REQ_OPTIONS "OPTIONS"
#define RTSP_REQ_DESCRIBE "DESCRIBE"
#define RTSP_REQ_SETUP "SETUP"
#define RTSP_REQ_TEARDOWN "TEARDOWN"
#define RTSP_REQ_PLAY "PLAY"
#define RTSP_REQ_PAUSE "PAUSE"
#define RTSP_REQ_ANNOUNCE "ANNOUNCE"
#define RTSP_REQ_RECORD "RECORD"
#define RTSP_REQ_SET_PARAMETER "SET_PARAMETER"
#define RTSP_REQ_SET_GET_PARAMETER "GET_PARAMETER"
#define RTSP_REQ_SET_GET_REDIRECT "REDIRECT"
namespace CTL{
using RTSP_REQ_FUN = std::function<void(RTSP_Request&,RTSP_Response*)>;
class StreamChannels{
int index_t = 0;
int client_port_rtp = 0;
int client_port_rtcp = 0;
int rtp_port = 0;
int rtcp_port = 0;
String ip;
bool IsRun = false;
void RTPDataChannel() const;
void RTCPDataChannel();
public:
~StreamChannels();
int getClientPortRtp() const{
return client_port_rtp;
}
void setClientPortRtp(const int port){
this->client_port_rtp = port;
}
int getClientPortRtcp() const{
return client_port_rtcp;
}
void setClientPortRtcp(const int port){
this->client_port_rtcp = port;
}
int getRtpPort() const{
return rtp_port;
}
void setRtpPort(const int port){
this->rtp_port = port;
}
int getRtcpPort() const{
return rtcp_port;
}
void setRtcpPort(const int port){
this->rtcp_port = port;
}
int getIndex() const{
return index_t;
}
void setIndex(const int index){
this->index_t = index;
}
void setServiceIP(const String& IP);
void create_RTP_RTCP_Service();
void close_Service();
};
class RTSPSession{
String session_id;
String client_ip;
Map<int,StreamChannels*> StreamDataChannels;
CCMutex mutex;
public:
RTSPSession() = default;
~RTSPSession();
String getSessionId() const{
return session_id;
}
void setSessionId(const String& id){
this->session_id = id;
}
CCVector<StreamChannels> getStreamChannels(){
CCVector<StreamChannels> ret;
const auto list = StreamDataChannels.values();
for (const auto& item : list) {
if (item) {
ret.add_lock(*item);
}
}
return ret;
}
StreamChannels* createStreamChannel(){
CCUniqueLock lock(mutex);
const auto item = new StreamChannels;
item->setIndex(StreamDataChannels.size());
return item;
}
StreamChannels* getStreamChannel(const int index){
if (StreamDataChannels.IsContains(index)) {
const auto item = StreamDataChannels.get(index);
if (item && *item) {
return *item;
}
}
return nullptr;
}
void addStreamChannel(StreamChannels* channel);
void removeStreamChannel(int index);
private:
};
class RTSPServer{
ServerSocket* server = nullptr;
bool ServerFlag = true;
EventLoop event_loop_t{};
ThreadPool M_S_POOl{};
size_t M_Pool_Size = 128,M_Pool_Max = 1024;
int M_Pool_KeepAliveTime = 1000;
IPVX IPvx = IPV4;
String ServerIP;
int startPort = 32000,endPort = 33000;
CCMutex Session_mutex;
inline static CCMutex port_mutex;
Map<String,RTSPSession*> Sessions;
Map<String,String> URL_SID;
RTSP_REQ_FUN REQ_CallBack = nullptr;
public:
private:
void HandleNewConnection();
void ProcessingClientRequests(ClientSocket* client);
static void getClientPort(int* rtp, int* rtcp, const String& transport);
RTSPSession* createRTSPSession();
void removeRTSPSession(const String& sessionId);
static String generateSessionId();
public:
RTSPServer();
~RTSPServer();
bool Init(const InetAddress& address,int Port);
void SetCallBack(const RTSP_REQ_FUN& fun);
int Running();
};
}
#endif