OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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 and returns an | 9 // AudioOutputStream object when requested by the renderer and returns an |
10 // internal ID. It then delegates calls to the AudioOutputStream object indexed | 10 // internal ID. It then delegates calls to the AudioOutputStream object indexed |
11 // by the internal id. | 11 // by the internal id. |
12 // | 12 // |
13 // AudioRendererHost::IPCAudioSource is a container of AudioOutputStream and | 13 // AudioRendererHost::IPCAudioSource is a container of AudioOutputStream and |
14 // provide audio packets to the associated AudioOutputStream through IPC. It | 14 // provide audio packets to the associated AudioOutputStream through IPC. It |
15 // transforms the pull data model to a push model used for IPC. When asked by | 15 // transforms the pull data model to a push model used for IPC. When asked by |
16 // AudioOutputStream for an audio packet, it would send a message to renderer, | 16 // AudioOutputStream for an audio packet, it would send a message to renderer, |
17 // passing a SharedMemoryHandle for filling the buffer. | 17 // passing a SharedMemoryHandle for filling the buffer. |
18 // NotifyPacketReady(|stream_id|) would be called when the buffer is filled | 18 // NotifyPacketReady(|stream_id|) would be called when the buffer is filled |
19 // and ready to be consumed. | 19 // and ready to be consumed. |
| 20 // |
| 21 // This class is owned by BrowserRenderProcessHost, and instantiated on UI |
| 22 // thread, but all other operations and method calls (except Destroy()) happens |
| 23 // in IO thread, so we need to be extra careful about the lifetime of this |
| 24 // object. AudioManager is a singleton and created in IO thread, audio output |
| 25 // streams are also created in the IO thread, so we need to destroy them also |
| 26 // in IO thread. After this class is created, a task of OnInitialized() is |
| 27 // posted on IO thread in which singleton of AudioManager is created and |
| 28 // AddRef() is called to increase one ref count of this object. Owner of this |
| 29 // class should call Destroy() before decrementing the ref count to this object, |
| 30 // which essentially post a task of OnDestroyed() on IO thread. Inside |
| 31 // OnDestroyed(), audio output streams are destroyed and Release() is called |
| 32 // which may result in self-destruction. |
| 33 // |
| 34 // TODO(hclam): Have these things done before having real implementations: |
| 35 // 1. Make sure this class has greater or equal lifetime to |
| 36 // IPC:Message::Sender, essentially ResourceMessageFilter. |
| 37 // 2. Listen to destruction event of the browser and do cleanup in case this |
| 38 // class is not destructed nicely during browser close. |
20 | 39 |
21 #ifndef CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ | 40 #ifndef CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ |
22 #define CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ | 41 #define CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ |
23 | 42 |
24 #include "base/id_map.h" | 43 #include "base/id_map.h" |
| 44 #include "base/ref_counted.h" |
25 #include "base/shared_memory.h" | 45 #include "base/shared_memory.h" |
26 #include "chrome/common/ipc_message.h" | 46 #include "chrome/common/ipc_message.h" |
27 #include "media/audio/audio_output.h" | 47 #include "media/audio/audio_output.h" |
28 | 48 |
29 class AudioManager; | 49 class AudioManager; |
30 class MessageLoop; | 50 class MessageLoop; |
31 | 51 |
32 class AudioRendererHost { | 52 class AudioRendererHost : public base::RefCountedThreadSafe<AudioRendererHost> { |
33 public: | 53 public: |
34 static const int32 INVALID_ID = 0; | 54 static const int32 INVALID_ID = 0; |
35 | 55 |
36 explicit AudioRendererHost(MessageLoop* message_loop); | 56 explicit AudioRendererHost(MessageLoop* message_loop); |
37 ~AudioRendererHost(); | 57 ~AudioRendererHost(); |
38 | 58 |
39 // Creates an audio output stream with the specified format, returns the | 59 // Creates an audio output stream with the specified format, returns the |
40 // stream id if successful, otherwise INVALID_ID. If this call is successful | 60 // stream id if successful, otherwise INVALID_ID. If this call is successful |
41 // this object would keep an internal entry of the stream about the | 61 // this object would keep an internal entry of the stream about the |
42 // required properties, renderer process handle and IPC channel for sending | 62 // required properties, renderer process handle and IPC channel for sending |
(...skipping 24 matching lines...) Expand all Loading... |
67 // Get the volume of the stream specified, returns true if successful, false | 87 // Get the volume of the stream specified, returns true if successful, false |
68 // is stream doesn't exist or can't get volume. | 88 // is stream doesn't exist or can't get volume. |
69 bool GetVolume( | 89 bool GetVolume( |
70 int32 stream_id, double* left_channel, double* right_channel); | 90 int32 stream_id, double* left_channel, double* right_channel); |
71 | 91 |
72 // Notify packet has been prepared for stream specified by |stream_id|. The | 92 // Notify packet has been prepared for stream specified by |stream_id|. The |
73 // buffer associated with |stream_id| has been filled and is ready to be | 93 // buffer associated with |stream_id| has been filled and is ready to be |
74 // consumed. | 94 // consumed. |
75 void NotifyPacketReady(int32 stream_id); | 95 void NotifyPacketReady(int32 stream_id); |
76 | 96 |
77 // Destroy all audio output streams. | 97 // Called from UI thread from the owner of this object. |
78 void DestroyAllStreams(); | 98 void Destroy(); |
79 | 99 |
80 // Destroy the stream specified by |stream_id| and remove it from map. | 100 // Destroy the stream specified by |stream_id| and remove it from map. |
81 // *DO NOT* call this method other than from IPCAudioSource. | 101 // *DO NOT* call this method other than from IPCAudioSource. |
82 void DestroySource(int32 stream_id); | 102 void DestroySource(int32 stream_id); |
83 | 103 |
84 private: | 104 private: |
| 105 // Methods called on IO thread. |
| 106 // Called on IO thread when this object is created and initialized. |
| 107 void OnInitialized(); |
| 108 // Called on IO thread when this object needs to be destroyed and after |
| 109 // Destroy() is called from owner of this class in UI thread. |
| 110 void OnDestroyed(); |
| 111 |
| 112 // Destroy all audio output streams. |
| 113 void DestroyAllStreams(); |
| 114 |
85 // The container for AudioOutputStream and serves audio packet for it by IPC. | 115 // The container for AudioOutputStream and serves audio packet for it by IPC. |
86 class IPCAudioSource : public AudioOutputStream::AudioSourceCallback { | 116 class IPCAudioSource : public AudioOutputStream::AudioSourceCallback { |
87 public: | 117 public: |
88 IPCAudioSource(AudioRendererHost* host, // Host of this source. | 118 IPCAudioSource(AudioRendererHost* host, // Host of this source. |
89 int32 id, // ID of this source. | 119 int32 id, // ID of this source. |
90 AudioOutputStream* stream, // Stream associated. | 120 AudioOutputStream* stream, // Stream associated. |
91 IPC::Message::Sender* sender, // IPC sender to user. | 121 IPC::Message::Sender* sender, // IPC sender to user. |
92 base::ProcessHandle process, // Render process handle. | 122 base::ProcessHandle process, // Render process handle. |
93 size_t packet_size); // Size of shared memory | 123 size_t packet_size); // Size of shared memory |
94 // buffer for writing. | 124 // buffer for writing. |
(...skipping 22 matching lines...) Expand all Loading... |
117 }; | 147 }; |
118 | 148 |
119 // A map of id to audio sources. | 149 // A map of id to audio sources. |
120 IDMap<IPCAudioSource> sources_; | 150 IDMap<IPCAudioSource> sources_; |
121 | 151 |
122 // An internal id for streams. | 152 // An internal id for streams. |
123 int32 next_id_; | 153 int32 next_id_; |
124 | 154 |
125 // Only used for DCHECKs to make sure all methods calls are from the same | 155 // Only used for DCHECKs to make sure all methods calls are from the same |
126 // thread as this object is created. | 156 // thread as this object is created. |
127 MessageLoop* message_loop_; | 157 MessageLoop* io_loop_; |
128 | 158 |
129 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 159 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
130 }; | 160 }; |
131 | 161 |
132 #endif // CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ | 162 #endif // CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ |
OLD | NEW |