Niels Möller | 213618e | 2018-07-24 07:29:58 | [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 Oreland | 6c2dae2 | 2022-09-29 08:28:24 | [diff] [blame] | 11 | #ifndef VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_ |
| 12 | #define VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_ |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 13 | |
Erik Språng | e2fd86a7 | 2018-10-24 09:32:39 | [diff] [blame] | 14 | #include <string> |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Evan Shrubsole | dff7925 | 2020-04-16 09:34:32 | [diff] [blame] | 17 | #include "api/video/video_adaptation_counters.h" |
Evan Shrubsole | ce0a11d | 2020-04-16 09:36:55 | [diff] [blame] | 18 | #include "api/video/video_adaptation_reason.h" |
Evan Shrubsole | cc62b16 | 2019-09-09 09:26:45 | [diff] [blame] | 19 | #include "api/video/video_bitrate_allocation.h" |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 20 | #include "api/video_codecs/video_encoder.h" |
Jonas Oreland | 6c2dae2 | 2022-09-29 08:28:24 | [diff] [blame] | 21 | #include "video/config/video_encoder_config.h" |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // TODO(nisse): Used for the OnSendEncodedImage callback below. The callback |
| 26 | // wants metadata such as size, encode timing, qp, but doesn't need actual |
| 27 | // encoded data. So use some other type to represent that. |
| 28 | class EncodedImage; |
| 29 | |
Evan Shrubsole | 09da10e | 2022-10-14 14:38:31 | [diff] [blame] | 30 | struct EncoderImplementation { |
| 31 | const std::string& name; |
| 32 | bool is_hardware_accelerated; |
| 33 | }; |
| 34 | |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 35 | // Broken out into a base class, with public inheritance below, only to ease |
| 36 | // unit testing of the internal class OveruseFrameDetector. |
| 37 | class CpuOveruseMetricsObserver { |
| 38 | public: |
| 39 | virtual ~CpuOveruseMetricsObserver() = default; |
| 40 | virtual void OnEncodedFrameTimeMeasured(int encode_duration_ms, |
| 41 | int encode_usage_percent) = 0; |
| 42 | }; |
| 43 | |
| 44 | class VideoStreamEncoderObserver : public CpuOveruseMetricsObserver { |
| 45 | public: |
Evan Shrubsole | dff7925 | 2020-04-16 09:34:32 | [diff] [blame] | 46 | struct AdaptationSettings { |
| 47 | AdaptationSettings() |
| 48 | : resolution_scaling_enabled(false), framerate_scaling_enabled(false) {} |
| 49 | |
| 50 | AdaptationSettings(bool resolution_scaling_enabled, |
| 51 | bool framerate_scaling_enabled) |
| 52 | : resolution_scaling_enabled(resolution_scaling_enabled), |
| 53 | framerate_scaling_enabled(framerate_scaling_enabled) {} |
| 54 | |
| 55 | bool resolution_scaling_enabled; |
| 56 | bool framerate_scaling_enabled; |
| 57 | }; |
| 58 | |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 59 | enum class DropReason { |
| 60 | kSource, |
Markus Handell | 254e230 | 2023-11-23 12:40:49 | [diff] [blame] | 61 | kBadTimestamp, |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 62 | kEncoderQueue, |
| 63 | kEncoder, |
Ying Wang | 9b881ab | 2020-02-07 13:29:32 | [diff] [blame] | 64 | kMediaOptimization, |
| 65 | kCongestionWindow |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 66 | }; |
| 67 | |
Mirko Bonadei | 8fdcac3 | 2018-08-28 14:30:18 | [diff] [blame] | 68 | ~VideoStreamEncoderObserver() override = default; |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 69 | |
| 70 | virtual void OnIncomingFrame(int width, int height) = 0; |
| 71 | |
Niels Möller | 6939f63 | 2022-07-05 06:55:19 | [diff] [blame] | 72 | // TODO(bugs.webrtc.org/8504): Merge into one callback per encoded frame. |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 73 | using CpuOveruseMetricsObserver::OnEncodedFrameTimeMeasured; |
| 74 | virtual void OnSendEncodedImage(const EncodedImage& encoded_image, |
| 75 | const CodecSpecificInfo* codec_info) = 0; |
| 76 | |
Erik Språng | e2fd86a7 | 2018-10-24 09:32:39 | [diff] [blame] | 77 | virtual void OnEncoderImplementationChanged( |
Evan Shrubsole | 09da10e | 2022-10-14 14:38:31 | [diff] [blame] | 78 | EncoderImplementation implementation) = 0; |
Erik Språng | e2fd86a7 | 2018-10-24 09:32:39 | [diff] [blame] | 79 | |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 80 | virtual void OnFrameDropped(DropReason reason) = 0; |
| 81 | |
| 82 | // Used to indicate change in content type, which may require a change in |
| 83 | // how stats are collected and set the configured preferred media bitrate. |
| 84 | virtual void OnEncoderReconfigured( |
| 85 | const VideoEncoderConfig& encoder_config, |
| 86 | const std::vector<VideoStream>& streams) = 0; |
| 87 | |
Evan Shrubsole | dff7925 | 2020-04-16 09:34:32 | [diff] [blame] | 88 | virtual void OnAdaptationChanged( |
Evan Shrubsole | ce0a11d | 2020-04-16 09:36:55 | [diff] [blame] | 89 | VideoAdaptationReason reason, |
Evan Shrubsole | dff7925 | 2020-04-16 09:34:32 | [diff] [blame] | 90 | const VideoAdaptationCounters& cpu_steps, |
| 91 | const VideoAdaptationCounters& quality_steps) = 0; |
| 92 | virtual void ClearAdaptationStats() = 0; |
| 93 | |
| 94 | virtual void UpdateAdaptationSettings( |
| 95 | AdaptationSettings cpu_settings, |
| 96 | AdaptationSettings quality_settings) = 0; |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 97 | virtual void OnMinPixelLimitReached() = 0; |
| 98 | virtual void OnInitialQualityResolutionAdaptDown() = 0; |
| 99 | |
| 100 | virtual void OnSuspendChange(bool is_suspended) = 0; |
| 101 | |
Evan Shrubsole | cc62b16 | 2019-09-09 09:26:45 | [diff] [blame] | 102 | virtual void OnBitrateAllocationUpdated( |
| 103 | const VideoCodec& codec, |
| 104 | const VideoBitrateAllocation& allocation) {} |
| 105 | |
Ilya Nikolaevskiy | eac08bf | 2020-03-10 09:50:26 | [diff] [blame] | 106 | // Informes observer if an internal encoder scaler has reduced video |
Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 107 | // resolution or not. `is_scaled` is a flag indicating if the video is scaled |
Ilya Nikolaevskiy | eac08bf | 2020-03-10 09:50:26 | [diff] [blame] | 108 | // down. |
| 109 | virtual void OnEncoderInternalScalerUpdate(bool is_scaled) {} |
| 110 | |
Niels Möller | 6939f63 | 2022-07-05 06:55:19 | [diff] [blame] | 111 | // TODO(bugs.webrtc.org/14246): VideoStreamEncoder wants to query the stats, |
| 112 | // which makes this not a pure observer. GetInputFrameRate is needed for the |
| 113 | // cpu adaptation, so can be deleted if that responsibility is moved out to a |
| 114 | // VideoStreamAdaptor class. |
Niels Möller | 213618e | 2018-07-24 07:29:58 | [diff] [blame] | 115 | virtual int GetInputFrameRate() const = 0; |
| 116 | }; |
| 117 | |
| 118 | } // namespace webrtc |
Jonas Oreland | 6c2dae2 | 2022-09-29 08:28:24 | [diff] [blame] | 119 | |
| 120 | #endif // VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_ |