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" |
| 30 | #include "talk/base/stringencode.h" |
| 31 | #include "talk/session/media/mediasession.h" |
| 32 | |
| 33 | using talk_base::scoped_ptr; |
| 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"; |
| 62 | const int JsepSessionDescription::kMaxVideoCodecWidth = 1280; |
| 63 | const int JsepSessionDescription::kMaxVideoCodecHeight = 720; |
| 64 | const int JsepSessionDescription::kDefaultVideoCodecPreference = 1; |
| 65 | |
| 66 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
| 67 | const std::string& sdp) { |
| 68 | return CreateSessionDescription(type, sdp, NULL); |
| 69 | } |
| 70 | |
| 71 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
| 72 | const std::string& sdp, |
| 73 | SdpParseError* error) { |
| 74 | if (!IsTypeSupported(type)) { |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | JsepSessionDescription* jsep_desc = new JsepSessionDescription(type); |
| 79 | if (!jsep_desc->Initialize(sdp, error)) { |
| 80 | delete jsep_desc; |
| 81 | return NULL; |
| 82 | } |
| 83 | return jsep_desc; |
| 84 | } |
| 85 | |
| 86 | JsepSessionDescription::JsepSessionDescription(const std::string& type) |
| 87 | : type_(type) { |
| 88 | } |
| 89 | |
| 90 | JsepSessionDescription::~JsepSessionDescription() {} |
| 91 | |
| 92 | bool JsepSessionDescription::Initialize( |
| 93 | cricket::SessionDescription* description, |
| 94 | const std::string& session_id, |
| 95 | const std::string& session_version) { |
| 96 | if (!description) |
| 97 | return false; |
| 98 | |
| 99 | session_id_ = session_id; |
| 100 | session_version_ = session_version; |
| 101 | description_.reset(description); |
| 102 | candidate_collection_.resize(number_of_mediasections()); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool JsepSessionDescription::Initialize(const std::string& sdp, |
| 107 | SdpParseError* error) { |
| 108 | return SdpDeserialize(sdp, this, error); |
| 109 | } |
| 110 | |
| 111 | bool JsepSessionDescription::AddCandidate( |
| 112 | const IceCandidateInterface* candidate) { |
| 113 | if (!candidate || candidate->sdp_mline_index() < 0) |
| 114 | return false; |
| 115 | size_t mediasection_index = 0; |
| 116 | if (!GetMediasectionIndex(candidate, &mediasection_index)) { |
| 117 | return false; |
| 118 | } |
| 119 | if (mediasection_index >= number_of_mediasections()) |
| 120 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | const std::string content_name = |
| 122 | description_->contents()[mediasection_index].name; |
| 123 | const cricket::TransportInfo* transport_info = |
| 124 | description_->GetTransportInfoByName(content_name); |
| 125 | if (!transport_info) { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | cricket::Candidate updated_candidate = candidate->candidate(); |
| 130 | if (updated_candidate.username().empty()) { |
| 131 | updated_candidate.set_username(transport_info->description.ice_ufrag); |
| 132 | } |
| 133 | if (updated_candidate.password().empty()) { |
| 134 | updated_candidate.set_password(transport_info->description.ice_pwd); |
| 135 | } |
| 136 | |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 137 | scoped_ptr<JsepIceCandidate> updated_candidate_wrapper( |
| 138 | new JsepIceCandidate(candidate->sdp_mid(), |
| 139 | static_cast<int>(mediasection_index), |
| 140 | updated_candidate)); |
| 141 | if (!candidate_collection_[mediasection_index].HasCandidate( |
| 142 | updated_candidate_wrapper.get())) |
| 143 | candidate_collection_[mediasection_index].add( |
| 144 | updated_candidate_wrapper.release()); |
| 145 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
| 149 | size_t JsepSessionDescription::number_of_mediasections() const { |
| 150 | if (!description_) |
| 151 | return 0; |
| 152 | return description_->contents().size(); |
| 153 | } |
| 154 | |
| 155 | const IceCandidateCollection* JsepSessionDescription::candidates( |
| 156 | size_t mediasection_index) const { |
| 157 | if (mediasection_index >= candidate_collection_.size()) |
| 158 | return NULL; |
| 159 | return &candidate_collection_[mediasection_index]; |
| 160 | } |
| 161 | |
| 162 | bool JsepSessionDescription::ToString(std::string* out) const { |
| 163 | if (!description_ || !out) |
| 164 | return false; |
| 165 | *out = SdpSerialize(*this); |
| 166 | return !out->empty(); |
| 167 | } |
| 168 | |
| 169 | bool JsepSessionDescription::GetMediasectionIndex( |
| 170 | const IceCandidateInterface* candidate, |
| 171 | size_t* index) { |
| 172 | if (!candidate || !index) { |
| 173 | return false; |
| 174 | } |
| 175 | *index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 176 | if (description_ && !candidate->sdp_mid().empty()) { |
| 177 | bool found = false; |
| 178 | // Try to match the sdp_mid with content name. |
| 179 | for (size_t i = 0; i < description_->contents().size(); ++i) { |
| 180 | if (candidate->sdp_mid() == description_->contents().at(i).name) { |
| 181 | *index = i; |
| 182 | found = true; |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | if (!found) { |
| 187 | // If the sdp_mid is presented but we can't find a match, we consider |
| 188 | // this as an error. |
| 189 | return false; |
| 190 | } |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | } // namespace webrtc |