10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/* libjingle 20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2012, Google Inc. 30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without 50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met: 60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 1. Redistributions of source code must retain the above copyright notice, 80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * this list of conditions and the following disclaimer. 90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 2. Redistributions in binary form must reproduce the above copyright notice, 100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * this list of conditions and the following disclaimer in the documentation 110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * and/or other materials provided with the distribution. 120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 3. The name of the author may not be used to endorse or promote products 130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * derived from this software without specific prior written permission. 140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */ 260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/app/webrtc/jsepsessiondescription.h" 280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/app/webrtc/webrtcsdp.h" 300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/session/media/mediasession.h" 31cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/stringencode.h" 320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 332a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgusing rtc::scoped_ptr; 340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgusing cricket::SessionDescription; 350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace webrtc { 370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char* kSupportedTypes[] = { 390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org JsepSessionDescription::kOffer, 400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org JsepSessionDescription::kPrAnswer, 410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org JsepSessionDescription::kAnswer 420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}; 430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic bool IsTypeSupported(const std::string& type) { 450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org bool type_supported = false; 460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org for (size_t i = 0; i < ARRAY_SIZE(kSupportedTypes); ++i) { 470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (kSupportedTypes[i] == type) { 480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org type_supported = true; 490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org break; 500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return type_supported; 530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst char SessionDescriptionInterface::kOffer[] = "offer"; 560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst char SessionDescriptionInterface::kPrAnswer[] = "pranswer"; 570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst char SessionDescriptionInterface::kAnswer[] = "answer"; 580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst int JsepSessionDescription::kDefaultVideoCodecId = 100; 6065ab28e69ec9341c282f5ba656dbaa8a5d1671c0niklas.enbom@webrtc.org// This is effectively a max value of the frame rate. 30 is default from camera. 6165ab28e69ec9341c282f5ba656dbaa8a5d1671c0niklas.enbom@webrtc.orgconst int JsepSessionDescription::kDefaultVideoCodecFramerate = 60; 620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8"; 63a26d7d6ee1c4d74b8996d19dbb46fc130df3ac17buildbot@webrtc.org// Used as default max video codec size before we have it in signaling. 6434f765950b7a4adb07c284f4522e2a64c9364affglaznev@webrtc.org#if defined(ANDROID) 6534f765950b7a4adb07c284f4522e2a64c9364affglaznev@webrtc.org// Limit default max video codec size for Android to avoid 6634f765950b7a4adb07c284f4522e2a64c9364affglaznev@webrtc.org// HW VP8 codec initialization failure for resolution higher than 720p. 6734f765950b7a4adb07c284f4522e2a64c9364affglaznev@webrtc.orgconst int JsepSessionDescription::kMaxVideoCodecWidth = 1280; 6834f765950b7a4adb07c284f4522e2a64c9364affglaznev@webrtc.orgconst int JsepSessionDescription::kMaxVideoCodecHeight = 720; 6934f765950b7a4adb07c284f4522e2a64c9364affglaznev@webrtc.org#else 706a9dda84d595cd6d4172ed395122300b64c5eea2glaznev@webrtc.orgconst int JsepSessionDescription::kMaxVideoCodecWidth = 1920; 716a9dda84d595cd6d4172ed395122300b64c5eea2glaznev@webrtc.orgconst int JsepSessionDescription::kMaxVideoCodecHeight = 1080; 7234f765950b7a4adb07c284f4522e2a64c9364affglaznev@webrtc.org#endif 730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst int JsepSessionDescription::kDefaultVideoCodecPreference = 1; 740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgSessionDescriptionInterface* CreateSessionDescription(const std::string& type, 760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& sdp) { 770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return CreateSessionDescription(type, sdp, NULL); 780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgSessionDescriptionInterface* CreateSessionDescription(const std::string& type, 810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& sdp, 820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org SdpParseError* error) { 830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!IsTypeSupported(type)) { 840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return NULL; 850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org JsepSessionDescription* jsep_desc = new JsepSessionDescription(type); 880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!jsep_desc->Initialize(sdp, error)) { 890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org delete jsep_desc; 900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return NULL; 910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return jsep_desc; 930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgJsepSessionDescription::JsepSessionDescription(const std::string& type) 960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org : type_(type) { 970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgJsepSessionDescription::~JsepSessionDescription() {} 1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool JsepSessionDescription::Initialize( 1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org cricket::SessionDescription* description, 1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& session_id, 1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& session_version) { 1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!description) 1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return false; 1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org session_id_ = session_id; 1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org session_version_ = session_version; 1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org description_.reset(description); 1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org candidate_collection_.resize(number_of_mediasections()); 1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return true; 1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool JsepSessionDescription::Initialize(const std::string& sdp, 1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org SdpParseError* error) { 1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return SdpDeserialize(sdp, this, error); 1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool JsepSessionDescription::AddCandidate( 1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const IceCandidateInterface* candidate) { 1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!candidate || candidate->sdp_mline_index() < 0) 1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return false; 1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org size_t mediasection_index = 0; 1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!GetMediasectionIndex(candidate, &mediasection_index)) { 1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return false; 1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (mediasection_index >= number_of_mediasections()) 1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return false; 1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string content_name = 1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org description_->contents()[mediasection_index].name; 1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const cricket::TransportInfo* transport_info = 1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org description_->GetTransportInfoByName(content_name); 1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!transport_info) { 1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return false; 1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org cricket::Candidate updated_candidate = candidate->candidate(); 1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (updated_candidate.username().empty()) { 1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org updated_candidate.set_username(transport_info->description.ice_ufrag); 1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (updated_candidate.password().empty()) { 1430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org updated_candidate.set_password(transport_info->description.ice_pwd); 1440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 146b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org scoped_ptr<JsepIceCandidate> updated_candidate_wrapper( 147b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org new JsepIceCandidate(candidate->sdp_mid(), 148b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org static_cast<int>(mediasection_index), 149b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org updated_candidate)); 150b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org if (!candidate_collection_[mediasection_index].HasCandidate( 151b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org updated_candidate_wrapper.get())) 152b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org candidate_collection_[mediasection_index].add( 153b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org updated_candidate_wrapper.release()); 154b881d27f23e9a8f52dc6a60fc66ebd75f9c2f15cmallinath@webrtc.org 1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return true; 1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgsize_t JsepSessionDescription::number_of_mediasections() const { 1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!description_) 1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return 0; 1610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return description_->contents().size(); 1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst IceCandidateCollection* JsepSessionDescription::candidates( 1650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org size_t mediasection_index) const { 1660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (mediasection_index >= candidate_collection_.size()) 1670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return NULL; 1680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return &candidate_collection_[mediasection_index]; 1690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool JsepSessionDescription::ToString(std::string* out) const { 1720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!description_ || !out) 1730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return false; 1740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *out = SdpSerialize(*this); 1750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return !out->empty(); 1760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool JsepSessionDescription::GetMediasectionIndex( 1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const IceCandidateInterface* candidate, 1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org size_t* index) { 1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!candidate || !index) { 1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return false; 1830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *index = static_cast<size_t>(candidate->sdp_mline_index()); 1850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (description_ && !candidate->sdp_mid().empty()) { 1860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org bool found = false; 1870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Try to match the sdp_mid with content name. 1880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org for (size_t i = 0; i < description_->contents().size(); ++i) { 1890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (candidate->sdp_mid() == description_->contents().at(i).name) { 1900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *index = i; 1910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org found = true; 1920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org break; 1930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!found) { 1960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // If the sdp_mid is presented but we can't find a match, we consider 1970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // this as an error. 1980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return false; 1990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return true; 2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 2030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 2040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} // namespace webrtc 205