Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/utility/simulcast_utility.h" |
Erik Språng | 8abd56c | 2018-10-01 18:47:03 +0200 | [diff] [blame] | 12 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 13 | #include <algorithm> |
Ilya Nikolaevskiy | dda5fdc | 2019-02-27 10:00:06 +0100 | [diff] [blame] | 14 | #include <cmath> |
| 15 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | uint32_t SimulcastUtility::SumStreamMaxBitrate(int streams, |
| 21 | const VideoCodec& codec) { |
| 22 | uint32_t bitrate_sum = 0; |
| 23 | for (int i = 0; i < streams; ++i) { |
| 24 | bitrate_sum += codec.simulcastStream[i].maxBitrate; |
| 25 | } |
| 26 | return bitrate_sum; |
| 27 | } |
| 28 | |
| 29 | int SimulcastUtility::NumberOfSimulcastStreams(const VideoCodec& codec) { |
| 30 | int streams = |
| 31 | codec.numberOfSimulcastStreams < 1 ? 1 : codec.numberOfSimulcastStreams; |
| 32 | uint32_t simulcast_max_bitrate = SumStreamMaxBitrate(streams, codec); |
| 33 | if (simulcast_max_bitrate == 0) { |
| 34 | streams = 1; |
| 35 | } |
| 36 | return streams; |
| 37 | } |
| 38 | |
Ilya Nikolaevskiy | dda5fdc | 2019-02-27 10:00:06 +0100 | [diff] [blame] | 39 | bool SimulcastUtility::ValidSimulcastParameters(const VideoCodec& codec, |
| 40 | int num_streams) { |
| 41 | // Check resolution. |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 42 | if (codec.width != codec.simulcastStream[num_streams - 1].width || |
| 43 | codec.height != codec.simulcastStream[num_streams - 1].height) { |
| 44 | return false; |
| 45 | } |
| 46 | for (int i = 0; i < num_streams; ++i) { |
| 47 | if (codec.width * codec.simulcastStream[i].height != |
| 48 | codec.height * codec.simulcastStream[i].width) { |
| 49 | return false; |
| 50 | } |
| 51 | } |
Mirta Dvornicic | 788f577 | 2019-02-13 17:35:40 +0100 | [diff] [blame] | 52 | if (codec.codecType == webrtc::kVideoCodecVP8) { |
| 53 | for (int i = 1; i < num_streams; ++i) { |
| 54 | if (codec.simulcastStream[i].width < codec.simulcastStream[i - 1].width) { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | } else { |
| 59 | // TODO(mirtad): H264 encoder implementation still assumes the default |
| 60 | // resolution downscaling is used. |
| 61 | for (int i = 1; i < num_streams; ++i) { |
| 62 | if (codec.simulcastStream[i].width != |
| 63 | codec.simulcastStream[i - 1].width * 2) { |
| 64 | return false; |
| 65 | } |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 66 | } |
| 67 | } |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 68 | |
Ilya Nikolaevskiy | dda5fdc | 2019-02-27 10:00:06 +0100 | [diff] [blame] | 69 | // Check frame-rate. |
| 70 | for (int i = 1; i < num_streams; ++i) { |
| 71 | if (fabs(codec.simulcastStream[i].maxFramerate - |
| 72 | codec.simulcastStream[i - 1].maxFramerate) > 1e-9) { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Check temporal layers. |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 78 | for (int i = 0; i < num_streams - 1; ++i) { |
| 79 | if (codec.simulcastStream[i].numberOfTemporalLayers != |
| 80 | codec.simulcastStream[i + 1].numberOfTemporalLayers) |
| 81 | return false; |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
Erik Språng | 8abd56c | 2018-10-01 18:47:03 +0200 | [diff] [blame] | 86 | bool SimulcastUtility::IsConferenceModeScreenshare(const VideoCodec& codec) { |
Florent Castelli | d351101 | 2020-08-04 11:40:23 +0200 | [diff] [blame] | 87 | return codec.mode == VideoCodecMode::kScreensharing && |
| 88 | codec.legacy_conference_mode; |
Erik Språng | 8abd56c | 2018-10-01 18:47:03 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int SimulcastUtility::NumberOfTemporalLayers(const VideoCodec& codec, |
| 92 | int spatial_id) { |
| 93 | uint8_t num_temporal_layers = |
| 94 | std::max<uint8_t>(1, codec.VP8().numberOfTemporalLayers); |
| 95 | if (codec.numberOfSimulcastStreams > 0) { |
| 96 | RTC_DCHECK_LT(spatial_id, codec.numberOfSimulcastStreams); |
| 97 | num_temporal_layers = |
| 98 | std::max(num_temporal_layers, |
| 99 | codec.simulcastStream[spatial_id].numberOfTemporalLayers); |
| 100 | } |
| 101 | return num_temporal_layers; |
| 102 | } |
| 103 | |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 104 | } // namespace webrtc |