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

Side by Side Diff: media/base/audio_splicer.cc

Issue 1229123002: Log audio splice sanitizer warnings and errors to media-internals (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address xhwang@'s comments and fix the unit test Created 5 years, 5 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 unified diff | Download patch
« no previous file with comments | « media/base/audio_splicer.h ('k') | media/base/audio_splicer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/base/audio_splicer.h" 5 #include "media/base/audio_splicer.h"
6 6
7 #include <cstdlib> 7 #include <cstdlib>
8 #include <deque> 8 #include <deque>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "media/base/audio_buffer.h" 11 #include "media/base/audio_buffer.h"
12 #include "media/base/audio_bus.h" 12 #include "media/base/audio_bus.h"
13 #include "media/base/audio_decoder_config.h" 13 #include "media/base/audio_decoder_config.h"
14 #include "media/base/audio_timestamp_helper.h" 14 #include "media/base/audio_timestamp_helper.h"
15 #include "media/base/media_log.h"
15 #include "media/base/vector_math.h" 16 #include "media/base/vector_math.h"
16 17
17 namespace media { 18 namespace media {
18 19
19 // Minimum gap size needed before the splicer will take action to 20 namespace {
20 // fill a gap. This avoids periodically inserting and then dropping samples 21
21 // when the buffer timestamps are slightly off because of timestamp rounding 22 enum {
22 // in the source content. Unit is frames. 23 // Minimum gap size needed before the splicer will take action to
23 static const int kMinGapSize = 2; 24 // fill a gap. This avoids periodically inserting and then dropping samples
25 // when the buffer timestamps are slightly off because of timestamp rounding
26 // in the source content. Unit is frames.
27 kMinGapSize = 2,
28
29 // Limits the number of MEDIA_LOG() per sanitizer instance warning the user
30 // about splicer overlaps within |kMaxTimeDeltaInMilliseconds| or gaps larger
31 // than |kMinGapSize| and less than |kMaxTimeDeltaInMilliseconds|. These
32 // warnings may be frequent for some streams, and number of sanitizer
33 // instances may be high, so keep this limit low to help reduce log spam.
34 kMaxSanitizerWarningLogs = 5,
35 };
24 36
25 // AudioBuffer::TrimStart() is not as accurate as the timestamp helper, so 37 // AudioBuffer::TrimStart() is not as accurate as the timestamp helper, so
26 // manually adjust the duration and timestamp after trimming. 38 // manually adjust the duration and timestamp after trimming.
27 static void AccurateTrimStart(int frames_to_trim, 39 void AccurateTrimStart(int frames_to_trim,
28 const scoped_refptr<AudioBuffer> buffer, 40 const scoped_refptr<AudioBuffer> buffer,
29 const AudioTimestampHelper& timestamp_helper) { 41 const AudioTimestampHelper& timestamp_helper) {
30 buffer->TrimStart(frames_to_trim); 42 buffer->TrimStart(frames_to_trim);
31 buffer->set_timestamp(timestamp_helper.GetTimestamp()); 43 buffer->set_timestamp(timestamp_helper.GetTimestamp());
32 } 44 }
33 45
34 // Returns an AudioBus whose frame buffer is backed by the provided AudioBuffer. 46 // Returns an AudioBus whose frame buffer is backed by the provided AudioBuffer.
35 static scoped_ptr<AudioBus> CreateAudioBufferWrapper( 47 scoped_ptr<AudioBus> CreateAudioBufferWrapper(
36 const scoped_refptr<AudioBuffer>& buffer) { 48 const scoped_refptr<AudioBuffer>& buffer) {
37 scoped_ptr<AudioBus> wrapper = 49 scoped_ptr<AudioBus> wrapper =
38 AudioBus::CreateWrapper(buffer->channel_count()); 50 AudioBus::CreateWrapper(buffer->channel_count());
39 wrapper->set_frames(buffer->frame_count()); 51 wrapper->set_frames(buffer->frame_count());
40 for (int ch = 0; ch < buffer->channel_count(); ++ch) { 52 for (int ch = 0; ch < buffer->channel_count(); ++ch) {
41 wrapper->SetChannelData( 53 wrapper->SetChannelData(
42 ch, reinterpret_cast<float*>(buffer->channel_data()[ch])); 54 ch, reinterpret_cast<float*>(buffer->channel_data()[ch]));
43 } 55 }
44 return wrapper.Pass(); 56 return wrapper.Pass();
45 } 57 }
46 58
59 } // namespace
60
47 class AudioStreamSanitizer { 61 class AudioStreamSanitizer {
48 public: 62 public:
49 explicit AudioStreamSanitizer(int samples_per_second); 63 AudioStreamSanitizer(int samples_per_second,
64 const scoped_refptr<MediaLog>& media_log);
50 ~AudioStreamSanitizer(); 65 ~AudioStreamSanitizer();
51 66
52 // Resets the sanitizer state by clearing the output buffers queue, and 67 // Resets the sanitizer state by clearing the output buffers queue, and
53 // resetting the timestamp helper. 68 // resetting the timestamp helper.
54 void Reset(); 69 void Reset();
55 70
56 // Similar to Reset(), but initializes the timestamp helper with the given 71 // Similar to Reset(), but initializes the timestamp helper with the given
57 // parameters. 72 // parameters.
58 void ResetTimestampState(int64 frame_count, base::TimeDelta base_timestamp); 73 void ResetTimestampState(int64 frame_count, base::TimeDelta base_timestamp);
59 74
(...skipping 22 matching lines...) Expand all
82 97
83 private: 98 private:
84 void AddOutputBuffer(const scoped_refptr<AudioBuffer>& buffer); 99 void AddOutputBuffer(const scoped_refptr<AudioBuffer>& buffer);
85 100
86 AudioTimestampHelper output_timestamp_helper_; 101 AudioTimestampHelper output_timestamp_helper_;
87 bool received_end_of_stream_; 102 bool received_end_of_stream_;
88 103
89 typedef std::deque<scoped_refptr<AudioBuffer> > BufferQueue; 104 typedef std::deque<scoped_refptr<AudioBuffer> > BufferQueue;
90 BufferQueue output_buffers_; 105 BufferQueue output_buffers_;
91 106
107 scoped_refptr<MediaLog> media_log_;
108
109 // To prevent log spam, counts the number of audio gap or overlaps warned in
110 // logs.
111 int num_warning_logs_;
112
92 DISALLOW_ASSIGN(AudioStreamSanitizer); 113 DISALLOW_ASSIGN(AudioStreamSanitizer);
93 }; 114 };
94 115
95 AudioStreamSanitizer::AudioStreamSanitizer(int samples_per_second) 116 AudioStreamSanitizer::AudioStreamSanitizer(
117 int samples_per_second,
118 const scoped_refptr<MediaLog>& media_log)
96 : output_timestamp_helper_(samples_per_second), 119 : output_timestamp_helper_(samples_per_second),
97 received_end_of_stream_(false) {} 120 received_end_of_stream_(false),
121 media_log_(media_log),
122 num_warning_logs_(0) {
123 }
98 124
99 AudioStreamSanitizer::~AudioStreamSanitizer() {} 125 AudioStreamSanitizer::~AudioStreamSanitizer() {}
100 126
101 void AudioStreamSanitizer::Reset() { 127 void AudioStreamSanitizer::Reset() {
102 ResetTimestampState(0, kNoTimestamp()); 128 ResetTimestampState(0, kNoTimestamp());
103 } 129 }
104 130
105 void AudioStreamSanitizer::ResetTimestampState(int64 frame_count, 131 void AudioStreamSanitizer::ResetTimestampState(int64 frame_count,
106 base::TimeDelta base_timestamp) { 132 base::TimeDelta base_timestamp) {
107 output_buffers_.clear(); 133 output_buffers_.clear();
(...skipping 13 matching lines...) Expand all
121 } 147 }
122 148
123 DCHECK(input->timestamp() != kNoTimestamp()); 149 DCHECK(input->timestamp() != kNoTimestamp());
124 DCHECK(input->duration() > base::TimeDelta()); 150 DCHECK(input->duration() > base::TimeDelta());
125 DCHECK_GT(input->frame_count(), 0); 151 DCHECK_GT(input->frame_count(), 0);
126 152
127 if (output_timestamp_helper_.base_timestamp() == kNoTimestamp()) 153 if (output_timestamp_helper_.base_timestamp() == kNoTimestamp())
128 output_timestamp_helper_.SetBaseTimestamp(input->timestamp()); 154 output_timestamp_helper_.SetBaseTimestamp(input->timestamp());
129 155
130 if (output_timestamp_helper_.base_timestamp() > input->timestamp()) { 156 if (output_timestamp_helper_.base_timestamp() > input->timestamp()) {
131 DVLOG(1) << "Input timestamp is before the base timestamp."; 157 MEDIA_LOG(ERROR, media_log_)
158 << "Audio splicing failed: unexpected timestamp sequence. base "
159 "timestamp="
160 << output_timestamp_helper_.base_timestamp().InMicroseconds()
161 << "us, input timestamp=" << input->timestamp().InMicroseconds()
162 << "us";
132 return false; 163 return false;
133 } 164 }
134 165
135 const base::TimeDelta timestamp = input->timestamp(); 166 const base::TimeDelta timestamp = input->timestamp();
136 const base::TimeDelta expected_timestamp = 167 const base::TimeDelta expected_timestamp =
137 output_timestamp_helper_.GetTimestamp(); 168 output_timestamp_helper_.GetTimestamp();
138 const base::TimeDelta delta = timestamp - expected_timestamp; 169 const base::TimeDelta delta = timestamp - expected_timestamp;
139 170
140 if (std::abs(delta.InMilliseconds()) > 171 if (std::abs(delta.InMilliseconds()) >
141 AudioSplicer::kMaxTimeDeltaInMilliseconds) { 172 AudioSplicer::kMaxTimeDeltaInMilliseconds) {
142 DVLOG(1) << "Timestamp delta too large: " << delta.InMicroseconds() << "us"; 173 MEDIA_LOG(ERROR, media_log_)
174 << "Audio splicing failed: coded frame timestamp differs from "
175 "expected timestamp " << expected_timestamp.InMicroseconds()
176 << "us by " << delta.InMicroseconds()
177 << "us, more than threshold of +/-"
178 << AudioSplicer::kMaxTimeDeltaInMilliseconds
179 << "ms. Expected timestamp is based on decoded frames and frame rate.";
143 return false; 180 return false;
144 } 181 }
145 182
146 int frames_to_fill = 0; 183 int frames_to_fill = 0;
147 if (delta != base::TimeDelta()) 184 if (delta != base::TimeDelta())
148 frames_to_fill = output_timestamp_helper_.GetFramesToTarget(timestamp); 185 frames_to_fill = output_timestamp_helper_.GetFramesToTarget(timestamp);
149 186
150 if (frames_to_fill == 0 || std::abs(frames_to_fill) < kMinGapSize) { 187 if (frames_to_fill == 0 || std::abs(frames_to_fill) < kMinGapSize) {
151 AddOutputBuffer(input); 188 AddOutputBuffer(input);
152 return true; 189 return true;
153 } 190 }
154 191
155 if (frames_to_fill > 0) { 192 if (frames_to_fill > 0) {
193 LIMITED_MEDIA_LOG(DEBUG, media_log_, num_warning_logs_,
194 kMaxSanitizerWarningLogs)
195 << "Audio splicer inserting silence for small gap of "
196 << delta.InMicroseconds() << "us at time "
197 << expected_timestamp.InMicroseconds() << "us.";
156 DVLOG(1) << "Gap detected @ " << expected_timestamp.InMicroseconds() 198 DVLOG(1) << "Gap detected @ " << expected_timestamp.InMicroseconds()
157 << " us: " << delta.InMicroseconds() << " us"; 199 << " us: " << delta.InMicroseconds() << " us";
158 200
159 // Create a buffer with enough silence samples to fill the gap and 201 // Create a buffer with enough silence samples to fill the gap and
160 // add it to the output buffer. 202 // add it to the output buffer.
161 scoped_refptr<AudioBuffer> gap = 203 scoped_refptr<AudioBuffer> gap =
162 AudioBuffer::CreateEmptyBuffer(input->channel_layout(), 204 AudioBuffer::CreateEmptyBuffer(input->channel_layout(),
163 input->channel_count(), 205 input->channel_count(),
164 input->sample_rate(), 206 input->sample_rate(),
165 frames_to_fill, 207 frames_to_fill,
166 expected_timestamp); 208 expected_timestamp);
167 AddOutputBuffer(gap); 209 AddOutputBuffer(gap);
168 210
169 // Add the input buffer now that the gap has been filled. 211 // Add the input buffer now that the gap has been filled.
170 AddOutputBuffer(input); 212 AddOutputBuffer(input);
171 return true; 213 return true;
172 } 214 }
173 215
174 // Overlapping buffers marked as splice frames are handled by AudioSplicer, 216 // Overlapping buffers marked as splice frames are handled by AudioSplicer,
175 // but decoder and demuxer quirks may sometimes produce overlapping samples 217 // but decoder and demuxer quirks may sometimes produce overlapping samples
176 // which need to be sanitized. 218 // which need to be sanitized.
177 // 219 //
178 // A crossfade can't be done here because only the current buffer is available 220 // A crossfade can't be done here because only the current buffer is available
179 // at this point, not previous buffers. 221 // at this point, not previous buffers.
222 LIMITED_MEDIA_LOG(DEBUG, media_log_, num_warning_logs_,
223 kMaxSanitizerWarningLogs)
224 << "Audio splicer skipping frames for small overlap of "
225 << -delta.InMicroseconds() << "us at time "
226 << expected_timestamp.InMicroseconds() << "us.";
180 DVLOG(1) << "Overlap detected @ " << expected_timestamp.InMicroseconds() 227 DVLOG(1) << "Overlap detected @ " << expected_timestamp.InMicroseconds()
181 << " us: " << -delta.InMicroseconds() << " us"; 228 << " us: " << -delta.InMicroseconds() << " us";
182 229
183 const int frames_to_skip = -frames_to_fill; 230 const int frames_to_skip = -frames_to_fill;
184 if (input->frame_count() <= frames_to_skip) { 231 if (input->frame_count() <= frames_to_skip) {
185 DVLOG(1) << "Dropping whole buffer"; 232 DVLOG(1) << "Dropping whole buffer";
186 return true; 233 return true;
187 } 234 }
188 235
189 // Copy the trailing samples that do not overlap samples already output 236 // Copy the trailing samples that do not overlap samples already output
(...skipping 30 matching lines...) Expand all
220 } 267 }
221 268
222 bool AudioStreamSanitizer::DrainInto(AudioStreamSanitizer* output) { 269 bool AudioStreamSanitizer::DrainInto(AudioStreamSanitizer* output) {
223 while (HasNextBuffer()) { 270 while (HasNextBuffer()) {
224 if (!output->AddInput(GetNextBuffer())) 271 if (!output->AddInput(GetNextBuffer()))
225 return false; 272 return false;
226 } 273 }
227 return true; 274 return true;
228 } 275 }
229 276
230 AudioSplicer::AudioSplicer(int samples_per_second) 277 AudioSplicer::AudioSplicer(int samples_per_second,
278 const scoped_refptr<MediaLog>& media_log)
231 : max_crossfade_duration_( 279 : max_crossfade_duration_(
232 base::TimeDelta::FromMilliseconds(kCrossfadeDurationInMilliseconds)), 280 base::TimeDelta::FromMilliseconds(kCrossfadeDurationInMilliseconds)),
233 splice_timestamp_(kNoTimestamp()), 281 splice_timestamp_(kNoTimestamp()),
234 max_splice_end_timestamp_(kNoTimestamp()), 282 max_splice_end_timestamp_(kNoTimestamp()),
235 output_sanitizer_(new AudioStreamSanitizer(samples_per_second)), 283 output_sanitizer_(
236 pre_splice_sanitizer_(new AudioStreamSanitizer(samples_per_second)), 284 new AudioStreamSanitizer(samples_per_second, media_log)),
237 post_splice_sanitizer_(new AudioStreamSanitizer(samples_per_second)), 285 pre_splice_sanitizer_(
238 have_all_pre_splice_buffers_(false) {} 286 new AudioStreamSanitizer(samples_per_second, media_log)),
287 post_splice_sanitizer_(
288 new AudioStreamSanitizer(samples_per_second, media_log)),
289 have_all_pre_splice_buffers_(false) {
290 }
239 291
240 AudioSplicer::~AudioSplicer() {} 292 AudioSplicer::~AudioSplicer() {}
241 293
242 void AudioSplicer::Reset() { 294 void AudioSplicer::Reset() {
243 output_sanitizer_->Reset(); 295 output_sanitizer_->Reset();
244 pre_splice_sanitizer_->Reset(); 296 pre_splice_sanitizer_->Reset();
245 post_splice_sanitizer_->Reset(); 297 post_splice_sanitizer_->Reset();
246 have_all_pre_splice_buffers_ = false; 298 have_all_pre_splice_buffers_ = false;
247 reset_splice_timestamps(); 299 reset_splice_timestamps();
248 } 300 }
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 AccurateTrimStart(frames_to_trim, remainder, output_ts_helper); 550 AccurateTrimStart(frames_to_trim, remainder, output_ts_helper);
499 CHECK(output_sanitizer_->AddInput(remainder)); 551 CHECK(output_sanitizer_->AddInput(remainder));
500 } 552 }
501 553
502 // Transfer all remaining buffers out and reset once empty. 554 // Transfer all remaining buffers out and reset once empty.
503 CHECK(post_splice_sanitizer_->DrainInto(output_sanitizer_.get())); 555 CHECK(post_splice_sanitizer_->DrainInto(output_sanitizer_.get()));
504 post_splice_sanitizer_->Reset(); 556 post_splice_sanitizer_->Reset();
505 } 557 }
506 558
507 } // namespace media 559 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_splicer.h ('k') | media/base/audio_splicer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698