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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "sdp/SdpMediaSection.h"
#include "sdp/RsdparsaSdpMediaSection.h"
#include "sdp/RsdparsaSdpGlue.h"
#include "sdp/RsdparsaSdpInc.h"
#include <ostream>
#ifdef CRLF
# undef CRLF
#endif
#define CRLF "\r\n"
namespace mozilla {
RsdparsaSdpMediaSection::RsdparsaSdpMediaSection(
size_t level, RsdparsaSessionHandle session,
const RustMediaSection* const section,
const RsdparsaSdpAttributeList* sessionLevel)
: SdpMediaSection(level), mSession(std::move(session)), mSection(section) {
switch (sdp_rust_get_media_type(section)) {
case RustSdpMediaValue::kRustAudio:
mMediaType = kAudio;
break;
case RustSdpMediaValue::kRustVideo:
mMediaType = kVideo;
break;
case RustSdpMediaValue::kRustApplication:
mMediaType = kApplication;
break;
}
RsdparsaSessionHandle attributeSession(sdp_new_reference(mSession.get()));
mAttributeList.reset(new RsdparsaSdpAttributeList(std::move(attributeSession),
section, sessionLevel));
LoadFormats();
LoadConnection();
}
unsigned int RsdparsaSdpMediaSection::GetPort() const {
return sdp_get_media_port(mSection);
}
void RsdparsaSdpMediaSection::SetPort(unsigned int port) {
sdp_set_media_port(mSection, port);
}
unsigned int RsdparsaSdpMediaSection::GetPortCount() const {
return sdp_get_media_port_count(mSection);
}
SdpMediaSection::Protocol RsdparsaSdpMediaSection::GetProtocol() const {
switch (sdp_get_media_protocol(mSection)) {
case RustSdpProtocolValue::kRustRtpSavpf:
return kRtpSavpf;
case RustSdpProtocolValue::kRustUdpTlsRtpSavp:
return kUdpTlsRtpSavp;
case RustSdpProtocolValue::kRustTcpDtlsRtpSavp:
return kTcpDtlsRtpSavp;
case RustSdpProtocolValue::kRustUdpTlsRtpSavpf:
return kUdpTlsRtpSavpf;
case RustSdpProtocolValue::kRustTcpDtlsRtpSavpf:
return kTcpDtlsRtpSavpf;
case RustSdpProtocolValue::kRustDtlsSctp:
return kDtlsSctp;
case RustSdpProtocolValue::kRustUdpDtlsSctp:
return kUdpDtlsSctp;
case RustSdpProtocolValue::kRustTcpDtlsSctp:
return kTcpDtlsSctp;
case RustSdpProtocolValue::kRustRtpAvp:
return kRtpAvp;
case RustSdpProtocolValue::kRustRtpAvpf:
return kRtpAvpf;
case RustSdpProtocolValue::kRustRtpSavp:
return kRtpSavp;
}
MOZ_CRASH("invalid media protocol");
}
const SdpConnection& RsdparsaSdpMediaSection::GetConnection() const {
MOZ_ASSERT(mConnection);
return *mConnection;
}
SdpConnection& RsdparsaSdpMediaSection::GetConnection() {
MOZ_ASSERT(mConnection);
return *mConnection;
}
uint32_t RsdparsaSdpMediaSection::GetBandwidth(const std::string& type) const {
return sdp_get_media_bandwidth(mSection, type.c_str());
}
const std::vector<std::string>& RsdparsaSdpMediaSection::GetFormats() const {
return mFormats;
}
const SdpAttributeList& RsdparsaSdpMediaSection::GetAttributeList() const {
return *mAttributeList;
}
SdpAttributeList& RsdparsaSdpMediaSection::GetAttributeList() {
return *mAttributeList;
}
SdpDirectionAttribute RsdparsaSdpMediaSection::GetDirectionAttribute() const {
return SdpDirectionAttribute(mAttributeList->GetDirection());
}
void RsdparsaSdpMediaSection::AddCodec(const std::string& pt,
const std::string& name,
uint32_t clockrate, uint16_t channels) {
StringView rustName{name.c_str(), name.size()};
// call the rust interface
auto nr = sdp_media_add_codec(mSection, std::stoul(pt), rustName, clockrate,
channels);
if (NS_SUCCEEDED(nr)) {
// If the rust call was successful, adjust the shadow C++ structures
mFormats.push_back(pt);
// Add a rtpmap in mAttributeList
SdpRtpmapAttributeList* rtpmap = new SdpRtpmapAttributeList();
if (mAttributeList->HasAttribute(SdpAttribute::kRtpmapAttribute)) {
const SdpRtpmapAttributeList& old = mAttributeList->GetRtpmap();
for (auto it = old.mRtpmaps.begin(); it != old.mRtpmaps.end(); ++it) {
rtpmap->mRtpmaps.push_back(*it);
}
}
SdpRtpmapAttributeList::CodecType codec =
SdpRtpmapAttributeList::kOtherCodec;
if (name == "opus") {
codec = SdpRtpmapAttributeList::kOpus;
} else if (name == "VP8") {
codec = SdpRtpmapAttributeList::kVP8;
} else if (name == "VP9") {
codec = SdpRtpmapAttributeList::kVP9;
} else if (name == "H264") {
codec = SdpRtpmapAttributeList::kH264;
}
rtpmap->PushEntry(pt, codec, name, clockrate, channels);
mAttributeList->SetAttribute(rtpmap);
}
}
void RsdparsaSdpMediaSection::ClearCodecs() {
// Clear the codecs in rust
sdp_media_clear_codecs(mSection);
mFormats.clear();
mAttributeList->RemoveAttribute(SdpAttribute::kRtpmapAttribute);
mAttributeList->RemoveAttribute(SdpAttribute::kFmtpAttribute);
mAttributeList->RemoveAttribute(SdpAttribute::kSctpmapAttribute);
mAttributeList->RemoveAttribute(SdpAttribute::kRtcpFbAttribute);
}
void RsdparsaSdpMediaSection::AddDataChannel(const std::string& name,
uint16_t port, uint16_t streams,
uint32_t message_size) {
StringView rustName{name.c_str(), name.size()};
auto nr = sdp_media_add_datachannel(mSection, rustName, port, streams,
message_size);
if (NS_SUCCEEDED(nr)) {
// Update the formats
mFormats.clear();
LoadFormats();
// Update the attribute list
RsdparsaSessionHandle sessHandle(sdp_new_reference(mSession.get()));
auto sessAttributes = mAttributeList->mSessionAttributes;
mAttributeList.reset(new RsdparsaSdpAttributeList(
std::move(sessHandle), mSection, sessAttributes));
}
}
void RsdparsaSdpMediaSection::Serialize(std::ostream& os) const {
os << "m=" << mMediaType << " " << GetPort();
if (GetPortCount()) {
os << "/" << GetPortCount();
}
os << " " << GetProtocol();
for (auto i = mFormats.begin(); i != mFormats.end(); ++i) {
os << " " << (*i);
}
os << CRLF;
// We dont do i=
if (mConnection) {
os << *mConnection;
}
BandwidthVec* bwVec = sdp_get_media_bandwidth_vec(mSection);
char* bwString = sdp_serialize_bandwidth(bwVec);
if (bwString) {
os << bwString;
sdp_free_string(bwString);
}
// We dont do k= because they're evil
os << *mAttributeList;
}
void RsdparsaSdpMediaSection::LoadFormats() {
RustSdpFormatType formatType = sdp_get_format_type(mSection);
if (formatType == RustSdpFormatType::kRustIntegers) {
U32Vec* vec = sdp_get_format_u32_vec(mSection);
size_t len = u32_vec_len(vec);
for (size_t i = 0; i < len; i++) {
uint32_t val;
u32_vec_get(vec, i, &val);
mFormats.push_back(std::to_string(val));
}
} else {
StringVec* vec = sdp_get_format_string_vec(mSection);
mFormats = convertStringVec(vec);
}
}
UniquePtr<SdpConnection> convertRustConnection(RustSdpConnection conn) {
auto address = convertExplicitlyTypedAddress(&conn.addr);
return MakeUnique<SdpConnection>(address.first, address.second, conn.ttl,
conn.amount);
}
void RsdparsaSdpMediaSection::LoadConnection() {
RustSdpConnection conn;
nsresult nr;
if (sdp_media_has_connection(mSection)) {
nr = sdp_get_media_connection(mSection, &conn);
if (NS_SUCCEEDED(nr)) {
mConnection = convertRustConnection(conn);
}
} else if (sdp_session_has_connection(mSession.get())) {
nr = sdp_get_session_connection(mSession.get(), &conn);
if (NS_SUCCEEDED(nr)) {
mConnection = convertRustConnection(conn);
}
}
}
} // namespace mozilla
|