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