zhangzengfei
2022-07-20 c90c3e794bdd95127d0c34ff1d9e8759d18a0445
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/******************************************************************************
* FILE:    RtspClientSession.h
* Description:    
*    Interface for rtsp client session class.
*
* Modified Code History
* Mark    Date        By            Modification Reason
*******************************************************************************
* 01    2013/5/28    songxw    Initial creation.
******************************************************************************/
#if !defined(__RTSP_CLIENT_SESSION_H__)
#define __RTSP_CLIENT_SESSION_H__
 
using namespace std;
 
#include <poll.h>
#include "TypeDef.h"
#include "str.h"
#include "TcpTransport.h"
#include "RtspProt.h"
#include "MyThread.h"
#include "TcpStreamTrans.h"
#include "UdpStreamTrans.h"
 
#include "jthread.h"
using namespace jthread;
 
 
#define RTSP_USER_AGENT    "XSipGW(RtspClient)"
 
 
//¶Ë¿Ú·ÖÅ䷶Χ
#define RTSP_CLIENT_SESSION_BEGIN_PORT  31000
#define RTSP_CLIENT_SESSION_END_PORT    (RTSP_CLIENT_SESSION_BEGIN_PORT + 1000)
#define RTSP_MESSAGE_MAX_LEN  (20 * 1024)  //K
#define RTSP_KEEPALIVE_TIME   60
 
 
/* ÏûÏ¢¹ý³ÌµÄ¼Ç¼Êý¾Ý */
class CContextData
{
public:
    CContextData(void) 
    {
        StatusCode = 0;
        ServerPort = 0;
        ClientPort = 0;
        strServerIp = "";
        strClientIp = "";
    }
    ~CContextData(void) {}
    
    UINT32 StatusCode;   //×îÐÂÏûÏ¢»ØÓ¦Â룬Ö÷ҪΪ½â¾öRTSPÇëÇó»ØÓ¦Óûص÷·½Ê½£¬Ö÷Òª¹¤×÷Á÷³Ì²»Äܵõ½¡£
    
    CString StrUrl;      //ÇëÇóURL
    CString SessionID;   //»á»°ID
//    CString StrVStreamUrl;  //ÊÓÆµÁ÷±ê¼ÇµÄURL
    CString StrVStream;  //ÊÓÆµÁ÷±ê¼Ç
    CString StrAStream;  //ÒôƵÁ÷±ê¼Ç
    
    UINT16 ServerPort;   //Ô¶¶Ë(É豸)ýÌå¶Ë¿Ú
    UINT16 ClientPort;   //±¾µØÃ½Ìå¶Ë¿Ú
 
    string strServerIp;     //Á÷ýÌå·¢ËͶ˵ÄipµØÖ·
    string strClientIp;     //Á÷ýÌå½âÎö¶ËµÄipµØÖ·
};
 
/* Rtsp client session interface class */
class CRtspClientSession : public CRtspProtocolResponse
{
public:
    CRtspClientSession();
    ~CRtspClientSession(void);
    
private:
    //»ñÈ¡±¾µØipµØÖ·
    static int GetLocalIP(char *address, int size);
    //²é¿´¶Ë¿ÚÊÇ·ñ±»Õ¼ÓÃ
    static bool PortIsOccupied(const UINT16 PId);
    static bool AllocatePortID(UINT16 &PId);           //Note: PId is export variable.
    //¹¹ÔìÈÏÖ¤ÐÅÏ¢£¬Ìáǰlive555
    char* createAuthenticatorString(char *cmd, char *url);
public:
#if 0    
    //Ïß³ÌÆô¶¯
    void* Thread(void);
    //Ï߳̽áÊø
    void  ThreadQuit(void);
#endif    
public:
    //Method API for RTSP
    //·¢ËÍrtspµÄOPTIONSЭÒé
    int Options(const char* pUrl);
    //·¢ËÍrtspµÄDESCRIBEЭÒé
    int Describe(const char* pUrl, bool bAuth = false);
    //·¢ËÍrtspµÄSETUPЭÒé
    int Setup(const CString& SessionID, const CString StreamId, bool bCreateTcpSvr = false);
    //·¢ËÍrtspµÄPLAYЭÒé
    int Play(const CString& SessionID, int ctrltype = 0, double ctrlparam = 0);
    //·¢ËÍrtspµÄPAUSEЭÒé
    int Pause(const CString& SessionID);
    //·¢ËÍrtspµÄTEARDOWNЭÒé
    int Teardown(const CString& SessionID, const CString StreamId);
    //·¢ËÍrtspµÄGETPARAMЭÒé
    int GetParam(const CString& SessionID);
public:
    //¹ý³ÌÊý¾Ý
    CContextData CnxData;
    CTcpTransport* pTcpTransport;    //TCPЭÒéÁ¬½Ó
 
    CTcpStreamTrans *m_pTcpStreamTrans; //TCP ÂëÁ÷½ÓÊÕ
    CUdpStreamTrans *m_pUdpStreamTrans; //UDP½ÓÊÕÂëÁ÷
protected:
    virtual void OnError(RtspErr error) {}
    
