blob: bf5e959f81ad8f0809edfe2c20d36ba86c8921b3 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <cstdint>
16#include <string>
Sebastian Janssonfa0aa392018-11-16 09:54:32 +010017#include <utility>
18
Amit Hilbuch2297d332019-02-19 12:49:22 -080019#include "absl/algorithm/container.h"
Åsa Persson23eba222018-10-02 14:47:06 +020020#include "api/video/video_bitrate_allocation.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/string_encode.h"
Åsa Persson23eba222018-10-02 14:47:06 +020023
skvladdc1c62c2016-03-16 19:07:43 -070024namespace cricket {
25
Paulina Hensman11b34f42018-04-09 14:24:52 +020026RtpCapabilities::RtpCapabilities() = default;
27RtpCapabilities::~RtpCapabilities() = default;
28
skvladdc1c62c2016-03-16 19:07:43 -070029webrtc::RtpParameters CreateRtpParametersWithOneEncoding() {
30 webrtc::RtpParameters parameters;
31 webrtc::RtpEncodingParameters encoding;
32 parameters.encodings.push_back(encoding);
33 return parameters;
34}
35
Zach Stein3ca452b2018-01-18 10:01:24 -080036webrtc::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 Hilbuch2297d332019-02-19 12:49:22 -080045
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 Stein3ca452b2018-01-18 10:01:24 -080052 webrtc::RtpParameters parameters;
53 parameters.encodings = encodings;
Florent Castellidacec712018-05-24 16:24:21 +020054 parameters.rtcp.cname = sp.cname;
Zach Stein3ca452b2018-01-18 10:01:24 -080055 return parameters;
56}
57
Florent Castellic1a0bcb2019-01-29 14:26:48 +010058webrtc::RTCError CheckRtpParametersValues(
Florent Castelli892acf02018-10-01 22:47:20 +020059 const webrtc::RtpParameters& rtp_parameters) {
60 using webrtc::RTCErrorType;
Florent Castelli892acf02018-10-01 22:47:20 +020061
62 for (size_t i = 0; i < rtp_parameters.encodings.size(); ++i) {
Florent Castelli892acf02018-10-01 22:47:20 +020063 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 Castellic1a0bcb2019-01-29 14:26:48 +010068 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 Castelli892acf02018-10-01 22:47:20 +020075 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 Persson23eba222018-10-02 14:47:06 +020084 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 Castelli892acf02018-10-01 22:47:20 +0200102 }
Florent Castellic1a0bcb2019-01-29 14:26:48 +0100103
Florent Castelli892acf02018-10-01 22:47:20 +0200104 return webrtc::RTCError::OK();
105}
106
Florent Castellic1a0bcb2019-01-29 14:26:48 +0100107webrtc::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 Hilbuch2297d332019-02-19 12:49:22 -0800127 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 Castellic1a0bcb2019-01-29 14:26:48 +0100142 }
143
144 return CheckRtpParametersValues(rtp_parameters);
145}
146
Sebastian Janssonfa0aa392018-11-16 09:54:32 +0100147CompositeMediaEngine::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
153CompositeMediaEngine::~CompositeMediaEngine() = default;
154
155bool CompositeMediaEngine::Init() {
156 voice().Init();
157 return true;
158}
159
Sebastian Janssonfa0aa392018-11-16 09:54:32 +0100160VoiceEngineInterface& CompositeMediaEngine::voice() {
161 return *voice_engine_.get();
162}
163
164VideoEngineInterface& CompositeMediaEngine::video() {
165 return *video_engine_.get();
166}
167
168const VoiceEngineInterface& CompositeMediaEngine::voice() const {
169 return *voice_engine_.get();
170}
171
172const VideoEngineInterface& CompositeMediaEngine::video() const {
173 return *video_engine_.get();
174}
175
Nico Weber22f99252019-02-20 10:13:16 -0500176} // namespace cricket