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 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_MESSAGE_FILTER_H_
      6 #define CONTENT_RENDERER_MEDIA_AUDIO_MESSAGE_FILTER_H_
      7 
      8 #include "base/gtest_prod_util.h"
      9 #include "base/id_map.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/shared_memory.h"
     12 #include "base/sync_socket.h"
     13 #include "base/synchronization/lock.h"
     14 #include "content/common/content_export.h"
     15 #include "ipc/message_filter.h"
     16 #include "media/audio/audio_output_ipc.h"
     17 #include "media/base/audio_hardware_config.h"
     18 
     19 namespace base {
     20 class MessageLoopProxy;
     21 }
     22 
     23 namespace content {
     24 
     25 // MessageFilter that handles audio messages and delegates them to audio
     26 // renderers. Created on render thread, AudioMessageFilter is operated on
     27 // IO thread (secondary thread of render process) it intercepts audio messages
     28 // and process them on IO thread since these messages are time critical.
     29 class CONTENT_EXPORT AudioMessageFilter : public IPC::MessageFilter {
     30  public:
     31   explicit AudioMessageFilter(
     32       const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
     33 
     34   // Getter for the one AudioMessageFilter object.
     35   static AudioMessageFilter* Get();
     36 
     37   // Create an AudioOutputIPC to be owned by one delegate.  |render_view_id| and
     38   // |render_frame_id| are the render view and render frame containing the
     39   // entity producing the audio.
     40   // TODO(jam): remove render_view_id
     41   //
     42   // The returned object is not thread-safe, and must be used on
     43   // |io_message_loop|.
     44   scoped_ptr<media::AudioOutputIPC> CreateAudioOutputIPC(int render_view_id,
     45                                                          int render_frame_id);
     46 
     47   // When set, AudioMessageFilter will update the AudioHardwareConfig with new
     48   // configuration values as received by OnOutputDeviceChanged().  The provided
     49   // |config| must outlive AudioMessageFilter.
     50   void SetAudioHardwareConfig(media::AudioHardwareConfig* config);
     51 
     52   // IO message loop associated with this message filter.
     53   scoped_refptr<base::MessageLoopProxy> io_message_loop() const {
     54     return io_message_loop_;
     55   }
     56 
     57  protected:
     58   virtual ~AudioMessageFilter();
     59 
     60  private:
     61   FRIEND_TEST_ALL_PREFIXES(AudioMessageFilterTest, Basic);
     62   FRIEND_TEST_ALL_PREFIXES(AudioMessageFilterTest, Delegates);
     63 
     64   // Implementation of media::AudioOutputIPC which augments IPC calls with
     65   // stream_id and the source render_view_id.
     66   class AudioOutputIPCImpl;
     67 
     68   // Sends an IPC message using |sender_|.
     69   void Send(IPC::Message* message);
     70 
     71   // IPC::MessageFilter override. Called on |io_message_loop|.
     72   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     73   virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE;
     74   virtual void OnFilterRemoved() OVERRIDE;
     75   virtual void OnChannelClosing() OVERRIDE;
     76 
     77   // Received when browser process has created an audio output stream.
     78   void OnStreamCreated(int stream_id, base::SharedMemoryHandle handle,
     79 #if defined(OS_WIN)
     80                        base::SyncSocket::Handle socket_handle,
     81 #else
     82                        base::FileDescriptor socket_descriptor,
     83 #endif
     84                        uint32 length);
     85 
     86   // Received when internal state of browser process' audio output device has
     87   // changed.
     88   void OnStreamStateChanged(int stream_id,
     89                             media::AudioOutputIPCDelegate::State state);
     90 
     91   // Received when the browser process detects an output device change.
     92   void OnOutputDeviceChanged(int stream_id, int new_buffer_size,
     93                              int new_sample_rate);
     94 
     95   // IPC sender for Send(); must only be accesed on |io_message_loop_|.
     96   IPC::Sender* sender_;
     97 
     98   // A map of stream ids to delegates; must only be accessed on
     99   // |io_message_loop_|.
    100   IDMap<media::AudioOutputIPCDelegate> delegates_;
    101 
    102   // Audio hardware configuration to update when OnOutputDeviceChanged() fires.
    103   // Access is guarded by |lock_|.
    104   base::Lock lock_;
    105   media::AudioHardwareConfig* audio_hardware_config_;
    106 
    107   // Message loop on which IPC calls are driven.
    108   const scoped_refptr<base::MessageLoopProxy> io_message_loop_;
    109 
    110   // The singleton instance for this filter.
    111   static AudioMessageFilter* g_filter;
    112 
    113   DISALLOW_COPY_AND_ASSIGN(AudioMessageFilter);
    114 };
    115 
    116 }  // namespace content
    117 
    118 #endif  // CONTENT_RENDERER_MEDIA_AUDIO_MESSAGE_FILTER_H_
    119