blob: 6ea62499a62adad7c1ab30cb3a7fe3cc3c383abe [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/jsepsessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Henrik Kjellander15583c12016-02-10 10:53:12 +010013#include "webrtc/api/webrtcsdp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include "talk/session/media/mediasession.h"
tfarina5237aaf2015-11-10 23:44:30 -080015#include "webrtc/base/arraysize.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000016#include "webrtc/base/stringencode.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018using rtc::scoped_ptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019using cricket::SessionDescription;
20
21namespace webrtc {
22
23static const char* kSupportedTypes[] = {
24 JsepSessionDescription::kOffer,
25 JsepSessionDescription::kPrAnswer,
26 JsepSessionDescription::kAnswer
27};
28
29static bool IsTypeSupported(const std::string& type) {
30 bool type_supported = false;
tfarina5237aaf2015-11-10 23:44:30 -080031 for (size_t i = 0; i < arraysize(kSupportedTypes); ++i) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032 if (kSupportedTypes[i] == type) {
33 type_supported = true;
34 break;
35 }
36 }
37 return type_supported;
38}
39
40const char SessionDescriptionInterface::kOffer[] = "offer";
41const char SessionDescriptionInterface::kPrAnswer[] = "pranswer";
42const char SessionDescriptionInterface::kAnswer[] = "answer";
43
44const int JsepSessionDescription::kDefaultVideoCodecId = 100;
niklas.enbom@webrtc.org4431fd62014-08-28 14:57:46 +000045// This is effectively a max value of the frame rate. 30 is default from camera.
46const int JsepSessionDescription::kDefaultVideoCodecFramerate = 60;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
buildbot@webrtc.orgb92f6f92014-07-14 18:22:37 +000048// Used as default max video codec size before we have it in signaling.
Weiyong Yaob92be452015-05-19 10:53:20 +080049#if defined(ANDROID) || defined(WEBRTC_IOS)
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000050// Limit default max video codec size for Android to avoid
glaznev@webrtc.orgbe40eb02015-01-12 17:55:47 +000051// HW VP8 codec initialization failure for resolutions higher
52// than 1280x720 or 720x1280.
Weiyong Yaob92be452015-05-19 10:53:20 +080053// Same patch for iOS to support 720P in portrait mode.
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000054const int JsepSessionDescription::kMaxVideoCodecWidth = 1280;
glaznev@webrtc.orgbe40eb02015-01-12 17:55:47 +000055const int JsepSessionDescription::kMaxVideoCodecHeight = 1280;
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000056#else
glaznev@webrtc.org192a54f2014-09-12 16:40:35 +000057const int JsepSessionDescription::kMaxVideoCodecWidth = 1920;
58const int JsepSessionDescription::kMaxVideoCodecHeight = 1080;
glaznev@webrtc.org83af77b2014-09-18 21:11:29 +000059#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060const int JsepSessionDescription::kDefaultVideoCodecPreference = 1;
61
62SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 const std::string& sdp,
64 SdpParseError* error) {
65 if (!IsTypeSupported(type)) {
66 return NULL;
67 }
68
69 JsepSessionDescription* jsep_desc = new JsepSessionDescription(type);
70 if (!jsep_desc->Initialize(sdp, error)) {
71 delete jsep_desc;
72 return NULL;
73 }
74 return jsep_desc;
75}
76
77JsepSessionDescription::JsepSessionDescription(const std::string& type)
78 : type_(type) {
79}
80
81JsepSessionDescription::~JsepSessionDescription() {}
82
83bool JsepSessionDescription::Initialize(
84 cricket::SessionDescription* description,
85 const std::string& session_id,
86 const std::string& session_version) {
87 if (!description)
88 return false;
89
90 session_id_ = session_id;
91 session_version_ = session_version;
92 description_.reset(description);
93 candidate_collection_.resize(number_of_mediasections());
94 return true;
95}
96
97bool JsepSessionDescription::Initialize(const std::string& sdp,
98 SdpParseError* error) {
99 return SdpDeserialize(sdp, this, error);
100}
101
102bool JsepSessionDescription::AddCandidate(
103 const IceCandidateInterface* candidate) {
104 if (!candidate || candidate->sdp_mline_index() < 0)
105 return false;
106 size_t mediasection_index = 0;
107 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
108 return false;
109 }
110 if (mediasection_index >= number_of_mediasections())
111 return false;
jbauch083b73f2015-07-16 02:46:32 -0700112 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 description_->contents()[mediasection_index].name;
114 const cricket::TransportInfo* transport_info =
115 description_->GetTransportInfoByName(content_name);
116 if (!transport_info) {
117 return false;
118 }
119
120 cricket::Candidate updated_candidate = candidate->candidate();
121 if (updated_candidate.username().empty()) {
122 updated_candidate.set_username(transport_info->description.ice_ufrag);
123 }
124 if (updated_candidate.password().empty()) {
125 updated_candidate.set_password(transport_info->description.ice_pwd);
126 }
127
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000128 scoped_ptr<JsepIceCandidate> updated_candidate_wrapper(
129 new JsepIceCandidate(candidate->sdp_mid(),
130 static_cast<int>(mediasection_index),
131 updated_candidate));
132 if (!candidate_collection_[mediasection_index].HasCandidate(
133 updated_candidate_wrapper.get()))
134 candidate_collection_[mediasection_index].add(
135 updated_candidate_wrapper.release());
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 return true;
138}
139
140size_t JsepSessionDescription::number_of_mediasections() const {
141 if (!description_)
142 return 0;
143 return description_->contents().size();
144}
145
146const IceCandidateCollection* JsepSessionDescription::candidates(
147 size_t mediasection_index) const {
148 if (mediasection_index >= candidate_collection_.size())
149 return NULL;
150 return &candidate_collection_[mediasection_index];
151}
152
153bool JsepSessionDescription::ToString(std::string* out) const {
154 if (!description_ || !out)
155 return false;
156 *out = SdpSerialize(*this);
157 return !out->empty();
158}
159
160bool JsepSessionDescription::GetMediasectionIndex(
161 const IceCandidateInterface* candidate,
162 size_t* index) {
163 if (!candidate || !index) {
164 return false;
165 }
166 *index = static_cast<size_t>(candidate->sdp_mline_index());
167 if (description_ && !candidate->sdp_mid().empty()) {
168 bool found = false;
169 // Try to match the sdp_mid with content name.
170 for (size_t i = 0; i < description_->contents().size(); ++i) {
171 if (candidate->sdp_mid() == description_->contents().at(i).name) {
172 *index = i;
173 found = true;
174 break;
175 }
176 }
177 if (!found) {
178 // If the sdp_mid is presented but we can't find a match, we consider
179 // this as an error.
180 return false;
181 }
182 }
183 return true;
184}
185
186} // namespace webrtc