OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/audio_input_message_filter.h" | 5 #include "content/renderer/media/audio_input_message_filter.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "content/common/child_process.h" | 10 #include "content/common/child_process.h" |
11 #include "content/common/media/audio_messages.h" | 11 #include "content/common/media/audio_messages.h" |
12 #include "ipc/ipc_logging.h" | 12 #include "ipc/ipc_logging.h" |
13 | 13 |
14 AudioInputMessageFilter* AudioInputMessageFilter::filter_ = NULL; | |
15 | |
16 AudioInputMessageFilter::AudioInputMessageFilter() | 14 AudioInputMessageFilter::AudioInputMessageFilter() |
17 : channel_(NULL) { | 15 : channel_(NULL) { |
18 DVLOG(1) << "AudioInputMessageFilter()"; | 16 DVLOG(1) << "AudioInputMessageFilter()"; |
19 DCHECK(!filter_); | |
20 filter_ = this; | |
21 } | 17 } |
22 | 18 |
23 AudioInputMessageFilter::~AudioInputMessageFilter() { | 19 AudioInputMessageFilter::~AudioInputMessageFilter() { |
24 DVLOG(1) << "AudioInputMessageFilter::~AudioInputMessageFilter()"; | 20 DVLOG(1) << "AudioInputMessageFilter::~AudioInputMessageFilter()"; |
25 | 21 |
26 // Just in case the message filter is deleted before the channel | 22 // Just in case the message filter is deleted before the channel |
27 // is closed and there are still living audio devices. | 23 // is closed and there are still living audio devices. |
28 OnChannelClosing(); | 24 OnChannelClosing(); |
29 | |
30 DCHECK_EQ(filter_, this); | |
31 filter_ = NULL; | |
32 } | |
33 | |
34 // static. | |
35 AudioInputMessageFilter* AudioInputMessageFilter::Get() { | |
36 return filter_; | |
37 } | 25 } |
38 | 26 |
39 bool AudioInputMessageFilter::Send(IPC::Message* message) { | 27 bool AudioInputMessageFilter::Send(IPC::Message* message) { |
40 if (!channel_) { | 28 if (!channel_) { |
41 delete message; | 29 delete message; |
42 return false; | 30 return false; |
43 } | 31 } |
44 | 32 |
45 if (MessageLoop::current() != ChildProcess::current()->io_message_loop()) { | 33 if (MessageLoop::current() != ChildProcess::current()->io_message_loop()) { |
46 // Can only access the IPC::Channel on the IPC thread since it's not thread | 34 // Can only access the IPC::Channel on the IPC thread since it's not thread |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 void AudioInputMessageFilter::OnDeviceStarted(int stream_id, | 129 void AudioInputMessageFilter::OnDeviceStarted(int stream_id, |
142 const std::string& device_id) { | 130 const std::string& device_id) { |
143 media::AudioInputIPCDelegate* delegate = delegates_.Lookup(stream_id); | 131 media::AudioInputIPCDelegate* delegate = delegates_.Lookup(stream_id); |
144 if (!delegate) { | 132 if (!delegate) { |
145 NOTREACHED(); | 133 NOTREACHED(); |
146 return; | 134 return; |
147 } | 135 } |
148 delegate->OnDeviceReady(device_id); | 136 delegate->OnDeviceReady(device_id); |
149 } | 137 } |
150 | 138 |
151 int AudioInputMessageFilter::AddDelegate( | 139 class AudioInputMessageFilter::AudioInputIPCImpl |
152 media::AudioInputIPCDelegate* delegate) { | 140 : public NON_EXPORTED_BASE(media::AudioInputIPC) { |
153 return delegates_.Add(delegate); | 141 public: |
| 142 AudioInputIPCImpl(int render_view_id, AudioInputMessageFilter* filter) |
| 143 : render_view_id_(render_view_id), filter_(filter) {} |
| 144 virtual ~AudioInputIPCImpl() {} |
| 145 |
| 146 // media::AudioInputIPC implementation |
| 147 virtual int AddDelegate(media::AudioInputIPCDelegate* delegate) OVERRIDE { |
| 148 return filter_->delegates_.Add(delegate); |
| 149 } |
| 150 virtual void RemoveDelegate(int id) OVERRIDE { |
| 151 filter_->delegates_.Remove(id); |
| 152 } |
| 153 virtual void CreateStream(int stream_id, const media::AudioParameters& params, |
| 154 const std::string& device_id, |
| 155 bool automatic_gain_control) OVERRIDE { |
| 156 filter_->Send(new AudioInputHostMsg_CreateStream( |
| 157 render_view_id_, stream_id, params, device_id, |
| 158 automatic_gain_control)); |
| 159 } |
| 160 virtual void StartDevice(int stream_id, int session_id) OVERRIDE { |
| 161 filter_->Send(new AudioInputHostMsg_StartDevice(stream_id, session_id)); |
| 162 } |
| 163 virtual void RecordStream(int stream_id) OVERRIDE { |
| 164 filter_->Send(new AudioInputHostMsg_RecordStream(stream_id)); |
| 165 } |
| 166 virtual void CloseStream(int stream_id) OVERRIDE { |
| 167 filter_->Send(new AudioInputHostMsg_CloseStream(stream_id)); |
| 168 } |
| 169 virtual void SetVolume(int stream_id, double volume) OVERRIDE { |
| 170 filter_->Send(new AudioInputHostMsg_SetVolume(stream_id, volume)); |
| 171 } |
| 172 |
| 173 private: |
| 174 const int render_view_id_; |
| 175 |
| 176 // Holds a reference to the audio input message filter (lives on the main |
| 177 // render thread) for the lifetime of this instance. |
| 178 scoped_refptr<AudioInputMessageFilter> filter_; |
| 179 |
| 180 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputIPCImpl); |
| 181 }; |
| 182 |
| 183 scoped_ptr<media::AudioInputIPC> AudioInputMessageFilter::CreateAudioInputIPC( |
| 184 int render_view_id) { |
| 185 return scoped_ptr<media::AudioInputIPC>( |
| 186 new AudioInputIPCImpl(render_view_id, this)); |
154 } | 187 } |
155 | |
156 void AudioInputMessageFilter::RemoveDelegate(int id) { | |
157 DVLOG(1) << "AudioInputMessageFilter::RemoveDelegate(id=" << id << ")"; | |
158 delegates_.Remove(id); | |
159 } | |
160 | |
161 void AudioInputMessageFilter::CreateStream(int stream_id, | |
162 const media::AudioParameters& params, const std::string& device_id, | |
163 bool automatic_gain_control) { | |
164 Send(new AudioInputHostMsg_CreateStream( | |
165 stream_id, params, device_id, automatic_gain_control)); | |
166 } | |
167 | |
168 void AudioInputMessageFilter::StartDevice(int stream_id, int session_id) { | |
169 Send(new AudioInputHostMsg_StartDevice(stream_id, session_id)); | |
170 } | |
171 | |
172 void AudioInputMessageFilter::RecordStream(int stream_id) { | |
173 Send(new AudioInputHostMsg_RecordStream(stream_id)); | |
174 } | |
175 | |
176 void AudioInputMessageFilter::CloseStream(int stream_id) { | |
177 Send(new AudioInputHostMsg_CloseStream(stream_id)); | |
178 } | |
179 | |
180 void AudioInputMessageFilter::SetVolume(int stream_id, double volume) { | |
181 Send(new AudioInputHostMsg_SetVolume(stream_id, volume)); | |
182 } | |
OLD | NEW |