从头写rtsp服务器-模块的划分

1. 网络库模块,这个模块主要作用是网络消息通知,还有一个定时器事件,至于网络库怎么写,不会的可以去学习一下网络编程,然后提取出相似的接口。

接口定义如下:

 1 class rtsp:public netoperation,netnocopyable
 2 {
 3 public:
 4     rtsp(){}
 5     rtsp(netaddress & server);
 6 
 7     virtual int v_connect(netconnection * n);
 8     virtual int v_close(netconnection * n);
 9     virtual int v_read(netconnection * n);
10     virtual int v_accept(netconnection * n);
11     virtual int v_write(netconnection * n);
12     virtual int v_timer(evtime * e);
13     int v_rtspRead(netconnection * n);
14     int v_rtpRead(netconnection * n);
15 private:
16      netaddress m_serveAddr;
17 
18 };

2.rtspconnection类,这里代表一条tcp连接,一个客户端和服务器的一条连接,定义如下:

class RtspClientConnection
{
public:
	RtspClientConnection(netconnection * n,	netaddress & addr);
	virtual ~RtspClientConnection();
	
	int handle_options(uint seq);
	
	
	int handle_describle(
		std::string & streamName,
		uint seq,
		std::string & fullRequestStr);
	
	const std::string & SessionId(){return m_sessionId;}
	
	int handle_setup(
		std::string & sessionId,
		std::string & streamName,  
		std::string & trackId, 
		uint seq,
		std::string & fullRequestStr);
	
	int handle_incmd(
		std::string & sessionId,
		std::string & cmd, 
		std::string & streamName, 
		std::string & trackId, 
		uint seq,  
		std::string & fullRequestStr);
	
	int handle_play(MediaSession * session, 
		MediaSubSession * subSession, 
		uint seq,  
		std::string & fullRequestStr);
	
	int handle_pause(MediaSession * session, 
		MediaSubSession * subSession,
		uint seq, 
		std::string & fullRequestStr);
	
	int handle_teardown(MediaSession * session, 
		MediaSubSession * subSession,
		uint seq, 
		std::string & fullRequestStr);
	
	int handle_getparameter(MediaSession * session, 
		MediaSubSession * subSession,
		uint seq, 
		std::string & fullRequestStr);
	
	int handle_setparameter(MediaSession * session, 
		MediaSubSession * subSession,
		uint seq, 
		std::string & fullRequestStr);

	int handle_register(std::string & urlSuffix,
		std::string & fullRequestStr,
		bool registerRemote);

	void handleCmd_bad(){/*400 Bad Request*/}

	void handleCmd_notSupported(){/*405 Method Not Allowed*/}

	void handleCmd_notFound(){/*404 stream not find*/}

	void handleCmd_sessionNotFound(){/*454 Session Not Found*/}

	void handleCmd_unsupportedTransport(){/*461 Unsupported Transport*/}


	
protected:
	netaddress m_serveAddr;
	MediaSession * m_mediaSession;
	netconnection * m_n;
	std::string m_sessionId;
};

 

3. MediaSession类,这里和live555不同的是,这里每一个rtsp请求,都会生产一个MediaSession对象,相当于live555的MediaSession和streamctrl的结合,主要是对submediassion的管理

接口定义如下:

 1 class MediaSession:public netnocopyable
 2 {
 3 public:
 4     MediaSession(const std::string & streamName);
 5 
 6     virtual ~MediaSession();
 7 
 8     void AddSubSession(MediaSubSession * pMediastream);
 9     
10     void DelSubSession(MediaSubSession * pMediastream);
11 
12     MediaSubSession* Lookup(std::string & trackId);
13 
14     int SubSessionCount();
15 
16     MediaSubSession * GetSubSession(int index);
17 
18     const std::string & StreamName();
19 
20     std::string  GenerateSDPDescription(netaddress & local);
21 
22 protected:
23     std::string m_streamName;
24     uint   m_createTime;
25     std::vector <MediaSubSession *> m_vecSubSession;
}

4.MediaSubSession类,代表的是一个stream,track,例如一般的MP4文件就有h264 + aac这2个track,接口定义如下

class MediaSubSession: public IRtspStream
{
public:
	MediaSubSession();

	virtual ~MediaSubSession();

	void SetTrackId(uint trackID);
	
	std::string GetTrackId();

	void SetPayloadType(uint8 number);

	const static std::string & TrackFmt();

	void SetStreamName(const std::string & streamName);

	virtual const std::string & SdpLines();

	//set up
	virtual int GetStreamParam(uint clientRtpPort, 
					   uint clientRtcpPort,
					   uint rtpChannelId, 
					   uint rtcpChannelId,
					   uint dstIp,
					   uint & dstTTL,
					   bool & isMulticast,
					   uint & serverRtpPort, 
					   uint & serverRtcpPort,
					   uint serverIp);

	virtual int StartStream(); //play

	virtual int TransferStream();

	virtual int OnTimer();

	virtual int PauseStream();

	virtual int SeekStream();

	//tear down
	virtual int StopStream();

	virtual uint RtpTimestamp();

	virtual uint SeqNo();

protected:
	std::string m_sdpLines;
	uint m_trackId;
	std::string m_streamName;
	rtp * m_rtp;
	rtcp * m_rtcp;
	uint8 m_payLoadType;
	uint m_baseRtpTimestamp;

	uint m_timestamp;
	uint m_timerId;
	int64 m_nextTimestamp;

	netmutex  m_mutex;
};

5.线程池任务接口:

//用于线程池调用TransferStream接口,进行文件读取,rtp打包,发送 

class IRtspStream:public netnocopyable 
{
public:
    virtual int TransferStream() = 0;

    virtual int OnTimer() = 0;
};

 

今天就更新到这里,下次接着更新

转载于:https://www.cnblogs.com/4k4s/p/3948822.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值