henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* libjingle |
| 2 | * Copyright 2012, Google Inc. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 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. |
| 14 | * |
| 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. |
| 25 | */ |
| 26 | |
| 27 | #include "talk/app/webrtc/jsepsessiondescription.h" |
| 28 | |
| 29 | #include "talk/app/webrtc/webrtcsdp.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | #include "talk/session/media/mediasession.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame^] | 31 | #include "webrtc/base/stringencode.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 33 | using rtc::scoped_ptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | using cricket::SessionDescription; |
| 35 | |
| 36 | namespace webrtc { |
| 37 | |
| 38 | static const char* kSupportedTypes[] = { |
| 39 | JsepSessionDescription::kOffer, |
| 40 | JsepSessionDescription::kPrAnswer, |
| 41 | JsepSessionDescription::kAnswer |
| 42 | }; |
| 43 | |
| 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; |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | return type_supported; |
| 53 | } |
| 54 | |
| 55 | const char SessionDescriptionInterface::kOffer[] = "offer"; |
| 56 | const char SessionDescriptionInterface::kPrAnswer[] = "pranswer"; |
| 57 | const char SessionDescriptionInterface::kAnswer[] = "answer"; |
| 58 | |
| 59 | const int JsepSessionDescription::kDefaultVideoCodecId = 100; |
| 60 | const int JsepSessionDescription::kDefaultVideoCodecFramerate = 30; |
| 61 | const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8"; |
buildbot@webrtc.org | b92f6f9 | 2014-07-14 18:22:37 +0000 | [diff] [blame] | 62 | // Used as default max video codec size before we have it in signaling. |
| 63 | const int JsepSessionDescription::kMaxVideoCodecWidth = 3840; |
| 64 | const int JsepSessionDescription::kMaxVideoCodecHeight = 2160; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | const int JsepSessionDescription::kDefaultVideoCodecPreference = 1; |
| 66 | |
| 67 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
| 68 | const std::string& sdp) { |
| 69 | return CreateSessionDescription(type, sdp, NULL); |
| 70 | } |
| 71 | |
| 72 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
| 73 | const std::string& sdp, |
| 74 | SdpParseError* error) { |
| 75 | if (!IsTypeSupported(type)) { |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | JsepSessionDescription* jsep_desc = new JsepSessionDescription(type); |
| 80 | if (!jsep_desc->Initialize(sdp, error)) { |
| 81 | delete jsep_desc; |
| 82 | return NULL; |
| 83 | } |
| 84 | return jsep_desc; |
| 85 | } |
| 86 | |
| 87 | JsepSessionDescription::JsepSessionDescription(const std::string& type) |
| 88 | : type_(type) { |
| 89 | } |
| 90 | |
| 91 | JsepSessionDescription::~JsepSessionDescription() {} |
| 92 | |
| 93 | bool JsepSessionDescription::Initialize( |
| 94 | cricket::SessionDescription* description, |
| 95 | const std::string& session_id, |
| 96 | const std::string& session_version) { |
| 97 | if (!description) |
| 98 | return false; |
| 99 | |
| 100 | session_id_ = session_id; |
| 101 | session_version_ = session_version; |
| 102 | description_.reset(description); |
| 103 | candidate_collection_.resize(number_of_mediasections()); |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool JsepSessionDescription::Initialize(const std::string& sdp, |
| 108 | SdpParseError* error) { |
| 109 | return SdpDeserialize(sdp, this, error); |
| 110 | } |
| 111 | |
| 112 | bool JsepSessionDescription::AddCandidate( |
| 113 | const IceCandidateInterface* candidate) { |
| 114 | if (!candidate || candidate->sdp_mline_index() < 0) |
| 115 | return false; |
| 116 | size_t mediasection_index = 0; |
| 117 | if (!GetMediasectionIndex(candidate, &mediasection_index)) { |
| 118 | return false; |
| 119 | } |
| 120 | if (mediasection_index >= number_of_mediasections()) |
| 121 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | const std::string content_name = |
| 123 | description_->contents()[mediasection_index].name; |
| 124 | const cricket::TransportInfo* transport_info = |
| 125 | description_->GetTransportInfoByName(content_name); |
| 126 | if (!transport_info) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | cricket::Candidate updated_candidate = candidate->candidate(); |
| 131 | if (updated_candidate.username().empty()) { |
| 132 | updated_candidate.set_username(transport_info->description.ice_ufrag); |
| 133 | } |
| 134 | if (updated_candidate.password().empty()) { |
| 135 | updated_candidate.set_password(transport_info->description.ice_pwd); |
| 136 | } |
| 137 | |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 138 | scoped_ptr<JsepIceCandidate> updated_candidate_wrapper( |
| 139 | new JsepIceCandidate(candidate->sdp_mid(), |
| 140 | static_cast<int>(mediasection_index), |
| 141 | updated_candidate)); |
| 142 | if (!candidate_collection_[mediasection_index].HasCandidate( |
| 143 | updated_candidate_wrapper.get())) |
| 144 | candidate_collection_[mediasection_index].add( |
| 145 | updated_candidate_wrapper.release()); |
| 146 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
| 150 | size_t JsepSessionDescription::number_of_mediasections() const { |
| 151 | if (!description_) |
| 152 | return 0; |
| 153 | return description_->contents().size(); |
| 154 | } |
| 155 | |
| 156 | const IceCandidateCollection* JsepSessionDescription::candidates( |
| 157 | size_t mediasection_index) const { |
| 158 | if (mediasection_index >= candidate_collection_.size()) |
| 159 | return NULL; |
| 160 | return &candidate_collection_[mediasection_index]; |
| 161 | } |
| 162 | |
| 163 | bool JsepSessionDescription::ToString(std::string* out) const { |
| 164 | if (!description_ || !out) |
| 165 | return false; |
| 166 | *out = SdpSerialize(*this); |
| 167 | return !out->empty(); |
| 168 | } |
| 169 | |
| 170 | bool JsepSessionDescription::GetMediasectionIndex( |
| 171 | const IceCandidateInterface* candidate, |
| 172 | size_t* index) { |
| 173 | if (!candidate || !index) { |
| 174 | return false; |
| 175 | } |
| 176 | *index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 177 | if (description_ && !candidate->sdp_mid().empty()) { |
| 178 | bool found = false; |
| 179 | // Try to match the sdp_mid with content name. |
| 180 | for (size_t i = 0; i < description_->contents().size(); ++i) { |
| 181 | if (candidate->sdp_mid() == description_->contents().at(i).name) { |
| 182 | *index = i; |
| 183 | found = true; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | if (!found) { |
| 188 | // If the sdp_mid is presented but we can't find a match, we consider |
| 189 | // this as an error. |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | } // namespace webrtc |