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