summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_output_resampler.cc
blob: 87463c93f12b7ffa3a22eb384d38b9320acb924d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright (c) 2012 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 "media/audio/audio_output_resampler.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/time.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_output_dispatcher_impl.h"
#include "media/audio/audio_output_proxy.h"
#include "media/audio/audio_util.h"
#include "media/audio/sample_rates.h"
#include "media/base/audio_pull_fifo.h"
#include "media/base/limits.h"
#include "media/base/media_switches.h"
#include "media/base/multi_channel_resampler.h"

namespace media {

// Record UMA statistics for hardware output configuration.
static void RecordStats(const AudioParameters& output_params) {
  UMA_HISTOGRAM_ENUMERATION(
      "Media.HardwareAudioBitsPerChannel", output_params.bits_per_sample(),
      limits::kMaxBitsPerSample);
// WASAPIAudioOutputStream will record this information for us.
// TODO(dalecurtis): This should go away when we support channel mixing and will
// receive the actual hardware channel parameters via |output_params|.  See
// http://crbug.com/138762
#if !defined(OS_WIN)
  UMA_HISTOGRAM_ENUMERATION(
      "Media.HardwareAudioChannelLayout", output_params.channel_layout(),
      CHANNEL_LAYOUT_MAX);
  UMA_HISTOGRAM_ENUMERATION(
      "Media.HardwareAudioChannelCount", output_params.channels(),
      limits::kMaxChannels);
#endif

  AudioSampleRate asr = media::AsAudioSampleRate(output_params.sample_rate());
  if (asr != kUnexpectedAudioSampleRate) {
    UMA_HISTOGRAM_ENUMERATION(
        "Media.HardwareAudioSamplesPerSecond", asr, kUnexpectedAudioSampleRate);
  } else {
    UMA_HISTOGRAM_COUNTS(
        "Media.HardwareAudioSamplesPerSecondUnexpected",
        output_params.sample_rate());
  }
}

// Record UMA statistics for hardware output configuration after fallback.
static void RecordFallbackStats(const AudioParameters& output_params) {
  UMA_HISTOGRAM_BOOLEAN("Media.FallbackToHighLatencyAudioPath", true);
  UMA_HISTOGRAM_ENUMERATION(
      "Media.FallbackHardwareAudioBitsPerChannel",
      output_params.bits_per_sample(), limits::kMaxBitsPerSample);
// WASAPIAudioOutputStream will record this information for us.
// TODO(dalecurtis): This should go away when we support channel mixing and will
// receive the actual hardware channel parameters via |output_params|.  See
// http://crbug.com/138762
#if !defined(OS_WIN)
  UMA_HISTOGRAM_ENUMERATION(
      "Media.FallbackHardwareAudioChannelLayout",
      output_params.channel_layout(), CHANNEL_LAYOUT_MAX);
  UMA_HISTOGRAM_ENUMERATION(
      "Media.FallbackHardwareAudioChannelCount",
      output_params.channels(), limits::kMaxChannels);
#endif

  AudioSampleRate asr = media::AsAudioSampleRate(output_params.sample_rate());
  if (asr != kUnexpectedAudioSampleRate) {
    UMA_HISTOGRAM_ENUMERATION(
        "Media.FallbackHardwareAudioSamplesPerSecond",
        asr, kUnexpectedAudioSampleRate);
  } else {
    UMA_HISTOGRAM_COUNTS(
        "Media.FallbackHardwareAudioSamplesPerSecondUnexpected",
        output_params.sample_rate());
  }
}

// Converts low latency based |output_params| into high latency appropriate
// output parameters in error situations.
static AudioParameters SetupFallbackParams(
    const AudioParameters& input_params, const AudioParameters& output_params) {
  // Choose AudioParameters appropriate for opening the device in high latency
  // mode.  |kMinLowLatencyFrameSize| is arbitrarily based on Pepper Flash's
  // MAXIMUM frame size for low latency.
  static const int kMinLowLatencyFrameSize = 2048;
  int frames_per_buffer = std::min(
      std::max(input_params.frames_per_buffer(), kMinLowLatencyFrameSize),
      static_cast<int>(
          GetHighLatencyOutputBufferSize(input_params.sample_rate())));

  return AudioParameters(
      AudioParameters::AUDIO_PCM_LINEAR, input_params.channel_layout(),
      input_params.sample_rate(), input_params.bits_per_sample(),
      frames_per_buffer);
}

AudioOutputResampler::AudioOutputResampler(AudioManager* audio_manager,
                                           const AudioParameters& input_params,
                                           const AudioParameters& output_params,
                                           const base::TimeDelta& close_delay)
    : AudioOutputDispatcher(audio_manager, input_params),
      source_callback_(NULL),
      io_ratio_(1),
      close_delay_(close_delay),
      outstanding_audio_bytes_(0),
      output_params_(output_params) {
  DCHECK_EQ(output_params_.format(), AudioParameters::AUDIO_PCM_LOW_LATENCY);

  // Record UMA statistics for the hardware configuration.
  RecordStats(output_params);

  // Immediately fallback if we're given invalid output parameters.  This may
  // happen if the OS provided us junk values for the hardware configuration.
  if (!output_params_.IsValid()) {
    LOG(ERROR) << "Invalid audio output parameters recieved; using fallback "
               << "path. Channels: " << output_params_.channels() << ", "
               << "Sample Rate: " << output_params_.sample_rate() << ", "
               << "Bits Per Sample: " << output_params_.bits_per_sample()
               << ", Frames Per Buffer: " << output_params_.frames_per_buffer();
    // Record UMA statistics about the hardware which triggered the failure so
    // we can debug and triage later.
    RecordFallbackStats(output_params);
    output_params_ = SetupFallbackParams(input_params, output_params);
  }

  Initialize();
}

AudioOutputResampler::~AudioOutputResampler() {}

void AudioOutputResampler::Initialize() {
  io_ratio_ = 1;

  // TODO(dalecurtis): Add channel remixing.  http://crbug.com/138762
  DCHECK_EQ(params_.channels(), output_params_.channels());
  // Only resample or rebuffer if the input parameters don't match the output
  // parameters to avoid any unnecessary work.
  if (params_.channels() != output_params_.channels() ||
      params_.sample_rate() != output_params_.sample_rate() ||
      params_.bits_per_sample() != output_params_.bits_per_sample() ||
      params_.frames_per_buffer() != output_params_.frames_per_buffer()) {
    // Only resample if necessary since it's expensive.
    if (params_.sample_rate() != output_params_.sample_rate()) {
      DVLOG(1) << "Resampling from " << params_.sample_rate() << " to "
               << output_params_.sample_rate();
      double io_sample_rate_ratio = params_.sample_rate() /
          static_cast<double>(output_params_.sample_rate());
      // Include the I/O resampling ratio in our global I/O ratio.
      io_ratio_ *= io_sample_rate_ratio;
      resampler_.reset(new MultiChannelResampler(
          output_params_.channels(), io_sample_rate_ratio, base::Bind(
              &AudioOutputResampler::ProvideInput, base::Unretained(this))));
    }

    // Include bits per channel differences.
    io_ratio_ *= static_cast<double>(params_.bits_per_sample()) /
        output_params_.bits_per_sample();

    // Include channel count differences.
    io_ratio_ *= static_cast<double>(params_.channels()) /
        output_params_.channels();

    // Since the resampler / output device may want a different buffer size than
    // the caller asked for, we need to use a FIFO to ensure that both sides
    // read in chunk sizes they're configured for.
    if (params_.sample_rate() != output_params_.sample_rate() ||
        params_.frames_per_buffer() != output_params_.frames_per_buffer()) {
      DVLOG(1) << "Rebuffering from " << params_.frames_per_buffer()
               << " to " << output_params_.frames_per_buffer();
      audio_fifo_.reset(new AudioPullFifo(
          params_.channels(), params_.frames_per_buffer(), base::Bind(
              &AudioOutputResampler::SourceCallback_Locked,
              base::Unretained(this))));
    }

    DVLOG(1) << "I/O ratio is " << io_ratio_;
  } else {
    DVLOG(1) << "Input and output params are the same; in pass-through mode.";
  }

  // TODO(dalecurtis): All this code should be merged into AudioOutputMixer once
  // we've stabilized the issues there.
  dispatcher_ = new AudioOutputDispatcherImpl(
      audio_manager_, output_params_, close_delay_);
}

bool AudioOutputResampler::OpenStream() {
  if (dispatcher_->OpenStream()) {
    // Only record the UMA statistic if we didn't fallback during construction.
    if (output_params_.format() == AudioParameters::AUDIO_PCM_LOW_LATENCY)
      UMA_HISTOGRAM_BOOLEAN("Media.FallbackToHighLatencyAudioPath", false);
    return true;
  }

  // If we've already tried to open the stream in high latency mode, there's
  // nothing more to be done.
  if (output_params_.format() == AudioParameters::AUDIO_PCM_LINEAR)
    return false;

  if (CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kDisableAudioFallback)) {
    LOG(ERROR) << "Open failed and automatic fallback to high latency audio "
               << "path is disabled, aborting.";
    return false;
  }

