blob: d7b37b5d761f746e225de9436573e5dc3c4543d3 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/* 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/base/stringencode.h"
31#include "talk/session/media/mediasession.h"
32
33using talk_base::scoped_ptr;
34using cricket::SessionDescription;
35
36namespace webrtc {
37
38static const char* kSupportedTypes[] = {
39 JsepSessionDescription::kOffer,
40 JsepSessionDescription::kPrAnswer,
41 JsepSessionDescription::kAnswer
42};
43
44static 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
55const char SessionDescriptionInterface::kOffer[] = "offer";
56const char SessionDescriptionInterface::kPrAnswer[] = "pranswer";
57const char SessionDescriptionInterface::kAnswer[] = "answer";
58
59const int JsepSessionDescription::kDefaultVideoCodecId = 100;
60const int JsepSessionDescription::kDefaultVideoCodecFramerate = 30;
61const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
62const int JsepSessionDescription::kMaxVideoCodecWidth = 1280;
63const int JsepSessionDescription::kMaxVideoCodecHeight = 720;
64const int JsepSessionDescription::kDefaultVideoCodecPreference = 1;
65
66SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
67 const std::string& sdp) {
68 return CreateSessionDescription(type, sdp, NULL);
69}
70
71SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
72 const std::string& sdp,
73 SdpParseError* error) {
74 if (!IsTypeSupported(type)) {
75 return NULL;
76 }
77
78 JsepSessionDescription* jsep_desc = new JsepSessionDescription(type);
79 if (!jsep_desc->Initialize(sdp, error)) {
80 delete jsep_desc;
81 return NULL;
82 }
83 return jsep_desc;
84}
85
86JsepSessionDescription::JsepSessionDescription(const std::string& type)
87 : type_(type) {
88}
89
90JsepSessionDescription::~JsepSessionDescription() {}
91
92bool JsepSessionDescription::Initialize(
93 cricket::SessionDescription* description,
94 const std::string& session_id,
95 const std::string& session_version) {
96 if (!description)
97 return false;
98
99 session_id_ = session_id;
100 session_version_ = session_version;
101 description_.reset(description);
102 candidate_collection_.resize(number_of_mediasections());
103 return true;
104}
105
106bool JsepSessionDescription::Initialize(const std::string& sdp,
107 SdpParseError* error) {
108 return SdpDeserialize(sdp, this, error);
109}
110
111bool JsepSessionDescription::AddCandidate(
112 const IceCandidateInterface* candidate) {
113 if (!candidate || candidate->sdp_mline_index() < 0)
114 return false;
115 size_t mediasection_index = 0;
116 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
117 return false;
118 }
119 if (mediasection_index >= number_of_mediasections())
120 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 const std::string content_name =
122 description_->contents()[mediasection_index].name;
123 const cricket::TransportInfo* transport_info =
124 description_->GetTransportInfoByName(content_name);
125 if (!transport_info) {
126 return false;
127 }
128
129 cricket::Candidate updated_candidate = candidate->candidate();
130 if (updated_candidate.username().empty()) {
131 updated_candidate.set_username(transport_info->description.ice_ufrag);
132 }
133 if (updated_candidate.password().empty()) {
134 updated_candidate.set_password(transport_info->description.ice_pwd);
135 }
136
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000137 scoped_ptr<JsepIceCandidate> updated_candidate_wrapper(
138 new JsepIceCandidate(candidate->sdp_mid(),
139 static_cast<int>(mediasection_index),
140 updated_candidate));
141 if (!candidate_collection_[mediasection_index].HasCandidate(
142 updated_candidate_wrapper.get()))
143 candidate_collection_[mediasection_index].add(
144 updated_candidate_wrapper.release());
145
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 return true;
147}
148
149size_t JsepSessionDescription::number_of_mediasections() const {
150 if (!description_)
151 return 0;
152 return description_->contents().size();
153}
154
155const IceCandidateCollection* JsepSessionDescription::candidates(
156 size_t mediasection_index) const {
157 if (mediasection_index >= candidate_collection_.size())
158 return NULL;
159 return &candidate_collection_[mediasection_index];
160}
161
162bool JsepSessionDescription::ToString(std::string* out) const {
163 if (!description_ || !out)
164 return false;
165 *out = SdpSerialize(*this);
166 return !out->empty();
167}
168
169bool JsepSessionDescription::GetMediasectionIndex(
170 const IceCandidateInterface* candidate,
171 size_t* index) {
172 if (!candidate || !index) {
173 return false;
174 }
175 *index = static_cast<size_t>(candidate->sdp_mline_index());
176 if (description_ && !candidate->sdp_mid().empty()) {
177 bool found = false;
178 // Try to match the sdp_mid with content name.
179 for (size_t i = 0; i < description_->contents().size(); ++i) {
180 if (candidate->sdp_mid() == description_->contents().at(i).name) {
181 *index = i;
182 found = true;
183 break;
184 }
185 }
186 if (!found) {
187 // If the sdp_mid is presented but we can't find a match, we consider
188 // this as an error.
189 return false;
190 }
191 }
192 return true;
193}
194
195} // namespace webrtc