blob: 43929116ac4359b268a28f94778955b4a8dd9f90 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.org5f93d0a2015-01-20 21:36:13 +00009 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000010
Steve Anton10542f22019-01-11 09:11:00 -080011#include "media/base/media_engine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
14#include <cstdint>
15#include <string>
Sebastian Janssonfa0aa392018-11-16 09:54:32 +010016#include <utility>
17
Amit Hilbuch2297d332019-02-19 12:49:22 -080018#include "absl/algorithm/container.h"
Åsa Persson23eba222018-10-02 14:47:06 +020019#include "api/video/video_bitrate_allocation.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/string_encode.h"
Åsa Persson23eba222018-10-02 14:47:06 +020022
skvladdc1c62c2016-03-16 19:07:43 -070023namespace cricket {
24
Paulina Hensman11b34f42018-04-09 14:24:52 +020025RtpCapabilities::RtpCapabilities() = default;
26RtpCapabilities::~RtpCapabilities() = default;
27
skvladdc1c62c2016-03-16 19:07:43 -070028webrtc::RtpParameters CreateRtpParametersWithOneEncoding() {
29 webrtc::RtpParameters parameters;
30 webrtc::RtpEncodingParameters encoding;
31 parameters.encodings.push_back(encoding);
32 return parameters;
33}
34
Zach Stein3ca452b2018-01-18 10:01:24 -080035webrtc::RtpParameters CreateRtpParametersWithEncodings(StreamParams sp) {
36 std::vector<uint32_t> primary_ssrcs;
37 sp.GetPrimarySsrcs(&primary_ssrcs);
38 size_t encoding_count = primary_ssrcs.size();
39
40 std::vector<webrtc::RtpEncodingParameters> encodings(encoding_count);
41 for (size_t i = 0; i < encodings.size(); ++i) {
42 encodings[i].ssrc = primary_ssrcs[i];
43 }
Amit Hilbuch2297d332019-02-19 12:49:22 -080044
45 const std::vector<RidDescription>& rids = sp.rids();
46 RTC_DCHECK(rids.size() == 0 || rids.size() == encoding_count);
47 for (size_t i = 0; i < rids.size(); ++i) {
48 encodings[i].rid = rids[i].rid;
49 }
50
Zach Stein3ca452b2018-01-18 10:01:24 -080051 webrtc::RtpParameters parameters;
52 parameters.encodings = encodings;
Florent Castellidacec712018-05-24 16:24:21 +020053 parameters.rtcp.cname = sp.cname;
Zach Stein3ca452b2018-01-18 10:01:24 -080054 return parameters;
55}
56
Florent Castellic1a0bcb2019-01-29 14:26:48 +010057webrtc::RTCError CheckRtpParametersValues(
Florent Castelli892acf02018-10-01 22:47:20 +020058 const webrtc::RtpParameters& rtp_parameters) {
59 using webrtc::RTCErrorType;
Florent Castelli892acf02018-10-01 22:47:20 +020060
61 for (size_t i = 0; i < rtp_parameters.encodings.size(); ++i) {
Florent Castelli892acf02018-10-01 22:47:20 +020062 if (rtp_parameters.encodings[i].bitrate_priority <= 0) {
63 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE,
64 "Attempted to set RtpParameters bitrate_priority to "
65 "an invalid number. bitrate_priority must be > 0.");
66 }
Florent Castellic1a0bcb2019-01-29 14:26:48 +010067 if (rtp_parameters.encodings[i].scale_resolution_down_by &&
68 *rtp_parameters.encodings[i].scale_resolution_down_by < 1.0) {
69 LOG_AND_RETURN_ERROR(
70 RTCErrorType::INVALID_RANGE,
71 "Attempted to set RtpParameters scale_resolution_down_by to an "
72 "invalid number. scale_resolution_down_by must be >= 1.0");
73 }
Florent Castelli892acf02018-10-01 22:47:20 +020074 if (rtp_parameters.encodings[i].min_bitrate_bps &&
75 rtp_parameters.encodings[i].max_bitrate_bps) {
76 if (*rtp_parameters.encodings[i].max_bitrate_bps <
77 *rtp_parameters.encodings[i].min_bitrate_bps) {
78 LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::INVALID_RANGE,
79 "Attempted to set RtpParameters min bitrate "
80 "larger than max bitrate.");
81 }
82 }
Åsa Persson23eba222018-10-02 14:47:06 +020083 if (rtp_parameters.encodings[i].num_temporal_layers) {
84 if (*rtp_parameters.encodings[i].num_temporal_layers < 1 ||
85 *rtp_parameters.encodings[i].num_temporal_layers >
86 webrtc::kMaxTemporalStreams) {
87 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE,
88 "Attempted to set RtpParameters "
89 "num_temporal_layers to an invalid number.");
90 }
91 }
92 if (i > 0 && (rtp_parameters.encodings[i].num_temporal_layers !=
93 rtp_parameters.encodings[i - 1].num_temporal_layers)) {
94 LOG_AND_RETURN_ERROR(
95 RTCErrorType::INVALID_MODIFICATION,
96 "Attempted to set RtpParameters num_temporal_layers "
97 "at encoding layer i: " +
98 rtc::ToString(i) +
99 " to a different value than other encoding layers.");
100 }
Florent Castelli892acf02018-10-01 22:47:20 +0200101 }
Florent Castellic1a0bcb2019-01-29 14:26:48 +0100102
Florent Castelli892acf02018-10-01 22:47:20 +0200103 return webrtc::RTCError::OK();
104}
105
Florent Castellic1a0bcb2019-01-29 14:26:48 +0100106webrtc::RTCError CheckRtpParametersInvalidModificationAndValues(
107 const webrtc::RtpParameters& old_rtp_parameters,
108 const webrtc::RtpParameters& rtp_parameters) {
109 using webrtc::RTCErrorType;
110 if (rtp_parameters.encodings.size() != old_rtp_parameters.encodings.size()) {
111 LOG_AND_RETURN_ERROR(
112 RTCErrorType::INVALID_MODIFICATION,
113 "Attempted to set RtpParameters with different encoding count");
114 }
115 if (rtp_parameters.rtcp != old_rtp_parameters.rtcp) {
116 LOG_AND_RETURN_ERROR(
117 RTCErrorType::INVALID_MODIFICATION,
118 "Attempted to set RtpParameters with modified RTCP parameters");
119 }
120 if (rtp_parameters.header_extensions !=
121 old_rtp_parameters.header_extensions) {
122 LOG_AND_RETURN_ERROR(
123 RTCErrorType::INVALID_MODIFICATION,
124 "Attempted to set RtpParameters with modified header extensions");
125 }
Amit Hilbuch2297d332019-02-19 12:49:22 -0800126 if (!absl::c_equal(old_rtp_parameters.encodings, rtp_parameters.encodings,
127 [](const webrtc::RtpEncodingParameters& encoding1,
128 const webrtc::RtpEncodingParameters& encoding2) {
129 return encoding1.rid == encoding2.rid;
130 })) {
131 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
132 "Attempted to change RID values in the encodings.");
133 }
134 if (!absl::c_equal(old_rtp_parameters.encodings, rtp_parameters.encodings,
135 [](const webrtc::RtpEncodingParameters& encoding1,
136 const webrtc::RtpEncodingParameters& encoding2) {
137 return encoding1.ssrc == encoding2.ssrc;
138 })) {
139 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
140 "Attempted to set RtpParameters with modified SSRC");
Florent Castellic1a0bcb2019-01-29 14:26:48 +0100141 }
142
143 return CheckRtpParametersValues(rtp_parameters);
144}
145
Sebastian Janssonfa0aa392018-11-16 09:54:32 +0100146CompositeMediaEngine::CompositeMediaEngine(
147 std::unique_ptr<VoiceEngineInterface> voice_engine,
148 std::unique_ptr<VideoEngineInterface> video_engine)
149 : voice_engine_(std::move(voice_engine)),
150 video_engine_(std::move(video_engine)) {}
151
152CompositeMediaEngine::~CompositeMediaEngine() = default;
153
154bool CompositeMediaEngine::Init() {
155 voice().Init();
156 return true;
157}
158
Sebastian Janssonfa0aa392018-11-16 09:54:32 +0100159VoiceEngineInterface& CompositeMediaEngine::voice() {
160 return *voice_engine_.get();
161}
162
163VideoEngineInterface& CompositeMediaEngine::video() {
164 return *video_engine_.get();
165}
166
167const VoiceEngineInterface& CompositeMediaEngine::voice() const {
168 return *voice_engine_.get();
169}
170
171const VideoEngineInterface& CompositeMediaEngine::video() const {
172 return *video_engine_.get();
173}
174
Henrik Kjellanderb2528562016-03-29 17:47:19 +0200175}; // namespace cricket