  // TODO(dalecurtis): Is it better to recreate the whole |dispatcher_| ?  See
  // http://crbug.com/149815
  dispatcher_->CloseStream(NULL);

  DLOG(ERROR) << "Unable to open audio device in low latency mode.  Falling "
              << "back to high latency audio output.";

  // Record UMA statistics about the hardware which triggered the failure so
  // we can debug and triage later.
  RecordFallbackStats(output_params_);
  output_params_ = SetupFallbackParams(params_, output_params_);
  Initialize();

  // Retry, if this fails, there's nothing left to do but report the error back.
  return dispatcher_->OpenStream();
}

bool AudioOutputResampler::StartStream(
    AudioOutputStream::AudioSourceCallback* callback,
    AudioOutputProxy* stream_proxy) {
  {
    base::AutoLock auto_lock(source_lock_);
    source_callback_ = callback;
  }
  return dispatcher_->StartStream(this, stream_proxy);
}

void AudioOutputResampler::StreamVolumeSet(AudioOutputProxy* stream_proxy,
                                           double volume) {
  dispatcher_->StreamVolumeSet(stream_proxy, volume);
}

void AudioOutputResampler::Reset() {
  base::AutoLock auto_lock(source_lock_);
  source_callback_ = NULL;
  outstanding_audio_bytes_ = 0;
  if (audio_fifo_.get())
    audio_fifo_->Clear();
  if (resampler_.get())
    resampler_->Flush();
}

