video_stream_encoder_observer.h 4.2 KB

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