| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2014 Google Inc. | |
| 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/media/webrtc/webrtcmediaengine.h" | |
| 29 | |
| 30 #include <algorithm> | |
| 31 | |
| 32 #ifdef HAVE_WEBRTC_VIDEO | |
| 33 #include "talk/media/webrtc/webrtcvideoengine2.h" | |
| 34 #else | |
| 35 #include "talk/media/webrtc/nullwebrtcvideoengine.h" | |
| 36 #endif | |
| 37 #include "talk/media/webrtc/webrtcvoiceengine.h" | |
| 38 | |
| 39 namespace cricket { | |
| 40 | |
| 41 class WebRtcMediaEngine2 | |
| 42 #ifdef HAVE_WEBRTC_VIDEO | |
| 43 : public CompositeMediaEngine<WebRtcVoiceEngine, WebRtcVideoEngine2> { | |
| 44 #else | |
| 45 : public CompositeMediaEngine<WebRtcVoiceEngine, NullWebRtcVideoEngine> { | |
| 46 #endif | |
| 47 public: | |
| 48 WebRtcMediaEngine2(webrtc::AudioDeviceModule* adm, | |
| 49 WebRtcVideoEncoderFactory* encoder_factory, | |
| 50 WebRtcVideoDecoderFactory* decoder_factory) { | |
| 51 voice_.SetAudioDeviceModule(adm); | |
| 52 video_.SetExternalDecoderFactory(decoder_factory); | |
| 53 video_.SetExternalEncoderFactory(encoder_factory); | |
| 54 } | |
| 55 }; | |
| 56 | |
| 57 } // namespace cricket | |
| 58 | |
| 59 cricket::MediaEngineInterface* CreateWebRtcMediaEngine( | |
| 60 webrtc::AudioDeviceModule* adm, | |
| 61 cricket::WebRtcVideoEncoderFactory* encoder_factory, | |
| 62 cricket::WebRtcVideoDecoderFactory* decoder_factory) { | |
| 63 return new cricket::WebRtcMediaEngine2(adm, encoder_factory, | |
| 64 decoder_factory); | |
| 65 } | |
| 66 | |
| 67 void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) { | |
| 68 delete media_engine; | |
| 69 } | |
| 70 | |
| 71 namespace cricket { | |
| 72 | |
| 73 // Used by PeerConnectionFactory to create a media engine passed into | |
| 74 // ChannelManager. | |
| 75 MediaEngineInterface* WebRtcMediaEngineFactory::Create( | |
| 76 webrtc::AudioDeviceModule* adm, | |
| 77 WebRtcVideoEncoderFactory* encoder_factory, | |
| 78 WebRtcVideoDecoderFactory* decoder_factory) { | |
| 79 return CreateWebRtcMediaEngine(adm, encoder_factory, decoder_factory); | |
| 80 } | |
| 81 | |
| 82 namespace { | |
| 83 // Remove mutually exclusive extensions with lower priority. | |
| 84 void DiscardRedundantExtensions( | |
| 85 std::vector<webrtc::RtpExtension>* extensions, | |
| 86 rtc::ArrayView<const char*> extensions_decreasing_prio) { | |
| 87 RTC_DCHECK(extensions); | |
| 88 bool found = false; | |
| 89 for (const char* name : extensions_decreasing_prio) { | |
| 90 auto it = std::find_if(extensions->begin(), extensions->end(), | |
| 91 [name](const webrtc::RtpExtension& rhs) { | |
| 92 return rhs.name == name; | |
| 93 }); | |
| 94 if (it != extensions->end()) { | |
| 95 if (found) { | |
| 96 extensions->erase(it); | |
| 97 } | |
| 98 found = true; | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 } // namespace | |
| 103 | |
| 104 bool ValidateRtpExtensions(const std::vector<RtpHeaderExtension>& extensions) { | |
| 105 bool id_used[14] = {false}; | |
| 106 for (const auto& extension : extensions) { | |
| 107 if (extension.id <= 0 || extension.id >= 15) { | |
| 108 LOG(LS_ERROR) << "Bad RTP extension ID: " << extension.ToString(); | |
| 109 return false; | |
| 110 } | |
| 111 if (id_used[extension.id - 1]) { | |
| 112 LOG(LS_ERROR) << "Duplicate RTP extension ID: " << extension.ToString(); | |
| 113 return false; | |
| 114 } | |
| 115 id_used[extension.id - 1] = true; | |
| 116 } | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 std::vector<webrtc::RtpExtension> FilterRtpExtensions( | |
| 121 const std::vector<RtpHeaderExtension>& extensions, | |
| 122 bool (*supported)(const std::string&), | |
| 123 bool filter_redundant_extensions) { | |
| 124 RTC_DCHECK(ValidateRtpExtensions(extensions)); | |
| 125 RTC_DCHECK(supported); | |
| 126 std::vector<webrtc::RtpExtension> result; | |
| 127 | |
| 128 // Ignore any extensions that we don't recognize. | |
| 129 for (const auto& extension : extensions) { | |
| 130 if (supported(extension.uri)) { | |
| 131 result.push_back({extension.uri, extension.id}); | |
| 132 } else { | |
| 133 LOG(LS_WARNING) << "Unsupported RTP extension: " << extension.ToString(); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 // Sort by name, ascending, so that we don't reset extensions if they were | |
| 138 // specified in a different order (also allows us to use std::unique below). | |
| 139 std::sort(result.begin(), result.end(), | |
| 140 [](const webrtc::RtpExtension& rhs, const webrtc::RtpExtension& lhs) { | |
| 141 return rhs.name < lhs.name; | |
| 142 }); | |
| 143 | |
| 144 // Remove unnecessary extensions (used on send side). | |
| 145 if (filter_redundant_extensions) { | |
| 146 auto it = std::unique(result.begin(), result.end(), | |
| 147 [](const webrtc::RtpExtension& rhs, const webrtc::RtpExtension& lhs) { | |
| 148 return rhs.name == lhs.name; | |
| 149 }); | |
| 150 result.erase(it, result.end()); | |
| 151 | |
| 152 // Keep just the highest priority extension of any in the following list. | |
| 153 static const char* kBweExtensionPriorities[] = { | |
| 154 kRtpTransportSequenceNumberHeaderExtension, | |
| 155 kRtpAbsoluteSenderTimeHeaderExtension, | |
| 156 kRtpTimestampOffsetHeaderExtension | |
| 157 }; | |
| 158 DiscardRedundantExtensions(&result, kBweExtensionPriorities); | |
| 159 } | |
| 160 | |
| 161 return result; | |
| 162 } | |
| 163 } // namespace cricket | |
| OLD | NEW |