blob: 167316041833941d9d5a0a19074e6284e5065915 [file] [log] [blame]
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
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.
9 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "test/encoder_settings.h"
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000011
perkjfa10b552016-10-02 23:45:26 -070012#include <algorithm>
13#include <string>
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000014
Mirko Bonadeid9708072019-01-25 20:26:48 +010015#include "api/scoped_refptr.h"
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "api/video_codecs/sdp_video_format.h"
17#include "call/rtp_config.h"
18#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/ref_counted_object.h"
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000020
21namespace webrtc {
22namespace test {
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000023
perkjfa10b552016-10-02 23:45:26 -070024const size_t DefaultVideoStreamFactory::kMaxNumberOfStreams;
25const int DefaultVideoStreamFactory::kMaxBitratePerStream[] = {150000, 450000,
26 1500000};
27const int DefaultVideoStreamFactory::kDefaultMinBitratePerStream[] = {
philipel5ef2bc12017-02-21 07:28:31 -080028 30000, 200000, 700000};
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000029
perkjfa10b552016-10-02 23:45:26 -070030// static
31std::vector<VideoStream> CreateVideoStreams(
32 int width,
33 int height,
34 const webrtc::VideoEncoderConfig& encoder_config) {
35 RTC_DCHECK(encoder_config.number_of_streams <=
36 DefaultVideoStreamFactory::kMaxNumberOfStreams);
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000037
perkjfa10b552016-10-02 23:45:26 -070038 std::vector<VideoStream> stream_settings(encoder_config.number_of_streams);
39 int bitrate_left_bps = encoder_config.max_bitrate_bps;
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000040
perkjfa10b552016-10-02 23:45:26 -070041 for (size_t i = 0; i < encoder_config.number_of_streams; ++i) {
Danil Chapovalov350531e2018-06-08 11:04:04 +000042 stream_settings[i].width =
43 (i + 1) * width / encoder_config.number_of_streams;
44 stream_settings[i].height =
45 (i + 1) * height / encoder_config.number_of_streams;
perkjfa10b552016-10-02 23:45:26 -070046 stream_settings[i].max_framerate = 30;
47 stream_settings[i].min_bitrate_bps =
48 DefaultVideoStreamFactory::kDefaultMinBitratePerStream[i];
philipel0676f222018-04-17 16:12:21 +020049
50 int target_bitrate_bps = -1;
51 int max_bitrate_bps = -1;
52 // Use configured values instead of default values if values has been
53 // configured.
54 if (i < encoder_config.simulcast_layers.size()) {
55 const VideoStream& stream = encoder_config.simulcast_layers[i];
56
57 max_bitrate_bps =
58 stream.max_bitrate_bps > 0
59 ? stream.max_bitrate_bps
60 : DefaultVideoStreamFactory::kMaxBitratePerStream[i];
61 max_bitrate_bps = std::min(bitrate_left_bps, max_bitrate_bps);
62
63 target_bitrate_bps =
64 stream.target_bitrate_bps > 0
65 ? stream.target_bitrate_bps
66 : DefaultVideoStreamFactory::kMaxBitratePerStream[i];
67 target_bitrate_bps = std::min(max_bitrate_bps, target_bitrate_bps);
Åsa Perssonf06bacc2018-10-12 11:07:20 +020068
Åsa Persson21740532019-04-15 14:00:30 +020069 if (stream.min_bitrate_bps > 0) {
70 RTC_DCHECK_LE(stream.min_bitrate_bps, target_bitrate_bps);
71 stream_settings[i].min_bitrate_bps = stream.min_bitrate_bps;
72 }
Åsa Perssonf06bacc2018-10-12 11:07:20 +020073 if (stream.max_framerate > 0) {
74 stream_settings[i].max_framerate = stream.max_framerate;
75 }
Åsa Perssond34597c2018-10-22 17:34:02 +020076 if (stream.num_temporal_layers) {
77 RTC_DCHECK_GE(*stream.num_temporal_layers, 1);
78 stream_settings[i].num_temporal_layers = stream.num_temporal_layers;
79 }
philipel0676f222018-04-17 16:12:21 +020080 } else {
81 max_bitrate_bps = std::min(
82 bitrate_left_bps, DefaultVideoStreamFactory::kMaxBitratePerStream[i]);
83 target_bitrate_bps = max_bitrate_bps;
84 }
85
86 RTC_DCHECK_NE(target_bitrate_bps, -1);
87 RTC_DCHECK_NE(max_bitrate_bps, -1);
88 stream_settings[i].target_bitrate_bps = target_bitrate_bps;
89 stream_settings[i].max_bitrate_bps = max_bitrate_bps;
perkjfa10b552016-10-02 23:45:26 -070090 stream_settings[i].max_qp = 56;
philipel0676f222018-04-17 16:12:21 +020091
Seth Hampsoncc7125f2018-02-02 08:46:16 -080092 if (i < encoder_config.simulcast_layers.size()) {
93 // Higher level controls are setting the active configuration for the
94 // VideoStream.
95 stream_settings[i].active = encoder_config.simulcast_layers[i].active;
96 } else {
97 stream_settings[i].active = true;
98 }
perkjfa10b552016-10-02 23:45:26 -070099 bitrate_left_bps -= stream_settings[i].target_bitrate_bps;
100 }
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000101
perkjfa10b552016-10-02 23:45:26 -0700102 stream_settings[encoder_config.number_of_streams - 1].max_bitrate_bps +=
103 bitrate_left_bps;
Seth Hampson24722b32017-12-22 09:36:42 -0800104 stream_settings[0].bitrate_priority = encoder_config.bitrate_priority;
perkjfa10b552016-10-02 23:45:26 -0700105
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000106 return stream_settings;
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000107}
108
perkjfa10b552016-10-02 23:45:26 -0700109DefaultVideoStreamFactory::DefaultVideoStreamFactory() {}
110
111std::vector<VideoStream> DefaultVideoStreamFactory::CreateEncoderStreams(
112 int width,
113 int height,
114 const webrtc::VideoEncoderConfig& encoder_config) {
115 return CreateVideoStreams(width, height, encoder_config);
116}
117
Niels Möller259a4972018-04-05 15:36:51 +0200118void FillEncoderConfiguration(VideoCodecType codec_type,
119 size_t num_streams,
perkjfa10b552016-10-02 23:45:26 -0700120 VideoEncoderConfig* configuration) {
121 RTC_DCHECK_LE(num_streams, DefaultVideoStreamFactory::kMaxNumberOfStreams);
122
Niels Möller259a4972018-04-05 15:36:51 +0200123 configuration->codec_type = codec_type;
perkjfa10b552016-10-02 23:45:26 -0700124 configuration->number_of_streams = num_streams;
125 configuration->video_stream_factory =
126 new rtc::RefCountedObject<DefaultVideoStreamFactory>();
127 configuration->max_bitrate_bps = 0;
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800128 configuration->simulcast_layers = std::vector<VideoStream>(num_streams);
perkjfa10b552016-10-02 23:45:26 -0700129 for (size_t i = 0; i < num_streams; ++i) {
130 configuration->max_bitrate_bps +=
131 DefaultVideoStreamFactory::kMaxBitratePerStream[i];
132 }
133}
134
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000135VideoReceiveStream::Decoder CreateMatchingDecoder(
Yves Gerey665174f2018-06-19 15:03:05 +0200136 int payload_type,
137 const std::string& payload_name) {
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000138 VideoReceiveStream::Decoder decoder;
Niels Möller259a4972018-04-05 15:36:51 +0200139 decoder.payload_type = payload_type;
Niels Möllercb7e1d22018-09-11 15:56:04 +0200140 decoder.video_format = SdpVideoFormat(payload_name);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000141 return decoder;
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000142}
Niels Möller259a4972018-04-05 15:36:51 +0200143
144VideoReceiveStream::Decoder CreateMatchingDecoder(
145 const VideoSendStream::Config& config) {
146 return CreateMatchingDecoder(config.rtp.payload_type,
147 config.rtp.payload_name);
148}
149
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000150} // namespace test
151} // namespace webrtc