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 // Low-latency audio capturing class utilizing audio input stream provided
      6 // by a server (browser) process by use of an IPC interface.
      7 //
      8 // Relationship of classes:
      9 //
     10 //  AudioInputController                 AudioInputDevice
     11 //           ^                                  ^
     12 //           |                                  |
     13 //           v                  IPC             v
     14 // AudioInputRendererHost  <----------->  AudioInputIPC
     15 //           ^                            (AudioInputMessageFilter)
     16 //           |
     17 //           v
     18 // AudioInputDeviceManager
     19 //
     20 // Transportation of audio samples from the browser to the render process
     21 // is done by using shared memory in combination with a SyncSocket.
     22 // The AudioInputDevice user registers an AudioInputDevice::CaptureCallback by
     23 // calling Initialize().  The callback will be called with recorded audio from
     24 // the underlying audio layers.
     25 // The session ID is used by the AudioInputRendererHost to start the device
     26 // referenced by this ID.
     27 //
     28 // State sequences:
     29 //
     30 // Start -> InitializeOnIOThread -> CreateStream ->
     31 //       <- OnStreamCreated <-
     32 //       -> StartOnIOThread -> PlayStream ->
     33 //
     34 //
     35 // AudioInputDevice::Capture => low latency audio transport on audio thread =>
     36 //                               |
     37 // Stop --> ShutDownOnIOThread ------>  CloseStream -> Close
     38 //
     39 // This class depends on two threads to function:
     40 //
     41 // 1. An IO thread.
     42 //    This thread is used to asynchronously process Start/Stop etc operations
     43 //    that are available via the public interface.  The public methods are
     44 //    asynchronous and simply post a task to the IO thread to actually perform
     45 //    the work.
     46 // 2. Audio transport thread.
     47 //    Responsible for calling the CaptureCallback and feed audio samples from
     48 //    the server side audio layer using a socket and shared memory.
     49 //
     50 // Implementation notes:
     51 // - The user must call Stop() before deleting the class instance.
     52 
     53 #ifndef MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_
     54 #define MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_
     55 
     56 #include <string>
     57 
     58 #include "base/basictypes.h"
     59 #include "base/compiler_specific.h"
     60 #include "base/memory/scoped_ptr.h"
     61 #include "base/memory/shared_memory.h"
     62 #include "media/audio/audio_device_thread.h"
     63 #include "media/audio/audio_input_ipc.h"
     64 #include "media/audio/audio_parameters.h"
     65 #include "media/audio/scoped_loop_observer.h"
     66 #include "media/base/audio_capturer_source.h"
     67 #include "media/base/media_export.h"
     68 
     69 namespace media {
     70 
     71 // TODO(henrika): This class is based on the AudioOutputDevice class and it has
     72 // many components in common. Investigate potential for re-factoring.
     73 // See http://crbug.com/179597.
     74 // TODO(henrika): Add support for event handling (e.g. OnStateChanged,
     75 // OnCaptureStopped etc.) and ensure that we can deliver these notifications
     76 // to any clients using this class.
     77 class MEDIA_EXPORT AudioInputDevice
     78     : NON_EXPORTED_BASE(public AudioCapturerSource),
     79       NON_EXPORTED_BASE(public AudioInputIPCDelegate),
     80       NON_EXPORTED_BASE(public ScopedLoopObserver) {
     81  public:
     82   // NOTE: Clients must call Initialize() before using.
     83   AudioInputDevice(scoped_ptr<AudioInputIPC> ipc,
     84                    const scoped_refptr<base::MessageLoopProxy>& io_loop);
     85 
     86   // AudioCapturerSource implementation.
     87   virtual void Initialize(const AudioParameters& params,
     88                           CaptureCallback* callback,
     89                           int session_id) OVERRIDE;
     90   virtual void Start() OVERRIDE;
     91   virtual void Stop() OVERRIDE;
     92   virtual void SetVolume(double volume) OVERRIDE;
     93   virtual void SetAutomaticGainControl(bool enabled) OVERRIDE;
     94 
     95  protected:
     96   friend class base::RefCountedThreadSafe<AudioInputDevice>;
     97   virtual ~AudioInputDevice();
     98 
     99   // Methods called on IO thread ----------------------------------------------
    100   // AudioInputIPCDelegate implementation.
    101   virtual void OnStreamCreated(base::SharedMemoryHandle handle,
    102                                base::SyncSocket::Handle socket_handle,
    103                                int length,
    104                                int total_segments) OVERRIDE;
    105   virtual void OnVolume(double volume) OVERRIDE;
    106   virtual void OnStateChanged(
    107       AudioInputIPCDelegate::State state) OVERRIDE;
    108   virtual void OnIPCClosed() OVERRIDE;
    109 
    110  private:
    111   // Note: The ordering of members in this enum is critical to correct behavior!
    112   enum State {
    113     IPC_CLOSED,  // No more IPCs can take place.
    114     IDLE,  // Not started.
    115     CREATING_STREAM,  // Waiting for OnStreamCreated() to be called back.
    116     RECORDING,  // Receiving audio data.
    117   };
    118 
    119   // Methods called on IO thread ----------------------------------------------
    120   // The following methods are tasks posted on the IO thread that needs to
    121   // be executed on that thread. They interact with AudioInputMessageFilter and
    122   // sends IPC messages on that thread.
    123   void StartUpOnIOThread();
    124   void ShutDownOnIOThread();
    125   void SetVolumeOnIOThread(double volume);
    126   void SetAutomaticGainControlOnIOThread(bool enabled);
    127 
    128   // base::MessageLoop::DestructionObserver implementation for the IO loop.
    129   // If the IO loop dies before we do, we shut down the audio thread from here.
    130   virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
    131 
    132   AudioParameters audio_parameters_;
    133 
    134   CaptureCallback* callback_;
    135 
    136   // A pointer to the IPC layer that takes care of sending requests over to
    137   // the AudioInputRendererHost.  Only valid when state_ != IPC_CLOSED and must
    138   // only be accessed on the IO thread.
    139   scoped_ptr<AudioInputIPC> ipc_;
    140 
    141   // Current state (must only be accessed from the IO thread).  See comments for
    142   // State enum above.
    143   State state_;
    144 
    145   // The media session ID used to identify which input device to be started.
    146   // Only modified in Initialize() and ShutDownOnIOThread().
    147   int session_id_;
    148 
    149   // Stores the Automatic Gain Control state. Default is false.
    150   // Only modified on the IO thread.
    151   bool agc_is_enabled_;
    152 
    153   // Our audio thread callback class.  See source file for details.
    154   class AudioThreadCallback;
    155 
    156   // In order to avoid a race between OnStreamCreated and Stop(), we use this
    157   // guard to control stopping and starting the audio thread.
    158   base::Lock audio_thread_lock_;
    159   AudioDeviceThread audio_thread_;
    160   scoped_ptr<AudioInputDevice::AudioThreadCallback> audio_callback_;
    161 
    162   // Temporary hack to ignore OnStreamCreated() due to the user calling Stop()
    163   // so we don't start the audio thread pointing to a potentially freed
    164   // |callback_|.
    165   //
    166   // TODO(miu): Replace this by changing AudioCapturerSource to accept the
    167   // callback via Start(). See http://crbug.com/151051 for details.
    168   bool stopping_hack_;
    169 
    170   DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice);
    171 };
    172 
    173 }  // namespace media
    174 
    175 #endif  // MEDIA_AUDIO_AUDIO_INPUT_DEVICE_H_
    176