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