• 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_SDP_H
17 #define OHOS_SHARING_RTSP_SDP_H
18 
19 #include <cstdint>
20 #include <list>
21 #include <memory>
22 #include <regex>
23 #include <string>
24 #include <vector>
25 
26 namespace OHOS {
27 namespace Sharing {
28 
29 /// SDP: Session Description Protocol
30 /// [[RFC 8866](https://datatracker.ietf.org/doc/html/rfc8866)]
31 
32 // o=<username> <sess-id> <sess-version> <nettype> <addrtype> <unicast-address>
33 struct SessionOrigin {
34     bool Parse(const std::string &origin);
35 
36     uint32_t sessionVersion;
37 
38     std::string netType;
39     std::string username;
40     std::string addrType;
41     std::string sessionId;
42     std::string unicastAddr;
43 };
44 
45 struct SessionTime {
46     // r=<repeat interval> <active duration> <offsets from start-time>
47     std::vector<std::string> repeat;
48     // t=<start-time> <stop-time>
49     std::pair<uint64_t, uint64_t> time{UINT64_MAX, UINT64_MAX};
50     // z=<adjustment time> <offset> <adjustment time> <offset> ...
51     std::string zone;
52 };
53 
54 struct SessionDescription {
55     uint8_t version;        // v=
56     std::string name;       // s=
57     std::string info;       // i=
58     std::string uri;        // u=
59     std::string email;      // e=
60     std::string phone;      // p=
61     std::string connection; // c=
62 
63     SessionTime time;                    // time descriptions
64     SessionOrigin origin;                // o=
65     std::vector<std::string> bandwidth;  // b=
66     std::vector<std::string> attributes; // a=
67 };
68 
69 // m=<media_> <port> <proto> <fmt> ...
70 struct MediaLine {
71     bool Parse(const std::string &mediaLine);
72 
73     uint16_t port;
74 
75     int32_t fmt;
76 
77     std::string mediaType;
78     std::string protoType;
79 };
80 
81 class MediaDescription {
82 public:
83     friend class RtspSdp;
84 
GetMediaType()85     std::string GetMediaType() const
86     {
87         return media_.mediaType;
88     }
89 
GetPayloadType()90     int32_t GetPayloadType() const
91     {
92         return media_.fmt;
93     }
94 
95     std::string GetRtpMap() const;
96     std::string GetTrackId() const;
97 
98     std::vector<uint8_t> GetVideoSps();
99     std::vector<uint8_t> GetVideoPps();
100     std::pair<int32_t, int32_t> GetVideoSize();
101 
102     int32_t GetAudioChannels() const;
103     int32_t GetAudioSamplingRate() const;
104     std::string GetAudioConfig() const;
105 
106 private:
107     bool ParseSpsPps();
108 
109     int32_t GetSe(uint8_t *buf, uint32_t nLen, uint32_t &pos);
110     int32_t GetUe(const uint8_t *buf, uint32_t nLen, uint32_t &pos);
111     int32_t GetU(uint8_t bitCount, const uint8_t *buf, uint32_t &pos);
112 
113     void ExtractNaluRbsp(uint8_t *buf, uint32_t *bufSize);
114 
115 private:
116     std::string title_;                   // i=
117     std::string connection_;              // c=
118     std::vector<uint8_t> sps_;
119     std::vector<uint8_t> pps_;
120     std::vector<std::string> bandwidth_;  // b=
121     std::vector<std::string> attributes_; // a=
122 
123     MediaLine media_;                     // m=
124 };
125 
126 class RtspSdp {
127 public:
128     bool Parse(const std::string &sdpStr);
129     bool Parse(const std::list<std::string> &sdpLines);
130 
GetOrigin()131     SessionOrigin GetOrigin() const
132     {
133         return session_.origin;
134     }
135 
GetName()136     std::string GetName() const
137     {
138         return session_.name;
139     }
140 
GetInfo()141     std::string GetInfo() const
142     {
143         return session_.info;
144     }
145 
GetUri()146     std::string GetUri() const
147     {
148         return session_.uri;
149     }
150 
GetEmail()151     std::string GetEmail() const
152     {
153         return session_.email;
154     }
155 
GetPhone()156     std::string GetPhone() const
157     {
158         return session_.phone;
159     }
160 
GetConnection()161     std::string GetConnection() const
162     {
163         return session_.connection;
164     }
165 
GetBandwidth()166     std::vector<std::string> GetBandwidth() const
167     {
168         return session_.bandwidth;
169     }
170 
GetTime()171     std::pair<uint64_t, uint64_t> GetTime() const
172     {
173         return session_.time.time;
174     }
175 
getAttributes()176     std::vector<std::string> getAttributes() const
177     {
178         return session_.attributes;
179     }
180 
181     std::shared_ptr<MediaDescription> GetVideoTrack();
182     std::shared_ptr<MediaDescription> GetAudioTrack();
183 
184 private:
185     std::vector<std::shared_ptr<MediaDescription>> media_;
186 
187     SessionDescription session_;
188 };
189 } // namespace Sharing
190 } // namespace OHOS
191 #endif // OHOS_SHARING_RTSP_SDP_H
192