blob: 697f332c27c64274951ba4255273c97568dcf09b [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
2 * libjingle
3 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/jsepsessiondescription.h"
29
30#include "talk/app/webrtc/webrtcsdp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000032#include "webrtc/base/stringencode.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034using rtc::scoped_ptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035using cricket::SessionDescription;
36
37namespace webrtc {
38
39static const char* kSupportedTypes[] = {
40 JsepSessionDescription::kOffer,
41 JsepSessionDescription::kPrAnswer,
42 JsepSessionDescription::kAnswer
43};
44
45static bool IsTypeSupported(const std::string& type) {
46 bool type_supported = false;
47 for (size_t i = 0; i < ARRAY_SIZE(kSupportedTypes); ++i) {
48 if (kSupportedTypes[i] == type) {
49 type_supported = true;
50 break;
51 }
52 }
53 return type_supported;
54}
55
56const char SessionDescriptionInterface::kOffer[] = "offer";
57const char SessionDescriptionInterface::kPrAnswer[] = "pranswer";
58const char SessionDescriptionInterface::kAnswer[] = "answer";
59
60const int JsepSessionDescription::kDefaultVideoCodecId = 100;
niklas.enbom@webrtc.org4431fd62014-08-28 14:57:46 +000061// This is effectively a max value of the frame rate. 30 is default from camera.
62const int JsepSessionDescription::kDefaultVideoCodecFramerate = 60;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
buildbot@webrtc.orgb92f6f92014-07-14 18:22:37 +000064// Used as default max video codec size before we have it in signaling.
Weiyong Yaob92be452015-05-19 10:53:20 +080065#if defined(ANDROID) || defined(WEBRTC_IOS)
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000066// Limit default max video codec size for Android to avoid
glaznev@webrtc.orgbe40eb02015-01-12 17:55:47 +000067// HW VP8 codec initialization failure for resolutions higher
68// than 1280x720 or 720x1280.
Weiyong Yaob92be452015-05-19 10:53:20 +080069// Same patch for iOS to support 720P in portrait mode.
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000070const int JsepSessionDescription::kMaxVideoCodecWidth = 1280;
glaznev@webrtc.orgbe40eb02015-01-12 17:55:47 +000071const int JsepSessionDescription::kMaxVideoCodecHeight = 1280;
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000072#else
glaznev@webrtc.org192a54f2014-09-12 16:40:35 +000073const int JsepSessionDescription::kMaxVideoCodecWidth = 1920;
74const int JsepSessionDescription::kMaxVideoCodecHeight = 1080;
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000075#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076const int JsepSessionDescription::kDefaultVideoCodecPreference = 1;
77
78SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
79 const std::string& sdp) {
80 return CreateSessionDescription(type, sdp, NULL);
81}
82
83SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
84 const std::string& sdp,
85 SdpParseError* error) {
86 if (!IsTypeSupported(type)) {
87 return NULL;
88 }
89
90 JsepSessionDescription* jsep_desc = new JsepSessionDescription(type);
91 if (!jsep_desc->Initialize(sdp, error)) {
92 delete jsep_desc;
93 return NULL;
94 }
95 return jsep_desc;
96}
97
98JsepSessionDescription::JsepSessionDescription(const std::string& type)
99 : type_(type) {
100}
101
102JsepSessionDescription::~JsepSessionDescription() {}
103
104bool JsepSessionDescription::Initialize(
105 cricket::SessionDescription* description,
106 const std::string& session_id,
107 const std::string& session_version) {
108 if (!description)
109 return false;
110
111 session_id_ = session_id;
112 session_version_ = session_version;
113 description_.reset(description);
114 candidate_collection_.resize(number_of_mediasections());
115 return true;
116}
117
118bool JsepSessionDescription::Initialize(const std::string& sdp,
119 SdpParseError* error) {
120 return SdpDeserialize(sdp, this, error);
121}
122
123bool JsepSessionDescription::AddCandidate(
124 const IceCandidateInterface* candidate) {
125 if (!candidate || candidate->sdp_mline_index() < 0)
126 return false;
127 size_t mediasection_index = 0;
128 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
129 return false;
130 }
131 if (mediasection_index >= number_of_mediasections())
132 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 const std::string content_name =
134 description_->contents()[mediasection_index].name;
135 const cricket::TransportInfo* transport_info =
136 description_->GetTransportInfoByName(content_name);
137 if (!transport_info) {
138 return false;
139 }
140
141 cricket::Candidate updated_candidate = candidate->candidate();
142 if (updated_candidate.username().empty()) {
143 updated_candidate.set_username(transport_info->description.ice_ufrag);
144 }
145 if (updated_candidate.password().empty()) {
146 updated_candidate.set_password(transport_info->description.ice_pwd);
147 }
148
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000149 scoped_ptr<JsepIceCandidate> updated_candidate_wrapper(
150 new JsepIceCandidate(candidate->sdp_mid(),
151 static_cast<int>(mediasection_index),
152 updated_candidate));
153 if (!candidate_collection_[mediasection_index].HasCandidate(
154 updated_candidate_wrapper.get()))
155 candidate_collection_[mediasection_index].add(
156 updated_candidate_wrapper.release());
157
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 return true;
159}
160
161size_t JsepSessionDescription::number_of_mediasections() const {
162 if (!description_)
163 return 0;
164 return description_->contents().size();
165}
166
167const IceCandidateCollection* JsepSessionDescription::candidates(
168 size_t mediasection_index) const {
169 if (mediasection_index >= candidate_collection_.size())
170 return NULL;
171 return &candidate_collection_[mediasection_index];
172}
173
174bool JsepSessionDescription::ToString(std::string* out) const {
175 if (!description_ || !out)
176 return false;
177 *out = SdpSerialize(*this);
178 return !out->empty();
179}
180
181bool JsepSessionDescription::GetMediasectionIndex(
182 const IceCandidateInterface* candidate,
183 size_t* index) {
184 if (!candidate || !index) {
185 return false;
186 }
187 *index = static_cast<size_t>(candidate->sdp_mline_index());
188 if (description_ && !candidate->sdp_mid().empty()) {
189 bool found = false;
190 // Try to match the sdp_mid with content name.
191 for (size_t i = 0; i < description_->contents().size(); ++i) {
192 if (candidate->sdp_mid() == description_->contents().at(i).name) {
193 *index = i;
194 found = true;
195 break;
196 }
197 }
198 if (!found) {
199 // If the sdp_mid is presented but we can't find a match, we consider
200 // this as an error.
201 return false;
202 }
203 }
204 return true;
205}
206
207} // namespace webrtc