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

Unified Diff: content/renderer/media/audio_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: some small changes 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/audio_repetition_detector.cc
diff --git a/content/renderer/media/audio_repetition_detector.cc b/content/renderer/media/audio_repetition_detector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4a04b42f8f408ef2774e0caad71079921e188fc9
--- /dev/null
+++ b/content/renderer/media/audio_repetition_detector.cc
@@ -0,0 +1,196 @@
+// 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/audio_repetition_detector.h"
+
+#include "base/logging.h"
+#include "base/macros.h"
+#include "base/metrics/histogram_macros.h"
+
+namespace content {
+
+namespace {
+
+const AudioRepetitionDetector::Pattern kRepetitionPatterns[] = {
+ {1, 10, 10},
+ {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},
+};
+
+// This is used for increasing the efficiency of copying data into the buffer.
+// Input longer than |kMaxFrames| won't be a problem, and will be devided into
+// trunks automatically.
+const size_t kMaxFrames = 480; // 10 ms * 48 kHz
+
+} // namespace
+
+AudioRepetitionDetector::State::State(const Pattern &pattern)
+ : pattern_(pattern) {
+ Reset();
+}
+
+void AudioRepetitionDetector::State::Increment(bool zero) {
+ if (0 == count_frames_ && zero) {
+ // If a repetition starts with zeros, we enter the all zero mode until
+ // a non zero is found later. The point is that the beginning zeros should
+ // be counted in the length of the repetition as long as the repetition does
+ // not comprise only zeros.
+ all_zero_ = true;
+ }
+ ++count_frames_;
+ if (!zero)
+ all_zero_ = false;
+}
+
+bool AudioRepetitionDetector::State::HasValidReport(int sample_rate) const {
+ return (!all_zero_ && count_frames_ >=
+ static_cast<size_t>(pattern_.min_length_ms * sample_rate / 1000));
+}
+
+void AudioRepetitionDetector::State::Reset() {
+ count_frames_ = 0;
+ all_zero_ = true;
+ reported_ = false;
+}
+
+AudioRepetitionDetector::AudioRepetitionDetector()
+ : max_look_back_ms_(0),
+ sample_rate_(0),
+ buffer_size_frames_(0),
+ buffer_end_index_(0),
+ max_frames_(kMaxFrames) {
+ RegisterRepetitionPatterns(kRepetitionPatterns,
+ arraysize(kRepetitionPatterns));
+}
+
+AudioRepetitionDetector::~AudioRepetitionDetector() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+}
+
+void AudioRepetitionDetector::RegisterRepetitionPatterns(
+ const Pattern* patterns, size_t num_patterns) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ Pattern pattern;
+ for (size_t idx = 0; idx < num_patterns; idx++) {
+ pattern = patterns[idx];
+ ids_.push_back(pattern.id);
+ states_.push_back(new State(pattern));
+ if (pattern.look_back_ms > max_look_back_ms_)
+ max_look_back_ms_ = pattern.look_back_ms;
+ }
+}
+
+void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ num_channels_ = num_channels;
+ sample_rate_ = sample_rate;
+
+ // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|.
+ buffer_size_frames_ =
+ (max_look_back_ms_ * sample_rate_ + 999) / 1000 + max_frames_;
+
+ audio_buffer_.resize(buffer_size_frames_ * num_channels_);
+ for (auto state : states_)
+ state->Reset();
+}
+
+void AudioRepetitionDetector::AddFramesToBuffer(const float* data,
+ size_t num_frames) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ 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 AudioRepetitionDetector::Equal(const float* frame,
+ int look_back_frames) const {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ 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 channel = 0; channel < num_channels_; ++channel, ++frame, ++it) {
+ if (*frame != *it)
+ return false;
+ }
+ return true;
+}
+
+bool AudioRepetitionDetector::IsZero(const float* frame,
+ size_t num_channels) const {
+ for (size_t channel = 0; channel < num_channels; ++channel, ++frame) {
+ if (*frame != 0)
+ return false;
+ }
+ return true;
+}
+
+void AudioRepetitionDetector::Detect(const float* data, size_t num_frames,
+ size_t num_channels, int sample_rate) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK_GT(states_.size(), 0ul);
+ if (num_channels != num_channels_ || sample_rate != sample_rate_)
+ Reset(num_channels, sample_rate);
+
+ // The maximum number of frames |audio_buffer_| can take in is |max_frames_|.
+ // Therefore, input data with larger frames needs be divided into trunks.
ajm 2015/09/30 00:23:43 s/trunks/chunks
+ while (num_frames > max_frames_) {
+ Detect(data, max_frames_, num_channels, sample_rate);
+ 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_) {
+ // Look back position depends on the sample rate. It is rounded down to
+ // the closest integer.
+ const size_t look_back_frames =
+ state->look_back_ms() * sample_rate_ / 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, num_channels));
+ if (state->HasValidReport(sample_rate)) {
+ ReportRepetition(state->id());
+ state->set_reported(true);
+ }
+ }
+ } else {
+ state->Reset();
+ }
+ }
+ }
+}
+
+void AudioRepetitionDetector::ReportRepetition(int id) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ UMA_HISTOGRAM_CUSTOM_ENUMERATION(
+ "Media.AudioCapturerRepetition", id, ids_);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698