blob: ee0a8e14eb093aedcf221488ff54891cf5180968 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/jsepsessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Henrik Kjellander15583c12016-02-10 10:53:12 +010013#include "webrtc/api/webrtcsdp.h"
tfarina5237aaf2015-11-10 23:44:30 -080014#include "webrtc/base/arraysize.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000015#include "webrtc/base/stringencode.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010016#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018using rtc::scoped_ptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019using cricket::SessionDescription;
20
21namespace webrtc {
22
23static const char* kSupportedTypes[] = {
24 JsepSessionDescription::kOffer,
25 JsepSessionDescription::kPrAnswer,
26 JsepSessionDescription::kAnswer
27};
28
29static bool IsTypeSupported(const std::string& type) {
30 bool type_supported = false;
tfarina5237aaf2015-11-10 23:44:30 -080031 for (size_t i = 0; i < arraysize(kSupportedTypes); ++i) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032 if (kSupportedTypes[i] == type) {
33 type_supported = true;
34 break;
35 }
36 }
37 return type_supported;
38}
39
40const char SessionDescriptionInterface::kOffer[] = "offer";
41const char SessionDescriptionInterface::kPrAnswer[] = "pranswer";
42const char SessionDescriptionInterface::kAnswer[] = "answer";
43
44const int JsepSessionDescription::kDefaultVideoCodecId = 100;
niklas.enbom@webrtc.org4431fd62014-08-28 14:57:46 +000045// This is effectively a max value of the frame rate. 30 is default from camera.
46const int JsepSessionDescription::kDefaultVideoCodecFramerate = 60;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
buildbot@webrtc.orgb92f6f92014-07-14 18:22:37 +000048// Used as default max video codec size before we have it in signaling.
Weiyong Yaob92be452015-05-19 10:53:20 +080049#if defined(ANDROID) || defined(WEBRTC_IOS)
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000050// Limit default max video codec size for Android to avoid
glaznev@webrtc.orgbe40eb02015-01-12 17:55:47 +000051// HW VP8 codec initialization failure for resolutions higher
52// than 1280x720 or 720x1280.
Weiyong Yaob92be452015-05-19 10:53:20 +080053// Same patch for iOS to support 720P in portrait mode.
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000054const int JsepSessionDescription::kMaxVideoCodecWidth = 1280;
glaznev@webrtc.orgbe40eb02015-01-12 17:55:47 +000055const int JsepSessionDescription::kMaxVideoCodecHeight = 1280;
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000056#else
glaznev@webrtc.org192a54f2014-09-12 16:40:35 +000057const int JsepSessionDescription::kMaxVideoCodecWidth = 1920;
58const int JsepSessionDescription::kMaxVideoCodecHeight = 1080;
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000059#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
61SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 const std::string& sdp,
63 SdpParseError* error) {
64 if (!IsTypeSupported(type)) {
65 return NULL;
66 }
67
68 JsepSessionDescription* jsep_desc = new JsepSessionDescription(type);
69 if (!jsep_desc->Initialize(sdp, error)) {
70 delete jsep_desc;
71 return NULL;
72 }
73 return jsep_desc;
74}
75
76JsepSessionDescription::JsepSessionDescription(const std::string& type)
77 : type_(type) {
78}
79
80JsepSessionDescription::~JsepSessionDescription() {}
81
82bool JsepSessionDescription::Initialize(
83 cricket::SessionDescription* description,
84 const std::string& session_id,
85 const std::string& session_version) {
86 if (!description)
87 return false;
88
89 session_id_ = session_id;
90 session_version_ = session_version;
91 description_.reset(description);
92 candidate_collection_.resize(number_of_mediasections());
93 return true;
94}
95
96bool JsepSessionDescription::Initialize(const std::string& sdp,
97 SdpParseError* error) {
98 return SdpDeserialize(sdp, this, error);
99}
100
101bool JsepSessionDescription::AddCandidate(
102 const IceCandidateInterface* candidate) {
103 if (!candidate || candidate->sdp_mline_index() < 0)
104 return false;
105 size_t mediasection_index = 0;
106 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
107 return false;
108 }
109 if (mediasection_index >= number_of_mediasections())
110 return false;
jbauch083b73f2015-07-16 02:46:32 -0700111 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 description_->contents()[mediasection_index].name;
113 const cricket::TransportInfo* transport_info =
114 description_->GetTransportInfoByName(content_name);
115 if (!transport_info) {
116 return false;
117 }
118
119 cricket::Candidate updated_candidate = candidate->candidate();
120 if (updated_candidate.username().empty()) {
121 updated_candidate.set_username(transport_info->description.ice_ufrag);
122 }
123 if (updated_candidate.password().empty()) {
124 updated_candidate.set_password(transport_info->description.ice_pwd);
125 }
126
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000127 scoped_ptr<JsepIceCandidate> updated_candidate_wrapper(
128 new JsepIceCandidate(candidate->sdp_mid(),
129 static_cast<int>(mediasection_index),
130 updated_candidate));
131 if (!candidate_collection_[mediasection_index].HasCandidate(
132 updated_candidate_wrapper.get()))
133 candidate_collection_[mediasection_index].add(
134 updated_candidate_wrapper.release());
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 return true;
137}
138
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700139size_t JsepSessionDescription::RemoveCandidates(
140 const std::vector<cricket::Candidate>& candidates) {
141 size_t num_removed = 0;
142 for (auto& candidate : candidates) {
143 int mediasection_index = GetMediasectionIndex(candidate);
144 if (mediasection_index < 0) {
145 // Not found.
146 continue;
147 }
148 num_removed += candidate_collection_[mediasection_index].remove(candidate);
149 }
150 return num_removed;
151}
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153size_t JsepSessionDescription::number_of_mediasections() const {
154 if (!description_)
155 return 0;
156 return description_->contents().size();
157}
158
159const IceCandidateCollection* JsepSessionDescription::candidates(
160 size_t mediasection_index) const {
161 if (mediasection_index >= candidate_collection_.size())
162 return NULL;
163 return &candidate_collection_[mediasection_index];
164}
165
166bool JsepSessionDescription::ToString(std::string* out) const {
deadbeef9d3584c2016-02-16 17:54:10 -0800167 if (!description_ || !out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 return false;
deadbeef9d3584c2016-02-16 17:54:10 -0800169 }
170 *out = SdpSerialize(*this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 return !out->empty();
172}
173
174bool JsepSessionDescription::GetMediasectionIndex(
175 const IceCandidateInterface* candidate,
176 size_t* index) {
177 if (!candidate || !index) {
178 return false;
179 }
180 *index = static_cast<size_t>(candidate->sdp_mline_index());
181 if (description_ && !candidate->sdp_mid().empty()) {
182 bool found = false;
183 // Try to match the sdp_mid with content name.
184 for (size_t i = 0; i < description_->contents().size(); ++i) {
185 if (candidate->sdp_mid() == description_->contents().at(i).name) {
186 *index = i;
187 found = true;
188 break;
189 }
190 }
191 if (!found) {
192 // If the sdp_mid is presented but we can't find a match, we consider
193 // this as an error.
194 return false;
195 }
196 }
197 return true;
198}
199
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700200int JsepSessionDescription::GetMediasectionIndex(
201 const cricket::Candidate& candidate) {
202 // Find the description with a matching transport name of the candidate.
203 const std::string& transport_name = candidate.transport_name();
204 for (size_t i = 0; i < description_->contents().size(); ++i) {
205 if (transport_name == description_->contents().at(i).name) {
206 return static_cast<int>(i);
207 }
208 }
209 return -1;
210}
211
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212} // namespace webrtc