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 #include "test/pc/e2e/analyzer/video/example_video_quality_analyzer.h"
12 
13 #include "api/array_view.h"
14 #include "rtc_base/logging.h"
15 
16 namespace webrtc {
17 namespace webrtc_pc_e2e {
18 
19 ExampleVideoQualityAnalyzer::ExampleVideoQualityAnalyzer() = default;
20 ExampleVideoQualityAnalyzer::~ExampleVideoQualityAnalyzer() = default;
21 
Start(std::string test_case_name,rtc::ArrayView<const std::string> peer_names,int max_threads_count)22 void ExampleVideoQualityAnalyzer::Start(
23     std::string test_case_name,
24     rtc::ArrayView<const std::string> peer_names,
25     int max_threads_count) {}
26 
OnFrameCaptured(absl::string_view peer_name,const std::string & stream_label,const webrtc::VideoFrame & frame)27 uint16_t ExampleVideoQualityAnalyzer::OnFrameCaptured(
28     absl::string_view peer_name,
29     const std::string& stream_label,
30     const webrtc::VideoFrame& frame) {
31   MutexLock lock(&lock_);
32   uint16_t frame_id = next_frame_id_++;
33   auto it = frames_in_flight_.find(frame_id);
34   if (it == frames_in_flight_.end()) {
35     frames_in_flight_.insert(frame_id);
36     frames_to_stream_label_.insert({frame_id, stream_label});
37   } else {
38     RTC_LOG(WARNING) << "Meet new frame with the same id: " << frame_id
39                      << ". Assumes old one as dropped";
40     // We needn't insert frame to frames_in_flight_, because it is already
41     // there.
42     ++frames_dropped_;
43     auto stream_it = frames_to_stream_label_.find(frame_id);
44     RTC_CHECK(stream_it != frames_to_stream_label_.end());
45     stream_it->second = stream_label;
46   }
47   ++frames_captured_;
48   return frame_id;
49 }
50 
OnFramePreEncode(absl::string_view peer_name,const webrtc::VideoFrame & frame)51 void ExampleVideoQualityAnalyzer::OnFramePreEncode(
52     absl::string_view peer_name,
53     const webrtc::VideoFrame& frame) {
54   MutexLock lock(&lock_);
55   ++frames_pre_encoded_;
56 }
57 
OnFrameEncoded(absl::string_view peer_name,uint16_t frame_id,const webrtc::EncodedImage & encoded_image,const EncoderStats & stats)58 void ExampleVideoQualityAnalyzer::OnFrameEncoded(
59     absl::string_view peer_name,
60     uint16_t frame_id,
61     const webrtc::EncodedImage& encoded_image,
62     const EncoderStats& stats) {
63   MutexLock lock(&lock_);
64   ++frames_encoded_;
65 }
66 
OnFrameDropped(absl::string_view peer_name,webrtc::EncodedImageCallback::DropReason reason)67 void ExampleVideoQualityAnalyzer::OnFrameDropped(
68     absl::string_view peer_name,
69     webrtc::EncodedImageCallback::DropReason reason) {
70   RTC_LOG(INFO) << "Frame dropped by encoder";
71   MutexLock lock(&lock_);
72   ++frames_dropped_;
73 }
74 
OnFramePreDecode(absl::string_view peer_name,uint16_t frame_id,const webrtc::EncodedImage & encoded_image)75 void ExampleVideoQualityAnalyzer::OnFramePreDecode(
76     absl::string_view peer_name,
77     uint16_t frame_id,
78     const webrtc::EncodedImage& encoded_image) {
79   MutexLock lock(&lock_);
80   ++frames_received_;
81 }
82 
OnFrameDecoded(absl::string_view peer_name,const webrtc::VideoFrame & frame,const DecoderStats & stats)83 void ExampleVideoQualityAnalyzer::OnFrameDecoded(
84     absl::string_view peer_name,
85     const webrtc::VideoFrame& frame,
86     const DecoderStats& stats) {
87   MutexLock lock(&lock_);
88   ++frames_decoded_;
89 }
90 
OnFrameRendered(absl::string_view peer_name,const webrtc::VideoFrame & frame)91 void ExampleVideoQualityAnalyzer::OnFrameRendered(
92     absl::string_view peer_name,
93     const webrtc::VideoFrame& frame) {
94   MutexLock lock(&lock_);
95   frames_in_flight_.erase(frame.id());
96   ++frames_rendered_;
97 }
98 
OnEncoderError(absl::string_view peer_name,const webrtc::VideoFrame & frame,int32_t error_code)99 void ExampleVideoQualityAnalyzer::OnEncoderError(
100     absl::string_view peer_name,
101     const webrtc::VideoFrame& frame,
102     int32_t error_code) {
103   RTC_LOG(LS_ERROR) << "Failed to encode frame " << frame.id()
104                     << ". Code: " << error_code;
105 }
106 
OnDecoderError(absl::string_view peer_name,uint16_t frame_id,int32_t error_code)107 void ExampleVideoQualityAnalyzer::OnDecoderError(absl::string_view peer_name,
108                                                  uint16_t frame_id,
109                                                  int32_t error_code) {
110   RTC_LOG(LS_ERROR) << "Failed to decode frame " << frame_id
111                     << ". Code: " << error_code;
112 }
113 
Stop()114 void ExampleVideoQualityAnalyzer::Stop() {
115   MutexLock lock(&lock_);
116   RTC_LOG(INFO) << "There are " << frames_in_flight_.size()
117                 << " frames in flight, assuming all of them are dropped";
118   frames_dropped_ += frames_in_flight_.size();
119 }
120 
GetStreamLabel(uint16_t frame_id)121 std::string ExampleVideoQualityAnalyzer::GetStreamLabel(uint16_t frame_id) {
122   MutexLock lock(&lock_);
123   auto it = frames_to_stream_label_.find(frame_id);
124   RTC_DCHECK(it != frames_to_stream_label_.end())
125       << "Unknown frame_id=" << frame_id;
126   return it->second;
127 }
128 
frames_captured() const129 uint64_t ExampleVideoQualityAnalyzer::frames_captured() const {
130   MutexLock lock(&lock_);
131   return frames_captured_;
132 }
133 
frames_pre_encoded() const134 uint64_t ExampleVideoQualityAnalyzer::frames_pre_encoded() const {
135   MutexLock lock(&lock_);
136   return frames_pre_encoded_;
137 }
138 
frames_encoded() const139 uint64_t ExampleVideoQualityAnalyzer::frames_encoded() const {
140   MutexLock lock(&lock_);
141   return frames_encoded_;
142 }
143 
frames_received() const144 uint64_t ExampleVideoQualityAnalyzer::frames_received() const {
145   MutexLock lock(&lock_);
146   return frames_received_;
147 }
148 
frames_decoded() const149 uint64_t ExampleVideoQualityAnalyzer::frames_decoded() const {
150   MutexLock lock(&lock_);
151   return frames_decoded_;
152 }
153 
frames_rendered() const154 uint64_t ExampleVideoQualityAnalyzer::frames_rendered() const {
155   MutexLock lock(&lock_);
156   return frames_rendered_;
157 }
158 
frames_dropped() const159 uint64_t ExampleVideoQualityAnalyzer::frames_dropped() const {
160   MutexLock lock(&lock_);
161   return frames_dropped_;
162 }
163 
164 }  // namespace webrtc_pc_e2e
165 }  // namespace webrtc
166