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/ipc_channel_proxy.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
     30     : public IPC::ChannelProxy::MessageFilter {
     31  public:
     32   explicit AudioMessageFilter(
     33       const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
     34 
     35   // Getter for the one AudioMessageFilter object.
     36   static AudioMessageFilter* Get();
     37 
     38   // Create an AudioOutputIPC to be owned by one delegate.  |render_view_id| is
     39   // the render view containing the entity producing the audio.
     40   //
     41   // The returned object is not thread-safe, and must be used on
     42   // |io_message_loop|.
     43   scoped_ptr<media::AudioOutputIPC> CreateAudioOutputIPC(int render_view_id);
     44 
     45   // When set, AudioMessageFilter will update the AudioHardwareConfig with new
     46   // configuration values as recieved by OnOutputDeviceChanged().  The provided
     47   // |config| must outlive AudioMessageFilter.
     48   void SetAudioHardwareConfig(media::AudioHardwareConfig* config);
     49 
     50   // IO message loop associated with this message filter.
     51   scoped_refptr<base::MessageLoopProxy> io_message_loop() const {
     52     return io_message_loop_;
     53   }
     54 
     55  protected:
     56   virtual ~AudioMessageFilter();
     57 
     58  private:
     59   FRIEND_TEST_ALL_PREFIXES(AudioMessageFilterTest, Basic);
     60   FRIEND_TEST_ALL_PREFIXES(AudioMessageFilterTest, Delegates);
     61 
     62   // Implementation of media::AudioOutputIPC which augments IPC calls with
     63   // stream_id and the source render_view_id.
     64   class AudioOutputIPCImpl;
     65 
     66   // Sends an IPC message using |channel_|.
     67   void Send(IPC::Message* message);
     68 
     69   // IPC::ChannelProxy::MessageFilter override. Called on |io_message_loop|.
     70   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     71   virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE;
     72   virtual void OnFilterRemoved() OVERRIDE;
     73   virtual void OnChannelClosing() OVERRIDE;
     74 
     75   // Received when browser process has created an audio output stream.
     76   void OnStreamCreated(int stream_id, base::SharedMemoryHandle handle,
     77 #if defined(OS_WIN)
     78                        base::SyncSocket::Handle socket_handle,
     79 #else
     80                        base::FileDescriptor socket_descriptor,
     81 #endif
     82                        uint32 length);
     83 
     84   // Received when internal state of browser process' audio output device has
     85   // changed.
     86   void OnStreamStateChanged(int stream_id,
     87                             media::AudioOutputIPCDelegate::State state);
     88 
     89   // Received when the browser process detects an output device change.
     90   void OnOutputDeviceChanged(int stream_id, int new_buffer_size,
     91                              int new_sample_rate);
     92 
     93   // IPC channel for Send(); must only be accesed on |io_message_loop_|.
     94   IPC::Channel* channel_;
     95 
     96   // A map of stream ids to delegates; must only be accessed on
     97   // |io_message_loop_|.
     98   IDMap<media::AudioOutputIPCDelegate> delegates_;
     99 
    100   // Audio hardware configuration to update when OnOutputDeviceChanged() fires.
    101   // Access is guarded by |lock_|.
    102   base::Lock lock_;
    103   media::AudioHardwareConfig* audio_hardware_config_;
    104 
    105   // Message loop on which IPC calls are driven.
    106   const scoped_refptr<base::MessageLoopProxy> io_message_loop_;
    107 
    108   // The singleton instance for this filter.
    109   static AudioMessageFilter* g_filter;
    110 
    111   DISALLOW_COPY_AND_ASSIGN(AudioMessageFilter);
    112 };
    113 
    114 }  // namespace content
    115 
    116 #endif  // CONTENT_RENDERER_MEDIA_AUDIO_MESSAGE_FILTER_H_
    117