• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #ifndef VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_
12 #define VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_
13 
14 #include <string>
15 #include <vector>
16 
17 #include "api/video/video_adaptation_counters.h"
18 #include "api/video/video_adaptation_reason.h"
19 #include "api/video/video_bitrate_allocation.h"
20 #include "api/video_codecs/video_encoder.h"
21 #include "video/config/video_encoder_config.h"
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 
30 struct EncoderImplementation {
31   const std::string& name;
32   bool is_hardware_accelerated;
33 };
34 
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:
46   struct AdaptationSettings {
AdaptationSettingsAdaptationSettings47     AdaptationSettings()
48         : resolution_scaling_enabled(false), framerate_scaling_enabled(false) {}
49 
AdaptationSettingsAdaptationSettings50     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 
59   enum class DropReason {
60     kSource,
61     kEncoderQueue,
62     kEncoder,
63     kMediaOptimization,
64     kCongestionWindow
65   };
66 
67   ~VideoStreamEncoderObserver() override = default;
68 
69   virtual void OnIncomingFrame(int width, int height) = 0;
70 
71   // TODO(bugs.webrtc.org/8504): Merge into one callback per encoded frame.
72   using CpuOveruseMetricsObserver::OnEncodedFrameTimeMeasured;
73   virtual void OnSendEncodedImage(const EncodedImage& encoded_image,
74                                   const CodecSpecificInfo* codec_info) = 0;
75 
76   virtual void OnEncoderImplementationChanged(
77       EncoderImplementation implementation) = 0;
78 
79   virtual void OnFrameDropped(DropReason reason) = 0;
80 
81   // Used to indicate change in content type, which may require a change in
82   // how stats are collected and set the configured preferred media bitrate.
83   virtual void OnEncoderReconfigured(
84       const VideoEncoderConfig& encoder_config,
85       const std::vector<VideoStream>& streams) = 0;
86 
87   virtual void OnAdaptationChanged(
88       VideoAdaptationReason reason,
89       const VideoAdaptationCounters& cpu_steps,
90       const VideoAdaptationCounters& quality_steps) = 0;
91   virtual void ClearAdaptationStats() = 0;
92 
93   virtual void UpdateAdaptationSettings(
94       AdaptationSettings cpu_settings,
95       AdaptationSettings quality_settings) = 0;
96   virtual void OnMinPixelLimitReached() = 0;
97   virtual void OnInitialQualityResolutionAdaptDown() = 0;
98 
99   virtual void OnSuspendChange(bool is_suspended) = 0;
100 
OnBitrateAllocationUpdated(const VideoCodec & codec,const VideoBitrateAllocation & allocation)101   virtual void OnBitrateAllocationUpdated(
102       const VideoCodec& codec,
103       const VideoBitrateAllocation& allocation) {}
104 
105   // Informes observer if an internal encoder scaler has reduced video
106   // resolution or not. `is_scaled` is a flag indicating if the video is scaled
107   // down.
OnEncoderInternalScalerUpdate(bool is_scaled)108   virtual void OnEncoderInternalScalerUpdate(bool is_scaled) {}
109 
110   // TODO(bugs.webrtc.org/14246): VideoStreamEncoder wants to query the stats,
111   // which makes this not a pure observer. GetInputFrameRate is needed for the
112   // cpu adaptation, so can be deleted if that responsibility is moved out to a
113   // VideoStreamAdaptor class.
114   virtual int GetInputFrameRate() const = 0;
115 };
116 
117 }  // namespace webrtc
118 
119 #endif  // VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_
120