OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 // | |
5 // Audio rendering unit utilizing audio output stream provided by browser | |
6 // process through IPC. | |
7 // | |
8 // Relationship of classes. | |
9 // | |
10 // AudioOutputController AudioDevice | |
11 // ^ ^ | |
12 // | | | |
13 // v IPC v | |
14 // AudioRendererHost <---------> AudioMessageFilter | |
15 // | |
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 | |
18 // to generate a low latency transport. The AudioDevice user registers an | |
19 // AudioDevice::RenderCallback at construction and will be polled by the | |
20 // AudioDevice for audio to be played out by the underlying audio layers. | |
21 // | |
22 // State sequences. | |
23 // | |
24 // Task [IO thread] IPC [IO thread] | |
25 // | |
26 // Start -> CreateStreamOnIOThread -----> CreateStream ------> | |
27 // <- OnStreamCreated <- AudioMsg_NotifyStreamCreated <- | |
28 // ---> PlayOnIOThread -----------> PlayStream --------> | |
29 // | |
30 // Optionally Play() / Pause() sequences may occur: | |
31 // Play -> PlayOnIOThread --------------> PlayStream ---------> | |
32 // Pause -> PauseOnIOThread ------------> PauseStream --------> | |
33 // (note that Play() / Pause() sequences before OnStreamCreated are | |
34 // deferred until OnStreamCreated, with the last valid state being used) | |
35 // | |
36 // AudioDevice::Render => audio transport on audio thread => | |
37 // | | |
38 // Stop --> ShutDownOnIOThread --------> CloseStream -> Close | |
39 // | |
40 // This class utilizes several threads during its lifetime, namely: | |
41 // 1. Creating thread. | |
42 // Must be the main render thread. | |
43 // 2. Control thread (may be the main render thread or another thread). | |
44 // The methods: Start(), Stop(), Play(), Pause(), SetVolume() | |
45 // must be called on the same thread. | |
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 | |
48 // IPC communications can only happen in this thread. | |
49 // 4. Audio transport thread (See AudioDeviceThread). | |
50 // Responsible for calling the AudioThreadCallback implementation that in | |
51 // turn calls AudioRendererSink::RenderCallback which feeds audio samples to | |
52 // the audio layer in the browser process using sync sockets and shared | |
53 // memory. | |
54 // | |
55 // Implementation notes: | |
56 // | |
57 // - Start() is asynchronous/non-blocking. | |
58 // - Stop() is asynchronous/non-blocking. | |
59 // - Play() is asynchronous/non-blocking. | |
60 // - Pause() is asynchronous/non-blocking. | |
61 // - The user must call Stop() before deleting the class instance. | |
62 | |
63 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | |
64 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | |
65 | |
66 #include "base/basictypes.h" | |
67 #include "base/bind.h" | |
68 #include "base/memory/scoped_ptr.h" | |
69 #include "base/message_loop.h" | |
70 #include "base/shared_memory.h" | |
71 #include "content/common/content_export.h" | |
72 #include "content/renderer/media/audio_device_thread.h" | |
73 #include "content/renderer/media/scoped_loop_observer.h" | |
74 #include "media/audio/audio_output_ipc.h" | |
75 #include "media/audio/audio_parameters.h" | |
76 #include "media/base/audio_renderer_sink.h" | |
77 | |
78 namespace media { | |
79 class AudioParameters; | |
80 } | |
81 | |
82 class CONTENT_EXPORT AudioDevice | |
83 : NON_EXPORTED_BASE(public media::AudioRendererSink), | |
84 public media::AudioOutputIPCDelegate, | |
85 NON_EXPORTED_BASE(public ScopedLoopObserver) { | |
86 public: | |
87 // Methods called on main render thread ------------------------------------- | |
88 | |
89 // AudioRendererSink implementation. | |
90 virtual void Initialize(const media::AudioParameters& params, | |
91 RenderCallback* callback) OVERRIDE; | |
92 virtual void Start() OVERRIDE; | |
93 virtual void Stop() OVERRIDE; | |
94 virtual void Play() OVERRIDE; | |
95 virtual void Pause(bool flush) OVERRIDE; | |
96 virtual bool SetVolume(double volume) OVERRIDE; | |
97 | |
98 // Methods called on IO thread ---------------------------------------------- | |
99 // AudioOutputIPCDelegate methods. | |
100 virtual void OnStateChanged( | |
101 media::AudioOutputIPCDelegate::State state) OVERRIDE; | |
102 virtual void OnStreamCreated(base::SharedMemoryHandle handle, | |
103 base::SyncSocket::Handle socket_handle, | |
104 int length) OVERRIDE; | |
105 virtual void OnIPCClosed() OVERRIDE; | |
106 | |
107 // Creates an uninitialized AudioDevice. Clients must call Initialize() | |
108 // before using. | |
109 // TODO(tommi): When all dependencies on |content| have been removed | |
110 // from AudioDevice, move this class over to media/audio. | |
111 AudioDevice(media::AudioOutputIPC* ipc, | |
112 const scoped_refptr<base::MessageLoopProxy>& io_loop); | |
113 | |
114 protected: | |
115 // Magic required by ref_counted.h to avoid any code deleting the object | |
116 // accidentally while there are references to it. | |
117 friend class base::RefCountedThreadSafe<AudioDevice>; | |
118 virtual ~AudioDevice(); | |
119 | |
120 private: | |
121 // Methods called on IO thread ---------------------------------------------- | |
122 // The following methods are tasks posted on the IO thread that needs to | |
123 // be executed on that thread. They interact with AudioMessageFilter and | |
124 // sends IPC messages on that thread. | |
125 void CreateStreamOnIOThread(const media::AudioParameters& params); | |
126 void PlayOnIOThread(); | |
127 void PauseOnIOThread(bool flush); | |
128 void ShutDownOnIOThread(); | |
129 void SetVolumeOnIOThread(double volume); | |
130 | |
131 // MessageLoop::DestructionObserver implementation for the IO loop. | |
132 // If the IO loop dies before we do, we shut down the audio thread from here. | |
133 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | |
134 | |
135 media::AudioParameters audio_parameters_; | |
136 | |
137 RenderCallback* callback_; | |
138 | |
139 // A pointer to the IPC layer that takes care of sending requests over to | |
140 // the AudioRendererHost. | |
141 media::AudioOutputIPC* ipc_; | |
142 | |
143 // Our stream ID on the message filter. Only accessed on the IO thread. | |
144 // Must only be modified on the IO thread. | |
145 int stream_id_; | |
146 | |
147 // State of Play() / Pause() calls before OnStreamCreated() is called. | |
148 bool play_on_start_; | |
149 | |
150 // Set to |true| when OnStreamCreated() is called. | |
151 // Set to |false| when ShutDownOnIOThread() is called. | |
152 // This is for use with play_on_start_ to track Play() / Pause() state. | |
153 // Must only be touched from the IO thread. | |
154 bool is_started_; | |
155 | |
156 // Our audio thread callback class. See source file for details. | |
157 class AudioThreadCallback; | |
158 | |
159 // In order to avoid a race between OnStreamCreated and Stop(), we use this | |
160 // guard to control stopping and starting the audio thread. | |
161 base::Lock audio_thread_lock_; | |
162 AudioDeviceThread audio_thread_; | |
163 scoped_ptr<AudioDevice::AudioThreadCallback> audio_callback_; | |
164 | |
165 | |
166 DISALLOW_COPY_AND_ASSIGN(AudioDevice); | |
167 }; | |
168 | |
169 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | |
OLD | NEW |