| 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. |
| 9 | */ |
| 10 |
| 11 | #include "media/base/media_engine.h" |
| 12 |
| 13 | #include <stddef.h> |
| 14 |
| 15 | #include <cstdint> |
| 16 | #include <string> |
| 17 | #include <utility> |
| 18 |
| 19 | #include "absl/algorithm/container.h" |
| 20 | #include "api/video/video_bitrate_allocation.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/string_encode.h" |
| 23 |
| 24 | namespace cricket { |
| 25 |
| 26 | RtpCapabilities::RtpCapabilities() = default; |
| 27 | RtpCapabilities::~RtpCapabilities() = default; |
| 28 |
| 29 | webrtc::RtpParameters CreateRtpParametersWithOneEncoding() { |
| 30 | webrtc::RtpParameters parameters; |
| 31 | webrtc::RtpEncodingParameters encoding; |
| 32 | parameters.encodings.push_back(encoding); |
| 33 | return parameters; |
| 34 | } |
| 35 |
| 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 | } |
| 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 |
| 52 | webrtc::RtpParameters parameters; |
| 53 | parameters.encodings = encodings; |
| 54 | parameters.rtcp.cname = sp.cname; |
| 55 | return parameters; |
| 56 | } |
| 57 |
| 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 |
| 68 | webrtc::RTCError CheckScalabilityModeValues( |
| 69 | const webrtc::RtpParameters& rtp_parameters, |
| 70 | rtc::ArrayView<cricket::Codec> codec_preferences, |
| 71 | absl::optional<cricket::Codec> send_codec) { |
| 72 | using webrtc::RTCErrorType; |
| 73 |
| 74 | if (codec_preferences.empty()) { |
| 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) { |
| 81 | if (rtp_parameters.encodings[i].codec) { |
| 82 | bool codecFound = false; |
| 83 | for (const cricket::VideoCodec& codec : codec_preferences) { |
| 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 | } |
| 97 | if (rtp_parameters.encodings[i].scalability_mode) { |
| 98 | if (!send_codec) { |
| 99 | bool scalabilityModeFound = false; |
| 100 | for (const cricket::VideoCodec& codec : codec_preferences) { |
| 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) { |
| 121 | if (ScalabilityModeToString(scalability_mode) == |
| 122 | *rtp_parameters.encodings[i].scalability_mode) { |
| 123 | scalabilityModeFound = true; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 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 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 |
| 137 | return webrtc::RTCError::OK(); |
| 138 | } |
| 139 |
| 140 | webrtc::RTCError CheckRtpParametersValues( |
| 141 | const webrtc::RtpParameters& rtp_parameters, |
| 142 | rtc::ArrayView<cricket::Codec> codec_preferences, |
| 143 | absl::optional<cricket::Codec> send_codec) { |
| 144 | using webrtc::RTCErrorType; |
| 145 |
| 146 | for (size_t i = 0; i < rtp_parameters.encodings.size(); ++i) { |
| 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 | } |
| 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 " |
| 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"); |
| 164 | } |
| 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 | } |
| 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 | } |
| 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 | } |
| 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 | } |
| 197 | } |
| 198 |
| 199 | return CheckScalabilityModeValues(rtp_parameters, codec_preferences, |
| 200 | send_codec); |
| 201 | } |
| 202 |
| 203 | webrtc::RTCError CheckRtpParametersInvalidModificationAndValues( |
| 204 | const webrtc::RtpParameters& old_rtp_parameters, |
| 205 | const webrtc::RtpParameters& rtp_parameters) { |
| 206 | return CheckRtpParametersInvalidModificationAndValues( |
| 207 | old_rtp_parameters, rtp_parameters, {}, absl::nullopt); |
| 208 | } |
| 209 |
| 210 | webrtc::RTCError CheckRtpParametersInvalidModificationAndValues( |
| 211 | const webrtc::RtpParameters& old_rtp_parameters, |
| 212 | const webrtc::RtpParameters& rtp_parameters, |
| 213 | rtc::ArrayView<cricket::Codec> codec_preferences, |
| 214 | absl::optional<cricket::Codec> send_codec) { |
| 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 | } |
| 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"); |
| 247 | } |
| 248 |
| 249 | return CheckRtpParametersValues(rtp_parameters, codec_preferences, |
| 250 | send_codec); |
| 251 | } |
| 252 |
| 253 | CompositeMediaEngine::CompositeMediaEngine( |
| 254 | std::unique_ptr<webrtc::FieldTrialsView> trials, |
| 255 | std::unique_ptr<VoiceEngineInterface> audio_engine, |
| 256 | std::unique_ptr<VideoEngineInterface> video_engine) |
| 257 | : trials_(std::move(trials)), |
| 258 | voice_engine_(std::move(audio_engine)), |
| 259 | video_engine_(std::move(video_engine)) {} |
| 260 |
| 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 |
| 268 | CompositeMediaEngine::~CompositeMediaEngine() = default; |
| 269 |
| 270 | bool CompositeMediaEngine::Init() { |
| 271 | voice().Init(); |
| 272 | return true; |
| 273 | } |
| 274 |
| 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 |
| 291 | } // namespace cricket |
| 292 |