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
/****************************************************************************
 *
 *  Copyright (C) 2000-2001 RealNetworks, Inc. All rights reserved.
 *
 *  This program is free software.  It may be distributed under the terms
 *  in the file LICENSE, found in the top level of the source distribution.
 *
 */
 
#ifndef _RTSPPROT_H
#define _RTSPPROT_H
 
#include "MyList.h"
#include "RtspMsg.h"
 
enum RtspErr
{
    RTSPE_NONE,
    RTSPE_CLOSED,
    RTSPE_NOTRAN,
    RTSPE_MAX
};
 
class CRtspProtocolResponse
{
public:
    CRtspProtocolResponse(void) {}
    virtual ~CRtspProtocolResponse(void) {}
    
    virtual void OnError(RtspErr err) = 0;
 
    virtual void OnDescribeRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnAnnounceRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnGetParamRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnSetParamRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnOptionsRequest( CRtspRequestMsg* pMsg) = 0;
    virtual void OnPauseRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnPlayRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnRecordRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnRedirectRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnSetupRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnTeardownRequest(CRtspRequestMsg* pMsg) = 0;
    virtual void OnExtensionRequest(CRtspRequestMsg* pMsg) = 0;
 
    virtual void OnDescribeResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnAnnounceResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnGetParamResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnSetParamResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnOptionsResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnPauseResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnPlayResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnRecordResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnRedirectResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnSetupResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnTeardownResponse(CRtspResponseMsg* pMsg) = 0;
    virtual void OnExtensionResponse(CRtspResponseMsg* pMsg) = 0;
};
 
#define FUDGE           16
#define A_LINE_MAX_LEN  2048
#define RTSP_CRLF       "\r\n"
 
class CRtspProtocol
{
public:
    CRtspProtocol(CRtspProtocolResponse* pResponse);
    virtual ~CRtspProtocol(void);
 
    UINT32 GetNextCseq(void) {return ++m_CSeqSend;}
 
    void ResetCseq(){m_CSeqSend=0;}
    
    //Translate message struct into ASCII string. WARNNING: free pDataStr dynamic pointer after call it.
    int Encoder(const CRtspMsg* pRtspMsg, char* &pDataStr, int &DataLen);
    int DecoderAndDrvie(int sockfd, const char* pDataBuf, int DataLen, CRtspMsg* &pRtspMsg, char SrcIP[], UINT16 SrcPort, int &Code, const char* &pNextPacketPos);
    int DecoderAndDrvie(const char* pDataBuf, int DataLen, CRtspMsg* &pRtspMsg, int &Code, char SrcIP[], UINT16 SrcPort);
    int DecoderAndDrvie(const char* pDataBuf, int DataLen, CRtspMsg* &pRtspMsg, int &Code);
    
    int HandleReadCmd(char ALine[], CRtspMsg* &pRtspMsg, int &Code);
    bool handleAuthenticationFailure(CRtspMsg* &pRtspMsg, char *paramsStr);
    int HandleReadHdr(char ALine[], CRtspMsg* &pRtspMsg, int &Code);
    int HandleReadBody(char* pBody, int BodyLen, CRtspMsg* &pRtspMsg, int &Code);
    int DispatchMessage(CRtspMsg* &pRtspMsg, int &Code);
    
public:
    class CRtspRequestTag
    {
    public:
        CRtspRequestTag(UINT32 cseq, RtspVerb verb) : m_cseq(cseq), m_verb(verb) {}
        
        //XXX: Keep rtsp version, url, etc...?
        UINT32   m_cseq;
        RtspVerb m_verb;
    };
    typedef TDoubleList<CRtspRequestTag> CRtspRequestTagList;
    
    MyList<CRtspRequestTag> RequestTagListQueue;
 
private:
    enum ReadState {
        stFail,                 //Unrecoverable error occurred
        stSync,                 //Trying to resync
        stReady,                //Waiting for a command
        stPkt,                  //Reading interleaved packet
        stCmd,                  //Reading command (request or response line)
        stHdr,                  //Reading headers
        stBody,                 //Reading body (entity)
        stDispatch,             //Fully formed message
        stREADSTATE_LAST
    };
 
    CRtspProtocolResponse* m_pResponse;
    
    UINT32       m_CSeqSend;    //Our next cseq
    UINT32       m_CSeqRecv;    //Peer's last cseq
};
 
#endif //#ifndef _RTSPPROT_H