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 API_VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_ 12 #define API_VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_ 13 14 #include <string> 15 #include <vector> 16 17 #include "absl/types/optional.h" 18 #include "api/video/video_adaptation_counters.h" 19 #include "api/video/video_adaptation_reason.h" 20 #include "api/video/video_bitrate_allocation.h" 21 #include "api/video/video_codec_constants.h" 22 #include "api/video_codecs/video_encoder.h" 23 #include "api/video_codecs/video_encoder_config.h" 24 25 namespace webrtc { 26 27 // TODO(nisse): Used for the OnSendEncodedImage callback below. The callback 28 // wants metadata such as size, encode timing, qp, but doesn't need actual 29 // encoded data. So use some other type to represent that. 30 class EncodedImage; 31 32 // Broken out into a base class, with public inheritance below, only to ease 33 // unit testing of the internal class OveruseFrameDetector. 34 class CpuOveruseMetricsObserver { 35 public: 36 virtual ~CpuOveruseMetricsObserver() = default; 37 virtual void OnEncodedFrameTimeMeasured(int encode_duration_ms, 38 int encode_usage_percent) = 0; 39 }; 40 41 class VideoStreamEncoderObserver : public CpuOveruseMetricsObserver { 42 public: 43 struct AdaptationSettings { AdaptationSettingsAdaptationSettings44 AdaptationSettings() 45 : resolution_scaling_enabled(false), framerate_scaling_enabled(false) {} 46 AdaptationSettingsAdaptationSettings47 AdaptationSettings(bool resolution_scaling_enabled, 48 bool framerate_scaling_enabled) 49 : resolution_scaling_enabled(resolution_scaling_enabled), 50 framerate_scaling_enabled(framerate_scaling_enabled) {} 51 52 bool resolution_scaling_enabled; 53 bool framerate_scaling_enabled; 54 }; 55 56 // TODO(nisse): Duplicates enum EncodedImageCallback::DropReason. 57 enum class DropReason { 58 kSource, 59 kEncoderQueue, 60 kEncoder, 61 kMediaOptimization, 62 kCongestionWindow 63 }; 64 65 ~VideoStreamEncoderObserver() override = default; 66 67 virtual void OnIncomingFrame(int width, int height) = 0; 68 69 // TODO(nisse): Merge into one callback per encoded frame. 70 using CpuOveruseMetricsObserver::OnEncodedFrameTimeMeasured; 71 virtual void OnSendEncodedImage(const EncodedImage& encoded_image, 72 const CodecSpecificInfo* codec_info) = 0; 73 74 virtual void OnEncoderImplementationChanged( 75 const std::string& implementation_name) = 0; 76 77 virtual void OnFrameDropped(DropReason reason) = 0; 78 79 // Used to indicate change in content type, which may require a change in 80 // how stats are collected and set the configured preferred media bitrate. 81 virtual void OnEncoderReconfigured( 82 const VideoEncoderConfig& encoder_config, 83 const std::vector<VideoStream>& streams) = 0; 84 85 virtual void OnAdaptationChanged( 86 VideoAdaptationReason reason, 87 const VideoAdaptationCounters& cpu_steps, 88 const VideoAdaptationCounters& quality_steps) = 0; 89 virtual void ClearAdaptationStats() = 0; 90 91 virtual void UpdateAdaptationSettings( 92 AdaptationSettings cpu_settings, 93 AdaptationSettings quality_settings) = 0; 94 virtual void OnMinPixelLimitReached() = 0; 95 virtual void OnInitialQualityResolutionAdaptDown() = 0; 96 97 virtual void OnSuspendChange(bool is_suspended) = 0; 98 OnBitrateAllocationUpdated(const VideoCodec & codec,const VideoBitrateAllocation & allocation)99 virtual void OnBitrateAllocationUpdated( 100 const VideoCodec& codec, 101 const VideoBitrateAllocation& allocation) {} 102 103 // Informes observer if an internal encoder scaler has reduced video 104 // resolution or not. |is_scaled| is a flag indicating if the video is scaled 105 // down. OnEncoderInternalScalerUpdate(bool is_scaled)106 virtual void OnEncoderInternalScalerUpdate(bool is_scaled) {} 107 108 // TODO(nisse): VideoStreamEncoder wants to query the stats, which makes this 109 // not a pure observer. GetInputFrameRate is needed for the cpu adaptation, so 110 // can be deleted if that responsibility is moved out to a VideoStreamAdaptor 111 // class. 112 virtual int GetInputFrameRate() const = 0; 113 }; 114 115 } // namespace webrtc 116 #endif // API_VIDEO_VIDEO_STREAM_ENCODER_OBSERVER_H_ 117