| 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 "absl/strings/string_view.h" |
| 20 | #include "api/numerics/samples_stats_counter.h" |
| 21 | #include "api/task_queue/task_queue_base.h" |
| 22 | #include "api/test/metrics/metric.h" |
| 23 | #include "api/video/video_source_interface.h" |
| 24 | #include "modules/rtp_rtcp/source/rtp_packet.h" |
| 25 | #include "modules/rtp_rtcp/source/video_rtp_depacketizer.h" |
| 26 | #include "rtc_base/event.h" |
| 27 | #include "rtc_base/numerics/running_statistics.h" |
| 28 | #include "rtc_base/numerics/sequence_number_unwrapper.h" |
| 29 | #include "rtc_base/platform_thread.h" |
| 30 | #include "rtc_base/synchronization/mutex.h" |
| 31 | #include "test/layer_filtering_transport.h" |
| 32 | #include "test/rtp_file_writer.h" |
| 33 |
| 34 | namespace webrtc { |
| 35 |
| 36 | class VideoAnalyzer : public PacketReceiver, |
| 37 | public Transport, |
| 38 | public rtc::VideoSinkInterface<VideoFrame> { |
| 39 | public: |
| 40 | VideoAnalyzer(test::LayerFilteringTransport* transport, |
| 41 | const std::string& test_label, |
| 42 | double avg_psnr_threshold, |
| 43 | double avg_ssim_threshold, |
| 44 | int duration_frames, |
| 45 | TimeDelta test_duration, |
| 46 | FILE* graph_data_output_file, |
| 47 | const std::string& graph_title, |
| 48 | uint32_t ssrc_to_analyze, |
| 49 | uint32_t rtx_ssrc_to_analyze, |
| 50 | size_t selected_stream, |
| 51 | int selected_sl, |
| 52 | int selected_tl, |
| 53 | bool is_quick_test_enabled, |
| 54 | Clock* clock, |
| 55 | std::string rtp_dump_name, |
| 56 | TaskQueueBase* task_queue); |
| 57 | ~VideoAnalyzer(); |
| 58 |
| 59 | virtual void SetReceiver(PacketReceiver* receiver); |
| 60 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* video_source, |
| 61 | bool respect_sink_wants); |
| 62 | void SetCall(Call* call); |
| 63 | void SetSendStream(VideoSendStream* stream); |
| 64 | void SetReceiveStream(VideoReceiveStreamInterface* stream); |
| 65 | void SetAudioReceiveStream(AudioReceiveStreamInterface* recv_stream); |
| 66 |
| 67 | rtc::VideoSinkInterface<VideoFrame>* InputInterface(); |
| 68 | rtc::VideoSourceInterface<VideoFrame>* OutputInterface(); |
| 69 |
| 70 | void DeliverRtcpPacket(rtc::CopyOnWriteBuffer packet) override; |
| 71 | void DeliverRtpPacket(MediaType media_type, |
| 72 | RtpPacketReceived packet, |
| 73 | PacketReceiver::OnUndemuxablePacketHandler |
| 74 | undemuxable_packet_handler) override; |
| 75 |
| 76 | void PreEncodeOnFrame(const VideoFrame& video_frame); |
| 77 | void PostEncodeOnFrame(size_t stream_id, uint32_t timestamp); |
| 78 |
| 79 | bool SendRtp(rtc::ArrayView<const uint8_t> packet, |
| 80 | const PacketOptions& options) override; |
| 81 |
| 82 | bool SendRtcp(rtc::ArrayView<const uint8_t> packet) override; |
| 83 | void OnFrame(const VideoFrame& video_frame) override; |
| 84 | void Wait(); |
| 85 |
| 86 | void StartMeasuringCpuProcessTime(); |
| 87 | void StopMeasuringCpuProcessTime(); |
| 88 | void StartExcludingCpuThreadTime() RTC_LOCKS_EXCLUDED(cpu_measurement_lock_); |
| 89 | void StopExcludingCpuThreadTime() RTC_LOCKS_EXCLUDED(cpu_measurement_lock_); |
| 90 | double GetCpuUsagePercent() RTC_LOCKS_EXCLUDED(cpu_measurement_lock_); |
| 91 |
| 92 | test::LayerFilteringTransport* const transport_; |
| 93 | PacketReceiver* receiver_; |
| 94 |
| 95 | private: |
| 96 | struct FrameComparison { |
| 97 | FrameComparison(); |
| 98 | FrameComparison(const VideoFrame& reference, |
| 99 | const VideoFrame& render, |
| 100 | bool dropped, |
| 101 | int64_t input_time_ms, |
| 102 | int64_t send_time_ms, |
| 103 | int64_t recv_time_ms, |
| 104 | int64_t render_time_ms, |
| 105 | size_t encoded_frame_size); |
| 106 | FrameComparison(bool 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 |
| 113 | absl::optional<VideoFrame> reference; |
| 114 | absl::optional<VideoFrame> render; |
| 115 | bool dropped; |
| 116 | int64_t input_time_ms; |
| 117 | int64_t send_time_ms; |
| 118 | int64_t recv_time_ms; |
| 119 | int64_t render_time_ms; |
| 120 | size_t encoded_frame_size; |
| 121 | }; |
| 122 |
| 123 | struct Sample { |
| 124 | Sample(int dropped, |
| 125 | int64_t input_time_ms, |
| 126 | int64_t send_time_ms, |
| 127 | int64_t recv_time_ms, |
| 128 | int64_t render_time_ms, |
| 129 | size_t encoded_frame_size, |
| 130 | double psnr, |
| 131 | double ssim); |
| 132 |
| 133 | int dropped; |
| 134 | int64_t input_time_ms; |
| 135 | int64_t send_time_ms; |
| 136 | int64_t recv_time_ms; |
| 137 | int64_t render_time_ms; |
| 138 | size_t encoded_frame_size; |
| 139 | double psnr; |
| 140 | double ssim; |
| 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 | CapturedFrameForwarder(VideoAnalyzer* analyzer, |
| 152 | Clock* clock, |
| 153 | int frames_to_capture, |
| 154 | TimeDelta test_duration); |
| 155 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* video_source); |
| 156 |
| 157 | private: |
| 158 | void OnFrame(const VideoFrame& video_frame) |
| 159 | RTC_LOCKS_EXCLUDED(lock_) override; |
| 160 |
| 161 | // Called when `send_stream_.SetSource()` is called. |
| 162 | void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 163 | const rtc::VideoSinkWants& wants) |
| 164 | RTC_LOCKS_EXCLUDED(lock_) override; |
| 165 |
| 166 | // Called by `send_stream_` when `send_stream_.SetSource()` is called. |
| 167 | void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) |
| 168 | RTC_LOCKS_EXCLUDED(lock_) override; |
| 169 |
| 170 | VideoAnalyzer* const analyzer_; |
| 171 | Mutex lock_; |
| 172 | rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ |
| 173 | RTC_GUARDED_BY(lock_); |
| 174 | VideoSourceInterface<VideoFrame>* video_source_; |
| 175 | Clock* clock_; |
| 176 | int captured_frames_ RTC_GUARDED_BY(lock_); |
| 177 | const int frames_to_capture_; |
| 178 | const Timestamp test_end_; |
| 179 | }; |
| 180 |
| 181 | struct FrameWithPsnr { |
| 182 | double psnr; |
| 183 | VideoFrame frame; |
| 184 | }; |
| 185 |
| 186 | bool IsInSelectedSpatialAndTemporalLayer(const RtpPacket& rtp_packet); |
| 187 |
| 188 | void AddFrameComparison(const VideoFrame& reference, |
| 189 | const VideoFrame& render, |
| 190 | bool dropped, |
| 191 | int64_t render_time_ms) |
| 192 | RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 193 |
| 194 | void PollStats() RTC_LOCKS_EXCLUDED(comparison_lock_); |
| 195 | static void FrameComparisonThread(void* obj); |
| 196 | bool CompareFrames(); |
| 197 | bool PopComparison(FrameComparison* comparison); |
| 198 | // Increment counter for number of frames received for comparison. |
| 199 | void FrameRecorded() RTC_EXCLUSIVE_LOCKS_REQUIRED(comparison_lock_); |
| 200 | // Returns true if all frames to be compared have been taken from the queue. |
| 201 | bool AllFramesRecorded() RTC_LOCKS_EXCLUDED(comparison_lock_); |
| 202 | bool AllFramesRecordedLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(comparison_lock_); |
| 203 | // Increase count of number of frames processed. Returns true if this was the |
| 204 | // last frame to be processed. |
| 205 | bool FrameProcessed() RTC_LOCKS_EXCLUDED(comparison_lock_); |
| 206 | void PrintResults() RTC_LOCKS_EXCLUDED(lock_, comparison_lock_); |
| 207 | void PerformFrameComparison(const FrameComparison& comparison) |
| 208 | RTC_LOCKS_EXCLUDED(comparison_lock_); |
| 209 | void PrintResult(absl::string_view result_type, |
| 210 | const SamplesStatsCounter& stats, |
| 211 | webrtc::test::Unit unit, |
| 212 | webrtc::test::ImprovementDirection improvement_direction); |
| 213 | void PrintResultWithExternalMean( |
| 214 | absl::string_view result_type, |
| 215 | double mean, |
| 216 | const SamplesStatsCounter& stats, |
| 217 | webrtc::test::Unit unit, |
| 218 | webrtc::test::ImprovementDirection improvement_direction); |
| 219 | void PrintSamplesToFile(void) RTC_LOCKS_EXCLUDED(comparison_lock_); |
| 220 | void AddCapturedFrameForComparison(const VideoFrame& video_frame) |
| 221 | RTC_LOCKS_EXCLUDED(lock_, comparison_lock_); |
| 222 |
| 223 | Call* call_; |
| 224 | VideoSendStream* send_stream_; |
| 225 | VideoReceiveStreamInterface* receive_stream_; |
| 226 | AudioReceiveStreamInterface* audio_receive_stream_; |
| 227 | CapturedFrameForwarder captured_frame_forwarder_; |
| 228 | const std::string test_label_; |
| 229 | FILE* const graph_data_output_file_; |
| 230 | const std::string graph_title_; |
| 231 | const uint32_t ssrc_to_analyze_; |
| 232 | const uint32_t rtx_ssrc_to_analyze_; |
| 233 | const size_t selected_stream_; |
| 234 | const int selected_sl_; |
| 235 | const int selected_tl_; |
| 236 |
| 237 | Mutex comparison_lock_; |
| 238 | std::vector<Sample> samples_ RTC_GUARDED_BY(comparison_lock_); |
| 239 | SamplesStatsCounter sender_time_ RTC_GUARDED_BY(comparison_lock_); |
| 240 | SamplesStatsCounter receiver_time_ RTC_GUARDED_BY(comparison_lock_); |
| 241 | SamplesStatsCounter network_time_ RTC_GUARDED_BY(comparison_lock_); |
| 242 | SamplesStatsCounter psnr_ RTC_GUARDED_BY(comparison_lock_); |
| 243 | SamplesStatsCounter ssim_ RTC_GUARDED_BY(comparison_lock_); |
| 244 | SamplesStatsCounter end_to_end_ RTC_GUARDED_BY(comparison_lock_); |
| 245 | SamplesStatsCounter rendered_delta_ RTC_GUARDED_BY(comparison_lock_); |
| 246 | SamplesStatsCounter encoded_frame_size_ RTC_GUARDED_BY(comparison_lock_); |
| 247 | SamplesStatsCounter encode_frame_rate_ RTC_GUARDED_BY(comparison_lock_); |
| 248 | SamplesStatsCounter encode_time_ms_ RTC_GUARDED_BY(comparison_lock_); |
| 249 | SamplesStatsCounter encode_usage_percent_ RTC_GUARDED_BY(comparison_lock_); |
| 250 | double mean_decode_time_ms_ RTC_GUARDED_BY(comparison_lock_); |
| 251 | SamplesStatsCounter decode_time_ms_ RTC_GUARDED_BY(comparison_lock_); |
| 252 | SamplesStatsCounter decode_time_max_ms_ RTC_GUARDED_BY(comparison_lock_); |
| 253 | SamplesStatsCounter media_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_); |
| 254 | SamplesStatsCounter fec_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_); |
| 255 | SamplesStatsCounter send_bandwidth_bps_ RTC_GUARDED_BY(comparison_lock_); |
| 256 | SamplesStatsCounter memory_usage_ RTC_GUARDED_BY(comparison_lock_); |
| 257 | SamplesStatsCounter audio_expand_rate_ RTC_GUARDED_BY(comparison_lock_); |
| 258 | SamplesStatsCounter audio_accelerate_rate_ RTC_GUARDED_BY(comparison_lock_); |
| 259 | SamplesStatsCounter audio_jitter_buffer_ms_ RTC_GUARDED_BY(comparison_lock_); |
| 260 | SamplesStatsCounter pixels_ RTC_GUARDED_BY(comparison_lock_); |
| 261 | // Rendered frame with worst PSNR is saved for further analysis. |
| 262 | absl::optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_); |
| 263 | // Freeze metrics. |
| 264 | SamplesStatsCounter time_between_freezes_ RTC_GUARDED_BY(comparison_lock_); |
| 265 | uint32_t freeze_count_ RTC_GUARDED_BY(comparison_lock_); |
| 266 | uint32_t total_freezes_duration_ms_ RTC_GUARDED_BY(comparison_lock_); |
| 267 | double total_inter_frame_delay_ RTC_GUARDED_BY(comparison_lock_); |
| 268 | double total_squared_inter_frame_delay_ RTC_GUARDED_BY(comparison_lock_); |
| 269 |
| 270 | double decode_frame_rate_ RTC_GUARDED_BY(comparison_lock_); |
| 271 | double render_frame_rate_ RTC_GUARDED_BY(comparison_lock_); |
| 272 |
| 273 | size_t last_fec_bytes_; |
| 274 |
| 275 | Mutex lock_ RTC_ACQUIRED_BEFORE(comparison_lock_) |
| 276 | RTC_ACQUIRED_BEFORE(cpu_measurement_lock_); |
| 277 | const int frames_to_process_; |
| 278 | const Timestamp test_end_; |
| 279 | int frames_recorded_ RTC_GUARDED_BY(comparison_lock_); |
| 280 | int frames_processed_ RTC_GUARDED_BY(comparison_lock_); |
| 281 | int captured_frames_ RTC_GUARDED_BY(comparison_lock_); |
| 282 | int dropped_frames_ RTC_GUARDED_BY(comparison_lock_); |
| 283 | int dropped_frames_before_first_encode_ RTC_GUARDED_BY(lock_); |
| 284 | int dropped_frames_before_rendering_ RTC_GUARDED_BY(lock_); |
| 285 | int64_t last_render_time_ RTC_GUARDED_BY(comparison_lock_); |
| 286 | int64_t last_render_delta_ms_ RTC_GUARDED_BY(comparison_lock_); |
| 287 | int64_t last_unfreeze_time_ms_ RTC_GUARDED_BY(comparison_lock_); |
| 288 | uint32_t rtp_timestamp_delta_ RTC_GUARDED_BY(lock_); |
| 289 |
| 290 | Mutex cpu_measurement_lock_; |
| 291 | int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_); |
| 292 | int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_); |
| 293 |
| 294 | std::deque<VideoFrame> frames_ RTC_GUARDED_BY(lock_); |
| 295 | absl::optional<VideoFrame> last_rendered_frame_ RTC_GUARDED_BY(lock_); |
| 296 | RtpTimestampUnwrapper wrap_handler_ RTC_GUARDED_BY(lock_); |
| 297 | std::map<int64_t, int64_t> send_times_ RTC_GUARDED_BY(lock_); |
| 298 | std::map<int64_t, int64_t> recv_times_ RTC_GUARDED_BY(lock_); |
| 299 | std::map<int64_t, size_t> encoded_frame_sizes_ RTC_GUARDED_BY(lock_); |
| 300 | absl::optional<uint32_t> first_encoded_timestamp_ RTC_GUARDED_BY(lock_); |
| 301 | absl::optional<uint32_t> first_sent_timestamp_ RTC_GUARDED_BY(lock_); |
| 302 | const double avg_psnr_threshold_; |
| 303 | const double avg_ssim_threshold_; |
| 304 | bool is_quick_test_enabled_; |
| 305 |
| 306 | std::vector<rtc::PlatformThread> comparison_thread_pool_; |
| 307 | rtc::Event comparison_available_event_; |
| 308 | std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_); |
| 309 | bool quit_ RTC_GUARDED_BY(comparison_lock_); |
| 310 | rtc::Event done_; |
| 311 |
| 312 | std::unique_ptr<VideoRtpDepacketizer> vp8_depacketizer_; |
| 313 | std::unique_ptr<VideoRtpDepacketizer> vp9_depacketizer_; |
| 314 | std::unique_ptr<test::RtpFileWriter> rtp_file_writer_; |
| 315 | Clock* const clock_; |
| 316 | const int64_t start_ms_; |
| 317 | TaskQueueBase* task_queue_; |
| 318 | }; |
| 319 |
| 320 | } // namespace webrtc |
| 321 | #endif // VIDEO_VIDEO_ANALYZER_H_ |
| 322 |