    //ÇëÇó½øÀ´
    void OnOptionsRequest( CRtspRequestMsg* pMsg) {}
    void OnDescribeRequest(CRtspRequestMsg* pMsg) {}
    void OnAnnounceRequest(CRtspRequestMsg* pMsg) {}
    void OnGetParamRequest(CRtspRequestMsg* pMsg) {}
    void OnSetParamRequest(CRtspRequestMsg* pMsg) {}
    void OnSetupRequest(CRtspRequestMsg* pMsg) {}
    void OnPlayRequest(CRtspRequestMsg* pMsg) {}
    void OnPauseRequest(CRtspRequestMsg* pMsg) {}
    void OnRecordRequest(CRtspRequestMsg* pMsg) {}
    void OnRedirectRequest(CRtspRequestMsg* pMsg) {}
    void OnTeardownRequest(CRtspRequestMsg* pMsg) {}
    void OnExtensionRequest(CRtspRequestMsg* pMsg) {}
    
    //ÏìÓ¦½øÀ´
    //´¦ÀíOPTINSµÄ·µ»ØÐÅÏ¢
    void OnOptionsResponse(CRtspResponseMsg* pMsg);
    void OnDescribeResponse(CRtspResponseMsg* pMsg);
    void OnAnnounceResponse(CRtspResponseMsg* pMsg);
    void OnGetParamResponse(CRtspResponseMsg* pMsg);
    void OnSetParamResponse(CRtspResponseMsg* pMsg);
    void OnSetupResponse(CRtspResponseMsg* pMsg);
    void OnPlayResponse(CRtspResponseMsg* pMsg);
    void OnPauseResponse(CRtspResponseMsg* pMsg);
    void OnRecordResponse(CRtspResponseMsg* pMsg);
    void OnRedirectResponse(CRtspResponseMsg* pMsg);
    void OnTeardownResponse(CRtspResponseMsg* pMsg);
    void OnExtensionResponse(CRtspResponseMsg* pMsg);
private:
    //RTSPЭÒéÐÄÌø±¥ºÍº¯Êý
    bool KeepAliveSession(void);
    //ÇëÇóÏûÏ¢±àÂë¡¢·¢Ëͺ͵ȴý»ØÓ¦
    int DoWork(CRtspRequestMsg& ReqMsg);
    //·¢ËÍÊý¾Ý
    int SendData(const char* pBuf, int Len, const int WaitForTimeout = 5/*s*/);
    int ReadData(const char* pBuf, int BufLen, const int WaitForTimeout = 5/*s*/);
    int ReadRtspData(UINT8 *pBuf, int BufLen);
public:
    //ÉèÖÃRTSPµÄÐÅÏ¢
    void SetRtspSessionInfo(const char *url, const char *username, const char *passwd, bool bauth)
    {
        if(url != NULL && strlen(url) > 0)
        {
            strncpy2(m_RtspUrl, url, HTTP_URL_MAX_LEN);    
        }
 
        if(username != NULL && strlen(username) > 0)
        {
            strncpy2(m_UserName, username, HTTP_URL_MAX_LEN);    
        }
 
        if(passwd != NULL && strlen(passwd) > 0)
        {
            strncpy2(m_Passwd, passwd, HTTP_URL_MAX_LEN);    
        }
        m_bSetAuth = bauth;
 
        if (bauth == true)
        {
            m_Authenticator.setUsernameAndPassword(username, passwd);
        }
    }
private:
    bool  m_bExitFlag;
    //Rtsp protocol stack
    CRtspProtocol* pRtspProt; //rtspЭÒé½âÎö
 
    char m_RtspUrl[HTTP_URL_MAX_LEN + 1];//rtspµÄurl
    char m_UserName[USERNAME_MAX_LEN+1];//rtspÇëÇóµÄusername
    char m_Passwd[PASSWD_MAX_LEN+1];//rtspÇëÇóµÄÃÜÂë
    bool m_bSetAuth;    //ÊÇ·ñÐèÒªÈÏÖ¤
 
    CAuthenticator m_Authenticator; //ÈÏÖ¤ÐÅÏ¢Àà
    bool m_bSupportsGetParam;    //ÊÇ·ñÖ§³ÖGETPARAM £¬ÓÃÓÚЭÒéÐÄÌø
 
    int SendKeepAliveTime;
    //Buffer³¤¶È
    char DataBuf[RTSP_MESSAGE_MAX_LEN + 1];
public:
    //DevCorpID_E DevCorpID;
    int Channel;         //É豸ͨµÀ
    //Rtsp Session ID
    CString SessionID;
    bool bAudio;         //ÒôƵ¿ª¹Ø
    CString StrVStream;  //ÊÓÆµÁ÷±ê¼Ç
    CString StrAStream;  //ÒôƵÁ÷±ê¼Ç
public:
    //Port rule
    static UINT16 RtspBeginPort;
    static UINT16 RtspEndPort;
    static UINT16 RtspCurrentPort;                  //Current specify port to allocate    
};
 
#endif  //#if !defined(__RTSP_CLIENT_SESSION_H__)