henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #include "webrtc/api/webrtcsdp.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 13 | #include <ctype.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | #include <limits.h> |
| 15 | #include <stdio.h> |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 16 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | #include <algorithm> |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 18 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | #include <string> |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 20 | #include <unordered_map> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 23 | #include "webrtc/api/jsepicecandidate.h" |
| 24 | #include "webrtc/api/jsepsessiondescription.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 25 | #include "webrtc/base/arraysize.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 26 | #include "webrtc/base/common.h" |
| 27 | #include "webrtc/base/logging.h" |
| 28 | #include "webrtc/base/messagedigest.h" |
| 29 | #include "webrtc/base/stringutils.h" |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 30 | // for RtpExtension |
| 31 | #include "webrtc/config.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 32 | #include "webrtc/media/base/codec.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 33 | #include "webrtc/media/base/cryptoparams.h" |
kjellander | f475277 | 2016-03-02 05:42:30 -0800 | [diff] [blame] | 34 | #include "webrtc/media/base/mediaconstants.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 35 | #include "webrtc/media/base/rtputils.h" |
| 36 | #include "webrtc/media/sctp/sctpdataengine.h" |
| 37 | #include "webrtc/p2p/base/candidate.h" |
kjellander | f475277 | 2016-03-02 05:42:30 -0800 | [diff] [blame] | 38 | #include "webrtc/p2p/base/p2pconstants.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 39 | #include "webrtc/p2p/base/port.h" |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame] | 40 | #include "webrtc/pc/mediasession.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | |
| 42 | using cricket::AudioContentDescription; |
| 43 | using cricket::Candidate; |
| 44 | using cricket::Candidates; |
| 45 | using cricket::ContentDescription; |
| 46 | using cricket::ContentInfo; |
| 47 | using cricket::CryptoParams; |
| 48 | using cricket::DataContentDescription; |
| 49 | using cricket::ICE_CANDIDATE_COMPONENT_RTP; |
| 50 | using cricket::ICE_CANDIDATE_COMPONENT_RTCP; |
| 51 | using cricket::kCodecParamMaxBitrate; |
| 52 | using cricket::kCodecParamMaxPTime; |
| 53 | using cricket::kCodecParamMaxQuantization; |
| 54 | using cricket::kCodecParamMinBitrate; |
| 55 | using cricket::kCodecParamMinPTime; |
| 56 | using cricket::kCodecParamPTime; |
| 57 | using cricket::kCodecParamSPropStereo; |
buildbot@webrtc.org | ed97bb0 | 2014-05-07 11:15:20 +0000 | [diff] [blame] | 58 | using cricket::kCodecParamStartBitrate; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | using cricket::kCodecParamStereo; |
| 60 | using cricket::kCodecParamUseInbandFec; |
Minyue Li | 7100dcd | 2015-03-27 05:05:59 +0100 | [diff] [blame] | 61 | using cricket::kCodecParamUseDtx; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | using cricket::kCodecParamSctpProtocol; |
| 63 | using cricket::kCodecParamSctpStreams; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 64 | using cricket::kCodecParamMaxAverageBitrate; |
buildbot@webrtc.org | 5d639b3 | 2014-09-10 07:57:12 +0000 | [diff] [blame] | 65 | using cricket::kCodecParamMaxPlaybackRate; |
stefan@webrtc.org | 85d2794 | 2014-06-09 12:51:39 +0000 | [diff] [blame] | 66 | using cricket::kCodecParamAssociatedPayloadType; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 67 | using cricket::MediaContentDescription; |
| 68 | using cricket::MediaType; |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 69 | using cricket::RtpHeaderExtensions; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | using cricket::SsrcGroup; |
| 71 | using cricket::StreamParams; |
| 72 | using cricket::StreamParamsVec; |
| 73 | using cricket::TransportDescription; |
| 74 | using cricket::TransportInfo; |
| 75 | using cricket::VideoContentDescription; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 76 | using rtc::SocketAddress; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | namespace cricket { |
| 79 | class SessionDescription; |
| 80 | } |
| 81 | |
| 82 | namespace webrtc { |
| 83 | |
| 84 | // Line type |
| 85 | // RFC 4566 |
| 86 | // An SDP session description consists of a number of lines of text of |
| 87 | // the form: |
| 88 | // <type>=<value> |
| 89 | // where <type> MUST be exactly one case-significant character. |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 90 | static const int kLinePrefixLength = 2; // Length of <type>= |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | static const char kLineTypeVersion = 'v'; |
| 92 | static const char kLineTypeOrigin = 'o'; |
| 93 | static const char kLineTypeSessionName = 's'; |
| 94 | static const char kLineTypeSessionInfo = 'i'; |
| 95 | static const char kLineTypeSessionUri = 'u'; |
| 96 | static const char kLineTypeSessionEmail = 'e'; |
| 97 | static const char kLineTypeSessionPhone = 'p'; |
| 98 | static const char kLineTypeSessionBandwidth = 'b'; |
| 99 | static const char kLineTypeTiming = 't'; |
| 100 | static const char kLineTypeRepeatTimes = 'r'; |
| 101 | static const char kLineTypeTimeZone = 'z'; |
| 102 | static const char kLineTypeEncryptionKey = 'k'; |
| 103 | static const char kLineTypeMedia = 'm'; |
| 104 | static const char kLineTypeConnection = 'c'; |
| 105 | static const char kLineTypeAttributes = 'a'; |
| 106 | |
| 107 | // Attributes |
| 108 | static const char kAttributeGroup[] = "group"; |
| 109 | static const char kAttributeMid[] = "mid"; |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 110 | static const char kAttributeMsid[] = "msid"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | static const char kAttributeRtcpMux[] = "rtcp-mux"; |
deadbeef | 1387149 | 2015-12-09 12:37:51 -0800 | [diff] [blame] | 112 | static const char kAttributeRtcpReducedSize[] = "rtcp-rsize"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | static const char kAttributeSsrc[] = "ssrc"; |
| 114 | static const char kSsrcAttributeCname[] = "cname"; |
| 115 | static const char kAttributeExtmap[] = "extmap"; |
| 116 | // draft-alvestrand-mmusic-msid-01 |
| 117 | // a=msid-semantic: WMS |
| 118 | static const char kAttributeMsidSemantics[] = "msid-semantic"; |
| 119 | static const char kMediaStreamSemantic[] = "WMS"; |
| 120 | static const char kSsrcAttributeMsid[] = "msid"; |
| 121 | static const char kDefaultMsid[] = "default"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | static const char kSsrcAttributeMslabel[] = "mslabel"; |
| 123 | static const char kSSrcAttributeLabel[] = "label"; |
| 124 | static const char kAttributeSsrcGroup[] = "ssrc-group"; |
| 125 | static const char kAttributeCrypto[] = "crypto"; |
| 126 | static const char kAttributeCandidate[] = "candidate"; |
| 127 | static const char kAttributeCandidateTyp[] = "typ"; |
| 128 | static const char kAttributeCandidateRaddr[] = "raddr"; |
| 129 | static const char kAttributeCandidateRport[] = "rport"; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 130 | static const char kAttributeCandidateUfrag[] = "ufrag"; |
| 131 | static const char kAttributeCandidatePwd[] = "pwd"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | static const char kAttributeCandidateGeneration[] = "generation"; |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 133 | static const char kAttributeCandidateNetworkId[] = "network-id"; |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 134 | static const char kAttributeCandidateNetworkCost[] = "network-cost"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | static const char kAttributeFingerprint[] = "fingerprint"; |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 136 | static const char kAttributeSetup[] = "setup"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | static const char kAttributeFmtp[] = "fmtp"; |
| 138 | static const char kAttributeRtpmap[] = "rtpmap"; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 139 | static const char kAttributeSctpmap[] = "sctpmap"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | static const char kAttributeRtcp[] = "rtcp"; |
| 141 | static const char kAttributeIceUfrag[] = "ice-ufrag"; |
| 142 | static const char kAttributeIcePwd[] = "ice-pwd"; |
| 143 | static const char kAttributeIceLite[] = "ice-lite"; |
| 144 | static const char kAttributeIceOption[] = "ice-options"; |
| 145 | static const char kAttributeSendOnly[] = "sendonly"; |
| 146 | static const char kAttributeRecvOnly[] = "recvonly"; |
| 147 | static const char kAttributeRtcpFb[] = "rtcp-fb"; |
| 148 | static const char kAttributeSendRecv[] = "sendrecv"; |
| 149 | static const char kAttributeInactive[] = "inactive"; |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 150 | // draft-ietf-mmusic-sctp-sdp-07 |
| 151 | // a=sctp-port |
| 152 | static const char kAttributeSctpPort[] = "sctp-port"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | |
| 154 | // Experimental flags |
| 155 | static const char kAttributeXGoogleFlag[] = "x-google-flag"; |
| 156 | static const char kValueConference[] = "conference"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | |
| 158 | // Candidate |
| 159 | static const char kCandidateHost[] = "host"; |
| 160 | static const char kCandidateSrflx[] = "srflx"; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 161 | static const char kCandidatePrflx[] = "prflx"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | static const char kCandidateRelay[] = "relay"; |
mallinath@webrtc.org | 2d60c5e | 2014-08-08 22:29:20 +0000 | [diff] [blame] | 163 | static const char kTcpCandidateType[] = "tcptype"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | |
| 165 | static const char kSdpDelimiterEqual = '='; |
| 166 | static const char kSdpDelimiterSpace = ' '; |
| 167 | static const char kSdpDelimiterColon = ':'; |
| 168 | static const char kSdpDelimiterSemicolon = ';'; |
| 169 | static const char kSdpDelimiterSlash = '/'; |
| 170 | static const char kNewLine = '\n'; |
| 171 | static const char kReturn = '\r'; |
| 172 | static const char kLineBreak[] = "\r\n"; |
| 173 | |
| 174 | // TODO: Generate the Session and Time description |
| 175 | // instead of hardcoding. |
| 176 | static const char kSessionVersion[] = "v=0"; |
| 177 | // RFC 4566 |
| 178 | static const char kSessionOriginUsername[] = "-"; |
| 179 | static const char kSessionOriginSessionId[] = "0"; |
| 180 | static const char kSessionOriginSessionVersion[] = "0"; |
| 181 | static const char kSessionOriginNettype[] = "IN"; |
| 182 | static const char kSessionOriginAddrtype[] = "IP4"; |
| 183 | static const char kSessionOriginAddress[] = "127.0.0.1"; |
| 184 | static const char kSessionName[] = "s=-"; |
| 185 | static const char kTimeDescription[] = "t=0 0"; |
| 186 | static const char kAttrGroup[] = "a=group:BUNDLE"; |
| 187 | static const char kConnectionNettype[] = "IN"; |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 188 | static const char kConnectionIpv4Addrtype[] = "IP4"; |
| 189 | static const char kConnectionIpv6Addrtype[] = "IP6"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 190 | static const char kMediaTypeVideo[] = "video"; |
| 191 | static const char kMediaTypeAudio[] = "audio"; |
| 192 | static const char kMediaTypeData[] = "application"; |
| 193 | static const char kMediaPortRejected[] = "0"; |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 194 | // draft-ietf-mmusic-trickle-ice-01 |
| 195 | // When no candidates have been gathered, set the connection |
| 196 | // address to IP6 ::. |
perkj@webrtc.org | d105cc8 | 2014-11-07 11:22:06 +0000 | [diff] [blame] | 197 | // TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333 |
| 198 | // Use IPV4 per default. |
| 199 | static const char kDummyAddress[] = "0.0.0.0"; |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 200 | static const char kDummyPort[] = "9"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 201 | // RFC 3556 |
| 202 | static const char kApplicationSpecificMaximum[] = "AS"; |
| 203 | |
| 204 | static const int kDefaultVideoClockrate = 90000; |
| 205 | |
| 206 | // ISAC special-case. |
| 207 | static const char kIsacCodecName[] = "ISAC"; // From webrtcvoiceengine.cc |
| 208 | static const int kIsacWbDefaultRate = 32000; // From acm_common_defs.h |
| 209 | static const int kIsacSwbDefaultRate = 56000; // From acm_common_defs.h |
| 210 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 211 | static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 212 | |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 213 | // RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload |
| 214 | // types. |
| 215 | const int kWildcardPayloadType = -1; |
| 216 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 217 | struct SsrcInfo { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 218 | uint32_t ssrc_id; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 219 | std::string cname; |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 220 | std::string stream_id; |
| 221 | std::string track_id; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | |
| 223 | // For backward compatibility. |
| 224 | // TODO(ronghuawu): Remove below 2 fields once all the clients support msid. |
| 225 | std::string label; |
| 226 | std::string mslabel; |
| 227 | }; |
| 228 | typedef std::vector<SsrcInfo> SsrcInfoVec; |
| 229 | typedef std::vector<SsrcGroup> SsrcGroupVec; |
| 230 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 231 | template <class T> |
| 232 | static void AddFmtpLine(const T& codec, std::string* message); |
| 233 | static void BuildMediaDescription(const ContentInfo* content_info, |
| 234 | const TransportInfo* transport_info, |
| 235 | const MediaType media_type, |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 236 | const std::vector<Candidate>& candidates, |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 237 | bool unified_plan_sdp, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | std::string* message); |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 239 | static void BuildSctpContentAttributes(std::string* message, int sctp_port); |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 240 | static void BuildRtpContentAttributes(const MediaContentDescription* media_desc, |
| 241 | const MediaType media_type, |
| 242 | bool unified_plan_sdp, |
| 243 | std::string* message); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 244 | static void BuildRtpMap(const MediaContentDescription* media_desc, |
| 245 | const MediaType media_type, |
| 246 | std::string* message); |
| 247 | static void BuildCandidate(const std::vector<Candidate>& candidates, |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 248 | bool include_ufrag, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 249 | std::string* message); |
| 250 | static void BuildIceOptions(const std::vector<std::string>& transport_options, |
| 251 | std::string* message); |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 252 | static bool IsRtp(const std::string& protocol); |
| 253 | static bool IsDtlsSctp(const std::string& protocol); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 254 | static bool ParseSessionDescription(const std::string& message, size_t* pos, |
| 255 | std::string* session_id, |
| 256 | std::string* session_version, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 257 | TransportDescription* session_td, |
| 258 | RtpHeaderExtensions* session_extmaps, |
| 259 | cricket::SessionDescription* desc, |
| 260 | SdpParseError* error); |
| 261 | static bool ParseGroupAttribute(const std::string& line, |
| 262 | cricket::SessionDescription* desc, |
| 263 | SdpParseError* error); |
| 264 | static bool ParseMediaDescription( |
| 265 | const std::string& message, |
| 266 | const TransportDescription& session_td, |
| 267 | const RtpHeaderExtensions& session_extmaps, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 268 | size_t* pos, cricket::SessionDescription* desc, |
| 269 | std::vector<JsepIceCandidate*>* candidates, |
| 270 | SdpParseError* error); |
| 271 | static bool ParseContent(const std::string& message, |
| 272 | const MediaType media_type, |
| 273 | int mline_index, |
| 274 | const std::string& protocol, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 275 | const std::vector<int>& payload_types, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 276 | size_t* pos, |
| 277 | std::string* content_name, |
| 278 | MediaContentDescription* media_desc, |
| 279 | TransportDescription* transport, |
| 280 | std::vector<JsepIceCandidate*>* candidates, |
| 281 | SdpParseError* error); |
| 282 | static bool ParseSsrcAttribute(const std::string& line, |
| 283 | SsrcInfoVec* ssrc_infos, |
| 284 | SdpParseError* error); |
| 285 | static bool ParseSsrcGroupAttribute(const std::string& line, |
| 286 | SsrcGroupVec* ssrc_groups, |
| 287 | SdpParseError* error); |
| 288 | static bool ParseCryptoAttribute(const std::string& line, |
| 289 | MediaContentDescription* media_desc, |
| 290 | SdpParseError* error); |
| 291 | static bool ParseRtpmapAttribute(const std::string& line, |
| 292 | const MediaType media_type, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 293 | const std::vector<int>& payload_types, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 294 | MediaContentDescription* media_desc, |
| 295 | SdpParseError* error); |
| 296 | static bool ParseFmtpAttributes(const std::string& line, |
| 297 | const MediaType media_type, |
| 298 | MediaContentDescription* media_desc, |
| 299 | SdpParseError* error); |
| 300 | static bool ParseFmtpParam(const std::string& line, std::string* parameter, |
| 301 | std::string* value, SdpParseError* error); |
| 302 | static bool ParseCandidate(const std::string& message, Candidate* candidate, |
| 303 | SdpParseError* error, bool is_raw); |
| 304 | static bool ParseRtcpFbAttribute(const std::string& line, |
| 305 | const MediaType media_type, |
| 306 | MediaContentDescription* media_desc, |
| 307 | SdpParseError* error); |
| 308 | static bool ParseIceOptions(const std::string& line, |
| 309 | std::vector<std::string>* transport_options, |
| 310 | SdpParseError* error); |
| 311 | static bool ParseExtmap(const std::string& line, |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 312 | RtpExtension* extmap, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 313 | SdpParseError* error); |
| 314 | static bool ParseFingerprintAttribute(const std::string& line, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 315 | rtc::SSLFingerprint** fingerprint, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 316 | SdpParseError* error); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 317 | static bool ParseDtlsSetup(const std::string& line, |
| 318 | cricket::ConnectionRole* role, |
| 319 | SdpParseError* error); |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 320 | static bool ParseMsidAttribute(const std::string& line, |
| 321 | std::string* stream_id, |
| 322 | std::string* track_id, |
| 323 | SdpParseError* error); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 324 | |
| 325 | // Helper functions |
| 326 | |
| 327 | // Below ParseFailed*** functions output the line that caused the parsing |
| 328 | // failure and the detailed reason (|description|) of the failure to |error|. |
| 329 | // The functions always return false so that they can be used directly in the |
| 330 | // following way when error happens: |
| 331 | // "return ParseFailed***(...);" |
| 332 | |
| 333 | // The line starting at |line_start| of |message| is the failing line. |
| 334 | // The reason for the failure should be provided in the |description|. |
| 335 | // An example of a description could be "unknown character". |
| 336 | static bool ParseFailed(const std::string& message, |
| 337 | size_t line_start, |
| 338 | const std::string& description, |
| 339 | SdpParseError* error) { |
| 340 | // Get the first line of |message| from |line_start|. |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 341 | std::string first_line; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 342 | size_t line_end = message.find(kNewLine, line_start); |
| 343 | if (line_end != std::string::npos) { |
| 344 | if (line_end > 0 && (message.at(line_end - 1) == kReturn)) { |
| 345 | --line_end; |
| 346 | } |
| 347 | first_line = message.substr(line_start, (line_end - line_start)); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 348 | } else { |
| 349 | first_line = message.substr(line_start); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | if (error) { |
| 353 | error->line = first_line; |
| 354 | error->description = description; |
| 355 | } |
| 356 | LOG(LS_ERROR) << "Failed to parse: \"" << first_line |
| 357 | << "\". Reason: " << description; |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | // |line| is the failing line. The reason for the failure should be |
| 362 | // provided in the |description|. |
| 363 | static bool ParseFailed(const std::string& line, |
| 364 | const std::string& description, |
| 365 | SdpParseError* error) { |
| 366 | return ParseFailed(line, 0, description, error); |
| 367 | } |
| 368 | |
| 369 | // Parses failure where the failing SDP line isn't know or there are multiple |
| 370 | // failing lines. |
| 371 | static bool ParseFailed(const std::string& description, |
| 372 | SdpParseError* error) { |
| 373 | return ParseFailed("", description, error); |
| 374 | } |
| 375 | |
| 376 | // |line| is the failing line. The failure is due to the fact that |line| |
| 377 | // doesn't have |expected_fields| fields. |
| 378 | static bool ParseFailedExpectFieldNum(const std::string& line, |
| 379 | int expected_fields, |
| 380 | SdpParseError* error) { |
| 381 | std::ostringstream description; |
| 382 | description << "Expects " << expected_fields << " fields."; |
| 383 | return ParseFailed(line, description.str(), error); |
| 384 | } |
| 385 | |
| 386 | // |line| is the failing line. The failure is due to the fact that |line| has |
| 387 | // less than |expected_min_fields| fields. |
| 388 | static bool ParseFailedExpectMinFieldNum(const std::string& line, |
| 389 | int expected_min_fields, |
| 390 | SdpParseError* error) { |
| 391 | std::ostringstream description; |
| 392 | description << "Expects at least " << expected_min_fields << " fields."; |
| 393 | return ParseFailed(line, description.str(), error); |
| 394 | } |
| 395 | |
| 396 | // |line| is the failing line. The failure is due to the fact that it failed to |
| 397 | // get the value of |attribute|. |
| 398 | static bool ParseFailedGetValue(const std::string& line, |
| 399 | const std::string& attribute, |
| 400 | SdpParseError* error) { |
| 401 | std::ostringstream description; |
| 402 | description << "Failed to get the value of attribute: " << attribute; |
| 403 | return ParseFailed(line, description.str(), error); |
| 404 | } |
| 405 | |
| 406 | // The line starting at |line_start| of |message| is the failing line. The |
| 407 | // failure is due to the line type (e.g. the "m" part of the "m-line") |
| 408 | // not matching what is expected. The expected line type should be |
| 409 | // provided as |line_type|. |
| 410 | static bool ParseFailedExpectLine(const std::string& message, |
| 411 | size_t line_start, |
| 412 | const char line_type, |
| 413 | const std::string& line_value, |
| 414 | SdpParseError* error) { |
| 415 | std::ostringstream description; |
| 416 | description << "Expect line: " << line_type << "=" << line_value; |
| 417 | return ParseFailed(message, line_start, description.str(), error); |
| 418 | } |
| 419 | |
| 420 | static bool AddLine(const std::string& line, std::string* message) { |
| 421 | if (!message) |
| 422 | return false; |
| 423 | |
| 424 | message->append(line); |
| 425 | message->append(kLineBreak); |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | static bool GetLine(const std::string& message, |
| 430 | size_t* pos, |
| 431 | std::string* line) { |
| 432 | size_t line_begin = *pos; |
| 433 | size_t line_end = message.find(kNewLine, line_begin); |
| 434 | if (line_end == std::string::npos) { |
| 435 | return false; |
| 436 | } |
| 437 | // Update the new start position |
| 438 | *pos = line_end + 1; |
| 439 | if (line_end > 0 && (message.at(line_end - 1) == kReturn)) { |
| 440 | --line_end; |
| 441 | } |
| 442 | *line = message.substr(line_begin, (line_end - line_begin)); |
| 443 | const char* cline = line->c_str(); |
| 444 | // RFC 4566 |
| 445 | // An SDP session description consists of a number of lines of text of |
| 446 | // the form: |
| 447 | // <type>=<value> |
| 448 | // where <type> MUST be exactly one case-significant character and |
| 449 | // <value> is structured text whose format depends on <type>. |
| 450 | // Whitespace MUST NOT be used on either side of the "=" sign. |
decurtis@webrtc.org | 8af1104 | 2015-01-07 19:15:51 +0000 | [diff] [blame] | 451 | if (line->length() < 3 || |
| 452 | !islower(cline[0]) || |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 453 | cline[1] != kSdpDelimiterEqual || |
| 454 | cline[2] == kSdpDelimiterSpace) { |
| 455 | *pos = line_begin; |
| 456 | return false; |
| 457 | } |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | // Init |os| to "|type|=|value|". |
| 462 | static void InitLine(const char type, |
| 463 | const std::string& value, |
| 464 | std::ostringstream* os) { |
| 465 | os->str(""); |
| 466 | *os << type << kSdpDelimiterEqual << value; |
| 467 | } |
| 468 | |
| 469 | // Init |os| to "a=|attribute|". |
| 470 | static void InitAttrLine(const std::string& attribute, std::ostringstream* os) { |
| 471 | InitLine(kLineTypeAttributes, attribute, os); |
| 472 | } |
| 473 | |
| 474 | // Writes a SDP attribute line based on |attribute| and |value| to |message|. |
| 475 | static void AddAttributeLine(const std::string& attribute, int value, |
| 476 | std::string* message) { |
| 477 | std::ostringstream os; |
| 478 | InitAttrLine(attribute, &os); |
| 479 | os << kSdpDelimiterColon << value; |
| 480 | AddLine(os.str(), message); |
| 481 | } |
| 482 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 483 | static bool IsLineType(const std::string& message, |
| 484 | const char type, |
| 485 | size_t line_start) { |
| 486 | if (message.size() < line_start + kLinePrefixLength) { |
| 487 | return false; |
| 488 | } |
| 489 | const char* cmessage = message.c_str(); |
| 490 | return (cmessage[line_start] == type && |
| 491 | cmessage[line_start + 1] == kSdpDelimiterEqual); |
| 492 | } |
| 493 | |
| 494 | static bool IsLineType(const std::string& line, |
| 495 | const char type) { |
| 496 | return IsLineType(line, type, 0); |
| 497 | } |
| 498 | |
| 499 | static bool GetLineWithType(const std::string& message, size_t* pos, |
| 500 | std::string* line, const char type) { |
| 501 | if (!IsLineType(message, type, *pos)) { |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | if (!GetLine(message, pos, line)) |
| 506 | return false; |
| 507 | |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | static bool HasAttribute(const std::string& line, |
| 512 | const std::string& attribute) { |
| 513 | return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0); |
| 514 | } |
| 515 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 516 | static bool AddSsrcLine(uint32_t ssrc_id, |
| 517 | const std::string& attribute, |
| 518 | const std::string& value, |
| 519 | std::string* message) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 520 | // RFC 5576 |
| 521 | // a=ssrc:<ssrc-id> <attribute>:<value> |
| 522 | std::ostringstream os; |
| 523 | InitAttrLine(kAttributeSsrc, &os); |
| 524 | os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace |
| 525 | << attribute << kSdpDelimiterColon << value; |
| 526 | return AddLine(os.str(), message); |
| 527 | } |
| 528 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 529 | // Get value only from <attribute>:<value>. |
| 530 | static bool GetValue(const std::string& message, const std::string& attribute, |
| 531 | std::string* value, SdpParseError* error) { |
| 532 | std::string leftpart; |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 533 | if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 534 | return ParseFailedGetValue(message, attribute, error); |
| 535 | } |
| 536 | // The left part should end with the expected attribute. |
| 537 | if (leftpart.length() < attribute.length() || |
| 538 | leftpart.compare(leftpart.length() - attribute.length(), |
| 539 | attribute.length(), attribute) != 0) { |
| 540 | return ParseFailedGetValue(message, attribute, error); |
| 541 | } |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | static bool CaseInsensitiveFind(std::string str1, std::string str2) { |
| 546 | std::transform(str1.begin(), str1.end(), str1.begin(), |
| 547 | ::tolower); |
| 548 | std::transform(str2.begin(), str2.end(), str2.begin(), |
| 549 | ::tolower); |
| 550 | return str1.find(str2) != std::string::npos; |
| 551 | } |
| 552 | |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 553 | template <class T> |
| 554 | static bool GetValueFromString(const std::string& line, |
| 555 | const std::string& s, |
| 556 | T* t, |
| 557 | SdpParseError* error) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 558 | if (!rtc::FromString(s, t)) { |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 559 | std::ostringstream description; |
| 560 | description << "Invalid value: " << s << "."; |
| 561 | return ParseFailed(line, description.str(), error); |
| 562 | } |
| 563 | return true; |
| 564 | } |
| 565 | |
pkasting@chromium.org | e9facf8 | 2015-02-17 20:36:28 +0000 | [diff] [blame] | 566 | static bool GetPayloadTypeFromString(const std::string& line, |
| 567 | const std::string& s, |
| 568 | int* payload_type, |
| 569 | SdpParseError* error) { |
| 570 | return GetValueFromString(line, s, payload_type, error) && |
| 571 | cricket::IsValidRtpPayloadType(*payload_type); |
| 572 | } |
| 573 | |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 574 | // |msid_stream_id| and |msid_track_id| represent the stream/track ID from the |
| 575 | // "a=msid" attribute, if it exists. They are empty if the attribute does not |
| 576 | // exist. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 577 | void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos, |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 578 | const std::string& msid_stream_id, |
| 579 | const std::string& msid_track_id, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 580 | StreamParamsVec* tracks) { |
| 581 | ASSERT(tracks != NULL); |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 582 | ASSERT(msid_stream_id.empty() == msid_track_id.empty()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 583 | for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin(); |
| 584 | ssrc_info != ssrc_infos.end(); ++ssrc_info) { |
| 585 | if (ssrc_info->cname.empty()) { |
| 586 | continue; |
| 587 | } |
| 588 | |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 589 | std::string stream_id; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 590 | std::string track_id; |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 591 | if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 592 | // If there's no msid and there's mslabel, we consider this is a sdp from |
| 593 | // a older version of client that doesn't support msid. |
| 594 | // In that case, we use the mslabel and label to construct the track. |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 595 | stream_id = ssrc_info->mslabel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 596 | track_id = ssrc_info->label; |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 597 | } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) { |
| 598 | // If there's no msid in the SSRC attributes, but there's a global one |
| 599 | // (from a=msid), use that. This is the case with unified plan SDP. |
| 600 | stream_id = msid_stream_id; |
| 601 | track_id = msid_track_id; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 602 | } else { |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 603 | stream_id = ssrc_info->stream_id; |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 604 | track_id = ssrc_info->track_id; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 605 | } |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 606 | // If a stream/track ID wasn't populated from the SSRC attributes OR the |
| 607 | // msid attribute, use default/random values. |
| 608 | if (stream_id.empty()) { |
| 609 | stream_id = kDefaultMsid; |
| 610 | } |
| 611 | if (track_id.empty()) { |
| 612 | // TODO(ronghuawu): What should we do if the track id doesn't appear? |
| 613 | // Create random string (which will be used as track label later)? |
| 614 | track_id = rtc::CreateRandomString(8); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | StreamParamsVec::iterator track = tracks->begin(); |
| 618 | for (; track != tracks->end(); ++track) { |
| 619 | if (track->id == track_id) { |
| 620 | break; |
| 621 | } |
| 622 | } |
| 623 | if (track == tracks->end()) { |
| 624 | // If we don't find an existing track, create a new one. |
| 625 | tracks->push_back(StreamParams()); |
| 626 | track = tracks->end() - 1; |
| 627 | } |
| 628 | track->add_ssrc(ssrc_info->ssrc_id); |
| 629 | track->cname = ssrc_info->cname; |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 630 | track->sync_label = stream_id; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 631 | track->id = track_id; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void GetMediaStreamLabels(const ContentInfo* content, |
| 636 | std::set<std::string>* labels) { |
| 637 | const MediaContentDescription* media_desc = |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 638 | static_cast<const MediaContentDescription*>( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 639 | content->description); |
| 640 | const cricket::StreamParamsVec& streams = media_desc->streams(); |
| 641 | for (cricket::StreamParamsVec::const_iterator it = streams.begin(); |
| 642 | it != streams.end(); ++it) { |
| 643 | labels->insert(it->sync_label); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // RFC 5245 |
| 648 | // It is RECOMMENDED that default candidates be chosen based on the |
| 649 | // likelihood of those candidates to work with the peer that is being |
| 650 | // contacted. It is RECOMMENDED that relayed > reflexive > host. |
| 651 | static const int kPreferenceUnknown = 0; |
| 652 | static const int kPreferenceHost = 1; |
| 653 | static const int kPreferenceReflexive = 2; |
| 654 | static const int kPreferenceRelayed = 3; |
| 655 | |
| 656 | static int GetCandidatePreferenceFromType(const std::string& type) { |
| 657 | int preference = kPreferenceUnknown; |
| 658 | if (type == cricket::LOCAL_PORT_TYPE) { |
| 659 | preference = kPreferenceHost; |
| 660 | } else if (type == cricket::STUN_PORT_TYPE) { |
| 661 | preference = kPreferenceReflexive; |
| 662 | } else if (type == cricket::RELAY_PORT_TYPE) { |
| 663 | preference = kPreferenceRelayed; |
| 664 | } else { |
| 665 | ASSERT(false); |
| 666 | } |
| 667 | return preference; |
| 668 | } |
| 669 | |
guoweis@webrtc.org | 57ac2c8 | 2015-02-06 00:45:13 +0000 | [diff] [blame] | 670 | // Get ip and port of the default destination from the |candidates| with the |
| 671 | // given value of |component_id|. The default candidate should be the one most |
| 672 | // likely to work, typically IPv4 relay. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 673 | // RFC 5245 |
| 674 | // The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP). |
| 675 | // TODO: Decide the default destination in webrtcsession and |
| 676 | // pass it down via SessionDescription. |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 677 | static void GetDefaultDestination( |
| 678 | const std::vector<Candidate>& candidates, |
| 679 | int component_id, std::string* port, |
| 680 | std::string* ip, std::string* addr_type) { |
perkj@webrtc.org | d105cc8 | 2014-11-07 11:22:06 +0000 | [diff] [blame] | 681 | *addr_type = kConnectionIpv4Addrtype; |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 682 | *port = kDummyPort; |
| 683 | *ip = kDummyAddress; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 684 | int current_preference = kPreferenceUnknown; |
guoweis@webrtc.org | 57ac2c8 | 2015-02-06 00:45:13 +0000 | [diff] [blame] | 685 | int current_family = AF_UNSPEC; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 686 | for (std::vector<Candidate>::const_iterator it = candidates.begin(); |
| 687 | it != candidates.end(); ++it) { |
| 688 | if (it->component() != component_id) { |
| 689 | continue; |
| 690 | } |
guoweis@webrtc.org | 57ac2c8 | 2015-02-06 00:45:13 +0000 | [diff] [blame] | 691 | // Default destination should be UDP only. |
| 692 | if (it->protocol() != cricket::UDP_PROTOCOL_NAME) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 693 | continue; |
| 694 | } |
guoweis@webrtc.org | 57ac2c8 | 2015-02-06 00:45:13 +0000 | [diff] [blame] | 695 | const int preference = GetCandidatePreferenceFromType(it->type()); |
| 696 | const int family = it->address().ipaddr().family(); |
| 697 | // See if this candidate is more preferable then the current one if it's the |
| 698 | // same family. Or if the current family is IPv4 already so we could safely |
| 699 | // ignore all IPv6 ones. WebRTC bug 4269. |
| 700 | // http://code.google.com/p/webrtc/issues/detail?id=4269 |
| 701 | if ((preference <= current_preference && current_family == family) || |
| 702 | (current_family == AF_INET && family == AF_INET6)) { |
| 703 | continue; |
| 704 | } |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 705 | if (family == AF_INET) { |
| 706 | addr_type->assign(kConnectionIpv4Addrtype); |
| 707 | } else if (family == AF_INET6) { |
| 708 | addr_type->assign(kConnectionIpv6Addrtype); |
| 709 | } |
guoweis@webrtc.org | 57ac2c8 | 2015-02-06 00:45:13 +0000 | [diff] [blame] | 710 | current_preference = preference; |
| 711 | current_family = family; |
| 712 | *port = it->address().PortAsString(); |
| 713 | *ip = it->address().ipaddr().ToString(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 714 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 715 | } |
| 716 | |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 717 | // Update |mline|'s default destination and append a c line after it. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 718 | static void UpdateMediaDefaultDestination( |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 719 | const std::vector<Candidate>& candidates, |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 720 | const std::string& mline, |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 721 | std::string* message) { |
| 722 | std::string new_lines; |
| 723 | AddLine(mline, &new_lines); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 724 | // RFC 4566 |
| 725 | // m=<media> <port> <proto> <fmt> ... |
| 726 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 727 | rtc::split(mline, kSdpDelimiterSpace, &fields); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 728 | if (fields.size() < 3) { |
| 729 | return; |
| 730 | } |
| 731 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 732 | std::ostringstream os; |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 733 | std::string rtp_port, rtp_ip, addr_type; |
| 734 | GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTP, |
| 735 | &rtp_port, &rtp_ip, &addr_type); |
| 736 | // Found default RTP candidate. |
| 737 | // RFC 5245 |
| 738 | // The default candidates are added to the SDP as the default |
| 739 | // destination for media. For streams based on RTP, this is done by |
| 740 | // placing the IP address and port of the RTP candidate into the c and m |
| 741 | // lines, respectively. |
| 742 | // Update the port in the m line. |
| 743 | // If this is a m-line with port equal to 0, we don't change it. |
| 744 | if (fields[1] != kMediaPortRejected) { |
| 745 | new_lines.replace(fields[0].size() + 1, |
| 746 | fields[1].size(), |
| 747 | rtp_port); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 748 | } |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 749 | // Add the c line. |
| 750 | // RFC 4566 |
| 751 | // c=<nettype> <addrtype> <connection-address> |
| 752 | InitLine(kLineTypeConnection, kConnectionNettype, &os); |
| 753 | os << " " << addr_type << " " << rtp_ip; |
| 754 | AddLine(os.str(), &new_lines); |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 755 | message->append(new_lines); |
| 756 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 757 | |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 758 | // Gets "a=rtcp" line if found default RTCP candidate from |candidates|. |
| 759 | static std::string GetRtcpLine(const std::vector<Candidate>& candidates) { |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 760 | std::string rtcp_line, rtcp_port, rtcp_ip, addr_type; |
| 761 | GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP, |
| 762 | &rtcp_port, &rtcp_ip, &addr_type); |
| 763 | // Found default RTCP candidate. |
| 764 | // RFC 5245 |
| 765 | // If the agent is utilizing RTCP, it MUST encode the RTCP candidate |
| 766 | // using the a=rtcp attribute as defined in RFC 3605. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 767 | |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 768 | // RFC 3605 |
| 769 | // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space |
| 770 | // connection-address] CRLF |
| 771 | std::ostringstream os; |
| 772 | InitAttrLine(kAttributeRtcp, &os); |
| 773 | os << kSdpDelimiterColon |
| 774 | << rtcp_port << " " |
| 775 | << kConnectionNettype << " " |
| 776 | << addr_type << " " |
| 777 | << rtcp_ip; |
| 778 | rtcp_line = os.str(); |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 779 | return rtcp_line; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | // Get candidates according to the mline index from SessionDescriptionInterface. |
| 783 | static void GetCandidatesByMindex(const SessionDescriptionInterface& desci, |
| 784 | int mline_index, |
| 785 | std::vector<Candidate>* candidates) { |
| 786 | if (!candidates) { |
| 787 | return; |
| 788 | } |
| 789 | const IceCandidateCollection* cc = desci.candidates(mline_index); |
| 790 | for (size_t i = 0; i < cc->count(); ++i) { |
| 791 | const IceCandidateInterface* candidate = cc->at(i); |
| 792 | candidates->push_back(candidate->candidate()); |
| 793 | } |
| 794 | } |
| 795 | |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 796 | std::string SdpSerialize(const JsepSessionDescription& jdesc, |
| 797 | bool unified_plan_sdp) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 798 | const cricket::SessionDescription* desc = jdesc.description(); |
| 799 | if (!desc) { |
| 800 | return ""; |
| 801 | } |
| 802 | |
| 803 | std::string message; |
| 804 | |
| 805 | // Session Description. |
| 806 | AddLine(kSessionVersion, &message); |
| 807 | // Session Origin |
| 808 | // RFC 4566 |
| 809 | // o=<username> <sess-id> <sess-version> <nettype> <addrtype> |
| 810 | // <unicast-address> |
| 811 | std::ostringstream os; |
| 812 | InitLine(kLineTypeOrigin, kSessionOriginUsername, &os); |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 813 | const std::string& session_id = jdesc.session_id().empty() ? |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 814 | kSessionOriginSessionId : jdesc.session_id(); |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 815 | const std::string& session_version = jdesc.session_version().empty() ? |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 816 | kSessionOriginSessionVersion : jdesc.session_version(); |
| 817 | os << " " << session_id << " " << session_version << " " |
| 818 | << kSessionOriginNettype << " " << kSessionOriginAddrtype << " " |
| 819 | << kSessionOriginAddress; |
| 820 | AddLine(os.str(), &message); |
| 821 | AddLine(kSessionName, &message); |
| 822 | |
| 823 | // Time Description. |
| 824 | AddLine(kTimeDescription, &message); |
| 825 | |
| 826 | // Group |
| 827 | if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) { |
| 828 | std::string group_line = kAttrGroup; |
| 829 | const cricket::ContentGroup* group = |
| 830 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 831 | ASSERT(group != NULL); |
| 832 | const cricket::ContentNames& content_names = group->content_names(); |
| 833 | for (cricket::ContentNames::const_iterator it = content_names.begin(); |
| 834 | it != content_names.end(); ++it) { |
| 835 | group_line.append(" "); |
| 836 | group_line.append(*it); |
| 837 | } |
| 838 | AddLine(group_line, &message); |
| 839 | } |
| 840 | |
| 841 | // MediaStream semantics |
| 842 | InitAttrLine(kAttributeMsidSemantics, &os); |
| 843 | os << kSdpDelimiterColon << " " << kMediaStreamSemantic; |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 844 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 845 | std::set<std::string> media_stream_labels; |
| 846 | const ContentInfo* audio_content = GetFirstAudioContent(desc); |
| 847 | if (audio_content) |
| 848 | GetMediaStreamLabels(audio_content, &media_stream_labels); |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 849 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 850 | const ContentInfo* video_content = GetFirstVideoContent(desc); |
| 851 | if (video_content) |
| 852 | GetMediaStreamLabels(video_content, &media_stream_labels); |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 853 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 854 | for (std::set<std::string>::const_iterator it = |
| 855 | media_stream_labels.begin(); it != media_stream_labels.end(); ++it) { |
| 856 | os << " " << *it; |
| 857 | } |
| 858 | AddLine(os.str(), &message); |
| 859 | |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 860 | // Preserve the order of the media contents. |
| 861 | int mline_index = -1; |
| 862 | for (cricket::ContentInfos::const_iterator it = desc->contents().begin(); |
| 863 | it != desc->contents().end(); ++it) { |
| 864 | const MediaContentDescription* mdesc = |
| 865 | static_cast<const MediaContentDescription*>(it->description); |
| 866 | std::vector<Candidate> candidates; |
| 867 | GetCandidatesByMindex(jdesc, ++mline_index, &candidates); |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 868 | BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name), |
| 869 | mdesc->type(), candidates, unified_plan_sdp, |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 870 | &message); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 871 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 872 | return message; |
| 873 | } |
| 874 | |
| 875 | // Serializes the passed in IceCandidateInterface to a SDP string. |
| 876 | // candidate - The candidate to be serialized. |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 877 | std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) { |
| 878 | return SdpSerializeCandidate(candidate.candidate()); |
| 879 | } |
| 880 | |
| 881 | // Serializes a cricket Candidate. |
| 882 | std::string SdpSerializeCandidate(const cricket::Candidate& candidate) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 883 | std::string message; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 884 | std::vector<cricket::Candidate> candidates(1, candidate); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 885 | BuildCandidate(candidates, true, &message); |
wu@webrtc.org | ec9f5fb | 2014-06-24 17:05:10 +0000 | [diff] [blame] | 886 | // From WebRTC draft section 4.8.1.1 candidate-attribute will be |
| 887 | // just candidate:<candidate> not a=candidate:<blah>CRLF |
| 888 | ASSERT(message.find("a=") == 0); |
| 889 | message.erase(0, 2); |
| 890 | ASSERT(message.find(kLineBreak) == message.size() - 2); |
| 891 | message.resize(message.size() - 2); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 892 | return message; |
| 893 | } |
| 894 | |
| 895 | bool SdpDeserialize(const std::string& message, |
| 896 | JsepSessionDescription* jdesc, |
| 897 | SdpParseError* error) { |
| 898 | std::string session_id; |
| 899 | std::string session_version; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 900 | TransportDescription session_td("", ""); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 901 | RtpHeaderExtensions session_extmaps; |
| 902 | cricket::SessionDescription* desc = new cricket::SessionDescription(); |
| 903 | std::vector<JsepIceCandidate*> candidates; |
| 904 | size_t current_pos = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 905 | |
| 906 | // Session Description |
| 907 | if (!ParseSessionDescription(message, ¤t_pos, &session_id, |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 908 | &session_version, &session_td, &session_extmaps, |
| 909 | desc, error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 910 | delete desc; |
| 911 | return false; |
| 912 | } |
| 913 | |
| 914 | // Media Description |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 915 | if (!ParseMediaDescription(message, session_td, session_extmaps, ¤t_pos, |
| 916 | desc, &candidates, error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 917 | delete desc; |
| 918 | for (std::vector<JsepIceCandidate*>::const_iterator |
| 919 | it = candidates.begin(); it != candidates.end(); ++it) { |
| 920 | delete *it; |
| 921 | } |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | jdesc->Initialize(desc, session_id, session_version); |
| 926 | |
| 927 | for (std::vector<JsepIceCandidate*>::const_iterator |
| 928 | it = candidates.begin(); it != candidates.end(); ++it) { |
| 929 | jdesc->AddCandidate(*it); |
| 930 | delete *it; |
| 931 | } |
| 932 | return true; |
| 933 | } |
| 934 | |
| 935 | bool SdpDeserializeCandidate(const std::string& message, |
| 936 | JsepIceCandidate* jcandidate, |
| 937 | SdpParseError* error) { |
| 938 | ASSERT(jcandidate != NULL); |
| 939 | Candidate candidate; |
| 940 | if (!ParseCandidate(message, &candidate, error, true)) { |
| 941 | return false; |
| 942 | } |
| 943 | jcandidate->SetCandidate(candidate); |
| 944 | return true; |
| 945 | } |
| 946 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 947 | bool SdpDeserializeCandidate(const std::string& transport_name, |
| 948 | const std::string& message, |
| 949 | cricket::Candidate* candidate, |
| 950 | SdpParseError* error) { |
| 951 | ASSERT(candidate != nullptr); |
| 952 | if (!ParseCandidate(message, candidate, error, true)) { |
| 953 | return false; |
| 954 | } |
| 955 | candidate->set_transport_name(transport_name); |
| 956 | return true; |
| 957 | } |
| 958 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 959 | bool ParseCandidate(const std::string& message, Candidate* candidate, |
| 960 | SdpParseError* error, bool is_raw) { |
| 961 | ASSERT(candidate != NULL); |
| 962 | |
| 963 | // Get the first line from |message|. |
jiayl@webrtc.org | 7ec3f9f | 2014-08-08 23:09:15 +0000 | [diff] [blame] | 964 | std::string first_line = message; |
| 965 | size_t pos = 0; |
| 966 | GetLine(message, &pos, &first_line); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 967 | |
jiayl@webrtc.org | 7ec3f9f | 2014-08-08 23:09:15 +0000 | [diff] [blame] | 968 | // Makes sure |message| contains only one line. |
| 969 | if (message.size() > first_line.size()) { |
| 970 | std::string left, right; |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 971 | if (rtc::tokenize_first(message, kNewLine, &left, &right) && |
| 972 | !right.empty()) { |
jiayl@webrtc.org | 7ec3f9f | 2014-08-08 23:09:15 +0000 | [diff] [blame] | 973 | return ParseFailed(message, 0, "Expect one line only", error); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | // From WebRTC draft section 4.8.1.1 candidate-attribute should be |
| 978 | // candidate:<candidate> when trickled, but we still support |
| 979 | // a=candidate:<blah>CRLF for backward compatibility and for parsing a line |
| 980 | // from the SDP. |
| 981 | if (IsLineType(first_line, kLineTypeAttributes)) { |
| 982 | first_line = first_line.substr(kLinePrefixLength); |
| 983 | } |
| 984 | |
| 985 | std::string attribute_candidate; |
| 986 | std::string candidate_value; |
| 987 | |
| 988 | // |first_line| must be in the form of "candidate:<value>". |
Donald Curtis | 144d018 | 2015-05-15 13:14:24 -0700 | [diff] [blame] | 989 | if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate, |
| 990 | &candidate_value) || |
jiayl@webrtc.org | 7ec3f9f | 2014-08-08 23:09:15 +0000 | [diff] [blame] | 991 | attribute_candidate != kAttributeCandidate) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 992 | if (is_raw) { |
| 993 | std::ostringstream description; |
jiayl@webrtc.org | 7ec3f9f | 2014-08-08 23:09:15 +0000 | [diff] [blame] | 994 | description << "Expect line: " << kAttributeCandidate |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 995 | << ":" << "<candidate-str>"; |
| 996 | return ParseFailed(first_line, 0, description.str(), error); |
| 997 | } else { |
| 998 | return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes, |
| 999 | kAttributeCandidate, error); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | std::vector<std::string> fields; |
jiayl@webrtc.org | 7ec3f9f | 2014-08-08 23:09:15 +0000 | [diff] [blame] | 1004 | rtc::split(candidate_value, kSdpDelimiterSpace, &fields); |
| 1005 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1006 | // RFC 5245 |
| 1007 | // a=candidate:<foundation> <component-id> <transport> <priority> |
| 1008 | // <connection-address> <port> typ <candidate-types> |
| 1009 | // [raddr <connection-address>] [rport <port>] |
| 1010 | // *(SP extension-att-name SP extension-att-value) |
| 1011 | const size_t expected_min_fields = 8; |
| 1012 | if (fields.size() < expected_min_fields || |
| 1013 | (fields[6] != kAttributeCandidateTyp)) { |
| 1014 | return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error); |
| 1015 | } |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 1016 | const std::string& foundation = fields[0]; |
jiayl@webrtc.org | 7ec3f9f | 2014-08-08 23:09:15 +0000 | [diff] [blame] | 1017 | |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 1018 | int component_id = 0; |
| 1019 | if (!GetValueFromString(first_line, fields[1], &component_id, error)) { |
| 1020 | return false; |
| 1021 | } |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 1022 | const std::string& transport = fields[2]; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1023 | uint32_t priority = 0; |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 1024 | if (!GetValueFromString(first_line, fields[3], &priority, error)) { |
| 1025 | return false; |
| 1026 | } |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 1027 | const std::string& connection_address = fields[4]; |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 1028 | int port = 0; |
| 1029 | if (!GetValueFromString(first_line, fields[5], &port, error)) { |
| 1030 | return false; |
| 1031 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1032 | SocketAddress address(connection_address, port); |
| 1033 | |
| 1034 | cricket::ProtocolType protocol; |
| 1035 | if (!StringToProto(transport.c_str(), &protocol)) { |
| 1036 | return ParseFailed(first_line, "Unsupported transport type.", error); |
| 1037 | } |
| 1038 | |
| 1039 | std::string candidate_type; |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 1040 | const std::string& type = fields[7]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1041 | if (type == kCandidateHost) { |
| 1042 | candidate_type = cricket::LOCAL_PORT_TYPE; |
| 1043 | } else if (type == kCandidateSrflx) { |
| 1044 | candidate_type = cricket::STUN_PORT_TYPE; |
| 1045 | } else if (type == kCandidateRelay) { |
| 1046 | candidate_type = cricket::RELAY_PORT_TYPE; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1047 | } else if (type == kCandidatePrflx) { |
| 1048 | candidate_type = cricket::PRFLX_PORT_TYPE; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1049 | } else { |
| 1050 | return ParseFailed(first_line, "Unsupported candidate type.", error); |
| 1051 | } |
| 1052 | |
| 1053 | size_t current_position = expected_min_fields; |
| 1054 | SocketAddress related_address; |
| 1055 | // The 2 optional fields for related address |
| 1056 | // [raddr <connection-address>] [rport <port>] |
| 1057 | if (fields.size() >= (current_position + 2) && |
| 1058 | fields[current_position] == kAttributeCandidateRaddr) { |
| 1059 | related_address.SetIP(fields[++current_position]); |
| 1060 | ++current_position; |
| 1061 | } |
| 1062 | if (fields.size() >= (current_position + 2) && |
| 1063 | fields[current_position] == kAttributeCandidateRport) { |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 1064 | int port = 0; |
| 1065 | if (!GetValueFromString( |
| 1066 | first_line, fields[++current_position], &port, error)) { |
| 1067 | return false; |
| 1068 | } |
| 1069 | related_address.SetPort(port); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1070 | ++current_position; |
| 1071 | } |
| 1072 | |
mallinath@webrtc.org | 2d60c5e | 2014-08-08 22:29:20 +0000 | [diff] [blame] | 1073 | // If this is a TCP candidate, it has additional extension as defined in |
| 1074 | // RFC 6544. |
| 1075 | std::string tcptype; |
| 1076 | if (fields.size() >= (current_position + 2) && |
| 1077 | fields[current_position] == kTcpCandidateType) { |
| 1078 | tcptype = fields[++current_position]; |
| 1079 | ++current_position; |
| 1080 | |
| 1081 | if (tcptype != cricket::TCPTYPE_ACTIVE_STR && |
| 1082 | tcptype != cricket::TCPTYPE_PASSIVE_STR && |
| 1083 | tcptype != cricket::TCPTYPE_SIMOPEN_STR) { |
| 1084 | return ParseFailed(first_line, "Invalid TCP candidate type.", error); |
| 1085 | } |
| 1086 | |
| 1087 | if (protocol != cricket::PROTO_TCP) { |
| 1088 | return ParseFailed(first_line, "Invalid non-TCP candidate", error); |
| 1089 | } |
| 1090 | } |
| 1091 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1092 | // Extension |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 1093 | // Though non-standard, we support the ICE ufrag and pwd being signaled on |
| 1094 | // the candidate to avoid issues with confusing which generation a candidate |
| 1095 | // belongs to when trickling multiple generations at the same time. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1096 | std::string username; |
| 1097 | std::string password; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1098 | uint32_t generation = 0; |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1099 | uint16_t network_id = 0; |
| 1100 | uint16_t network_cost = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1101 | for (size_t i = current_position; i + 1 < fields.size(); ++i) { |
| 1102 | // RFC 5245 |
| 1103 | // *(SP extension-att-name SP extension-att-value) |
| 1104 | if (fields[i] == kAttributeCandidateGeneration) { |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 1105 | if (!GetValueFromString(first_line, fields[++i], &generation, error)) { |
| 1106 | return false; |
| 1107 | } |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 1108 | } else if (fields[i] == kAttributeCandidateUfrag) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1109 | username = fields[++i]; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 1110 | } else if (fields[i] == kAttributeCandidatePwd) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1111 | password = fields[++i]; |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1112 | } else if (fields[i] == kAttributeCandidateNetworkId) { |
| 1113 | if (!GetValueFromString(first_line, fields[++i], &network_id, error)) { |
| 1114 | return false; |
| 1115 | } |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 1116 | } else if (fields[i] == kAttributeCandidateNetworkCost) { |
| 1117 | if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) { |
| 1118 | return false; |
| 1119 | } |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1120 | network_cost = std::min(network_cost, rtc::kNetworkCostMax); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1121 | } else { |
| 1122 | // Skip the unknown extension. |
| 1123 | ++i; |
| 1124 | } |
| 1125 | } |
| 1126 | |
guoweis@webrtc.org | 61c1247 | 2015-01-15 06:53:07 +0000 | [diff] [blame] | 1127 | *candidate = Candidate(component_id, cricket::ProtoToString(protocol), |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 1128 | address, priority, username, password, candidate_type, |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1129 | generation, foundation, network_id, network_cost); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1130 | candidate->set_related_address(related_address); |
mallinath@webrtc.org | 2d60c5e | 2014-08-08 22:29:20 +0000 | [diff] [blame] | 1131 | candidate->set_tcptype(tcptype); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1132 | return true; |
| 1133 | } |
| 1134 | |
| 1135 | bool ParseIceOptions(const std::string& line, |
| 1136 | std::vector<std::string>* transport_options, |
| 1137 | SdpParseError* error) { |
| 1138 | std::string ice_options; |
| 1139 | if (!GetValue(line, kAttributeIceOption, &ice_options, error)) { |
| 1140 | return false; |
| 1141 | } |
| 1142 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1143 | rtc::split(ice_options, kSdpDelimiterSpace, &fields); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1144 | for (size_t i = 0; i < fields.size(); ++i) { |
| 1145 | transport_options->push_back(fields[i]); |
| 1146 | } |
| 1147 | return true; |
| 1148 | } |
| 1149 | |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 1150 | bool ParseSctpPort(const std::string& line, |
| 1151 | int* sctp_port, |
| 1152 | SdpParseError* error) { |
| 1153 | // draft-ietf-mmusic-sctp-sdp-07 |
| 1154 | // a=sctp-port |
| 1155 | std::vector<std::string> fields; |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 1156 | const size_t expected_min_fields = 2; |
lally | 69f5760 | 2015-10-08 10:15:04 -0700 | [diff] [blame] | 1157 | rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields); |
| 1158 | if (fields.size() < expected_min_fields) { |
| 1159 | fields.resize(0); |
| 1160 | rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields); |
| 1161 | } |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 1162 | if (fields.size() < expected_min_fields) { |
| 1163 | return ParseFailedExpectMinFieldNum(line, expected_min_fields, error); |
| 1164 | } |
| 1165 | if (!rtc::FromString(fields[1], sctp_port)) { |
lally | 69f5760 | 2015-10-08 10:15:04 -0700 | [diff] [blame] | 1166 | return ParseFailed(line, "Invalid sctp port value.", error); |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 1167 | } |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 1171 | bool ParseExtmap(const std::string& line, |
| 1172 | RtpExtension* extmap, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1173 | SdpParseError* error) { |
| 1174 | // RFC 5285 |
| 1175 | // a=extmap:<value>["/"<direction>] <URI> <extensionattributes> |
| 1176 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1177 | rtc::split(line.substr(kLinePrefixLength), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1178 | kSdpDelimiterSpace, &fields); |
| 1179 | const size_t expected_min_fields = 2; |
| 1180 | if (fields.size() < expected_min_fields) { |
| 1181 | return ParseFailedExpectMinFieldNum(line, expected_min_fields, error); |
| 1182 | } |
| 1183 | std::string uri = fields[1]; |
| 1184 | |
| 1185 | std::string value_direction; |
| 1186 | if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) { |
| 1187 | return false; |
| 1188 | } |
| 1189 | std::vector<std::string> sub_fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1190 | rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields); |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 1191 | int value = 0; |
| 1192 | if (!GetValueFromString(line, sub_fields[0], &value, error)) { |
| 1193 | return false; |
| 1194 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1195 | |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 1196 | *extmap = RtpExtension(uri, value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1197 | return true; |
| 1198 | } |
| 1199 | |
| 1200 | void BuildMediaDescription(const ContentInfo* content_info, |
| 1201 | const TransportInfo* transport_info, |
| 1202 | const MediaType media_type, |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 1203 | const std::vector<Candidate>& candidates, |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 1204 | bool unified_plan_sdp, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1205 | std::string* message) { |
| 1206 | ASSERT(message != NULL); |
| 1207 | if (content_info == NULL || message == NULL) { |
| 1208 | return; |
| 1209 | } |
| 1210 | // TODO: Rethink if we should use sprintfn instead of stringstream. |
| 1211 | // According to the style guide, streams should only be used for logging. |
| 1212 | // http://google-styleguide.googlecode.com/svn/ |
| 1213 | // trunk/cppguide.xml?showone=Streams#Streams |
| 1214 | std::ostringstream os; |
| 1215 | const MediaContentDescription* media_desc = |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 1216 | static_cast<const MediaContentDescription*>( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1217 | content_info->description); |
| 1218 | ASSERT(media_desc != NULL); |
| 1219 | |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 1220 | int sctp_port = cricket::kSctpDefaultPort; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1221 | |
| 1222 | // RFC 4566 |
| 1223 | // m=<media> <port> <proto> <fmt> |
| 1224 | // fmt is a list of payload type numbers that MAY be used in the session. |
| 1225 | const char* type = NULL; |
| 1226 | if (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 1227 | type = kMediaTypeAudio; |
| 1228 | else if (media_type == cricket::MEDIA_TYPE_VIDEO) |
| 1229 | type = kMediaTypeVideo; |
| 1230 | else if (media_type == cricket::MEDIA_TYPE_DATA) |
| 1231 | type = kMediaTypeData; |
| 1232 | else |
| 1233 | ASSERT(false); |
| 1234 | |
| 1235 | std::string fmt; |
| 1236 | if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1237 | const VideoContentDescription* video_desc = |
| 1238 | static_cast<const VideoContentDescription*>(media_desc); |
| 1239 | for (std::vector<cricket::VideoCodec>::const_iterator it = |
| 1240 | video_desc->codecs().begin(); |
| 1241 | it != video_desc->codecs().end(); ++it) { |
| 1242 | fmt.append(" "); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1243 | fmt.append(rtc::ToString<int>(it->id)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1244 | } |
| 1245 | } else if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1246 | const AudioContentDescription* audio_desc = |
| 1247 | static_cast<const AudioContentDescription*>(media_desc); |
| 1248 | for (std::vector<cricket::AudioCodec>::const_iterator it = |
| 1249 | audio_desc->codecs().begin(); |
| 1250 | it != audio_desc->codecs().end(); ++it) { |
| 1251 | fmt.append(" "); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1252 | fmt.append(rtc::ToString<int>(it->id)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1253 | } |
| 1254 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 1255 | const DataContentDescription* data_desc = |
| 1256 | static_cast<const DataContentDescription*>(media_desc); |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 1257 | if (IsDtlsSctp(media_desc->protocol())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1258 | fmt.append(" "); |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 1259 | |
| 1260 | for (std::vector<cricket::DataCodec>::const_iterator it = |
| 1261 | data_desc->codecs().begin(); |
| 1262 | it != data_desc->codecs().end(); ++it) { |
| 1263 | if (it->id == cricket::kGoogleSctpDataCodecId && |
| 1264 | it->GetParam(cricket::kCodecParamPort, &sctp_port)) { |
| 1265 | break; |
| 1266 | } |
| 1267 | } |
| 1268 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1269 | fmt.append(rtc::ToString<int>(sctp_port)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1270 | } else { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1271 | for (std::vector<cricket::DataCodec>::const_iterator it = |
| 1272 | data_desc->codecs().begin(); |
| 1273 | it != data_desc->codecs().end(); ++it) { |
| 1274 | fmt.append(" "); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1275 | fmt.append(rtc::ToString<int>(it->id)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | // The fmt must never be empty. If no codecs are found, set the fmt attribute |
| 1280 | // to 0. |
| 1281 | if (fmt.empty()) { |
| 1282 | fmt = " 0"; |
| 1283 | } |
| 1284 | |
| 1285 | // The port number in the m line will be updated later when associate with |
| 1286 | // the candidates. |
| 1287 | // RFC 3264 |
| 1288 | // To reject an offered stream, the port number in the corresponding stream in |
| 1289 | // the answer MUST be set to zero. |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 1290 | const std::string& port = content_info->rejected ? |
pthatcher@webrtc.org | c9d6d14 | 2014-10-23 23:37:22 +0000 | [diff] [blame] | 1291 | kMediaPortRejected : kDummyPort; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1292 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1293 | rtc::SSLFingerprint* fp = (transport_info) ? |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1294 | transport_info->description.identity_fingerprint.get() : NULL; |
| 1295 | |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 1296 | // Add the m and c lines. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1297 | InitLine(kLineTypeMedia, type, &os); |
| 1298 | os << " " << port << " " << media_desc->protocol() << fmt; |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 1299 | std::string mline = os.str(); |
| 1300 | UpdateMediaDefaultDestination(candidates, mline, message); |
| 1301 | |
| 1302 | // RFC 4566 |
| 1303 | // b=AS:<bandwidth> |
Peter Thatcher | c0c3a86 | 2015-06-24 15:31:25 -0700 | [diff] [blame] | 1304 | if (media_desc->bandwidth() >= 1000) { |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 1305 | InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os); |
| 1306 | os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000); |
| 1307 | AddLine(os.str(), message); |
| 1308 | } |
| 1309 | |
| 1310 | // Add the a=rtcp line. |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 1311 | if (IsRtp(media_desc->protocol())) { |
wu@webrtc.org | 4c3e991 | 2014-07-16 21:03:13 +0000 | [diff] [blame] | 1312 | std::string rtcp_line = GetRtcpLine(candidates); |
| 1313 | if (!rtcp_line.empty()) { |
| 1314 | AddLine(rtcp_line, message); |
| 1315 | } |
| 1316 | } |
| 1317 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 1318 | // Build the a=candidate lines. We don't include ufrag and pwd in the |
| 1319 | // candidates in the SDP to avoid redundancy. |
| 1320 | BuildCandidate(candidates, false, message); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1321 | |
| 1322 | // Use the transport_info to build the media level ice-ufrag and ice-pwd. |
| 1323 | if (transport_info) { |
| 1324 | // RFC 5245 |
| 1325 | // ice-pwd-att = "ice-pwd" ":" password |
| 1326 | // ice-ufrag-att = "ice-ufrag" ":" ufrag |
| 1327 | // ice-ufrag |
deadbeef | 3f7219b | 2015-12-28 15:17:14 -0800 | [diff] [blame] | 1328 | if (!transport_info->description.ice_ufrag.empty()) { |
| 1329 | InitAttrLine(kAttributeIceUfrag, &os); |
| 1330 | os << kSdpDelimiterColon << transport_info->description.ice_ufrag; |
| 1331 | AddLine(os.str(), message); |
| 1332 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1333 | // ice-pwd |
deadbeef | 3f7219b | 2015-12-28 15:17:14 -0800 | [diff] [blame] | 1334 | if (!transport_info->description.ice_pwd.empty()) { |
| 1335 | InitAttrLine(kAttributeIcePwd, &os); |
| 1336 | os << kSdpDelimiterColon << transport_info->description.ice_pwd; |
| 1337 | AddLine(os.str(), message); |
| 1338 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1339 | |
| 1340 | // draft-petithuguenin-mmusic-ice-attributes-level-03 |
| 1341 | BuildIceOptions(transport_info->description.transport_options, message); |
| 1342 | |
| 1343 | // RFC 4572 |
| 1344 | // fingerprint-attribute = |
| 1345 | // "fingerprint" ":" hash-func SP fingerprint |
| 1346 | if (fp) { |
| 1347 | // Insert the fingerprint attribute. |
| 1348 | InitAttrLine(kAttributeFingerprint, &os); |
| 1349 | os << kSdpDelimiterColon |
| 1350 | << fp->algorithm << kSdpDelimiterSpace |
| 1351 | << fp->GetRfc4572Fingerprint(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1352 | AddLine(os.str(), message); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1353 | |
| 1354 | // Inserting setup attribute. |
| 1355 | if (transport_info->description.connection_role != |
| 1356 | cricket::CONNECTIONROLE_NONE) { |
| 1357 | // Making sure we are not using "passive" mode. |
| 1358 | cricket::ConnectionRole role = |
| 1359 | transport_info->description.connection_role; |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1360 | std::string dtls_role_str; |
| 1361 | VERIFY(cricket::ConnectionRoleToString(role, &dtls_role_str)); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1362 | InitAttrLine(kAttributeSetup, &os); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1363 | os << kSdpDelimiterColon << dtls_role_str; |
| 1364 | AddLine(os.str(), message); |
| 1365 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | // RFC 3388 |
| 1370 | // mid-attribute = "a=mid:" identification-tag |
| 1371 | // identification-tag = token |
| 1372 | // Use the content name as the mid identification-tag. |
| 1373 | InitAttrLine(kAttributeMid, &os); |
| 1374 | os << kSdpDelimiterColon << content_info->name; |
| 1375 | AddLine(os.str(), message); |
| 1376 | |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 1377 | if (IsDtlsSctp(media_desc->protocol())) { |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 1378 | BuildSctpContentAttributes(message, sctp_port); |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 1379 | } else if (IsRtp(media_desc->protocol())) { |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 1380 | BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp, |
| 1381 | message); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 1385 | void BuildSctpContentAttributes(std::string* message, int sctp_port) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1386 | // draft-ietf-mmusic-sctp-sdp-04 |
| 1387 | // a=sctpmap:sctpmap-number protocol [streams] |
lally | 69f5760 | 2015-10-08 10:15:04 -0700 | [diff] [blame] | 1388 | // TODO(lally): switch this over to mmusic-sctp-sdp-12 (or later), with |
| 1389 | // 'a=sctp-port:' |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1390 | std::ostringstream os; |
| 1391 | InitAttrLine(kAttributeSctpmap, &os); |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 1392 | os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1393 | << kDefaultSctpmapProtocol << kSdpDelimiterSpace |
| 1394 | << (cricket::kMaxSctpSid + 1); |
| 1395 | AddLine(os.str(), message); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 1398 | // If unified_plan_sdp is true, will use "a=msid". |
| 1399 | void BuildRtpContentAttributes(const MediaContentDescription* media_desc, |
| 1400 | const MediaType media_type, |
| 1401 | bool unified_plan_sdp, |
| 1402 | std::string* message) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1403 | std::ostringstream os; |
| 1404 | // RFC 5285 |
| 1405 | // a=extmap:<value>["/"<direction>] <URI> <extensionattributes> |
| 1406 | // The definitions MUST be either all session level or all media level. This |
| 1407 | // implementation uses all media level. |
| 1408 | for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) { |
| 1409 | InitAttrLine(kAttributeExtmap, &os); |
| 1410 | os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id |
| 1411 | << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri; |
| 1412 | AddLine(os.str(), message); |
| 1413 | } |
| 1414 | |
| 1415 | // RFC 3264 |
| 1416 | // a=sendrecv || a=sendonly || a=sendrecv || a=inactive |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1417 | switch (media_desc->direction()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1418 | case cricket::MD_INACTIVE: |
| 1419 | InitAttrLine(kAttributeInactive, &os); |
| 1420 | break; |
| 1421 | case cricket::MD_SENDONLY: |
| 1422 | InitAttrLine(kAttributeSendOnly, &os); |
| 1423 | break; |
| 1424 | case cricket::MD_RECVONLY: |
| 1425 | InitAttrLine(kAttributeRecvOnly, &os); |
| 1426 | break; |
| 1427 | case cricket::MD_SENDRECV: |
| 1428 | default: |
| 1429 | InitAttrLine(kAttributeSendRecv, &os); |
| 1430 | break; |
| 1431 | } |
| 1432 | AddLine(os.str(), message); |
| 1433 | |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 1434 | // draft-ietf-mmusic-msid-11 |
| 1435 | // a=msid:<stream id> <track id> |
| 1436 | if (unified_plan_sdp && !media_desc->streams().empty()) { |
| 1437 | if (media_desc->streams().size() > 1u) { |
| 1438 | LOG(LS_WARNING) << "Trying to serialize unified plan SDP with more than " |
| 1439 | << "one track in a media section. Omitting 'a=msid'."; |
| 1440 | } else { |
| 1441 | auto track = media_desc->streams().begin(); |
| 1442 | const std::string& stream_id = track->sync_label; |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 1443 | InitAttrLine(kAttributeMsid, &os); |
| 1444 | os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id; |
| 1445 | AddLine(os.str(), message); |
| 1446 | } |
| 1447 | } |
| 1448 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1449 | // RFC 5761 |
| 1450 | // a=rtcp-mux |
| 1451 | if (media_desc->rtcp_mux()) { |
| 1452 | InitAttrLine(kAttributeRtcpMux, &os); |
| 1453 | AddLine(os.str(), message); |
| 1454 | } |
| 1455 | |
deadbeef | 1387149 | 2015-12-09 12:37:51 -0800 | [diff] [blame] | 1456 | // RFC 5506 |
| 1457 | // a=rtcp-rsize |
| 1458 | if (media_desc->rtcp_reduced_size()) { |
| 1459 | InitAttrLine(kAttributeRtcpReducedSize, &os); |
| 1460 | AddLine(os.str(), message); |
| 1461 | } |
| 1462 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1463 | // RFC 4568 |
| 1464 | // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>] |
| 1465 | for (std::vector<CryptoParams>::const_iterator it = |
| 1466 | media_desc->cryptos().begin(); |
| 1467 | it != media_desc->cryptos().end(); ++it) { |
| 1468 | InitAttrLine(kAttributeCrypto, &os); |
| 1469 | os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " " |
| 1470 | << it->key_params; |
| 1471 | if (!it->session_params.empty()) { |
| 1472 | os << " " << it->session_params; |
| 1473 | } |
| 1474 | AddLine(os.str(), message); |
| 1475 | } |
| 1476 | |
| 1477 | // RFC 4566 |
| 1478 | // a=rtpmap:<payload type> <encoding name>/<clock rate> |
| 1479 | // [/<encodingparameters>] |
| 1480 | BuildRtpMap(media_desc, media_type, message); |
| 1481 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1482 | for (StreamParamsVec::const_iterator track = media_desc->streams().begin(); |
| 1483 | track != media_desc->streams().end(); ++track) { |
| 1484 | // Require that the track belongs to a media stream, |
| 1485 | // ie the sync_label is set. This extra check is necessary since the |
| 1486 | // MediaContentDescription always contains a streamparam with an ssrc even |
| 1487 | // if no track or media stream have been created. |
| 1488 | if (track->sync_label.empty()) continue; |
| 1489 | |
| 1490 | // Build the ssrc-group lines. |
| 1491 | for (size_t i = 0; i < track->ssrc_groups.size(); ++i) { |
| 1492 | // RFC 5576 |
| 1493 | // a=ssrc-group:<semantics> <ssrc-id> ... |
| 1494 | if (track->ssrc_groups[i].ssrcs.empty()) { |
| 1495 | continue; |
| 1496 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1497 | InitAttrLine(kAttributeSsrcGroup, &os); |
| 1498 | os << kSdpDelimiterColon << track->ssrc_groups[i].semantics; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1499 | std::vector<uint32_t>::const_iterator ssrc = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1500 | track->ssrc_groups[i].ssrcs.begin(); |
| 1501 | for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1502 | os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1503 | } |
| 1504 | AddLine(os.str(), message); |
| 1505 | } |
| 1506 | // Build the ssrc lines for each ssrc. |
| 1507 | for (size_t i = 0; i < track->ssrcs.size(); ++i) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1508 | uint32_t ssrc = track->ssrcs[i]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1509 | // RFC 5576 |
| 1510 | // a=ssrc:<ssrc-id> cname:<value> |
| 1511 | AddSsrcLine(ssrc, kSsrcAttributeCname, |
| 1512 | track->cname, message); |
| 1513 | |
| 1514 | // draft-alvestrand-mmusic-msid-00 |
| 1515 | // a=ssrc:<ssrc-id> msid:identifier [appdata] |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 1516 | // The appdata consists of the "id" attribute of a MediaStreamTrack, |
| 1517 | // which corresponds to the "id" attribute of StreamParams. |
| 1518 | const std::string& stream_id = track->sync_label; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1519 | InitAttrLine(kAttributeSsrc, &os); |
| 1520 | os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 1521 | << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id |
| 1522 | << kSdpDelimiterSpace << track->id; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1523 | AddLine(os.str(), message); |
| 1524 | |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 1525 | // TODO(ronghuawu): Remove below code which is for backward |
| 1526 | // compatibility. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1527 | // draft-alvestrand-rtcweb-mid-01 |
| 1528 | // a=ssrc:<ssrc-id> mslabel:<value> |
| 1529 | // The label isn't yet defined. |
| 1530 | // a=ssrc:<ssrc-id> label:<value> |
| 1531 | AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message); |
| 1532 | AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message); |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | void WriteFmtpHeader(int payload_type, std::ostringstream* os) { |
| 1538 | // fmtp header: a=fmtp:|payload_type| <parameters> |
| 1539 | // Add a=fmtp |
| 1540 | InitAttrLine(kAttributeFmtp, os); |
| 1541 | // Add :|payload_type| |
| 1542 | *os << kSdpDelimiterColon << payload_type; |
| 1543 | } |
| 1544 | |
| 1545 | void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) { |
| 1546 | // rtcp-fb header: a=rtcp-fb:|payload_type| |
| 1547 | // <parameters>/<ccm <ccm_parameters>> |
| 1548 | // Add a=rtcp-fb |
| 1549 | InitAttrLine(kAttributeRtcpFb, os); |
| 1550 | // Add : |
| 1551 | *os << kSdpDelimiterColon; |
| 1552 | if (payload_type == kWildcardPayloadType) { |
| 1553 | *os << "*"; |
| 1554 | } else { |
| 1555 | *os << payload_type; |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | void WriteFmtpParameter(const std::string& parameter_name, |
| 1560 | const std::string& parameter_value, |
| 1561 | std::ostringstream* os) { |
| 1562 | // fmtp parameters: |parameter_name|=|parameter_value| |
| 1563 | *os << parameter_name << kSdpDelimiterEqual << parameter_value; |
| 1564 | } |
| 1565 | |
| 1566 | void WriteFmtpParameters(const cricket::CodecParameterMap& parameters, |
| 1567 | std::ostringstream* os) { |
| 1568 | for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin(); |
| 1569 | fmtp != parameters.end(); ++fmtp) { |
hta | 62a216e | 2016-04-15 11:02:14 -0700 | [diff] [blame] | 1570 | // Parameters are a semicolon-separated list, no spaces. |
| 1571 | // The list is separated from the header by a space. |
| 1572 | if (fmtp == parameters.begin()) { |
| 1573 | *os << kSdpDelimiterSpace; |
| 1574 | } else { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1575 | *os << kSdpDelimiterSemicolon; |
| 1576 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1577 | WriteFmtpParameter(fmtp->first, fmtp->second, os); |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | bool IsFmtpParam(const std::string& name) { |
| 1582 | const char* kFmtpParams[] = { |
hta | a6b9944 | 2016-04-12 10:29:17 -0700 | [diff] [blame] | 1583 | // TODO(hta): Split FMTP parameters apart from parameters in general. |
| 1584 | // FMTP parameters are codec specific, not generic. |
| 1585 | kCodecParamMinPTime, |
| 1586 | kCodecParamSPropStereo, |
| 1587 | kCodecParamStereo, |
| 1588 | kCodecParamUseInbandFec, |
| 1589 | kCodecParamUseDtx, |
| 1590 | kCodecParamStartBitrate, |
| 1591 | kCodecParamMaxBitrate, |
| 1592 | kCodecParamMinBitrate, |
| 1593 | kCodecParamMaxQuantization, |
| 1594 | kCodecParamSctpProtocol, |
| 1595 | kCodecParamSctpStreams, |
| 1596 | kCodecParamMaxAverageBitrate, |
| 1597 | kCodecParamMaxPlaybackRate, |
| 1598 | kCodecParamAssociatedPayloadType, |
| 1599 | cricket::kH264FmtpPacketizationMode, |
| 1600 | cricket::kH264FmtpLevelAsymmetryAllowed, |
| 1601 | cricket::kH264FmtpProfileLevelId}; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 1602 | for (size_t i = 0; i < arraysize(kFmtpParams); ++i) { |
hta | a6b9944 | 2016-04-12 10:29:17 -0700 | [diff] [blame] | 1603 | if (name.compare(kFmtpParams[i]) == 0) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1604 | return true; |
| 1605 | } |
| 1606 | } |
| 1607 | return false; |
| 1608 | } |
| 1609 | |
| 1610 | // Retreives fmtp parameters from |params|, which may contain other parameters |
| 1611 | // as well, and puts them in |fmtp_parameters|. |
| 1612 | void GetFmtpParams(const cricket::CodecParameterMap& params, |
| 1613 | cricket::CodecParameterMap* fmtp_parameters) { |
| 1614 | for (cricket::CodecParameterMap::const_iterator iter = params.begin(); |
| 1615 | iter != params.end(); ++iter) { |
| 1616 | if (IsFmtpParam(iter->first)) { |
| 1617 | (*fmtp_parameters)[iter->first] = iter->second; |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | template <class T> |
| 1623 | void AddFmtpLine(const T& codec, std::string* message) { |
| 1624 | cricket::CodecParameterMap fmtp_parameters; |
| 1625 | GetFmtpParams(codec.params, &fmtp_parameters); |
| 1626 | if (fmtp_parameters.empty()) { |
| 1627 | // No need to add an fmtp if it will have no (optional) parameters. |
| 1628 | return; |
| 1629 | } |
| 1630 | std::ostringstream os; |
| 1631 | WriteFmtpHeader(codec.id, &os); |
| 1632 | WriteFmtpParameters(fmtp_parameters, &os); |
| 1633 | AddLine(os.str(), message); |
| 1634 | return; |
| 1635 | } |
| 1636 | |
| 1637 | template <class T> |
| 1638 | void AddRtcpFbLines(const T& codec, std::string* message) { |
| 1639 | for (std::vector<cricket::FeedbackParam>::const_iterator iter = |
| 1640 | codec.feedback_params.params().begin(); |
| 1641 | iter != codec.feedback_params.params().end(); ++iter) { |
| 1642 | std::ostringstream os; |
| 1643 | WriteRtcpFbHeader(codec.id, &os); |
| 1644 | os << " " << iter->id(); |
| 1645 | if (!iter->param().empty()) { |
| 1646 | os << " " << iter->param(); |
| 1647 | } |
| 1648 | AddLine(os.str(), message); |
| 1649 | } |
| 1650 | } |
| 1651 | |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 1652 | bool AddSctpDataCodec(DataContentDescription* media_desc, |
| 1653 | int sctp_port) { |
| 1654 | if (media_desc->HasCodec(cricket::kGoogleSctpDataCodecId)) { |
| 1655 | return ParseFailed("", |
| 1656 | "Can't have multiple sctp port attributes.", |
| 1657 | NULL); |
| 1658 | } |
| 1659 | // Add the SCTP Port number as a pseudo-codec "port" parameter |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 1660 | cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecId, |
| 1661 | cricket::kGoogleSctpDataCodecName); |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 1662 | codec_port.SetParam(cricket::kCodecParamPort, sctp_port); |
| 1663 | LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " |
| 1664 | << sctp_port; |
| 1665 | media_desc->AddCodec(codec_port); |
| 1666 | return true; |
| 1667 | } |
| 1668 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1669 | bool GetMinValue(const std::vector<int>& values, int* value) { |
| 1670 | if (values.empty()) { |
| 1671 | return false; |
| 1672 | } |
| 1673 | std::vector<int>::const_iterator found = |
| 1674 | std::min_element(values.begin(), values.end()); |
| 1675 | *value = *found; |
| 1676 | return true; |
| 1677 | } |
| 1678 | |
| 1679 | bool GetParameter(const std::string& name, |
| 1680 | const cricket::CodecParameterMap& params, int* value) { |
| 1681 | std::map<std::string, std::string>::const_iterator found = |
| 1682 | params.find(name); |
| 1683 | if (found == params.end()) { |
| 1684 | return false; |
| 1685 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1686 | if (!rtc::FromString(found->second, value)) { |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 1687 | return false; |
| 1688 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1689 | return true; |
| 1690 | } |
| 1691 | |
| 1692 | void BuildRtpMap(const MediaContentDescription* media_desc, |
| 1693 | const MediaType media_type, |
| 1694 | std::string* message) { |
| 1695 | ASSERT(message != NULL); |
| 1696 | ASSERT(media_desc != NULL); |
| 1697 | std::ostringstream os; |
| 1698 | if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 1699 | const VideoContentDescription* video_desc = |
| 1700 | static_cast<const VideoContentDescription*>(media_desc); |
| 1701 | for (std::vector<cricket::VideoCodec>::const_iterator it = |
| 1702 | video_desc->codecs().begin(); |
| 1703 | it != video_desc->codecs().end(); ++it) { |
| 1704 | // RFC 4566 |
| 1705 | // a=rtpmap:<payload type> <encoding name>/<clock rate> |
| 1706 | // [/<encodingparameters>] |
| 1707 | if (it->id != kWildcardPayloadType) { |
| 1708 | InitAttrLine(kAttributeRtpmap, &os); |
| 1709 | os << kSdpDelimiterColon << it->id << " " << it->name |
| 1710 | << "/" << kDefaultVideoClockrate; |
| 1711 | AddLine(os.str(), message); |
| 1712 | } |
| 1713 | AddRtcpFbLines(*it, message); |
| 1714 | AddFmtpLine(*it, message); |
| 1715 | } |
| 1716 | } else if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1717 | const AudioContentDescription* audio_desc = |
| 1718 | static_cast<const AudioContentDescription*>(media_desc); |
| 1719 | std::vector<int> ptimes; |
| 1720 | std::vector<int> maxptimes; |
| 1721 | int max_minptime = 0; |
| 1722 | for (std::vector<cricket::AudioCodec>::const_iterator it = |
| 1723 | audio_desc->codecs().begin(); |
| 1724 | it != audio_desc->codecs().end(); ++it) { |
| 1725 | ASSERT(!it->name.empty()); |
| 1726 | // RFC 4566 |
| 1727 | // a=rtpmap:<payload type> <encoding name>/<clock rate> |
| 1728 | // [/<encodingparameters>] |
| 1729 | InitAttrLine(kAttributeRtpmap, &os); |
| 1730 | os << kSdpDelimiterColon << it->id << " "; |
| 1731 | os << it->name << "/" << it->clockrate; |
| 1732 | if (it->channels != 1) { |
| 1733 | os << "/" << it->channels; |
| 1734 | } |
| 1735 | AddLine(os.str(), message); |
| 1736 | AddRtcpFbLines(*it, message); |
| 1737 | AddFmtpLine(*it, message); |
| 1738 | int minptime = 0; |
| 1739 | if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) { |
| 1740 | max_minptime = std::max(minptime, max_minptime); |
| 1741 | } |
| 1742 | int ptime; |
| 1743 | if (GetParameter(kCodecParamPTime, it->params, &ptime)) { |
| 1744 | ptimes.push_back(ptime); |
| 1745 | } |
| 1746 | int maxptime; |
| 1747 | if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) { |
| 1748 | maxptimes.push_back(maxptime); |
| 1749 | } |
| 1750 | } |
| 1751 | // Populate the maxptime attribute with the smallest maxptime of all codecs |
| 1752 | // under the same m-line. |
| 1753 | int min_maxptime = INT_MAX; |
| 1754 | if (GetMinValue(maxptimes, &min_maxptime)) { |
| 1755 | AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message); |
| 1756 | } |
| 1757 | ASSERT(min_maxptime > max_minptime); |
| 1758 | // Populate the ptime attribute with the smallest ptime or the largest |
| 1759 | // minptime, whichever is the largest, for all codecs under the same m-line. |
| 1760 | int ptime = INT_MAX; |
| 1761 | if (GetMinValue(ptimes, &ptime)) { |
| 1762 | ptime = std::min(ptime, min_maxptime); |
| 1763 | ptime = std::max(ptime, max_minptime); |
| 1764 | AddAttributeLine(kCodecParamPTime, ptime, message); |
| 1765 | } |
| 1766 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
| 1767 | const DataContentDescription* data_desc = |
| 1768 | static_cast<const DataContentDescription*>(media_desc); |
| 1769 | for (std::vector<cricket::DataCodec>::const_iterator it = |
| 1770 | data_desc->codecs().begin(); |
| 1771 | it != data_desc->codecs().end(); ++it) { |
| 1772 | // RFC 4566 |
| 1773 | // a=rtpmap:<payload type> <encoding name>/<clock rate> |
| 1774 | // [/<encodingparameters>] |
| 1775 | InitAttrLine(kAttributeRtpmap, &os); |
| 1776 | os << kSdpDelimiterColon << it->id << " " |
| 1777 | << it->name << "/" << it->clockrate; |
| 1778 | AddLine(os.str(), message); |
| 1779 | } |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | void BuildCandidate(const std::vector<Candidate>& candidates, |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 1784 | bool include_ufrag, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1785 | std::string* message) { |
| 1786 | std::ostringstream os; |
| 1787 | |
| 1788 | for (std::vector<Candidate>::const_iterator it = candidates.begin(); |
| 1789 | it != candidates.end(); ++it) { |
| 1790 | // RFC 5245 |
| 1791 | // a=candidate:<foundation> <component-id> <transport> <priority> |
| 1792 | // <connection-address> <port> typ <candidate-types> |
| 1793 | // [raddr <connection-address>] [rport <port>] |
| 1794 | // *(SP extension-att-name SP extension-att-value) |
| 1795 | std::string type; |
| 1796 | // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay" |
| 1797 | if (it->type() == cricket::LOCAL_PORT_TYPE) { |
| 1798 | type = kCandidateHost; |
| 1799 | } else if (it->type() == cricket::STUN_PORT_TYPE) { |
| 1800 | type = kCandidateSrflx; |
| 1801 | } else if (it->type() == cricket::RELAY_PORT_TYPE) { |
| 1802 | type = kCandidateRelay; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1803 | } else if (it->type() == cricket::PRFLX_PORT_TYPE) { |
| 1804 | type = kCandidatePrflx; |
| 1805 | // Peer reflexive candidate may be signaled for being removed. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1806 | } else { |
| 1807 | ASSERT(false); |
Peter Thatcher | 019087f | 2015-04-28 09:06:26 -0700 | [diff] [blame] | 1808 | // Never write out candidates if we don't know the type. |
| 1809 | continue; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | InitAttrLine(kAttributeCandidate, &os); |
| 1813 | os << kSdpDelimiterColon |
mallinath@webrtc.org | 2d60c5e | 2014-08-08 22:29:20 +0000 | [diff] [blame] | 1814 | << it->foundation() << " " |
| 1815 | << it->component() << " " |
| 1816 | << it->protocol() << " " |
| 1817 | << it->priority() << " " |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1818 | << it->address().ipaddr().ToString() << " " |
| 1819 | << it->address().PortAsString() << " " |
mallinath@webrtc.org | 2d60c5e | 2014-08-08 22:29:20 +0000 | [diff] [blame] | 1820 | << kAttributeCandidateTyp << " " |
| 1821 | << type << " "; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1822 | |
| 1823 | // Related address |
| 1824 | if (!it->related_address().IsNil()) { |
| 1825 | os << kAttributeCandidateRaddr << " " |
| 1826 | << it->related_address().ipaddr().ToString() << " " |
| 1827 | << kAttributeCandidateRport << " " |
| 1828 | << it->related_address().PortAsString() << " "; |
| 1829 | } |
| 1830 | |
mallinath@webrtc.org | 2d60c5e | 2014-08-08 22:29:20 +0000 | [diff] [blame] | 1831 | if (it->protocol() == cricket::TCP_PROTOCOL_NAME) { |
mallinath@webrtc.org | e999bd0 | 2014-08-13 06:05:55 +0000 | [diff] [blame] | 1832 | os << kTcpCandidateType << " " << it->tcptype() << " "; |
mallinath@webrtc.org | 2d60c5e | 2014-08-08 22:29:20 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1835 | // Extensions |
| 1836 | os << kAttributeCandidateGeneration << " " << it->generation(); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 1837 | if (include_ufrag && !it->username().empty()) { |
| 1838 | os << " " << kAttributeCandidateUfrag << " " << it->username(); |
| 1839 | } |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1840 | if (it->network_id() > 0) { |
| 1841 | os << " " << kAttributeCandidateNetworkId << " " << it->network_id(); |
| 1842 | } |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 1843 | if (it->network_cost() > 0) { |
| 1844 | os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost(); |
| 1845 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1846 | |
| 1847 | AddLine(os.str(), message); |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | void BuildIceOptions(const std::vector<std::string>& transport_options, |
| 1852 | std::string* message) { |
| 1853 | if (!transport_options.empty()) { |
| 1854 | std::ostringstream os; |
| 1855 | InitAttrLine(kAttributeIceOption, &os); |
| 1856 | os << kSdpDelimiterColon << transport_options[0]; |
| 1857 | for (size_t i = 1; i < transport_options.size(); ++i) { |
| 1858 | os << kSdpDelimiterSpace << transport_options[i]; |
| 1859 | } |
| 1860 | AddLine(os.str(), message); |
| 1861 | } |
| 1862 | } |
| 1863 | |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 1864 | bool IsRtp(const std::string& protocol) { |
| 1865 | return protocol.empty() || |
| 1866 | (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos); |
| 1867 | } |
| 1868 | |
| 1869 | bool IsDtlsSctp(const std::string& protocol) { |
| 1870 | // This intentionally excludes "SCTP" and "SCTP/DTLS". |
lally@webrtc.org | a747093 | 2015-02-24 20:19:21 +0000 | [diff] [blame] | 1871 | return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos; |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1874 | bool ParseSessionDescription(const std::string& message, size_t* pos, |
| 1875 | std::string* session_id, |
| 1876 | std::string* session_version, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1877 | TransportDescription* session_td, |
| 1878 | RtpHeaderExtensions* session_extmaps, |
| 1879 | cricket::SessionDescription* desc, |
| 1880 | SdpParseError* error) { |
| 1881 | std::string line; |
| 1882 | |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 1883 | desc->set_msid_supported(false); |
| 1884 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1885 | // RFC 4566 |
| 1886 | // v= (protocol version) |
| 1887 | if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) { |
| 1888 | return ParseFailedExpectLine(message, *pos, kLineTypeVersion, |
| 1889 | std::string(), error); |
| 1890 | } |
| 1891 | // RFC 4566 |
| 1892 | // o=<username> <sess-id> <sess-version> <nettype> <addrtype> |
| 1893 | // <unicast-address> |
| 1894 | if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) { |
| 1895 | return ParseFailedExpectLine(message, *pos, kLineTypeOrigin, |
| 1896 | std::string(), error); |
| 1897 | } |
| 1898 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1899 | rtc::split(line.substr(kLinePrefixLength), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1900 | kSdpDelimiterSpace, &fields); |
| 1901 | const size_t expected_fields = 6; |
| 1902 | if (fields.size() != expected_fields) { |
| 1903 | return ParseFailedExpectFieldNum(line, expected_fields, error); |
| 1904 | } |
| 1905 | *session_id = fields[1]; |
| 1906 | *session_version = fields[2]; |
| 1907 | |
| 1908 | // RFC 4566 |
| 1909 | // s= (session name) |
| 1910 | if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) { |
| 1911 | return ParseFailedExpectLine(message, *pos, kLineTypeSessionName, |
| 1912 | std::string(), error); |
| 1913 | } |
| 1914 | |
| 1915 | // Optional lines |
| 1916 | // Those are the optional lines, so shouldn't return false if not present. |
| 1917 | // RFC 4566 |
| 1918 | // i=* (session information) |
| 1919 | GetLineWithType(message, pos, &line, kLineTypeSessionInfo); |
| 1920 | |
| 1921 | // RFC 4566 |
| 1922 | // u=* (URI of description) |
| 1923 | GetLineWithType(message, pos, &line, kLineTypeSessionUri); |
| 1924 | |
| 1925 | // RFC 4566 |
| 1926 | // e=* (email address) |
| 1927 | GetLineWithType(message, pos, &line, kLineTypeSessionEmail); |
| 1928 | |
| 1929 | // RFC 4566 |
| 1930 | // p=* (phone number) |
| 1931 | GetLineWithType(message, pos, &line, kLineTypeSessionPhone); |
| 1932 | |
| 1933 | // RFC 4566 |
| 1934 | // c=* (connection information -- not required if included in |
| 1935 | // all media) |
| 1936 | GetLineWithType(message, pos, &line, kLineTypeConnection); |
| 1937 | |
| 1938 | // RFC 4566 |
| 1939 | // b=* (zero or more bandwidth information lines) |
| 1940 | while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) { |
| 1941 | // By pass zero or more b lines. |
| 1942 | } |
| 1943 | |
| 1944 | // RFC 4566 |
| 1945 | // One or more time descriptions ("t=" and "r=" lines; see below) |
| 1946 | // t= (time the session is active) |
| 1947 | // r=* (zero or more repeat times) |
| 1948 | // Ensure there's at least one time description |
| 1949 | if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) { |
| 1950 | return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(), |
| 1951 | error); |
| 1952 | } |
| 1953 | |
| 1954 | while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) { |
| 1955 | // By pass zero or more r lines. |
| 1956 | } |
| 1957 | |
| 1958 | // Go through the rest of the time descriptions |
| 1959 | while (GetLineWithType(message, pos, &line, kLineTypeTiming)) { |
| 1960 | while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) { |
| 1961 | // By pass zero or more r lines. |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | // RFC 4566 |
| 1966 | // z=* (time zone adjustments) |
| 1967 | GetLineWithType(message, pos, &line, kLineTypeTimeZone); |
| 1968 | |
| 1969 | // RFC 4566 |
| 1970 | // k=* (encryption key) |
| 1971 | GetLineWithType(message, pos, &line, kLineTypeEncryptionKey); |
| 1972 | |
| 1973 | // RFC 4566 |
| 1974 | // a=* (zero or more session attribute lines) |
| 1975 | while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) { |
| 1976 | if (HasAttribute(line, kAttributeGroup)) { |
| 1977 | if (!ParseGroupAttribute(line, desc, error)) { |
| 1978 | return false; |
| 1979 | } |
| 1980 | } else if (HasAttribute(line, kAttributeIceUfrag)) { |
| 1981 | if (!GetValue(line, kAttributeIceUfrag, |
| 1982 | &(session_td->ice_ufrag), error)) { |
| 1983 | return false; |
| 1984 | } |
| 1985 | } else if (HasAttribute(line, kAttributeIcePwd)) { |
| 1986 | if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) { |
| 1987 | return false; |
| 1988 | } |
| 1989 | } else if (HasAttribute(line, kAttributeIceLite)) { |
| 1990 | session_td->ice_mode = cricket::ICEMODE_LITE; |
| 1991 | } else if (HasAttribute(line, kAttributeIceOption)) { |
| 1992 | if (!ParseIceOptions(line, &(session_td->transport_options), error)) { |
| 1993 | return false; |
| 1994 | } |
| 1995 | } else if (HasAttribute(line, kAttributeFingerprint)) { |
| 1996 | if (session_td->identity_fingerprint.get()) { |
| 1997 | return ParseFailed( |
| 1998 | line, |
| 1999 | "Can't have multiple fingerprint attributes at the same level.", |
| 2000 | error); |
| 2001 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2002 | rtc::SSLFingerprint* fingerprint = NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2003 | if (!ParseFingerprintAttribute(line, &fingerprint, error)) { |
| 2004 | return false; |
| 2005 | } |
| 2006 | session_td->identity_fingerprint.reset(fingerprint); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2007 | } else if (HasAttribute(line, kAttributeSetup)) { |
| 2008 | if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) { |
| 2009 | return false; |
| 2010 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2011 | } else if (HasAttribute(line, kAttributeMsidSemantics)) { |
| 2012 | std::string semantics; |
| 2013 | if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) { |
| 2014 | return false; |
| 2015 | } |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 2016 | desc->set_msid_supported( |
| 2017 | CaseInsensitiveFind(semantics, kMediaStreamSemantic)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2018 | } else if (HasAttribute(line, kAttributeExtmap)) { |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 2019 | RtpExtension extmap; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2020 | if (!ParseExtmap(line, &extmap, error)) { |
| 2021 | return false; |
| 2022 | } |
| 2023 | session_extmaps->push_back(extmap); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | return true; |
| 2028 | } |
| 2029 | |
| 2030 | bool ParseGroupAttribute(const std::string& line, |
| 2031 | cricket::SessionDescription* desc, |
| 2032 | SdpParseError* error) { |
| 2033 | ASSERT(desc != NULL); |
| 2034 | |
| 2035 | // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00 |
| 2036 | // a=group:BUNDLE video voice |
| 2037 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2038 | rtc::split(line.substr(kLinePrefixLength), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2039 | kSdpDelimiterSpace, &fields); |
| 2040 | std::string semantics; |
| 2041 | if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) { |
| 2042 | return false; |
| 2043 | } |
| 2044 | cricket::ContentGroup group(semantics); |
| 2045 | for (size_t i = 1; i < fields.size(); ++i) { |
| 2046 | group.AddContentName(fields[i]); |
| 2047 | } |
| 2048 | desc->AddGroup(group); |
| 2049 | return true; |
| 2050 | } |
| 2051 | |
| 2052 | static bool ParseFingerprintAttribute(const std::string& line, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2053 | rtc::SSLFingerprint** fingerprint, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2054 | SdpParseError* error) { |
| 2055 | if (!IsLineType(line, kLineTypeAttributes) || |
| 2056 | !HasAttribute(line, kAttributeFingerprint)) { |
| 2057 | return ParseFailedExpectLine(line, 0, kLineTypeAttributes, |
| 2058 | kAttributeFingerprint, error); |
| 2059 | } |
| 2060 | |
| 2061 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2062 | rtc::split(line.substr(kLinePrefixLength), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2063 | kSdpDelimiterSpace, &fields); |
| 2064 | const size_t expected_fields = 2; |
| 2065 | if (fields.size() != expected_fields) { |
| 2066 | return ParseFailedExpectFieldNum(line, expected_fields, error); |
| 2067 | } |
| 2068 | |
| 2069 | // The first field here is "fingerprint:<hash>. |
| 2070 | std::string algorithm; |
| 2071 | if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) { |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
| 2075 | // Downcase the algorithm. Note that we don't need to downcase the |
| 2076 | // fingerprint because hex_decode can handle upper-case. |
| 2077 | std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(), |
| 2078 | ::tolower); |
| 2079 | |
| 2080 | // The second field is the digest value. De-hexify it. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2081 | *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2082 | algorithm, fields[1]); |
| 2083 | if (!*fingerprint) { |
| 2084 | return ParseFailed(line, |
| 2085 | "Failed to create fingerprint from the digest.", |
| 2086 | error); |
| 2087 | } |
| 2088 | |
| 2089 | return true; |
| 2090 | } |
| 2091 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2092 | static bool ParseDtlsSetup(const std::string& line, |
| 2093 | cricket::ConnectionRole* role, |
| 2094 | SdpParseError* error) { |
| 2095 | // setup-attr = "a=setup:" role |
| 2096 | // role = "active" / "passive" / "actpass" / "holdconn" |
| 2097 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2098 | rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2099 | const size_t expected_fields = 2; |
| 2100 | if (fields.size() != expected_fields) { |
| 2101 | return ParseFailedExpectFieldNum(line, expected_fields, error); |
| 2102 | } |
| 2103 | std::string role_str = fields[1]; |
| 2104 | if (!cricket::StringToConnectionRole(role_str, role)) { |
| 2105 | return ParseFailed(line, "Invalid attribute value.", error); |
| 2106 | } |
| 2107 | return true; |
| 2108 | } |
| 2109 | |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 2110 | static bool ParseMsidAttribute(const std::string& line, |
| 2111 | std::string* stream_id, |
| 2112 | std::string* track_id, |
| 2113 | SdpParseError* error) { |
| 2114 | // draft-ietf-mmusic-msid-11 |
| 2115 | // a=msid:<stream id> <track id> |
| 2116 | // msid-value = msid-id [ SP msid-appdata ] |
| 2117 | // msid-id = 1*64token-char ; see RFC 4566 |
| 2118 | // msid-appdata = 1*64token-char ; see RFC 4566 |
| 2119 | std::string field1; |
| 2120 | if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace, |
| 2121 | &field1, track_id)) { |
| 2122 | const size_t expected_fields = 2; |
| 2123 | return ParseFailedExpectFieldNum(line, expected_fields, error); |
| 2124 | } |
| 2125 | |
| 2126 | // msid:<msid-id> |
| 2127 | if (!GetValue(field1, kAttributeMsid, stream_id, error)) { |
| 2128 | return false; |
| 2129 | } |
| 2130 | return true; |
| 2131 | } |
| 2132 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2133 | // RFC 3551 |
| 2134 | // PT encoding media type clock rate channels |
| 2135 | // name (Hz) |
| 2136 | // 0 PCMU A 8,000 1 |
| 2137 | // 1 reserved A |
| 2138 | // 2 reserved A |
| 2139 | // 3 GSM A 8,000 1 |
| 2140 | // 4 G723 A 8,000 1 |
| 2141 | // 5 DVI4 A 8,000 1 |
| 2142 | // 6 DVI4 A 16,000 1 |
| 2143 | // 7 LPC A 8,000 1 |
| 2144 | // 8 PCMA A 8,000 1 |
| 2145 | // 9 G722 A 8,000 1 |
| 2146 | // 10 L16 A 44,100 2 |
| 2147 | // 11 L16 A 44,100 1 |
| 2148 | // 12 QCELP A 8,000 1 |
| 2149 | // 13 CN A 8,000 1 |
| 2150 | // 14 MPA A 90,000 (see text) |
| 2151 | // 15 G728 A 8,000 1 |
| 2152 | // 16 DVI4 A 11,025 1 |
| 2153 | // 17 DVI4 A 22,050 1 |
| 2154 | // 18 G729 A 8,000 1 |
| 2155 | struct StaticPayloadAudioCodec { |
| 2156 | const char* name; |
| 2157 | int clockrate; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 2158 | size_t channels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2159 | }; |
| 2160 | static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = { |
| 2161 | { "PCMU", 8000, 1 }, |
| 2162 | { "reserved", 0, 0 }, |
| 2163 | { "reserved", 0, 0 }, |
| 2164 | { "GSM", 8000, 1 }, |
| 2165 | { "G723", 8000, 1 }, |
| 2166 | { "DVI4", 8000, 1 }, |
| 2167 | { "DVI4", 16000, 1 }, |
| 2168 | { "LPC", 8000, 1 }, |
| 2169 | { "PCMA", 8000, 1 }, |
| 2170 | { "G722", 8000, 1 }, |
| 2171 | { "L16", 44100, 2 }, |
| 2172 | { "L16", 44100, 1 }, |
| 2173 | { "QCELP", 8000, 1 }, |
| 2174 | { "CN", 8000, 1 }, |
| 2175 | { "MPA", 90000, 1 }, |
| 2176 | { "G728", 8000, 1 }, |
| 2177 | { "DVI4", 11025, 1 }, |
| 2178 | { "DVI4", 22050, 1 }, |
| 2179 | { "G729", 8000, 1 }, |
| 2180 | }; |
| 2181 | |
| 2182 | void MaybeCreateStaticPayloadAudioCodecs( |
| 2183 | const std::vector<int>& fmts, AudioContentDescription* media_desc) { |
| 2184 | if (!media_desc) { |
| 2185 | return; |
| 2186 | } |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2187 | RTC_DCHECK(media_desc->codecs().empty()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2188 | std::vector<int>::const_iterator it = fmts.begin(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2189 | for (; it != fmts.end(); ++it) { |
| 2190 | int payload_type = *it; |
| 2191 | if (!media_desc->HasCodec(payload_type) && |
| 2192 | payload_type >= 0 && |
kjellander | 3e33bfe | 2016-06-20 07:04:09 -0700 | [diff] [blame^] | 2193 | static_cast<uint32_t>(payload_type) < |
| 2194 | arraysize(kStaticPayloadAudioCodecs)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2195 | std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name; |
| 2196 | int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 2197 | size_t channels = kStaticPayloadAudioCodecs[payload_type].channels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2198 | media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2199 | clock_rate, 0, channels)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2200 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | template <class C> |
| 2205 | static C* ParseContentDescription(const std::string& message, |
| 2206 | const MediaType media_type, |
| 2207 | int mline_index, |
| 2208 | const std::string& protocol, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2209 | const std::vector<int>& payload_types, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2210 | size_t* pos, |
| 2211 | std::string* content_name, |
| 2212 | TransportDescription* transport, |
| 2213 | std::vector<JsepIceCandidate*>* candidates, |
| 2214 | webrtc::SdpParseError* error) { |
| 2215 | C* media_desc = new C(); |
| 2216 | switch (media_type) { |
| 2217 | case cricket::MEDIA_TYPE_AUDIO: |
| 2218 | *content_name = cricket::CN_AUDIO; |
| 2219 | break; |
| 2220 | case cricket::MEDIA_TYPE_VIDEO: |
| 2221 | *content_name = cricket::CN_VIDEO; |
| 2222 | break; |
| 2223 | case cricket::MEDIA_TYPE_DATA: |
| 2224 | *content_name = cricket::CN_DATA; |
| 2225 | break; |
| 2226 | default: |
| 2227 | ASSERT(false); |
| 2228 | break; |
| 2229 | } |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2230 | if (!ParseContent(message, media_type, mline_index, protocol, payload_types, |
| 2231 | pos, content_name, media_desc, transport, candidates, |
| 2232 | error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2233 | delete media_desc; |
| 2234 | return NULL; |
| 2235 | } |
| 2236 | // Sort the codecs according to the m-line fmt list. |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2237 | std::unordered_map<int, int> payload_type_preferences; |
| 2238 | // "size + 1" so that the lowest preference payload type has a preference of |
| 2239 | // 1, which is greater than the default (0) for payload types not in the fmt |
| 2240 | // list. |
| 2241 | int preference = static_cast<int>(payload_types.size() + 1); |
| 2242 | for (int pt : payload_types) { |
| 2243 | payload_type_preferences[pt] = preference--; |
| 2244 | } |
| 2245 | std::vector<typename C::CodecType> codecs = media_desc->codecs(); |
| 2246 | std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences]( |
| 2247 | const typename C::CodecType& a, |
| 2248 | const typename C::CodecType& b) { |
| 2249 | return payload_type_preferences[a.id] > payload_type_preferences[b.id]; |
| 2250 | }); |
| 2251 | media_desc->set_codecs(codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2252 | return media_desc; |
| 2253 | } |
| 2254 | |
| 2255 | bool ParseMediaDescription(const std::string& message, |
| 2256 | const TransportDescription& session_td, |
| 2257 | const RtpHeaderExtensions& session_extmaps, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2258 | size_t* pos, |
| 2259 | cricket::SessionDescription* desc, |
| 2260 | std::vector<JsepIceCandidate*>* candidates, |
| 2261 | SdpParseError* error) { |
| 2262 | ASSERT(desc != NULL); |
| 2263 | std::string line; |
| 2264 | int mline_index = -1; |
| 2265 | |
| 2266 | // Zero or more media descriptions |
| 2267 | // RFC 4566 |
| 2268 | // m=<media> <port> <proto> <fmt> |
| 2269 | while (GetLineWithType(message, pos, &line, kLineTypeMedia)) { |
| 2270 | ++mline_index; |
| 2271 | |
| 2272 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2273 | rtc::split(line.substr(kLinePrefixLength), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2274 | kSdpDelimiterSpace, &fields); |
| 2275 | const size_t expected_min_fields = 4; |
| 2276 | if (fields.size() < expected_min_fields) { |
| 2277 | return ParseFailedExpectMinFieldNum(line, expected_min_fields, error); |
| 2278 | } |
| 2279 | bool rejected = false; |
| 2280 | // RFC 3264 |
| 2281 | // To reject an offered stream, the port number in the corresponding stream |
| 2282 | // in the answer MUST be set to zero. |
| 2283 | if (fields[1] == kMediaPortRejected) { |
| 2284 | rejected = true; |
| 2285 | } |
| 2286 | |
| 2287 | std::string protocol = fields[2]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2288 | |
| 2289 | // <fmt> |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2290 | std::vector<int> payload_types; |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 2291 | if (IsRtp(protocol)) { |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 2292 | for (size_t j = 3 ; j < fields.size(); ++j) { |
| 2293 | // TODO(wu): Remove when below bug is fixed. |
| 2294 | // https://bugzilla.mozilla.org/show_bug.cgi?id=996329 |
pbos | bb36fdf | 2015-07-09 07:48:14 -0700 | [diff] [blame] | 2295 | if (fields[j].empty() && j == fields.size() - 1) { |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 2296 | continue; |
| 2297 | } |
wu@webrtc.org | 36eda7c | 2014-04-15 20:37:30 +0000 | [diff] [blame] | 2298 | |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 2299 | int pl = 0; |
pkasting@chromium.org | e9facf8 | 2015-02-17 20:36:28 +0000 | [diff] [blame] | 2300 | if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) { |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 2301 | return false; |
| 2302 | } |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2303 | payload_types.push_back(pl); |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 2304 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | // Make a temporary TransportDescription based on |session_td|. |
| 2308 | // Some of this gets overwritten by ParseContent. |
deadbeef | 46eed76 | 2016-01-28 13:24:37 -0800 | [diff] [blame] | 2309 | TransportDescription transport( |
| 2310 | session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd, |
| 2311 | session_td.ice_mode, session_td.connection_role, |
| 2312 | session_td.identity_fingerprint.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2313 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 2314 | std::unique_ptr<MediaContentDescription> content; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2315 | std::string content_name; |
| 2316 | if (HasAttribute(line, kMediaTypeVideo)) { |
| 2317 | content.reset(ParseContentDescription<VideoContentDescription>( |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2318 | message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol, |
| 2319 | payload_types, pos, &content_name, &transport, candidates, error)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2320 | } else if (HasAttribute(line, kMediaTypeAudio)) { |
| 2321 | content.reset(ParseContentDescription<AudioContentDescription>( |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2322 | message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol, |
| 2323 | payload_types, pos, &content_name, &transport, candidates, error)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2324 | } else if (HasAttribute(line, kMediaTypeData)) { |
jiayl@webrtc.org | 0e52772 | 2014-09-08 21:43:43 +0000 | [diff] [blame] | 2325 | DataContentDescription* data_desc = |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 2326 | ParseContentDescription<DataContentDescription>( |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2327 | message, cricket::MEDIA_TYPE_DATA, mline_index, protocol, |
| 2328 | payload_types, pos, &content_name, &transport, candidates, error); |
jiayl@webrtc.org | 0e52772 | 2014-09-08 21:43:43 +0000 | [diff] [blame] | 2329 | content.reset(data_desc); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 2330 | |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 2331 | int p; |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 2332 | if (data_desc && IsDtlsSctp(protocol) && rtc::FromString(fields[3], &p)) { |
jiayl@webrtc.org | 0e52772 | 2014-09-08 21:43:43 +0000 | [diff] [blame] | 2333 | if (!AddSctpDataCodec(data_desc, p)) |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 2334 | return false; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 2335 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2336 | } else { |
| 2337 | LOG(LS_WARNING) << "Unsupported media type: " << line; |
| 2338 | continue; |
| 2339 | } |
| 2340 | if (!content.get()) { |
| 2341 | // ParseContentDescription returns NULL if failed. |
| 2342 | return false; |
| 2343 | } |
| 2344 | |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 2345 | if (IsRtp(protocol)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2346 | // Set the extmap. |
| 2347 | if (!session_extmaps.empty() && |
| 2348 | !content->rtp_header_extensions().empty()) { |
| 2349 | return ParseFailed("", |
| 2350 | "The a=extmap MUST be either all session level or " |
| 2351 | "all media level.", |
| 2352 | error); |
| 2353 | } |
| 2354 | for (size_t i = 0; i < session_extmaps.size(); ++i) { |
| 2355 | content->AddRtpHeaderExtension(session_extmaps[i]); |
| 2356 | } |
| 2357 | } |
| 2358 | content->set_protocol(protocol); |
| 2359 | desc->AddContent(content_name, |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 2360 | IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP : |
| 2361 | cricket::NS_JINGLE_RTP, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2362 | rejected, |
| 2363 | content.release()); |
| 2364 | // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag". |
| 2365 | TransportInfo transport_info(content_name, transport); |
| 2366 | |
| 2367 | if (!desc->AddTransportInfo(transport_info)) { |
| 2368 | std::ostringstream description; |
| 2369 | description << "Failed to AddTransportInfo with content name: " |
| 2370 | << content_name; |
| 2371 | return ParseFailed("", description.str(), error); |
| 2372 | } |
| 2373 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 2374 | |
| 2375 | size_t end_of_message = message.size(); |
| 2376 | if (mline_index == -1 && *pos != end_of_message) { |
| 2377 | ParseFailed(message, *pos, "Expects m line.", error); |
| 2378 | return false; |
| 2379 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2380 | return true; |
| 2381 | } |
| 2382 | |
| 2383 | bool VerifyCodec(const cricket::Codec& codec) { |
| 2384 | // Codec has not been populated correctly unless the name has been set. This |
| 2385 | // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't |
| 2386 | // have a corresponding "rtpmap" line. |
| 2387 | cricket::Codec default_codec; |
| 2388 | return default_codec.name != codec.name; |
| 2389 | } |
| 2390 | |
| 2391 | bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) { |
| 2392 | const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs(); |
| 2393 | for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin(); |
| 2394 | iter != codecs.end(); ++iter) { |
| 2395 | if (!VerifyCodec(*iter)) { |
| 2396 | return false; |
| 2397 | } |
| 2398 | } |
| 2399 | return true; |
| 2400 | } |
| 2401 | |
| 2402 | bool VerifyVideoCodecs(const VideoContentDescription* video_desc) { |
| 2403 | const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs(); |
| 2404 | for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin(); |
| 2405 | iter != codecs.end(); ++iter) { |
| 2406 | if (!VerifyCodec(*iter)) { |
| 2407 | return false; |
| 2408 | } |
| 2409 | } |
| 2410 | return true; |
| 2411 | } |
| 2412 | |
| 2413 | void AddParameters(const cricket::CodecParameterMap& parameters, |
| 2414 | cricket::Codec* codec) { |
| 2415 | for (cricket::CodecParameterMap::const_iterator iter = |
| 2416 | parameters.begin(); iter != parameters.end(); ++iter) { |
| 2417 | codec->SetParam(iter->first, iter->second); |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param, |
| 2422 | cricket::Codec* codec) { |
| 2423 | codec->AddFeedbackParam(feedback_param); |
| 2424 | } |
| 2425 | |
| 2426 | void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params, |
| 2427 | cricket::Codec* codec) { |
| 2428 | for (std::vector<cricket::FeedbackParam>::const_iterator iter = |
| 2429 | feedback_params.params().begin(); |
| 2430 | iter != feedback_params.params().end(); ++iter) { |
| 2431 | codec->AddFeedbackParam(*iter); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | // Gets the current codec setting associated with |payload_type|. If there |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 2436 | // is no Codec associated with that payload type it returns an empty codec |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2437 | // with that payload type. |
| 2438 | template <class T> |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 2439 | T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) { |
| 2440 | T ret_val; |
| 2441 | if (!FindCodecById(codecs, payload_type, &ret_val)) { |
| 2442 | ret_val.id = payload_type; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2443 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2444 | return ret_val; |
| 2445 | } |
| 2446 | |
| 2447 | // Updates or creates a new codec entry in the audio description. |
| 2448 | template <class T, class U> |
| 2449 | void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) { |
| 2450 | T* desc = static_cast<T*>(content_desc); |
| 2451 | std::vector<U> codecs = desc->codecs(); |
| 2452 | bool found = false; |
| 2453 | |
| 2454 | typename std::vector<U>::iterator iter; |
| 2455 | for (iter = codecs.begin(); iter != codecs.end(); ++iter) { |
| 2456 | if (iter->id == codec.id) { |
| 2457 | *iter = codec; |
| 2458 | found = true; |
| 2459 | break; |
| 2460 | } |
| 2461 | } |
| 2462 | if (!found) { |
| 2463 | desc->AddCodec(codec); |
| 2464 | return; |
| 2465 | } |
| 2466 | desc->set_codecs(codecs); |
| 2467 | } |
| 2468 | |
| 2469 | // Adds or updates existing codec corresponding to |payload_type| according |
| 2470 | // to |parameters|. |
| 2471 | template <class T, class U> |
| 2472 | void UpdateCodec(MediaContentDescription* content_desc, int payload_type, |
| 2473 | const cricket::CodecParameterMap& parameters) { |
| 2474 | // Codec might already have been populated (from rtpmap). |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 2475 | U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(), |
| 2476 | payload_type); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2477 | AddParameters(parameters, &new_codec); |
| 2478 | AddOrReplaceCodec<T, U>(content_desc, new_codec); |
| 2479 | } |
| 2480 | |
| 2481 | // Adds or updates existing codec corresponding to |payload_type| according |
| 2482 | // to |feedback_param|. |
| 2483 | template <class T, class U> |
| 2484 | void UpdateCodec(MediaContentDescription* content_desc, int payload_type, |
| 2485 | const cricket::FeedbackParam& feedback_param) { |
| 2486 | // Codec might already have been populated (from rtpmap). |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 2487 | U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(), |
| 2488 | payload_type); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2489 | AddFeedbackParameter(feedback_param, &new_codec); |
| 2490 | AddOrReplaceCodec<T, U>(content_desc, new_codec); |
| 2491 | } |
| 2492 | |
jlmiller@webrtc.org | a744a28 | 2015-02-18 21:37:46 +0000 | [diff] [blame] | 2493 | template <class T> |
| 2494 | bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) { |
| 2495 | for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2496 | if (iter->id == kWildcardPayloadType) { |
| 2497 | *wildcard_codec = *iter; |
| 2498 | codecs->erase(iter); |
| 2499 | return true; |
| 2500 | } |
| 2501 | } |
| 2502 | return false; |
| 2503 | } |
| 2504 | |
jlmiller@webrtc.org | a744a28 | 2015-02-18 21:37:46 +0000 | [diff] [blame] | 2505 | template<class T> |
| 2506 | void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) { |
| 2507 | auto codecs = desc->codecs(); |
| 2508 | T wildcard_codec; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2509 | if (!PopWildcardCodec(&codecs, &wildcard_codec)) { |
| 2510 | return; |
| 2511 | } |
jlmiller@webrtc.org | a744a28 | 2015-02-18 21:37:46 +0000 | [diff] [blame] | 2512 | for (auto& codec : codecs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2513 | AddFeedbackParameters(wildcard_codec.feedback_params, &codec); |
| 2514 | } |
jlmiller@webrtc.org | a744a28 | 2015-02-18 21:37:46 +0000 | [diff] [blame] | 2515 | desc->set_codecs(codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | void AddAudioAttribute(const std::string& name, const std::string& value, |
| 2519 | AudioContentDescription* audio_desc) { |
| 2520 | if (value.empty()) { |
| 2521 | return; |
| 2522 | } |
| 2523 | std::vector<cricket::AudioCodec> codecs = audio_desc->codecs(); |
| 2524 | for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin(); |
| 2525 | iter != codecs.end(); ++iter) { |
| 2526 | iter->params[name] = value; |
| 2527 | } |
| 2528 | audio_desc->set_codecs(codecs); |
| 2529 | } |
| 2530 | |
| 2531 | bool ParseContent(const std::string& message, |
| 2532 | const MediaType media_type, |
| 2533 | int mline_index, |
| 2534 | const std::string& protocol, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2535 | const std::vector<int>& payload_types, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2536 | size_t* pos, |
| 2537 | std::string* content_name, |
| 2538 | MediaContentDescription* media_desc, |
| 2539 | TransportDescription* transport, |
| 2540 | std::vector<JsepIceCandidate*>* candidates, |
| 2541 | SdpParseError* error) { |
| 2542 | ASSERT(media_desc != NULL); |
| 2543 | ASSERT(content_name != NULL); |
| 2544 | ASSERT(transport != NULL); |
| 2545 | |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 2546 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 2547 | MaybeCreateStaticPayloadAudioCodecs( |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2548 | payload_types, static_cast<AudioContentDescription*>(media_desc)); |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2551 | // The media level "ice-ufrag" and "ice-pwd". |
| 2552 | // The candidates before update the media level "ice-pwd" and "ice-ufrag". |
| 2553 | Candidates candidates_orig; |
| 2554 | std::string line; |
| 2555 | std::string mline_id; |
| 2556 | // Tracks created out of the ssrc attributes. |
| 2557 | StreamParamsVec tracks; |
| 2558 | SsrcInfoVec ssrc_infos; |
| 2559 | SsrcGroupVec ssrc_groups; |
| 2560 | std::string maxptime_as_string; |
| 2561 | std::string ptime_as_string; |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 2562 | std::string stream_id; |
| 2563 | std::string track_id; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2564 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2565 | // Loop until the next m line |
| 2566 | while (!IsLineType(message, kLineTypeMedia, *pos)) { |
| 2567 | if (!GetLine(message, pos, &line)) { |
| 2568 | if (*pos >= message.size()) { |
| 2569 | break; // Done parsing |
| 2570 | } else { |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 2571 | return ParseFailed(message, *pos, "Invalid SDP line.", error); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2572 | } |
| 2573 | } |
| 2574 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2575 | // RFC 4566 |
| 2576 | // b=* (zero or more bandwidth information lines) |
| 2577 | if (IsLineType(line, kLineTypeSessionBandwidth)) { |
| 2578 | std::string bandwidth; |
| 2579 | if (HasAttribute(line, kApplicationSpecificMaximum)) { |
| 2580 | if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) { |
| 2581 | return false; |
| 2582 | } else { |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 2583 | int b = 0; |
| 2584 | if (!GetValueFromString(line, bandwidth, &b, error)) { |
| 2585 | return false; |
| 2586 | } |
Peter Thatcher | c0c3a86 | 2015-06-24 15:31:25 -0700 | [diff] [blame] | 2587 | // We should never use more than the default bandwidth for RTP-based |
| 2588 | // data channels. Don't allow SDP to set the bandwidth, because |
| 2589 | // that would give JS the opportunity to "break the Internet". |
| 2590 | // See: https://code.google.com/p/chromium/issues/detail?id=280726 |
| 2591 | if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) && |
| 2592 | b > cricket::kDataMaxBandwidth / 1000) { |
| 2593 | std::ostringstream description; |
| 2594 | description << "RTP-based data channels may not send more than " |
| 2595 | << cricket::kDataMaxBandwidth / 1000 << "kbps."; |
| 2596 | return ParseFailed(line, description.str(), error); |
| 2597 | } |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 2598 | media_desc->set_bandwidth(b * 1000); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | continue; |
| 2602 | } |
| 2603 | |
| 2604 | if (!IsLineType(line, kLineTypeAttributes)) { |
| 2605 | // TODO: Handle other lines if needed. |
| 2606 | LOG(LS_INFO) << "Ignored line: " << line; |
| 2607 | continue; |
| 2608 | } |
| 2609 | |
| 2610 | // Handle attributes common to SCTP and RTP. |
| 2611 | if (HasAttribute(line, kAttributeMid)) { |
| 2612 | // RFC 3388 |
| 2613 | // mid-attribute = "a=mid:" identification-tag |
| 2614 | // identification-tag = token |
| 2615 | // Use the mid identification-tag as the content name. |
| 2616 | if (!GetValue(line, kAttributeMid, &mline_id, error)) { |
| 2617 | return false; |
| 2618 | } |
| 2619 | *content_name = mline_id; |
| 2620 | } else if (HasAttribute(line, kAttributeCandidate)) { |
| 2621 | Candidate candidate; |
| 2622 | if (!ParseCandidate(line, &candidate, error, false)) { |
| 2623 | return false; |
| 2624 | } |
| 2625 | candidates_orig.push_back(candidate); |
| 2626 | } else if (HasAttribute(line, kAttributeIceUfrag)) { |
| 2627 | if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) { |
| 2628 | return false; |
| 2629 | } |
| 2630 | } else if (HasAttribute(line, kAttributeIcePwd)) { |
| 2631 | if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) { |
| 2632 | return false; |
| 2633 | } |
| 2634 | } else if (HasAttribute(line, kAttributeIceOption)) { |
| 2635 | if (!ParseIceOptions(line, &transport->transport_options, error)) { |
| 2636 | return false; |
| 2637 | } |
| 2638 | } else if (HasAttribute(line, kAttributeFmtp)) { |
| 2639 | if (!ParseFmtpAttributes(line, media_type, media_desc, error)) { |
| 2640 | return false; |
| 2641 | } |
| 2642 | } else if (HasAttribute(line, kAttributeFingerprint)) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2643 | rtc::SSLFingerprint* fingerprint = NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2644 | |
| 2645 | if (!ParseFingerprintAttribute(line, &fingerprint, error)) { |
| 2646 | return false; |
| 2647 | } |
| 2648 | transport->identity_fingerprint.reset(fingerprint); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2649 | } else if (HasAttribute(line, kAttributeSetup)) { |
| 2650 | if (!ParseDtlsSetup(line, &(transport->connection_role), error)) { |
| 2651 | return false; |
| 2652 | } |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 2653 | } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) { |
jiayl@webrtc.org | ddb85ab | 2014-09-05 16:31:56 +0000 | [diff] [blame] | 2654 | int sctp_port; |
| 2655 | if (!ParseSctpPort(line, &sctp_port, error)) { |
| 2656 | return false; |
| 2657 | } |
| 2658 | if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc), |
| 2659 | sctp_port)) { |
| 2660 | return false; |
| 2661 | } |
pthatcher@webrtc.org | 3341b40 | 2015-02-13 21:14:22 +0000 | [diff] [blame] | 2662 | } else if (IsRtp(protocol)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2663 | // |
| 2664 | // RTP specific attrubtes |
| 2665 | // |
| 2666 | if (HasAttribute(line, kAttributeRtcpMux)) { |
| 2667 | media_desc->set_rtcp_mux(true); |
deadbeef | 1387149 | 2015-12-09 12:37:51 -0800 | [diff] [blame] | 2668 | } else if (HasAttribute(line, kAttributeRtcpReducedSize)) { |
| 2669 | media_desc->set_rtcp_reduced_size(true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2670 | } else if (HasAttribute(line, kAttributeSsrcGroup)) { |
| 2671 | if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) { |
| 2672 | return false; |
| 2673 | } |
| 2674 | } else if (HasAttribute(line, kAttributeSsrc)) { |
| 2675 | if (!ParseSsrcAttribute(line, &ssrc_infos, error)) { |
| 2676 | return false; |
| 2677 | } |
| 2678 | } else if (HasAttribute(line, kAttributeCrypto)) { |
| 2679 | if (!ParseCryptoAttribute(line, media_desc, error)) { |
| 2680 | return false; |
| 2681 | } |
| 2682 | } else if (HasAttribute(line, kAttributeRtpmap)) { |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2683 | if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc, |
| 2684 | error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2685 | return false; |
| 2686 | } |
| 2687 | } else if (HasAttribute(line, kCodecParamMaxPTime)) { |
| 2688 | if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) { |
| 2689 | return false; |
| 2690 | } |
| 2691 | } else if (HasAttribute(line, kAttributeRtcpFb)) { |
| 2692 | if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) { |
| 2693 | return false; |
| 2694 | } |
| 2695 | } else if (HasAttribute(line, kCodecParamPTime)) { |
| 2696 | if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) { |
| 2697 | return false; |
| 2698 | } |
| 2699 | } else if (HasAttribute(line, kAttributeSendOnly)) { |
| 2700 | media_desc->set_direction(cricket::MD_SENDONLY); |
| 2701 | } else if (HasAttribute(line, kAttributeRecvOnly)) { |
| 2702 | media_desc->set_direction(cricket::MD_RECVONLY); |
| 2703 | } else if (HasAttribute(line, kAttributeInactive)) { |
| 2704 | media_desc->set_direction(cricket::MD_INACTIVE); |
| 2705 | } else if (HasAttribute(line, kAttributeSendRecv)) { |
| 2706 | media_desc->set_direction(cricket::MD_SENDRECV); |
| 2707 | } else if (HasAttribute(line, kAttributeExtmap)) { |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 2708 | RtpExtension extmap; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2709 | if (!ParseExtmap(line, &extmap, error)) { |
| 2710 | return false; |
| 2711 | } |
| 2712 | media_desc->AddRtpHeaderExtension(extmap); |
| 2713 | } else if (HasAttribute(line, kAttributeXGoogleFlag)) { |
| 2714 | // Experimental attribute. Conference mode activates more aggressive |
| 2715 | // AEC and NS settings. |
| 2716 | // TODO: expose API to set these directly. |
| 2717 | std::string flag_value; |
| 2718 | if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) { |
| 2719 | return false; |
| 2720 | } |
| 2721 | if (flag_value.compare(kValueConference) == 0) |
| 2722 | media_desc->set_conference_mode(true); |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 2723 | } else if (HasAttribute(line, kAttributeMsid)) { |
| 2724 | if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) { |
| 2725 | return false; |
| 2726 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2727 | } |
| 2728 | } else { |
| 2729 | // Only parse lines that we are interested of. |
| 2730 | LOG(LS_INFO) << "Ignored line: " << line; |
| 2731 | continue; |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | // Create tracks from the |ssrc_infos|. |
Taylor Brandstetter | 5de6b75 | 2016-03-09 17:02:30 -0800 | [diff] [blame] | 2736 | // If the stream_id/track_id for all SSRCS are identical, one StreamParams |
| 2737 | // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from |
| 2738 | // the m= section. |
| 2739 | CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2740 | |
| 2741 | // Add the ssrc group to the track. |
| 2742 | for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin(); |
| 2743 | ssrc_group != ssrc_groups.end(); ++ssrc_group) { |
| 2744 | if (ssrc_group->ssrcs.empty()) { |
| 2745 | continue; |
| 2746 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2747 | uint32_t ssrc = ssrc_group->ssrcs.front(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2748 | for (StreamParamsVec::iterator track = tracks.begin(); |
| 2749 | track != tracks.end(); ++track) { |
| 2750 | if (track->has_ssrc(ssrc)) { |
| 2751 | track->ssrc_groups.push_back(*ssrc_group); |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | // Add the new tracks to the |media_desc|. |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 2757 | for (StreamParams& track : tracks) { |
| 2758 | media_desc->AddStream(track); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 2762 | AudioContentDescription* audio_desc = |
| 2763 | static_cast<AudioContentDescription*>(media_desc); |
jlmiller@webrtc.org | a744a28 | 2015-02-18 21:37:46 +0000 | [diff] [blame] | 2764 | UpdateFromWildcardCodecs(audio_desc); |
| 2765 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2766 | // Verify audio codec ensures that no audio codec has been populated with |
| 2767 | // only fmtp. |
| 2768 | if (!VerifyAudioCodecs(audio_desc)) { |
| 2769 | return ParseFailed("Failed to parse audio codecs correctly.", error); |
| 2770 | } |
| 2771 | AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc); |
| 2772 | AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc); |
| 2773 | } |
| 2774 | |
| 2775 | if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
jlmiller@webrtc.org | a744a28 | 2015-02-18 21:37:46 +0000 | [diff] [blame] | 2776 | VideoContentDescription* video_desc = |
| 2777 | static_cast<VideoContentDescription*>(media_desc); |
| 2778 | UpdateFromWildcardCodecs(video_desc); |
| 2779 | // Verify video codec ensures that no video codec has been populated with |
| 2780 | // only rtcp-fb. |
| 2781 | if (!VerifyVideoCodecs(video_desc)) { |
| 2782 | return ParseFailed("Failed to parse video codecs correctly.", error); |
| 2783 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | // RFC 5245 |
| 2787 | // Update the candidates with the media level "ice-pwd" and "ice-ufrag". |
| 2788 | for (Candidates::iterator it = candidates_orig.begin(); |
| 2789 | it != candidates_orig.end(); ++it) { |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 2790 | ASSERT((*it).username().empty() || |
| 2791 | (*it).username() == transport->ice_ufrag); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2792 | (*it).set_username(transport->ice_ufrag); |
| 2793 | ASSERT((*it).password().empty()); |
| 2794 | (*it).set_password(transport->ice_pwd); |
| 2795 | candidates->push_back( |
| 2796 | new JsepIceCandidate(mline_id, mline_index, *it)); |
| 2797 | } |
| 2798 | return true; |
| 2799 | } |
| 2800 | |
| 2801 | bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos, |
| 2802 | SdpParseError* error) { |
| 2803 | ASSERT(ssrc_infos != NULL); |
| 2804 | // RFC 5576 |
| 2805 | // a=ssrc:<ssrc-id> <attribute> |
| 2806 | // a=ssrc:<ssrc-id> <attribute>:<value> |
| 2807 | std::string field1, field2; |
Donald Curtis | 144d018 | 2015-05-15 13:14:24 -0700 | [diff] [blame] | 2808 | if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace, |
| 2809 | &field1, &field2)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2810 | const size_t expected_fields = 2; |
| 2811 | return ParseFailedExpectFieldNum(line, expected_fields, error); |
| 2812 | } |
| 2813 | |
| 2814 | // ssrc:<ssrc-id> |
| 2815 | std::string ssrc_id_s; |
| 2816 | if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) { |
| 2817 | return false; |
| 2818 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2819 | uint32_t ssrc_id = 0; |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 2820 | if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) { |
| 2821 | return false; |
| 2822 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2823 | |
| 2824 | std::string attribute; |
| 2825 | std::string value; |
Donald Curtis | 144d018 | 2015-05-15 13:14:24 -0700 | [diff] [blame] | 2826 | if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2827 | std::ostringstream description; |
| 2828 | description << "Failed to get the ssrc attribute value from " << field2 |
| 2829 | << ". Expected format <attribute>:<value>."; |
| 2830 | return ParseFailed(line, description.str(), error); |
| 2831 | } |
| 2832 | |
| 2833 | // Check if there's already an item for this |ssrc_id|. Create a new one if |
| 2834 | // there isn't. |
| 2835 | SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin(); |
| 2836 | for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) { |
| 2837 | if (ssrc_info->ssrc_id == ssrc_id) { |
| 2838 | break; |
| 2839 | } |
| 2840 | } |
| 2841 | if (ssrc_info == ssrc_infos->end()) { |
| 2842 | SsrcInfo info; |
| 2843 | info.ssrc_id = ssrc_id; |
| 2844 | ssrc_infos->push_back(info); |
| 2845 | ssrc_info = ssrc_infos->end() - 1; |
| 2846 | } |
| 2847 | |
| 2848 | // Store the info to the |ssrc_info|. |
| 2849 | if (attribute == kSsrcAttributeCname) { |
| 2850 | // RFC 5576 |
| 2851 | // cname:<value> |
| 2852 | ssrc_info->cname = value; |
| 2853 | } else if (attribute == kSsrcAttributeMsid) { |
| 2854 | // draft-alvestrand-mmusic-msid-00 |
| 2855 | // "msid:" identifier [ " " appdata ] |
| 2856 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2857 | rtc::split(value, kSdpDelimiterSpace, &fields); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2858 | if (fields.size() < 1 || fields.size() > 2) { |
| 2859 | return ParseFailed(line, |
| 2860 | "Expected format \"msid:<identifier>[ <appdata>]\".", |
| 2861 | error); |
| 2862 | } |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 2863 | ssrc_info->stream_id = fields[0]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2864 | if (fields.size() == 2) { |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 2865 | ssrc_info->track_id = fields[1]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2866 | } |
| 2867 | } else if (attribute == kSsrcAttributeMslabel) { |
| 2868 | // draft-alvestrand-rtcweb-mid-01 |
| 2869 | // mslabel:<value> |
| 2870 | ssrc_info->mslabel = value; |
| 2871 | } else if (attribute == kSSrcAttributeLabel) { |
| 2872 | // The label isn't defined. |
| 2873 | // label:<value> |
| 2874 | ssrc_info->label = value; |
| 2875 | } |
| 2876 | return true; |
| 2877 | } |
| 2878 | |
| 2879 | bool ParseSsrcGroupAttribute(const std::string& line, |
| 2880 | SsrcGroupVec* ssrc_groups, |
| 2881 | SdpParseError* error) { |
| 2882 | ASSERT(ssrc_groups != NULL); |
| 2883 | // RFC 5576 |
| 2884 | // a=ssrc-group:<semantics> <ssrc-id> ... |
| 2885 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2886 | rtc::split(line.substr(kLinePrefixLength), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2887 | kSdpDelimiterSpace, &fields); |
| 2888 | const size_t expected_min_fields = 2; |
| 2889 | if (fields.size() < expected_min_fields) { |
| 2890 | return ParseFailedExpectMinFieldNum(line, expected_min_fields, error); |
| 2891 | } |
| 2892 | std::string semantics; |
| 2893 | if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) { |
| 2894 | return false; |
| 2895 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2896 | std::vector<uint32_t> ssrcs; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2897 | for (size_t i = 1; i < fields.size(); ++i) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2898 | uint32_t ssrc = 0; |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 2899 | if (!GetValueFromString(line, fields[i], &ssrc, error)) { |
| 2900 | return false; |
| 2901 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2902 | ssrcs.push_back(ssrc); |
| 2903 | } |
| 2904 | ssrc_groups->push_back(SsrcGroup(semantics, ssrcs)); |
| 2905 | return true; |
| 2906 | } |
| 2907 | |
| 2908 | bool ParseCryptoAttribute(const std::string& line, |
| 2909 | MediaContentDescription* media_desc, |
| 2910 | SdpParseError* error) { |
| 2911 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2912 | rtc::split(line.substr(kLinePrefixLength), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2913 | kSdpDelimiterSpace, &fields); |
| 2914 | // RFC 4568 |
| 2915 | // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>] |
| 2916 | const size_t expected_min_fields = 3; |
| 2917 | if (fields.size() < expected_min_fields) { |
| 2918 | return ParseFailedExpectMinFieldNum(line, expected_min_fields, error); |
| 2919 | } |
| 2920 | std::string tag_value; |
| 2921 | if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) { |
| 2922 | return false; |
| 2923 | } |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 2924 | int tag = 0; |
| 2925 | if (!GetValueFromString(line, tag_value, &tag, error)) { |
| 2926 | return false; |
| 2927 | } |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 2928 | const std::string& crypto_suite = fields[1]; |
| 2929 | const std::string& key_params = fields[2]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2930 | std::string session_params; |
| 2931 | if (fields.size() > 3) { |
| 2932 | session_params = fields[3]; |
| 2933 | } |
| 2934 | media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params, |
| 2935 | session_params)); |
| 2936 | return true; |
| 2937 | } |
| 2938 | |
| 2939 | // Updates or creates a new codec entry in the audio description with according |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2940 | // to |name|, |clockrate|, |bitrate|, and |channels|. |
| 2941 | void UpdateCodec(int payload_type, |
| 2942 | const std::string& name, |
| 2943 | int clockrate, |
| 2944 | int bitrate, |
| 2945 | size_t channels, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2946 | AudioContentDescription* audio_desc) { |
| 2947 | // Codec may already be populated with (only) optional parameters |
| 2948 | // (from an fmtp). |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 2949 | cricket::AudioCodec codec = |
| 2950 | GetCodecWithPayloadType(audio_desc->codecs(), payload_type); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2951 | codec.name = name; |
| 2952 | codec.clockrate = clockrate; |
| 2953 | codec.bitrate = bitrate; |
| 2954 | codec.channels = channels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2955 | AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc, |
| 2956 | codec); |
| 2957 | } |
| 2958 | |
| 2959 | // Updates or creates a new codec entry in the video description according to |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2960 | // |name|, |width|, |height|, and |framerate|. |
| 2961 | void UpdateCodec(int payload_type, |
| 2962 | const std::string& name, |
| 2963 | int width, |
| 2964 | int height, |
| 2965 | int framerate, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2966 | VideoContentDescription* video_desc) { |
| 2967 | // Codec may already be populated with (only) optional parameters |
| 2968 | // (from an fmtp). |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 2969 | cricket::VideoCodec codec = |
| 2970 | GetCodecWithPayloadType(video_desc->codecs(), payload_type); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2971 | codec.name = name; |
| 2972 | codec.width = width; |
| 2973 | codec.height = height; |
| 2974 | codec.framerate = framerate; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2975 | AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc, |
| 2976 | codec); |
| 2977 | } |
| 2978 | |
| 2979 | bool ParseRtpmapAttribute(const std::string& line, |
| 2980 | const MediaType media_type, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 2981 | const std::vector<int>& payload_types, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2982 | MediaContentDescription* media_desc, |
| 2983 | SdpParseError* error) { |
| 2984 | std::vector<std::string> fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2985 | rtc::split(line.substr(kLinePrefixLength), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2986 | kSdpDelimiterSpace, &fields); |
| 2987 | // RFC 4566 |
| 2988 | // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>] |
| 2989 | const size_t expected_min_fields = 2; |
| 2990 | if (fields.size() < expected_min_fields) { |
| 2991 | return ParseFailedExpectMinFieldNum(line, expected_min_fields, error); |
| 2992 | } |
| 2993 | std::string payload_type_value; |
| 2994 | if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) { |
| 2995 | return false; |
| 2996 | } |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 2997 | int payload_type = 0; |
pkasting@chromium.org | e9facf8 | 2015-02-17 20:36:28 +0000 | [diff] [blame] | 2998 | if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type, |
| 2999 | error)) { |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 3000 | return false; |
| 3001 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3002 | |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 3003 | if (std::find(payload_types.begin(), payload_types.end(), payload_type) == |
| 3004 | payload_types.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3005 | LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the " |
| 3006 | << "<fmt> of the m-line: " << line; |
| 3007 | return true; |
| 3008 | } |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 3009 | const std::string& encoder = fields[1]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3010 | std::vector<std::string> codec_params; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 3011 | rtc::split(encoder, '/', &codec_params); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3012 | // <encoding name>/<clock rate>[/<encodingparameters>] |
| 3013 | // 2 mandatory fields |
| 3014 | if (codec_params.size() < 2 || codec_params.size() > 3) { |
| 3015 | return ParseFailed(line, |
| 3016 | "Expected format \"<encoding name>/<clock rate>" |
| 3017 | "[/<encodingparameters>]\".", |
| 3018 | error); |
| 3019 | } |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 3020 | const std::string& encoding_name = codec_params[0]; |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 3021 | int clock_rate = 0; |
| 3022 | if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) { |
| 3023 | return false; |
| 3024 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3025 | if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3026 | VideoContentDescription* video_desc = |
| 3027 | static_cast<VideoContentDescription*>(media_desc); |
| 3028 | // TODO: We will send resolution in SDP. For now use |
| 3029 | // JsepSessionDescription::kMaxVideoCodecWidth and kMaxVideoCodecHeight. |
| 3030 | UpdateCodec(payload_type, encoding_name, |
| 3031 | JsepSessionDescription::kMaxVideoCodecWidth, |
| 3032 | JsepSessionDescription::kMaxVideoCodecHeight, |
| 3033 | JsepSessionDescription::kDefaultVideoCodecFramerate, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 3034 | video_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3035 | } else if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 3036 | // RFC 4566 |
| 3037 | // For audio streams, <encoding parameters> indicates the number |
| 3038 | // of audio channels. This parameter is OPTIONAL and may be |
| 3039 | // omitted if the number of channels is one, provided that no |
| 3040 | // additional parameters are needed. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 3041 | size_t channels = 1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3042 | if (codec_params.size() == 3) { |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 3043 | if (!GetValueFromString(line, codec_params[2], &channels, error)) { |
| 3044 | return false; |
| 3045 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3046 | } |
| 3047 | int bitrate = 0; |
| 3048 | // The default behavior for ISAC (bitrate == 0) in webrtcvoiceengine.cc |
| 3049 | // (specifically FindWebRtcCodec) is bandwidth-adaptive variable bitrate. |
| 3050 | // The bandwidth adaptation doesn't always work well, so this code |
| 3051 | // sets a fixed target bitrate instead. |
| 3052 | if (_stricmp(encoding_name.c_str(), kIsacCodecName) == 0) { |
| 3053 | if (clock_rate <= 16000) { |
| 3054 | bitrate = kIsacWbDefaultRate; |
| 3055 | } else { |
| 3056 | bitrate = kIsacSwbDefaultRate; |
| 3057 | } |
| 3058 | } |
| 3059 | AudioContentDescription* audio_desc = |
| 3060 | static_cast<AudioContentDescription*>(media_desc); |
| 3061 | UpdateCodec(payload_type, encoding_name, clock_rate, bitrate, channels, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 3062 | audio_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3063 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
| 3064 | DataContentDescription* data_desc = |
| 3065 | static_cast<DataContentDescription*>(media_desc); |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 3066 | data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3067 | } |
| 3068 | return true; |
| 3069 | } |
| 3070 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3071 | bool ParseFmtpParam(const std::string& line, std::string* parameter, |
| 3072 | std::string* value, SdpParseError* error) { |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 3073 | if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3074 | ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error); |
| 3075 | return false; |
| 3076 | } |
| 3077 | // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ... |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3078 | return true; |
| 3079 | } |
| 3080 | |
| 3081 | bool ParseFmtpAttributes(const std::string& line, const MediaType media_type, |
| 3082 | MediaContentDescription* media_desc, |
| 3083 | SdpParseError* error) { |
| 3084 | if (media_type != cricket::MEDIA_TYPE_AUDIO && |
| 3085 | media_type != cricket::MEDIA_TYPE_VIDEO) { |
| 3086 | return true; |
| 3087 | } |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 3088 | |
| 3089 | std::string line_payload; |
| 3090 | std::string line_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3091 | |
| 3092 | // RFC 5576 |
| 3093 | // a=fmtp:<format> <format specific parameters> |
| 3094 | // At least two fields, whereas the second one is any of the optional |
| 3095 | // parameters. |
Donald Curtis | 144d018 | 2015-05-15 13:14:24 -0700 | [diff] [blame] | 3096 | if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace, |
| 3097 | &line_payload, &line_params)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3098 | ParseFailedExpectMinFieldNum(line, 2, error); |
| 3099 | return false; |
| 3100 | } |
| 3101 | |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 3102 | // Parse out the payload information. |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 3103 | std::string payload_type_str; |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 3104 | if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3105 | return false; |
| 3106 | } |
| 3107 | |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 3108 | int payload_type = 0; |
Donald Curtis | 144d018 | 2015-05-15 13:14:24 -0700 | [diff] [blame] | 3109 | if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type, |
| 3110 | error)) { |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 3111 | return false; |
| 3112 | } |
| 3113 | |
| 3114 | // Parse out format specific parameters. |
| 3115 | std::vector<std::string> fields; |
| 3116 | rtc::split(line_params, kSdpDelimiterSemicolon, &fields); |
| 3117 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3118 | cricket::CodecParameterMap codec_params; |
Donald Curtis | 144d018 | 2015-05-15 13:14:24 -0700 | [diff] [blame] | 3119 | for (auto& iter : fields) { |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 3120 | if (iter.find(kSdpDelimiterEqual) == std::string::npos) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3121 | // Only fmtps with equals are currently supported. Other fmtp types |
| 3122 | // should be ignored. Unknown fmtps do not constitute an error. |
| 3123 | continue; |
| 3124 | } |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 3125 | |
| 3126 | std::string name; |
| 3127 | std::string value; |
| 3128 | if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3129 | return false; |
| 3130 | } |
| 3131 | codec_params[name] = value; |
| 3132 | } |
| 3133 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3134 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 3135 | UpdateCodec<AudioContentDescription, cricket::AudioCodec>( |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 3136 | media_desc, payload_type, codec_params); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3137 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3138 | UpdateCodec<VideoContentDescription, cricket::VideoCodec>( |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 3139 | media_desc, payload_type, codec_params); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3140 | } |
| 3141 | return true; |
| 3142 | } |
| 3143 | |
| 3144 | bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type, |
| 3145 | MediaContentDescription* media_desc, |
| 3146 | SdpParseError* error) { |
| 3147 | if (media_type != cricket::MEDIA_TYPE_AUDIO && |
| 3148 | media_type != cricket::MEDIA_TYPE_VIDEO) { |
| 3149 | return true; |
| 3150 | } |
| 3151 | std::vector<std::string> rtcp_fb_fields; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 3152 | rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3153 | if (rtcp_fb_fields.size() < 2) { |
| 3154 | return ParseFailedGetValue(line, kAttributeRtcpFb, error); |
| 3155 | } |
| 3156 | std::string payload_type_string; |
| 3157 | if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string, |
| 3158 | error)) { |
| 3159 | return false; |
| 3160 | } |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 3161 | int payload_type = kWildcardPayloadType; |
| 3162 | if (payload_type_string != "*") { |
pkasting@chromium.org | e9facf8 | 2015-02-17 20:36:28 +0000 | [diff] [blame] | 3163 | if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type, |
| 3164 | error)) { |
wu@webrtc.org | 5e760e7 | 2014-04-02 23:19:09 +0000 | [diff] [blame] | 3165 | return false; |
| 3166 | } |
| 3167 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3168 | std::string id = rtcp_fb_fields[1]; |
| 3169 | std::string param = ""; |
| 3170 | for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2; |
| 3171 | iter != rtcp_fb_fields.end(); ++iter) { |
| 3172 | param.append(*iter); |
| 3173 | } |
| 3174 | const cricket::FeedbackParam feedback_param(id, param); |
| 3175 | |
| 3176 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 3177 | UpdateCodec<AudioContentDescription, cricket::AudioCodec>( |
| 3178 | media_desc, payload_type, feedback_param); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3179 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 3180 | UpdateCodec<VideoContentDescription, cricket::VideoCodec>( |
| 3181 | media_desc, payload_type, feedback_param); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3182 | } |
| 3183 | return true; |
| 3184 | } |
| 3185 | |
| 3186 | } // namespace webrtc |