void AudioOutputResampler::StopStream(AudioOutputProxy* stream_proxy) {
  Reset();
  dispatcher_->StopStream(stream_proxy);
}

void AudioOutputResampler::CloseStream(AudioOutputProxy* stream_proxy) {
  Reset();
  dispatcher_->CloseStream(stream_proxy);
}

void AudioOutputResampler::Shutdown() {
  Reset();
  dispatcher_->Shutdown();
}

int AudioOutputResampler::OnMoreData(AudioBus* dest,
                                     AudioBuffersState buffers_state) {
  return OnMoreIOData(NULL, dest, buffers_state);
}

int AudioOutputResampler::OnMoreIOData(AudioBus* source,
                                       AudioBus* dest,
                                       AudioBuffersState buffers_state) {
  base::AutoLock auto_lock(source_lock_);
  // While we waited for |source_lock_| the callback might have been cleared.
  if (!source_callback_) {
    dest->Zero();
    return dest->frames();
  }

  current_buffers_state_ = buffers_state;

  if (!resampler_.get() && !audio_fifo_.get()) {
    // We have no internal buffers, so clear any outstanding audio data.
    outstanding_audio_bytes_ = 0;
    SourceIOCallback_Locked(source, dest);
    return dest->frames();
  }

  if (resampler_.get())
    resampler_->Resample(dest, dest->frames());
  else
    ProvideInput(dest);

  // Calculate how much data is left in the internal FIFO and resampler buffers.
  outstanding_audio_bytes_ -=
      dest->frames() * output_params_.GetBytesPerFrame();

  // Due to rounding errors while multiplying against |io_ratio_|,
  // |outstanding_audio_bytes_| might (rarely) slip below zero.
  if (outstanding_audio_bytes_ < 0) {
    DLOG(ERROR) << "Outstanding audio bytes went negative! Value: "
                << outstanding_audio_bytes_;
    outstanding_audio_bytes_ = 0;
  }

  // Always return the full number of frames requested, ProvideInput() will pad
  // with silence if it wasn't able to acquire enough data.
  return dest->frames();
}

void AudioOutputResampler::SourceCallback_Locked(AudioBus* dest) {
  SourceIOCallback_Locked(NULL, dest);
}

void AudioOutputResampler::SourceIOCallback_Locked(
    AudioBus* source, AudioBus* dest) {
  source_lock_.AssertAcquired();

  // Adjust playback delay to include the state of the internal buffers used by
  // the resampler and/or the FIFO.  Since the sample rate and bits per channel
  // may be different, we need to scale this value appropriately.
  AudioBuffersState new_buffers_state;
  new_buffers_state.pending_bytes = io_ratio_ *
      (current_buffers_state_.total_bytes() + outstanding_audio_bytes_);

  // Retrieve data from the original callback.  Zero any unfilled frames.
  int frames = source_callback_->OnMoreIOData(source, dest, new_buffers_state);
  if (frames < dest->frames())
    dest->ZeroFramesPartial(frames, dest->frames() - frames);

  // Scale the number of frames we got back in terms of input bytes to output
  // bytes accordingly.
  outstanding_audio_bytes_ +=
      (dest->frames() * params_.GetBytesPerFrame()) / io_ratio_;
}

void AudioOutputResampler::ProvideInput(AudioBus* audio_bus) {
  audio_fifo_->Consume(audio_bus, audio_bus->frames());
}

void AudioOutputResampler::OnError(AudioOutputStream* stream, int code) {
  base::AutoLock auto_lock(source_lock_);
  if (source_callback_)
    source_callback_->OnError(stream, code);
}

void AudioOutputResampler::WaitTillDataReady() {
  base::AutoLock auto_lock(source_lock_);
  if (source_callback_ && !outstanding_audio_bytes_)
    source_callback_->WaitTillDataReady();
}

}  // namespace media