jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/app/webrtc/jsepsessiondescription.h" |
| 29 | |
| 30 | #include "talk/app/webrtc/webrtcsdp.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 31 | #include "talk/session/media/mediasession.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 32 | #include "webrtc/base/stringencode.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 34 | using rtc::scoped_ptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | using cricket::SessionDescription; |
| 36 | |
| 37 | namespace webrtc { |
| 38 | |
| 39 | static const char* kSupportedTypes[] = { |
| 40 | JsepSessionDescription::kOffer, |
| 41 | JsepSessionDescription::kPrAnswer, |
| 42 | JsepSessionDescription::kAnswer |
| 43 | }; |
| 44 | |
| 45 | static bool IsTypeSupported(const std::string& type) { |
| 46 | bool type_supported = false; |
| 47 | for (size_t i = 0; i < ARRAY_SIZE(kSupportedTypes); ++i) { |
| 48 | if (kSupportedTypes[i] == type) { |
| 49 | type_supported = true; |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | return type_supported; |
| 54 | } |
| 55 | |
| 56 | const char SessionDescriptionInterface::kOffer[] = "offer"; |
| 57 | const char SessionDescriptionInterface::kPrAnswer[] = "pranswer"; |
| 58 | const char SessionDescriptionInterface::kAnswer[] = "answer"; |
| 59 | |
| 60 | const int JsepSessionDescription::kDefaultVideoCodecId = 100; |
niklas.enbom@webrtc.org | 4431fd6 | 2014-08-28 14:57:46 +0000 | [diff] [blame] | 61 | // This is effectively a max value of the frame rate. 30 is default from camera. |
| 62 | const int JsepSessionDescription::kDefaultVideoCodecFramerate = 60; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8"; |
buildbot@webrtc.org | b92f6f9 | 2014-07-14 18:22:37 +0000 | [diff] [blame] | 64 | // Used as default max video codec size before we have it in signaling. |
glaznev@webrtc.org | 83af77b | 2014-09-18 21:11:29 +0000 | [diff] [blame] | 65 | #if defined(ANDROID) |
| 66 | // Limit default max video codec size for Android to avoid |
glaznev@webrtc.org | be40eb0 | 2015-01-12 17:55:47 +0000 | [diff] [blame] | 67 | // HW VP8 codec initialization failure for resolutions higher |
| 68 | // than 1280x720 or 720x1280. |
glaznev@webrtc.org | 83af77b | 2014-09-18 21:11:29 +0000 | [diff] [blame] | 69 | const int JsepSessionDescription::kMaxVideoCodecWidth = 1280; |
glaznev@webrtc.org | be40eb0 | 2015-01-12 17:55:47 +0000 | [diff] [blame] | 70 | const int JsepSessionDescription::kMaxVideoCodecHeight = 1280; |
glaznev@webrtc.org | 83af77b | 2014-09-18 21:11:29 +0000 | [diff] [blame] | 71 | #else |
glaznev@webrtc.org | 192a54f | 2014-09-12 16:40:35 +0000 | [diff] [blame] | 72 | const int JsepSessionDescription::kMaxVideoCodecWidth = 1920; |
| 73 | const int JsepSessionDescription::kMaxVideoCodecHeight = 1080; |
glaznev@webrtc.org | 83af77b | 2014-09-18 21:11:29 +0000 | [diff] [blame] | 74 | #endif |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | const int JsepSessionDescription::kDefaultVideoCodecPreference = 1; |
| 76 | |
| 77 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
| 78 | const std::string& sdp) { |
| 79 | return CreateSessionDescription(type, sdp, NULL); |
| 80 | } |
| 81 | |
| 82 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
| 83 | const std::string& sdp, |
| 84 | SdpParseError* error) { |
| 85 | if (!IsTypeSupported(type)) { |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | JsepSessionDescription* jsep_desc = new JsepSessionDescription(type); |
| 90 | if (!jsep_desc->Initialize(sdp, error)) { |
| 91 | delete jsep_desc; |
| 92 | return NULL; |
| 93 | } |
| 94 | return jsep_desc; |
| 95 | } |
| 96 | |
| 97 | JsepSessionDescription::JsepSessionDescription(const std::string& type) |
| 98 | : type_(type) { |
| 99 | } |
| 100 | |
| 101 | JsepSessionDescription::~JsepSessionDescription() {} |
| 102 | |
| 103 | bool JsepSessionDescription::Initialize( |
| 104 | cricket::SessionDescription* description, |
| 105 | const std::string& session_id, |
| 106 | const std::string& session_version) { |
| 107 | if (!description) |
| 108 | return false; |
| 109 | |
| 110 | session_id_ = session_id; |
| 111 | session_version_ = session_version; |
| 112 | description_.reset(description); |
| 113 | candidate_collection_.resize(number_of_mediasections()); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool JsepSessionDescription::Initialize(const std::string& sdp, |
| 118 | SdpParseError* error) { |
| 119 | return SdpDeserialize(sdp, this, error); |
| 120 | } |
| 121 | |
| 122 | bool JsepSessionDescription::AddCandidate( |
| 123 | const IceCandidateInterface* candidate) { |
| 124 | if (!candidate || candidate->sdp_mline_index() < 0) |
| 125 | return false; |
| 126 | size_t mediasection_index = 0; |
| 127 | if (!GetMediasectionIndex(candidate, &mediasection_index)) { |
| 128 | return false; |
| 129 | } |
| 130 | if (mediasection_index >= number_of_mediasections()) |
| 131 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | const std::string content_name = |
| 133 | description_->contents()[mediasection_index].name; |
| 134 | const cricket::TransportInfo* transport_info = |
| 135 | description_->GetTransportInfoByName(content_name); |
| 136 | if (!transport_info) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | cricket::Candidate updated_candidate = candidate->candidate(); |
| 141 | if (updated_candidate.username().empty()) { |
| 142 | updated_candidate.set_username(transport_info->description.ice_ufrag); |
| 143 | } |
| 144 | if (updated_candidate.password().empty()) { |
| 145 | updated_candidate.set_password(transport_info->description.ice_pwd); |
| 146 | } |
| 147 | |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 148 | scoped_ptr<JsepIceCandidate> updated_candidate_wrapper( |
| 149 | new JsepIceCandidate(candidate->sdp_mid(), |
| 150 | static_cast<int>(mediasection_index), |
| 151 | updated_candidate)); |
| 152 | if (!candidate_collection_[mediasection_index].HasCandidate( |
| 153 | updated_candidate_wrapper.get())) |
| 154 | candidate_collection_[mediasection_index].add( |
| 155 | updated_candidate_wrapper.release()); |
| 156 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
| 160 | size_t JsepSessionDescription::number_of_mediasections() const { |
| 161 | if (!description_) |
| 162 | return 0; |
| 163 | return description_->contents().size(); |
| 164 | } |
| 165 | |
| 166 | const IceCandidateCollection* JsepSessionDescription::candidates( |
| 167 | size_t mediasection_index) const { |
| 168 | if (mediasection_index >= candidate_collection_.size()) |
| 169 | return NULL; |
| 170 | return &candidate_collection_[mediasection_index]; |
| 171 | } |
| 172 | |
| 173 | bool JsepSessionDescription::ToString(std::string* out) const { |
| 174 | if (!description_ || !out) |
| 175 | return false; |
| 176 | *out = SdpSerialize(*this); |
| 177 | return !out->empty(); |
| 178 | } |
| 179 | |
| 180 | bool JsepSessionDescription::GetMediasectionIndex( |
| 181 | const IceCandidateInterface* candidate, |
| 182 | size_t* index) { |
| 183 | if (!candidate || !index) { |
| 184 | return false; |
| 185 | } |
| 186 | *index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 187 | if (description_ && !candidate->sdp_mid().empty()) { |
| 188 | bool found = false; |
| 189 | // Try to match the sdp_mid with content name. |
| 190 | for (size_t i = 0; i < description_->contents().size(); ++i) { |
| 191 | if (candidate->sdp_mid() == description_->contents().at(i).name) { |
| 192 | *index = i; |
| 193 | found = true; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | if (!found) { |
| 198 | // If the sdp_mid is presented but we can't find a match, we consider |
| 199 | // this as an error. |
| 200 | return false; |
| 201 | } |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | } // namespace webrtc |