pbos@webrtc.org | 289a35c | 2014-06-03 14:51:34 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 9e65a3b | 2014-06-11 13:42:37 +0000 | [diff] [blame] | 28 | #include "talk/media/webrtc/webrtcmediaengine.h" |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 29 | |
| 30 | #include <algorithm> |
| 31 | |
jbauch | 4cb3e39 | 2016-01-26 13:07:54 -0800 | [diff] [blame^] | 32 | #ifdef HAVE_WEBRTC_VIDEO |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 33 | #include "talk/media/webrtc/webrtcvideoengine2.h" |
jbauch | 4cb3e39 | 2016-01-26 13:07:54 -0800 | [diff] [blame^] | 34 | #else |
| 35 | #include "talk/media/webrtc/nullwebrtcvideoengine.h" |
| 36 | #endif |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 37 | #include "talk/media/webrtc/webrtcvoiceengine.h" |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 38 | |
| 39 | namespace cricket { |
| 40 | |
pbos@webrtc.org | e77c9c8 | 2015-03-11 10:49:33 +0000 | [diff] [blame] | 41 | class WebRtcMediaEngine2 |
jbauch | 4cb3e39 | 2016-01-26 13:07:54 -0800 | [diff] [blame^] | 42 | #ifdef HAVE_WEBRTC_VIDEO |
pbos@webrtc.org | e77c9c8 | 2015-03-11 10:49:33 +0000 | [diff] [blame] | 43 | : public CompositeMediaEngine<WebRtcVoiceEngine, WebRtcVideoEngine2> { |
jbauch | 4cb3e39 | 2016-01-26 13:07:54 -0800 | [diff] [blame^] | 44 | #else |
| 45 | : public CompositeMediaEngine<WebRtcVoiceEngine, NullWebRtcVideoEngine> { |
| 46 | #endif |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 47 | public: |
| 48 | WebRtcMediaEngine2(webrtc::AudioDeviceModule* adm, |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 49 | WebRtcVideoEncoderFactory* encoder_factory, |
| 50 | WebRtcVideoDecoderFactory* decoder_factory) { |
Fredrik Solenberg | ccb49e7 | 2015-05-19 11:37:56 +0200 | [diff] [blame] | 51 | voice_.SetAudioDeviceModule(adm); |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 52 | video_.SetExternalDecoderFactory(decoder_factory); |
| 53 | video_.SetExternalEncoderFactory(encoder_factory); |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 54 | } |
| 55 | }; |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 56 | |
| 57 | } // namespace cricket |
pbos@webrtc.org | 9e65a3b | 2014-06-11 13:42:37 +0000 | [diff] [blame] | 58 | |
pbos@webrtc.org | 9e65a3b | 2014-06-11 13:42:37 +0000 | [diff] [blame] | 59 | cricket::MediaEngineInterface* CreateWebRtcMediaEngine( |
| 60 | webrtc::AudioDeviceModule* adm, |
pbos@webrtc.org | 9e65a3b | 2014-06-11 13:42:37 +0000 | [diff] [blame] | 61 | cricket::WebRtcVideoEncoderFactory* encoder_factory, |
| 62 | cricket::WebRtcVideoDecoderFactory* decoder_factory) { |
Fredrik Solenberg | ccb49e7 | 2015-05-19 11:37:56 +0200 | [diff] [blame] | 63 | return new cricket::WebRtcMediaEngine2(adm, encoder_factory, |
tommi@webrtc.org | b789f62 | 2015-03-22 12:50:30 +0000 | [diff] [blame] | 64 | decoder_factory); |
pbos@webrtc.org | 9e65a3b | 2014-06-11 13:42:37 +0000 | [diff] [blame] | 65 | } |
| 66 | |
pbos@webrtc.org | 9e65a3b | 2014-06-11 13:42:37 +0000 | [diff] [blame] | 67 | void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) { |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 68 | delete media_engine; |
pbos@webrtc.org | 9e65a3b | 2014-06-11 13:42:37 +0000 | [diff] [blame] | 69 | } |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 70 | |
| 71 | namespace cricket { |
| 72 | |
buildbot@webrtc.org | 95bbd18 | 2014-08-20 07:49:30 +0000 | [diff] [blame] | 73 | // Used by PeerConnectionFactory to create a media engine passed into |
| 74 | // ChannelManager. |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 75 | MediaEngineInterface* WebRtcMediaEngineFactory::Create( |
| 76 | webrtc::AudioDeviceModule* adm, |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 77 | WebRtcVideoEncoderFactory* encoder_factory, |
| 78 | WebRtcVideoDecoderFactory* decoder_factory) { |
Fredrik Solenberg | ccb49e7 | 2015-05-19 11:37:56 +0200 | [diff] [blame] | 79 | return CreateWebRtcMediaEngine(adm, encoder_factory, decoder_factory); |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 80 | } |
| 81 | |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 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; |
Stefan Holmer | bbaf363 | 2015-10-29 18:53:23 +0100 | [diff] [blame] | 99 | } |
| 100 | } |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 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; |
Stefan Holmer | bbaf363 | 2015-10-29 18:53:23 +0100 | [diff] [blame] | 118 | } |
| 119 | |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 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 | } |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 163 | } // namespace cricket |