Chromium Code Reviews| 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; | |
| 80 class AudioRendererHost; | |
| 76 class MediaInternals; | 81 class MediaInternals; |
| 77 class MediaStreamManager; | 82 class MediaStreamManager; |
| 78 class MediaStreamUIProxy; | 83 class MediaStreamUIProxy; |
| 79 class ResourceContext; | 84 class ResourceContext; |
| 80 | 85 |
| 81 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { | 86 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter { |
| 82 public: | 87 public: |
| 88 class CONTENT_EXPORT AudioEntry | |
|
Henrik Grunell
2016/04/19 15:36:09
Why does it have to be moved to the header?
rchtara
2016/04/21 09:10:18
Because, I'm using the AudioEntry in the audio_out
Henrik Grunell
2016/04/22 08:23:03
Acknowledged.
| |
| 89 : public media::AudioOutputController::EventHandler { | |
| 90 public: | |
| 91 AudioEntry(AudioRendererHost* host, | |
| 92 int stream_id, | |
| 93 int render_frame_id, | |
| 94 const media::AudioParameters& params, | |
| 95 const std::string& output_device_id, | |
| 96 std::unique_ptr<base::SharedMemory> shared_memory, | |
| 97 std::unique_ptr<media::AudioOutputController::SyncReader> reader, | |
| 98 const mojom::AudioOutput::CreateStreamCallback& callback); | |
| 99 | |
| 100 ~AudioEntry() override; | |
| 101 | |
| 102 int stream_id() const { return stream_id_; } | |
| 103 | |
| 104 int render_frame_id() const { return render_frame_id_; } | |
| 105 | |
| 106 media::AudioOutputController* controller() const { | |
| 107 return controller_.get(); | |
| 108 } | |
| 109 | |
| 110 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | |
| 111 | |
| 112 media::AudioOutputController::SyncReader* reader() const { | |
| 113 return reader_.get(); | |
| 114 } | |
| 115 | |
| 116 bool playing() const { return playing_; } | |
| 117 void set_playing(bool playing) { playing_ = playing; } | |
| 118 | |
| 119 void set_stream_ptr(AudioOutputStreamImpl* stream_ptr) { | |
| 120 stream_ptr_ = stream_ptr; | |
| 121 } | |
| 122 void OnError() override; | |
| 123 | |
| 124 private: | |
| 125 friend class AudioRendererHost; | |
| 126 // media::AudioOutputController::EventHandler implementation. | |
| 127 void OnCreated( | |
| 128 const mojom::AudioOutput::CreateStreamCallback& callback) override; | |
| 129 void OnPlaying() override; | |
| 130 void OnPaused() 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 std::unique_ptr<base::SharedMemory> shared_memory_; | |
| 140 | |
| 141 // The synchronous reader to be used by |controller_|. | |
| 142 const std::unique_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 AudioOutputStreamImpl* stream_ptr_; | |
| 150 }; | |
| 151 | |
| 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 |
| 109 private: | 178 private: |
| 179 friend class AudioEntry; | |
| 180 friend class AudioOutputImpl; | |
| 181 friend class AudioOutputStreamImpl; | |
| 110 friend class AudioRendererHostTest; | 182 friend class AudioRendererHostTest; |
| 111 friend class BrowserThread; | 183 friend class BrowserThread; |
| 112 friend class base::DeleteHelper<AudioRendererHost>; | 184 friend class base::DeleteHelper<AudioRendererHost>; |
| 113 friend class MockAudioRendererHost; | 185 friend class MockAudioRendererHost; |
| 114 friend class TestAudioRendererHost; | 186 friend class TestAudioRendererHost; |
| 115 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 187 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
| 116 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 188 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
| 117 | 189 |
| 118 class AudioEntry; | |
| 119 typedef std::map<int, AudioEntry*> AudioEntryMap; | 190 typedef std::map<int, AudioEntry*> AudioEntryMap; |
| 120 | 191 |
| 121 // Internal callback type for access requests to output devices. | 192 // Internal callback type for access requests to output devices. |
| 122 // |have_access| is true only if there is permission to access the device. | 193 // |have_access| is true only if there is permission to access the device. |
| 123 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; | 194 typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB; |
| 124 | 195 |
| 125 // Internal callback type for information requests about an output device. | 196 // Internal callback type for information requests about an output device. |
| 126 // |success| indicates the operation was successful. If true, |device_info| | 197 // |success| indicates the operation was successful. If true, |device_info| |
| 127 // contains data about the device. | 198 // contains data about the device. |
| 128 typedef base::Callback<void(bool success, | 199 typedef base::Callback<void(bool success, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 147 int render_frame_id, | 218 int render_frame_id, |
| 148 int session_id, | 219 int session_id, |
| 149 const std::string& device_id, | 220 const std::string& device_id, |
| 150 const url::Origin& gurl_security_origin); | 221 const url::Origin& gurl_security_origin); |
| 151 | 222 |
| 152 // Creates an audio output stream with the specified format. | 223 // Creates an audio output stream with the specified format. |
| 153 // Upon success/failure, the peer is notified via the NotifyStreamCreated | 224 // Upon success/failure, the peer is notified via the NotifyStreamCreated |
| 154 // message. | 225 // message. |
| 155 void OnCreateStream(int stream_id, | 226 void OnCreateStream(int stream_id, |
| 156 int render_frame_id, | 227 int render_frame_id, |
| 157 const media::AudioParameters& params); | 228 const media::AudioParameters& params, |
| 229 const mojom::AudioOutput::CreateStreamCallback& callback); | |
| 158 | 230 |
| 159 // Play the audio stream referenced by |stream_id|. | 231 // Play the audio stream referenced by |stream_id|. |
| 160 void OnPlayStream(int stream_id); | 232 void OnPlayStream(int stream_id); |
| 161 | 233 |
| 162 // Pause the audio stream referenced by |stream_id|. | 234 // Pause the audio stream referenced by |stream_id|. |
| 163 void OnPauseStream(int stream_id); | 235 void OnPauseStream(int stream_id); |
| 164 | 236 |
| 165 // Close the audio stream referenced by |stream_id|. | 237 // Close the audio stream referenced by |stream_id|. |
| 166 void OnCloseStream(int stream_id); | 238 void OnCloseStream(int stream_id); |
| 167 | 239 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 179 // Proceed with device authorization after translating device ID. | 251 // Proceed with device authorization after translating device ID. |
| 180 void OnDeviceIDTranslated(int stream_id, | 252 void OnDeviceIDTranslated(int stream_id, |
| 181 bool device_found, | 253 bool device_found, |
| 182 const AudioOutputDeviceInfo& device_info); | 254 const AudioOutputDeviceInfo& device_info); |
| 183 | 255 |
| 184 // Start the actual creation of an audio stream, after the device | 256 // Start the actual creation of an audio stream, after the device |
| 185 // authorization process is complete. | 257 // authorization process is complete. |
| 186 void DoCreateStream(int stream_id, | 258 void DoCreateStream(int stream_id, |
| 187 int render_frame_id, | 259 int render_frame_id, |
| 188 const media::AudioParameters& params, | 260 const media::AudioParameters& params, |
| 189 const std::string& device_unique_id); | 261 const std::string& device_unique_id, |
| 262 const mojom::AudioOutput::CreateStreamCallback& callback); | |
| 190 | 263 |
| 191 // Complete the process of creating an audio stream. This will set up the | 264 // 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 | 265 // shared memory or shared socket in low latency mode and send the |
| 193 // NotifyStreamCreated message to the peer. | 266 // NotifyStreamCreated message to the peer. |
| 194 void DoCompleteCreation(int stream_id); | 267 void DoCompleteCreation( |
| 268 int stream_id, | |
| 269 const mojom::AudioOutput::CreateStreamCallback& callback); | |
| 195 | 270 |
| 196 // Send playing/paused status to the renderer. | 271 // Send playing/paused status to the renderer. |
| 197 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); | 272 void DoNotifyStreamStateChanged(int stream_id, bool is_playing); |
| 198 | 273 |
| 199 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; | 274 RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const; |
| 200 | 275 |
| 201 // Send an error message to the renderer. | 276 // Send an error message to the renderer. |
| 202 void SendErrorMessage(int stream_id); | 277 void SendErrorMessage(int stream_id); |
| 203 | 278 |
| 204 // Delete an audio entry, notifying observers first. This is called by | 279 // Delete an audio entry, notifying observers first. This is called by |
| 205 // AudioOutputController after it has closed. | 280 // AudioOutputController after it has closed. |
| 206 void DeleteEntry(std::unique_ptr<AudioEntry> entry); | 281 void DeleteEntry(std::unique_ptr<AudioEntry> entry); |
| 207 | 282 |
| 208 // Send an error message to the renderer, then close the stream. | 283 // Send an error message to the renderer, then close the stream. |
| 209 void ReportErrorAndClose(int stream_id); | 284 void ReportErrorAndClose(int stream_id); |
| 210 | 285 |
| 286 // Send an error message to the renderer, then close the stream. | |
| 287 void ReportErrorAndCloseMojo( | |
| 288 int stream_id, | |
| 289 const mojom::AudioOutput::CreateStreamCallback& callback); | |
| 290 | |
| 211 // A helper method to look up a AudioEntry identified by |stream_id|. | 291 // A helper method to look up a AudioEntry identified by |stream_id|. |
| 212 // Returns NULL if not found. | 292 // Returns NULL if not found. |
| 213 AudioEntry* LookupById(int stream_id); | 293 AudioEntry* LookupById(int stream_id); |
| 214 | 294 |
| 215 // A helper method to update the number of playing streams and alert the | 295 // A helper method to update the number of playing streams and alert the |
| 216 // ResourceScheduler when the renderer starts or stops playing an audiostream. | 296 // ResourceScheduler when the renderer starts or stops playing an audiostream. |
| 217 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); | 297 void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing); |
| 218 | 298 |
| 219 // Check if the renderer process has access to the requested output device. | 299 // Check if the renderer process has access to the requested output device. |
| 220 void CheckOutputDeviceAccess(int render_frame_id, | 300 void CheckOutputDeviceAccess(int render_frame_id, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 236 // Helper method to check if the authorization procedure for stream | 316 // Helper method to check if the authorization procedure for stream |
| 237 // |stream_id| has started. | 317 // |stream_id| has started. |
| 238 bool IsAuthorizationStarted(int stream_id); | 318 bool IsAuthorizationStarted(int stream_id); |
| 239 | 319 |
| 240 // ID of the RenderProcessHost that owns this instance. | 320 // ID of the RenderProcessHost that owns this instance. |
| 241 const int render_process_id_; | 321 const int render_process_id_; |
| 242 | 322 |
| 243 media::AudioManager* const audio_manager_; | 323 media::AudioManager* const audio_manager_; |
| 244 AudioMirroringManager* const mirroring_manager_; | 324 AudioMirroringManager* const mirroring_manager_; |
| 245 std::unique_ptr<media::AudioLog> audio_log_; | 325 std::unique_ptr<media::AudioLog> audio_log_; |
| 326 AudioOutputImpl* audio_output_impl_; | |
| 246 | 327 |
| 247 // Used to access to AudioInputDeviceManager. | 328 // Used to access to AudioInputDeviceManager. |
| 248 MediaStreamManager* media_stream_manager_; | 329 MediaStreamManager* media_stream_manager_; |
| 249 | 330 |
| 250 // A map of stream IDs to audio sources. | 331 // A map of stream IDs to audio sources. |
| 251 AudioEntryMap audio_entries_; | 332 AudioEntryMap audio_entries_; |
| 252 | 333 |
| 253 // The number of streams in the playing state. Atomic read safe from any | 334 // The number of streams in the playing state. Atomic read safe from any |
| 254 // thread, but should only be updated from the IO thread. | 335 // thread, but should only be updated from the IO thread. |
| 255 base::AtomicRefCount num_playing_streams_; | 336 base::AtomicRefCount num_playing_streams_; |
| 256 | 337 |
| 257 // Salt required to translate renderer device IDs to raw device unique IDs | 338 // Salt required to translate renderer device IDs to raw device unique IDs |
| 258 ResourceContext::SaltCallback salt_callback_; | 339 ResourceContext::SaltCallback salt_callback_; |
| 259 | 340 |
| 260 // Map of device authorizations for streams that are not yet created | 341 // 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 | 342 // 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. | 343 // is a bool that is true if the authorization process completes successfully. |
| 263 // The second element contains the unique ID of the authorized device. | 344 // The second element contains the unique ID of the authorized device. |
| 264 std::map<int, std::pair<bool, std::string>> authorizations_; | 345 std::map<int, std::pair<bool, std::string>> authorizations_; |
| 265 | 346 |
| 266 // The maximum number of simultaneous streams during the lifetime of this | 347 // The maximum number of simultaneous streams during the lifetime of this |
| 267 // host. Reported as UMA stat at shutdown. | 348 // host. Reported as UMA stat at shutdown. |
| 268 size_t max_simultaneous_streams_; | 349 size_t max_simultaneous_streams_; |
| 269 | 350 |
| 351 mojom::AudioOutputStreamPtr stream_ptr_; | |
| 352 | |
| 270 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 353 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 271 }; | 354 }; |
| 272 | 355 |
| 273 } // namespace content | 356 } // namespace content |
| 274 | 357 |
| 275 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 358 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |