• 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_REQUEST_H
17 #define OHOS_SHARING_RTSP_REQUEST_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 RtspRequest {
27 public:
28     RtspRequest() = default;
RtspRequest(const std::string & request)29     explicit RtspRequest(const std::string &request) { Parse(request); }
30 
RtspRequest(const std::string & method,int32_t cseq,const std::string & url)31     RtspRequest(const std::string &method, int32_t cseq, const std::string &url)
32         : cSeq_(cseq), method_(method), url_(url){};
33     virtual ~RtspRequest() = default;
34 
SetMethod(const std::string & method)35     RtspRequest &SetMethod(const std::string &method)
36     {
37         method_ = method;
38         return *this;
39     }
40 
GetMethod()41     std::string GetMethod() const { return method_; }
42 
SetCSeq(int32_t cSeq)43     RtspRequest &SetCSeq(int32_t cSeq)
44     {
45         cSeq_ = cSeq;
46         return *this;
47     }
48 
GetCSeq()49     int32_t GetCSeq() const { return cSeq_; }
50 
SetUrl(const std::string & url)51     RtspRequest &SetUrl(const std::string &url)
52     {
53         url_ = url;
54         return *this;
55     }
56 
GetUrl()57     std::string GetUrl() const { return url_; }
58 
SetUserAgent(const std::string & userAgent)59     RtspRequest &SetUserAgent(const std::string &userAgent)
60     {
61         userAgent_ = userAgent;
62         return *this;
63     }
64 
GetUserAgent()65     std::string GetUserAgent() const { return userAgent_; }
66 
SetSession(const std::string & session)67     RtspRequest &SetSession(const std::string &session)
68     {
69         session_ = session;
70         return *this;
71     }
72 
SetTimeout(int32_t timeout)73     RtspRequest &SetTimeout(int32_t timeout)
74     {
75         timeout_ = timeout;
76         return *this;
77     }
78 
GetSession()79     std::string GetSession() const { return session_; }
80 
AddCustomHeader(const std::string & header)81     RtspRequest &AddCustomHeader(const std::string &header)
82     {
83         customHeaders_ += header;
84         return *this;
85     }
86 
ClearCustomHeader()87     void ClearCustomHeader() { customHeaders_.clear(); }
88 
GetBody()89     std::list<std::string> GetBody() const { return body_; }
90 
91     virtual std::string Stringify();
92     virtual RtspError Parse(const std::string &request);
93 
94     std::string GetToken(const std::string &token);
95 
96 protected:
97     std::list<std::string> body_;
98 
99 private:
100     int32_t cSeq_ = 0;
101     int32_t timeout_ = -1;
102     std::string method_;
103     std::string session_;
104     std::string url_ = "*";
105     std::string customHeaders_;
106     std::string userAgent_ = "KaihongOS";
107 
108     std::unordered_map<std::string, std::string> tokens_;
109 };
110 
111 class RtspRequestOptions : public RtspRequest {
112 public:
RtspRequestOptions(int32_t cseq,const std::string & url)113     RtspRequestOptions(int32_t cseq, const std::string &url) : RtspRequest(RTSP_METHOD_OPTIONS, cseq, url) {}
SetRequire(const std::string & methods)114     RtspRequestOptions &SetRequire(const std::string &methods)
115     {
116         if (!methods.empty()) {
117             require_ = methods;
118         }
119 
120         return *this;
121     }
122 
123     std::string Stringify() override;
124 
125 private:
126     std::string require_;
127 };
128 
129 class RtspRequestDescribe : public RtspRequest {
130 public:
RtspRequestDescribe(int32_t cseq,const std::string & url)131     RtspRequestDescribe(int32_t cseq, const std::string &url) : RtspRequest(RTSP_METHOD_DESCRIBE, cseq, url) {}
SetAcceptType(const std::string & type)132     RtspRequestDescribe &SetAcceptType(const std::string &type)
133     {
134         if (!type.empty()) {
135             acceptType_ = type;
136         }
137 
138         return *this;
139     }
140 
141     std::string Stringify() override;
142 
143 private:
144     std::string acceptType_ = "application/sdp";
145 };
146 
147 class RtspRequestSetup : public RtspRequest {
148 public:
149     RtspRequestSetup() = default;
RtspRequestSetup(int32_t cseq,const std::string & url)150     RtspRequestSetup(int32_t cseq, const std::string &url) : RtspRequest(RTSP_METHOD_SETUP, cseq, url) {}
151 
152     RtspRequestSetup &SetClientPort(int32_t minPort, int32_t maxPort = 0)
153     {
154         if (minPort > 0) {
155             minPort_ = minPort;
156         }
157         if (maxPort > minPort) {
158             maxPort_ = maxPort;
159         }
160 
161         return *this;
162     }
163 
164     std::string Stringify() override;
165 
166 private:
167     int32_t minPort_ = 0;
168     int32_t maxPort_ = 0;
169 };
170 
171 class RtspRequestPlay : public RtspRequest {
172 public:
173     RtspRequestPlay() = default;
RtspRequestPlay(int32_t cseq,const std::string & url)174     RtspRequestPlay(int32_t cseq, const std::string &url) : RtspRequest(RTSP_METHOD_PLAY, cseq, url) {}
SetRangeStart(float start)175     RtspRequestPlay &SetRangeStart(float start)
176     {
177         range_ = start;
178         return *this;
179     }
180 
181     std::string Stringify() override;
182 
183 private:
184     float range_ = -1;
185 };
186 
187 class RtspRequestPause : public RtspRequest {
188 public:
189     RtspRequestPause() = default;
RtspRequestPause(int32_t cseq,const std::string & url)190     RtspRequestPause(int32_t cseq, const std::string &url) : RtspRequest(RTSP_METHOD_PAUSE, cseq, url) {}
191 };
192 
193 class RtspRequestTeardown : public RtspRequest {
194 public:
195     RtspRequestTeardown() = default;
RtspRequestTeardown(int32_t cseq,const std::string & url)196     RtspRequestTeardown(int32_t cseq, const std::string &url) : RtspRequest(RTSP_METHOD_TEARDOWN, cseq, url) {}
197 };
198 
199 class RtspRequestParameter : public RtspRequest {
200 public:
201     RtspRequestParameter() = default;
RtspRequestParameter(const std::string & method,int32_t cseq,const std::string & url)202     RtspRequestParameter(const std::string &method, int32_t cseq, const std::string &url)
203         : RtspRequest(method, cseq, url){};
204     ~RtspRequestParameter() override = default;
205 
AddBodyItem(const std::string & line)206     RtspRequestParameter &AddBodyItem(const std::string &line)
207     {
208         body_.emplace_back(line);
209         return *this;
210     }
211 
212     std::string Stringify() override;
213 };
214 
215 class RtspRequestGetParameter : public RtspRequestParameter {
216 public:
RtspRequestGetParameter(int32_t cseq,const std::string & url)217     RtspRequestGetParameter(int32_t cseq, const std::string &url)
218         : RtspRequestParameter(RTSP_METHOD_GET_PARAMETER, cseq, url){};
219 };
220 
221 class RtspRequestSetParameter : public RtspRequestParameter {
222 public:
RtspRequestSetParameter(int32_t cseq,const std::string & url)223     RtspRequestSetParameter(int32_t cseq, const std::string &url)
224         : RtspRequestParameter(RTSP_METHOD_SET_PARAMETER, cseq, url){};
225 };
226 } // namespace Sharing
227 } // namespace OHOS
228 #endif // OHOS_SHARING_RTSP_REQUEST_H
229