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 "media/audio/audio_util.h" | |
8 #include "media/audio/linux/audio_manager_linux.h" | |
9 #include "media/base/data_buffer.h" | |
10 #include "media/base/seekable_buffer.h" | |
11 | |
12 static pa_sample_format_t BitsToFormat(int bits_per_sample) { | |
13 switch (bits_per_sample) { | |
14 // Unsupported sample formats shown for reference. I am assuming we want | |
15 // signed and little endian because that is what we gave to ALSA. | |
16 case 8: | |
17 return PA_SAMPLE_U8; | |
18 // Also 8-bits: PA_SAMPLE_ALAW and PA_SAMPLE_ULAW | |
19 case 16: | |
20 return PA_SAMPLE_S16LE; | |
21 // Also 16-bits: PA_SAMPLE_S16BE (big endian). | |
22 case 24: | |
23 return PA_SAMPLE_S24LE; | |
24 // Also 24-bits: PA_SAMPLE_S24BE (big endian). | |
25 // Other cases: PA_SAMPLE_24_32LE (in LSB of 32-bit field, little endian), | |
26 // and PA_SAMPLE_24_32BE (in LSB of 32-bit field, big endian), | |
27 case 32: | |
28 return PA_SAMPLE_S32LE; | |
29 // Also 32-bits: PA_SAMPLE_S32BE (big endian), | |
30 // PA_SAMPLE_FLOAT32LE (floating point little endian), | |
31 // and PA_SAMPLE_FLOAT32BE (floating point big endian). | |
32 default: | |
33 return PA_SAMPLE_INVALID; | |
34 } | |
35 } | |
36 | |
37 static size_t MicrosecondsToBytes( | |
38 uint32 microseconds, uint32 sample_rate, size_t bytes_per_frame) { | |
39 return microseconds * sample_rate * bytes_per_frame / | |
40 base::Time::kMicrosecondsPerSecond; | |
41 } | |
42 | |
43 void PulseAudioOutputStream::MainloopIterateTask() { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
Also nit: method name should be more descriptive.
slock
2011/08/15 20:35:06
Done.
slock
2011/08/15 20:35:06
Done.
| |
44 // Iterate the PulseAudio mainloop until the WriteCallback is called or the | |
45 // stream is stopped. The PulseAudio mainloop will call the WriteCallback to | |
46 // request more data when the server-side buffer needs more data to write to | |
47 // the audio sink. WriteCallback moves data from the |client_buffer_| to the | |
48 // server-side buffer. If the |client_buffer_| doesn't have enough data for | |
49 // the request, BufferPacketInClient is called to move data from the source | |
50 // into |client_buffer_|. | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
Comment with a "WARNING WARNING this blocks on Pul
slock
2011/08/15 20:35:06
Done.
| |
51 pa_write_has_calledback_ = false; | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
nit: Change field name to "write_callback_handled_
slock
2011/08/15 20:35:06
Done.
| |
52 while (!pa_write_has_calledback_ && !stream_stopped_ && !source_exhausted_) { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
nit: no {}
slock
2011/08/15 20:35:06
Done.
| |
53 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
54 } | |
55 } | |
56 | |
57 void PulseAudioOutputStream::ContextStateCallback(pa_context* context, | |
58 void* userdata) { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
slock
2011/08/15 20:35:06
Ignored per offline with vrk: can't check the mess
| |
59 pa_context_state_t* state = static_cast<pa_context_state_t*>(userdata); | |
60 *state = pa_context_get_state(context); | |
61 } | |
62 | |
63 void PulseAudioOutputStream::WriteCallback(pa_stream* stream, size_t length, | |
64 void* userdata) { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
slock
2011/08/15 20:35:06
Done.
| |
65 PulseAudioOutputStream* stream_ptr = | |
66 static_cast<PulseAudioOutputStream*>(userdata); | |
67 | |
68 stream_ptr->pa_write_has_calledback_ = true; | |
69 | |
70 // Request data from upstream if necessary. | |
71 while (stream_ptr->client_buffer_->forward_bytes() < length && | |
72 !stream_ptr->source_exhausted_) { | |
73 stream_ptr->BufferPacketInClient(); | |
74 } | |
75 | |
76 // Get data to write. | |
77 scoped_array<uint8> read_data(new uint8[length]); | |
78 stream_ptr->client_buffer_->Read(read_data.get(), length); | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
OK, so there are two things here that aren't so id
slock
2011/08/15 20:35:06
Done.
| |
79 | |
80 // Write to stream. | |
81 pa_stream_write(stream, read_data.get(), length, NULL, 0LL, PA_SEEK_RELATIVE); | |
82 | |
83 // Continue playback. | |
84 stream_ptr->message_loop_->PostTask( | |
85 FROM_HERE, | |
86 stream_ptr->method_factory_.NewRunnableMethod( | |
87 &PulseAudioOutputStream::MainloopIterateTask)); | |
88 } | |
89 | |
90 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params, | |
91 AudioManagerLinux* manager, | |
92 MessageLoop* message_loop) | |
93 : channel_layout_(params.channel_layout), | |
94 sample_format_(BitsToFormat(params.bits_per_sample)), | |
95 sample_rate_(params.sample_rate), | |
96 bytes_per_frame_(params.channels * params.bits_per_sample / 8), | |
97 packet_size_(params.GetPacketSize()), | |
98 frames_per_packet_(packet_size_ / bytes_per_frame_), | |
99 stream_stopped_(false), | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
Shouldn't this value be true?
slock
2011/08/15 20:35:06
Done.
| |
100 manager_(manager), | |
101 pa_context_(NULL), | |
102 pa_mainloop_(NULL), | |
103 pa_mainloop_api_(NULL), | |
104 playback_handle_(NULL), | |
105 pa_write_has_calledback_(false), | |
106 client_buffer_(NULL), | |
107 source_exhausted_(false), | |
108 volume_(1.0f), | |
109 source_callback_(NULL), | |
110 message_loop_(message_loop), | |
111 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
slock
2011/08/15 20:35:06
Done.
| |
112 // TODO(slock): Sanity check input values. | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK(manager_)
slock
2011/08/15 20:35:06
Done.
| |
113 } | |
114 | |
115 PulseAudioOutputStream::~PulseAudioOutputStream() { | |
116 // All internal structures should already have been freed in Close(), | |
117 // which calls AudioManagerLinux::Release which deletes this object. | |
118 DCHECK(playback_handle_ == NULL); | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
nit: instead of == NULL, !<field> here and the res
slock
2011/08/15 20:35:06
Done.
| |
119 DCHECK(pa_context_ == NULL); | |
120 DCHECK(pa_mainloop_ == NULL); | |
121 DCHECK(pa_mainloop_api_ == NULL); | |
122 } | |
123 | |
124 bool PulseAudioOutputStream::Open() { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
slock
2011/08/15 20:35:06
Done.
| |
125 // TODO(slock): Possibly move most of this to a OpenPlaybackDevice function in | |
126 // a new class 'pulse_util', like alsa_util. | |
127 | |
128 // Create a mainloop API and connect to the default server. | |
129 pa_mainloop_ = pa_mainloop_new(); | |
130 pa_mainloop_api_ = pa_mainloop_get_api(pa_mainloop_); | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
Actually, looks like pa_mainloop_api_ isn't used a
slock
2011/08/15 20:35:06
Done.
| |
131 pa_context_ = pa_context_new(pa_mainloop_api_, "Chromium"); | |
132 pa_context_state_t pa_context_state = PA_CONTEXT_UNCONNECTED; | |
133 pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOFLAGS, NULL); | |
134 | |
135 // Wait until PulseAudio is ready. | |
136 pa_context_set_state_callback(pa_context_, &ContextStateCallback, | |
137 &pa_context_state); | |
138 while (pa_context_state != PA_CONTEXT_READY) { | |
139 pa_mainloop_iterate(pa_mainloop_, 1, NULL); | |
140 if (pa_context_state == PA_CONTEXT_FAILED || | |
141 pa_context_state == PA_CONTEXT_TERMINATED) { | |
142 stream_stopped_ = false; | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
Why are you setting stream_stopped_ to false? If a
slock
2011/08/15 20:35:06
Because I am dumb. That should definitely be fals
| |
143 return false; | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
When you return false here and below, you are like
slock
2011/08/15 20:35:06
Done.
| |
144 } | |
145 } | |
146 | |
147 // Set sample specifications and open playback stream. | |
148 pa_sample_specs_.format = sample_format_; | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
Any reason why pa_sample_specs_ is a field instead
slock
2011/08/15 20:35:06
Done, not a field anymore.
| |
149 pa_sample_specs_.rate = sample_rate_; | |
150 pa_sample_specs_.channels = ChannelLayoutToChannelCount(channel_layout_); | |
151 playback_handle_ = pa_stream_new(pa_context_, "Playback", | |
152 &pa_sample_specs_, NULL); | |
153 | |
154 // Initialize client buffer. | |
155 uint32 output_packet_size = frames_per_packet_ * bytes_per_frame_; | |
156 client_buffer_.reset(new media::SeekableBuffer(0, output_packet_size)); | |
157 | |
158 // Set write callback. | |
159 pa_stream_set_write_callback(playback_handle_, &WriteCallback, this); | |
160 | |
161 // Set server-side buffer attributes. | |
162 // (uint32_t)-1 is the default and recommended value from PulseAudio's | |
163 // documentation, found at: | |
164 // http://freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.h tml | |
165 pa_buffer_attributes_.maxlength = static_cast<uint32_t>(-1); | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
Any reason why pa_buffer_attributes_ is a field in
slock
2011/08/15 20:35:06
Done, not a field anymore.
| |
166 pa_buffer_attributes_.tlength = output_packet_size; | |
167 pa_buffer_attributes_.prebuf = static_cast<uint32_t>(-1); | |
168 pa_buffer_attributes_.minreq = static_cast<uint32_t>(-1); | |
169 pa_buffer_attributes_.fragsize = static_cast<uint32_t>(-1); | |
170 | |
171 // Connect playback stream. | |
172 pa_stream_connect_playback(playback_handle_, NULL, | |
173 &pa_buffer_attributes_, | |
174 (pa_stream_flags_t) | |
175 (PA_STREAM_INTERPOLATE_TIMING | | |
176 PA_STREAM_ADJUST_LATENCY | | |
177 PA_STREAM_AUTO_TIMING_UPDATE), | |
178 NULL, NULL); | |
179 | |
180 if (!playback_handle_) { | |
181 stream_stopped_ = true; | |
182 return false; | |
183 } | |
184 | |
185 return true; | |
186 } | |
187 | |
188 void PulseAudioOutputStream::Close() { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
slock
2011/08/15 20:35:06
Done.
| |
189 // Close the stream. | |
190 if (playback_handle_) { | |
191 pa_stream_flush(playback_handle_, NULL, NULL); | |
192 pa_stream_disconnect(playback_handle_); | |
193 | |
194 // Release PulseAudio structures. | |
195 pa_stream_unref(playback_handle_); | |
196 playback_handle_ = NULL; | |
197 } | |
198 if (pa_context_) { | |
199 pa_context_unref(pa_context_); | |
200 pa_context_ = NULL; | |
201 } | |
202 if (pa_mainloop_) { | |
203 pa_mainloop_free(pa_mainloop_); | |
204 pa_mainloop_ = NULL; | |
205 } | |
206 // |pa_mainloop_api| is freed with |pa_mainloop_|. | |
207 pa_mainloop_api_ = NULL; | |
208 | |
209 // Release internal buffer. | |
210 client_buffer_.reset(); | |
211 | |
212 // Signal to the manager that we're closed and can be removed. | |
213 // This should be the last call in the function as it deletes "this". | |
214 manager_->ReleaseOutputStream(this); | |
215 } | |
216 | |
217 void PulseAudioOutputStream::BufferPacketInClient() { | |
218 // Request more data if we have more capacity. | |
219 if (client_buffer_->forward_capacity() > client_buffer_->forward_bytes()) { | |
220 | |
221 // Before making request to source for data we need to determine the delay | |
222 // (in bytes) for the requested data to be played. | |
223 uint32 buffer_delay = client_buffer_->forward_bytes(); | |
224 pa_usec_t pa_latency_micros; | |
225 int negative; | |
226 pa_stream_get_latency(playback_handle_, &pa_latency_micros, &negative); | |
227 uint32 hardware_delay = MicrosecondsToBytes(pa_latency_micros, sample_rate_, | |
228 bytes_per_frame_); | |
229 // TODO(slock): Deal with negative latency (negative == 1). This has yet to | |
230 // happen in practice though. | |
231 scoped_refptr<media::DataBuffer> packet = | |
232 new media::DataBuffer(packet_size_); | |
233 size_t packet_size = RunDataCallback(packet->GetWritableData(), | |
234 packet->GetBufferSize(), | |
235 AudioBuffersState(buffer_delay, | |
236 hardware_delay)); | |
237 CHECK(packet_size <= packet->GetBufferSize()) << | |
238 "Data source overran buffer."; | |
239 | |
240 // TODO(slock): Swizzling and downmixing. | |
241 //volume_ = volume_ * 65536.0; //pa_sw_volume_from_linear(volume_); | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
?
slock
2011/08/15 20:35:06
Done. Deleted, was old code from an experiment.
| |
242 media::AdjustVolume(packet->GetWritableData(), | |
243 packet_size, | |
244 ChannelLayoutToChannelCount(channel_layout_), | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
Save ChannelLayoutToChannelCount(channel_layout_)
slock
2011/08/15 20:35:06
Done.
| |
245 bytes_per_frame_ / ChannelLayoutToChannelCount( | |
246 channel_layout_), | |
247 volume_); | |
248 | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
Delete extra blank line.
slock
2011/08/15 20:35:06
Done.
| |
249 | |
250 if (packet_size > 0) { | |
251 packet->SetDataSize(packet_size); | |
252 // Add the packet to the buffer. | |
253 client_buffer_->Append(packet); | |
254 } else { | |
255 source_exhausted_ = true; | |
256 } | |
257 } | |
258 } | |
259 | |
260 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
slock
2011/08/15 20:35:06
Done.
| |
261 CHECK(callback); | |
262 source_callback_ = callback; | |
263 | |
264 // Clear buffer, it might still have data in it. | |
265 client_buffer_->Clear(); | |
266 stream_stopped_ = false; | |
267 source_exhausted_ = false; | |
268 | |
269 // Start playback. | |
270 message_loop_->PostTask( | |
271 FROM_HERE, | |
272 method_factory_.NewRunnableMethod( | |
273 &PulseAudioOutputStream::MainloopIterateTask)); | |
274 } | |
275 | |
276 void PulseAudioOutputStream::Stop() { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
slock
2011/08/15 20:35:06
Done.
| |
277 // Effect will not be instantaneous as the PulseAudio server buffer drains. | |
278 // TODO(slock): Immediate stopping. | |
279 stream_stopped_ = true; | |
280 } | |
281 | |
282 void PulseAudioOutputStream::SetVolume(double volume) { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
slock
2011/08/15 20:35:06
Done.
| |
283 volume_ = static_cast<float>(volume); | |
284 } | |
285 | |
286 void PulseAudioOutputStream::GetVolume(double* volume) { | |
vrk (LEFT CHROMIUM)
2011/08/12 23:16:51
DCHECK_EQ(message_loop_, MessageLoop::current());
slock
2011/08/15 20:35:06
Done.
| |
287 *volume = volume_; | |
288 } | |
289 | |
290 uint32 PulseAudioOutputStream::RunDataCallback( | |
291 uint8* dest, uint32 max_size, AudioBuffersState buffers_state) { | |
292 if (source_callback_) | |
293 return source_callback_->OnMoreData(this, dest, max_size, buffers_state); | |
294 | |
295 return 0; | |
296 } | |
OLD | NEW |