FrameStatistics.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef FrameStatistics_h_
  6. #define FrameStatistics_h_
  7. #include "mozilla/ReentrantMonitor.h"
  8. namespace mozilla {
  9. struct FrameStatisticsData
  10. {
  11. // Number of frames parsed and demuxed from media.
  12. // Access protected by mReentrantMonitor.
  13. uint64_t mParsedFrames = 0;
  14. // Number of parsed frames which were actually decoded.
  15. // Access protected by mReentrantMonitor.
  16. uint64_t mDecodedFrames = 0;
  17. // Number of decoded frames which were actually sent down the rendering
  18. // pipeline to be painted ("presented"). Access protected by mReentrantMonitor.
  19. uint64_t mPresentedFrames = 0;
  20. // Number of frames that have been skipped because they have missed their
  21. // composition deadline.
  22. uint64_t mDroppedFrames = 0;
  23. // Sum of all inter-keyframe segment durations, in microseconds.
  24. // Dividing by count will give the average inter-keyframe time.
  25. uint64_t mInterKeyframeSum_us = 0;
  26. // Number of inter-keyframe segments summed so far.
  27. size_t mInterKeyframeCount = 0;
  28. // Maximum inter-keyframe segment duration, in microseconds.
  29. uint64_t mInterKeyFrameMax_us = 0;
  30. FrameStatisticsData() = default;
  31. FrameStatisticsData(uint64_t aParsed, uint64_t aDecoded, uint64_t aDropped)
  32. : mParsedFrames(aParsed)
  33. , mDecodedFrames(aDecoded)
  34. , mDroppedFrames(aDropped)
  35. {}
  36. void
  37. Accumulate(const FrameStatisticsData& aStats)
  38. {
  39. mParsedFrames += aStats.mParsedFrames;
  40. mDecodedFrames += aStats.mDecodedFrames;
  41. mPresentedFrames += aStats.mPresentedFrames;
  42. mDroppedFrames += aStats.mDroppedFrames;
  43. mInterKeyframeSum_us += aStats.mInterKeyframeSum_us;
  44. mInterKeyframeCount += aStats.mInterKeyframeCount;
  45. // It doesn't make sense to add max numbers, instead keep the bigger one.
  46. if (mInterKeyFrameMax_us < aStats.mInterKeyFrameMax_us) {
  47. mInterKeyFrameMax_us = aStats.mInterKeyFrameMax_us;
  48. }
  49. }
  50. };
  51. // Frame decoding/painting related performance counters.
  52. // Threadsafe.
  53. class FrameStatistics
  54. {
  55. public:
  56. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FrameStatistics);
  57. FrameStatistics()
  58. : mReentrantMonitor("FrameStats")
  59. {}
  60. // Returns a copy of all frame statistics data.
  61. // Can be called on any thread.
  62. FrameStatisticsData GetFrameStatisticsData() const
  63. {
  64. ReentrantMonitorAutoEnter mon(mReentrantMonitor);
  65. return mFrameStatisticsData;
  66. }
  67. // Returns number of frames which have been parsed from the media.
  68. // Can be called on any thread.
  69. uint64_t GetParsedFrames() const
  70. {
  71. ReentrantMonitorAutoEnter mon(mReentrantMonitor);
  72. return mFrameStatisticsData.mParsedFrames;
  73. }
  74. // Returns the number of parsed frames which have been decoded.
  75. // Can be called on any thread.
  76. uint64_t GetDecodedFrames() const
  77. {
  78. ReentrantMonitorAutoEnter mon(mReentrantMonitor);
  79. return mFrameStatisticsData.mDecodedFrames;
  80. }
  81. // Returns the number of decoded frames which have been sent to the rendering
  82. // pipeline for painting ("presented").
  83. // Can be called on any thread.
  84. uint64_t GetPresentedFrames() const
  85. {
  86. ReentrantMonitorAutoEnter mon(mReentrantMonitor);
  87. return mFrameStatisticsData.mPresentedFrames;
  88. }
  89. // Returns the number of frames that have been skipped because they have
  90. // missed their composition deadline.
  91. uint64_t GetDroppedFrames() const
  92. {
  93. ReentrantMonitorAutoEnter mon(mReentrantMonitor);
  94. return mFrameStatisticsData.mDroppedFrames;
  95. }
  96. // Increments the parsed and decoded frame counters by the passed in counts.
  97. // Can be called on any thread.
  98. void NotifyDecodedFrames(const FrameStatisticsData& aStats)
  99. {
  100. ReentrantMonitorAutoEnter mon(mReentrantMonitor);
  101. mFrameStatisticsData.Accumulate(aStats);
  102. }
  103. // Increments the presented frame counters.
  104. // Can be called on any thread.
  105. void NotifyPresentedFrame()
  106. {
  107. ReentrantMonitorAutoEnter mon(mReentrantMonitor);
  108. ++mFrameStatisticsData.mPresentedFrames;
  109. }
  110. private:
  111. ~FrameStatistics() {}
  112. // ReentrantMonitor to protect access of playback statistics.
  113. mutable ReentrantMonitor mReentrantMonitor;
  114. FrameStatisticsData mFrameStatisticsData;
  115. };
  116. } // namespace mozilla
  117. #endif // FrameStatistics_h_