audio_output_resampler.h revision f2477e01787aa58f445919b809d89e252beef54f
131af32cfcd61671cbb5e567870103766b3231521Petr Machata// Copyright (c) 2012 The Chromium Authors. All rights reserved.
231af32cfcd61671cbb5e567870103766b3231521Petr Machata// Use of this source code is governed by a BSD-style license that can be
331af32cfcd61671cbb5e567870103766b3231521Petr Machata// found in the LICENSE file.
431af32cfcd61671cbb5e567870103766b3231521Petr Machata
531af32cfcd61671cbb5e567870103766b3231521Petr Machata#ifndef MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_
631af32cfcd61671cbb5e567870103766b3231521Petr Machata#define MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_
731af32cfcd61671cbb5e567870103766b3231521Petr Machata
831af32cfcd61671cbb5e567870103766b3231521Petr Machata#include <map>
931af32cfcd61671cbb5e567870103766b3231521Petr Machata
1031af32cfcd61671cbb5e567870103766b3231521Petr Machata#include "base/basictypes.h"
1131af32cfcd61671cbb5e567870103766b3231521Petr Machata#include "base/memory/ref_counted.h"
1231af32cfcd61671cbb5e567870103766b3231521Petr Machata#include "base/time/time.h"
1331af32cfcd61671cbb5e567870103766b3231521Petr Machata#include "media/audio/audio_io.h"
1431af32cfcd61671cbb5e567870103766b3231521Petr Machata#include "media/audio/audio_manager.h"
1531af32cfcd61671cbb5e567870103766b3231521Petr Machata#include "media/audio/audio_output_dispatcher.h"
1631af32cfcd61671cbb5e567870103766b3231521Petr Machata#include "media/audio/audio_parameters.h"
1731af32cfcd61671cbb5e567870103766b3231521Petr Machata
1831af32cfcd61671cbb5e567870103766b3231521Petr Machatanamespace media {
1931af32cfcd61671cbb5e567870103766b3231521Petr Machata
2031af32cfcd61671cbb5e567870103766b3231521Petr Machataclass OnMoreDataConverter;
2131af32cfcd61671cbb5e567870103766b3231521Petr Machata
2231af32cfcd61671cbb5e567870103766b3231521Petr Machata// AudioOutputResampler is a browser-side resampling and buffering solution
2331af32cfcd61671cbb5e567870103766b3231521Petr Machata// which ensures audio data is always output at given parameters.  See the
2431af32cfcd61671cbb5e567870103766b3231521Petr Machata// AudioConverter class for details on the conversion process.
2531af32cfcd61671cbb5e567870103766b3231521Petr Machata//
2631af32cfcd61671cbb5e567870103766b3231521Petr Machata// AOR works by intercepting the AudioSourceCallback provided to StartStream()
2731af32cfcd61671cbb5e567870103766b3231521Petr Machata// and redirecting it through an AudioConverter instance.  AudioBuffersState is
2831af32cfcd61671cbb5e567870103766b3231521Petr Machata// adjusted for buffer delay caused by the conversion process.
2931af32cfcd61671cbb5e567870103766b3231521Petr Machata//
3031af32cfcd61671cbb5e567870103766b3231521Petr Machata// AOR will automatically fall back from AUDIO_PCM_LOW_LATENCY to
3131af32cfcd61671cbb5e567870103766b3231521Petr Machata// AUDIO_PCM_LINEAR if the output device fails to open at the requested output
3231af32cfcd61671cbb5e567870103766b3231521Petr Machata// parameters.
3331af32cfcd61671cbb5e567870103766b3231521Petr Machata//
3431af32cfcd61671cbb5e567870103766b3231521Petr Machata// TODO(dalecurtis): Ideally the low latency path will be as reliable as the
3531af32cfcd61671cbb5e567870103766b3231521Petr Machata// high latency path once we have channel mixing and support querying for the
3631af32cfcd61671cbb5e567870103766b3231521Petr Machata// hardware's configured bit depth.  Monitor the UMA stats for fallback and
3731af32cfcd61671cbb5e567870103766b3231521Petr Machata// remove fallback support once it's stable.  http://crbug.com/148418
3831af32cfcd61671cbb5e567870103766b3231521Petr Machataclass MEDIA_EXPORT AudioOutputResampler : public AudioOutputDispatcher {
3931af32cfcd61671cbb5e567870103766b3231521Petr Machata public:
4031af32cfcd61671cbb5e567870103766b3231521Petr Machata  AudioOutputResampler(AudioManager* audio_manager,
4131af32cfcd61671cbb5e567870103766b3231521Petr Machata                       const AudioParameters& input_params,
4231af32cfcd61671cbb5e567870103766b3231521Petr Machata                       const AudioParameters& output_params,
4331af32cfcd61671cbb5e567870103766b3231521Petr Machata                       const std::string& output_device_id,
4431af32cfcd61671cbb5e567870103766b3231521Petr Machata                       const std::string& input_device_id,
4531af32cfcd61671cbb5e567870103766b3231521Petr Machata                       const base::TimeDelta& close_delay);
4631af32cfcd61671cbb5e567870103766b3231521Petr Machata
4731af32cfcd61671cbb5e567870103766b3231521Petr Machata  // AudioOutputDispatcher interface.
4831af32cfcd61671cbb5e567870103766b3231521Petr Machata  virtual bool OpenStream() OVERRIDE;
4931af32cfcd61671cbb5e567870103766b3231521Petr Machata  virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
5031af32cfcd61671cbb5e567870103766b3231521Petr Machata                           AudioOutputProxy* stream_proxy) OVERRIDE;
5131af32cfcd61671cbb5e567870103766b3231521Petr Machata  virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE;
52  virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy,
53                               double volume) OVERRIDE;
54  virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE;
55  virtual void Shutdown() OVERRIDE;
56
57 private:
58  friend class base::RefCountedThreadSafe<AudioOutputResampler>;
59  virtual ~AudioOutputResampler();
60
61  // Converts low latency based output parameters into high latency
62  // appropriate output parameters in error situations.
63  void SetupFallbackParams();
64
65  // Used to initialize and reinitialize |dispatcher_|.
66  void Initialize();
67
68  // Dispatcher to proxy all AudioOutputDispatcher calls too.
69  scoped_refptr<AudioOutputDispatcher> dispatcher_;
70
71  // Map of outstanding OnMoreDataConverter objects.  A new object is created
72  // on every StartStream() call and destroyed on CloseStream().
73  typedef std::map<AudioOutputProxy*, OnMoreDataConverter*> CallbackMap;
74  CallbackMap callbacks_;
75
76  // Used by AudioOutputDispatcherImpl; kept so we can reinitialize on the fly.
77  base::TimeDelta close_delay_;
78
79  // AudioParameters used to setup the output stream.
80  AudioParameters output_params_;
81
82  // Whether any streams have been opened through |dispatcher_|, if so we can't
83  // fallback on future OpenStream() failures.
84  bool streams_opened_;
85
86  DISALLOW_COPY_AND_ASSIGN(AudioOutputResampler);
87};
88
89}  // namespace media
90
91#endif  // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_
92