You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2018 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "pc/sdp_serializer.h"
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. #include "absl/algorithm/container.h"
  15. #include "api/jsep.h"
  16. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/string_encode.h"
  19. #include "rtc_base/string_to_number.h"
  20. #include "rtc_base/strings/string_builder.h"
  21. using cricket::RidDescription;
  22. using cricket::RidDirection;
  23. using cricket::SimulcastDescription;
  24. using cricket::SimulcastLayer;
  25. using cricket::SimulcastLayerList;
  26. namespace webrtc {
  27. namespace {
  28. // delimiters
  29. const char kDelimiterComma[] = ",";
  30. const char kDelimiterCommaChar = ',';
  31. const char kDelimiterEqual[] = "=";
  32. const char kDelimiterEqualChar = '=';
  33. const char kDelimiterSemicolon[] = ";";
  34. const char kDelimiterSemicolonChar = ';';
  35. const char kDelimiterSpace[] = " ";
  36. const char kDelimiterSpaceChar = ' ';
  37. // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
  38. // https://tools.ietf.org/html/draft-ietf-mmusic-rid-15#section-10
  39. const char kSimulcastPausedStream[] = "~";
  40. const char kSimulcastPausedStreamChar = '~';
  41. const char kSendDirection[] = "send";
  42. const char kReceiveDirection[] = "recv";
  43. const char kPayloadType[] = "pt";
  44. RTCError ParseError(const std::string& message) {
  45. return RTCError(RTCErrorType::SYNTAX_ERROR, message);
  46. }
  47. // These methods serialize simulcast according to the specification:
  48. // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
  49. rtc::StringBuilder& operator<<(rtc::StringBuilder& builder,
  50. const SimulcastLayer& simulcast_layer) {
  51. if (simulcast_layer.is_paused) {
  52. builder << kSimulcastPausedStream;
  53. }
  54. builder << simulcast_layer.rid;
  55. return builder;
  56. }
  57. rtc::StringBuilder& operator<<(
  58. rtc::StringBuilder& builder,
  59. const std::vector<SimulcastLayer>& layer_alternatives) {
  60. bool first = true;
  61. for (const SimulcastLayer& rid : layer_alternatives) {
  62. if (!first) {
  63. builder << kDelimiterComma;
  64. }
  65. builder << rid;
  66. first = false;
  67. }
  68. return builder;
  69. }
  70. rtc::StringBuilder& operator<<(rtc::StringBuilder& builder,
  71. const SimulcastLayerList& simulcast_layers) {
  72. bool first = true;
  73. for (const auto& alternatives : simulcast_layers) {
  74. if (!first) {
  75. builder << kDelimiterSemicolon;
  76. }
  77. builder << alternatives;
  78. first = false;
  79. }
  80. return builder;
  81. }
  82. // This method deserializes simulcast according to the specification:
  83. // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
  84. // sc-str-list = sc-alt-list *( ";" sc-alt-list )
  85. // sc-alt-list = sc-id *( "," sc-id )
  86. // sc-id-paused = "~"
  87. // sc-id = [sc-id-paused] rid-id
  88. // rid-id = 1*(alpha-numeric / "-" / "_") ; see: I-D.ietf-mmusic-rid
  89. RTCErrorOr<SimulcastLayerList> ParseSimulcastLayerList(const std::string& str) {
  90. std::vector<std::string> tokens;
  91. rtc::tokenize_with_empty_tokens(str, kDelimiterSemicolonChar, &tokens);
  92. if (tokens.empty()) {
  93. return ParseError("Layer list cannot be empty.");
  94. }
  95. SimulcastLayerList result;
  96. for (const std::string& token : tokens) {
  97. if (token.empty()) {
  98. return ParseError("Simulcast alternative layer list is empty.");
  99. }
  100. std::vector<std::string> rid_tokens;
  101. rtc::tokenize_with_empty_tokens(token, kDelimiterCommaChar, &rid_tokens);
  102. if (rid_tokens.empty()) {
  103. return ParseError("Simulcast alternative layer list is malformed.");
  104. }
  105. std::vector<SimulcastLayer> layers;
  106. for (const std::string& rid_token : rid_tokens) {
  107. if (rid_token.empty() || rid_token == kSimulcastPausedStream) {
  108. return ParseError("Rid must not be empty.");
  109. }
  110. bool paused = rid_token[0] == kSimulcastPausedStreamChar;
  111. std::string rid = paused ? rid_token.substr(1) : rid_token;
  112. layers.push_back(SimulcastLayer(rid, paused));
  113. }
  114. result.AddLayerWithAlternatives(layers);
  115. }
  116. return std::move(result);
  117. }
  118. webrtc::RTCError ParseRidPayloadList(const std::string& payload_list,
  119. RidDescription* rid_description) {
  120. RTC_DCHECK(rid_description);
  121. std::vector<int>& payload_types = rid_description->payload_types;
  122. // Check that the description doesn't have any payload types or restrictions.
  123. // If the pt= field is specified, it must be first and must not repeat.
  124. if (!payload_types.empty()) {
  125. return ParseError("Multiple pt= found in RID Description.");
  126. }
  127. if (!rid_description->restrictions.empty()) {
  128. return ParseError("Payload list must appear first in the restrictions.");
  129. }
  130. // If the pt= field is specified, it must have a value.
  131. if (payload_list.empty()) {
  132. return ParseError("Payload list must have at least one value.");
  133. }
  134. // Tokenize the ',' delimited list
  135. std::vector<std::string> string_payloads;
  136. rtc::tokenize(payload_list, kDelimiterCommaChar, &string_payloads);
  137. if (string_payloads.empty()) {
  138. return ParseError("Payload list must have at least one value.");
  139. }
  140. for (const std::string& payload_type : string_payloads) {
  141. absl::optional<int> value = rtc::StringToNumber<int>(payload_type);
  142. if (!value.has_value()) {
  143. return ParseError("Invalid payload type: " + payload_type);
  144. }
  145. // Check if the value already appears in the payload list.
  146. if (absl::c_linear_search(payload_types, value.value())) {
  147. return ParseError("Duplicate payload type in list: " + payload_type);
  148. }
  149. payload_types.push_back(value.value());
  150. }
  151. return RTCError::OK();
  152. }
  153. } // namespace
  154. std::string SdpSerializer::SerializeSimulcastDescription(
  155. const cricket::SimulcastDescription& simulcast) const {
  156. rtc::StringBuilder sb;
  157. std::string delimiter;
  158. if (!simulcast.send_layers().empty()) {
  159. sb << kSendDirection << kDelimiterSpace << simulcast.send_layers();
  160. delimiter = kDelimiterSpace;
  161. }
  162. if (!simulcast.receive_layers().empty()) {
  163. sb << delimiter << kReceiveDirection << kDelimiterSpace
  164. << simulcast.receive_layers();
  165. }
  166. return sb.str();
  167. }
  168. // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
  169. // a:simulcast:<send> <streams> <recv> <streams>
  170. // Formal Grammar
  171. // sc-value = ( sc-send [SP sc-recv] ) / ( sc-recv [SP sc-send] )
  172. // sc-send = %s"send" SP sc-str-list
  173. // sc-recv = %s"recv" SP sc-str-list
  174. // sc-str-list = sc-alt-list *( ";" sc-alt-list )
  175. // sc-alt-list = sc-id *( "," sc-id )
  176. // sc-id-paused = "~"
  177. // sc-id = [sc-id-paused] rid-id
  178. // rid-id = 1*(alpha-numeric / "-" / "_") ; see: I-D.ietf-mmusic-rid
  179. RTCErrorOr<SimulcastDescription> SdpSerializer::DeserializeSimulcastDescription(
  180. absl::string_view string) const {
  181. std::vector<std::string> tokens;
  182. rtc::tokenize(std::string(string), kDelimiterSpaceChar, &tokens);
  183. if (tokens.size() != 2 && tokens.size() != 4) {
  184. return ParseError("Must have one or two <direction, streams> pairs.");
  185. }
  186. bool bidirectional = tokens.size() == 4; // indicates both send and recv
  187. // Tokens 0, 2 (if exists) should be send / recv
  188. if ((tokens[0] != kSendDirection && tokens[0] != kReceiveDirection) ||
  189. (bidirectional && tokens[2] != kSendDirection &&
  190. tokens[2] != kReceiveDirection) ||
  191. (bidirectional && tokens[0] == tokens[2])) {
  192. return ParseError("Valid values: send / recv.");
  193. }
  194. // Tokens 1, 3 (if exists) should be alternative layer lists
  195. RTCErrorOr<SimulcastLayerList> list1, list2;
  196. list1 = ParseSimulcastLayerList(tokens[1]);
  197. if (!list1.ok()) {
  198. return list1.MoveError();
  199. }
  200. if (bidirectional) {
  201. list2 = ParseSimulcastLayerList(tokens[3]);
  202. if (!list2.ok()) {
  203. return list2.MoveError();
  204. }
  205. }
  206. // Set the layers so that list1 is for send and list2 is for recv
  207. if (tokens[0] != kSendDirection) {
  208. std::swap(list1, list2);
  209. }
  210. // Set the layers according to which pair is send and which is recv
  211. // At this point if the simulcast is unidirectional then
  212. // either |list1| or |list2| will be in 'error' state indicating that
  213. // the value should not be used.
  214. SimulcastDescription simulcast;
  215. if (list1.ok()) {
  216. simulcast.send_layers() = list1.MoveValue();
  217. }
  218. if (list2.ok()) {
  219. simulcast.receive_layers() = list2.MoveValue();
  220. }
  221. return std::move(simulcast);
  222. }
  223. std::string SdpSerializer::SerializeRidDescription(
  224. const RidDescription& rid_description) const {
  225. RTC_DCHECK(!rid_description.rid.empty());
  226. RTC_DCHECK(rid_description.direction == RidDirection::kSend ||
  227. rid_description.direction == RidDirection::kReceive);
  228. rtc::StringBuilder builder;
  229. builder << rid_description.rid << kDelimiterSpace
  230. << (rid_description.direction == RidDirection::kSend
  231. ? kSendDirection
  232. : kReceiveDirection);
  233. const auto& payload_types = rid_description.payload_types;
  234. const auto& restrictions = rid_description.restrictions;
  235. // First property is separated by ' ', the next ones by ';'.
  236. const char* propertyDelimiter = kDelimiterSpace;
  237. // Serialize any codecs in the description.
  238. if (!payload_types.empty()) {
  239. builder << propertyDelimiter << kPayloadType << kDelimiterEqual;
  240. propertyDelimiter = kDelimiterSemicolon;
  241. const char* formatDelimiter = "";
  242. for (int payload_type : payload_types) {
  243. builder << formatDelimiter << payload_type;
  244. formatDelimiter = kDelimiterComma;
  245. }
  246. }
  247. // Serialize any restrictions in the description.
  248. for (const auto& pair : restrictions) {
  249. // Serialize key=val pairs. =val part is ommitted if val is empty.
  250. builder << propertyDelimiter << pair.first;
  251. if (!pair.second.empty()) {
  252. builder << kDelimiterEqual << pair.second;
  253. }
  254. propertyDelimiter = kDelimiterSemicolon;
  255. }
  256. return builder.str();
  257. }
  258. // https://tools.ietf.org/html/draft-ietf-mmusic-rid-15#section-10
  259. // Formal Grammar
  260. // rid-syntax = %s"a=rid:" rid-id SP rid-dir
  261. // [ rid-pt-param-list / rid-param-list ]
  262. // rid-id = 1*(alpha-numeric / "-" / "_")
  263. // rid-dir = %s"send" / %s"recv"
  264. // rid-pt-param-list = SP rid-fmt-list *( ";" rid-param )
  265. // rid-param-list = SP rid-param *( ";" rid-param )
  266. // rid-fmt-list = %s"pt=" fmt *( "," fmt )
  267. // rid-param = 1*(alpha-numeric / "-") [ "=" param-val ]
  268. // param-val = *( %x20-58 / %x60-7E )
  269. // ; Any printable character except semicolon
  270. RTCErrorOr<RidDescription> SdpSerializer::DeserializeRidDescription(
  271. absl::string_view string) const {
  272. std::vector<std::string> tokens;
  273. rtc::tokenize(std::string(string), kDelimiterSpaceChar, &tokens);
  274. if (tokens.size() < 2) {
  275. return ParseError("RID Description must contain <RID> <direction>.");
  276. }
  277. if (tokens.size() > 3) {
  278. return ParseError("Invalid RID Description format. Too many arguments.");
  279. }
  280. if (!IsLegalRsidName(tokens[0])) {
  281. return ParseError("Invalid RID value: " + tokens[0] + ".");
  282. }
  283. if (tokens[1] != kSendDirection && tokens[1] != kReceiveDirection) {
  284. return ParseError("Invalid RID direction. Supported values: send / recv.");
  285. }
  286. RidDirection direction = tokens[1] == kSendDirection ? RidDirection::kSend
  287. : RidDirection::kReceive;
  288. RidDescription rid_description(tokens[0], direction);
  289. // If there is a third argument it is a payload list and/or restriction list.
  290. if (tokens.size() == 3) {
  291. std::vector<std::string> restrictions;
  292. rtc::tokenize(tokens[2], kDelimiterSemicolonChar, &restrictions);
  293. // Check for malformed restriction list, such as ';' or ';;;' etc.
  294. if (restrictions.empty()) {
  295. return ParseError("Invalid RID restriction list: " + tokens[2]);
  296. }
  297. // Parse the restrictions. The payload indicator (pt) can only appear first.
  298. for (const std::string& restriction : restrictions) {
  299. std::vector<std::string> parts;
  300. rtc::tokenize(restriction, kDelimiterEqualChar, &parts);
  301. if (parts.empty() || parts.size() > 2) {
  302. return ParseError("Invalid format for restriction: " + restriction);
  303. }
  304. // |parts| contains at least one value and it does not contain a space.
  305. // Note: |parts| and other values might still contain tab, newline,
  306. // unprintable characters, etc. which will not generate errors here but
  307. // will (most-likely) be ignored by components down stream.
  308. if (parts[0] == kPayloadType) {
  309. RTCError error = ParseRidPayloadList(
  310. parts.size() > 1 ? parts[1] : std::string(), &rid_description);
  311. if (!error.ok()) {
  312. return std::move(error);
  313. }
  314. continue;
  315. }
  316. // Parse |parts| as a key=value pair which allows unspecified values.
  317. if (rid_description.restrictions.find(parts[0]) !=
  318. rid_description.restrictions.end()) {
  319. return ParseError("Duplicate restriction specified: " + parts[0]);
  320. }
  321. rid_description.restrictions[parts[0]] =
  322. parts.size() > 1 ? parts[1] : std::string();
  323. }
  324. }
  325. return std::move(rid_description);
  326. }
  327. } // namespace webrtc