| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 #include <map> | 45 #include <map> |
| 46 #include <memory> | 46 #include <memory> |
| 47 #include <string> | 47 #include <string> |
| 48 #include <utility> | 48 #include <utility> |
| 49 | 49 |
| 50 #include "base/atomic_ref_count.h" | 50 #include "base/atomic_ref_count.h" |
| 51 #include "base/gtest_prod_util.h" | 51 #include "base/gtest_prod_util.h" |
| 52 #include "base/macros.h" | 52 #include "base/macros.h" |
| 53 #include "base/memory/ref_counted.h" | 53 #include "base/memory/ref_counted.h" |
| 54 #include "base/memory/shared_memory.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" |
| 58 #include "content/public/browser/browser_message_filter.h" | 59 #include "content/public/browser/browser_message_filter.h" |
| 59 #include "content/public/browser/browser_thread.h" | 60 #include "content/public/browser/browser_thread.h" |
| 60 #include "content/public/browser/render_process_host.h" | 61 #include "content/public/browser/render_process_host.h" |
| 61 #include "content/public/browser/resource_context.h" | 62 #include "content/public/browser/resource_context.h" |
| 62 #include "media/audio/audio_io.h" | 63 #include "media/audio/audio_io.h" |
| 63 #include "media/audio/audio_logging.h" | 64 #include "media/audio/audio_logging.h" |
| 64 #include "media/audio/audio_output_controller.h" | 65 #include "media/audio/audio_output_controller.h" |
| 65 #include "media/audio/simple_sources.h" | 66 #include "media/audio/simple_sources.h" |
| 67 #include "media/mojo/interfaces/audio_output.mojom.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 AudioOutputImpl; |
| 79 class AudioOutputStreamImpl; |
| 76 class MediaInternals; | 80 class MediaInternals; |
| 77 class MediaStreamManager; | 81 class MediaStreamManager; |
| 78 class MediaStreamUIProxy; | 82 class MediaStreamUIProxy; |
| 79 class ResourceContext; | 83 class ResourceContext; |
| 80 | 84 |
| 81 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { | 85 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { |
| 82 public: | 86 public: |
| 83 // Called from UI thread from the owner of this object. | 87 // Called from UI thread from the owner of this object. |
| 84 AudioRendererHost(int render_process_id, | 88 AudioRendererHost(int render_process_id, |
| 85 media::AudioManager* audio_manager, | 89 media::AudioManager* audio_manager, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 100 | 104 |
| 101 // Returns true if any streams managed by this host are actively playing. Can | 105 // Returns true if any streams managed by this host are actively playing. Can |
| 102 // be called from any thread. | 106 // be called from any thread. |
| 103 bool HasActiveAudio(); | 107 bool HasActiveAudio(); |
| 104 | 108 |
| 105 // Returns true if any streams managed by the RenderFrame identified by | 109 // Returns true if any streams managed by the RenderFrame identified by |
| 106 // |render_frame_id| are actively playing. Can be called from any thread. | 110 // |render_frame_id| are actively playing. Can be called from any thread. |
| 107 bool RenderFrameHasActiveAudio(int render_frame_id) const; | 111 bool RenderFrameHasActiveAudio(int render_frame_id) const; |
| 108 | 112 |
| 109 private: | 113 private: |
| 114 friend class AudioEntry; |
| 115 friend class AudioOutputImpl; |
| 116 friend class AudioOutputStreamImpl; |
| 110 friend class AudioRendererHostTest; | 117 friend class AudioRendererHostTest; |
| 111 friend class BrowserThread; | 118 friend class BrowserThread; |
| 112 friend class base::DeleteHelper<AudioRendererHost>; | 119 friend class base::DeleteHelper<AudioRendererHost>; |
| 113 friend class MockAudioRendererHost; | 120 friend class MockAudioRendererHost; |
| 114 friend class TestAudioRendererHost; | 121 friend class TestAudioRendererHost; |
| 115 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 122 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
| 116 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 123 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
| 117 | 124 |
| 118 class AudioEntry; | 125 class AudioEntry; |
| 119 typedef std::map<int, AudioEntry*> AudioEntryMap; | 126 typedef std::map<int, AudioEntry*> AudioEntryMap; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 145 // parameters via the NotifyDeviceAuthorized message. | 152 // parameters via the NotifyDeviceAuthorized message. |
| 146 void OnRequestDeviceAuthorization(int stream_id, | 153 void OnRequestDeviceAuthorization(int stream_id, |
| 147 int render_frame_id, | 154 int render_frame_id, |
| 148 int session_id, | 155 int session_id, |
| 149 const std::string& device_id, | 156 const std::string& device_id, |
| 150 const url::Origin& security_origin); | 157 const url::Origin& security_origin); |
| 151 | 158 |
| 152 // Creates an audio output stream with the specified format. | 159 // Creates an audio output stream with the specified format. |
| 153 // Upon success/failure, the peer is notified via the NotifyStreamCreated | 160 // Upon success/failure, the peer is notified via the NotifyStreamCreated |
| 154 // message. | 161 // message. |
| 155 void OnCreateStream(int stream_id, | 162 void CreateStream( |
| 156 int render_frame_id, | 163 int stream_id, |
| 157 const media::AudioParameters& params); | 164 int render_frame_id, |
| 165 const media::AudioParameters& params, |
| 166 const media::mojom::AudioOutput::CreateStreamCallback& callback); |
| 158 | 167 |
| 159 // Play the audio stream referenced by |stream_id|. | 168 // Play the audio stream referenced by |stream_id|. |
| 160 void OnPlayStream(int stream_id); | 169 void OnPlayStream(int stream_id); |
| 161 | 170 |
| 162 // Pause the audio stream referenced by |stream_id|. | 171 // Pause the audio stream referenced by |stream_id|. |
| 163 void OnPauseStream(int stream_id); | 172 void OnPauseStream(int stream_id); |
| 164 | 173 |
| 165 // Close the audio stream referenced by |stream_id|. | 174 // Close the audio stream referenced by |stream_id|. |
| 166 void OnCloseStream(int stream_id); | 175 void OnCloseStream(int stream_id); |
| 167 | 176 |
| 168 // Set the volume of the audio stream referenced by |stream_id|. | 177 // Set the volume of the audio stream referenced by |stream_id|. |
| 169 void OnSetVolume(int stream_id, double volume); | 178 void OnSetVolume(int stream_id, double volume); |
| 170 | 179 |
| 171 // Helper methods. | 180 // Helper methods. |
| 172 | 181 |
| 173 // Proceed with device authorization after checking permissions. | 182 // Proceed with device authorization after checking permissions. |
| 174 void OnDeviceAuthorized(int stream_id, | 183 void OnDeviceAuthorized(int stream_id, |
| 175 const std::string& device_id, | 184 const std::string& device_id, |
| 176 const url::Origin& security_origin, | 185 const url::Origin& security_origin, |
| 177 bool have_access); | 186 bool have_access); |
| 178 | 187 |
| 179 // Proceed with device authorization after translating device ID. | 188 // Proceed with device authorization after translating device ID. |
| 180 void OnDeviceIDTranslated(int stream_id, | 189 void OnDeviceIDTranslated(int stream_id, |
| 181 bool device_found, | 190 bool device_found, |
| 182 const AudioOutputDeviceInfo& device_info); | 191 const AudioOutputDeviceInfo& device_info); |
| 183 | 192 |
| 184 // Start the actual creation of an audio stream, after the device | 193 // Start the actual creation of an audio stream, after the device |
| 185 // authorization process is complete. | 194 // authorization process is complete. |
| 186 void DoCreateStream(int stream_id, | 195 void DoCreateStream( |
| 187 int render_frame_id, | 196 int stream_id, |
| 188 const media::AudioParameters& params, | 197 int render_frame_id, |
| 189 const std::string& device_unique_id); | 198 const media::AudioParameters& params, |
| 199 const std::string& device_unique_id, |
| 200 const media::mojom::AudioOutput::CreateStreamCallback& |
| 201 create_stream_callback); |
| 190 | 202 |
| 191 // Complete the process of creating an audio stream. This will set up the | 203 // 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 | 204 // shared memory or shared socket in low latency mode and send the |
| 193 // NotifyStreamCreated message to the peer. | 205 // NotifyStreamCreated message to the peer. |
| 194 void DoCompleteCreation(int stream_id); | 206 void DoCompleteCreation( |
| 207 int stream_id, |
| 208 const media::mojom::AudioOutput::CreateStreamCallback& |
| 209 create_stream_callback); |
| 195 | 210 |
| 196 // Send playing/paused status to the renderer. | 211 // Send playing/paused status to the renderer. |
| 197 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); | 212 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); |
| 198 | 213 |
| 199 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 214 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| 200 | 215 |
| 201 // Send an error message to the renderer. | 216 // Send an error message to the renderer. |
| 202 void SendErrorMessage(int stream_id); | 217 void SendErrorMessage(int stream_id); |
| 203 | 218 |
| 204 // Delete an audio entry, notifying observers first. This is called by | 219 // Delete an audio entry, notifying observers first. This is called by |
| 205 // AudioOutputController after it has closed. | 220 // AudioOutputController after it has closed. |
| 206 void DeleteEntry(std::unique_ptr<AudioEntry> entry); | 221 void DeleteEntry(std::unique_ptr<AudioEntry> entry); |
| 207 | 222 |
| 208 // Send an error message to the renderer, then close the stream. | 223 // Send an error message to the renderer, then close the stream. |
| 209 void ReportErrorAndClose(int stream_id); | 224 void ReportErrorAndClose(int stream_id); |
| 210 | 225 |
| 226 // Send an error message to the renderer, then close the stream. |
| 227 void ReportErrorAndCloseStream( |
| 228 int stream_id, |
| 229 const media::mojom::AudioOutput::CreateStreamCallback& callback); |
| 230 |
| 211 // A helper method to look up a AudioEntry identified by |stream_id|. | 231 // A helper method to look up a AudioEntry identified by |stream_id|. |
| 212 // Returns NULL if not found. | 232 // Returns NULL if not found. |
| 213 AudioEntry* LookupById(int stream_id); | 233 AudioEntry* LookupById(int stream_id); |
| 214 | 234 |
| 215 // A helper method to update the number of playing streams and alert the | 235 // A helper method to update the number of playing streams and alert the |
| 216 // ResourceScheduler when the renderer starts or stops playing an audiostream. | 236 // ResourceScheduler when the renderer starts or stops playing an audiostream. |
| 217 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); | 237 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); |
| 218 | 238 |
| 219 // Check if the renderer process has access to the requested output device. | 239 // Check if the renderer process has access to the requested output device. |
| 220 void CheckOutputDeviceAccess(int render_frame_id, | 240 void CheckOutputDeviceAccess(int render_frame_id, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 236 // Helper method to check if the authorization procedure for stream | 256 // Helper method to check if the authorization procedure for stream |
| 237 // |stream_id| has started. | 257 // |stream_id| has started. |
| 238 bool IsAuthorizationStarted(int stream_id); | 258 bool IsAuthorizationStarted(int stream_id); |
| 239 | 259 |
| 240 // ID of the RenderProcessHost that owns this instance. | 260 // ID of the RenderProcessHost that owns this instance. |
| 241 const int render_process_id_; | 261 const int render_process_id_; |
| 242 | 262 |
| 243 media::AudioManager* const audio_manager_; | 263 media::AudioManager* const audio_manager_; |
| 244 AudioMirroringManager* const mirroring_manager_; | 264 AudioMirroringManager* const mirroring_manager_; |
| 245 std::unique_ptr<media::AudioLog> audio_log_; | 265 std::unique_ptr<media::AudioLog> audio_log_; |
| 266 AudioOutputImpl* audio_output_impl_; |
| 246 | 267 |
| 247 // Used to access to AudioInputDeviceManager. | 268 // Used to access to AudioInputDeviceManager. |
| 248 MediaStreamManager* media_stream_manager_; | 269 MediaStreamManager* media_stream_manager_; |
| 249 | 270 |
| 250 // A map of stream IDs to audio sources. | 271 // A map of stream IDs to audio sources. |
| 251 AudioEntryMap audio_entries_; | 272 AudioEntryMap audio_entries_; |
| 252 | 273 |
| 253 // The number of streams in the playing state. Atomic read safe from any | 274 // The number of streams in the playing state. Atomic read safe from any |
| 254 // thread, but should only be updated from the IO thread. | 275 // thread, but should only be updated from the IO thread. |
| 255 base::AtomicRefCount num_playing_streams_; | 276 base::AtomicRefCount num_playing_streams_; |
| 256 | 277 |
| 257 // Salt required to translate renderer device IDs to raw device unique IDs | 278 // Salt required to translate renderer device IDs to raw device unique IDs |
| 258 ResourceContext::SaltCallback salt_callback_; | 279 ResourceContext::SaltCallback salt_callback_; |
| 259 | 280 |
| 260 // Map of device authorizations for streams that are not yet created | 281 // 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 | 282 // 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. | 283 // is a bool that is true if the authorization process completes successfully. |
| 263 // The second element contains the unique ID of the authorized device. | 284 // The second element contains the unique ID of the authorized device. |
| 264 std::map<int, std::pair<bool, std::string>> authorizations_; | 285 std::map<int, std::pair<bool, std::string>> authorizations_; |
| 265 | 286 |
| 266 // The maximum number of simultaneous streams during the lifetime of this | 287 // The maximum number of simultaneous streams during the lifetime of this |
| 267 // host. Reported as UMA stat at shutdown. | 288 // host. Reported as UMA stat at shutdown. |
| 268 size_t max_simultaneous_streams_; | 289 size_t max_simultaneous_streams_; |
| 269 | 290 |
| 291 media::mojom::AudioOutputStreamPtr stream_ptr_; |
| 292 |
| 270 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 293 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 271 }; | 294 }; |
| 272 | 295 |
| 273 } // namespace content | 296 } // namespace content |
| 274 | 297 |
| 275 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 298 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |