jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-08 04:46:45 | [diff] [blame] | 2 | * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-08 04:46:45 | [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. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 | [diff] [blame] | 9 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "media/base/media_engine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 15 | #include <cstdint> |
| 16 | #include <string> |
Sebastian Jansson | fa0aa39 | 2018-11-16 08:54:32 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 19 | #include "absl/algorithm/container.h" |
Åsa Persson | 23eba22 | 2018-10-02 12:47:06 | [diff] [blame] | 20 | #include "api/video/video_bitrate_allocation.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "rtc_base/string_encode.h" |
Åsa Persson | 23eba22 | 2018-10-02 12:47:06 | [diff] [blame] | 23 | |
skvlad | dc1c62c | 2016-03-17 02:07:43 | [diff] [blame] | 24 | namespace cricket { |
| 25 | |
Paulina Hensman | 11b34f4 | 2018-04-09 12:24:52 | [diff] [blame] | 26 | RtpCapabilities::RtpCapabilities() = default; |
| 27 | RtpCapabilities::~RtpCapabilities() = default; |
| 28 | |
skvlad | dc1c62c | 2016-03-17 02:07:43 | [diff] [blame] | 29 | webrtc::RtpParameters CreateRtpParametersWithOneEncoding() { |
| 30 | webrtc::RtpParameters parameters; |
| 31 | webrtc::RtpEncodingParameters encoding; |
| 32 | parameters.encodings.push_back(encoding); |
| 33 | return parameters; |
| 34 | } |
| 35 | |
Zach Stein | 3ca452b | 2018-01-18 18:01:24 | [diff] [blame] | 36 | webrtc::RtpParameters CreateRtpParametersWithEncodings(StreamParams sp) { |
| 37 | std::vector<uint32_t> primary_ssrcs; |
| 38 | sp.GetPrimarySsrcs(&primary_ssrcs); |
| 39 | size_t encoding_count = primary_ssrcs.size(); |
| 40 | |
| 41 | std::vector<webrtc::RtpEncodingParameters> encodings(encoding_count); |
| 42 | for (size_t i = 0; i < encodings.size(); ++i) { |
| 43 | encodings[i].ssrc = primary_ssrcs[i]; |
| 44 | } |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 45 | |
| 46 | const std::vector<RidDescription>& rids = sp.rids(); |
| 47 | RTC_DCHECK(rids.size() == 0 || rids.size() == encoding_count); |
| 48 | for (size_t i = 0; i < rids.size(); ++i) { |
| 49 | encodings[i].rid = rids[i].rid; |
| 50 | } |
| 51 | |
Zach Stein | 3ca452b | 2018-01-18 18:01:24 | [diff] [blame] | 52 | webrtc::RtpParameters parameters; |
| 53 | parameters.encodings = encodings; |
Florent Castelli | dacec71 | 2018-05-24 14:24:21 | [diff] [blame] | 54 | parameters.rtcp.cname = sp.cname; |
Zach Stein | 3ca452b | 2018-01-18 18:01:24 | [diff] [blame] | 55 | return parameters; |
| 56 | } |
| 57 | |
Markus Handell | 0357b3e | 2020-03-16 12:40:51 | [diff] [blame] | 58 | std::vector<webrtc::RtpExtension> GetDefaultEnabledRtpHeaderExtensions( |
| 59 | const RtpHeaderExtensionQueryInterface& query_interface) { |
| 60 | std::vector<webrtc::RtpExtension> extensions; |
| 61 | for (const auto& entry : query_interface.GetRtpHeaderExtensions()) { |
| 62 | if (entry.direction != webrtc::RtpTransceiverDirection::kStopped) |
| 63 | extensions.emplace_back(entry.uri, *entry.preferred_id); |
| 64 | } |
| 65 | return extensions; |
| 66 | } |
| 67 | |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 68 | webrtc::RTCError CheckScalabilityModeValues( |
| 69 | const webrtc::RtpParameters& rtp_parameters, |
Henrik Boström | 1e7a6f3 | 2024-02-13 08:22:20 | [diff] [blame] | 70 | rtc::ArrayView<cricket::Codec> codec_preferences, |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 71 | absl::optional<cricket::Codec> send_codec) { |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 72 | using webrtc::RTCErrorType; |
| 73 | |
Henrik Boström | 1e7a6f3 | 2024-02-13 08:22:20 | [diff] [blame] | 74 | if (codec_preferences.empty()) { |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 75 | // This is an audio sender or an extra check in the stack where the codec |
| 76 | // list is not available and we can't check the scalability_mode values. |
| 77 | return webrtc::RTCError::OK(); |
| 78 | } |
| 79 | |
| 80 | for (size_t i = 0; i < rtp_parameters.encodings.size(); ++i) { |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 81 | if (rtp_parameters.encodings[i].codec) { |
| 82 | bool codecFound = false; |
Henrik Boström | 1e7a6f3 | 2024-02-13 08:22:20 | [diff] [blame] | 83 | for (const cricket::VideoCodec& codec : codec_preferences) { |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 84 | if (codec.MatchesRtpCodec(*rtp_parameters.encodings[i].codec)) { |
| 85 | codecFound = true; |
| 86 | send_codec = codec; |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | if (!codecFound) { |
| 91 | LOG_AND_RETURN_ERROR( |
| 92 | RTCErrorType::INVALID_MODIFICATION, |
| 93 | "Attempted to use an unsupported codec for layer " + |
| 94 | std::to_string(i)); |
| 95 | } |
| 96 | } |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 97 | if (rtp_parameters.encodings[i].scalability_mode) { |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 98 | if (!send_codec) { |
| 99 | bool scalabilityModeFound = false; |
Henrik Boström | 1e7a6f3 | 2024-02-13 08:22:20 | [diff] [blame] | 100 | for (const cricket::VideoCodec& codec : codec_preferences) { |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 101 | for (const auto& scalability_mode : codec.scalability_modes) { |
| 102 | if (ScalabilityModeToString(scalability_mode) == |
| 103 | *rtp_parameters.encodings[i].scalability_mode) { |
| 104 | scalabilityModeFound = true; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | if (scalabilityModeFound) |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | if (!scalabilityModeFound) { |
| 113 | LOG_AND_RETURN_ERROR( |
| 114 | RTCErrorType::INVALID_MODIFICATION, |
| 115 | "Attempted to set RtpParameters scalabilityMode " |
| 116 | "to an unsupported value for the current codecs."); |
| 117 | } |
| 118 | } else { |
| 119 | bool scalabilityModeFound = false; |
| 120 | for (const auto& scalability_mode : send_codec->scalability_modes) { |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 121 | if (ScalabilityModeToString(scalability_mode) == |
| 122 | *rtp_parameters.encodings[i].scalability_mode) { |
| 123 | scalabilityModeFound = true; |
| 124 | break; |
| 125 | } |
| 126 | } |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 127 | if (!scalabilityModeFound) { |
| 128 | LOG_AND_RETURN_ERROR( |
| 129 | RTCErrorType::INVALID_MODIFICATION, |
| 130 | "Attempted to set RtpParameters scalabilityMode " |
| 131 | "to an unsupported value for the current codecs."); |
| 132 | } |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return webrtc::RTCError::OK(); |
| 138 | } |
| 139 | |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 140 | webrtc::RTCError CheckRtpParametersValues( |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 141 | const webrtc::RtpParameters& rtp_parameters, |
Henrik Boström | 1e7a6f3 | 2024-02-13 08:22:20 | [diff] [blame] | 142 | rtc::ArrayView<cricket::Codec> codec_preferences, |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 143 | absl::optional<cricket::Codec> send_codec) { |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 144 | using webrtc::RTCErrorType; |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 145 | |
| 146 | for (size_t i = 0; i < rtp_parameters.encodings.size(); ++i) { |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 147 | if (rtp_parameters.encodings[i].bitrate_priority <= 0) { |
| 148 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 149 | "Attempted to set RtpParameters bitrate_priority to " |
| 150 | "an invalid number. bitrate_priority must be > 0."); |
| 151 | } |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 152 | if (rtp_parameters.encodings[i].scale_resolution_down_by && |
| 153 | *rtp_parameters.encodings[i].scale_resolution_down_by < 1.0) { |
| 154 | LOG_AND_RETURN_ERROR( |
| 155 | RTCErrorType::INVALID_RANGE, |
| 156 | "Attempted to set RtpParameters scale_resolution_down_by to an " |
Florent Castelli | 907dc80 | 2019-12-06 14:03:19 | [diff] [blame] | 157 | "invalid value. scale_resolution_down_by must be >= 1.0"); |
| 158 | } |
| 159 | if (rtp_parameters.encodings[i].max_framerate && |
| 160 | *rtp_parameters.encodings[i].max_framerate < 0.0) { |
| 161 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 162 | "Attempted to set RtpParameters max_framerate to an " |
| 163 | "invalid value. max_framerate must be >= 0.0"); |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 164 | } |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 165 | if (rtp_parameters.encodings[i].min_bitrate_bps && |
| 166 | rtp_parameters.encodings[i].max_bitrate_bps) { |
| 167 | if (*rtp_parameters.encodings[i].max_bitrate_bps < |
| 168 | *rtp_parameters.encodings[i].min_bitrate_bps) { |
| 169 | LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::INVALID_RANGE, |
| 170 | "Attempted to set RtpParameters min bitrate " |
| 171 | "larger than max bitrate."); |
| 172 | } |
| 173 | } |
Åsa Persson | 23eba22 | 2018-10-02 12:47:06 | [diff] [blame] | 174 | if (rtp_parameters.encodings[i].num_temporal_layers) { |
| 175 | if (*rtp_parameters.encodings[i].num_temporal_layers < 1 || |
| 176 | *rtp_parameters.encodings[i].num_temporal_layers > |
| 177 | webrtc::kMaxTemporalStreams) { |
| 178 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 179 | "Attempted to set RtpParameters " |
| 180 | "num_temporal_layers to an invalid number."); |
| 181 | } |
| 182 | } |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 183 | |
| 184 | if (rtp_parameters.encodings[i].requested_resolution && |
| 185 | rtp_parameters.encodings[i].scale_resolution_down_by) { |
| 186 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 187 | "Attempted to set scale_resolution_down_by and " |
| 188 | "requested_resolution simultaniously."); |
| 189 | } |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 190 | |
| 191 | if (i > 0 && rtp_parameters.encodings[i - 1].codec != |
| 192 | rtp_parameters.encodings[i].codec) { |
| 193 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 194 | "Attempted to use different codec values for " |
| 195 | "different encodings."); |
| 196 | } |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 197 | } |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 198 | |
Henrik Boström | 1e7a6f3 | 2024-02-13 08:22:20 | [diff] [blame] | 199 | return CheckScalabilityModeValues(rtp_parameters, codec_preferences, |
| 200 | send_codec); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 201 | } |
| 202 | |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 203 | webrtc::RTCError CheckRtpParametersInvalidModificationAndValues( |
| 204 | const webrtc::RtpParameters& old_rtp_parameters, |
| 205 | const webrtc::RtpParameters& rtp_parameters) { |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 206 | return CheckRtpParametersInvalidModificationAndValues( |
| 207 | old_rtp_parameters, rtp_parameters, {}, absl::nullopt); |
Florent Castelli | 725ee24 | 2022-10-18 15:05:58 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | webrtc::RTCError CheckRtpParametersInvalidModificationAndValues( |
| 211 | const webrtc::RtpParameters& old_rtp_parameters, |
| 212 | const webrtc::RtpParameters& rtp_parameters, |
Henrik Boström | 1e7a6f3 | 2024-02-13 08:22:20 | [diff] [blame] | 213 | rtc::ArrayView<cricket::Codec> codec_preferences, |
Florent Castelli | 43a5dd8 | 2023-04-12 10:45:07 | [diff] [blame] | 214 | absl::optional<cricket::Codec> send_codec) { |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 215 | using webrtc::RTCErrorType; |
| 216 | if (rtp_parameters.encodings.size() != old_rtp_parameters.encodings.size()) { |
| 217 | LOG_AND_RETURN_ERROR( |
| 218 | RTCErrorType::INVALID_MODIFICATION, |
| 219 | "Attempted to set RtpParameters with different encoding count"); |
| 220 | } |
| 221 | if (rtp_parameters.rtcp != old_rtp_parameters.rtcp) { |
| 222 | LOG_AND_RETURN_ERROR( |
| 223 | RTCErrorType::INVALID_MODIFICATION, |
| 224 | "Attempted to set RtpParameters with modified RTCP parameters"); |
| 225 | } |
| 226 | if (rtp_parameters.header_extensions != |
| 227 | old_rtp_parameters.header_extensions) { |
| 228 | LOG_AND_RETURN_ERROR( |
| 229 | RTCErrorType::INVALID_MODIFICATION, |
| 230 | "Attempted to set RtpParameters with modified header extensions"); |
| 231 | } |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 232 | if (!absl::c_equal(old_rtp_parameters.encodings, rtp_parameters.encodings, |
| 233 | [](const webrtc::RtpEncodingParameters& encoding1, |
| 234 | const webrtc::RtpEncodingParameters& encoding2) { |
| 235 | return encoding1.rid == encoding2.rid; |
| 236 | })) { |
| 237 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 238 | "Attempted to change RID values in the encodings."); |
| 239 | } |
| 240 | if (!absl::c_equal(old_rtp_parameters.encodings, rtp_parameters.encodings, |
| 241 | [](const webrtc::RtpEncodingParameters& encoding1, |
| 242 | const webrtc::RtpEncodingParameters& encoding2) { |
| 243 | return encoding1.ssrc == encoding2.ssrc; |
| 244 | })) { |
| 245 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 246 | "Attempted to set RtpParameters with modified SSRC"); |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 247 | } |
| 248 | |
Henrik Boström | 1e7a6f3 | 2024-02-13 08:22:20 | [diff] [blame] | 249 | return CheckRtpParametersValues(rtp_parameters, codec_preferences, |
| 250 | send_codec); |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 251 | } |
| 252 | |
Sebastian Jansson | fa0aa39 | 2018-11-16 08:54:32 | [diff] [blame] | 253 | CompositeMediaEngine::CompositeMediaEngine( |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 254 | std::unique_ptr<webrtc::FieldTrialsView> trials, |
Erik Språng | ceb4495 | 2020-09-22 09:36:35 | [diff] [blame] | 255 | std::unique_ptr<VoiceEngineInterface> audio_engine, |
Sebastian Jansson | fa0aa39 | 2018-11-16 08:54:32 | [diff] [blame] | 256 | std::unique_ptr<VideoEngineInterface> video_engine) |
Erik Språng | ceb4495 | 2020-09-22 09:36:35 | [diff] [blame] | 257 | : trials_(std::move(trials)), |
| 258 | voice_engine_(std::move(audio_engine)), |
Sebastian Jansson | fa0aa39 | 2018-11-16 08:54:32 | [diff] [blame] | 259 | video_engine_(std::move(video_engine)) {} |
| 260 | |
Erik Språng | ceb4495 | 2020-09-22 09:36:35 | [diff] [blame] | 261 | CompositeMediaEngine::CompositeMediaEngine( |
| 262 | std::unique_ptr<VoiceEngineInterface> audio_engine, |
| 263 | std::unique_ptr<VideoEngineInterface> video_engine) |
| 264 | : CompositeMediaEngine(nullptr, |
| 265 | std::move(audio_engine), |
| 266 | std::move(video_engine)) {} |
| 267 | |
Sebastian Jansson | fa0aa39 | 2018-11-16 08:54:32 | [diff] [blame] | 268 | CompositeMediaEngine::~CompositeMediaEngine() = default; |
| 269 | |
| 270 | bool CompositeMediaEngine::Init() { |
| 271 | voice().Init(); |
| 272 | return true; |
| 273 | } |
| 274 | |
Sebastian Jansson | fa0aa39 | 2018-11-16 08:54:32 | [diff] [blame] | 275 | VoiceEngineInterface& CompositeMediaEngine::voice() { |
| 276 | return *voice_engine_.get(); |
| 277 | } |
| 278 | |
| 279 | VideoEngineInterface& CompositeMediaEngine::video() { |
| 280 | return *video_engine_.get(); |
| 281 | } |
| 282 | |
| 283 | const VoiceEngineInterface& CompositeMediaEngine::voice() const { |
| 284 | return *voice_engine_.get(); |
| 285 | } |
| 286 | |
| 287 | const VideoEngineInterface& CompositeMediaEngine::video() const { |
| 288 | return *video_engine_.get(); |
| 289 | } |
| 290 | |
Nico Weber | 22f9925 | 2019-02-20 15:13:16 | [diff] [blame] | 291 | } // namespace cricket |