1 /* 2 * Copyright (c) 2019 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 11 #ifndef TEST_PC_E2E_ANALYZER_VIDEO_EXAMPLE_VIDEO_QUALITY_ANALYZER_H_ 12 #define TEST_PC_E2E_ANALYZER_VIDEO_EXAMPLE_VIDEO_QUALITY_ANALYZER_H_ 13 14 #include <atomic> 15 #include <map> 16 #include <set> 17 #include <string> 18 19 #include "api/array_view.h" 20 #include "api/test/video_quality_analyzer_interface.h" 21 #include "api/video/encoded_image.h" 22 #include "api/video/video_frame.h" 23 #include "rtc_base/synchronization/mutex.h" 24 25 namespace webrtc { 26 27 // This class is an example implementation of 28 // webrtc::VideoQualityAnalyzerInterface and calculates simple metrics 29 // just to demonstration purposes. Assumed to be used in the single process 30 // test cases, where both peers are in the same process. 31 class ExampleVideoQualityAnalyzer : public VideoQualityAnalyzerInterface { 32 public: 33 ExampleVideoQualityAnalyzer(); 34 ~ExampleVideoQualityAnalyzer() override; 35 36 void Start(std::string test_case_name, 37 rtc::ArrayView<const std::string> peer_names, 38 int max_threads_count) override; 39 uint16_t OnFrameCaptured(absl::string_view peer_name, 40 const std::string& stream_label, 41 const VideoFrame& frame) override; 42 void OnFramePreEncode(absl::string_view peer_name, 43 const VideoFrame& frame) override; 44 void OnFrameEncoded(absl::string_view peer_name, 45 uint16_t frame_id, 46 const EncodedImage& encoded_image, 47 const EncoderStats& stats, 48 bool discarded) override; 49 void OnFrameDropped(absl::string_view peer_name, 50 EncodedImageCallback::DropReason reason) override; 51 void OnFramePreDecode(absl::string_view peer_name, 52 uint16_t frame_id, 53 const EncodedImage& encoded_image) override; 54 void OnFrameDecoded(absl::string_view peer_name, 55 const VideoFrame& frame, 56 const DecoderStats& stats) override; 57 void OnFrameRendered(absl::string_view peer_name, 58 const VideoFrame& frame) override; 59 void OnEncoderError(absl::string_view peer_name, 60 const VideoFrame& frame, 61 int32_t error_code) override; 62 void OnDecoderError(absl::string_view peer_name, 63 uint16_t frame_id, 64 int32_t error_code, 65 const DecoderStats& stats) override; 66 void Stop() override; 67 std::string GetStreamLabel(uint16_t frame_id) override; 68 69 uint64_t frames_captured() const; 70 uint64_t frames_pre_encoded() const; 71 uint64_t frames_encoded() const; 72 uint64_t frames_received() const; 73 uint64_t frames_decoded() const; 74 uint64_t frames_rendered() const; 75 uint64_t frames_dropped() const; 76 77 private: 78 // When peer A captured the frame it will come into analyzer's OnFrameCaptured 79 // and will be stored in frames_in_flight_. It will be removed from there 80 // when it will be received in peer B, so we need to guard it with lock. 81 // Also because analyzer will serve for all video streams it can be called 82 // from different threads inside one peer. 83 mutable Mutex lock_; 84 // Stores frame ids, that are currently going from one peer to another. We 85 // need to keep them to correctly determine dropped frames and also correctly 86 // process frame id overlap. 87 std::set<uint16_t> frames_in_flight_ RTC_GUARDED_BY(lock_); 88 std::map<uint16_t, std::string> frames_to_stream_label_ RTC_GUARDED_BY(lock_); 89 uint16_t next_frame_id_ RTC_GUARDED_BY(lock_) = 1; 90 uint64_t frames_captured_ RTC_GUARDED_BY(lock_) = 0; 91 uint64_t frames_pre_encoded_ RTC_GUARDED_BY(lock_) = 0; 92 uint64_t frames_encoded_ RTC_GUARDED_BY(lock_) = 0; 93 uint64_t frames_received_ RTC_GUARDED_BY(lock_) = 0; 94 uint64_t frames_decoded_ RTC_GUARDED_BY(lock_) = 0; 95 uint64_t frames_rendered_ RTC_GUARDED_BY(lock_) = 0; 96 uint64_t frames_dropped_ RTC_GUARDED_BY(lock_) = 0; 97 }; 98 99 } // namespace webrtc 100 101 #endif // TEST_PC_E2E_ANALYZER_VIDEO_EXAMPLE_VIDEO_QUALITY_ANALYZER_H_ 102