OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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. | 6 // lives inside the render process and provide access to audio hardware. |
7 // | 7 // |
8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI | 8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI |
9 // thread, but all other operations and method calls happen on IO thread, so we | 9 // thread, but all other operations and method calls happen on IO thread, so we |
10 // need to be extra careful about the lifetime of this object. AudioManager is a | 10 // need to be extra careful about the lifetime of this object. AudioManager is a |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 class AudioManager; | 69 class AudioManager; |
70 struct AudioParameters; | 70 struct AudioParameters; |
71 | 71 |
72 namespace content { | 72 namespace content { |
73 class ResourceContext; | 73 class ResourceContext; |
74 } // namespace content | 74 } // namespace content |
75 | 75 |
76 class AudioRendererHost : public BrowserMessageFilter, | 76 class AudioRendererHost : public BrowserMessageFilter, |
77 public media::AudioOutputController::EventHandler { | 77 public media::AudioOutputController::EventHandler { |
78 public: | 78 public: |
79 typedef std::pair<int32, int> AudioEntryId; | |
80 | |
81 struct AudioEntry { | 79 struct AudioEntry { |
82 AudioEntry(); | 80 AudioEntry(); |
83 ~AudioEntry(); | 81 ~AudioEntry(); |
84 | 82 |
85 // The AudioOutputController that manages the audio stream. | 83 // The AudioOutputController that manages the audio stream. |
86 scoped_refptr<media::AudioOutputController> controller; | 84 scoped_refptr<media::AudioOutputController> controller; |
87 | 85 |
88 // Render view ID that requested the audio stream. | 86 // The audio stream ID. |
89 int32 render_view_id; | |
90 | |
91 // The audio stream ID in the render view. | |
92 int stream_id; | 87 int stream_id; |
93 | 88 |
94 // Shared memory for transmission of the audio data. | 89 // Shared memory for transmission of the audio data. |
95 base::SharedMemory shared_memory; | 90 base::SharedMemory shared_memory; |
96 | 91 |
97 // The synchronous reader to be used by the controller. We have the | 92 // The synchronous reader to be used by the controller. We have the |
98 // ownership of the reader. | 93 // ownership of the reader. |
99 scoped_ptr<media::AudioOutputController::SyncReader> reader; | 94 scoped_ptr<media::AudioOutputController::SyncReader> reader; |
100 | 95 |
101 bool pending_buffer_request; | 96 bool pending_buffer_request; |
102 | 97 |
103 // Set to true after we called Close() for the controller. | 98 // Set to true after we called Close() for the controller. |
104 bool pending_close; | 99 bool pending_close; |
105 }; | 100 }; |
106 | 101 |
107 typedef std::map<AudioEntryId, AudioEntry*> AudioEntryMap; | 102 typedef std::map<int, AudioEntry*> AudioEntryMap; |
108 | 103 |
109 // Called from UI thread from the owner of this object. | 104 // Called from UI thread from the owner of this object. |
110 AudioRendererHost(const content::ResourceContext* resource_context); | 105 AudioRendererHost(const content::ResourceContext* resource_context); |
111 | 106 |
112 // BrowserMessageFilter implementation. | 107 // BrowserMessageFilter implementation. |
113 virtual void OnChannelClosing(); | 108 virtual void OnChannelClosing(); |
114 virtual void OnDestruct() const; | 109 virtual void OnDestruct() const; |
115 virtual bool OnMessageReceived(const IPC::Message& message, | 110 virtual bool OnMessageReceived(const IPC::Message& message, |
116 bool* message_was_ok); | 111 bool* message_was_ok); |
117 | 112 |
118 ///////////////////////////////////////////////////////////////////////////// | |
119 // AudioOutputController::EventHandler implementations. | 113 // AudioOutputController::EventHandler implementations. |
120 virtual void OnCreated(media::AudioOutputController* controller); | 114 virtual void OnCreated(media::AudioOutputController* controller); |
121 virtual void OnPlaying(media::AudioOutputController* controller); | 115 virtual void OnPlaying(media::AudioOutputController* controller); |
122 virtual void OnPaused(media::AudioOutputController* controller); | 116 virtual void OnPaused(media::AudioOutputController* controller); |
123 virtual void OnError(media::AudioOutputController* controller, | 117 virtual void OnError(media::AudioOutputController* controller, |
124 int error_code); | 118 int error_code); |
125 virtual void OnMoreData(media::AudioOutputController* controller, | 119 virtual void OnMoreData(media::AudioOutputController* controller, |
126 AudioBuffersState buffers_state); | 120 AudioBuffersState buffers_state); |
127 | 121 |
128 private: | 122 private: |
129 friend class AudioRendererHostTest; | 123 friend class AudioRendererHostTest; |
130 friend class BrowserThread; | 124 friend class BrowserThread; |
131 friend class DeleteTask<AudioRendererHost>; | 125 friend class DeleteTask<AudioRendererHost>; |
132 friend class MockAudioRendererHost; | 126 friend class MockAudioRendererHost; |
133 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 127 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
134 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 128 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
135 | 129 |
136 virtual ~AudioRendererHost(); | 130 virtual ~AudioRendererHost(); |
137 | 131 |
138 //////////////////////////////////////////////////////////////////////////// | 132 // Methods called on IO thread ---------------------------------------------- |
139 // Methods called on IO thread. | |
140 // Returns true if the message is an audio related message and should be | |
141 // handled by this class. | |
142 bool IsAudioRendererHostMessage(const IPC::Message& message); | |
143 | 133 |
144 // Audio related IPC message handlers. | 134 // Audio related IPC message handlers. |
145 // Creates an audio output stream with the specified format. If this call is | 135 // Creates an audio output stream with the specified format. If this call is |
146 // successful this object would keep an internal entry of the stream for the | 136 // successful this object would keep an internal entry of the stream for the |
147 // required properties. | 137 // required properties. |
148 void OnCreateStream(const IPC::Message& msg, int stream_id, | 138 void OnCreateStream(int stream_id, |
149 const AudioParameters& params, | 139 const AudioParameters& params, |
150 bool low_latency); | 140 bool low_latency); |
151 | 141 |
152 // Play the audio stream referenced by |stream_id|. | 142 // Play the audio stream referenced by |stream_id|. |
153 void OnPlayStream(const IPC::Message& msg, int stream_id); | 143 void OnPlayStream(int stream_id); |
154 | 144 |
155 // Pause the audio stream referenced by |stream_id|. | 145 // Pause the audio stream referenced by |stream_id|. |
156 void OnPauseStream(const IPC::Message& msg, int stream_id); | 146 void OnPauseStream(int stream_id); |
157 | 147 |
158 // Discard all audio data in stream referenced by |stream_id|. | 148 // Discard all audio data in stream referenced by |stream_id|. |
159 void OnFlushStream(const IPC::Message& msg, int stream_id); | 149 void OnFlushStream(int stream_id); |
160 | 150 |
161 // Close the audio stream referenced by |stream_id|. | 151 // Close the audio stream referenced by |stream_id|. |
162 void OnCloseStream(const IPC::Message& msg, int stream_id); | 152 void OnCloseStream(int stream_id); |
163 | 153 |
164 // Set the volume of the audio stream referenced by |stream_id|. | 154 // Set the volume of the audio stream referenced by |stream_id|. |
165 void OnSetVolume(const IPC::Message& msg, int stream_id, double volume); | 155 void OnSetVolume(int stream_id, double volume); |
166 | 156 |
167 // Get the volume of the audio stream referenced by |stream_id|. | 157 // Get the volume of the audio stream referenced by |stream_id|. |
168 void OnGetVolume(const IPC::Message& msg, int stream_id); | 158 void OnGetVolume(int stream_id); |
169 | 159 |
170 // Notify packet has been prepared for the audio stream. | 160 // Notify packet has been prepared for the audio stream. |
171 void OnNotifyPacketReady(const IPC::Message& msg, int stream_id, | 161 void OnNotifyPacketReady(int stream_id, uint32 packet_size); |
172 uint32 packet_size); | |
173 | 162 |
174 // Complete the process of creating an audio stream. This will set up the | 163 // Complete the process of creating an audio stream. This will set up the |
175 // shared memory or shared socket in low latency mode. | 164 // shared memory or shared socket in low latency mode. |
176 void DoCompleteCreation(media::AudioOutputController* controller); | 165 void DoCompleteCreation(media::AudioOutputController* controller); |
177 | 166 |
178 // Send a state change message to the renderer. | 167 // Send a state change message to the renderer. |
179 void DoSendPlayingMessage(media::AudioOutputController* controller); | 168 void DoSendPlayingMessage(media::AudioOutputController* controller); |
180 void DoSendPausedMessage(media::AudioOutputController* controller); | 169 void DoSendPausedMessage(media::AudioOutputController* controller); |
181 | 170 |
182 // Request more data from the renderer. This method is used only in normal | 171 // Request more data from the renderer. This method is used only in normal |
183 // latency mode. | 172 // latency mode. |
184 void DoRequestMoreData(media::AudioOutputController* controller, | 173 void DoRequestMoreData(media::AudioOutputController* controller, |
185 AudioBuffersState buffers_state); | 174 AudioBuffersState buffers_state); |
186 | 175 |
187 // Handle error coming from audio stream. | 176 // Handle error coming from audio stream. |
188 void DoHandleError(media::AudioOutputController* controller, int error_code); | 177 void DoHandleError(media::AudioOutputController* controller, int error_code); |
189 | 178 |
190 // Send an error message to the renderer. | 179 // Send an error message to the renderer. |
191 void SendErrorMessage(int32 render_view_id, int32 stream_id); | 180 void SendErrorMessage(int stream_id); |
192 | 181 |
193 // Delete all audio entry and all audio streams | 182 // Delete all audio entry and all audio streams |
194 void DeleteEntries(); | 183 void DeleteEntries(); |
195 | 184 |
196 // Closes the stream. The stream is then deleted in DeleteEntry() after it | 185 // Closes the stream. The stream is then deleted in DeleteEntry() after it |
197 // is closed. | 186 // is closed. |
198 void CloseAndDeleteStream(AudioEntry* entry); | 187 void CloseAndDeleteStream(AudioEntry* entry); |
199 | 188 |
200 // Called on the audio thread after the audio stream is closed. | 189 // Called on the audio thread after the audio stream is closed. |
201 void OnStreamClosed(AudioEntry* entry); | 190 void OnStreamClosed(AudioEntry* entry); |
202 | 191 |
203 // Delete an audio entry and close the related audio stream. | 192 // Delete an audio entry and close the related audio stream. |
204 void DeleteEntry(AudioEntry* entry); | 193 void DeleteEntry(AudioEntry* entry); |
205 | 194 |
206 // Delete audio entry and close the related audio stream due to an error, | 195 // Delete audio entry and close the related audio stream due to an error, |
207 // and error message is send to the renderer. | 196 // and error message is send to the renderer. |
208 void DeleteEntryOnError(AudioEntry* entry); | 197 void DeleteEntryOnError(AudioEntry* entry); |
209 | 198 |
210 // A helper method to look up a AudioEntry with a tuple of render view | 199 // A helper method to look up a AudioEntry identified by |stream_id|. |
211 // id and stream id. Returns NULL if not found. | 200 // Returns NULL if not found. |
212 AudioEntry* LookupById(int render_view_id, int stream_id); | 201 AudioEntry* LookupById(int stream_id); |
213 | 202 |
214 // Search for a AudioEntry having the reference to |controller|. | 203 // Search for a AudioEntry having the reference to |controller|. |
215 // This method is used to look up an AudioEntry after a controller | 204 // This method is used to look up an AudioEntry after a controller |
216 // event is received. | 205 // event is received. |
217 AudioEntry* LookupByController(media::AudioOutputController* controller); | 206 AudioEntry* LookupByController(media::AudioOutputController* controller); |
218 | 207 |
219 // A map of id to audio sources. | 208 // A map of stream IDs to audio sources. |
220 AudioEntryMap audio_entries_; | 209 AudioEntryMap audio_entries_; |
| 210 |
221 const content::ResourceContext* resource_context_; | 211 const content::ResourceContext* resource_context_; |
222 | 212 |
223 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 213 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
224 }; | 214 }; |
225 | 215 |
226 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 216 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
OLD | NEW |