| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // AudioRendererHost serves audio related requests from AudioRenderer which | 5 // AudioRendererHost serves audio related requests from AudioRenderer which |
| 6 // lives inside the render process and provide access to audio hardware. It maps | 6 // lives inside the render process and provide access to audio hardware. It maps |
| 7 // an internal ID to AudioRendererHost::IPCAudioSource in a map, which is the | 7 // an internal ID to AudioRendererHost::IPCAudioSource in a map, which is the |
| 8 // actual object providing audio packets through IPC. It creates the actual | 8 // actual object providing audio packets through IPC. It creates the actual |
| 9 // AudioOutputStream object when requested by the renderer provided with | 9 // AudioOutputStream object when requested by the renderer provided with |
| 10 // render view id and stream id. | 10 // render view id and stream id. |
| 11 // | 11 // |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 #include <deque> | 71 #include <deque> |
| 72 | 72 |
| 73 #include "base/lock.h" | 73 #include "base/lock.h" |
| 74 #include "base/process.h" | 74 #include "base/process.h" |
| 75 #include "base/ref_counted.h" | 75 #include "base/ref_counted.h" |
| 76 #include "base/shared_memory.h" | 76 #include "base/shared_memory.h" |
| 77 #include "base/waitable_event.h" | 77 #include "base/waitable_event.h" |
| 78 #include "chrome/common/ipc_message.h" | 78 #include "chrome/common/ipc_message.h" |
| 79 #include "media/audio/audio_output.h" | 79 #include "media/audio/audio_output.h" |
| 80 #include "media/audio/simple_sources.h" | 80 #include "media/audio/simple_sources.h" |
| 81 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 81 | 82 |
| 82 class AudioManager; | 83 class AudioManager; |
| 83 class MessageLoop; | 84 class MessageLoop; |
| 84 struct ViewHostMsg_Audio_CreateStream; | 85 struct ViewHostMsg_Audio_CreateStream; |
| 85 | 86 |
| 86 class AudioRendererHost : public base::RefCountedThreadSafe<AudioRendererHost> { | 87 class AudioRendererHost : public base::RefCountedThreadSafe<AudioRendererHost> { |
| 87 private: | 88 private: |
| 88 class IPCAudioSource; | 89 class IPCAudioSource; |
| 89 friend class AudioRendererHost::IPCAudioSource; | |
| 90 public: | 90 public: |
| 91 // Called from UI thread from the owner of this object. | 91 // Called from UI thread from the owner of this object. |
| 92 explicit AudioRendererHost(MessageLoop* message_loop); | 92 explicit AudioRendererHost(MessageLoop* message_loop); |
| 93 | 93 |
| 94 // Destruction can happen on either UI thread or IO thread, but at destruction | 94 // Destruction can happen on either UI thread or IO thread, but at destruction |
| 95 // all associated sources are destroyed and streams are closed. | 95 // all associated sources are destroyed and streams are closed. |
| 96 ~AudioRendererHost(); | 96 ~AudioRendererHost(); |
| 97 | 97 |
| 98 // Called from UI thread from the owner of this object to kick start | 98 // Called from UI thread from the owner of this object to kick start |
| 99 // destruction of streams in IO thread. | 99 // destruction of streams in IO thread. |
| 100 void Destroy(); | 100 void Destroy(); |
| 101 | 101 |
| 102 //--------------------------------------------------------------------------- | 102 //--------------------------------------------------------------------------- |
| 103 // The following public methods are called from ResourceMessageFilter in the | 103 // The following public methods are called from ResourceMessageFilter in the |
| 104 // IO thread. | 104 // IO thread. |
| 105 | 105 |
| 106 // Event received when IPC channel is connected with the renderer process. | 106 // Event received when IPC channel is connected with the renderer process. |
| 107 void IPCChannelConnected(int process_id, base::ProcessHandle process_handle, | 107 void IPCChannelConnected(int process_id, base::ProcessHandle process_handle, |
| 108 IPC::Message::Sender* ipc_sender); | 108 IPC::Message::Sender* ipc_sender); |
| 109 | 109 |
| 110 // Event received when IPC channel is closing. | 110 // Event received when IPC channel is closing. |
| 111 void IPCChannelClosing(); | 111 void IPCChannelClosing(); |
| 112 | 112 |
| 113 // Returns true if the message is a audio related message and was processed. | 113 // Returns true if the message is a audio related message and was processed. |
| 114 // If it was, message_was_ok will be false iff the message was corrupt. | 114 // If it was, message_was_ok will be false iff the message was corrupt. |
| 115 bool OnMessageReceived(const IPC::Message& message, bool* message_was_ok); | 115 bool OnMessageReceived(const IPC::Message& message, bool* message_was_ok); |
| 116 | 116 |
| 117 private: | 117 protected: |
| 118 //--------------------------------------------------------------------------- | |
| 119 // Methods called on IO thread. | |
| 120 // Returns true if the message is an audio related message and should be | |
| 121 // handled by this class. | |
| 122 bool IsAudioRendererHostMessage(const IPC::Message& message); | |
| 123 | |
| 124 // Audio related IPC message handlers. | |
| 125 // Creates an audio output stream with the specified format. If this call is | |
| 126 // successful this object would keep an internal entry of the stream for the | |
| 127 // required properties. See IPCAudioSource::CreateIPCAudioSource for more | |
| 128 // details. | |
| 129 void OnCreateStream(const IPC::Message& msg, int stream_id, | |
| 130 const ViewHostMsg_Audio_CreateStream& params); | |
| 131 | |
| 132 // Starts buffering for the audio output stream. Delegates the start method | |
| 133 // call to the corresponding IPCAudioSource::Start. | |
| 134 // ViewMsg_NotifyAudioStreamStateChanged with | |
| 135 // AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the | |
| 136 // required IPCAudioSource is not found. | |
| 137 void OnStartStream(const IPC::Message& msg, int stream_id); | |
| 138 | |
| 139 // Pauses the audio output stream. Delegates the pause method call to the | |
| 140 // corresponding IPCAudioSource::Pause, ViewMsg_NotifyAudioStreamStateChanged | |
| 141 // with AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the | |
| 142 // required IPCAudioSource is not found. | |
| 143 void OnPauseStream(const IPC::Message& msg, int stream_id); | |
| 144 | |
| 145 // Closes the audio output stream, delegates the close method call to the | |
| 146 // corresponding IPCAudioSource::Close, no returning IPC message to renderer | |
| 147 // upon success and failure. | |
| 148 void OnCloseStream(const IPC::Message& msg, int stream_id); | |
| 149 | |
| 150 // Set the volume for the stream specified. Delegates the SetVolume method | |
| 151 // call to IPCAudioSource. No returning IPC message to renderer upon success. | |
| 152 // ViewMsg_NotifyAudioStreamStateChanged with | |
| 153 // AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the | |
| 154 // required IPCAudioSource is not found. | |
| 155 void OnSetVolume(const IPC::Message& msg, int stream_id, | |
| 156 double left_channel, double right_channel); | |
| 157 | |
| 158 // Gets the volume of the stream specified, delegates to corresponding | |
| 159 // IPCAudioSource::GetVolume, see the method for more details. | |
| 160 // ViewMsg_NotifyAudioStreamStateChanged with | |
| 161 // AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the | |
| 162 // required IPCAudioSource is not found. | |
| 163 void OnGetVolume(const IPC::Message& msg, int stream_id); | |
| 164 | |
| 165 // Notify packet has been prepared for stream, delegates to corresponding | |
| 166 // IPCAudioSource::NotifyPacketReady, see the method for more details. | |
| 167 // ViewMsg_NotifyAudioStreamStateChanged with | |
| 168 // AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the | |
| 169 // required IPCAudioSource is not found. | |
| 170 void OnNotifyPacketReady(const IPC::Message& msg, int stream_id, | |
| 171 size_t packet_size); | |
| 172 | |
| 173 // Called on IO thread when this object is created and initialized. | |
| 174 void OnInitialized(); | |
| 175 | |
| 176 // Called on IO thread when this object needs to be destroyed and after | |
| 177 // Destroy() is called from owner of this class in UI thread. | |
| 178 void OnDestroyed(); | |
| 179 | |
| 180 // Sends IPC messages using ipc_sender_. | |
| 181 void OnSend(IPC::Message* message); | |
| 182 | |
| 183 // Closes the source, deletes it and removes it from the internal map. | |
| 184 // Destruction of source and associated stream should always be done by this | |
| 185 // method. *DO NOT* call this method from other than IPCAudioSource and from | |
| 186 // this class. | |
| 187 void OnDestroySource(IPCAudioSource* source); | |
| 188 | |
| 189 // A helper method that destroy all IPCAudioSource and associated audio | |
| 190 // output streams. | |
| 191 void DestroyAllSources(); | |
| 192 | |
| 193 // A helper method to look up a IPCAudioSource with a tuple of render view id | |
| 194 // and stream id. Returns NULL if not found. | |
| 195 IPCAudioSource* Lookup(int render_view_id, int stream_id); | |
| 196 | |
| 197 //--------------------------------------------------------------------------- | 118 //--------------------------------------------------------------------------- |
| 198 // Helper methods called from IPCAudioSource or from this class, since | 119 // Helper methods called from IPCAudioSource or from this class, since |
| 199 // methods in IPCAudioSource maybe called from hardware audio threads, these | 120 // methods in IPCAudioSource maybe called from hardware audio threads, these |
| 200 // methods make sure the actual tasks happen on IO thread. | 121 // methods make sure the actual tasks happen on IO thread. |
| 122 // These methods are virtual protected so we can mock these methods to test |
| 123 // IPCAudioSource. |
| 201 | 124 |
| 202 // A helper method to send an IPC message to renderer process on IO thread. | 125 // A helper method to send an IPC message to renderer process on IO thread. |
| 203 void Send(IPC::Message* message); | 126 virtual void Send(IPC::Message* message); |
| 204 | 127 |
| 205 // A helper method for sending error IPC messages. | 128 // A helper method for sending error IPC messages. |
| 206 void SendErrorMessage(int32 render_view_id, int32 stream_id, int info); | 129 virtual void SendErrorMessage(int32 render_view_id, |
| 130 int32 stream_id, |
| 131 int info); |
| 207 | 132 |
| 208 // A helper method for calling OnDestroySource on IO thread. | 133 // A helper method for calling OnDestroySource on IO thread. |
| 209 void DestroySource(IPCAudioSource* source); | 134 virtual void DestroySource(IPCAudioSource* source); |
| 210 | 135 |
| 211 MessageLoop* io_loop() { return io_loop_; } | 136 private: |
| 137 friend class AudioRendererHost::IPCAudioSource; |
| 138 friend class AudioRendererHostTest; |
| 139 FRIEND_TEST(AudioRendererHostTest, CreateMockStream); |
| 140 FRIEND_TEST(AudioRendererHostTest, MockStreamDataConversation); |
| 212 | 141 |
| 213 // The container for AudioOutputStream and serves the audio packet received | 142 // The container for AudioOutputStream and serves the audio packet received |
| 214 // via IPC. | 143 // via IPC. |
| 215 class IPCAudioSource : public AudioOutputStream::AudioSourceCallback { | 144 class IPCAudioSource : public AudioOutputStream::AudioSourceCallback { |
| 216 public: | 145 public: |
| 217 // Factory method for creating an IPCAudioSource, returns NULL if failed. | 146 // Factory method for creating an IPCAudioSource, returns NULL if failed. |
| 218 // The IPCAudioSource object will have an internal state of | 147 // The IPCAudioSource object will have an internal state of |
| 219 // AudioOutputStream::STATE_CREATED after creation. | 148 // AudioOutputStream::STATE_CREATED after creation. |
| 220 // If an IPCAudioSource is created successfully, a | 149 // If an IPCAudioSource is created successfully, a |
| 221 // ViewMsg_NotifyAudioStreamCreated message is sent to the renderer. | 150 // ViewMsg_NotifyAudioStreamCreated message is sent to the renderer. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 // Number of bytes copied in the last OnMoreData call. | 247 // Number of bytes copied in the last OnMoreData call. |
| 319 size_t last_copied_bytes_; | 248 size_t last_copied_bytes_; |
| 320 | 249 |
| 321 // Protects: | 250 // Protects: |
| 322 // - |outstanding_requests_| | 251 // - |outstanding_requests_| |
| 323 // - |last_copied_bytes_| | 252 // - |last_copied_bytes_| |
| 324 // - |push_source_| | 253 // - |push_source_| |
| 325 Lock lock_; | 254 Lock lock_; |
| 326 }; | 255 }; |
| 327 | 256 |
| 257 //--------------------------------------------------------------------------- |
| 258 // Methods called on IO thread. |
| 259 // Returns true if the message is an audio related message and should be |
| 260 // handled by this class. |
| 261 bool IsAudioRendererHostMessage(const IPC::Message& message); |
| 262 |
| 263 // Audio related IPC message handlers. |
| 264 // Creates an audio output stream with the specified format. If this call is |
| 265 // successful this object would keep an internal entry of the stream for the |
| 266 // required properties. See IPCAudioSource::CreateIPCAudioSource() for more |
| 267 // details. |
| 268 void OnCreateStream(const IPC::Message& msg, int stream_id, |
| 269 const ViewHostMsg_Audio_CreateStream& params); |
| 270 |
| 271 // Starts buffering for the audio output stream. Delegates the start method |
| 272 // call to the corresponding IPCAudioSource::Start(). |
| 273 // ViewMsg_NotifyAudioStreamStateChanged with |
| 274 // AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the |
| 275 // required IPCAudioSource is not found. |
| 276 void OnStartStream(const IPC::Message& msg, int stream_id); |
| 277 |
| 278 // Pauses the audio output stream. Delegates the pause method call to the |
| 279 // corresponding IPCAudioSource::Pause(), |
| 280 // ViewMsg_NotifyAudioStreamStateChanged with |
| 281 // AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the |
| 282 // required IPCAudioSource is not found. |
| 283 void OnPauseStream(const IPC::Message& msg, int stream_id); |
| 284 |
| 285 // Closes the audio output stream, delegates the close method call to the |
| 286 // corresponding IPCAudioSource::Close(), no returning IPC message to renderer |
| 287 // upon success and failure. |
| 288 void OnCloseStream(const IPC::Message& msg, int stream_id); |
| 289 |
| 290 // Set the volume for the stream specified. Delegates the SetVolume() method |
| 291 // call to IPCAudioSource. No returning IPC message to renderer upon success. |
| 292 // ViewMsg_NotifyAudioStreamStateChanged with |
| 293 // AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the |
| 294 // required IPCAudioSource is not found. |
| 295 void OnSetVolume(const IPC::Message& msg, int stream_id, |
| 296 double left_channel, double right_channel); |
| 297 |
| 298 // Gets the volume of the stream specified, delegates to corresponding |
| 299 // IPCAudioSource::GetVolume(), see the method for more details. |
| 300 // ViewMsg_NotifyAudioStreamStateChanged with |
| 301 // AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the |
| 302 // required IPCAudioSource is not found. |
| 303 void OnGetVolume(const IPC::Message& msg, int stream_id); |
| 304 |
| 305 // Notify packet has been prepared for stream, delegates to corresponding |
| 306 // IPCAudioSource::NotifyPacketReady(), see the method for more details. |
| 307 // ViewMsg_NotifyAudioStreamStateChanged with |
| 308 // AudioOutputStream::AUDIO_STREAM_ERROR is sent back to renderer if the |
| 309 // required IPCAudioSource is not found. |
| 310 void OnNotifyPacketReady(const IPC::Message& msg, int stream_id, |
| 311 size_t packet_size); |
| 312 |
| 313 // Called on IO thread when this object is created and initialized. |
| 314 void OnInitialized(); |
| 315 |
| 316 // Called on IO thread when this object needs to be destroyed and after |
| 317 // Destroy() is called from owner of this class in UI thread. |
| 318 void OnDestroyed(); |
| 319 |
| 320 // Sends IPC messages using ipc_sender_. |
| 321 void OnSend(IPC::Message* message); |
| 322 |
| 323 // Closes the source, deletes it and removes it from the internal map. |
| 324 // Destruction of source and associated stream should always be done by this |
| 325 // method. *DO NOT* call this method from other than IPCAudioSource and from |
| 326 // this class. |
| 327 void OnDestroySource(IPCAudioSource* source); |
| 328 |
| 329 // A helper method that destroy all IPCAudioSource and associated audio |
| 330 // output streams. |
| 331 void DestroyAllSources(); |
| 332 |
| 333 // A helper method to look up a IPCAudioSource with a tuple of render view id |
| 334 // and stream id. Returns NULL if not found. |
| 335 IPCAudioSource* Lookup(int render_view_id, int stream_id); |
| 336 |
| 337 MessageLoop* io_loop() { return io_loop_; } |
| 338 |
| 328 int process_id_; | 339 int process_id_; |
| 329 base::ProcessHandle process_handle_; | 340 base::ProcessHandle process_handle_; |
| 330 IPC::Message::Sender* ipc_sender_; | 341 IPC::Message::Sender* ipc_sender_; |
| 331 | 342 |
| 332 // A map of id to audio sources. | 343 // A map of id to audio sources. |
| 333 typedef std::pair<int32, int32> SourceID; | 344 typedef std::pair<int32, int32> SourceID; |
| 334 typedef std::map<SourceID, IPCAudioSource*> SourceMap; | 345 typedef std::map<SourceID, IPCAudioSource*> SourceMap; |
| 335 SourceMap sources_; | 346 SourceMap sources_; |
| 336 | 347 |
| 337 MessageLoop* io_loop_; | 348 MessageLoop* io_loop_; |
| 338 | 349 |
| 339 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 350 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 340 }; | 351 }; |
| 341 | 352 |
| 342 #endif // CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ | 353 #endif // CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |