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/session/media/mediasession.h"
31 #include "webrtc/base/stringencode.h"
33 using rtc::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 // 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.
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;
70 const int JsepSessionDescription::kMaxVideoCodecWidth = 1920;
71 const int JsepSessionDescription::kMaxVideoCodecHeight = 1080;
73 const int JsepSessionDescription::kDefaultVideoCodecPreference = 1;
75 SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
76 const std::string& sdp) {
77 return CreateSessionDescription(type, sdp, NULL);
80 SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
81 const std::string& sdp,
82 SdpParseError* error) {
83 if (!IsTypeSupported(type)) {
87 JsepSessionDescription* jsep_desc = new JsepSessionDescription(type);
88 if (!jsep_desc->Initialize(sdp, error)) {
95 JsepSessionDescription::JsepSessionDescription(const std::string& type)
99 JsepSessionDescription::~JsepSessionDescription() {}
101 bool JsepSessionDescription::Initialize(
102 cricket::SessionDescription* description,
103 const std::string& session_id,
104 const std::string& session_version) {
108 session_id_ = session_id;
109 session_version_ = session_version;
110 description_.reset(description);
111 candidate_collection_.resize(number_of_mediasections());
115 bool JsepSessionDescription::Initialize(const std::string& sdp,
116 SdpParseError* error) {
117 return SdpDeserialize(sdp, this, error);
120 bool JsepSessionDescription::AddCandidate(
121 const IceCandidateInterface* candidate) {
122 if (!candidate || candidate->sdp_mline_index() < 0)
124 size_t mediasection_index = 0;
125 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
128 if (mediasection_index >= number_of_mediasections())
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) {
138 cricket::Candidate updated_candidate = candidate->candidate();
139 if (updated_candidate.username().empty()) {
140 updated_candidate.set_username(transport_info->description.ice_ufrag);
142 if (updated_candidate.password().empty()) {
143 updated_candidate.set_password(transport_info->description.ice_pwd);
146 scoped_ptr<JsepIceCandidate> updated_candidate_wrapper(
147 new JsepIceCandidate(candidate->sdp_mid(),
148 static_cast<int>(mediasection_index),
150 if (!candidate_collection_[mediasection_index].HasCandidate(
151 updated_candidate_wrapper.get()))
152 candidate_collection_[mediasection_index].add(
153 updated_candidate_wrapper.release());
158 size_t JsepSessionDescription::number_of_mediasections() const {
161 return description_->contents().size();
164 const IceCandidateCollection* JsepSessionDescription::candidates(
165 size_t mediasection_index) const {
166 if (mediasection_index >= candidate_collection_.size())
168 return &candidate_collection_[mediasection_index];
171 bool JsepSessionDescription::ToString(std::string* out) const {
172 if (!description_ || !out)
174 *out = SdpSerialize(*this);
175 return !out->empty();
178 bool JsepSessionDescription::GetMediasectionIndex(
179 const IceCandidateInterface* candidate,
181 if (!candidate || !index) {
184 *index = static_cast<size_t>(candidate->sdp_mline_index());
185 if (description_ && !candidate->sdp_mid().empty()) {
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) {
196 // If the sdp_mid is presented but we can't find a match, we consider
204 } // namespace webrtc