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 21 matching lines...) Expand all Loading... |
32 // | CloseStream > | | 32 // | CloseStream > | |
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/callback_forward.h" |
42 #include "base/gtest_prod_util.h" | 43 #include "base/gtest_prod_util.h" |
| 44 #include "base/lazy_instance.h" |
43 #include "base/memory/ref_counted.h" | 45 #include "base/memory/ref_counted.h" |
44 #include "base/memory/scoped_ptr.h" | 46 #include "base/memory/scoped_ptr.h" |
45 #include "base/process.h" | 47 #include "base/process.h" |
46 #include "base/sequenced_task_runner_helpers.h" | 48 #include "base/sequenced_task_runner_helpers.h" |
47 #include "base/shared_memory.h" | 49 #include "base/shared_memory.h" |
48 #include "content/common/content_export.h" | 50 #include "content/common/content_export.h" |
49 #include "content/public/browser/browser_message_filter.h" | 51 #include "content/public/browser/browser_message_filter.h" |
50 #include "content/public/browser/browser_thread.h" | 52 #include "content/public/browser/browser_thread.h" |
51 #include "media/audio/audio_io.h" | 53 #include "media/audio/audio_io.h" |
52 #include "media/audio/audio_output_controller.h" | 54 #include "media/audio/audio_output_controller.h" |
53 #include "media/audio/simple_sources.h" | 55 #include "media/audio/simple_sources.h" |
54 | 56 |
55 namespace media { | 57 namespace media { |
56 class AudioManager; | 58 class AudioManager; |
57 class AudioParameters; | 59 class AudioParameters; |
58 } | 60 } |
59 | 61 |
60 namespace content { | 62 namespace content { |
61 | 63 |
62 class MediaObserver; | 64 class MediaObserver; |
63 class ResourceContext; | 65 class ResourceContext; |
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: |
| 71 // Interface to be implemented by audio mirroring destinations. See |
| 72 // comments for StartMirroring() and StopMirroring() below. |
| 73 class MirroringDestination |
| 74 : public base::RefCountedThreadSafe<MirroringDestination> { |
| 75 public: |
| 76 // Begin pulling data from |callback|, which is providing data in the format |
| 77 // specified by |params|. |
| 78 virtual void AddInput( |
| 79 media::AudioOutputStream::AudioSourceCallback* callback, |
| 80 const media::AudioParameters& params) = 0; |
| 81 |
| 82 // Stop pulling data from |callback|. |done| is run once the caller can |
| 83 // safely destroy the callback. |
| 84 virtual void RemoveInput( |
| 85 media::AudioOutputStream::AudioSourceCallback* callback, |
| 86 const base::Closure& done) = 0; |
| 87 |
| 88 protected: |
| 89 friend class base::RefCountedThreadSafe<MirroringDestination>; |
| 90 virtual ~MirroringDestination(); |
| 91 }; |
| 92 |
69 // Called from UI thread from the owner of this object. | 93 // Called from UI thread from the owner of this object. |
70 AudioRendererHost(media::AudioManager* audio_manager, | 94 AudioRendererHost(int render_process_id, |
| 95 media::AudioManager* audio_manager, |
71 MediaObserver* media_observer); | 96 MediaObserver* media_observer); |
72 | 97 |
73 // BrowserMessageFilter implementation. | 98 // BrowserMessageFilter implementation. |
| 99 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; |
74 virtual void OnChannelClosing() OVERRIDE; | 100 virtual void OnChannelClosing() OVERRIDE; |
75 virtual void OnDestruct() const OVERRIDE; | 101 virtual void OnDestruct() const OVERRIDE; |
76 virtual bool OnMessageReceived(const IPC::Message& message, | 102 virtual bool OnMessageReceived(const IPC::Message& message, |
77 bool* message_was_ok) OVERRIDE; | 103 bool* message_was_ok) OVERRIDE; |
78 | 104 |
79 // AudioOutputController::EventHandler implementations. | 105 // AudioOutputController::EventHandler implementations. |
80 virtual void OnCreated(media::AudioOutputController* controller) OVERRIDE; | 106 virtual void OnCreated(media::AudioOutputController* controller) OVERRIDE; |
81 virtual void OnPlaying(media::AudioOutputController* controller) OVERRIDE; | 107 virtual void OnPlaying(media::AudioOutputController* controller) OVERRIDE; |
82 virtual void OnPaused(media::AudioOutputController* controller) OVERRIDE; | 108 virtual void OnPaused(media::AudioOutputController* controller) OVERRIDE; |
83 virtual void OnError(media::AudioOutputController* controller, | 109 virtual void OnError(media::AudioOutputController* controller, |
84 int error_code) OVERRIDE; | 110 int error_code) OVERRIDE; |
85 | 111 |
| 112 // Start/stop mirroring all audio output streams associated with |
| 113 // |render_process_id| + |render_view_id| to |destination|. Any call to |
| 114 // StartMirroring() must be paired with a later call to StopMirroring() since |
| 115 // a reference is held on the MirroringDestination while the mirroring session |
| 116 // is active. |
| 117 static void StartMirroring( |
| 118 int render_process_id, int render_view_id, |
| 119 const scoped_refptr<MirroringDestination>& destination); |
| 120 static void StopMirroring( |
| 121 int render_process_id, int render_view_id, |
| 122 const scoped_refptr<MirroringDestination>& destination); |
| 123 |
86 private: | 124 private: |
87 friend class AudioRendererHostTest; | 125 friend class AudioRendererHostTest; |
88 friend class BrowserThread; | 126 friend class BrowserThread; |
89 friend class base::DeleteHelper<AudioRendererHost>; | 127 friend class base::DeleteHelper<AudioRendererHost>; |
90 friend class MockAudioRendererHost; | 128 friend class MockAudioRendererHost; |
91 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 129 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
92 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 130 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
93 | 131 |
94 struct AudioEntry; | 132 struct AudioEntry; |
95 typedef std::map<int, AudioEntry*> AudioEntryMap; | 133 typedef std::map<int, AudioEntry*> AudioEntryMap; |
| 134 typedef std::map<int, scoped_refptr<MirroringDestination> > MirrorSessionMap; |
| 135 typedef std::map<int, AudioRendererHost*> ActiveHostMap; |
96 | 136 |
97 virtual ~AudioRendererHost(); | 137 virtual ~AudioRendererHost(); |
98 | 138 |
99 // Methods called on IO thread ---------------------------------------------- | 139 // Methods called on IO thread ---------------------------------------------- |
100 | 140 |
101 // Audio related IPC message handlers. | 141 // Audio related IPC message handlers. |
102 // Creates an audio output stream with the specified format. If this call is | 142 // 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 | 143 // successful this object would keep an internal entry of the stream for the |
104 // required properties. | 144 // required properties. |
105 void OnCreateStream(int stream_id, | 145 void OnCreateStream(int stream_id, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 // Returns NULL if not found. | 197 // Returns NULL if not found. |
158 AudioEntry* LookupById(int stream_id); | 198 AudioEntry* LookupById(int stream_id); |
159 | 199 |
160 // Search for a AudioEntry having the reference to |controller|. | 200 // Search for a AudioEntry having the reference to |controller|. |
161 // This method is used to look up an AudioEntry after a controller | 201 // This method is used to look up an AudioEntry after a controller |
162 // event is received. | 202 // event is received. |
163 AudioEntry* LookupByController(media::AudioOutputController* controller); | 203 AudioEntry* LookupByController(media::AudioOutputController* controller); |
164 | 204 |
165 media::AudioOutputController* LookupControllerByIdForTesting(int stream_id); | 205 media::AudioOutputController* LookupControllerByIdForTesting(int stream_id); |
166 | 206 |
| 207 // Global look-up for the AudioRendererHost instance owned by the |
| 208 // RenderProcessHost specified by |render_process_id|. |
| 209 static AudioRendererHost* FromRenderProcessID(int render_process_id); |
| 210 |
| 211 // Implementations for StartMirroring() and StopMirroring() that are run on |
| 212 // the IO BrowserThread. |
| 213 static void DoStartMirroring( |
| 214 int render_process_id, int render_view_id, |
| 215 const scoped_refptr<MirroringDestination>& destination); |
| 216 static void DoStopMirroring( |
| 217 int render_process_id, int render_view_id, |
| 218 const scoped_refptr<MirroringDestination>& destination); |
| 219 |
| 220 // ID of the RenderProcessHost that owns this instance. |
| 221 const int render_process_id_; |
| 222 |
167 // A map of stream IDs to audio sources. | 223 // A map of stream IDs to audio sources. |
168 AudioEntryMap audio_entries_; | 224 AudioEntryMap audio_entries_; |
169 | 225 |
| 226 // A map of render view IDs to the destination for active mirroring sessions. |
| 227 MirrorSessionMap mirror_sessions_; |
| 228 |
170 media::AudioManager* audio_manager_; | 229 media::AudioManager* audio_manager_; |
171 MediaObserver* media_observer_; | 230 MediaObserver* media_observer_; |
172 | 231 |
| 232 // Global look-up map for currently-active (i.e., IPC channel is open) |
| 233 // AudioRendererHosts. |
| 234 static base::LazyInstance<ActiveHostMap>::Leaky g_host_map_; |
| 235 |
173 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 236 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
174 }; | 237 }; |
175 | 238 |
176 } // namespace content | 239 } // namespace content |
177 | 240 |
178 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 241 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
OLD | NEW |