| 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 // 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 RenderProcessHostImpl, and instantiated on UI | 8 // This class is owned by RenderProcessHostImpl, 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 | 42 |
| 43 #include <map> | 43 #include <map> |
| 44 #include <string> | 44 #include <string> |
| 45 #include <utility> | 45 #include <utility> |
| 46 | 46 |
| 47 #include <stddef.h> | 47 #include <stddef.h> |
| 48 | 48 |
| 49 #include "base/atomic_ref_count.h" | 49 #include "base/atomic_ref_count.h" |
| 50 #include "base/gtest_prod_util.h" | 50 #include "base/gtest_prod_util.h" |
| 51 #include "base/macros.h" | 51 #include "base/macros.h" |
| 52 #include "base/memory/shared_memory.h" |
| 52 #include "base/memory/ref_counted.h" | 53 #include "base/memory/ref_counted.h" |
| 53 #include "base/memory/scoped_ptr.h" | 54 #include "base/memory/scoped_ptr.h" |
| 54 #include "base/process/process.h" | 55 #include "base/process/process.h" |
| 55 #include "base/sequenced_task_runner_helpers.h" | 56 #include "base/sequenced_task_runner_helpers.h" |
| 56 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h" | 57 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h" |
| 57 #include "content/common/content_export.h" | 58 #include "content/common/content_export.h" |
| 59 #include "content/common/media/audio_output.mojom.h" |
| 58 #include "content/public/browser/browser_message_filter.h" | 60 #include "content/public/browser/browser_message_filter.h" |
| 59 #include "content/public/browser/browser_thread.h" | 61 #include "content/public/browser/browser_thread.h" |
| 60 #include "content/public/browser/render_process_host.h" | 62 #include "content/public/browser/render_process_host.h" |
| 61 #include "content/public/browser/resource_context.h" | 63 #include "content/public/browser/resource_context.h" |
| 62 #include "media/audio/audio_io.h" | 64 #include "media/audio/audio_io.h" |
| 63 #include "media/audio/audio_logging.h" | 65 #include "media/audio/audio_logging.h" |
| 64 #include "media/audio/audio_output_controller.h" | 66 #include "media/audio/audio_output_controller.h" |
| 65 #include "media/audio/simple_sources.h" | 67 #include "media/audio/simple_sources.h" |
| 66 #include "url/origin.h" | 68 #include "url/origin.h" |
| 67 | 69 |
| 68 namespace media { | 70 namespace media { |
| 69 class AudioManager; | 71 class AudioManager; |
| 70 class AudioParameters; | 72 class AudioParameters; |
| 71 } | 73 } |
| 72 | 74 |
| 73 namespace content { | 75 namespace content { |
| 74 | 76 |
| 75 class AudioMirroringManager; | 77 class AudioMirroringManager; |
| 78 class AudioRendererHost; |
| 76 class MediaInternals; | 79 class MediaInternals; |
| 77 class MediaStreamManager; | 80 class MediaStreamManager; |
| 78 class MediaStreamUIProxy; | 81 class MediaStreamUIProxy; |
| 79 class ResourceContext; | 82 class ResourceContext; |
| 80 | 83 |
| 84 class CONTENT_EXPORT AudioEntry |
| 85 : public media::AudioOutputController::EventHandler { |
| 86 public: |
| 87 AudioEntry(AudioRendererHost* host, |
| 88 int stream_id, |
| 89 int render_frame_id, |
| 90 const media::AudioParameters& params, |
| 91 const std::string& output_device_id, |
| 92 scoped_ptr<base::SharedMemory> shared_memory, |
| 93 scoped_ptr<media::AudioOutputController::SyncReader> reader); |
| 94 |
| 95 AudioEntry(AudioRendererHost* host, |
| 96 int stream_id, |
| 97 int render_frame_id, |
| 98 const media::AudioParameters& params, |
| 99 const std::string& output_device_id, |
| 100 scoped_ptr<base::SharedMemory> shared_memory, |
| 101 scoped_ptr<media::AudioOutputController::SyncReader> reader, |
| 102 mojom::AudioOutputStreamPtr stream, |
| 103 const mojom::AudioOutput::CreateStreamCallback& callback); |
| 104 |
| 105 ~AudioEntry() override; |
| 106 |
| 107 int stream_id() const { return stream_id_; } |
| 108 |
| 109 int render_frame_id() const { return render_frame_id_; } |
| 110 |
| 111 media::AudioOutputController* controller() const { return controller_.get(); } |
| 112 |
| 113 base::SharedMemory* shared_memory() { return shared_memory_.get(); } |
| 114 |
| 115 media::AudioOutputController::SyncReader* reader() const { |
| 116 return reader_.get(); |
| 117 } |
| 118 |
| 119 bool playing() const { return playing_; } |
| 120 void set_playing(bool playing) { playing_ = playing; } |
| 121 |
| 122 private: |
| 123 // media::AudioOutputController::EventHandler implementation. |
| 124 void OnCreated( |
| 125 mojom::AudioOutputStreamPtr stream, |
| 126 const mojom::AudioOutput::CreateStreamCallback& callback) override; |
| 127 void OnCreated() override; |
| 128 void OnPlaying() override; |
| 129 void OnPaused() override; |
| 130 void OnError() override; |
| 131 |
| 132 AudioRendererHost* const host_; |
| 133 const int stream_id_; |
| 134 |
| 135 // The routing ID of the source RenderFrame. |
| 136 const int render_frame_id_; |
| 137 |
| 138 // Shared memory for transmission of the audio data. Used by |reader_|. |
| 139 const scoped_ptr<base::SharedMemory> shared_memory_; |
| 140 |
| 141 // The synchronous reader to be used by |controller_|. |
| 142 const scoped_ptr<media::AudioOutputController::SyncReader> reader_; |
| 143 |
| 144 // The AudioOutputController that manages the audio stream. |
| 145 const scoped_refptr<media::AudioOutputController> controller_; |
| 146 |
| 147 bool playing_; |
| 148 }; |
| 149 |
| 81 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { | 150 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { |
| 82 public: | 151 public: |
| 83 // Called from UI thread from the owner of this object. | 152 // Called from UI thread from the owner of this object. |
| 84 AudioRendererHost(int render_process_id, | 153 AudioRendererHost(int render_process_id, |
| 85 media::AudioManager* audio_manager, | 154 media::AudioManager* audio_manager, |
| 86 AudioMirroringManager* mirroring_manager, | 155 AudioMirroringManager* mirroring_manager, |
| 87 MediaInternals* media_internals, | 156 MediaInternals* media_internals, |
| 88 MediaStreamManager* media_stream_manager, | 157 MediaStreamManager* media_stream_manager, |
| 89 const ResourceContext::SaltCallback& salt_callback); | 158 const ResourceContext::SaltCallback& salt_callback); |
| 90 | 159 |
| 91 // Calls |callback| with the list of AudioOutputControllers for this object. | 160 // Calls |callback| with the list of AudioOutputControllers for this object. |
| 92 void GetOutputControllers( | 161 void GetOutputControllers( |
| 93 const RenderProcessHost::GetAudioOutputControllersCallback& | 162 const RenderProcessHost::GetAudioOutputControllersCallback& |
| 94 callback) const; | 163 callback) const; |
| 95 | 164 |
| 96 // BrowserMessageFilter implementation. | 165 // BrowserMessageFilter implementation. |
| 97 void OnChannelClosing() override; | 166 void OnChannelClosing() override; |
| 98 void OnDestruct() const override; | 167 void OnDestruct() const override; |
| 99 bool OnMessageReceived(const IPC::Message& message) override; | 168 bool OnMessageReceived(const IPC::Message& message) override; |
| 100 | 169 |
| 101 // Returns true if any streams managed by this host are actively playing. Can | 170 // Returns true if any streams managed by this host are actively playing. Can |
| 102 // be called from any thread. | 171 // be called from any thread. |
| 103 bool HasActiveAudio(); | 172 bool HasActiveAudio(); |
| 104 | 173 |
| 105 // Returns true if any streams managed by the RenderFrame identified by | 174 // Returns true if any streams managed by the RenderFrame identified by |
| 106 // |render_frame_id| are actively playing. Can be called from any thread. | 175 // |render_frame_id| are actively playing. Can be called from any thread. |
| 107 bool RenderFrameHasActiveAudio(int render_frame_id) const; | 176 bool RenderFrameHasActiveAudio(int render_frame_id) const; |
| 108 | 177 |
| 178 // Creates an audio output stream with the specified format. |
| 179 // Upon success/failure, the peer is notified via the NotifyStreamCreated |
| 180 // message. |
| 181 void OnCreateStream(int stream_id, |
| 182 int render_frame_id, |
| 183 const media::AudioParameters& params); |
| 184 |
| 185 // Creates an audio output stream with the specified format. |
| 186 // Upon success/failure, the peer is notified via the NotifyStreamCreated |
| 187 // message. |
| 188 void OnCreateStreamMojo( |
| 189 int stream_id, |
| 190 int render_frame_id, |
| 191 const media::AudioParameters& params, |
| 192 mojom::AudioOutputStreamPtr stream, |
| 193 const mojom::AudioOutput::CreateStreamCallback& callback); |
| 194 |
| 195 // Close the audio stream referenced by |stream_id|. |
| 196 void OnCloseStream(int stream_id); |
| 197 |
| 198 // Play the audio stream referenced by |stream_id|. |
| 199 void OnPlayEntry(AudioEntry* entry); |
| 200 |
| 109 private: | 201 private: |
| 202 friend class AudioEntry; |
| 110 friend class AudioRendererHostTest; | 203 friend class AudioRendererHostTest; |
| 111 friend class BrowserThread; | 204 friend class BrowserThread; |
| 112 friend class base::DeleteHelper<AudioRendererHost>; | 205 friend class base::DeleteHelper<AudioRendererHost>; |
| 113 friend class MockAudioRendererHost; | 206 friend class MockAudioRendererHost; |
| 114 friend class TestAudioRendererHost; | 207 friend class TestAudioRendererHost; |
| 115 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 208 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
| 116 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 209 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
| 117 | 210 |
| 118 class AudioEntry; | |
| 119 typedef std::map<int, AudioEntry*> AudioEntryMap; | 211 typedef std::map<int, AudioEntry*> AudioEntryMap; |
| 120 | 212 |
| 121 // Internal callback type for access requests to output devices. | 213 // Internal callback type for access requests to output devices. |
| 122 // |have_access| is true only if there is permission to access the device. | 214 // |have_access| is true only if there is permission to access the device. |
| 123 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; | 215 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; |
| 124 | 216 |
| 125 // Internal callback type for information requests about an output device. | 217 // Internal callback type for information requests about an output device. |
| 126 // |success| indicates the operation was successful. If true, |device_info| | 218 // |success| indicates the operation was successful. If true, |device_info| |
| 127 // contains data about the device. | 219 // contains data about the device. |
| 128 typedef base::Callback<void(bool success, | 220 typedef base::Callback<void(bool success, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 142 // |session_id| will be ignored and the given |device_id| and | 234 // |session_id| will be ignored and the given |device_id| and |
| 143 // |security_origin| will be used to select the output device. | 235 // |security_origin| will be used to select the output device. |
| 144 // Upon completion of the process, the peer is notified with the device output | 236 // Upon completion of the process, the peer is notified with the device output |
| 145 // parameters via the NotifyDeviceAuthorized message. | 237 // parameters via the NotifyDeviceAuthorized message. |
| 146 void OnRequestDeviceAuthorization(int stream_id, | 238 void OnRequestDeviceAuthorization(int stream_id, |
| 147 int render_frame_id, | 239 int render_frame_id, |
| 148 int session_id, | 240 int session_id, |
| 149 const std::string& device_id, | 241 const std::string& device_id, |
| 150 const url::Origin& gurl_security_origin); | 242 const url::Origin& gurl_security_origin); |
| 151 | 243 |
| 152 // Creates an audio output stream with the specified format. | |
| 153 // Upon success/failure, the peer is notified via the NotifyStreamCreated | |
| 154 // message. | |
| 155 void OnCreateStream(int stream_id, | |
| 156 int render_frame_id, | |
| 157 const media::AudioParameters& params); | |
| 158 | |
| 159 // Play the audio stream referenced by |stream_id|. | 244 // Play the audio stream referenced by |stream_id|. |
| 160 void OnPlayStream(int stream_id); | 245 void OnPlayStream(int stream_id); |
| 161 | 246 |
| 162 // Pause the audio stream referenced by |stream_id|. | 247 // Pause the audio stream referenced by |stream_id|. |
| 163 void OnPauseStream(int stream_id); | 248 void OnPauseStream(int stream_id); |
| 164 | 249 |
| 165 // Close the audio stream referenced by |stream_id|. | |
| 166 void OnCloseStream(int stream_id); | |
| 167 | |
| 168 // Set the volume of the audio stream referenced by |stream_id|. | 250 // Set the volume of the audio stream referenced by |stream_id|. |
| 169 void OnSetVolume(int stream_id, double volume); | 251 void OnSetVolume(int stream_id, double volume); |
| 170 | 252 |
| 171 // Helper methods. | 253 // Helper methods. |
| 172 | 254 |
| 173 // Proceed with device authorization after checking permissions. | 255 // Proceed with device authorization after checking permissions. |
| 174 void OnDeviceAuthorized(int stream_id, | 256 void OnDeviceAuthorized(int stream_id, |
| 175 const std::string& device_id, | 257 const std::string& device_id, |
| 176 const GURL& security_origin, | 258 const GURL& security_origin, |
| 177 bool have_access); | 259 bool have_access); |
| 178 | 260 |
| 179 // Proceed with device authorization after translating device ID. | 261 // Proceed with device authorization after translating device ID. |
| 180 void OnDeviceIDTranslated(int stream_id, | 262 void OnDeviceIDTranslated(int stream_id, |
| 181 bool device_found, | 263 bool device_found, |
| 182 const AudioOutputDeviceInfo& device_info); | 264 const AudioOutputDeviceInfo& device_info); |
| 183 | 265 |
| 184 // Start the actual creation of an audio stream, after the device | 266 // Start the actual creation of an audio stream, after the device |
| 185 // authorization process is complete. | 267 // authorization process is complete. |
| 186 void DoCreateStream(int stream_id, | 268 void DoCreateStream(int stream_id, |
| 187 int render_frame_id, | 269 int render_frame_id, |
| 188 const media::AudioParameters& params, | 270 const media::AudioParameters& params, |
| 271 const std::string& device_unique_id, |
| 272 mojom::AudioOutputStreamPtr stream, |
| 273 const mojom::AudioOutput::CreateStreamCallback& callback); |
| 274 |
| 275 // Start the actual creation of an audio stream, after the device |
| 276 // authorization process is complete. |
| 277 void DoCreateStream(int stream_id, |
| 278 int render_frame_id, |
| 279 const media::AudioParameters& params, |
| 189 const std::string& device_unique_id); | 280 const std::string& device_unique_id); |
| 190 | 281 |
| 191 // Complete the process of creating an audio stream. This will set up the | 282 // Complete the process of creating an audio stream. This will set up the |
| 192 // shared memory or shared socket in low latency mode and send the | 283 // shared memory or shared socket in low latency mode and send the |
| 193 // NotifyStreamCreated message to the peer. | 284 // NotifyStreamCreated message to the peer. |
| 194 void DoCompleteCreation(int stream_id); | 285 void DoCompleteCreation(int stream_id); |
| 195 | 286 |
| 287 // Complete the process of creating an audio stream. This will set up the |
| 288 // shared memory or shared socket in low latency mode and send the |
| 289 // NotifyStreamCreated message to the peer. |
| 290 void DoCompleteCreationMojo( |
| 291 int stream_id, |
| 292 const mojom::AudioOutput::CreateStreamCallback& callback); |
| 196 // Send playing/paused status to the renderer. | 293 // Send playing/paused status to the renderer. |
| 197 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); | 294 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); |
| 198 | 295 |
| 199 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 296 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| 200 | 297 |
| 201 // Send an error message to the renderer. | 298 // Send an error message to the renderer. |
| 202 void SendErrorMessage(int stream_id); | 299 void SendErrorMessage(int stream_id); |
| 203 | 300 |
| 204 // Delete an audio entry, notifying observers first. This is called by | 301 // Delete an audio entry, notifying observers first. This is called by |
| 205 // AudioOutputController after it has closed. | 302 // AudioOutputController after it has closed. |
| 206 void DeleteEntry(scoped_ptr<AudioEntry> entry); | 303 void DeleteEntry(scoped_ptr<AudioEntry> entry); |
| 207 | 304 |
| 208 // Send an error message to the renderer, then close the stream. | 305 // Send an error message to the renderer, then close the stream. |
| 209 void ReportErrorAndClose(int stream_id); | 306 void ReportErrorAndClose(int stream_id); |
| 210 | 307 |
| 308 // Send an error message to the renderer, then close the stream. |
| 309 void ReportErrorAndCloseMojo( |
| 310 int stream_id, |
| 311 const mojom::AudioOutput::CreateStreamCallback& callback); |
| 312 |
| 211 // A helper method to look up a AudioEntry identified by |stream_id|. | 313 // A helper method to look up a AudioEntry identified by |stream_id|. |
| 212 // Returns NULL if not found. | 314 // Returns NULL if not found. |
| 213 AudioEntry* LookupById(int stream_id); | 315 AudioEntry* LookupById(int stream_id); |
| 214 | 316 |
| 215 // A helper method to update the number of playing streams and alert the | 317 // A helper method to update the number of playing streams and alert the |
| 216 // ResourceScheduler when the renderer starts or stops playing an audiostream. | 318 // ResourceScheduler when the renderer starts or stops playing an audiostream. |
| 217 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); | 319 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); |
| 218 | 320 |
| 219 // Check if the renderer process has access to the requested output device. | 321 // Check if the renderer process has access to the requested output device. |
| 220 void CheckOutputDeviceAccess(int render_frame_id, | 322 void CheckOutputDeviceAccess(int render_frame_id, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 // Map of device authorizations for streams that are not yet created | 362 // Map of device authorizations for streams that are not yet created |
| 261 // The key is the stream ID, and the value is a pair. The pair's first element | 363 // The key is the stream ID, and the value is a pair. The pair's first element |
| 262 // is a bool that is true if the authorization process completes successfully. | 364 // is a bool that is true if the authorization process completes successfully. |
| 263 // The second element contains the unique ID of the authorized device. | 365 // The second element contains the unique ID of the authorized device. |
| 264 std::map<int, std::pair<bool, std::string>> authorizations_; | 366 std::map<int, std::pair<bool, std::string>> authorizations_; |
| 265 | 367 |
| 266 // The maximum number of simultaneous streams during the lifetime of this | 368 // The maximum number of simultaneous streams during the lifetime of this |
| 267 // host. Reported as UMA stat at shutdown. | 369 // host. Reported as UMA stat at shutdown. |
| 268 size_t max_simultaneous_streams_; | 370 size_t max_simultaneous_streams_; |
| 269 | 371 |
| 372 mojom::AudioOutputStreamPtr stream_ptr_; |
| 373 |
| 270 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 374 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 271 }; | 375 }; |
| 272 | 376 |
| 273 } // namespace content | 377 } // namespace content |
| 274 | 378 |
| 275 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 379 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |