| 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_message_filter.h" | 5 #include "content/renderer/media/audio_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 "content/renderer/render_thread_impl.h" | 12 #include "content/renderer/render_thread_impl.h" |
| 13 #include "ipc/ipc_logging.h" | 13 #include "ipc/ipc_logging.h" |
| 14 | 14 |
| 15 AudioMessageFilter* AudioMessageFilter::filter_ = NULL; | |
| 16 | |
| 17 // static | |
| 18 AudioMessageFilter* AudioMessageFilter::Get() { | |
| 19 return filter_; | |
| 20 } | |
| 21 | |
| 22 AudioMessageFilter::AudioMessageFilter() | 15 AudioMessageFilter::AudioMessageFilter() |
| 23 : channel_(NULL) { | 16 : channel_(NULL) { |
| 24 DVLOG(1) << "AudioMessageFilter::AudioMessageFilter()"; | 17 DVLOG(1) << "AudioMessageFilter::AudioMessageFilter()"; |
| 25 DCHECK(!filter_); | |
| 26 filter_ = this; | |
| 27 } | 18 } |
| 28 | 19 |
| 29 int AudioMessageFilter::AddDelegate(media::AudioOutputIPCDelegate* delegate) { | 20 class AudioMessageFilter::AudioOutputIPCImpl |
| 30 return delegates_.Add(delegate); | 21 : public NON_EXPORTED_BASE(media::AudioOutputIPC) { |
| 31 } | 22 public: |
| 23 AudioOutputIPCImpl(int render_view_id, AudioMessageFilter* filter) |
| 24 : render_view_id_(render_view_id), filter_(filter) {} |
| 25 virtual ~AudioOutputIPCImpl() {} |
| 32 | 26 |
| 33 void AudioMessageFilter::RemoveDelegate(int id) { | 27 // media::AudioOutputIPC implementation. |
| 34 delegates_.Remove(id); | 28 virtual int AddDelegate(media::AudioOutputIPCDelegate* delegate) OVERRIDE { |
| 35 } | 29 return filter_->delegates_.Add(delegate); |
| 30 } |
| 31 virtual void RemoveDelegate(int id) OVERRIDE { |
| 32 filter_->delegates_.Remove(id); |
| 33 } |
| 34 virtual void CreateStream(int stream_id, |
| 35 const media::AudioParameters& params, |
| 36 int input_channels) OVERRIDE { |
| 37 filter_->Send(new AudioHostMsg_CreateStream( |
| 38 render_view_id_, stream_id, params, input_channels)); |
| 39 } |
| 40 virtual void PlayStream(int stream_id) OVERRIDE { |
| 41 filter_->Send(new AudioHostMsg_PlayStream(stream_id)); |
| 42 } |
| 43 virtual void PauseStream(int stream_id) OVERRIDE { |
| 44 filter_->Send(new AudioHostMsg_PauseStream(stream_id)); |
| 45 } |
| 46 virtual void FlushStream(int stream_id) OVERRIDE { |
| 47 filter_->Send(new AudioHostMsg_FlushStream(stream_id)); |
| 48 } |
| 49 virtual void CloseStream(int stream_id) OVERRIDE { |
| 50 filter_->Send(new AudioHostMsg_CloseStream(stream_id)); |
| 51 } |
| 52 virtual void SetVolume(int stream_id, double volume) OVERRIDE { |
| 53 filter_->Send(new AudioHostMsg_SetVolume(stream_id, volume)); |
| 54 } |
| 36 | 55 |
| 37 void AudioMessageFilter::CreateStream(int stream_id, | 56 private: |
| 38 const media::AudioParameters& params, | 57 const int render_view_id_; |
| 39 int input_channels) { | |
| 40 Send(new AudioHostMsg_CreateStream(stream_id, params, input_channels)); | |
| 41 } | |
| 42 | 58 |
| 43 void AudioMessageFilter::PlayStream(int stream_id) { | 59 // Holds a reference to the audio message filter (lives on the main render |
| 44 Send(new AudioHostMsg_PlayStream(stream_id)); | 60 // thread) for the lifetime of this instance. |
| 45 } | 61 scoped_refptr<AudioMessageFilter> filter_; |
| 46 | 62 |
| 47 void AudioMessageFilter::PauseStream(int stream_id) { | 63 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioOutputIPCImpl); |
| 48 Send(new AudioHostMsg_PauseStream(stream_id)); | 64 }; |
| 49 } | |
| 50 | 65 |
| 51 void AudioMessageFilter::FlushStream(int stream_id) { | 66 scoped_ptr<media::AudioOutputIPC> AudioMessageFilter::CreateAudioOutputIPC( |
| 52 Send(new AudioHostMsg_FlushStream(stream_id)); | 67 int render_view_id) { |
| 53 } | 68 return scoped_ptr<media::AudioOutputIPC>( |
| 54 | 69 new AudioOutputIPCImpl(render_view_id, this)); |
| 55 void AudioMessageFilter::CloseStream(int stream_id) { | |
| 56 Send(new AudioHostMsg_CloseStream(stream_id)); | |
| 57 } | |
| 58 | |
| 59 void AudioMessageFilter::SetVolume(int stream_id, double volume) { | |
| 60 Send(new AudioHostMsg_SetVolume(stream_id, volume)); | |
| 61 } | 70 } |
| 62 | 71 |
| 63 bool AudioMessageFilter::Send(IPC::Message* message) { | 72 bool AudioMessageFilter::Send(IPC::Message* message) { |
| 64 if (!channel_) { | 73 if (!channel_) { |
| 65 delete message; | 74 delete message; |
| 66 return false; | 75 return false; |
| 67 } | 76 } |
| 68 | 77 |
| 69 if (MessageLoop::current() != ChildProcess::current()->io_message_loop()) { | 78 if (MessageLoop::current() != ChildProcess::current()->io_message_loop()) { |
| 70 // Can only access the IPC::Channel on the IPC thread since it's not thread | 79 // Can only access the IPC::Channel on the IPC thread since it's not thread |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 it.Advance(); | 119 it.Advance(); |
| 111 } | 120 } |
| 112 } | 121 } |
| 113 | 122 |
| 114 AudioMessageFilter::~AudioMessageFilter() { | 123 AudioMessageFilter::~AudioMessageFilter() { |
| 115 DVLOG(1) << "AudioMessageFilter::~AudioMessageFilter()"; | 124 DVLOG(1) << "AudioMessageFilter::~AudioMessageFilter()"; |
| 116 | 125 |
| 117 // Just in case the message filter is deleted before the channel | 126 // Just in case the message filter is deleted before the channel |
| 118 // is closed and there are still living audio devices. | 127 // is closed and there are still living audio devices. |
| 119 OnChannelClosing(); | 128 OnChannelClosing(); |
| 120 | |
| 121 DCHECK(filter_); | |
| 122 filter_ = NULL; | |
| 123 } | 129 } |
| 124 | 130 |
| 125 void AudioMessageFilter::OnStreamCreated( | 131 void AudioMessageFilter::OnStreamCreated( |
| 126 int stream_id, | 132 int stream_id, |
| 127 base::SharedMemoryHandle handle, | 133 base::SharedMemoryHandle handle, |
| 128 #if defined(OS_WIN) | 134 #if defined(OS_WIN) |
| 129 base::SyncSocket::Handle socket_handle, | 135 base::SyncSocket::Handle socket_handle, |
| 130 #else | 136 #else |
| 131 base::FileDescriptor socket_descriptor, | 137 base::FileDescriptor socket_descriptor, |
| 132 #endif | 138 #endif |
| (...skipping 14 matching lines...) Expand all Loading... |
| 147 | 153 |
| 148 void AudioMessageFilter::OnStreamStateChanged( | 154 void AudioMessageFilter::OnStreamStateChanged( |
| 149 int stream_id, media::AudioOutputIPCDelegate::State state) { | 155 int stream_id, media::AudioOutputIPCDelegate::State state) { |
| 150 media::AudioOutputIPCDelegate* delegate = delegates_.Lookup(stream_id); | 156 media::AudioOutputIPCDelegate* delegate = delegates_.Lookup(stream_id); |
| 151 if (!delegate) { | 157 if (!delegate) { |
| 152 DLOG(WARNING) << "No delegate found for state change. " << state; | 158 DLOG(WARNING) << "No delegate found for state change. " << state; |
| 153 return; | 159 return; |
| 154 } | 160 } |
| 155 delegate->OnStateChanged(state); | 161 delegate->OnStateChanged(state); |
| 156 } | 162 } |
| OLD | NEW |