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