xref: /aosp_15_r20/external/webrtc/video/video_stream_encoder_observer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_
12*d9f75844SAndroid Build Coastguard Worker #define VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <string>
15*d9f75844SAndroid Build Coastguard Worker #include <vector>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_adaptation_counters.h"
18*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_adaptation_reason.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_bitrate_allocation.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/video_codecs/video_encoder.h"
21*d9f75844SAndroid Build Coastguard Worker #include "video/config/video_encoder_config.h"
22*d9f75844SAndroid Build Coastguard Worker 
23*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker // TODO(nisse): Used for the OnSendEncodedImage callback below. The callback
26*d9f75844SAndroid Build Coastguard Worker // wants metadata such as size, encode timing, qp, but doesn't need actual
27*d9f75844SAndroid Build Coastguard Worker // encoded data. So use some other type to represent that.
28*d9f75844SAndroid Build Coastguard Worker class EncodedImage;
29*d9f75844SAndroid Build Coastguard Worker 
30*d9f75844SAndroid Build Coastguard Worker struct EncoderImplementation {
31*d9f75844SAndroid Build Coastguard Worker   const std::string& name;
32*d9f75844SAndroid Build Coastguard Worker   bool is_hardware_accelerated;
33*d9f75844SAndroid Build Coastguard Worker };
34*d9f75844SAndroid Build Coastguard Worker 
35*d9f75844SAndroid Build Coastguard Worker // Broken out into a base class, with public inheritance below, only to ease
36*d9f75844SAndroid Build Coastguard Worker // unit testing of the internal class OveruseFrameDetector.
37*d9f75844SAndroid Build Coastguard Worker class CpuOveruseMetricsObserver {
38*d9f75844SAndroid Build Coastguard Worker  public:
39*d9f75844SAndroid Build Coastguard Worker   virtual ~CpuOveruseMetricsObserver() = default;
40*d9f75844SAndroid Build Coastguard Worker   virtual void OnEncodedFrameTimeMeasured(int encode_duration_ms,
41*d9f75844SAndroid Build Coastguard Worker                                           int encode_usage_percent) = 0;
42*d9f75844SAndroid Build Coastguard Worker };
43*d9f75844SAndroid Build Coastguard Worker 
44*d9f75844SAndroid Build Coastguard Worker class VideoStreamEncoderObserver : public CpuOveruseMetricsObserver {
45*d9f75844SAndroid Build Coastguard Worker  public:
46*d9f75844SAndroid Build Coastguard Worker   struct AdaptationSettings {
AdaptationSettingsAdaptationSettings47*d9f75844SAndroid Build Coastguard Worker     AdaptationSettings()
48*d9f75844SAndroid Build Coastguard Worker         : resolution_scaling_enabled(false), framerate_scaling_enabled(false) {}
49*d9f75844SAndroid Build Coastguard Worker 
AdaptationSettingsAdaptationSettings50*d9f75844SAndroid Build Coastguard Worker     AdaptationSettings(bool resolution_scaling_enabled,
51*d9f75844SAndroid Build Coastguard Worker                        bool framerate_scaling_enabled)
52*d9f75844SAndroid Build Coastguard Worker         : resolution_scaling_enabled(resolution_scaling_enabled),
53*d9f75844SAndroid Build Coastguard Worker           framerate_scaling_enabled(framerate_scaling_enabled) {}
54*d9f75844SAndroid Build Coastguard Worker 
55*d9f75844SAndroid Build Coastguard Worker     bool resolution_scaling_enabled;
56*d9f75844SAndroid Build Coastguard Worker     bool framerate_scaling_enabled;
57*d9f75844SAndroid Build Coastguard Worker   };
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   enum class DropReason {
60*d9f75844SAndroid Build Coastguard Worker     kSource,
61*d9f75844SAndroid Build Coastguard Worker     kEncoderQueue,
62*d9f75844SAndroid Build Coastguard Worker     kEncoder,
63*d9f75844SAndroid Build Coastguard Worker     kMediaOptimization,
64*d9f75844SAndroid Build Coastguard Worker     kCongestionWindow
65*d9f75844SAndroid Build Coastguard Worker   };
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker   ~VideoStreamEncoderObserver() override = default;
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker   virtual void OnIncomingFrame(int width, int height) = 0;
70*d9f75844SAndroid Build Coastguard Worker 
71*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/8504): Merge into one callback per encoded frame.
72*d9f75844SAndroid Build Coastguard Worker   using CpuOveruseMetricsObserver::OnEncodedFrameTimeMeasured;
73*d9f75844SAndroid Build Coastguard Worker   virtual void OnSendEncodedImage(const EncodedImage& encoded_image,
74*d9f75844SAndroid Build Coastguard Worker                                   const CodecSpecificInfo* codec_info) = 0;
75*d9f75844SAndroid Build Coastguard Worker 
76*d9f75844SAndroid Build Coastguard Worker   virtual void OnEncoderImplementationChanged(
77*d9f75844SAndroid Build Coastguard Worker       EncoderImplementation implementation) = 0;
78*d9f75844SAndroid Build Coastguard Worker 
79*d9f75844SAndroid Build Coastguard Worker   virtual void OnFrameDropped(DropReason reason) = 0;
80*d9f75844SAndroid Build Coastguard Worker 
81*d9f75844SAndroid Build Coastguard Worker   // Used to indicate change in content type, which may require a change in
82*d9f75844SAndroid Build Coastguard Worker   // how stats are collected and set the configured preferred media bitrate.
83*d9f75844SAndroid Build Coastguard Worker   virtual void OnEncoderReconfigured(
84*d9f75844SAndroid Build Coastguard Worker       const VideoEncoderConfig& encoder_config,
85*d9f75844SAndroid Build Coastguard Worker       const std::vector<VideoStream>& streams) = 0;
86*d9f75844SAndroid Build Coastguard Worker 
87*d9f75844SAndroid Build Coastguard Worker   virtual void OnAdaptationChanged(
88*d9f75844SAndroid Build Coastguard Worker       VideoAdaptationReason reason,
89*d9f75844SAndroid Build Coastguard Worker       const VideoAdaptationCounters& cpu_steps,
90*d9f75844SAndroid Build Coastguard Worker       const VideoAdaptationCounters& quality_steps) = 0;
91*d9f75844SAndroid Build Coastguard Worker   virtual void ClearAdaptationStats() = 0;
92*d9f75844SAndroid Build Coastguard Worker 
93*d9f75844SAndroid Build Coastguard Worker   virtual void UpdateAdaptationSettings(
94*d9f75844SAndroid Build Coastguard Worker       AdaptationSettings cpu_settings,
95*d9f75844SAndroid Build Coastguard Worker       AdaptationSettings quality_settings) = 0;
96*d9f75844SAndroid Build Coastguard Worker   virtual void OnMinPixelLimitReached() = 0;
97*d9f75844SAndroid Build Coastguard Worker   virtual void OnInitialQualityResolutionAdaptDown() = 0;
98*d9f75844SAndroid Build Coastguard Worker 
99*d9f75844SAndroid Build Coastguard Worker   virtual void OnSuspendChange(bool is_suspended) = 0;
100*d9f75844SAndroid Build Coastguard Worker 
OnBitrateAllocationUpdated(const VideoCodec & codec,const VideoBitrateAllocation & allocation)101*d9f75844SAndroid Build Coastguard Worker   virtual void OnBitrateAllocationUpdated(
102*d9f75844SAndroid Build Coastguard Worker       const VideoCodec& codec,
103*d9f75844SAndroid Build Coastguard Worker       const VideoBitrateAllocation& allocation) {}
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker   // Informes observer if an internal encoder scaler has reduced video
106*d9f75844SAndroid Build Coastguard Worker   // resolution or not. `is_scaled` is a flag indicating if the video is scaled
107*d9f75844SAndroid Build Coastguard Worker   // down.
OnEncoderInternalScalerUpdate(bool is_scaled)108*d9f75844SAndroid Build Coastguard Worker   virtual void OnEncoderInternalScalerUpdate(bool is_scaled) {}
109*d9f75844SAndroid Build Coastguard Worker 
110*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/14246): VideoStreamEncoder wants to query the stats,
111*d9f75844SAndroid Build Coastguard Worker   // which makes this not a pure observer. GetInputFrameRate is needed for the
112*d9f75844SAndroid Build Coastguard Worker   // cpu adaptation, so can be deleted if that responsibility is moved out to a
113*d9f75844SAndroid Build Coastguard Worker   // VideoStreamAdaptor class.
114*d9f75844SAndroid Build Coastguard Worker   virtual int GetInputFrameRate() const = 0;
115*d9f75844SAndroid Build Coastguard Worker };
116*d9f75844SAndroid Build Coastguard Worker 
117*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
118*d9f75844SAndroid Build Coastguard Worker 
119*d9f75844SAndroid Build Coastguard Worker #endif  // VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_
120