blob: 60467ba620ab9a9d32497915c324f77c0681a33e [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.
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000065#if defined(ANDROID)
66// 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.
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000069const int JsepSessionDescription::kMaxVideoCodecWidth = 1280;
glaznev@webrtc.orgbe40eb02015-01-12 17:55:47 +000070const int JsepSessionDescription::kMaxVideoCodecHeight = 1280;
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000071#else
glaznev@webrtc.org192a54f2014-09-12 16:40:35 +000072const int JsepSessionDescription::kMaxVideoCodecWidth = 1920;
73const int JsepSessionDescription::kMaxVideoCodecHeight = 1080;
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000074#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const int JsepSessionDescription::kDefaultVideoCodecPreference = 1;
76
77SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
78 const std::string& sdp) {
79 return CreateSessionDescription(type, sdp, NULL);
80}
81
82SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
83 const std::string& sdp,
84 SdpParseError* error) {
85 if (!IsTypeSupported(type)) {
86 return NULL;
87 }
88
89 JsepSessionDescription* jsep_desc = new JsepSessionDescription(type);
90 if (!jsep_desc->Initialize(sdp, error)) {
91 delete jsep_desc;
92 return NULL;
93 }
94 return jsep_desc;
95}
96
97JsepSessionDescription::JsepSessionDescription(const std::string& type)
98 : type_(type) {
99}
100
101JsepSessionDescription::~JsepSessionDescription() {}
102
103bool JsepSessionDescription::Initialize(
104 cricket::SessionDescription* description,
105 const std::string& session_id,
106 const std::string& session_version) {
107 if (!description)
108 return false;
109
110 session_id_ = session_id;
111 session_version_ = session_version;
112 description_.reset(description);
113 candidate_collection_.resize(number_of_mediasections());
114 return true;
115}
116
117bool JsepSessionDescription::Initialize(const std::string& sdp,
118 SdpParseError* error) {
119 return SdpDeserialize(sdp, this, error);
120}
121
122bool JsepSessionDescription::AddCandidate(
123 const IceCandidateInterface* candidate) {
124 if (!candidate || candidate->sdp_mline_index() < 0)
125 return false;
126 size_t mediasection_index = 0;
127 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
128 return false;
129 }
130 if (mediasection_index >= number_of_mediasections())
131 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 const std::string content_name =
133 description_->contents()[mediasection_index].name;
134 const cricket::TransportInfo* transport_info =
135 description_->GetTransportInfoByName(content_name);
136 if (!transport_info) {
137 return false;
138 }
139
140 cricket::Candidate updated_candidate = candidate->candidate();
141 if (updated_candidate.username().empty()) {
142 updated_candidate.set_username(transport_info->description.ice_ufrag);
143 }
144 if (updated_candidate.password().empty()) {
145 updated_candidate.set_password(transport_info->description.ice_pwd);
146 }
147
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000148 scoped_ptr<JsepIceCandidate> updated_candidate_wrapper(
149 new JsepIceCandidate(candidate->sdp_mid(),
150 static_cast<int>(mediasection_index),
151 updated_candidate));
152 if (!candidate_collection_[mediasection_index].HasCandidate(
153 updated_candidate_wrapper.get()))
154 candidate_collection_[mediasection_index].add(
155 updated_candidate_wrapper.release());
156
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 return true;
158}
159
160size_t JsepSessionDescription::number_of_mediasections() const {
161 if (!description_)
162 return 0;
163 return description_->contents().size();
164}
165
166const IceCandidateCollection* JsepSessionDescription::candidates(
167 size_t mediasection_index) const {
168 if (mediasection_index >= candidate_collection_.size())
169 return NULL;
170 return &candidate_collection_[mediasection_index];
171}
172
173bool JsepSessionDescription::ToString(std::string* out) const {
174 if (!description_ || !out)
175 return false;
176 *out = SdpSerialize(*this);
177 return !out->empty();
178}
179
180bool JsepSessionDescription::GetMediasectionIndex(
181 const IceCandidateInterface* candidate,
182 size_t* index) {
183 if (!candidate || !index) {
184 return false;
185 }
186 *index = static_cast<size_t>(candidate->sdp_mline_index());
187 if (description_ && !candidate->sdp_mid().empty()) {
188 bool found = false;
189 // Try to match the sdp_mid with content name.
190 for (size_t i = 0; i < description_->contents().size(); ++i) {
191 if (candidate->sdp_mid() == description_->contents().at(i).name) {
192 *index = i;
193 found = true;
194 break;
195 }
196 }
197 if (!found) {
198 // If the sdp_mid is presented but we can't find a match, we consider
199 // this as an error.
200 return false;
201 }
202 }
203 return true;
204}
205
206} // namespace webrtc