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_INPUT_MESSAGE_FILTER_H_
6#define CONTENT_RENDERER_MEDIA_AUDIO_INPUT_MESSAGE_FILTER_H_
7
8#include "base/id_map.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/shared_memory.h"
11#include "base/sync_socket.h"
12#include "content/common/content_export.h"
13#include "ipc/ipc_channel_proxy.h"
14#include "media/audio/audio_input_ipc.h"
15
16namespace base {
17class MessageLoopProxy;
18}
19
20namespace content {
21
22// MessageFilter that handles audio input messages and delegates them to
23// audio capturers. Created on render thread, AudioMessageFilter is operated on
24// IO thread (secondary thread of render process), it intercepts audio messages
25// and process them on IO thread since these messages are time critical.
26class CONTENT_EXPORT AudioInputMessageFilter
27    : public IPC::ChannelProxy::MessageFilter {
28 public:
29  explicit AudioInputMessageFilter(
30      const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
31
32  // Getter for the one AudioInputMessageFilter object.
33  static AudioInputMessageFilter* Get();
34
35  // Create an AudioInputIPC to be owned by one delegate.  |render_view_id| is
36  // the render view containing the entity consuming the audio.
37  //
38  // The returned object is not thread-safe, and must be used on
39  // |io_message_loop|.
40  scoped_ptr<media::AudioInputIPC> CreateAudioInputIPC(int render_view_id);
41
42  scoped_refptr<base::MessageLoopProxy> io_message_loop() const {
43    return io_message_loop_;
44  }
45
46 private:
47  // Implementation of media::AudioInputIPC which augments IPC calls with
48  // stream_id and the destination render_view_id.
49  class AudioInputIPCImpl;
50
51  virtual ~AudioInputMessageFilter();
52
53  // Sends an IPC message using |channel_|.
54  void Send(IPC::Message* message);
55
56  // IPC::ChannelProxy::MessageFilter override. Called on |io_message_loop_|.
57  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
58  virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE;
59  virtual void OnFilterRemoved() OVERRIDE;
60  virtual void OnChannelClosing() OVERRIDE;
61
62  // Received when browser process has created an audio input stream.
63  void OnStreamCreated(int stream_id,
64                       base::SharedMemoryHandle handle,
65#if defined(OS_WIN)
66                       base::SyncSocket::Handle socket_handle,
67#else
68                       base::FileDescriptor socket_descriptor,
69#endif
70                       uint32 length,
71                       uint32 total_segments);
72
73  // Notification of volume property of an audio input stream.
74  void OnStreamVolume(int stream_id, double volume);
75
76  // Received when internal state of browser process' audio input stream has
77  // changed.
78  void OnStreamStateChanged(int stream_id,
79                            media::AudioInputIPCDelegate::State state);
80
81  // A map of stream ids to delegates.
82  IDMap<media::AudioInputIPCDelegate> delegates_;
83
84  // IPC channel for Send(), must only be accesed on |io_message_loop_|.
85  IPC::Channel* channel_;
86
87  // Message loop on which IPC calls are driven.
88  const scoped_refptr<base::MessageLoopProxy> io_message_loop_;
89
90  // The singleton instance for this filter.
91  static AudioInputMessageFilter* g_filter;
92
93  DISALLOW_COPY_AND_ASSIGN(AudioInputMessageFilter);
94};
95
96}  // namespace content
97
98#endif  // CONTENT_RENDERER_MEDIA_AUDIO_INPUT_MESSAGE_FILTER_H_
99