blob: 23a7cdde60a469b0d56b4be43fda3a4563b3fa38 [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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000031#include "webrtc/base/stringencode.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033using rtc::scoped_ptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034using 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";
buildbot@webrtc.orgb92f6f92014-07-14 18:22:37 +000062// Used as default max video codec size before we have it in signaling.
63const int JsepSessionDescription::kMaxVideoCodecWidth = 3840;
64const int JsepSessionDescription::kMaxVideoCodecHeight = 2160;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065const int JsepSessionDescription::kDefaultVideoCodecPreference = 1;
66
67SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
68 const std::string& sdp) {
69 return CreateSessionDescription(type, sdp, NULL);
70}
71
72SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
73 const std::string& sdp,
74 SdpParseError* error) {
75 if (!IsTypeSupported(type)) {
76 return NULL;
77 }
78
79 JsepSessionDescription* jsep_desc = new JsepSessionDescription(type);
80 if (!jsep_desc->Initialize(sdp, error)) {
81 delete jsep_desc;
82 return NULL;
83 }
84 return jsep_desc;
85}
86
87JsepSessionDescription::JsepSessionDescription(const std::string& type)
88 : type_(type) {
89}
90
91JsepSessionDescription::~JsepSessionDescription() {}
92
93bool JsepSessionDescription::Initialize(
94 cricket::SessionDescription* description,
95 const std::string& session_id,
96 const std::string& session_version) {
97 if (!description)
98 return false;
99
100 session_id_ = session_id;
101 session_version_ = session_version;
102 description_.reset(description);
103 candidate_collection_.resize(number_of_mediasections());
104 return true;
105}
106
107bool JsepSessionDescription::Initialize(const std::string& sdp,
108 SdpParseError* error) {
109 return SdpDeserialize(sdp, this, error);
110}
111
112bool JsepSessionDescription::AddCandidate(
113 const IceCandidateInterface* candidate) {
114 if (!candidate || candidate->sdp_mline_index() < 0)
115 return false;
116 size_t mediasection_index = 0;
117 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
118 return false;
119 }
120 if (mediasection_index >= number_of_mediasections())
121 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 const std::string content_name =
123 description_->contents()[mediasection_index].name;
124 const cricket::TransportInfo* transport_info =
125 description_->GetTransportInfoByName(content_name);
126 if (!transport_info) {
127 return false;
128 }
129
130 cricket::Candidate updated_candidate = candidate->candidate();
131 if (updated_candidate.username().empty()) {
132 updated_candidate.set_username(transport_info->description.ice_ufrag);
133 }
134 if (updated_candidate.password().empty()) {
135 updated_candidate.set_password(transport_info->description.ice_pwd);
136 }
137
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000138 scoped_ptr<JsepIceCandidate> updated_candidate_wrapper(
139 new JsepIceCandidate(candidate->sdp_mid(),
140 static_cast<int>(mediasection_index),
141 updated_candidate));
142 if (!candidate_collection_[mediasection_index].HasCandidate(
143 updated_candidate_wrapper.get()))
144 candidate_collection_[mediasection_index].add(
145 updated_candidate_wrapper.release());
146
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 return true;
148}
149
150size_t JsepSessionDescription::number_of_mediasections() const {
151 if (!description_)
152 return 0;
153 return description_->contents().size();
154}
155
156const IceCandidateCollection* JsepSessionDescription::candidates(
157 size_t mediasection_index) const {
158 if (mediasection_index >= candidate_collection_.size())
159 return NULL;
160 return &candidate_collection_[mediasection_index];
161}
162
163bool JsepSessionDescription::ToString(std::string* out) const {
164 if (!description_ || !out)
165 return false;
166 *out = SdpSerialize(*this);
167 return !out->empty();
168}
169
170bool JsepSessionDescription::GetMediasectionIndex(
171 const IceCandidateInterface* candidate,
172 size_t* index) {
173 if (!candidate || !index) {
174 return false;
175 }
176 *index = static_cast<size_t>(candidate->sdp_mline_index());
177 if (description_ && !candidate->sdp_mid().empty()) {
178 bool found = false;
179 // Try to match the sdp_mid with content name.
180 for (size_t i = 0; i < description_->contents().size(); ++i) {
181 if (candidate->sdp_mid() == description_->contents().at(i).name) {
182 *index = i;
183 found = true;
184 break;
185 }
186 }
187 if (!found) {
188 // If the sdp_mid is presented but we can't find a match, we consider
189 // this as an error.
190 return false;
191 }
192 }
193 return true;
194}
195
196} // namespace webrtc