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
19namespace base {
20class MessageLoopProxy;
21}
22
23namespace 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.
29class 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                       base::SyncSocket::TransitDescriptor socket_descriptor,
80                       uint32 length);
81
82  // Received when internal state of browser process' audio output device has
83  // changed.
84  void OnStreamStateChanged(int stream_id,
85                            media::AudioOutputIPCDelegate::State state);
86
87  // Received when the browser process detects an output device change.
88  void OnOutputDeviceChanged(int stream_id, int new_buffer_size,
89                             int new_sample_rate);
90
91  // IPC sender for Send(); must only be accesed on |io_message_loop_|.
92  IPC::Sender* sender_;
93
94  // A map of stream ids to delegates; must only be accessed on
95  // |io_message_loop_|.
96  IDMap<media::AudioOutputIPCDelegate> delegates_;
97
98  // Audio hardware configuration to update when OnOutputDeviceChanged() fires.
99  // Access is guarded by |lock_|.
100  base::Lock lock_;
101  media::AudioHardwareConfig* audio_hardware_config_;
102
103  // Message loop on which IPC calls are driven.
104  const scoped_refptr<base::MessageLoopProxy> io_message_loop_;
105
106  // The singleton instance for this filter.
107  static AudioMessageFilter* g_filter;
108
109  DISALLOW_COPY_AND_ASSIGN(AudioMessageFilter);
110};
111
112}  // namespace content
113
114#endif  // CONTENT_RENDERER_MEDIA_AUDIO_MESSAGE_FILTER_H_
115