pbos@webrtc.org | 289a35c | 2014-06-03 14:51:34 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
pbos@webrtc.org | 289a35c | 2014-06-03 14:51:34 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
pbos@webrtc.org | 289a35c | 2014-06-03 14:51:34 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "media/engine/webrtc_media_engine.h" |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 12 | |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 13 | #include <utility> |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 14 | |
Steve Anton | 2c9ebef | 2019-01-28 17:27:58 -0800 | [diff] [blame] | 15 | #include "absl/algorithm/container.h" |
Sebastian Jansson | fa0aa39 | 2018-11-16 09:54:32 +0100 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "media/engine/webrtc_voice_engine.h" |
Konrad Hofbauer | b0397d6 | 2019-01-23 09:46:58 +0100 | [diff] [blame] | 18 | #include "system_wrappers/include/field_trial.h" |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 19 | |
jbauch | 4cb3e39 | 2016-01-26 13:07:54 -0800 | [diff] [blame] | 20 | #ifdef HAVE_WEBRTC_VIDEO |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "media/engine/webrtc_video_engine.h" |
jbauch | 4cb3e39 | 2016-01-26 13:07:54 -0800 | [diff] [blame] | 22 | #else |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "media/engine/null_webrtc_video_engine.h" |
jbauch | 4cb3e39 | 2016-01-26 13:07:54 -0800 | [diff] [blame] | 24 | #endif |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 25 | |
| 26 | namespace cricket { |
| 27 | |
Danil Chapovalov | 4844c5f | 2019-04-10 14:10:10 +0200 | [diff] [blame] | 28 | std::unique_ptr<MediaEngineInterface> CreateMediaEngine( |
| 29 | MediaEngineDependencies dependencies) { |
| 30 | auto audio_engine = absl::make_unique<WebRtcVoiceEngine>( |
| 31 | dependencies.task_queue_factory, std::move(dependencies.adm), |
| 32 | std::move(dependencies.audio_encoder_factory), |
| 33 | std::move(dependencies.audio_decoder_factory), |
| 34 | std::move(dependencies.audio_mixer), |
| 35 | std::move(dependencies.audio_processing)); |
| 36 | #ifdef HAVE_WEBRTC_VIDEO |
| 37 | auto video_engine = absl::make_unique<WebRtcVideoEngine>( |
| 38 | std::move(dependencies.video_encoder_factory), |
Jonas Oreland | a3aa9bd | 2019-04-17 07:38:40 +0200 | [diff] [blame] | 39 | std::move(dependencies.video_decoder_factory)); |
Danil Chapovalov | 4844c5f | 2019-04-10 14:10:10 +0200 | [diff] [blame] | 40 | #else |
| 41 | auto video_engine = absl::make_unique<NullWebRtcVideoEngine>(); |
| 42 | #endif |
| 43 | return absl::make_unique<CompositeMediaEngine>(std::move(audio_engine), |
| 44 | std::move(video_engine)); |
| 45 | } |
| 46 | |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 47 | namespace { |
Konrad Hofbauer | b0397d6 | 2019-01-23 09:46:58 +0100 | [diff] [blame] | 48 | // If this FieldTrial is enabled, we will not filter out the abs-send-time |
| 49 | // header extension when the TWCC extensions were also negotiated, but keep |
| 50 | // kAbsSendTimeUri also if kTransportSequenceNumberUri is present. |
| 51 | bool IsKeepAbsSendTimeExtensionFieldTrialEnabled() { |
| 52 | return webrtc::field_trial::IsEnabled("WebRTC-KeepAbsSendTimeExtension"); |
| 53 | } |
| 54 | |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 55 | // Remove mutually exclusive extensions with lower priority. |
| 56 | void DiscardRedundantExtensions( |
| 57 | std::vector<webrtc::RtpExtension>* extensions, |
agrieve | 26622d3 | 2017-08-08 10:48:15 -0700 | [diff] [blame] | 58 | rtc::ArrayView<const char* const> extensions_decreasing_prio) { |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 59 | RTC_DCHECK(extensions); |
| 60 | bool found = false; |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 61 | for (const char* uri : extensions_decreasing_prio) { |
Steve Anton | 2c9ebef | 2019-01-28 17:27:58 -0800 | [diff] [blame] | 62 | auto it = absl::c_find_if( |
| 63 | *extensions, |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 64 | [uri](const webrtc::RtpExtension& rhs) { return rhs.uri == uri; }); |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 65 | if (it != extensions->end()) { |
| 66 | if (found) { |
| 67 | extensions->erase(it); |
| 68 | } |
| 69 | found = true; |
Stefan Holmer | bbaf363 | 2015-10-29 18:53:23 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 72 | } |
| 73 | } // namespace |
| 74 | |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 75 | bool ValidateRtpExtensions( |
| 76 | const std::vector<webrtc::RtpExtension>& extensions) { |
Johannes Kron | 07ba2b9 | 2018-09-26 13:33:35 +0200 | [diff] [blame] | 77 | bool id_used[1 + webrtc::RtpExtension::kMaxId] = {false}; |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 78 | for (const auto& extension : extensions) { |
Johannes Kron | 07ba2b9 | 2018-09-26 13:33:35 +0200 | [diff] [blame] | 79 | if (extension.id < webrtc::RtpExtension::kMinId || |
| 80 | extension.id > webrtc::RtpExtension::kMaxId) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 81 | RTC_LOG(LS_ERROR) << "Bad RTP extension ID: " << extension.ToString(); |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 82 | return false; |
| 83 | } |
Johannes Kron | 07ba2b9 | 2018-09-26 13:33:35 +0200 | [diff] [blame] | 84 | if (id_used[extension.id]) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 85 | RTC_LOG(LS_ERROR) << "Duplicate RTP extension ID: " |
| 86 | << extension.ToString(); |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 87 | return false; |
| 88 | } |
Johannes Kron | 07ba2b9 | 2018-09-26 13:33:35 +0200 | [diff] [blame] | 89 | id_used[extension.id] = true; |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 90 | } |
| 91 | return true; |
Stefan Holmer | bbaf363 | 2015-10-29 18:53:23 +0100 | [diff] [blame] | 92 | } |
| 93 | |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 94 | std::vector<webrtc::RtpExtension> FilterRtpExtensions( |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 95 | const std::vector<webrtc::RtpExtension>& extensions, |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 96 | bool (*supported)(const std::string&), |
| 97 | bool filter_redundant_extensions) { |
| 98 | RTC_DCHECK(ValidateRtpExtensions(extensions)); |
| 99 | RTC_DCHECK(supported); |
| 100 | std::vector<webrtc::RtpExtension> result; |
| 101 | |
| 102 | // Ignore any extensions that we don't recognize. |
| 103 | for (const auto& extension : extensions) { |
| 104 | if (supported(extension.uri)) { |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 105 | result.push_back(extension); |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 106 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 107 | RTC_LOG(LS_WARNING) << "Unsupported RTP extension: " |
| 108 | << extension.ToString(); |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 112 | // Sort by name, ascending (prioritise encryption), so that we don't reset |
| 113 | // extensions if they were specified in a different order (also allows us |
| 114 | // to use std::unique below). |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 115 | absl::c_sort(result, [](const webrtc::RtpExtension& rhs, |
| 116 | const webrtc::RtpExtension& lhs) { |
| 117 | return rhs.encrypt == lhs.encrypt ? rhs.uri < lhs.uri |
| 118 | : rhs.encrypt > lhs.encrypt; |
| 119 | }); |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 120 | |
| 121 | // Remove unnecessary extensions (used on send side). |
| 122 | if (filter_redundant_extensions) { |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 123 | auto it = std::unique( |
| 124 | result.begin(), result.end(), |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 125 | [](const webrtc::RtpExtension& rhs, const webrtc::RtpExtension& lhs) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 126 | return rhs.uri == lhs.uri && rhs.encrypt == lhs.encrypt; |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 127 | }); |
| 128 | result.erase(it, result.end()); |
| 129 | |
Konrad Hofbauer | b0397d6 | 2019-01-23 09:46:58 +0100 | [diff] [blame] | 130 | // Keep just the highest priority extension of any in the following lists. |
| 131 | if (IsKeepAbsSendTimeExtensionFieldTrialEnabled()) { |
| 132 | static const char* const kBweExtensionPriorities[] = { |
| 133 | webrtc::RtpExtension::kAbsSendTimeUri, |
| 134 | webrtc::RtpExtension::kTimestampOffsetUri}; |
| 135 | DiscardRedundantExtensions(&result, kBweExtensionPriorities); |
| 136 | } else { |
| 137 | static const char* const kBweExtensionPriorities[] = { |
| 138 | webrtc::RtpExtension::kTransportSequenceNumberUri, |
| 139 | webrtc::RtpExtension::kAbsSendTimeUri, |
| 140 | webrtc::RtpExtension::kTimestampOffsetUri}; |
| 141 | DiscardRedundantExtensions(&result, kBweExtensionPriorities); |
| 142 | } |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 143 | } |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 144 | return result; |
| 145 | } |
stefan | 13f1a0a | 2016-11-30 07:22:58 -0800 | [diff] [blame] | 146 | |
Sebastian Jansson | fc8d26b | 2018-02-21 09:52:06 +0100 | [diff] [blame] | 147 | webrtc::BitrateConstraints GetBitrateConfigForCodec(const Codec& codec) { |
| 148 | webrtc::BitrateConstraints config; |
stefan | 13f1a0a | 2016-11-30 07:22:58 -0800 | [diff] [blame] | 149 | int bitrate_kbps = 0; |
| 150 | if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) && |
| 151 | bitrate_kbps > 0) { |
| 152 | config.min_bitrate_bps = bitrate_kbps * 1000; |
| 153 | } else { |
| 154 | config.min_bitrate_bps = 0; |
| 155 | } |
| 156 | if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) && |
| 157 | bitrate_kbps > 0) { |
| 158 | config.start_bitrate_bps = bitrate_kbps * 1000; |
| 159 | } else { |
| 160 | // Do not reconfigure start bitrate unless it's specified and positive. |
| 161 | config.start_bitrate_bps = -1; |
| 162 | } |
| 163 | if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) && |
| 164 | bitrate_kbps > 0) { |
| 165 | config.max_bitrate_bps = bitrate_kbps * 1000; |
| 166 | } else { |
| 167 | config.max_bitrate_bps = -1; |
| 168 | } |
| 169 | return config; |
| 170 | } |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 171 | } // namespace cricket |