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/message_filter.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 : public IPC::MessageFilter {
31 explicit AudioMessageFilter(
32 const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
34 // Getter for the one AudioMessageFilter object.
35 static AudioMessageFilter* Get();
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
42 // The returned object is not thread-safe, and must be used on
44 scoped_ptr<media::AudioOutputIPC> CreateAudioOutputIPC(int render_view_id,
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);
52 // IO message loop associated with this message filter.
53 scoped_refptr<base::MessageLoopProxy> io_message_loop() const {
54 return io_message_loop_;
58 ~AudioMessageFilter() override;
61 FRIEND_TEST_ALL_PREFIXES(AudioMessageFilterTest, Basic);
62 FRIEND_TEST_ALL_PREFIXES(AudioMessageFilterTest, Delegates);
64 // Implementation of media::AudioOutputIPC which augments IPC calls with
65 // stream_id and the source render_view_id.
66 class AudioOutputIPCImpl;
68 // Sends an IPC message using |sender_|.
69 void Send(IPC::Message* message);
71 // IPC::MessageFilter override. Called on |io_message_loop|.
72 bool OnMessageReceived(const IPC::Message& message) override;
73 void OnFilterAdded(IPC::Sender* sender) override;
74 void OnFilterRemoved() override;
75 void OnChannelClosing() override;
77 // Received when browser process has created an audio output stream.
78 void OnStreamCreated(int stream_id, base::SharedMemoryHandle handle,
79 base::SyncSocket::TransitDescriptor socket_descriptor,
82 // Received when internal state of browser process' audio output device has
84 void OnStreamStateChanged(int stream_id,
85 media::AudioOutputIPCDelegate::State state);
87 // Received when the browser process detects an output device change.
88 void OnOutputDeviceChanged(int stream_id, int new_buffer_size,
91 // IPC sender for Send(); must only be accesed on |io_message_loop_|.
94 // A map of stream ids to delegates; must only be accessed on
95 // |io_message_loop_|.
96 IDMap<media::AudioOutputIPCDelegate> delegates_;
98 // Audio hardware configuration to update when OnOutputDeviceChanged() fires.
99 // Access is guarded by |lock_|.
101 media::AudioHardwareConfig* audio_hardware_config_;
103 // Message loop on which IPC calls are driven.
104 const scoped_refptr<base::MessageLoopProxy> io_message_loop_;
106 // The singleton instance for this filter.
107 static AudioMessageFilter* g_filter;
109 DISALLOW_COPY_AND_ASSIGN(AudioMessageFilter);
112 } // namespace content
114 #endif // CONTENT_RENDERER_MEDIA_AUDIO_MESSAGE_FILTER_H_