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 namespace webrtc_pc_e2e {
27 
28 // This class is an example implementation of
29 // webrtc::VideoQualityAnalyzerInterface and calculates simple metrics
30 // just to demonstration purposes. Assumed to be used in the single process
31 // test cases, where both peers are in the same process.
32 class ExampleVideoQualityAnalyzer : public VideoQualityAnalyzerInterface {
33  public:
34   ExampleVideoQualityAnalyzer();
35   ~ExampleVideoQualityAnalyzer() override;
36 
37   void Start(std::string test_case_name,
38              rtc::ArrayView<const std::string> peer_names,
39              int max_threads_count) override;
40   uint16_t OnFrameCaptured(absl::string_view peer_name,
41                            const std::string& stream_label,
42                            const VideoFrame& frame) override;
43   void OnFramePreEncode(absl::string_view peer_name,
44                         const VideoFrame& frame) override;
45   void OnFrameEncoded(absl::string_view peer_name,
46                       uint16_t frame_id,
47                       const EncodedImage& encoded_image,
48                       const EncoderStats& stats) 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) override;
65   void Stop() override;
66   std::string GetStreamLabel(uint16_t frame_id) override;
67 
68   uint64_t frames_captured() const;
69   uint64_t frames_pre_encoded() const;
70   uint64_t frames_encoded() const;
71   uint64_t frames_received() const;
72   uint64_t frames_decoded() const;
73   uint64_t frames_rendered() const;
74   uint64_t frames_dropped() const;
75 
76  private:
77   // When peer A captured the frame it will come into analyzer's OnFrameCaptured
78   // and will be stored in frames_in_flight_. It will be removed from there
79   // when it will be received in peer B, so we need to guard it with lock.
80   // Also because analyzer will serve for all video streams it can be called
81   // from different threads inside one peer.
82   mutable Mutex lock_;
83   // Stores frame ids, that are currently going from one peer to another. We
84   // need to keep them to correctly determine dropped frames and also correctly
85   // process frame id overlap.
86   std::set<uint16_t> frames_in_flight_ RTC_GUARDED_BY(lock_);
87   std::map<uint16_t, std::string> frames_to_stream_label_ RTC_GUARDED_BY(lock_);
88   uint16_t next_frame_id_ RTC_GUARDED_BY(lock_) = 0;
89   uint64_t frames_captured_ RTC_GUARDED_BY(lock_) = 0;
90   uint64_t frames_pre_encoded_ RTC_GUARDED_BY(lock_) = 0;
91   uint64_t frames_encoded_ RTC_GUARDED_BY(lock_) = 0;
92   uint64_t frames_received_ RTC_GUARDED_BY(lock_) = 0;
93   uint64_t frames_decoded_ RTC_GUARDED_BY(lock_) = 0;
94   uint64_t frames_rendered_ RTC_GUARDED_BY(lock_) = 0;
95   uint64_t frames_dropped_ RTC_GUARDED_BY(lock_) = 0;
96 };
97 
98 }  // namespace webrtc_pc_e2e
99 }  // namespace webrtc
100 
101 #endif  // TEST_PC_E2E_ANALYZER_VIDEO_EXAMPLE_VIDEO_QUALITY_ANALYZER_H_
102