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 | |
Erik Språng | 8abd56c | 2018-10-01 18:47:03 +0200 | [diff] [blame^] | 11 | #include <algorithm> |
| 12 | |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 13 | #include "modules/video_coding/utility/simulcast_utility.h" |
| 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | uint32_t SimulcastUtility::SumStreamMaxBitrate(int streams, |
| 18 | const VideoCodec& codec) { |
| 19 | uint32_t bitrate_sum = 0; |
| 20 | for (int i = 0; i < streams; ++i) { |
| 21 | bitrate_sum += codec.simulcastStream[i].maxBitrate; |
| 22 | } |
| 23 | return bitrate_sum; |
| 24 | } |
| 25 | |
| 26 | int SimulcastUtility::NumberOfSimulcastStreams(const VideoCodec& codec) { |
| 27 | int streams = |
| 28 | codec.numberOfSimulcastStreams < 1 ? 1 : codec.numberOfSimulcastStreams; |
| 29 | uint32_t simulcast_max_bitrate = SumStreamMaxBitrate(streams, codec); |
| 30 | if (simulcast_max_bitrate == 0) { |
| 31 | streams = 1; |
| 32 | } |
| 33 | return streams; |
| 34 | } |
| 35 | |
| 36 | bool SimulcastUtility::ValidSimulcastResolutions(const VideoCodec& codec, |
| 37 | int num_streams) { |
| 38 | if (codec.width != codec.simulcastStream[num_streams - 1].width || |
| 39 | codec.height != codec.simulcastStream[num_streams - 1].height) { |
| 40 | return false; |
| 41 | } |
| 42 | for (int i = 0; i < num_streams; ++i) { |
| 43 | if (codec.width * codec.simulcastStream[i].height != |
| 44 | codec.height * codec.simulcastStream[i].width) { |
| 45 | return false; |
| 46 | } |
| 47 | } |
| 48 | for (int i = 1; i < num_streams; ++i) { |
| 49 | if (codec.simulcastStream[i].width != |
| 50 | codec.simulcastStream[i - 1].width * 2) { |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool SimulcastUtility::ValidSimulcastTemporalLayers(const VideoCodec& codec, |
| 58 | int num_streams) { |
| 59 | for (int i = 0; i < num_streams - 1; ++i) { |
| 60 | if (codec.simulcastStream[i].numberOfTemporalLayers != |
| 61 | codec.simulcastStream[i + 1].numberOfTemporalLayers) |
| 62 | return false; |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
Erik Språng | 8abd56c | 2018-10-01 18:47:03 +0200 | [diff] [blame^] | 67 | bool SimulcastUtility::IsConferenceModeScreenshare(const VideoCodec& codec) { |
| 68 | if (codec.mode != VideoCodecMode::kScreensharing || |
| 69 | NumberOfTemporalLayers(codec, 0) != 2) { |
| 70 | return false; |
| 71 | } |
| 72 | // Fixed default bitrates for legacy screenshare layers mode. |
| 73 | return (codec.numberOfSimulcastStreams == 0 && codec.maxBitrate == 1000) || |
| 74 | (codec.numberOfSimulcastStreams >= 1 && |
| 75 | codec.simulcastStream[0].maxBitrate == 1000 && |
| 76 | codec.simulcastStream[0].targetBitrate == 200); |
| 77 | } |
| 78 | |
| 79 | int SimulcastUtility::NumberOfTemporalLayers(const VideoCodec& codec, |
| 80 | int spatial_id) { |
| 81 | uint8_t num_temporal_layers = |
| 82 | std::max<uint8_t>(1, codec.VP8().numberOfTemporalLayers); |
| 83 | if (codec.numberOfSimulcastStreams > 0) { |
| 84 | RTC_DCHECK_LT(spatial_id, codec.numberOfSimulcastStreams); |
| 85 | num_temporal_layers = |
| 86 | std::max(num_temporal_layers, |
| 87 | codec.simulcastStream[spatial_id].numberOfTemporalLayers); |
| 88 | } |
| 89 | return num_temporal_layers; |
| 90 | } |
| 91 | |
Sergio Garcia Murillo | 43800f9 | 2018-06-21 16:16:38 +0200 | [diff] [blame] | 92 | } // namespace webrtc |