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; 121 if (candidate_collection_[mediasection_index].HasCandidate(candidate)) { 122 return true; // Silently ignore this candidate if we already have it. 123 } 124 const std::string content_name = 125 description_->contents()[mediasection_index].name; 126 const cricket::TransportInfo* transport_info = 127 description_->GetTransportInfoByName(content_name); 128 if (!transport_info) { 129 return false; 130 } 131 132 cricket::Candidate updated_candidate = candidate->candidate(); 133 if (updated_candidate.username().empty()) { 134 updated_candidate.set_username(transport_info->description.ice_ufrag); 135 } 136 if (updated_candidate.password().empty()) { 137 updated_candidate.set_password(transport_info->description.ice_pwd); 138 } 139 140 candidate_collection_[mediasection_index].add( 141 new JsepIceCandidate(candidate->sdp_mid(), 142 static_cast<int>(mediasection_index), 143 updated_candidate)); 144 return true; 145 } 146 147 size_t JsepSessionDescription::number_of_mediasections() const { 148 if (!description_) 149 return 0; 150 return description_->contents().size(); 151 } 152 153 const IceCandidateCollection* JsepSessionDescription::candidates( 154 size_t mediasection_index) const { 155 if (mediasection_index >= candidate_collection_.size()) 156 return NULL; 157 return &candidate_collection_[mediasection_index]; 158 } 159 160 bool JsepSessionDescription::ToString(std::string* out) const { 161 if (!description_ || !out) 162 return false; 163 *out = SdpSerialize(*this); 164 return !out->empty(); 165 } 166 167 bool JsepSessionDescription::GetMediasectionIndex( 168 const IceCandidateInterface* candidate, 169 size_t* index) { 170 if (!candidate || !index) { 171 return false; 172 } 173 *index = static_cast<size_t>(candidate->sdp_mline_index()); 174 if (description_ && !candidate->sdp_mid().empty()) { 175 bool found = false; 176 // Try to match the sdp_mid with content name. 177 for (size_t i = 0; i < description_->contents().size(); ++i) { 178 if (candidate->sdp_mid() == description_->contents().at(i).name) { 179 *index = i; 180 found = true; 181 break; 182 } 183 } 184 if (!found) { 185 // If the sdp_mid is presented but we can't find a match, we consider 186 // this as an error. 187 return false; 188 } 189 } 190 return true; 191 } 192 193 } // namespace webrtc 194