jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +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. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 9 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "media/base/media_engine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <cstdint> |
| 16 | #include <string> |
Sebastian Jansson | fa0aa39 | 2018-11-16 09:54:32 +0100 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 19 | #include "absl/algorithm/container.h" |
Åsa Persson | 23eba22 | 2018-10-02 14:47:06 +0200 | [diff] [blame] | 20 | #include "api/video/video_bitrate_allocation.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/string_encode.h" |
Åsa Persson | 23eba22 | 2018-10-02 14:47:06 +0200 | [diff] [blame] | 23 | |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [diff] [blame] | 24 | namespace cricket { |
| 25 | |
Paulina Hensman | 11b34f4 | 2018-04-09 14:24:52 +0200 | [diff] [blame] | 26 | RtpCapabilities::RtpCapabilities() = default; |
| 27 | RtpCapabilities::~RtpCapabilities() = default; |
| 28 | |
skvlad | dc1c62c | 2016-03-16 19:07:43 -0700 | [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 10:01:24 -0800 | [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 12:49:22 -0800 | [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 10:01:24 -0800 | [diff] [blame] | 52 | webrtc::RtpParameters parameters; |
| 53 | parameters.encodings = encodings; |
Florent Castelli | dacec71 | 2018-05-24 16:24:21 +0200 | [diff] [blame] | 54 | parameters.rtcp.cname = sp.cname; |
Zach Stein | 3ca452b | 2018-01-18 10:01:24 -0800 | [diff] [blame] | 55 | return parameters; |
| 56 | } |
| 57 | |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 58 | webrtc::RTCError CheckRtpParametersValues( |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 59 | const webrtc::RtpParameters& rtp_parameters) { |
| 60 | using webrtc::RTCErrorType; |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 61 | |
| 62 | for (size_t i = 0; i < rtp_parameters.encodings.size(); ++i) { |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 63 | if (rtp_parameters.encodings[i].bitrate_priority <= 0) { |
| 64 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 65 | "Attempted to set RtpParameters bitrate_priority to " |
| 66 | "an invalid number. bitrate_priority must be > 0."); |
| 67 | } |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 68 | if (rtp_parameters.encodings[i].scale_resolution_down_by && |
| 69 | *rtp_parameters.encodings[i].scale_resolution_down_by < 1.0) { |
| 70 | LOG_AND_RETURN_ERROR( |
| 71 | RTCErrorType::INVALID_RANGE, |
| 72 | "Attempted to set RtpParameters scale_resolution_down_by to an " |
| 73 | "invalid number. scale_resolution_down_by must be >= 1.0"); |
| 74 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 75 | if (rtp_parameters.encodings[i].min_bitrate_bps && |
| 76 | rtp_parameters.encodings[i].max_bitrate_bps) { |
| 77 | if (*rtp_parameters.encodings[i].max_bitrate_bps < |
| 78 | *rtp_parameters.encodings[i].min_bitrate_bps) { |
| 79 | LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::INVALID_RANGE, |
| 80 | "Attempted to set RtpParameters min bitrate " |
| 81 | "larger than max bitrate."); |
| 82 | } |
| 83 | } |
Åsa Persson | 23eba22 | 2018-10-02 14:47:06 +0200 | [diff] [blame] | 84 | if (rtp_parameters.encodings[i].num_temporal_layers) { |
| 85 | if (*rtp_parameters.encodings[i].num_temporal_layers < 1 || |
| 86 | *rtp_parameters.encodings[i].num_temporal_layers > |
| 87 | webrtc::kMaxTemporalStreams) { |
| 88 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 89 | "Attempted to set RtpParameters " |
| 90 | "num_temporal_layers to an invalid number."); |
| 91 | } |
| 92 | } |
| 93 | if (i > 0 && (rtp_parameters.encodings[i].num_temporal_layers != |
| 94 | rtp_parameters.encodings[i - 1].num_temporal_layers)) { |
| 95 | LOG_AND_RETURN_ERROR( |
| 96 | RTCErrorType::INVALID_MODIFICATION, |
| 97 | "Attempted to set RtpParameters num_temporal_layers " |
| 98 | "at encoding layer i: " + |
| 99 | rtc::ToString(i) + |
| 100 | " to a different value than other encoding layers."); |
| 101 | } |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 102 | } |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 103 | |
Florent Castelli | 892acf0 | 2018-10-01 22:47:20 +0200 | [diff] [blame] | 104 | return webrtc::RTCError::OK(); |
| 105 | } |
| 106 | |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 107 | webrtc::RTCError CheckRtpParametersInvalidModificationAndValues( |
| 108 | const webrtc::RtpParameters& old_rtp_parameters, |
| 109 | const webrtc::RtpParameters& rtp_parameters) { |
| 110 | using webrtc::RTCErrorType; |
| 111 | if (rtp_parameters.encodings.size() != old_rtp_parameters.encodings.size()) { |
| 112 | LOG_AND_RETURN_ERROR( |
| 113 | RTCErrorType::INVALID_MODIFICATION, |
| 114 | "Attempted to set RtpParameters with different encoding count"); |
| 115 | } |
| 116 | if (rtp_parameters.rtcp != old_rtp_parameters.rtcp) { |
| 117 | LOG_AND_RETURN_ERROR( |
| 118 | RTCErrorType::INVALID_MODIFICATION, |
| 119 | "Attempted to set RtpParameters with modified RTCP parameters"); |
| 120 | } |
| 121 | if (rtp_parameters.header_extensions != |
| 122 | old_rtp_parameters.header_extensions) { |
| 123 | LOG_AND_RETURN_ERROR( |
| 124 | RTCErrorType::INVALID_MODIFICATION, |
| 125 | "Attempted to set RtpParameters with modified header extensions"); |
| 126 | } |
Amit Hilbuch | 2297d33 | 2019-02-19 12:49:22 -0800 | [diff] [blame] | 127 | if (!absl::c_equal(old_rtp_parameters.encodings, rtp_parameters.encodings, |
| 128 | [](const webrtc::RtpEncodingParameters& encoding1, |
| 129 | const webrtc::RtpEncodingParameters& encoding2) { |
| 130 | return encoding1.rid == encoding2.rid; |
| 131 | })) { |
| 132 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 133 | "Attempted to change RID values in the encodings."); |
| 134 | } |
| 135 | if (!absl::c_equal(old_rtp_parameters.encodings, rtp_parameters.encodings, |
| 136 | [](const webrtc::RtpEncodingParameters& encoding1, |
| 137 | const webrtc::RtpEncodingParameters& encoding2) { |
| 138 | return encoding1.ssrc == encoding2.ssrc; |
| 139 | })) { |
| 140 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 141 | "Attempted to set RtpParameters with modified SSRC"); |
Florent Castelli | c1a0bcb | 2019-01-29 14:26:48 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return CheckRtpParametersValues(rtp_parameters); |
| 145 | } |
| 146 | |
Sebastian Jansson | fa0aa39 | 2018-11-16 09:54:32 +0100 | [diff] [blame] | 147 | CompositeMediaEngine::CompositeMediaEngine( |
| 148 | std::unique_ptr<VoiceEngineInterface> voice_engine, |
| 149 | std::unique_ptr<VideoEngineInterface> video_engine) |
| 150 | : voice_engine_(std::move(voice_engine)), |
| 151 | video_engine_(std::move(video_engine)) {} |
| 152 | |
| 153 | CompositeMediaEngine::~CompositeMediaEngine() = default; |
| 154 | |
| 155 | bool CompositeMediaEngine::Init() { |
| 156 | voice().Init(); |
| 157 | return true; |
| 158 | } |
| 159 | |
Sebastian Jansson | fa0aa39 | 2018-11-16 09:54:32 +0100 | [diff] [blame] | 160 | VoiceEngineInterface& CompositeMediaEngine::voice() { |
| 161 | return *voice_engine_.get(); |
| 162 | } |
| 163 | |
| 164 | VideoEngineInterface& CompositeMediaEngine::video() { |
| 165 | return *video_engine_.get(); |
| 166 | } |
| 167 | |
| 168 | const VoiceEngineInterface& CompositeMediaEngine::voice() const { |
| 169 | return *voice_engine_.get(); |
| 170 | } |
| 171 | |
| 172 | const VideoEngineInterface& CompositeMediaEngine::video() const { |
| 173 | return *video_engine_.get(); |
| 174 | } |
| 175 | |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 176 | } // namespace cricket |