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