• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_SHARING_RTSP_RESPONSE_H
17 #define OHOS_SHARING_RTSP_RESPONSE_H
18 
19 #include <list>
20 #include <string>
21 #include <unordered_map>
22 #include "rtsp_common.h"
23 
24 namespace OHOS {
25 namespace Sharing {
26 class RtspResponse {
27 public:
28     RtspResponse() = default;
RtspResponse(int32_t cseq,int32_t status)29     RtspResponse(int32_t cseq, int32_t status) : cSeq_(cseq), status_(status) {}
30     virtual ~RtspResponse() = default;
31 
AddCustomHeader(const std::string & header)32     RtspResponse &AddCustomHeader(const std::string &header)
33     {
34         customHeaders_ += header;
35         return *this;
36     }
37 
SetSession(const std::string & session)38     RtspResponse &SetSession(const std::string &session)
39     {
40         session_ = session;
41         return *this;
42     }
43 
SetTimeout(int32_t timeout)44     RtspResponse &SetTimeout(int32_t timeout)
45     {
46         timeout_ = timeout;
47         return *this;
48     }
49 
GetCSeq()50     int32_t GetCSeq() const { return cSeq_; }
51 
GetDate()52     std::string GetDate() const { return date_; }
53 
GetStatus()54     int32_t GetStatus() const { return status_; }
55 
GetSession()56     std::string GetSession() const { return session_; }
57 
GetDigestRealm()58     std::string GetDigestRealm() const { return digestRealm_; }
59 
GetNonce()60     std::string GetNonce() const { return nonce_; }
61 
GetTimeout()62     int32_t GetTimeout() const { return timeout_; }
63 
GetBody()64     std::list<std::string> GetBody() const { return body_; }
65 
ClearCustomHeader()66     void ClearCustomHeader() { customHeaders_.clear(); }
67 
68     virtual std::string Stringify();
69     virtual RtspError Parse(const std::string &response);
70     std::string GetToken(const std::string &token) const;
71 
72 protected:
73     std::list<std::string> body_;
74 
75 private:
76     int32_t cSeq_ = 0;
77     int32_t status_ = 200;
78     int32_t timeout_ = -1;
79 
80     std::string date_;
81     std::string nonce_;
82     std::string session_;
83     std::string customHeaders_;
84     std::string digestRealm_;
85 
86     std::unordered_map<std::string, std::string> tokens_;
87 };
88 
89 class RtspResponseOptions : public RtspResponse {
90 public:
RtspResponseOptions(int32_t cseq,int32_t status)91     RtspResponseOptions(int32_t cseq, int32_t status) : RtspResponse(cseq, status) {}
SetPublicItems(const std::string & lists)92     RtspResponseOptions &SetPublicItems(const std::string &lists)
93     {
94         publicItems_ = lists;
95         return *this;
96     }
97 
98     std::string Stringify() override;
99 
100 private:
101     std::string publicItems_;
102 };
103 
104 class RtspResponseDescribe : public RtspResponse {
105 public:
RtspResponseDescribe(int32_t cseq,int32_t status)106     RtspResponseDescribe(int32_t cseq, int32_t status) : RtspResponse(cseq, status) {}
107     std::string Stringify() override;
108 
AddBodyItem(const std::string & line)109     RtspResponseDescribe &AddBodyItem(const std::string &line)
110     {
111         body_.emplace_back(line);
112         return *this;
113     }
114 
SetUrl(const std::string & url)115     RtspResponseDescribe &SetUrl(const std::string &url)
116     {
117         url_ = url;
118         return *this;
119     }
120 
121 private:
122     std::string url_;
123 };
124 
125 class RtspResponseSetup : public RtspResponse {
126 public:
RtspResponseSetup(int32_t cseq,int32_t status)127     RtspResponseSetup(int32_t cseq, int32_t status) : RtspResponse(cseq, status) {}
128     std::string Stringify() override;
129 
SetDestination(const std::string & destination)130     RtspResponseSetup &SetDestination(const std::string &destination)
131     {
132         destination_ = destination;
133         return *this;
134     }
135 
SetSource(const std::string & source)136     RtspResponseSetup &SetSource(const std::string &source)
137     {
138         source_ = source;
139         return *this;
140     }
141 
142     RtspResponseSetup &SetClientPort(int32_t minClientPort, int32_t maxClientPort = 0)
143     {
144         minClientPort_ = minClientPort;
145         if (maxClientPort > minClientPort) {
146             maxServerPort_ = maxClientPort;
147         }
148 
149         return *this;
150     }
151 
152     RtspResponseSetup &SetServerPort(int32_t minServerPort, int32_t maxServerPort = 0)
153     {
154         minServerPort_ = minServerPort;
155         if (maxServerPort > minServerPort) {
156             maxServerPort_ = maxServerPort;
157         }
158 
159         return *this;
160     }
161 
162 private:
163     int32_t minClientPort_ = 0;
164     int32_t maxClientPort_ = 0;
165     int32_t minServerPort_ = 0;
166     int32_t maxServerPort_ = 0;
167 
168     std::string source_;
169     std::string destination_;
170 };
171 
172 class RtspResponsePlay : public RtspResponse {
173 public:
RtspResponsePlay(int32_t cseq,int32_t status)174     RtspResponsePlay(int32_t cseq, int32_t status) : RtspResponse(cseq, status) {}
175 };
176 
177 class RtspResponseTeardown : public RtspResponse {
178 public:
RtspResponseTeardown(int32_t cseq,int32_t status)179     RtspResponseTeardown(int32_t cseq, int32_t status) : RtspResponse(cseq, status) {}
180 };
181 
182 class RtspResponseGetParameter : public RtspResponse {
183 public:
RtspResponseGetParameter(int32_t cseq,int32_t status)184     RtspResponseGetParameter(int32_t cseq, int32_t status) : RtspResponse(cseq, status) {}
185     std::string Stringify() override;
186 
AddBodyItem(const std::string & line)187     RtspResponseGetParameter &AddBodyItem(const std::string &line)
188     {
189         body_.emplace_back(line);
190         return *this;
191     }
192 
193 private:
194     std::list<std::string> body_;
195 };
196 
197 class RtspResponseSetParameter : public RtspResponse {
198 public:
RtspResponseSetParameter(int32_t cseq,int32_t status)199     RtspResponseSetParameter(int32_t cseq, int32_t status) : RtspResponse(cseq, status) {}
200 };
201 } // namespace Sharing
202 } // namespace OHOS
203 #endif // OHOS_SHARING_RTSP_RESPONSE_H
204