Home | History | Annotate | Download | only in audio
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_
      6 #define MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_
      7 
      8 #include <map>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/time/time.h"
     13 #include "media/audio/audio_io.h"
     14 #include "media/audio/audio_manager.h"
     15 #include "media/audio/audio_output_dispatcher.h"
     16 #include "media/audio/audio_parameters.h"
     17 
     18 namespace media {
     19 
     20 class OnMoreDataConverter;
     21 
     22 // AudioOutputResampler is a browser-side resampling and buffering solution
     23 // which ensures audio data is always output at given parameters.  See the
     24 // AudioConverter class for details on the conversion process.
     25 //
     26 // AOR works by intercepting the AudioSourceCallback provided to StartStream()
     27 // and redirecting it through an AudioConverter instance.  AudioBuffersState is
     28 // adjusted for buffer delay caused by the conversion process.
     29 //
     30 // AOR will automatically fall back from AUDIO_PCM_LOW_LATENCY to
     31 // AUDIO_PCM_LINEAR if the output device fails to open at the requested output
     32 // parameters.
     33 //
     34 // TODO(dalecurtis): Ideally the low latency path will be as reliable as the
     35 // high latency path once we have channel mixing and support querying for the
     36 // hardware's configured bit depth.  Monitor the UMA stats for fallback and
     37 // remove fallback support once it's stable.  http://crbug.com/148418
     38 class MEDIA_EXPORT AudioOutputResampler : public AudioOutputDispatcher {
     39  public:
     40   AudioOutputResampler(AudioManager* audio_manager,
     41                        const AudioParameters& input_params,
     42                        const AudioParameters& output_params,
     43                        const std::string& input_device_id,
     44                        const base::TimeDelta& close_delay);
     45 
     46   // AudioOutputDispatcher interface.
     47   virtual bool OpenStream() OVERRIDE;
     48   virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
     49                            AudioOutputProxy* stream_proxy) OVERRIDE;
     50   virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE;
     51   virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy,
     52                                double volume) OVERRIDE;
     53   virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE;
     54   virtual void Shutdown() OVERRIDE;
     55 
     56  private:
     57   friend class base::RefCountedThreadSafe<AudioOutputResampler>;
     58   virtual ~AudioOutputResampler();
     59 
     60   // Used to initialize and reinitialize |dispatcher_|.
     61   void Initialize();
     62 
     63   // Dispatcher to proxy all AudioOutputDispatcher calls too.
     64   scoped_refptr<AudioOutputDispatcher> dispatcher_;
     65 
     66   // Map of outstanding OnMoreDataConverter objects.  A new object is created
     67   // on every StartStream() call and destroyed on CloseStream().
     68   typedef std::map<AudioOutputProxy*, OnMoreDataConverter*> CallbackMap;
     69   CallbackMap callbacks_;
     70 
     71   // Used by AudioOutputDispatcherImpl; kept so we can reinitialize on the fly.
     72   base::TimeDelta close_delay_;
     73 
     74   // AudioParameters used to setup the output stream.
     75   AudioParameters output_params_;
     76 
     77   // Device ID to be used by the unified IO to open the correct input device.
     78   const std::string input_device_id_;
     79 
     80   // Whether any streams have been opened through |dispatcher_|, if so we can't
     81   // fallback on future OpenStream() failures.
     82   bool streams_opened_;
     83 
     84   DISALLOW_COPY_AND_ASSIGN(AudioOutputResampler);
     85 };
     86 
     87 }  // namespace media
     88 
     89 #endif  // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_
     90