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

Side by Side Diff: media/audio/audio_output_resampler.cc

Issue 2582703003: Audio output debug recording. (Closed)
Patch Set: Code review. Created 3 years, 10 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
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/audio/audio_output_resampler.h" 5 #include "media/audio/audio_output_resampler.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <string> 10 #include <string>
(...skipping 15 matching lines...) Expand all
26 #include "media/base/audio_timestamp_helper.h" 26 #include "media/base/audio_timestamp_helper.h"
27 #include "media/base/limits.h" 27 #include "media/base/limits.h"
28 28
29 namespace media { 29 namespace media {
30 30
31 class OnMoreDataConverter 31 class OnMoreDataConverter
32 : public AudioOutputStream::AudioSourceCallback, 32 : public AudioOutputStream::AudioSourceCallback,
33 public AudioConverter::InputCallback { 33 public AudioConverter::InputCallback {
34 public: 34 public:
35 OnMoreDataConverter(const AudioParameters& input_params, 35 OnMoreDataConverter(const AudioParameters& input_params,
36 const AudioParameters& output_params); 36 const AudioParameters& output_params,
37 std::unique_ptr<AudioDebugRecorder> debug_recorder);
37 ~OnMoreDataConverter() override; 38 ~OnMoreDataConverter() override;
38 39
39 // AudioSourceCallback interface. 40 // AudioSourceCallback interface.
40 int OnMoreData(base::TimeDelta delay, 41 int OnMoreData(base::TimeDelta delay,
41 base::TimeTicks delay_timestamp, 42 base::TimeTicks delay_timestamp,
42 int prior_frames_skipped, 43 int prior_frames_skipped,
43 AudioBus* dest) override; 44 AudioBus* dest) override;
44 void OnError(AudioOutputStream* stream) override; 45 void OnError(AudioOutputStream* stream) override;
45 46
46 // Sets |source_callback_|. If this is not a new object, then Stop() must be 47 // Sets |source_callback_|. If this is not a new object, then Stop() must be
47 // called before Start(). 48 // called before Start().
48 void Start(AudioOutputStream::AudioSourceCallback* callback); 49 void Start(AudioOutputStream::AudioSourceCallback* callback);
49 50
50 // Clears |source_callback_| and flushes the resampler. 51 // Clears |source_callback_| and flushes the resampler.
51 void Stop(); 52 void Stop();
52 53
53 bool started() const { return source_callback_ != nullptr; } 54 bool started() const { return source_callback_ != nullptr; }
54 55
55 bool error_occurred() const { return error_occurred_; } 56 bool error_occurred() const { return error_occurred_; }
56 57
58 const AudioParameters& output_params() const { return output_params_; }
59
57 private: 60 private:
58 // AudioConverter::InputCallback implementation. 61 // AudioConverter::InputCallback implementation.
59 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; 62 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override;
60 63
61 // Ratio of input bytes to output bytes used to correct playback delay with 64 // Ratio of input bytes to output bytes used to correct playback delay with
62 // regard to buffering and resampling. 65 // regard to buffering and resampling.
63 const double io_ratio_; 66 const double io_ratio_;
64 67
65 // Source callback. 68 // Source callback.
66 AudioOutputStream::AudioSourceCallback* source_callback_; 69 AudioOutputStream::AudioSourceCallback* source_callback_;
67 70
68 // Last |delay| and |delay_timestamp| received via OnMoreData(). Used to 71 // Last |delay| and |delay_timestamp| received via OnMoreData(). Used to
69 // correct playback delay in ProvideInput() before calling |source_callback_|. 72 // correct playback delay in ProvideInput() before calling |source_callback_|.
70 base::TimeDelta current_delay_; 73 base::TimeDelta current_delay_;
71 base::TimeTicks current_delay_timestamp_; 74 base::TimeTicks current_delay_timestamp_;
72 75
73 const int input_samples_per_second_; 76 const int input_samples_per_second_;
74 77
75 // Handles resampling, buffering, and channel mixing between input and output 78 // Handles resampling, buffering, and channel mixing between input and output
76 // parameters. 79 // parameters.
77 AudioConverter audio_converter_; 80 AudioConverter audio_converter_;
78 81
79 // True if OnError() was ever called. Should only be read if the underlying 82 // True if OnError() was ever called. Should only be read if the underlying
80 // stream has been stopped. 83 // stream has been stopped.
81 bool error_occurred_; 84 bool error_occurred_;
82 85
83 // Information about input and output buffer sizes to be traced. 86 // Information about input buffer sizes to be traced.
84 const int input_buffer_size_; 87 const int input_buffer_size_;
85 const int output_buffer_size_; 88
89 // Output parameters used for buffer size tracing and for users to get.
90 const AudioParameters output_params_;
91
92 // For audio debug recordings.
93 std::unique_ptr<AudioDebugRecorder> debug_recorder_;
86 94
87 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); 95 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter);
88 }; 96 };
89 97
90 // Record UMA statistics for hardware output configuration. 98 // Record UMA statistics for hardware output configuration.
91 static void RecordStats(const AudioParameters& output_params) { 99 static void RecordStats(const AudioParameters& output_params) {
92 // Note the 'PRESUBMIT_IGNORE_UMA_MAX's below, these silence the PRESUBMIT.py 100 // Note the 'PRESUBMIT_IGNORE_UMA_MAX's below, these silence the PRESUBMIT.py
93 // check for uma enum max usage, since we're abusing UMA_HISTOGRAM_ENUMERATION 101 // check for uma enum max usage, since we're abusing UMA_HISTOGRAM_ENUMERATION
94 // to report a discrete value. 102 // to report a discrete value.
95 UMA_HISTOGRAM_ENUMERATION( 103 UMA_HISTOGRAM_ENUMERATION(
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 216
209 output_params_ = AudioParameters( 217 output_params_ = AudioParameters(
210 AudioParameters::AUDIO_PCM_LINEAR, params_.channel_layout(), 218 AudioParameters::AUDIO_PCM_LINEAR, params_.channel_layout(),
211 params_.sample_rate(), params_.bits_per_sample(), 219 params_.sample_rate(), params_.bits_per_sample(),
212 frames_per_buffer); 220 frames_per_buffer);
213 device_id_ = ""; 221 device_id_ = "";
214 Initialize(); 222 Initialize();
215 #endif 223 #endif
216 } 224 }
217 225
218 AudioOutputResampler::AudioOutputResampler(AudioManager* audio_manager, 226 AudioOutputResampler::AudioOutputResampler(
219 const AudioParameters& input_params, 227 AudioManager* audio_manager,
220 const AudioParameters& output_params, 228 const AudioParameters& input_params,
221 const std::string& output_device_id, 229 const AudioParameters& output_params,
222 const base::TimeDelta& close_delay) 230 const std::string& output_device_id,
231 base::TimeDelta close_delay,
232 const RegisterDebugRecordingSourceCallback&
233 register_debug_recording_source_callback)
223 : AudioOutputDispatcher(audio_manager, input_params, output_device_id), 234 : AudioOutputDispatcher(audio_manager, input_params, output_device_id),
224 close_delay_(close_delay), 235 close_delay_(close_delay),
225 output_params_(output_params), 236 output_params_(output_params),
226 original_output_params_(output_params), 237 original_output_params_(output_params),
227 streams_opened_(false), 238 streams_opened_(false),
228 reinitialize_timer_(FROM_HERE, 239 reinitialize_timer_(FROM_HERE,
229 close_delay_, 240 close_delay_,
230 base::Bind(&AudioOutputResampler::Reinitialize, 241 base::Bind(&AudioOutputResampler::Reinitialize,
231 base::Unretained(this)), 242 base::Unretained(this)),
232 false), 243 false),
244 register_debug_recording_source_callback_(
245 register_debug_recording_source_callback),
233 weak_factory_(this) { 246 weak_factory_(this) {
234 DCHECK(input_params.IsValid()); 247 DCHECK(input_params.IsValid());
235 DCHECK(output_params.IsValid()); 248 DCHECK(output_params.IsValid());
236 DCHECK_EQ(output_params_.format(), AudioParameters::AUDIO_PCM_LOW_LATENCY); 249 DCHECK_EQ(output_params_.format(), AudioParameters::AUDIO_PCM_LOW_LATENCY);
237 250
238 // Record UMA statistics for the hardware configuration. 251 // Record UMA statistics for the hardware configuration.
239 RecordStats(output_params); 252 RecordStats(output_params);
240 253
241 Initialize(); 254 Initialize();
242 } 255 }
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 } 347 }
335 348
336 bool AudioOutputResampler::StartStream( 349 bool AudioOutputResampler::StartStream(
337 AudioOutputStream::AudioSourceCallback* callback, 350 AudioOutputStream::AudioSourceCallback* callback,
338 AudioOutputProxy* stream_proxy) { 351 AudioOutputProxy* stream_proxy) {
339 DCHECK(task_runner_->BelongsToCurrentThread()); 352 DCHECK(task_runner_->BelongsToCurrentThread());
340 353
341 OnMoreDataConverter* resampler_callback = nullptr; 354 OnMoreDataConverter* resampler_callback = nullptr;
342 CallbackMap::iterator it = callbacks_.find(stream_proxy); 355 CallbackMap::iterator it = callbacks_.find(stream_proxy);
343 if (it == callbacks_.end()) { 356 if (it == callbacks_.end()) {
344 resampler_callback = new OnMoreDataConverter(params_, output_params_); 357 std::unique_ptr<AudioDebugRecorder> debug_recorder =
358 register_debug_recording_source_callback_.Run(output_params_);
359 resampler_callback = new OnMoreDataConverter(params_, output_params_,
360 std::move(debug_recorder));
345 callbacks_[stream_proxy] = 361 callbacks_[stream_proxy] =
346 base::WrapUnique<OnMoreDataConverter>(resampler_callback); 362 base::WrapUnique<OnMoreDataConverter>(resampler_callback);
347 } else { 363 } else {
348 resampler_callback = it->second.get(); 364 resampler_callback = it->second.get();
349 } 365 }
350 366
351 resampler_callback->Start(callback); 367 resampler_callback->Start(callback);
352 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); 368 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy);
353 if (!result) 369 if (!result)
354 resampler_callback->Stop(); 370 resampler_callback->Stop();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 // Now that StopStream() has completed the underlying physical stream should 415 // Now that StopStream() has completed the underlying physical stream should
400 // be stopped and no longer calling OnMoreData(), making it safe to Stop() the 416 // be stopped and no longer calling OnMoreData(), making it safe to Stop() the
401 // OnMoreDataConverter. 417 // OnMoreDataConverter.
402 callback->Stop(); 418 callback->Stop();
403 419
404 // Destroy idle streams if any errors occurred during output; this ensures 420 // Destroy idle streams if any errors occurred during output; this ensures
405 // bad streams will not be reused. Note: Errors may occur during the Stop() 421 // bad streams will not be reused. Note: Errors may occur during the Stop()
406 // call above. 422 // call above.
407 if (callback->error_occurred()) 423 if (callback->error_occurred())
408 dispatcher_->CloseAllIdleStreams(); 424 dispatcher_->CloseAllIdleStreams();
425
426 // TODO BEFORE COMMIT: I think we need to clear the recorder wrapper here and
427 // register again at start. Seems like converters are re-used.
o1ka 2017/01/31 11:00:11 Please figure this out. Now we register recording
Henrik Grunell 2017/02/08 11:29:38 I've looked into this now. We should unregister wh
409 } 428 }
410 429
411 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, 430 OnMoreDataConverter::OnMoreDataConverter(
412 const AudioParameters& output_params) 431 const AudioParameters& input_params,
432 const AudioParameters& output_params,
433 std::unique_ptr<AudioDebugRecorder> debug_recorder)
413 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / 434 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) /
414 output_params.GetBytesPerSecond()), 435 output_params.GetBytesPerSecond()),
415 source_callback_(nullptr), 436 source_callback_(nullptr),
416 input_samples_per_second_(input_params.sample_rate()), 437 input_samples_per_second_(input_params.sample_rate()),
417 audio_converter_(input_params, output_params, false), 438 audio_converter_(input_params, output_params, false),
418 error_occurred_(false), 439 error_occurred_(false),
419 input_buffer_size_(input_params.frames_per_buffer()), 440 input_buffer_size_(input_params.frames_per_buffer()),
420 output_buffer_size_(output_params.frames_per_buffer()) { 441 output_params_(output_params),
442 debug_recorder_(std::move(debug_recorder)) {
421 RecordRebufferingStats(input_params, output_params); 443 RecordRebufferingStats(input_params, output_params);
422 } 444 }
423 445
424 OnMoreDataConverter::~OnMoreDataConverter() { 446 OnMoreDataConverter::~OnMoreDataConverter() {
425 // Ensure Stop() has been called so we don't end up with an AudioOutputStream 447 // Ensure Stop() has been called so we don't end up with an AudioOutputStream
426 // calling back into OnMoreData() after destruction. 448 // calling back into OnMoreData() after destruction.
427 CHECK(!source_callback_); 449 CHECK(!source_callback_);
428 } 450 }
429 451
430 void OnMoreDataConverter::Start( 452 void OnMoreDataConverter::Start(
(...skipping 11 matching lines...) Expand all
442 CHECK(source_callback_); 464 CHECK(source_callback_);
443 source_callback_ = nullptr; 465 source_callback_ = nullptr;
444 audio_converter_.RemoveInput(this); 466 audio_converter_.RemoveInput(this);
445 } 467 }
446 468
447 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, 469 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay,
448 base::TimeTicks delay_timestamp, 470 base::TimeTicks delay_timestamp,
449 int /* prior_frames_skipped */, 471 int /* prior_frames_skipped */,
450 AudioBus* dest) { 472 AudioBus* dest) {
451 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", 473 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size",
452 input_buffer_size_, "output buffer size", output_buffer_size_); 474 input_buffer_size_, "output buffer size",
475 output_params_.frames_per_buffer());
453 current_delay_ = delay; 476 current_delay_ = delay;
454 current_delay_timestamp_ = delay_timestamp; 477 current_delay_timestamp_ = delay_timestamp;
455 audio_converter_.Convert(dest); 478 audio_converter_.Convert(dest);
456 479
480 if (debug_recorder_)
o1ka 2017/01/31 11:00:11 If you are going to unregistered in Stop, there ma
Henrik Grunell 2017/02/08 11:29:38 See above comment - won't unregister in Stop().
481 debug_recorder_->OnData(dest);
482
457 // Always return the full number of frames requested, ProvideInput() 483 // Always return the full number of frames requested, ProvideInput()
458 // will pad with silence if it wasn't able to acquire enough data. 484 // will pad with silence if it wasn't able to acquire enough data.
459 return dest->frames(); 485 return dest->frames();
460 } 486 }
461 487
462 double OnMoreDataConverter::ProvideInput(AudioBus* dest, 488 double OnMoreDataConverter::ProvideInput(AudioBus* dest,
463 uint32_t frames_delayed) { 489 uint32_t frames_delayed) {
464 base::TimeDelta new_delay = 490 base::TimeDelta new_delay =
465 current_delay_ + AudioTimestampHelper::FramesToTime( 491 current_delay_ + AudioTimestampHelper::FramesToTime(
466 frames_delayed, input_samples_per_second_); 492 frames_delayed, input_samples_per_second_);
467 // Retrieve data from the original callback. 493 // Retrieve data from the original callback.
468 const int frames = source_callback_->OnMoreData( 494 const int frames = source_callback_->OnMoreData(
469 new_delay, current_delay_timestamp_, 0, dest); 495 new_delay, current_delay_timestamp_, 0, dest);
470 496
471 // Zero any unfilled frames if anything was filled, otherwise we'll just 497 // Zero any unfilled frames if anything was filled, otherwise we'll just
472 // return a volume of zero and let AudioConverter drop the output. 498 // return a volume of zero and let AudioConverter drop the output.
473 if (frames > 0 && frames < dest->frames()) 499 if (frames > 0 && frames < dest->frames())
474 dest->ZeroFramesPartial(frames, dest->frames() - frames); 500 dest->ZeroFramesPartial(frames, dest->frames() - frames);
475 return frames > 0 ? 1 : 0; 501 return frames > 0 ? 1 : 0;
476 } 502 }
477 503
478 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { 504 void OnMoreDataConverter::OnError(AudioOutputStream* stream) {
479 error_occurred_ = true; 505 error_occurred_ = true;
480 source_callback_->OnError(stream); 506 source_callback_->OnError(stream);
481 } 507 }
482 508
483 } // namespace media 509 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698