OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/media/repetition_detector.h" | |
6 | |
7 #include "base/macros.h" // arraysize | |
Henrik Grunell
2015/09/23 10:36:41
Remove comments.
minyue
2015/09/25 14:40:07
Done.
| |
8 #include "base/metrics/histogram_macros.h" // UMA_HISTOGRAM_CUSTOM_ENUMERATION | |
9 #include "base/numerics/safe_conversions.h" // checked_cast | |
10 #include "third_party/webrtc/base/checks.h" // rtc::CheckedDivExact | |
Henrik Grunell
2015/09/23 10:36:41
Remove. Use CHECK or DCHECK before division instea
minyue
2015/09/25 14:40:07
Done.
| |
11 | |
12 namespace content { | |
13 | |
14 namespace { | |
15 static const RepetitionDetector::Pattern kRepetitionPatterns[] = { | |
Henrik Grunell
2015/09/23 10:36:41
Let the user of this class give the repetitions pa
Henrik Grunell
2015/09/23 10:36:42
Empty line before.
tommi (sloooow) - chröme
2015/09/25 08:57:14
no need for static since this is in an anonymous n
minyue
2015/09/25 14:40:07
I thought about letting user to register the patte
minyue
2015/09/25 14:40:07
Done.
minyue
2015/09/25 14:40:08
Done.
| |
16 // {id_, look_back_ms_, min_length_ms_} | |
Henrik Grunell
2015/09/23 10:36:42
Remove this comment. It duplicates info.
minyue
2015/09/25 14:40:07
Done.
| |
17 {1, 10, 10}, | |
tommi (sloooow) - chröme
2015/09/25 08:57:14
is id_ needed? Looks like it's just pos+1.
minyue
2015/09/25 14:40:07
Yes, indeed, but not necessarily, Imagine that we
tommi (sloooow) - chröme
2015/09/28 10:28:39
Sorry, I'm not following... it still looks to me l
minyue
2015/09/28 14:47:50
Sorry for being not clear enough. I meant that if
tommi (sloooow) - chröme
2015/09/28 16:13:03
Ah I see. Thanks.
| |
18 {2, 20, 10}, | |
19 {3, 30, 10}, | |
20 {4, 40, 10}, | |
21 {5, 50, 10}, | |
22 {6, 60, 10}, | |
23 {7, 70, 10}, | |
24 {8, 80, 10}, | |
25 {9, 90, 10}, | |
26 {10, 100, 10}, | |
27 {20, 200, 10}, | |
28 }; | |
29 static const size_t kMaxFrames = 480; // 10ms * 48kHz | |
Henrik Grunell
2015/09/23 10:36:41
Nit: 10 ms * 48 kHz
Henrik Grunell
2015/09/23 10:36:42
Empty line before.
tommi (sloooow) - chröme
2015/09/25 08:57:14
looks like this is assuming a certain sample rate.
minyue
2015/09/25 14:40:07
Done.
minyue
2015/09/25 14:40:07
No it does not assume any sample rate. kMaxFrames
minyue
2015/09/25 14:40:08
Done.
| |
30 } | |
Henrik Grunell
2015/09/23 10:36:42
Empty line before.
Add " // namespace"
minyue
2015/09/25 14:40:07
Done.
| |
31 | |
32 RepetitionDetector::State::State(int id, int look_back_ms, int min_length_ms) | |
33 : id_(id), | |
34 look_back_ms_(look_back_ms), | |
35 min_length_ms_(min_length_ms) { | |
36 Reset(); | |
37 } | |
38 | |
39 void RepetitionDetector::State::Increment(bool zero) { | |
40 if (0 == count_frames_ && zero) { | |
Henrik Grunell
2015/09/23 10:36:41
count_frames_ == 0
Henrik Grunell
2015/09/23 10:36:41
Nit: You can remove {} from single line if blocks
minyue
2015/09/25 14:40:07
Yes, I generally prefer "0 == xxx" so that mistype
minyue
2015/09/25 14:40:07
I am sometimes confused of which one is preferable
| |
41 all_zero_ = true; | |
tommi (sloooow) - chröme
2015/09/25 08:57:14
is there a chance that all_zero_ is true when the
minyue
2015/09/25 14:40:07
The point is that when we find "0 == 0" as the fir
| |
42 } | |
43 ++count_frames_; | |
tommi (sloooow) - chröme
2015/09/25 08:57:14
the class doesn't have any locking. please add a t
minyue
2015/09/25 14:40:08
Done.
| |
44 if (!zero) { | |
45 all_zero_ = false; | |
Henrik Grunell
2015/09/23 10:36:42
Is this correct? |zero| alone will determine what
tommi (sloooow) - chröme
2015/09/25 08:57:14
it looks like we're checking zero twice, setting a
minyue
2015/09/25 14:40:07
not really.
1 2 3 0 1 2 3 0 will be considered al
| |
46 } | |
47 } | |
48 | |
49 bool RepetitionDetector::State::HasValidReport(int sample_rate_hz) const { | |
50 return (!all_zero_ && count_frames_ >= | |
51 base::checked_cast<size_t>(min_length_ms_ * sample_rate_hz / 1000)); | |
tommi (sloooow) - chröme
2015/09/25 08:57:14
what benefit does checked_cast provide?
minyue
2015/09/25 14:40:07
not much indeed. Only if min_length_ms_ or sample_
| |
52 } | |
53 | |
54 void RepetitionDetector::State::Reset() { | |
55 count_frames_ = 0; | |
56 all_zero_ = true; | |
57 reported_ = false; | |
58 } | |
59 | |
60 RepetitionDetector::RepetitionDetector() | |
61 : max_look_back_ms_(0), | |
62 sample_rate_hz_(0), | |
63 buffer_size_frames_(0), | |
64 buffer_end_index_(0), | |
65 max_frames_(kMaxFrames) { | |
66 RegisterRepetitionPatterns(kRepetitionPatterns, | |
67 arraysize(kRepetitionPatterns)); | |
68 } | |
69 | |
70 RepetitionDetector::~RepetitionDetector() = default; | |
71 | |
72 void RepetitionDetector::RegisterRepetitionPatterns(const Pattern* patterns, | |
73 size_t num_patterns) { | |
74 Pattern pattern; | |
75 for (size_t idx = 0; idx < num_patterns; idx++) { | |
76 pattern = patterns[idx]; | |
77 ids_.push_back(pattern.id_); | |
Henrik Grunell
2015/09/23 10:36:42
Can the ids and states be a map? Or do they need t
minyue
2015/09/25 14:40:07
|ids_| is provided to UMA_HISTOGRAM_CUSTOM_ENUMERA
| |
78 states_.push_back(new State(pattern.id_, pattern.look_back_ms_, | |
tommi (sloooow) - chröme
2015/09/25 08:57:14
+1 on Henrik's comments. it seems strange that we'
minyue
2015/09/25 14:40:07
please refer to my comment above
| |
79 pattern.min_length_ms_)); | |
80 if (pattern.look_back_ms_ > max_look_back_ms_) { | |
81 max_look_back_ms_ = pattern.look_back_ms_; | |
82 } | |
83 } | |
84 } | |
85 | |
86 void RepetitionDetector::Reset(size_t num_channels, int sample_rate_hz) { | |
87 num_channels_ = num_channels; | |
88 sample_rate_hz_ = sample_rate_hz; | |
89 int sample_1k = max_look_back_ms_ * sample_rate_hz_; | |
Henrik Grunell
2015/09/23 10:36:41
What does the name mean? One sample? 1k samples?
N
minyue
2015/09/25 14:40:07
Yes, it is a bad name, it means sample * 1000. I'd
| |
90 // |(sample_1k + 999) / 1000| is an arithmetic way to round up | |
91 // |sample_1k / 1000| | |
92 buffer_size_frames_ = (sample_1k + 999) / 1000 + max_frames_; | |
93 audio_buffer_.resize(buffer_size_frames_ * num_channels_); | |
94 for (auto state : states_) { | |
95 state->Reset(); | |
96 } | |
97 } | |
98 | |
99 void RepetitionDetector::AddFramesToBuffer(const float* data, | |
100 size_t num_frames) { | |
101 DCHECK_LE(num_frames, buffer_size_frames_); | |
102 const size_t margin = buffer_size_frames_ - buffer_end_index_; | |
103 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_; | |
104 if (num_frames <= margin) { | |
105 std::copy(data, data + num_frames * num_channels_, it); | |
106 buffer_end_index_ += num_frames; | |
107 } else { | |
108 std::copy(data, data + margin * num_channels_, it); | |
109 std::copy(data + margin * num_channels_, data + num_frames * num_channels_, | |
110 audio_buffer_.begin()); | |
111 buffer_end_index_ = num_frames - margin; | |
112 } | |
113 } | |
114 | |
115 bool RepetitionDetector::Equal(const float* frame, | |
116 int look_back_frames) const { | |
117 const size_t look_back_index = | |
118 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) % | |
119 buffer_size_frames_ ; | |
120 auto it = audio_buffer_.begin() + look_back_index * num_channels_; | |
121 for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++frame, ++it) { | |
Henrik Grunell
2015/09/23 10:36:42
What is cdx?
minyue
2015/09/25 14:40:07
i'd rename it.
| |
122 if (*frame != *it) { | |
123 return false; | |
124 } | |
125 } | |
126 return true; | |
127 } | |
128 | |
129 bool RepetitionDetector::IsZero(const float* frame) const { | |
130 for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++frame) { | |
131 if (*frame != 0) { | |
132 return false; | |
133 } | |
134 } | |
135 return true; | |
136 } | |
137 | |
138 void RepetitionDetector::Detect(const float* data, size_t num_frames, | |
139 size_t num_channels, int sample_rate_hz) { | |
140 DCHECK_GT(states_.size(), 0ul); | |
141 if (num_channels != num_channels_ || sample_rate_hz != sample_rate_hz_) { | |
142 Reset(num_channels, sample_rate_hz); | |
143 } | |
144 | |
145 while (num_frames > max_frames_) { | |
146 Detect(data, max_frames_, num_channels, sample_rate_hz); | |
147 data += max_frames_ * num_channels; | |
148 num_frames -= max_frames_; | |
149 } | |
150 | |
151 if (num_frames == 0) | |
152 return; | |
153 | |
154 AddFramesToBuffer(data, num_frames); | |
155 | |
156 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) { | |
157 for (auto state : states_) { | |
158 const size_t look_back_frames = | |
159 rtc::CheckedDivExact(state->look_back_ms() * sample_rate_hz_, 1000); | |
160 // Equal(data, offset) checks if |data| equals the audio frame located | |
161 // |offset| frames from the end of buffer. Now a full frame has been | |
162 // inserted to the buffer, and thus |offset| should compensate for it. | |
163 if (Equal(data, look_back_frames + idx)) { | |
164 if (!state->reported()) { | |
165 state->Increment(IsZero(data)); | |
166 if (state->HasValidReport(sample_rate_hz)) { | |
167 ReportRepetition(state->id()); | |
Henrik Grunell
2015/09/23 10:36:41
This should be reported at the end of a session as
minyue
2015/09/25 14:40:07
Yes, what to add as UMA in this case is really dis
| |
168 state->set_reported(true); | |
169 } | |
170 } | |
171 } else { | |
172 state->Reset(); | |
173 } | |
174 } | |
175 } | |
176 } | |
177 | |
178 void RepetitionDetector::ReportRepetition(int id) { | |
179 UMA_HISTOGRAM_CUSTOM_ENUMERATION( | |
180 "Media.AudioCapturerRepetition", id, ids_); | |
181 } | |
182 | |
183 } // namespace content | |
OLD | NEW |