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 // AudioOutputDispatcher is a single-threaded base class that dispatches
      6 // creation and deletion of audio output streams. AudioOutputProxy objects use
      7 // this class to allocate and recycle actual audio output streams. When playback
      8 // is started, the proxy calls StartStream() to get an output stream that it
      9 // uses to play audio. When playback is stopped, the proxy returns the stream
     10 // back to the dispatcher by calling StopStream().
     11 //
     12 // AudioManagerBase creates one specialization of AudioOutputDispatcher on the
     13 // audio thread for each possible set of audio parameters. I.e streams with
     14 // different parameters are managed independently.  The AudioOutputDispatcher
     15 // instance is then deleted on the audio thread when the AudioManager shuts
     16 // down.
     17 
     18 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_
     19 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_
     20 
     21 #include "base/basictypes.h"
     22 #include "base/memory/ref_counted.h"
     23 #include "base/timer/timer.h"
     24 #include "media/audio/audio_io.h"
     25 #include "media/audio/audio_manager.h"
     26 #include "media/audio/audio_parameters.h"
     27 
     28 namespace base {
     29 class MessageLoopProxy;
     30 }
     31 
     32 namespace media {
     33 
     34 class AudioOutputProxy;
     35 
     36 class MEDIA_EXPORT AudioOutputDispatcher
     37     : public base::RefCountedThreadSafe<AudioOutputDispatcher> {
     38  public:
     39   AudioOutputDispatcher(AudioManager* audio_manager,
     40                         const AudioParameters& params,
     41                         const std::string& output_device_id,
     42                         const std::string& input_device_id);
     43 
     44   // Called by AudioOutputProxy to open the stream.
     45   // Returns false, if it fails to open it.
     46   virtual bool OpenStream() = 0;
     47 
     48   // Called by AudioOutputProxy when the stream is started.
     49   // Uses |callback| to get source data and report errors, if any.
     50   // Does *not* take ownership of this callback.
     51   // Returns true if started successfully, false otherwise.
     52   virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
     53                            AudioOutputProxy* stream_proxy) = 0;
     54 
     55   // Called by AudioOutputProxy when the stream is stopped.
     56   // Ownership of the |stream_proxy| is passed to the dispatcher.
     57   virtual void StopStream(AudioOutputProxy* stream_proxy) = 0;
     58 
     59   // Called by AudioOutputProxy when the volume is set.
     60   virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy,
     61                                double volume) = 0;
     62 
     63   // Called by AudioOutputProxy when the stream is closed.
     64   virtual void CloseStream(AudioOutputProxy* stream_proxy) = 0;
     65 
     66   // Called on the audio thread when the AudioManager is shutting down.
     67   virtual void Shutdown() = 0;
     68 
     69   // Called by the AudioManager to restart streams when a wedge is detected.  A
     70   // wedge means the OS failed to request any audio after StartStream().  When a
     71   // wedge is detected all streams across all dispatchers must be closed.  After
     72   // all streams are closed, streams are restarted.  See http://crbug.com/160920
     73   virtual void CloseStreamsForWedgeFix() = 0;
     74   virtual void RestartStreamsForWedgeFix() = 0;
     75 
     76   // Accessor to the input device id used by unified IO.
     77   const std::string& input_device_id() const { return input_device_id_; }
     78 
     79  protected:
     80   friend class base::RefCountedThreadSafe<AudioOutputDispatcher>;
     81   virtual ~AudioOutputDispatcher();
     82 
     83   // A no-reference-held pointer (we don't want circular references) back to the
     84   // AudioManager that owns this object.
     85   AudioManager* audio_manager_;
     86   const scoped_refptr<base::MessageLoopProxy> message_loop_;
     87   const AudioParameters params_;
     88   std::string output_device_id_;
     89   const std::string input_device_id_;
     90 
     91  private:
     92   DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcher);
     93 };
     94 
     95 }  // namespace media
     96 
     97 #endif  // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_
     98