Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Unified Diff: content/renderer/media/repetition_detector.cc

Issue 1357013006: Add detection for repeated audio in capturing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixing dll Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/media/repetition_detector.cc
diff --git a/content/renderer/media/repetition_detector.cc b/content/renderer/media/repetition_detector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ad63c8115e0fda8e70bf734a875671dd66e86ce3
--- /dev/null
+++ b/content/renderer/media/repetition_detector.cc
@@ -0,0 +1,183 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/media/repetition_detector.h"
+
+#include "base/macros.h" // arraysize
Henrik Grunell 2015/09/23 10:36:41 Remove comments.
minyue 2015/09/25 14:40:07 Done.
+#include "base/metrics/histogram_macros.h" // UMA_HISTOGRAM_CUSTOM_ENUMERATION
+#include "base/numerics/safe_conversions.h" // checked_cast
+#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.
+
+namespace content {
+
+namespace {
+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.
+ // {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.
+ {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.
+ {2, 20, 10},
+ {3, 30, 10},
+ {4, 40, 10},
+ {5, 50, 10},
+ {6, 60, 10},
+ {7, 70, 10},
+ {8, 80, 10},
+ {9, 90, 10},
+ {10, 100, 10},
+ {20, 200, 10},
+};
+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.
+}
Henrik Grunell 2015/09/23 10:36:42 Empty line before. Add " // namespace"
minyue 2015/09/25 14:40:07 Done.
+
+RepetitionDetector::State::State(int id, int look_back_ms, int min_length_ms)
+ : id_(id),
+ look_back_ms_(look_back_ms),
+ min_length_ms_(min_length_ms) {
+ Reset();
+}
+
+void RepetitionDetector::State::Increment(bool zero) {
+ 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
+ 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
+ }
+ ++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.
+ if (!zero) {
+ 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
+ }
+}
+
+bool RepetitionDetector::State::HasValidReport(int sample_rate_hz) const {
+ return (!all_zero_ && count_frames_ >=
+ 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_
+}
+
+void RepetitionDetector::State::Reset() {
+ count_frames_ = 0;
+ all_zero_ = true;
+ reported_ = false;
+}
+
+RepetitionDetector::RepetitionDetector()
+ : max_look_back_ms_(0),
+ sample_rate_hz_(0),
+ buffer_size_frames_(0),
+ buffer_end_index_(0),
+ max_frames_(kMaxFrames) {
+ RegisterRepetitionPatterns(kRepetitionPatterns,
+ arraysize(kRepetitionPatterns));
+}
+
+RepetitionDetector::~RepetitionDetector() = default;
+
+void RepetitionDetector::RegisterRepetitionPatterns(const Pattern* patterns,
+ size_t num_patterns) {
+ Pattern pattern;
+ for (size_t idx = 0; idx < num_patterns; idx++) {
+ pattern = patterns[idx];
+ 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
+ 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
+ pattern.min_length_ms_));
+ if (pattern.look_back_ms_ > max_look_back_ms_) {
+ max_look_back_ms_ = pattern.look_back_ms_;
+ }
+ }
+}
+
+void RepetitionDetector::Reset(size_t num_channels, int sample_rate_hz) {
+ num_channels_ = num_channels;
+ sample_rate_hz_ = sample_rate_hz;
+ 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
+ // |(sample_1k + 999) / 1000| is an arithmetic way to round up
+ // |sample_1k / 1000|
+ buffer_size_frames_ = (sample_1k + 999) / 1000 + max_frames_;
+ audio_buffer_.resize(buffer_size_frames_ * num_channels_);
+ for (auto state : states_) {
+ state->Reset();
+ }
+}
+
+void RepetitionDetector::AddFramesToBuffer(const float* data,
+ size_t num_frames) {
+ DCHECK_LE(num_frames, buffer_size_frames_);
+ const size_t margin = buffer_size_frames_ - buffer_end_index_;
+ const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_;
+ if (num_frames <= margin) {
+ std::copy(data, data + num_frames * num_channels_, it);
+ buffer_end_index_ += num_frames;
+ } else {
+ std::copy(data, data + margin * num_channels_, it);
+ std::copy(data + margin * num_channels_, data + num_frames * num_channels_,
+ audio_buffer_.begin());
+ buffer_end_index_ = num_frames - margin;
+ }
+}
+
+bool RepetitionDetector::Equal(const float* frame,
+ int look_back_frames) const {
+ const size_t look_back_index =
+ (buffer_end_index_ + buffer_size_frames_ - look_back_frames) %
+ buffer_size_frames_ ;
+ auto it = audio_buffer_.begin() + look_back_index * num_channels_;
+ 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.
+ if (*frame != *it) {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool RepetitionDetector::IsZero(const float* frame) const {
+ for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++frame) {
+ if (*frame != 0) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void RepetitionDetector::Detect(const float* data, size_t num_frames,
+ size_t num_channels, int sample_rate_hz) {
+ DCHECK_GT(states_.size(), 0ul);
+ if (num_channels != num_channels_ || sample_rate_hz != sample_rate_hz_) {
+ Reset(num_channels, sample_rate_hz);
+ }
+
+ while (num_frames > max_frames_) {
+ Detect(data, max_frames_, num_channels, sample_rate_hz);
+ data += max_frames_ * num_channels;
+ num_frames -= max_frames_;
+ }
+
+ if (num_frames == 0)
+ return;
+
+ AddFramesToBuffer(data, num_frames);
+
+ for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) {
+ for (auto state : states_) {
+ const size_t look_back_frames =
+ rtc::CheckedDivExact(state->look_back_ms() * sample_rate_hz_, 1000);
+ // Equal(data, offset) checks if |data| equals the audio frame located
+ // |offset| frames from the end of buffer. Now a full frame has been
+ // inserted to the buffer, and thus |offset| should compensate for it.
+ if (Equal(data, look_back_frames + idx)) {
+ if (!state->reported()) {
+ state->Increment(IsZero(data));
+ if (state->HasValidReport(sample_rate_hz)) {
+ 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
+ state->set_reported(true);
+ }
+ }
+ } else {
+ state->Reset();
+ }
+ }
+ }
+}
+
+void RepetitionDetector::ReportRepetition(int id) {
+ UMA_HISTOGRAM_CUSTOM_ENUMERATION(
+ "Media.AudioCapturerRepetition", id, ids_);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698