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 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 22 matching lines...) Expand all Loading... | |
| 33 // v v | 33 // v v |
| 34 | 34 |
| 35 // A SyncSocket pair is used to signal buffer readiness between processes. | 35 // A SyncSocket pair is used to signal buffer readiness between processes. |
| 36 | 36 |
| 37 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 37 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| 38 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 38 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| 39 | 39 |
| 40 #include <map> | 40 #include <map> |
| 41 | 41 |
| 42 #include "base/gtest_prod_util.h" | 42 #include "base/gtest_prod_util.h" |
| 43 #include "base/lazy_instance.h" | |
| 43 #include "base/memory/ref_counted.h" | 44 #include "base/memory/ref_counted.h" |
| 44 #include "base/memory/scoped_ptr.h" | 45 #include "base/memory/scoped_ptr.h" |
| 45 #include "base/process.h" | 46 #include "base/process.h" |
| 46 #include "base/sequenced_task_runner_helpers.h" | 47 #include "base/sequenced_task_runner_helpers.h" |
| 47 #include "base/shared_memory.h" | 48 #include "base/shared_memory.h" |
| 48 #include "content/common/content_export.h" | 49 #include "content/common/content_export.h" |
| 49 #include "content/public/browser/browser_message_filter.h" | 50 #include "content/public/browser/browser_message_filter.h" |
| 50 #include "content/public/browser/browser_thread.h" | 51 #include "content/public/browser/browser_thread.h" |
| 51 #include "media/audio/audio_io.h" | 52 #include "media/audio/audio_io.h" |
| 52 #include "media/audio/audio_output_controller.h" | 53 #include "media/audio/audio_output_controller.h" |
| 53 #include "media/audio/simple_sources.h" | 54 #include "media/audio/simple_sources.h" |
| 54 | 55 |
| 55 namespace media { | 56 namespace media { |
| 56 class AudioManager; | 57 class AudioManager; |
| 57 class AudioParameters; | 58 class AudioParameters; |
| 58 } | 59 } |
| 59 | 60 |
| 60 namespace content { | 61 namespace content { |
| 61 | 62 |
| 62 class MediaObserver; | 63 class MediaObserver; |
| 63 class ResourceContext; | 64 class ResourceContext; |
| 65 class WebContentsAudioInputStream; | |
| 64 | 66 |
| 65 class CONTENT_EXPORT AudioRendererHost | 67 class CONTENT_EXPORT AudioRendererHost |
| 66 : public BrowserMessageFilter, | 68 : public BrowserMessageFilter, |
| 67 public media::AudioOutputController::EventHandler { | 69 public media::AudioOutputController::EventHandler { |
| 68 public: | 70 public: |
| 69 // Called from UI thread from the owner of this object. | 71 // Called from UI thread from the owner of this object. |
| 70 AudioRendererHost(media::AudioManager* audio_manager, | 72 AudioRendererHost(int render_process_id, |
| 73 media::AudioManager* audio_manager, | |
| 71 MediaObserver* media_observer); | 74 MediaObserver* media_observer); |
| 72 | 75 |
| 73 // BrowserMessageFilter implementation. | 76 // BrowserMessageFilter implementation. |
| 77 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | |
| 74 virtual void OnChannelClosing() OVERRIDE; | 78 virtual void OnChannelClosing() OVERRIDE; |
| 75 virtual void OnDestruct() const OVERRIDE; | 79 virtual void OnDestruct() const OVERRIDE; |
| 76 virtual bool OnMessageReceived(const IPC::Message& message, | 80 virtual bool OnMessageReceived(const IPC::Message& message, |
| 77 bool* message_was_ok) OVERRIDE; | 81 bool* message_was_ok) OVERRIDE; |
| 78 | 82 |
| 79 // AudioOutputController::EventHandler implementations. | 83 // AudioOutputController::EventHandler implementations. |
| 80 virtual void OnCreated(media::AudioOutputController* controller) OVERRIDE; | 84 virtual void OnCreated(media::AudioOutputController* controller) OVERRIDE; |
| 81 virtual void OnPlaying(media::AudioOutputController* controller) OVERRIDE; | 85 virtual void OnPlaying(media::AudioOutputController* controller) OVERRIDE; |
| 82 virtual void OnPaused(media::AudioOutputController* controller) OVERRIDE; | 86 virtual void OnPaused(media::AudioOutputController* controller) OVERRIDE; |
| 83 virtual void OnError(media::AudioOutputController* controller, | 87 virtual void OnError(media::AudioOutputController* controller, |
| 84 int error_code) OVERRIDE; | 88 int error_code) OVERRIDE; |
| 85 | 89 |
| 90 // Start/stop mirroring all audio output streams associated with | |
| 91 // |render_process_id| + |render_view_id| to |destination|. Any call to | |
| 92 // StartMirroring() must be paired with a later call to StopMirroring() since | |
| 93 // references are held to both the AudioRendererHost and | |
|
Alpha Left Google
2012/11/28 20:02:25
Can we not hold reference to AudioRendererHost?
miu
2012/11/28 22:05:50
Done.
| |
| 94 // WebContentsAudioInputStream instances while the mirroring session is | |
| 95 // active. | |
| 96 static void StartMirroring( | |
|
Alpha Left Google
2012/11/28 20:02:25
It seems to me these two methods are called on the
| |
| 97 int render_process_id, int render_view_id, | |
| 98 const scoped_refptr<WebContentsAudioInputStream>& destination); | |
| 99 static void StopMirroring( | |
| 100 int render_process_id, int render_view_id, | |
| 101 const scoped_refptr<WebContentsAudioInputStream>& destination); | |
| 102 | |
| 86 private: | 103 private: |
| 87 friend class AudioRendererHostTest; | 104 friend class AudioRendererHostTest; |
| 88 friend class BrowserThread; | 105 friend class BrowserThread; |
| 89 friend class base::DeleteHelper<AudioRendererHost>; | 106 friend class base::DeleteHelper<AudioRendererHost>; |
| 90 friend class MockAudioRendererHost; | 107 friend class MockAudioRendererHost; |
| 91 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 108 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
| 92 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 109 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
| 93 | 110 |
| 94 struct AudioEntry; | 111 struct AudioEntry; |
| 95 typedef std::map<int, AudioEntry*> AudioEntryMap; | 112 typedef std::map<int, AudioEntry*> AudioEntryMap; |
| 113 typedef std::map<int, scoped_refptr<WebContentsAudioInputStream> > | |
| 114 MirrorSessionMap; | |
| 115 typedef std::map<int, scoped_refptr<AudioRendererHost> > ActiveHostMap; | |
|
Alpha Left Google
2012/11/28 20:02:25
Don't use scoped_refptr, just a * is enough. I sug
miu
2012/11/28 22:05:50
Done.
Not sure what you meant about the asserts.
| |
| 96 | 116 |
| 97 virtual ~AudioRendererHost(); | 117 virtual ~AudioRendererHost(); |
| 98 | 118 |
| 99 // Methods called on IO thread ---------------------------------------------- | 119 // Methods called on IO thread ---------------------------------------------- |
| 100 | 120 |
| 101 // Audio related IPC message handlers. | 121 // Audio related IPC message handlers. |
| 102 // Creates an audio output stream with the specified format. If this call is | 122 // Creates an audio output stream with the specified format. If this call is |
| 103 // successful this object would keep an internal entry of the stream for the | 123 // successful this object would keep an internal entry of the stream for the |
| 104 // required properties. | 124 // required properties. |
| 105 void OnCreateStream(int stream_id, | 125 void OnCreateStream(int stream_id, |
| 106 const media::AudioParameters& params, | 126 const media::AudioParameters& params, |
| 107 int input_channels); | 127 int input_channels); |
| 108 | 128 |
| 129 // Associate |render_view_id| as the producer of audio for the stream | |
| 130 // referenced by |stream_id|. | |
| 131 void OnAssociateStreamWithProducer(int stream_id, int render_view_id); | |
| 132 | |
| 109 // Play the audio stream referenced by |stream_id|. | 133 // Play the audio stream referenced by |stream_id|. |
| 110 void OnPlayStream(int stream_id); | 134 void OnPlayStream(int stream_id); |
| 111 | 135 |
| 112 // Pause the audio stream referenced by |stream_id|. | 136 // Pause the audio stream referenced by |stream_id|. |
| 113 void OnPauseStream(int stream_id); | 137 void OnPauseStream(int stream_id); |
| 114 | 138 |
| 115 // Discard all audio data in stream referenced by |stream_id|. | 139 // Discard all audio data in stream referenced by |stream_id|. |
| 116 void OnFlushStream(int stream_id); | 140 void OnFlushStream(int stream_id); |
| 117 | 141 |
| 118 // Close the audio stream referenced by |stream_id|. | 142 // Close the audio stream referenced by |stream_id|. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 // Returns NULL if not found. | 177 // Returns NULL if not found. |
| 154 AudioEntry* LookupById(int stream_id); | 178 AudioEntry* LookupById(int stream_id); |
| 155 | 179 |
| 156 // Search for a AudioEntry having the reference to |controller|. | 180 // Search for a AudioEntry having the reference to |controller|. |
| 157 // This method is used to look up an AudioEntry after a controller | 181 // This method is used to look up an AudioEntry after a controller |
| 158 // event is received. | 182 // event is received. |
| 159 AudioEntry* LookupByController(media::AudioOutputController* controller); | 183 AudioEntry* LookupByController(media::AudioOutputController* controller); |
| 160 | 184 |
| 161 media::AudioOutputController* LookupControllerByIdForTesting(int stream_id); | 185 media::AudioOutputController* LookupControllerByIdForTesting(int stream_id); |
| 162 | 186 |
| 187 // Global look-up for the AudioRendererHost instance owned by the | |
| 188 // RenderProcessHost specified by |render_process_id|. | |
| 189 static AudioRendererHost* FromRenderProcessID(int render_process_id); | |
| 190 | |
| 191 // Implementations for StartMirroring() and StopMirroring() that are run on | |
| 192 // the IO BrowserThread. | |
| 193 static void DoStartMirroring( | |
| 194 int render_process_id, int render_view_id, | |
| 195 const scoped_refptr<WebContentsAudioInputStream>& destination); | |
| 196 static void DoStopMirroring( | |
| 197 int render_process_id, int render_view_id, | |
| 198 const scoped_refptr<WebContentsAudioInputStream>& destination); | |
| 199 | |
| 200 // ID of the RenderProcessHost that owns this instance. | |
| 201 const int render_process_id_; | |
| 202 | |
| 163 // A map of stream IDs to audio sources. | 203 // A map of stream IDs to audio sources. |
| 164 AudioEntryMap audio_entries_; | 204 AudioEntryMap audio_entries_; |
| 165 | 205 |
| 206 // A map of render view IDs to the destination for active mirroring sessions. | |
| 207 MirrorSessionMap mirror_sessions_; | |
| 208 | |
| 166 media::AudioManager* audio_manager_; | 209 media::AudioManager* audio_manager_; |
| 167 MediaObserver* media_observer_; | 210 MediaObserver* media_observer_; |
| 168 | 211 |
| 212 // Global look-up map for currently-active (i.e., IPC channel is open) | |
| 213 // AudioRendererHosts. | |
| 214 static base::LazyInstance<ActiveHostMap>::Leaky g_host_map_; | |
| 215 | |
| 169 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 216 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 170 }; | 217 }; |
| 171 | 218 |
| 172 } // namespace content | 219 } // namespace content |
| 173 | 220 |
| 174 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 221 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |