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.
5 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_MESSAGE_FILTER_H_
6 #define CONTENT_RENDERER_MEDIA_AUDIO_MESSAGE_FILTER_H_
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"
20 class MessageLoopProxy;
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 {
32 explicit AudioMessageFilter(
33 const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
35 // Getter for the one AudioMessageFilter object.
36 static AudioMessageFilter* Get();
38 // Create an AudioOutputIPC to be owned by one delegate. |render_view_id| is
39 // the render view containing the entity producing the audio.
41 // The returned object is not thread-safe, and must be used on
43 scoped_ptr<media::AudioOutputIPC> CreateAudioOutputIPC(int render_view_id);
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);
50 // IO message loop associated with this message filter.
51 scoped_refptr<base::MessageLoopProxy> io_message_loop() const {
52 return io_message_loop_;
56 virtual ~AudioMessageFilter();
59 FRIEND_TEST_ALL_PREFIXES(AudioMessageFilterTest, Basic);
60 FRIEND_TEST_ALL_PREFIXES(AudioMessageFilterTest, Delegates);
62 // Implementation of media::AudioOutputIPC which augments IPC calls with
63 // stream_id and the source render_view_id.
64 class AudioOutputIPCImpl;
66 // Sends an IPC message using |channel_|.
67 void Send(IPC::Message* message);
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;
75 // Received when browser process has created an audio output stream.
76 void OnStreamCreated(int stream_id, base::SharedMemoryHandle handle,
78 base::SyncSocket::Handle socket_handle,
80 base::FileDescriptor socket_descriptor,
84 // Received when internal state of browser process' audio output device has
86 void OnStreamStateChanged(int stream_id,
87 media::AudioOutputIPCDelegate::State state);
89 // Received when the browser process detects an output device change.
90 void OnOutputDeviceChanged(int stream_id, int new_buffer_size,
93 // IPC channel for Send(); must only be accesed on |io_message_loop_|.
94 IPC::Channel* channel_;
96 // A map of stream ids to delegates; must only be accessed on
97 // |io_message_loop_|.
98 IDMap<media::AudioOutputIPCDelegate> delegates_;
100 // Audio hardware configuration to update when OnOutputDeviceChanged() fires.
101 // Access is guarded by |lock_|.
103 media::AudioHardwareConfig* audio_hardware_config_;
105 // Message loop on which IPC calls are driven.
106 const scoped_refptr<base::MessageLoopProxy> io_message_loop_;
108 // The singleton instance for this filter.
109 static AudioMessageFilter* g_filter;
111 DISALLOW_COPY_AND_ASSIGN(AudioMessageFilter);
114 } // namespace content
116 #endif // CONTENT_RENDERER_MEDIA_AUDIO_MESSAGE_FILTER_H_