OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 #include "media/audio/linux/pulse_output.h" | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "media/audio/audio_parameters.h" | |
9 #include "media/audio/audio_util.h" | |
10 #include "media/audio/linux/audio_manager_linux.h" | |
11 #include "media/base/data_buffer.h" | |
12 #include "media/base/seekable_buffer.h" | |
13 | |
14 static pa_sample_format_t BitsToFormat(int bits_per_sample) { | |
15 switch (bits_per_sample) { | |
16 // Unsupported sample formats shown for reference. I am assuming we want | |
17 // signed and little endian because that is what we gave to ALSA. | |
18 case 8: | |
19 return PA_SAMPLE_U8; | |
20 // Also 8-bits: PA_SAMPLE_ALAW and PA_SAMPLE_ULAW | |
21 case 16: | |
22 return PA_SAMPLE_S16LE; | |
23 // Also 16-bits: PA_SAMPLE_S16BE (big endian). | |
24 case 24: | |
25 return PA_SAMPLE_S24LE; | |
26 // Also 24-bits: PA_SAMPLE_S24BE (big endian). | |
27 // Other cases: PA_SAMPLE_24_32LE (in LSB of 32-bit field, little endian), | |
28 // and PA_SAMPLE_24_32BE (in LSB of 32-bit field, big endian), | |
29 case 32: | |
30 return PA_SAMPLE_S32LE; | |
31 // Also 32-bits: PA_SAMPLE_S32BE (big endian), | |
32 // PA_SAMPLE_FLOAT32LE (floating point little endian), | |
33 // and PA_SAMPLE_FLOAT32BE (floating point big endian). | |
34 default: | |
35 return PA_SAMPLE_INVALID; | |
36 } | |
37 } | |
38 | |
39 static size_t MicrosecondsToBytes( | |
40 uint32 microseconds, uint32 sample_rate, size_t bytes_per_frame) { | |
41 return microseconds * sample_rate * bytes_per_frame / | |
42 base::Time::kMicrosecondsPerSecond; | |
43 } | |
44 | |
45 void PulseAudioOutputStream::ContextStateCallback(pa_context* context, | |
46 void* state_addr) { | |
47 pa_context_state_t* state = static_cast<pa_context_state_t*>(state_addr); | |
48 *state = pa_context_get_state(context); | |
49 } | |
50 | |
51 void PulseAudioOutputStream::WriteRequestCallback( | |
52 pa_stream* playback_handle, size_t length, void* stream_addr) { | |
53 PulseAudioOutputStream* stream = | |
54 static_cast<PulseAudioOutputStream*>(stream_addr); | |
55 | |
56 DCHECK_EQ(stream->message_loop_, MessageLoop::current()); | |
57 | |
58 stream->write_callback_handled_ = true; | |
59 | |
60 // Fulfill write request. | |
61 stream->FulfillWriteRequest(length); | |
62 } | |
63 | |
64 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, | |
65 AudioManagerLinux* manager, | |
66 MessageLoop* message_loop) | |
67 : channel_layout_(params.channel_layout), | |
68 channel_count_(ChannelLayoutToChannelCount(channel_layout_)), | |
69 sample_format_(BitsToFormat(params.bits_per_sample)), | |
70 sample_rate_(params.sample_rate), | |
71 bytes_per_frame_(params.channels * params.bits_per_sample / 8), | |
72 manager_(manager), | |
73 pa_context_(NULL), | |
74 pa_mainloop_(NULL), | |
75 playback_handle_(NULL), | |
76 packet_size_(params.GetPacketSize()), | |
77 frames_per_packet_(packet_size_ / bytes_per_frame_), | |
78 client_buffer_(NULL), | |
79 volume_(1.0f), | |
80 stream_stopped_(true), | |
81 write_callback_handled_(false), | |
82 message_loop_(message_loop), | |
83 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | |
84 source_callback_(NULL) { | |
85 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
86 DCHECK(manager_); | |
87 | |
88 // TODO(slock): Sanity check input values. | |
89 } | |
90 | |
91 PulseAudioOutputStream::~PulseAudioOutputStream() { | |
92 // All internal structures should already have been freed in Close(), | |
93 // which calls AudioManagerLinux::Release which deletes this object. | |
94 DCHECK(!playback_handle_); | |
95 DCHECK(!pa_context_); | |
96 DCHECK(!pa_mainloop_); | |
97 } | |
98 | |
99 bool PulseAudioOutputStream::Open() { | |
100 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
101 | |
102 // TODO(slock): Possibly move most of this to an OpenPlaybackDevice function | |
103 // in a new class 'pulse_util', like alsa_util. | |
104 | |
105 // Create a mainloop API and connect to the default server. | |
106 pa_mainloop_ = pa_mainloop_new(); | |
107 pa_mainloop_api* pa_mainloop_api = pa_mainloop_get_api(pa_mainloop_); | |
108 pa_context_ = pa_context_new(pa_mainloop_api, "Chromium"); | |
109 pa_context_state_t pa_context_state = PA_CONTEXT_UNCONNECTED; | |
110 pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOFLAGS, NULL); | |
111 | |
112 // Wait until PulseAudio is ready. | |
113 pa_context_set_state_callback(pa_context_, &ContextStateCallback, | |
114 &pa_context_state); | |
115 while (pa_context_state != PA_CONTEXT_READY) { | |
116 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
117 if (pa_context_state == PA_CONTEXT_FAILED || | |
118 pa_context_state == PA_CONTEXT_TERMINATED) { | |
119 Reset(); | |
120 return false; | |
121 } | |
122 } | |
123 | |
124 // Set sample specifications and open playback stream. | |
125 pa_sample_spec pa_sample_specifications; | |
126 pa_sample_specifications.format = sample_format_; | |
127 pa_sample_specifications.rate = sample_rate_; | |
128 pa_sample_specifications.channels = channel_count_; | |
129 playback_handle_ = pa_stream_new(pa_context_, "Playback", | |
130 &pa_sample_specifications, NULL); | |
131 | |
132 // Initialize client buffer. | |
133 uint32 output_packet_size = frames_per_packet_ * bytes_per_frame_; | |
134 client_buffer_.reset(new media::SeekableBuffer(0, output_packet_size)); | |
135 | |
136 // Set write callback. | |
137 pa_stream_set_write_callback(playback_handle_, &WriteRequestCallback, this); | |
138 | |
139 // Set server-side buffer attributes. | |
140 // (uint32_t)-1 is the default and recommended value from PulseAudio's | |
141 // documentation, found at: | |
142 // http://freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.h tml. | |
143 pa_buffer_attr pa_buffer_attributes; | |
144 pa_buffer_attributes.maxlength = static_cast<uint32_t>(-1); | |
145 pa_buffer_attributes.tlength = output_packet_size; | |
146 pa_buffer_attributes.prebuf = static_cast<uint32_t>(-1); | |
147 pa_buffer_attributes.minreq = static_cast<uint32_t>(-1); | |
148 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); | |
149 | |
150 // Connect playback stream. | |
151 pa_stream_connect_playback(playback_handle_, NULL, | |
152 &pa_buffer_attributes, | |
153 (pa_stream_flags_t) | |
154 (PA_STREAM_INTERPOLATE_TIMING | | |
155 PA_STREAM_ADJUST_LATENCY | | |
156 PA_STREAM_AUTO_TIMING_UPDATE), | |
157 NULL, NULL); | |
158 | |
159 if (!playback_handle_) { | |
160 Reset(); | |
161 return false; | |
162 } | |
163 | |
164 return true; | |
165 } | |
166 | |
167 void PulseAudioOutputStream::Reset() { | |
168 stream_stopped_ = true; | |
169 | |
170 // Close the stream. | |
171 if (playback_handle_) { | |
172 pa_stream_flush(playback_handle_, NULL, NULL); | |
173 pa_stream_disconnect(playback_handle_); | |
174 | |
175 // Release PulseAudio structures. | |
176 pa_stream_unref(playback_handle_); | |
177 playback_handle_ = NULL; | |
178 } | |
179 if (pa_context_) { | |
180 pa_context_unref(pa_context_); | |
181 pa_context_ = NULL; | |
182 } | |
183 if (pa_mainloop_) { | |
184 pa_mainloop_free(pa_mainloop_); | |
185 pa_mainloop_ = NULL; | |
186 } | |
187 | |
188 // Release internal buffer. | |
189 client_buffer_.reset(); | |
190 } | |
191 | |
192 void PulseAudioOutputStream::Close() { | |
193 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
194 | |
195 Reset(); | |
196 | |
197 // Signal to the manager that we're closed and can be removed. | |
198 // This should be the last call in the function as it deletes "this". | |
199 manager_->ReleaseOutputStream(this); | |
200 } | |
201 | |
202 void PulseAudioOutputStream::WaitForWriteRequest() { | |
203 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
204 | |
205 // Iterate the PulseAudio mainloop. If the stream isn't stopped or PulseAudio | |
206 // doesn't request a write, post a task to iterate the mainloop again. | |
207 write_callback_handled_ = false; | |
208 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
209 if (!write_callback_handled_ && !stream_stopped_) { | |
vrk (LEFT CHROMIUM)
2011/08/18 20:00:48
Instead of checking && !stream_stopped_ here (str
slock
2011/08/18 20:12:01
Done.
| |
210 message_loop_->PostTask( | |
211 FROM_HERE, | |
212 method_factory_.NewRunnableMethod( | |
213 &PulseAudioOutputStream::WaitForWriteRequest)); | |
214 } | |
215 } | |
216 | |
217 bool PulseAudioOutputStream::BufferPacketFromSource() { | |
218 uint32 buffer_delay = client_buffer_->forward_bytes(); | |
219 pa_usec_t pa_latency_micros; | |
220 int negative; | |
221 pa_stream_get_latency(playback_handle_, &pa_latency_micros, &negative); | |
222 uint32 hardware_delay = MicrosecondsToBytes(pa_latency_micros, | |
223 sample_rate_, | |
224 bytes_per_frame_); | |
225 // TODO(slock): Deal with negative latency (negative == 1). This has yet | |
226 // to happen in practice though. | |
227 scoped_refptr<media::DataBuffer> packet = | |
228 new media::DataBuffer(packet_size_); | |
229 size_t packet_size = RunDataCallback(packet->GetWritableData(), | |
230 packet->GetBufferSize(), | |
231 AudioBuffersState(buffer_delay, | |
232 hardware_delay)); | |
233 | |
234 if (packet_size == 0) | |
235 return false; | |
236 | |
237 // TODO(slock): Swizzling and downmixing. | |
238 media::AdjustVolume(packet->GetWritableData(), | |
239 packet_size, | |
240 channel_count_, | |
241 bytes_per_frame_ / channel_count_, | |
242 volume_); | |
243 packet->SetDataSize(packet_size); | |
244 // Add the packet to the buffer. | |
245 client_buffer_->Append(packet); | |
246 return true; | |
247 } | |
248 | |
249 void PulseAudioOutputStream::FulfillWriteRequest(size_t requested_bytes) { | |
250 // If we have enough data to fulfill the request, we can finish the write. | |
251 if (stream_stopped_) | |
252 return; | |
253 | |
254 // Request more data from the source until we can fulfill the request or | |
255 // fail to receive anymore data. | |
256 bool buffering_successful = true; | |
257 while (client_buffer_->forward_bytes() < requested_bytes && | |
258 buffering_successful) { | |
259 buffering_successful = BufferPacketFromSource(); | |
260 } | |
261 | |
262 size_t bytes_written = 0; | |
263 if (client_buffer_->forward_bytes() > 0) { | |
264 // Try to fulfill the request by writing as many of the requested bytes to | |
265 // the stream as we can. | |
266 WriteToStream(requested_bytes, &bytes_written); | |
267 } | |
268 | |
269 if (bytes_written < requested_bytes) { | |
270 // We weren't able to buffer enough data to fulfill the request. Try to | |
271 // fulfill the rest of the request later. | |
272 message_loop_->PostTask( | |
273 FROM_HERE, | |
274 method_factory_.NewRunnableMethod( | |
275 &PulseAudioOutputStream::FulfillWriteRequest, | |
276 requested_bytes - bytes_written)); | |
277 } else { | |
278 // Continue playback. | |
279 message_loop_->PostTask( | |
280 FROM_HERE, | |
281 method_factory_.NewRunnableMethod( | |
282 &PulseAudioOutputStream::WaitForWriteRequest)); | |
283 } | |
284 } | |
285 | |
286 void PulseAudioOutputStream::WriteToStream(size_t bytes_to_write, | |
287 size_t* bytes_written) { | |
288 *bytes_written = 0; | |
289 while (*bytes_written < bytes_to_write) { | |
290 const uint8* chunk; | |
291 size_t chunk_size; | |
292 | |
293 // Stop writing if there is no more data available. | |
294 if (!client_buffer_->GetCurrentChunk(&chunk, &chunk_size)) | |
295 break; | |
296 | |
297 // Write data to stream. | |
298 pa_stream_write(playback_handle_, chunk, chunk_size, | |
299 NULL, 0LL, PA_SEEK_RELATIVE); | |
300 client_buffer_->Seek(chunk_size); | |
301 *bytes_written += chunk_size; | |
302 } | |
303 } | |
304 | |
305 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { | |
306 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
307 | |
308 CHECK(callback); | |
309 source_callback_ = callback; | |
310 | |
311 // Clear buffer, it might still have data in it. | |
312 client_buffer_->Clear(); | |
313 stream_stopped_ = false; | |
314 | |
315 // Start playback. | |
316 message_loop_->PostTask( | |
317 FROM_HERE, | |
318 method_factory_.NewRunnableMethod( | |
319 &PulseAudioOutputStream::WaitForWriteRequest)); | |
320 } | |
321 | |
322 void PulseAudioOutputStream::Stop() { | |
323 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
324 | |
325 stream_stopped_ = true; | |
326 } | |
327 | |
328 void PulseAudioOutputStream::SetVolume(double volume) { | |
329 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
330 | |
331 volume_ = static_cast<float>(volume); | |
332 } | |
333 | |
334 void PulseAudioOutputStream::GetVolume(double* volume) { | |
335 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
336 | |
337 *volume = volume_; | |
338 } | |
339 | |
340 uint32 PulseAudioOutputStream::RunDataCallback( | |
341 uint8* dest, uint32 max_size, AudioBuffersState buffers_state) { | |
342 if (source_callback_) | |
343 return source_callback_->OnMoreData(this, dest, max_size, buffers_state); | |
344 | |
345 return 0; | |
346 } | |
OLD | NEW |