video_quality_observer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 VIDEO_VIDEO_QUALITY_OBSERVER_H_
  11. #define VIDEO_VIDEO_QUALITY_OBSERVER_H_
  12. #include <stdint.h>
  13. #include <set>
  14. #include <vector>
  15. #include "absl/types/optional.h"
  16. #include "api/video/video_codec_type.h"
  17. #include "api/video/video_content_type.h"
  18. #include "api/video/video_frame.h"
  19. #include "rtc_base/numerics/moving_average.h"
  20. #include "rtc_base/numerics/sample_counter.h"
  21. namespace webrtc {
  22. // Calculates spatial and temporal quality metrics and reports them to UMA
  23. // stats.
  24. class VideoQualityObserver {
  25. public:
  26. // Use either VideoQualityObserver::kBlockyQpThresholdVp8 or
  27. // VideoQualityObserver::kBlockyQpThresholdVp9.
  28. explicit VideoQualityObserver(VideoContentType content_type);
  29. ~VideoQualityObserver() = default;
  30. void OnDecodedFrame(const VideoFrame& frame,
  31. absl::optional<uint8_t> qp,
  32. VideoCodecType codec);
  33. void OnRenderedFrame(const VideoFrame& frame, int64_t now_ms);
  34. void OnStreamInactive();
  35. uint32_t NumFreezes() const;
  36. uint32_t NumPauses() const;
  37. uint32_t TotalFreezesDurationMs() const;
  38. uint32_t TotalPausesDurationMs() const;
  39. uint32_t TotalFramesDurationMs() const;
  40. double SumSquaredFrameDurationsSec() const;
  41. void UpdateHistograms();
  42. static const uint32_t kMinFrameSamplesToDetectFreeze;
  43. static const uint32_t kMinIncreaseForFreezeMs;
  44. static const uint32_t kAvgInterframeDelaysWindowSizeFrames;
  45. private:
  46. enum Resolution {
  47. Low = 0,
  48. Medium = 1,
  49. High = 2,
  50. };
  51. int64_t last_frame_rendered_ms_;
  52. int64_t num_frames_rendered_;
  53. int64_t first_frame_rendered_ms_;
  54. int64_t last_frame_pixels_;
  55. bool is_last_frame_blocky_;
  56. // Decoded timestamp of the last delayed frame.
  57. int64_t last_unfreeze_time_ms_;
  58. rtc::MovingAverage render_interframe_delays_;
  59. double sum_squared_interframe_delays_secs_;
  60. // An inter-frame delay is counted as a freeze if it's significantly longer
  61. // than average inter-frame delay.
  62. rtc::SampleCounter freezes_durations_;
  63. rtc::SampleCounter pauses_durations_;
  64. // Time between freezes.
  65. rtc::SampleCounter smooth_playback_durations_;
  66. // Counters for time spent in different resolutions. Time between each two
  67. // Consecutive frames is counted to bin corresponding to the first frame
  68. // resolution.
  69. std::vector<int64_t> time_in_resolution_ms_;
  70. // Resolution of the last decoded frame. Resolution enum is used as an index.
  71. Resolution current_resolution_;
  72. int num_resolution_downgrades_;
  73. // Similar to resolution, time spent in high-QP video.
  74. int64_t time_in_blocky_video_ms_;
  75. // Content type of the last decoded frame.
  76. VideoContentType content_type_;
  77. bool is_paused_;
  78. // Set of decoded frames with high QP value.
  79. std::set<int64_t> blocky_frames_;
  80. };
  81. } // namespace webrtc
  82. #endif // VIDEO_VIDEO_QUALITY_OBSERVER_H_