jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #include "webrtc/api/jsepsessiondescription.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 13 | #include "webrtc/api/webrtcsdp.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | #include "talk/session/media/mediasession.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 15 | #include "webrtc/base/arraysize.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 16 | #include "webrtc/base/stringencode.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 18 | using rtc::scoped_ptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | using cricket::SessionDescription; |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | static const char* kSupportedTypes[] = { |
| 24 | JsepSessionDescription::kOffer, |
| 25 | JsepSessionDescription::kPrAnswer, |
| 26 | JsepSessionDescription::kAnswer |
| 27 | }; |
| 28 | |
| 29 | static bool IsTypeSupported(const std::string& type) { |
| 30 | bool type_supported = false; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 31 | for (size_t i = 0; i < arraysize(kSupportedTypes); ++i) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | if (kSupportedTypes[i] == type) { |
| 33 | type_supported = true; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | return type_supported; |
| 38 | } |
| 39 | |
| 40 | const char SessionDescriptionInterface::kOffer[] = "offer"; |
| 41 | const char SessionDescriptionInterface::kPrAnswer[] = "pranswer"; |
| 42 | const char SessionDescriptionInterface::kAnswer[] = "answer"; |
| 43 | |
| 44 | const int JsepSessionDescription::kDefaultVideoCodecId = 100; |
niklas.enbom@webrtc.org | 4431fd6 | 2014-08-28 14:57:46 +0000 | [diff] [blame] | 45 | // This is effectively a max value of the frame rate. 30 is default from camera. |
| 46 | const int JsepSessionDescription::kDefaultVideoCodecFramerate = 60; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8"; |
buildbot@webrtc.org | b92f6f9 | 2014-07-14 18:22:37 +0000 | [diff] [blame] | 48 | // Used as default max video codec size before we have it in signaling. |
Weiyong Yao | b92be45 | 2015-05-19 10:53:20 +0800 | [diff] [blame] | 49 | #if defined(ANDROID) || defined(WEBRTC_IOS) |
glaznev@webrtc.org | 83af77b | 2014-09-18 21:11:29 +0000 | [diff] [blame] | 50 | // Limit default max video codec size for Android to avoid |
glaznev@webrtc.org | be40eb0 | 2015-01-12 17:55:47 +0000 | [diff] [blame] | 51 | // HW VP8 codec initialization failure for resolutions higher |
| 52 | // than 1280x720 or 720x1280. |
Weiyong Yao | b92be45 | 2015-05-19 10:53:20 +0800 | [diff] [blame] | 53 | // Same patch for iOS to support 720P in portrait mode. |
glaznev@webrtc.org | 83af77b | 2014-09-18 21:11:29 +0000 | [diff] [blame] | 54 | const int JsepSessionDescription::kMaxVideoCodecWidth = 1280; |
glaznev@webrtc.org | be40eb0 | 2015-01-12 17:55:47 +0000 | [diff] [blame] | 55 | const int JsepSessionDescription::kMaxVideoCodecHeight = 1280; |
glaznev@webrtc.org | 83af77b | 2014-09-18 21:11:29 +0000 | [diff] [blame] | 56 | #else |
glaznev@webrtc.org | 192a54f | 2014-09-12 16:40:35 +0000 | [diff] [blame] | 57 | const int JsepSessionDescription::kMaxVideoCodecWidth = 1920; |
| 58 | const int JsepSessionDescription::kMaxVideoCodecHeight = 1080; |
glaznev@webrtc.org | 83af77b | 2014-09-18 21:11:29 +0000 | [diff] [blame] | 59 | #endif |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 60 | const int JsepSessionDescription::kDefaultVideoCodecPreference = 1; |
| 61 | |
| 62 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | 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 | |
| 77 | JsepSessionDescription::JsepSessionDescription(const std::string& type) |
| 78 | : type_(type) { |
| 79 | } |
| 80 | |
| 81 | JsepSessionDescription::~JsepSessionDescription() {} |
| 82 | |
| 83 | bool 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 | |
| 97 | bool JsepSessionDescription::Initialize(const std::string& sdp, |
| 98 | SdpParseError* error) { |
| 99 | return SdpDeserialize(sdp, this, error); |
| 100 | } |
| 101 | |
| 102 | bool 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; |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 112 | const std::string& content_name = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | 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.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 128 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
| 140 | size_t JsepSessionDescription::number_of_mediasections() const { |
| 141 | if (!description_) |
| 142 | return 0; |
| 143 | return description_->contents().size(); |
| 144 | } |
| 145 | |
| 146 | const 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 | |
| 153 | bool JsepSessionDescription::ToString(std::string* out) const { |
| 154 | if (!description_ || !out) |
| 155 | return false; |
| 156 | *out = SdpSerialize(*this); |
| 157 | return !out->empty(); |
| 158 | } |
| 159 | |
| 160 | bool 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 |