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 // AudioRendererHost serves audio related requests from AudioRenderer which
      6 // lives inside the render process and provide access to audio hardware.
      7 //
      8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI
      9 // thread, but all other operations and method calls happen on IO thread, so we
     10 // need to be extra careful about the lifetime of this object. AudioManager is a
     11 // singleton and created in IO thread, audio output streams are also created in
     12 // the IO thread, so we need to destroy them also in IO thread. After this class
     13 // is created, a task of OnInitialized() is posted on IO thread in which
     14 // singleton of AudioManager is created.
     15 //
     16 // Here's an example of a typical IPC dialog for audio:
     17 //
     18 //   Renderer                     AudioRendererHost
     19 //      |                               |
     20 //      |         CreateStream >        |
     21 //      |     < NotifyStreamCreated     |
     22 //      |                               |
     23 //      |          PlayStream >         |
     24 //      |  < NotifyStreamStateChanged   | kAudioStreamPlaying
     25 //      |                               |
     26 //      |         PauseStream >         |
     27 //      |  < NotifyStreamStateChanged   | kAudioStreamPaused
     28 //      |                               |
     29 //      |          PlayStream >         |
     30 //      |  < NotifyStreamStateChanged   | kAudioStreamPlaying
     31 //      |             ...               |
     32 //      |         CloseStream >         |
     33 //      v                               v
     34 
     35 // A SyncSocket pair is used to signal buffer readiness between processes.
     36 
     37 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
     38 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
     39 
     40 #include <map>
     41 
     42 #include "base/gtest_prod_util.h"
     43 #include "base/memory/ref_counted.h"
     44 #include "base/memory/scoped_ptr.h"
     45 #include "base/process/process.h"
     46 #include "base/sequenced_task_runner_helpers.h"
     47 #include "content/common/content_export.h"
     48 #include "content/public/browser/browser_message_filter.h"
     49 #include "content/public/browser/browser_thread.h"
     50 #include "media/audio/audio_io.h"
     51 #include "media/audio/audio_output_controller.h"
     52 #include "media/audio/simple_sources.h"
     53 
     54 namespace media {
     55 class AudioManager;
     56 class AudioParameters;
     57 }
     58 
     59 namespace content {
     60 
     61 class AudioMirroringManager;
     62 class MediaInternals;
     63 class MediaStreamManager;
     64 class ResourceContext;
     65 
     66 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter {
     67  public:
     68   // Called from UI thread from the owner of this object.
     69   AudioRendererHost(int render_process_id,
     70                     media::AudioManager* audio_manager,
     71                     AudioMirroringManager* mirroring_manager,
     72                     MediaInternals* media_internals,
     73                     MediaStreamManager* media_stream_manager);
     74 
     75   // BrowserMessageFilter implementation.
     76   virtual void OnChannelClosing() OVERRIDE;
     77   virtual void OnDestruct() const OVERRIDE;
     78   virtual bool OnMessageReceived(const IPC::Message& message,
     79                                  bool* message_was_ok) OVERRIDE;
     80 
     81  private:
     82   friend class AudioRendererHostTest;
     83   friend class BrowserThread;
     84   friend class base::DeleteHelper<AudioRendererHost>;
     85   friend class MockAudioRendererHost;
     86   FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream);
     87   FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation);
     88 
     89   class AudioEntry;
     90   typedef std::map<int, AudioEntry*> AudioEntryMap;
     91 
     92   virtual ~AudioRendererHost();
     93 
     94   // Methods called on IO thread ----------------------------------------------
     95 
     96   // Audio related IPC message handlers.
     97 
     98   // Creates an audio output stream with the specified format whose data is
     99   // produced by an entity in the render view referenced by |render_view_id|.
    100   // |session_id| is used for unified IO to find out which input device to be
    101   // opened for the stream. For clients that do not use unified IO,
    102   // |session_id| will be ignored.
    103   // Upon success/failure, the peer is notified via the NotifyStreamCreated
    104   // message.
    105   void OnCreateStream(int stream_id,
    106                       int render_view_id,
    107                       int session_id,
    108                       const media::AudioParameters& params);
    109 
    110   // Play the audio stream referenced by |stream_id|.
    111   void OnPlayStream(int stream_id);
    112 
    113   // Pause the audio stream referenced by |stream_id|.
    114   void OnPauseStream(int stream_id);
    115 
    116   // Close the audio stream referenced by |stream_id|.
    117   void OnCloseStream(int stream_id);
    118 
    119   // Set the volume of the audio stream referenced by |stream_id|.
    120   void OnSetVolume(int stream_id, double volume);
    121 
    122   // Complete the process of creating an audio stream. This will set up the
    123   // shared memory or shared socket in low latency mode and send the
    124   // NotifyStreamCreated message to the peer.
    125   void DoCompleteCreation(int stream_id);
    126 
    127   // Propagate measured power level of the audio signal to MediaObserver.
    128   void DoNotifyAudioPowerLevel(int stream_id, float power_dbfs, bool clipped);
    129 
    130   // Send an error message to the renderer.
    131   void SendErrorMessage(int stream_id);
    132 
    133   // Delete an audio entry, notifying observers first.  This is called by
    134   // AudioOutputController after it has closed.
    135   void DeleteEntry(scoped_ptr<AudioEntry> entry);
    136 
    137   // Send an error message to the renderer, then close the stream.
    138   void ReportErrorAndClose(int stream_id);
    139 
    140   // A helper method to look up a AudioEntry identified by |stream_id|.
    141   // Returns NULL if not found.
    142   AudioEntry* LookupById(int stream_id);
    143 
    144   // ID of the RenderProcessHost that owns this instance.
    145   const int render_process_id_;
    146 
    147   media::AudioManager* const audio_manager_;
    148   AudioMirroringManager* const mirroring_manager_;
    149   MediaInternals* const media_internals_;
    150 
    151   // Used to access to AudioInputDeviceManager.
    152   MediaStreamManager* media_stream_manager_;
    153 
    154   // A map of stream IDs to audio sources.
    155   AudioEntryMap audio_entries_;
    156 
    157   DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
    158 };
    159 
    160 }  // namespace content
    161 
    162 #endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
    163