Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: content/renderer/media/audio_device.h

Issue 10790121: First step towards moving AudioDevice from content/ to media/audio. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove unnecessary #include Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Audio rendering unit utilizing audio output stream provided by browser 5 // Audio rendering unit utilizing audio output stream provided by browser
6 // process through IPC. 6 // process through IPC.
7 // 7 //
8 // Relationship of classes. 8 // Relationship of classes.
9 // 9 //
10 // AudioOutputController AudioDevice 10 // AudioOutputController AudioDevice
11 // ^ ^ 11 // ^ ^
12 // | | 12 // | |
13 // v IPC v 13 // v IPC v
14 // AudioRendererHost <---------> AudioMessageFilter 14 // AudioRendererHost <---------> AudioMessageFilter
15 // 15 //
16 // Transportation of audio samples from the render to the browser process 16 // Transportation of audio samples from the render to the browser process
17 // is done by using shared memory in combination with a sync socket pair 17 // is done by using shared memory in combination with a sync socket pair
18 // to generate a low latency transport. The AudioDevice user registers an 18 // to generate a low latency transport. The AudioDevice user registers an
19 // AudioDevice::RenderCallback at construction and will be polled by the 19 // AudioDevice::RenderCallback at construction and will be polled by the
20 // AudioDevice for audio to be played out by the underlying audio layers. 20 // AudioDevice for audio to be played out by the underlying audio layers.
21 // 21 //
22 // State sequences. 22 // State sequences.
23 // 23 //
24 // Task [IO thread] IPC [IO thread] 24 // Task [IO thread] IPC [IO thread]
25 // 25 //
26 // Start -> CreateStreamOnIOThread -----> AudioHostMsg_CreateStream ------> 26 // Start -> CreateStreamOnIOThread -----> CreateStream ------>
27 // <- OnStreamCreated <- AudioMsg_NotifyStreamCreated <- 27 // <- OnStreamCreated <- AudioMsg_NotifyStreamCreated <-
28 // ---> PlayOnIOThread -----------> AudioHostMsg_PlayStream --------> 28 // ---> PlayOnIOThread -----------> PlayStream -------->
29 // 29 //
30 // Optionally Play() / Pause() sequences may occur: 30 // Optionally Play() / Pause() sequences may occur:
31 // Play -> PlayOnIOThread --------------> AudioHostMsg_PlayStream ---------> 31 // Play -> PlayOnIOThread --------------> PlayStream --------->
32 // Pause -> PauseOnIOThread ------------> AudioHostMsg_PauseStream --------> 32 // Pause -> PauseOnIOThread ------------> PauseStream -------->
33 // (note that Play() / Pause() sequences before OnStreamCreated are 33 // (note that Play() / Pause() sequences before OnStreamCreated are
34 // deferred until OnStreamCreated, with the last valid state being used) 34 // deferred until OnStreamCreated, with the last valid state being used)
35 // 35 //
36 // AudioDevice::Render => audio transport on audio thread => 36 // AudioDevice::Render => audio transport on audio thread =>
37 // | 37 // |
38 // Stop --> ShutDownOnIOThread --------> AudioHostMsg_CloseStream -> Close 38 // Stop --> ShutDownOnIOThread --------> CloseStream -> Close
39 // 39 //
40 // This class utilizes several threads during its lifetime, namely: 40 // This class utilizes several threads during its lifetime, namely:
41 // 1. Creating thread. 41 // 1. Creating thread.
42 // Must be the main render thread. 42 // Must be the main render thread.
43 // 2. Control thread (may be the main render thread or another thread). 43 // 2. Control thread (may be the main render thread or another thread).
44 // The methods: Start(), Stop(), Play(), Pause(), SetVolume() 44 // The methods: Start(), Stop(), Play(), Pause(), SetVolume()
45 // must be called on the same thread. 45 // must be called on the same thread.
46 // 3. IO thread (internal implementation detail - not exposed to public API) 46 // 3. IO thread (internal implementation detail - not exposed to public API)
47 // The thread within which this class receives all the IPC messages and 47 // The thread within which this class receives all the IPC messages and
48 // IPC communications can only happen in this thread. 48 // IPC communications can only happen in this thread.
(...skipping 14 matching lines...) Expand all
63 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ 63 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_
64 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ 64 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_
65 65
66 #include "base/basictypes.h" 66 #include "base/basictypes.h"
67 #include "base/bind.h" 67 #include "base/bind.h"
68 #include "base/memory/scoped_ptr.h" 68 #include "base/memory/scoped_ptr.h"
69 #include "base/message_loop.h" 69 #include "base/message_loop.h"
70 #include "base/shared_memory.h" 70 #include "base/shared_memory.h"
71 #include "content/common/content_export.h" 71 #include "content/common/content_export.h"
72 #include "content/renderer/media/audio_device_thread.h" 72 #include "content/renderer/media/audio_device_thread.h"
73 #include "content/renderer/media/audio_message_filter.h"
74 #include "content/renderer/media/scoped_loop_observer.h" 73 #include "content/renderer/media/scoped_loop_observer.h"
74 #include "media/audio/audio_device_ipc.h"
75 #include "media/audio/audio_parameters.h" 75 #include "media/audio/audio_parameters.h"
76 #include "media/base/audio_renderer_sink.h" 76 #include "media/base/audio_renderer_sink.h"
77 77
78 namespace media { 78 namespace media {
79 class AudioParameters; 79 class AudioParameters;
80 } 80 }
81 81
82 namespace content {
83 class AudioDeviceFactory;
84 }
85
86 class CONTENT_EXPORT AudioDevice 82 class CONTENT_EXPORT AudioDevice
87 : NON_EXPORTED_BASE(public media::AudioRendererSink), 83 : NON_EXPORTED_BASE(public media::AudioRendererSink),
88 public AudioMessageFilter::Delegate, 84 public media::AudioDeviceIPCDelegate,
89 NON_EXPORTED_BASE(public ScopedLoopObserver) { 85 NON_EXPORTED_BASE(public ScopedLoopObserver) {
90 public: 86 public:
91 // Methods called on main render thread ------------------------------------- 87 // Methods called on main render thread -------------------------------------
92 88
93 // AudioRendererSink implementation. 89 // AudioRendererSink implementation.
94 virtual void Initialize(const media::AudioParameters& params, 90 virtual void Initialize(const media::AudioParameters& params,
95 RenderCallback* callback) OVERRIDE; 91 RenderCallback* callback) OVERRIDE;
96 virtual void Start() OVERRIDE; 92 virtual void Start() OVERRIDE;
97 virtual void Stop() OVERRIDE; 93 virtual void Stop() OVERRIDE;
98 virtual void Play() OVERRIDE; 94 virtual void Play() OVERRIDE;
99 virtual void Pause(bool flush) OVERRIDE; 95 virtual void Pause(bool flush) OVERRIDE;
100 virtual bool SetVolume(double volume) OVERRIDE; 96 virtual bool SetVolume(double volume) OVERRIDE;
101 97
102 // Methods called on IO thread ---------------------------------------------- 98 // Methods called on IO thread ----------------------------------------------
103 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. 99 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter.
104 virtual void OnStateChanged(AudioStreamState state) OVERRIDE; 100 virtual void OnStateChanged(media::AudioStreamState state) OVERRIDE;
105 virtual void OnStreamCreated(base::SharedMemoryHandle handle, 101 virtual void OnStreamCreated(base::SharedMemoryHandle handle,
106 base::SyncSocket::Handle socket_handle, 102 base::SyncSocket::Handle socket_handle,
107 uint32 length) OVERRIDE; 103 uint32 length) OVERRIDE;
108 104
109 protected:
110 friend class content::AudioDeviceFactory;
111
112 // Creates an uninitialized AudioDevice. Clients must call Initialize() 105 // Creates an uninitialized AudioDevice. Clients must call Initialize()
113 // before using. The constructor is protected to ensure that the 106 // before using.
114 // AudioDeviceFactory is always used for construction in Chrome.
115 // Tests should use a test class that inherits from AudioDevice to gain
116 // access to the constructor.
117 // TODO(tommi): When all dependencies on |content| have been removed 107 // TODO(tommi): When all dependencies on |content| have been removed
118 // from AudioDevice, move this class over to media/audio. 108 // from AudioDevice, move this class over to media/audio.
119 explicit AudioDevice(const scoped_refptr<base::MessageLoopProxy>& io_loop); 109 AudioDevice(media::AudioDeviceIPC* ipc,
110 const scoped_refptr<base::MessageLoopProxy>& io_loop);
120 111
112 protected:
121 // Magic required by ref_counted.h to avoid any code deleting the object 113 // Magic required by ref_counted.h to avoid any code deleting the object
122 // accidentally while there are references to it. 114 // accidentally while there are references to it.
123 friend class base::RefCountedThreadSafe<AudioDevice>; 115 friend class base::RefCountedThreadSafe<AudioDevice>;
124 virtual ~AudioDevice(); 116 virtual ~AudioDevice();
125 117
126 private: 118 private:
127 // Methods called on IO thread ---------------------------------------------- 119 // Methods called on IO thread ----------------------------------------------
128 // The following methods are tasks posted on the IO thread that needs to 120 // The following methods are tasks posted on the IO thread that needs to
129 // be executed on that thread. They interact with AudioMessageFilter and 121 // be executed on that thread. They interact with AudioMessageFilter and
130 // sends IPC messages on that thread. 122 // sends IPC messages on that thread.
131 void CreateStreamOnIOThread(const media::AudioParameters& params); 123 void CreateStreamOnIOThread(const media::AudioParameters& params);
132 void PlayOnIOThread(); 124 void PlayOnIOThread();
133 void PauseOnIOThread(bool flush); 125 void PauseOnIOThread(bool flush);
134 void ShutDownOnIOThread(); 126 void ShutDownOnIOThread();
135 void SetVolumeOnIOThread(double volume); 127 void SetVolumeOnIOThread(double volume);
136 128
137 void Send(IPC::Message* message);
138
139 // MessageLoop::DestructionObserver implementation for the IO loop. 129 // MessageLoop::DestructionObserver implementation for the IO loop.
140 // If the IO loop dies before we do, we shut down the audio thread from here. 130 // If the IO loop dies before we do, we shut down the audio thread from here.
141 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; 131 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
142 132
143 media::AudioParameters audio_parameters_; 133 media::AudioParameters audio_parameters_;
144 134
145 RenderCallback* callback_; 135 RenderCallback* callback_;
146 136
147 // Cached audio message filter (lives on the main render thread). 137 // A pointer to the IPC layer that takes care of sending requests over to
148 scoped_refptr<AudioMessageFilter> filter_; 138 // the AudioRendererHost.
139 media::AudioDeviceIPC* ipc_;
149 140
150 // Our stream ID on the message filter. Only accessed on the IO thread. 141 // Our stream ID on the message filter. Only accessed on the IO thread.
151 // Must only be modified on the IO thread. 142 // Must only be modified on the IO thread.
152 int32 stream_id_; 143 int stream_id_;
153 144
154 // State of Play() / Pause() calls before OnStreamCreated() is called. 145 // State of Play() / Pause() calls before OnStreamCreated() is called.
155 bool play_on_start_; 146 bool play_on_start_;
156 147
157 // Set to |true| when OnStreamCreated() is called. 148 // Set to |true| when OnStreamCreated() is called.
158 // Set to |false| when ShutDownOnIOThread() is called. 149 // Set to |false| when ShutDownOnIOThread() is called.
159 // This is for use with play_on_start_ to track Play() / Pause() state. 150 // This is for use with play_on_start_ to track Play() / Pause() state.
160 // Must only be touched from the IO thread. 151 // Must only be touched from the IO thread.
161 bool is_started_; 152 bool is_started_;
162 153
163 // Our audio thread callback class. See source file for details. 154 // Our audio thread callback class. See source file for details.
164 class AudioThreadCallback; 155 class AudioThreadCallback;
165 156
166 // In order to avoid a race between OnStreamCreated and Stop(), we use this 157 // In order to avoid a race between OnStreamCreated and Stop(), we use this
167 // guard to control stopping and starting the audio thread. 158 // guard to control stopping and starting the audio thread.
168 base::Lock audio_thread_lock_; 159 base::Lock audio_thread_lock_;
169 AudioDeviceThread audio_thread_; 160 AudioDeviceThread audio_thread_;
170 scoped_ptr<AudioDevice::AudioThreadCallback> audio_callback_; 161 scoped_ptr<AudioDevice::AudioThreadCallback> audio_callback_;
171 162
172 163
173 DISALLOW_COPY_AND_ASSIGN(AudioDevice); 164 DISALLOW_COPY_AND_ASSIGN(AudioDevice);
174 }; 165 };
175 166
176 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ 167 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698