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

Side by Side Diff: media/audio/audio_output_device.h

Issue 1703473002: Make AudioOutputDevice restartable and reinitializable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@new_mixing
Patch Set: Code review (guidou@). Created 4 years, 7 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
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 AudioOutputDevice 10 // AudioOutputController AudioOutputDevice
(...skipping 27 matching lines...) Expand all
38 // state being used) 38 // state being used)
39 // 39 //
40 // AudioOutputDevice::Render => audio transport on audio thread => 40 // AudioOutputDevice::Render => audio transport on audio thread =>
41 // | 41 // |
42 // Stop --> ShutDownOnIOThread --------> CloseStream -> Close 42 // Stop --> ShutDownOnIOThread --------> CloseStream -> Close
43 // 43 //
44 // This class utilizes several threads during its lifetime, namely: 44 // This class utilizes several threads during its lifetime, namely:
45 // 1. Creating thread. 45 // 1. Creating thread.
46 // Must be the main render thread. 46 // Must be the main render thread.
47 // 2. Control thread (may be the main render thread or another thread). 47 // 2. Control thread (may be the main render thread or another thread).
48 // The methods: Start(), Stop(), Play(), Pause(), SetVolume()
49 // must be called on the same thread.
50 // 3. IO thread (internal implementation detail - not exposed to public API) 48 // 3. IO thread (internal implementation detail - not exposed to public API)
51 // The thread within which this class receives all the IPC messages and 49 // The thread within which this class receives all the IPC messages and
52 // IPC communications can only happen in this thread. 50 // IPC communications can only happen in this thread.
53 // 4. Audio transport thread (See AudioDeviceThread). 51 // 4. Audio transport thread (See AudioDeviceThread).
54 // Responsible for calling the AudioThreadCallback implementation that in 52 // Responsible for calling the AudioThreadCallback implementation that in
55 // turn calls AudioRendererSink::RenderCallback which feeds audio samples to 53 // turn calls AudioRendererSink::RenderCallback which feeds audio samples to
56 // the audio layer in the browser process using sync sockets and shared 54 // the audio layer in the browser process using sync sockets and shared
57 // memory. 55 // memory.
58 // 56 //
59 // Implementation notes: 57 // NOTE: The user must call Stop() before deleting the class instance.
60 // - The user must call Stop() before deleting the class instance.
61 58
62 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ 59 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_
63 #define MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ 60 #define MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_
64 61
65 #include <memory> 62 #include <memory>
66 #include <string> 63 #include <string>
67 64
68 #include "base/bind.h" 65 #include "base/bind.h"
69 #include "base/macros.h" 66 #include "base/macros.h"
70 #include "base/memory/shared_memory.h" 67 #include "base/memory/shared_memory.h"
71 #include "base/synchronization/waitable_event.h" 68 #include "base/synchronization/waitable_event.h"
72 #include "media/audio/audio_device_thread.h" 69 #include "media/audio/audio_device_thread.h"
73 #include "media/audio/audio_output_ipc.h" 70 #include "media/audio/audio_output_ipc.h"
74 #include "media/audio/scoped_task_runner_observer.h" 71 #include "media/audio/scoped_task_runner_observer.h"
75 #include "media/base/audio_parameters.h" 72 #include "media/base/audio_parameters.h"
76 #include "media/base/audio_renderer_sink.h" 73 #include "media/base/audio_renderer_sink.h"
77 #include "media/base/media_export.h" 74 #include "media/base/media_export.h"
78 #include "media/base/output_device_info.h" 75 #include "media/base/output_device_info.h"
79 76
80 namespace media { 77 namespace media {
81 78
82 class MEDIA_EXPORT AudioOutputDevice 79 class MEDIA_EXPORT AudioOutputDevice
83 : NON_EXPORTED_BASE(public AudioRendererSink), 80 : NON_EXPORTED_BASE(public RestartableAudioRendererSink),
81 NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback),
84 NON_EXPORTED_BASE(public AudioOutputIPCDelegate), 82 NON_EXPORTED_BASE(public AudioOutputIPCDelegate),
85 NON_EXPORTED_BASE(public ScopedTaskRunnerObserver) { 83 NON_EXPORTED_BASE(public ScopedTaskRunnerObserver) {
86 public: 84 public:
87 // NOTE: Clients must call Initialize() before using. 85 // NOTE: Clients must call Initialize() before using.
88 AudioOutputDevice( 86 AudioOutputDevice(
89 std::unique_ptr<AudioOutputIPC> ipc, 87 std::unique_ptr<AudioOutputIPC> ipc,
90 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 88 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
91 int session_id, 89 int session_id,
92 const std::string& device_id, 90 const std::string& device_id,
93 const url::Origin& security_origin); 91 const url::Origin& security_origin);
94 92
95 // Request authorization to use the device specified in the constructor. 93 // Request authorization to use the device specified in the constructor.
96 void RequestDeviceAuthorization(); 94 void RequestDeviceAuthorization();
97 95
98 // AudioRendererSink implementation. 96 // AudioRendererSink implementation.
99 void Initialize(const AudioParameters& params, 97 void Initialize(const AudioParameters& params,
100 RenderCallback* callback) override; 98 AudioRendererSink::RenderCallback* callback) override;
101 void Start() override; 99 void Start() override;
102 void Stop() override; 100 void Stop() override;
103 void Play() override; 101 void Play() override;
104 void Pause() override; 102 void Pause() override;
105 bool SetVolume(double volume) override; 103 bool SetVolume(double volume) override;
106 OutputDeviceInfo GetOutputDeviceInfo() override; 104 OutputDeviceInfo GetOutputDeviceInfo() override;
107 105
108 // Methods called on IO thread ---------------------------------------------- 106 // Methods called on IO thread ----------------------------------------------
109 // AudioOutputIPCDelegate methods. 107 // AudioOutputIPCDelegate methods.
110 void OnStateChanged(AudioOutputIPCDelegateState state) override; 108 void OnStateChanged(AudioOutputIPCDelegateState state) override;
(...skipping 16 matching lines...) Expand all
127 enum State { 125 enum State {
128 IPC_CLOSED, // No more IPCs can take place. 126 IPC_CLOSED, // No more IPCs can take place.
129 IDLE, // Not started. 127 IDLE, // Not started.
130 AUTHORIZING, // Sent device authorization request, waiting for reply. 128 AUTHORIZING, // Sent device authorization request, waiting for reply.
131 AUTHORIZED, // Successful device authorization received. 129 AUTHORIZED, // Successful device authorization received.
132 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back. 130 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back.
133 PAUSED, // Paused. OnStreamCreated() has been called. Can Play()/Stop(). 131 PAUSED, // Paused. OnStreamCreated() has been called. Can Play()/Stop().
134 PLAYING, // Playing back. Can Pause()/Stop(). 132 PLAYING, // Playing back. Can Pause()/Stop().
135 }; 133 };
136 134
135 // media::AudioRendererSink::RenderCallback implementation.
136 // These two functions are called on the audio worker thread and passes calls
137 // through to |render_callback_| if it exists.
138 int Render(media::AudioBus* audio_bus,
139 uint32_t frames_delayed,
140 uint32_t frames_skipped) override;
141 void OnRenderError() override;
142
137 // Methods called on IO thread ---------------------------------------------- 143 // Methods called on IO thread ----------------------------------------------
138 // The following methods are tasks posted on the IO thread that need to 144 // The following methods are tasks posted on the IO thread that need to
139 // be executed on that thread. They use AudioOutputIPC to send IPC messages 145 // be executed on that thread. They use AudioOutputIPC to send IPC messages
140 // upon state changes. 146 // upon state changes.
141 void RequestDeviceAuthorizationOnIOThread(); 147 void RequestDeviceAuthorizationOnIOThread();
142 void CreateStreamOnIOThread(const AudioParameters& params); 148 void InitializeOnIOThread(const AudioParameters& params);
149 void CreateStreamOnIOThread();
143 void PlayOnIOThread(); 150 void PlayOnIOThread();
144 void PauseOnIOThread(); 151 void PauseOnIOThread();
145 void ShutDownOnIOThread(); 152 void StopOnIOThread();
146 void SetVolumeOnIOThread(double volume); 153 void SetVolumeOnIOThread(double volume);
147 154
148 // base::MessageLoop::DestructionObserver implementation for the IO loop. 155 // base::MessageLoop::DestructionObserver implementation for the IO loop.
149 // If the IO loop dies before we do, we shut down the audio thread from here. 156 // If the IO loop dies before we do, we shut down the audio thread from here.
150 void WillDestroyCurrentMessageLoop() override; 157 void WillDestroyCurrentMessageLoop() override;
151 158
152 AudioParameters audio_parameters_; 159 AudioParameters audio_parameters_;
153 160
154 RenderCallback* callback_; 161 base::Lock callback_lock_;
o1ka 2016/05/10 13:04:54 Probably add a comment that nothing can be done un
Henrik Grunell 2016/05/11 13:11:16 Done.
162 AudioRendererSink::RenderCallback* render_callback_;
155 163
156 // A pointer to the IPC layer that takes care of sending requests over to 164 // A pointer to the IPC layer that takes care of sending requests over to
157 // the AudioRendererHost. Only valid when state_ != IPC_CLOSED and must only 165 // the AudioRendererHost. Only valid when state_ != IPC_CLOSED and must only
158 // be accessed on the IO thread. 166 // be accessed on the IO thread.
159 std::unique_ptr<AudioOutputIPC> ipc_; 167 std::unique_ptr<AudioOutputIPC> ipc_;
160 168
161 // Current state (must only be accessed from the IO thread). See comments for 169 // Current state (must only be accessed from the IO thread). See comments for
162 // State enum above. 170 // State enum above.
163 State state_; 171 State state_;
164 172
(...skipping 11 matching lines...) Expand all
176 const std::string device_id_; 184 const std::string device_id_;
177 const url::Origin security_origin_; 185 const url::Origin security_origin_;
178 186
179 // If |device_id_| is empty and |session_id_| is not, |matched_device_id_| is 187 // If |device_id_| is empty and |session_id_| is not, |matched_device_id_| is
180 // received in OnDeviceAuthorized(). 188 // received in OnDeviceAuthorized().
181 std::string matched_device_id_; 189 std::string matched_device_id_;
182 190
183 // Our audio thread callback class. See source file for details. 191 // Our audio thread callback class. See source file for details.
184 class AudioThreadCallback; 192 class AudioThreadCallback;
185 193
186 // In order to avoid a race between OnStreamCreated and Stop(), we use this 194 // The audio thread implementation.
187 // guard to control stopping and starting the audio thread.
188 base::Lock audio_thread_lock_;
189 AudioDeviceThread audio_thread_; 195 AudioDeviceThread audio_thread_;
190 std::unique_ptr<AudioOutputDevice::AudioThreadCallback> audio_callback_; 196 std::unique_ptr<AudioOutputDevice::AudioThreadCallback>
191 197 audio_thread_callback_;
192 // Temporary hack to ignore OnStreamCreated() due to the user calling Stop()
193 // so we don't start the audio thread pointing to a potentially freed
194 // |callback_|.
195 //
196 // TODO(scherkus): Replace this by changing AudioRendererSink to either accept
197 // the callback via Start(). See http://crbug.com/151051 for details.
198 bool stopping_hack_;
199 198
200 base::WaitableEvent did_receive_auth_; 199 base::WaitableEvent did_receive_auth_;
201 AudioParameters output_params_; 200 AudioParameters output_params_;
202 OutputDeviceStatus device_status_; 201 OutputDeviceStatus device_status_;
203 202
204 DISALLOW_COPY_AND_ASSIGN(AudioOutputDevice); 203 DISALLOW_COPY_AND_ASSIGN(AudioOutputDevice);
205 }; 204 };
206 205
207 } // namespace media 206 } // namespace media
208 207
209 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_ 208 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DEVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698