video_analyzer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright 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_ANALYZER_H_
  11. #define VIDEO_VIDEO_ANALYZER_H_
  12. #include <deque>
  13. #include <map>
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. #include "api/task_queue/task_queue_base.h"
  18. #include "api/video/video_source_interface.h"
  19. #include "modules/rtp_rtcp/source/rtp_packet.h"
  20. #include "modules/rtp_rtcp/source/video_rtp_depacketizer.h"
  21. #include "rtc_base/event.h"
  22. #include "rtc_base/numerics/running_statistics.h"
  23. #include "rtc_base/platform_thread.h"
  24. #include "rtc_base/time_utils.h"
  25. #include "test/layer_filtering_transport.h"
  26. #include "test/rtp_file_writer.h"
  27. #include "test/testsupport/perf_test.h"
  28. namespace webrtc {
  29. class VideoAnalyzer : public PacketReceiver,
  30. public Transport,
  31. public rtc::VideoSinkInterface<VideoFrame> {
  32. public:
  33. using Statistics = RunningStatistics<double>;
  34. VideoAnalyzer(test::LayerFilteringTransport* transport,
  35. const std::string& test_label,
  36. double avg_psnr_threshold,
  37. double avg_ssim_threshold,
  38. int duration_frames,
  39. TimeDelta test_duration,
  40. FILE* graph_data_output_file,
  41. const std::string& graph_title,
  42. uint32_t ssrc_to_analyze,
  43. uint32_t rtx_ssrc_to_analyze,
  44. size_t selected_stream,
  45. int selected_sl,
  46. int selected_tl,
  47. bool is_quick_test_enabled,
  48. Clock* clock,
  49. std::string rtp_dump_name,
  50. TaskQueueBase* task_queue);
  51. ~VideoAnalyzer();
  52. virtual void SetReceiver(PacketReceiver* receiver);
  53. void SetSource(rtc::VideoSourceInterface<VideoFrame>* video_source,
  54. bool respect_sink_wants);
  55. void SetCall(Call* call);
  56. void SetSendStream(VideoSendStream* stream);
  57. void SetReceiveStream(VideoReceiveStream* stream);
  58. void SetAudioReceiveStream(AudioReceiveStream* recv_stream);
  59. rtc::VideoSinkInterface<VideoFrame>* InputInterface();
  60. rtc::VideoSourceInterface<VideoFrame>* OutputInterface();
  61. DeliveryStatus DeliverPacket(MediaType media_type,
  62. rtc::CopyOnWriteBuffer packet,
  63. int64_t packet_time_us) override;
  64. void PreEncodeOnFrame(const VideoFrame& video_frame);
  65. void PostEncodeOnFrame(size_t stream_id, uint32_t timestamp);
  66. bool SendRtp(const uint8_t* packet,
  67. size_t length,
  68. const PacketOptions& options) override;
  69. bool SendRtcp(const uint8_t* packet, size_t length) override;
  70. void OnFrame(const VideoFrame& video_frame) override;
  71. void Wait();
  72. void StartMeasuringCpuProcessTime();
  73. void StopMeasuringCpuProcessTime();
  74. void StartExcludingCpuThreadTime();
  75. void StopExcludingCpuThreadTime();
  76. double GetCpuUsagePercent();
  77. test::LayerFilteringTransport* const transport_;
  78. PacketReceiver* receiver_;
  79. private:
  80. struct FrameComparison {
  81. FrameComparison();
  82. FrameComparison(const VideoFrame& reference,
  83. const VideoFrame& render,
  84. bool dropped,
  85. int64_t input_time_ms,
  86. int64_t send_time_ms,
  87. int64_t recv_time_ms,
  88. int64_t render_time_ms,
  89. size_t encoded_frame_size);
  90. FrameComparison(bool dropped,
  91. int64_t input_time_ms,
  92. int64_t send_time_ms,
  93. int64_t recv_time_ms,
  94. int64_t render_time_ms,
  95. size_t encoded_frame_size);
  96. absl::optional<VideoFrame> reference;
  97. absl::optional<VideoFrame> render;
  98. bool dropped;
  99. int64_t input_time_ms;
  100. int64_t send_time_ms;
  101. int64_t recv_time_ms;
  102. int64_t render_time_ms;
  103. size_t encoded_frame_size;
  104. };
  105. struct Sample {
  106. Sample(int dropped,
  107. int64_t input_time_ms,
  108. int64_t send_time_ms,
  109. int64_t recv_time_ms,
  110. int64_t render_time_ms,
  111. size_t encoded_frame_size,
  112. double psnr,
  113. double ssim);
  114. int dropped;
  115. int64_t input_time_ms;
  116. int64_t send_time_ms;
  117. int64_t recv_time_ms;
  118. int64_t render_time_ms;
  119. size_t encoded_frame_size;
  120. double psnr;
  121. double ssim;
  122. };
  123. // Implements VideoSinkInterface to receive captured frames from a
  124. // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
  125. // as a source to VideoSendStream.
  126. // It forwards all input frames to the VideoAnalyzer for later comparison and
  127. // forwards the captured frames to the VideoSendStream.
  128. class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
  129. public rtc::VideoSourceInterface<VideoFrame> {
  130. public:
  131. CapturedFrameForwarder(VideoAnalyzer* analyzer,
  132. Clock* clock,
  133. int frames_to_capture,
  134. TimeDelta test_duration);
  135. void SetSource(rtc::VideoSourceInterface<VideoFrame>* video_source);
  136. private:
  137. void OnFrame(const VideoFrame& video_frame) override;
  138. // Called when |send_stream_.SetSource()| is called.
  139. void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
  140. const rtc::VideoSinkWants& wants) override;
  141. // Called by |send_stream_| when |send_stream_.SetSource()| is called.
  142. void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override;
  143. VideoAnalyzer* const analyzer_;
  144. rtc::CriticalSection crit_;
  145. rtc::VideoSinkInterface<VideoFrame>* send_stream_input_
  146. RTC_GUARDED_BY(crit_);
  147. VideoSourceInterface<VideoFrame>* video_source_;
  148. Clock* clock_;
  149. int captured_frames_ RTC_GUARDED_BY(crit_);
  150. const int frames_to_capture_;
  151. const Timestamp test_end_;
  152. };
  153. struct FrameWithPsnr {
  154. double psnr;
  155. VideoFrame frame;
  156. };
  157. bool IsInSelectedSpatialAndTemporalLayer(const RtpPacket& rtp_packet);
  158. void AddFrameComparison(const VideoFrame& reference,
  159. const VideoFrame& render,
  160. bool dropped,
  161. int64_t render_time_ms)
  162. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  163. void PollStats();
  164. static void FrameComparisonThread(void* obj);
  165. bool CompareFrames();
  166. bool PopComparison(FrameComparison* comparison);
  167. // Increment counter for number of frames received for comparison.
  168. void FrameRecorded();
  169. // Returns true if all frames to be compared have been taken from the queue.
  170. bool AllFramesRecorded();
  171. // Increase count of number of frames processed. Returns true if this was the
  172. // last frame to be processed.
  173. bool FrameProcessed();
  174. void PrintResults();
  175. void PerformFrameComparison(const FrameComparison& comparison);
  176. void PrintResult(const char* result_type,
  177. Statistics stats,
  178. const char* unit,
  179. webrtc::test::ImproveDirection improve_direction);
  180. void PrintResultWithExternalMean(
  181. const char* result_type,
  182. double mean,
  183. Statistics stats,
  184. const char* unit,
  185. webrtc::test::ImproveDirection improve_direction);
  186. void PrintSamplesToFile(void);
  187. void AddCapturedFrameForComparison(const VideoFrame& video_frame);
  188. Call* call_;
  189. VideoSendStream* send_stream_;
  190. VideoReceiveStream* receive_stream_;
  191. AudioReceiveStream* audio_receive_stream_;
  192. CapturedFrameForwarder captured_frame_forwarder_;
  193. const std::string test_label_;
  194. FILE* const graph_data_output_file_;
  195. const std::string graph_title_;
  196. const uint32_t ssrc_to_analyze_;
  197. const uint32_t rtx_ssrc_to_analyze_;
  198. const size_t selected_stream_;
  199. const int selected_sl_;
  200. const int selected_tl_;
  201. rtc::CriticalSection comparison_lock_;
  202. std::vector<Sample> samples_ RTC_GUARDED_BY(comparison_lock_);
  203. Statistics sender_time_ RTC_GUARDED_BY(comparison_lock_);
  204. Statistics receiver_time_ RTC_GUARDED_BY(comparison_lock_);
  205. Statistics network_time_ RTC_GUARDED_BY(comparison_lock_);
  206. Statistics psnr_ RTC_GUARDED_BY(comparison_lock_);
  207. Statistics ssim_ RTC_GUARDED_BY(comparison_lock_);
  208. Statistics end_to_end_ RTC_GUARDED_BY(comparison_lock_);
  209. Statistics rendered_delta_ RTC_GUARDED_BY(comparison_lock_);
  210. Statistics encoded_frame_size_ RTC_GUARDED_BY(comparison_lock_);
  211. Statistics encode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
  212. Statistics encode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
  213. Statistics encode_usage_percent_ RTC_GUARDED_BY(comparison_lock_);
  214. double mean_decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
  215. Statistics decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
  216. Statistics decode_time_max_ms_ RTC_GUARDED_BY(comparison_lock_);
  217. Statistics media_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
  218. Statistics fec_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
  219. Statistics send_bandwidth_bps_ RTC_GUARDED_BY(comparison_lock_);
  220. Statistics memory_usage_ RTC_GUARDED_BY(comparison_lock_);
  221. Statistics audio_expand_rate_ RTC_GUARDED_BY(comparison_lock_);
  222. Statistics audio_accelerate_rate_ RTC_GUARDED_BY(comparison_lock_);
  223. Statistics audio_jitter_buffer_ms_ RTC_GUARDED_BY(comparison_lock_);
  224. Statistics pixels_ RTC_GUARDED_BY(comparison_lock_);
  225. // Rendered frame with worst PSNR is saved for further analysis.
  226. absl::optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_);
  227. // Freeze metrics.
  228. Statistics time_between_freezes_ RTC_GUARDED_BY(comparison_lock_);
  229. uint32_t freeze_count_ RTC_GUARDED_BY(comparison_lock_);
  230. uint32_t total_freezes_duration_ms_ RTC_GUARDED_BY(comparison_lock_);
  231. uint32_t total_frames_duration_ms_ RTC_GUARDED_BY(comparison_lock_);
  232. double sum_squared_frame_durations_ RTC_GUARDED_BY(comparison_lock_);
  233. double decode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
  234. double render_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
  235. size_t last_fec_bytes_;
  236. rtc::CriticalSection crit_;
  237. const int frames_to_process_;
  238. const Timestamp test_end_;
  239. int frames_recorded_ RTC_GUARDED_BY(comparison_lock_);
  240. int frames_processed_ RTC_GUARDED_BY(comparison_lock_);
  241. int captured_frames_ RTC_GUARDED_BY(comparison_lock_);
  242. int dropped_frames_ RTC_GUARDED_BY(comparison_lock_);
  243. int dropped_frames_before_first_encode_ RTC_GUARDED_BY(crit_);
  244. int dropped_frames_before_rendering_ RTC_GUARDED_BY(crit_);
  245. int64_t last_render_time_ RTC_GUARDED_BY(comparison_lock_);
  246. int64_t last_render_delta_ms_ RTC_GUARDED_BY(comparison_lock_);
  247. int64_t last_unfreeze_time_ms_ RTC_GUARDED_BY(comparison_lock_);
  248. uint32_t rtp_timestamp_delta_ RTC_GUARDED_BY(crit_);
  249. rtc::CriticalSection cpu_measurement_lock_;
  250. int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
  251. int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
  252. std::deque<VideoFrame> frames_ RTC_GUARDED_BY(crit_);
  253. absl::optional<VideoFrame> last_rendered_frame_ RTC_GUARDED_BY(crit_);
  254. rtc::TimestampWrapAroundHandler wrap_handler_ RTC_GUARDED_BY(crit_);
  255. std::map<int64_t, int64_t> send_times_ RTC_GUARDED_BY(crit_);
  256. std::map<int64_t, int64_t> recv_times_ RTC_GUARDED_BY(crit_);
  257. std::map<int64_t, size_t> encoded_frame_sizes_ RTC_GUARDED_BY(crit_);
  258. absl::optional<uint32_t> first_encoded_timestamp_ RTC_GUARDED_BY(crit_);
  259. absl::optional<uint32_t> first_sent_timestamp_ RTC_GUARDED_BY(crit_);
  260. const double avg_psnr_threshold_;
  261. const double avg_ssim_threshold_;
  262. bool is_quick_test_enabled_;
  263. std::vector<rtc::PlatformThread*> comparison_thread_pool_;
  264. rtc::Event comparison_available_event_;
  265. std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_);
  266. bool quit_ RTC_GUARDED_BY(comparison_lock_);
  267. rtc::Event done_;
  268. std::unique_ptr<VideoRtpDepacketizer> vp8_depacketizer_;
  269. std::unique_ptr<VideoRtpDepacketizer> vp9_depacketizer_;
  270. std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
  271. Clock* const clock_;
  272. const int64_t start_ms_;
  273. TaskQueueBase* task_queue_;
  274. };
  275. } // namespace webrtc
  276. #endif // VIDEO_VIDEO_ANALYZER_H_