2 * Copyright 2012, Google Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
27 #include "talk/app/webrtc/jsepsessiondescription.h"
29 #include "talk/app/webrtc/webrtcsdp.h"
30 #include "talk/base/stringencode.h"
31 #include "talk/session/media/mediasession.h"
33 using talk_base::scoped_ptr;
34 using cricket::SessionDescription;
38 static const char* kSupportedTypes[] = {
39 JsepSessionDescription::kOffer,
40 JsepSessionDescription::kPrAnswer,
41 JsepSessionDescription::kAnswer
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;
52 return type_supported;
55 const char SessionDescriptionInterface::kOffer[] = "offer";
56 const char SessionDescriptionInterface::kPrAnswer[] = "pranswer";
57 const char SessionDescriptionInterface::kAnswer[] = "answer";
59 const int JsepSessionDescription::kDefaultVideoCodecId = 100;
60 const int JsepSessionDescription::kDefaultVideoCodecFramerate = 30;
61 const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
62 const int JsepSessionDescription::kMaxVideoCodecWidth = 1280;
63 const int JsepSessionDescription::kMaxVideoCodecHeight = 720;
64 const int JsepSessionDescription::kDefaultVideoCodecPreference = 1;
66 SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
67 const std::string& sdp) {
68 return CreateSessionDescription(type, sdp, NULL);
71 SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
72 const std::string& sdp,
73 SdpParseError* error) {
74 if (!IsTypeSupported(type)) {
78 JsepSessionDescription* jsep_desc = new JsepSessionDescription(type);
79 if (!jsep_desc->Initialize(sdp, error)) {
86 JsepSessionDescription::JsepSessionDescription(const std::string& type)
90 JsepSessionDescription::~JsepSessionDescription() {}
92 bool JsepSessionDescription::Initialize(
93 cricket::SessionDescription* description,
94 const std::string& session_id,
95 const std::string& session_version) {
99 session_id_ = session_id;
100 session_version_ = session_version;
101 description_.reset(description);
102 candidate_collection_.resize(number_of_mediasections());
106 bool JsepSessionDescription::Initialize(const std::string& sdp,
107 SdpParseError* error) {
108 return SdpDeserialize(sdp, this, error);
111 bool JsepSessionDescription::AddCandidate(
112 const IceCandidateInterface* candidate) {
113 if (!candidate || candidate->sdp_mline_index() < 0)
115 size_t mediasection_index = 0;
116 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
119 if (mediasection_index >= number_of_mediasections())
121 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) {
129 cricket::Candidate updated_candidate = candidate->candidate();
130 if (updated_candidate.username().empty()) {
131 updated_candidate.set_username(transport_info->description.ice_ufrag);
133 if (updated_candidate.password().empty()) {
134 updated_candidate.set_password(transport_info->description.ice_pwd);
137 scoped_ptr<JsepIceCandidate> updated_candidate_wrapper(
138 new JsepIceCandidate(candidate->sdp_mid(),
139 static_cast<int>(mediasection_index),
141 if (!candidate_collection_[mediasection_index].HasCandidate(
142 updated_candidate_wrapper.get()))
143 candidate_collection_[mediasection_index].add(
144 updated_candidate_wrapper.release());
149 size_t JsepSessionDescription::number_of_mediasections() const {
152 return description_->contents().size();
155 const IceCandidateCollection* JsepSessionDescription::candidates(
156 size_t mediasection_index) const {
157 if (mediasection_index >= candidate_collection_.size())
159 return &candidate_collection_[mediasection_index];
162 bool JsepSessionDescription::ToString(std::string* out) const {
163 if (!description_ || !out)
165 *out = SdpSerialize(*this);
166 return !out->empty();
169 bool JsepSessionDescription::GetMediasectionIndex(
170 const IceCandidateInterface* candidate,
172 if (!candidate || !index) {
175 *index = static_cast<size_t>(candidate->sdp_mline_index());
176 if (description_ && !candidate->sdp_mid().empty()) {
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) {
187 // If the sdp_mid is presented but we can't find a match, we consider
195 } // namespace webrtc