Chromium Code Reviews| 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/audio_repetition_detector.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/macros.h" | |
| 9 #include "base/metrics/histogram_macros.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 namespace { | |
| 14 // Minimum duration of a repetition. | |
| 15 const int kMinLengthMs = 1; | |
| 16 | |
| 17 // The following variables defines the look back time of repetitions that will | |
| 18 // be logged. The complexity of the detector is proportional to the number of | |
| 19 // look back times we keep track. | |
| 20 const int kMinLookbackTimeMS = 10; | |
|
Henrik Grunell
2015/10/16 07:59:50
Nit: Ms
minyue
2015/10/16 08:34:42
Oh, yes
| |
| 21 const int kMaxLookbackTimeMS = 200; | |
| 22 const int kLookbackTimeStepMS = 10; | |
| 23 | |
| 24 // This is used for increasing the efficiency of copying data into the buffer. | |
| 25 // Input longer than |kMaxFrames| won't be a problem, and will be devided into | |
| 26 // chunks automatically. | |
| 27 const size_t kMaxFrames = 480; // 10 ms * 48 kHz | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 AudioRepetitionDetector::AudioRepetitionDetector() | |
| 32 : max_look_back_ms_(0), | |
| 33 min_length_ms_(kMinLengthMs), | |
|
Henrik Grunell
2015/10/16 07:59:50
Can |min_length_ms_| be removed?
minyue
2015/10/16 08:34:42
|min_length_ms_| is important in the algorithm, an
Henrik Grunell
2015/10/16 08:44:11
I meant to use the constant instead since it never
minyue
2015/10/16 09:27:27
Sure it is not really subjected to change in curre
Henrik Grunell
2015/10/16 11:08:11
I'm not sure I understand why the test has a lower
minyue
2015/10/23 12:05:23
Already covered with offline discussion.
Adding a
| |
| 34 sample_rate_(0), | |
| 35 buffer_size_frames_(0), | |
| 36 buffer_end_index_(0), | |
| 37 max_frames_(kMaxFrames) { | |
|
Henrik Grunell
2015/10/16 07:59:50
Can |max_frames_| be removed?
minyue
2015/10/16 08:34:42
same as min_length_ms_
minyue
2015/10/16 09:27:28
|kMaxFrames| is set to be the largest possible fra
Henrik Grunell
2015/10/16 11:08:11
Can you explain why we need to test with some othe
| |
| 38 for (int time = kMaxLookbackTimeMS; time >= kMinLookbackTimeMS; | |
| 39 time -= kLookbackTimeStepMS) | |
|
Henrik Grunell
2015/10/16 07:59:50
Use {} (Since total for block is more than 2 lines
minyue
2015/10/16 08:34:42
Ok. But why alligning -=, I am not aware that line
Henrik Grunell
2015/10/16 08:44:11
To align the second line with the (.
for (int tim
minyue
2015/10/16 09:27:28
ok. these ;-separated lines seems to me different
Henrik Grunell
2015/10/16 11:08:11
Align as functions is common practice in Chromium.
ajm
2015/10/16 15:54:47
Just run git cl format and be done with it :)
| |
| 40 RegisterLookbackTime(time); | |
| 41 | |
| 42 // May be created in the main render thread and used in the audio threads. | |
| 43 thread_checker_.DetachFromThread(); | |
| 44 } | |
| 45 | |
| 46 AudioRepetitionDetector::~AudioRepetitionDetector() = default; | |
| 47 | |
| 48 void AudioRepetitionDetector::Detect(const float* data, size_t num_frames, | |
| 49 size_t num_channels, int sample_rate) { | |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 51 DCHECK(!states_.empty()); | |
| 52 | |
| 53 if (num_channels != num_channels_ || sample_rate != sample_rate_) | |
| 54 Reset(num_channels, sample_rate); | |
| 55 | |
| 56 // The maximum number of frames |audio_buffer_| can take in is |max_frames_|. | |
| 57 // Therefore, input data with larger frames needs be divided into chunks. | |
| 58 const size_t chunk_size = max_frames_ * num_channels; | |
| 59 while (num_frames > max_frames_) { | |
| 60 Detect(data, max_frames_, num_channels, sample_rate); | |
| 61 data += chunk_size; | |
| 62 num_frames -= max_frames_; | |
| 63 } | |
| 64 | |
| 65 if (num_frames == 0) | |
| 66 return; | |
| 67 | |
| 68 AddFramesToBuffer(data, num_frames); | |
| 69 | |
| 70 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) { | |
| 71 for (State* state : states_) { | |
| 72 // Look back position depends on the sample rate. It is rounded down to | |
| 73 // the closest integer. | |
| 74 const size_t look_back_frames = | |
| 75 state->look_back_ms() * sample_rate_ / 1000; | |
| 76 // Equal(data, offset) checks if |data| equals the audio frame located | |
| 77 // |offset| frames from the end of buffer. Now a full frame has been | |
| 78 // inserted to the buffer, and thus |offset| should compensate for it. | |
| 79 if (Equal(data, look_back_frames + idx)) { | |
| 80 if (!state->reported()) { | |
| 81 state->Increment(IsZero(data, num_channels)); | |
| 82 if (HasValidReport(state)) { | |
| 83 ReportRepetition(state->look_back_ms()); | |
| 84 state->set_reported(true); | |
| 85 } | |
| 86 } | |
| 87 } else { | |
| 88 state->Reset(); | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 AudioRepetitionDetector::State::State(int look_back_ms) | |
| 95 : look_back_ms_(look_back_ms) { | |
| 96 Reset(); | |
| 97 } | |
| 98 | |
| 99 void AudioRepetitionDetector::State::Increment(bool zero) { | |
| 100 if (0 == count_frames_ && zero) { | |
| 101 // If a repetition starts with zeros, we enter the all zero mode until | |
| 102 // a non zero is found later. The point is that the beginning zeros should | |
| 103 // be counted in the length of the repetition as long as the repetition does | |
| 104 // not comprise only zeros. | |
| 105 all_zero_ = true; | |
| 106 } | |
| 107 ++count_frames_; | |
| 108 if (!zero) | |
| 109 all_zero_ = false; | |
| 110 } | |
| 111 | |
| 112 void AudioRepetitionDetector::State::Reset() { | |
| 113 count_frames_ = 0; | |
| 114 all_zero_ = true; | |
| 115 reported_ = false; | |
| 116 } | |
| 117 | |
| 118 void AudioRepetitionDetector::RegisterLookbackTime(int look_back_ms) { | |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 120 | |
| 121 // States are added in the order of their look back times. | |
| 122 auto it = states_.begin(); | |
| 123 for (; it != states_.end(); ++it) { | |
| 124 const int it_look_back = (*it)->look_back_ms(); | |
| 125 if (it_look_back == look_back_ms) | |
| 126 return; | |
| 127 if (it_look_back < look_back_ms) | |
| 128 break; | |
| 129 } | |
| 130 states_.insert(it, new State(look_back_ms)); | |
| 131 if (look_back_ms > max_look_back_ms_) { | |
| 132 max_look_back_ms_ = look_back_ms; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate) { | |
| 137 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 138 num_channels_ = num_channels; | |
| 139 sample_rate_ = sample_rate; | |
| 140 | |
| 141 // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|. | |
| 142 buffer_size_frames_ = | |
| 143 (max_look_back_ms_ * sample_rate_ + 999) / 1000 + max_frames_; | |
| 144 | |
| 145 audio_buffer_.resize(buffer_size_frames_ * num_channels_); | |
| 146 for (State* state : states_) | |
| 147 state->Reset(); | |
| 148 } | |
| 149 | |
| 150 void AudioRepetitionDetector::AddFramesToBuffer(const float* data, | |
| 151 size_t num_frames) { | |
| 152 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 153 DCHECK_LE(num_frames, buffer_size_frames_); | |
| 154 const size_t margin = buffer_size_frames_ - buffer_end_index_; | |
| 155 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_; | |
| 156 if (num_frames <= margin) { | |
| 157 std::copy(data, data + num_frames * num_channels_, it); | |
| 158 buffer_end_index_ += num_frames; | |
| 159 } else { | |
| 160 std::copy(data, data + margin * num_channels_, it); | |
| 161 std::copy(data + margin * num_channels_, data + num_frames * num_channels_, | |
| 162 audio_buffer_.begin()); | |
| 163 buffer_end_index_ = num_frames - margin; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 bool AudioRepetitionDetector::Equal(const float* frame, | |
| 168 int look_back_frames) const { | |
| 169 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 170 const size_t look_back_index = | |
| 171 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) % | |
| 172 buffer_size_frames_ ; | |
| 173 auto it = audio_buffer_.begin() + look_back_index * num_channels_; | |
| 174 for (size_t channel = 0; channel < num_channels_; ++channel, ++frame, ++it) { | |
| 175 if (*frame != *it) | |
| 176 return false; | |
| 177 } | |
| 178 return true; | |
| 179 } | |
| 180 | |
| 181 bool AudioRepetitionDetector::IsZero(const float* frame, | |
| 182 size_t num_channels) const { | |
| 183 for (size_t channel = 0; channel < num_channels; ++channel, ++frame) { | |
| 184 if (*frame != 0) | |
| 185 return false; | |
| 186 } | |
| 187 return true; | |
| 188 } | |
| 189 | |
| 190 bool AudioRepetitionDetector::HasValidReport(const State* state) const { | |
| 191 return (!state->all_zero() && state->count_frames() >= | |
| 192 static_cast<size_t>(min_length_ms_ * sample_rate_ / 1000)); | |
| 193 } | |
| 194 | |
| 195 void AudioRepetitionDetector::ReportRepetition(int look_back_ms) { | |
| 196 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 197 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 198 "Media.AudioCapturerRepetition", look_back_ms, | |
| 199 kMinLookbackTimeMS, kMaxLookbackTimeMS, | |
| 200 (kMaxLookbackTimeMS - kMinLookbackTimeMS) / kLookbackTimeStepMS + 1); | |
| 201 } | |
| 202 | |
| 203 } // namespace content | |
| OLD | NEW |