Home | History | Annotate | Download | only in media
      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 // AudioInputRendererHost serves audio related requests from audio capturer
      6 // which lives inside the render process and provide access to audio hardware.
      7 //
      8 // Create stream sequence (AudioInputController = AIC):
      9 //
     10 // AudioInputHostMsg_CreateStream -> OnCreateStream -> AIC::CreateLowLatency ->
     11 //   <- AudioInputMsg_NotifyStreamCreated <- DoCompleteCreation <- OnCreated <-
     12 //
     13 // Close stream sequence:
     14 //
     15 // AudioInputHostMsg_CloseStream -> OnCloseStream -> AIC::Close ->
     16 //
     17 // This class is owned by BrowserRenderProcessHost and instantiated on UI
     18 // thread. All other operations and method calls happen on IO thread, so we
     19 // need to be extra careful about the lifetime of this object.
     20 //
     21 // To ensure low latency audio, a SyncSocket pair is used to signal buffer
     22 // readiness without having to route messages using the IO thread.
     23 
     24 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_
     25 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_
     26 
     27 #include <map>
     28 #include <string>
     29 
     30 #include "base/compiler_specific.h"
     31 #include "base/gtest_prod_util.h"
     32 #include "base/memory/ref_counted.h"
     33 #include "base/memory/scoped_ptr.h"
     34 #include "base/memory/shared_memory.h"
     35 #include "base/process/process.h"
     36 #include "base/sequenced_task_runner_helpers.h"
     37 #include "content/common/media/audio_messages.h"
     38 #include "content/public/browser/browser_message_filter.h"
     39 #include "content/public/browser/browser_thread.h"
     40 #include "media/audio/audio_input_controller.h"
     41 #include "media/audio/audio_io.h"
     42 #include "media/audio/simple_sources.h"
     43 
     44 namespace media {
     45 class AudioManager;
     46 class AudioParameters;
     47 }
     48 
     49 namespace content {
     50 class AudioMirroringManager;
     51 class MediaStreamManager;
     52 
     53 class CONTENT_EXPORT AudioInputRendererHost
     54     : public BrowserMessageFilter,
     55       public media::AudioInputController::EventHandler {
     56  public:
     57   // Called from UI thread from the owner of this object.
     58   AudioInputRendererHost(
     59       media::AudioManager* audio_manager,
     60       MediaStreamManager* media_stream_manager,
     61       AudioMirroringManager* audio_mirroring_manager);
     62 
     63   // BrowserMessageFilter implementation.
     64   virtual void OnChannelClosing() OVERRIDE;
     65   virtual void OnDestruct() const OVERRIDE;
     66   virtual bool OnMessageReceived(const IPC::Message& message,
     67                                  bool* message_was_ok) OVERRIDE;
     68 
     69   // AudioInputController::EventHandler implementation.
     70   virtual void OnCreated(media::AudioInputController* controller) OVERRIDE;
     71   virtual void OnRecording(media::AudioInputController* controller) OVERRIDE;
     72   virtual void OnError(media::AudioInputController* controller) OVERRIDE;
     73   virtual void OnData(media::AudioInputController* controller,
     74                       const uint8* data,
     75                       uint32 size) OVERRIDE;
     76 
     77  private:
     78   // TODO(henrika): extend test suite (compare AudioRenderHost)
     79   friend class BrowserThread;
     80   friend class base::DeleteHelper<AudioInputRendererHost>;
     81 
     82   struct AudioEntry;
     83   typedef std::map<int, AudioEntry*> AudioEntryMap;
     84 
     85   virtual ~AudioInputRendererHost();
     86 
     87   // Methods called on IO thread ----------------------------------------------
     88 
     89   // Audio related IPC message handlers.
     90 
     91   // Creates an audio input stream with the specified format whose data is
     92   // consumed by an entity in the render view referenced by |render_view_id|.
     93   // |session_id| is used to find out which device to be used for the stream.
     94   // Upon success/failure, the peer is notified via the
     95   // NotifyStreamCreated message.
     96   void OnCreateStream(int stream_id,
     97                       int render_view_id,
     98                       int session_id,
     99                       const AudioInputHostMsg_CreateStream_Config& config);
    100 
    101   // Record the audio input stream referenced by |stream_id|.
    102   void OnRecordStream(int stream_id);
    103 
    104   // Close the audio stream referenced by |stream_id|.
    105   void OnCloseStream(int stream_id);
    106 
    107   // Set the volume of the audio stream referenced by |stream_id|.
    108   void OnSetVolume(int stream_id, double volume);
    109 
    110   // Complete the process of creating an audio input stream. This will set up
    111   // the shared memory or shared socket in low latency mode and send the
    112   // NotifyStreamCreated message to the peer.
    113   void DoCompleteCreation(media::AudioInputController* controller);
    114 
    115   // Send a state change message to the renderer.
    116   void DoSendRecordingMessage(media::AudioInputController* controller);
    117 
    118   // Handle error coming from audio stream.
    119   void DoHandleError(media::AudioInputController* controller);
    120 
    121   // Send an error message to the renderer.
    122   void SendErrorMessage(int stream_id);
    123 
    124   // Delete all audio entry and all audio streams
    125   void DeleteEntries();
    126 
    127   // Closes the stream. The stream is then deleted in DeleteEntry() after it
    128   // is closed.
    129   void CloseAndDeleteStream(AudioEntry* entry);
    130 
    131   // Delete an audio entry and close the related audio stream.
    132   void DeleteEntry(AudioEntry* entry);
    133 
    134   // Delete audio entry and close the related audio input stream.
    135   void DeleteEntryOnError(AudioEntry* entry);
    136 
    137   // A helper method to look up a AudioEntry identified by |stream_id|.
    138   // Returns NULL if not found.
    139   AudioEntry* LookupById(int stream_id);
    140 
    141   // Search for a AudioEntry having the reference to |controller|.
    142   // This method is used to look up an AudioEntry after a controller
    143   // event is received.
    144   AudioEntry* LookupByController(media::AudioInputController* controller);
    145 
    146   // Used to create an AudioInputController.
    147   media::AudioManager* audio_manager_;
    148 
    149   // Used to access to AudioInputDeviceManager.
    150   MediaStreamManager* media_stream_manager_;
    151 
    152   AudioMirroringManager* audio_mirroring_manager_;
    153 
    154   // A map of stream IDs to audio sources.
    155   AudioEntryMap audio_entries_;
    156 
    157   DISALLOW_COPY_AND_ASSIGN(AudioInputRendererHost);
    158 };
    159 
    160 }  // namespace content
    161 
    162 #endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_RENDERER_HOST_H_
    163