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

Side by Side Diff: media/audio/linux/pulse_output.cc

Issue 7473021: PulseAudio Sound Playback on Linux (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: "Uncommented thing I forgot to uncomment" Created 9 years, 4 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
(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/linux/audio_manager_linux.h"
8 #include "media/base/data_buffer.h"
9 #include "media/base/seekable_buffer.h"
10
11 static pa_sample_format_t BitsToFormat(int bits_per_sample) {
12 switch(bits_per_sample) {
scherkus (not reviewing) 2011/08/10 19:55:30 nit: space before (
slock 2011/08/10 22:41:04 Done.
13 // Unsupported sample formats shown for reference. I am assuming we want
14 // signed and little endian because that is what we gave to ALSA.
15 case 8:
16 return PA_SAMPLE_U8;
17 // Also 8-bits: PA_SAMPLE_ALAW and PA_SAMPLE_ULAW
18 case 16:
19 return PA_SAMPLE_S16LE;
20 // Also 16-bits: PA_SAMPLE_S16BE (big endian).
21 case 24:
22 return PA_SAMPLE_S24LE;
23 // Also 24-bits: PA_SAMPLE_S24BE (big endian).
24 // Other cases: PA_SAMPLE_24_32LE (in LSBs of 32-bit field, little endian),
scherkus (not reviewing) 2011/08/10 19:55:30 align //
slock 2011/08/10 22:41:04 Done.
25 // and PA_SAMPLE_24_32BE (in LSBs of 32-bit field, big endian),
26 case 32:
27 return PA_SAMPLE_S32LE;
28 // Also 32-bits: PA_SAMPLE_S32BE (big endian),
29 // PA_SAMPLE_FLOAT32LE (floating point little endian),
30 // and PA_SAMPLE_FLOAT32BE (floating point big endian).
31 default:
32 return PA_SAMPLE_INVALID;
33 }
34 }
35
36 static size_t MicrosecondsToBytes(
37 uint32 microseconds, uint32 sample_rate, size_t bytes_per_frame) {
38 return microseconds * sample_rate * bytes_per_frame /
39 base::Time::kMicrosecondsPerSecond;
40 }
41
42 void PulseAudioOutputStream::ContextStateCallback(pa_context* context,
43 void* userdata) {
44 int* context_ready = static_cast<int*>(userdata);
45 pa_context_state_t state = pa_context_get_state(context);
46 switch(state) {
Sergey Ulanov 2011/08/10 17:36:32 nit: space after switch
slock 2011/08/10 22:41:04 This code no longer exists.
47 default:
Sergey Ulanov 2011/08/10 17:36:32 nit: default is usually at the end of switch state
slock 2011/08/10 22:41:04 This code no longer exists.
48 break;
49 case PA_CONTEXT_FAILED:
50 *context_ready = 3;
Sergey Ulanov 2011/08/10 17:36:32 need break after that line.
Sergey Ulanov 2011/08/10 17:36:32 nit: this should be indented 2 spaces relative to
slock 2011/08/10 22:41:04 This code no longer exists.
slock 2011/08/10 22:41:04 This code no longer exists.
51 case PA_CONTEXT_TERMINATED:
52 *context_ready = 2;
53 case PA_CONTEXT_READY:
54 *context_ready = 0;
55 }
56 }
57
58 void PulseAudioOutputStream::WriteCallback(pa_stream* stream, size_t length,
59 void* userdata) {
60 PulseAudioOutputStream* stream_ptr =
61 static_cast<PulseAudioOutputStream*>(userdata);
62
63 // Request data from upstream if necessary.
64 while (stream_ptr->client_buffer_->forward_bytes() < length &&
65 !stream_ptr->source_exhausted_) {
66 stream_ptr->BufferPacketInClient();
67 }
68
69 // Get data to write.
70 scoped_array<uint8> read_data(new uint8[length]);
71 stream_ptr->client_buffer_->Read(read_data.get(), length);
72 // Write to stream.
73 pa_stream_write(stream, read_data.get(), length, NULL, 0LL, PA_SEEK_RELATIVE);
74 }
75
76 PulseAudioOutputStream::PulseAudioOutputStream(const AudioParameters& params,
77 AudioManagerLinux* manager)
78 : channel_layout_(params.channel_layout),
79 sample_format_(BitsToFormat(params.bits_per_sample)),
80 sample_rate_(params.sample_rate),
81 bytes_per_frame_(params.channels * params.bits_per_sample / 8),
82 packet_size_(params.GetPacketSize()),
83 frames_per_packet_(packet_size_ / bytes_per_frame_),
84 stream_stopped_(false),
85 manager_(manager),
86 pa_mainloop_(NULL),
87 pa_mainloop_api_(NULL),
88 pa_context_(NULL),
89 playback_handle_(NULL),
90 client_buffer_(NULL),
91 source_exhausted_(false),
92 source_callback_(NULL) {
93 // TODO(slock): Sanity check input values.
94 }
95
96 PulseAudioOutputStream::~PulseAudioOutputStream() {
97 // All internal structures are already freed in Close(), which calls
98 // AudioManagerLinux::Release which deletes this object.
99 }
100
101 bool PulseAudioOutputStream::Open() {
102 // TODO(slock): Possibly move most of this to a OpenPlaybackDevice function in
103 // 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_get_api(pa_mainloop_);
108 pa_context_ = pa_context_new(pa_mainloop_api_, "Chromium");
109 pa_context_connect(pa_context_, NULL, PA_CONTEXT_NOFLAGS, NULL);
110
111 // Wait until PulseAudio is ready.
112 int pa_context_ready = 1;
113 pa_context_set_state_callback(pa_context_, &ContextStateCallback,
114 &pa_context_ready);
115 while (pa_context_ready == 1)
116 pa_mainloop_iterate(pa_mainloop_, 1, NULL);
117 if (pa_context_ready != 0) {
118 stream_stopped_ = false;
119 return false;
120 }
121
122 // Set sample specifications and open playback stream.
123 pa_sample_specs_.format = sample_format_;
124 pa_sample_specs_.rate = sample_rate_;
125 pa_sample_specs_.channels = ChannelLayoutToChannelCount(channel_layout_);
126 playback_handle_ = pa_stream_new(pa_context_, "Playback",
127 &pa_sample_specs_, NULL);
128
129 // Initialize client buffer.
130 uint32 output_packet_size = frames_per_packet_ * bytes_per_frame_;
131 client_buffer_.reset(new media::SeekableBuffer(0, output_packet_size));
132
133 // Set write callback.
134 pa_stream_set_write_callback(playback_handle_, &WriteCallback, this);
135
136 // Set server side buffer attributes.
137 // TODO(slock): Figure out what these values should actually be, for now use
138 // recommended values from PulseAudio's documentation:
139 // http://freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.h tml
140 pa_buffer_attributes_.maxlength = (uint32_t)-1;
scherkus (not reviewing) 2011/08/10 19:55:30 static_cast<> instead of c-style cast
slock 2011/08/10 22:41:04 Done.
141 pa_buffer_attributes_.tlength = output_packet_size;
142 pa_buffer_attributes_.prebuf = (uint32_t)-1;
143 pa_buffer_attributes_.minreq = (uint32_t)-1;
144 pa_buffer_attributes_.fragsize = (uint32_t)-1;
145
146 // Set volume
147 pa_volume_.channels = ChannelLayoutToChannelCount(channel_layout_);
148 pa_cvolume_set(&pa_volume_, pa_volume_.channels, PA_VOLUME_NORM);
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),
Sergey Ulanov 2011/08/10 17:36:32 nit: operator must be on the previous line
slock 2011/08/10 22:41:04 Done.
157 &pa_volume_, NULL);
158
159 if (!playback_handle_) {
160 stream_stopped_ = true;
161 return false;
162 }
163
164 return true;
165 }
166
167 void PulseAudioOutputStream::Close() {
168 // Close the device.
169 if (playback_handle_) {
170 pa_stream_flush(playback_handle_, NULL, NULL);
171 pa_stream_disconnect(playback_handle_);
172
173 // Release PulseAudio structures.
174 pa_stream_unref(playback_handle_);
175 }
176 if (pa_context_)
177 pa_context_unref(pa_context_);
178 if (pa_mainloop_)
179 pa_mainloop_free(pa_mainloop_);
180
181 // Release internal buffer.
182 client_buffer_.reset();
183
184 // Signal to the manager that we're closed and can be removed.
185 // This should be the last call in the function as it deletes "this".
186 manager_->ReleaseOutputStream(this);
187 }
188
189 void PulseAudioOutputStream::BufferPacketInClient() {
190 // Request more data if we have more capacity.
191 if (client_buffer_->forward_capacity() > client_buffer_->forward_bytes()) {
192
193 // Before making request to source for data we need to determine the delay
194 // (in bytes) for the requested data to be played.
195 uint32 buffer_delay = client_buffer_->forward_bytes();
196 pa_usec_t pa_latency_micros;
197 int negative;
198 pa_stream_get_latency(playback_handle_, &pa_latency_micros, &negative);
199 uint32 hardware_delay = MicrosecondsToBytes(pa_latency_micros, sample_rate_,
200 bytes_per_frame_);
201 // TODO(slock): Deal with negative latency (negative == 1). This has yet to
202 // happen in practice though.
203 scoped_refptr<media::DataBuffer> packet =
204 new media::DataBuffer(packet_size_);
205 size_t packet_size = RunDataCallback(packet->GetWritableData(),
206 packet->GetBufferSize(),
207 AudioBuffersState(buffer_delay,
208 hardware_delay));
209 CHECK(packet_size <= packet->GetBufferSize()) <<
210 "Data source overran buffer.";
211
212 // TODO(slock): Swizzling, downmixing, and volume adjusting.
213
214 if (packet_size > 0) {
215 packet->SetDataSize(packet_size);
216 // Add the packet to the buffer.
217 client_buffer_->Append(packet);
218 } else {
219 source_exhausted_ = true;
220 }
221 }
222 }
223
224 void PulseAudioOutputStream::ClientBufferLoop() {
225 while(!stream_stopped_ && !source_exhausted_) {
scherkus (not reviewing) 2011/08/10 19:55:30 nit: space before (
slock 2011/08/10 22:41:04 Done.
226 // As long as the stream is active, we should be buffering packets if need
227 // be and writing packets if need be. These are asynchronous processes.
228 // This loop buffers packets and the PulseAudio mainloop writes them.
229 // BufferPacket() only actually buffers under certain circumstances and
230 // pa_mainloop_iterate() only calls WriteCallback under certain
231 // circumstances, but the loop marches on in either case.
232 pa_mainloop_iterate(pa_mainloop_, 1, NULL);
233 }
234 }
235
236 void PulseAudioOutputStream::Start(AudioSourceCallback* callback) {
237 CHECK(callback);
238 source_callback_ = callback;
239
240 // Clear buffer, it might still have data in it.
241 client_buffer_->Clear();
242 source_exhausted_ = false;
243
244 // Start playing.
245 ClientBufferLoop();
246 }
247
248 void PulseAudioOutputStream::Stop() {
249 // Effect will not be instantaneous as the PulseAudio server buffer drains.
250 // TODO(slock): Immediate stopping.
251 stream_stopped_ = true;
252 }
253
254 void PulseAudioOutputStream::SetVolume(double volume) {
255 pa_volume_t new_volume = pa_sw_volume_from_linear(volume);
256 pa_cvolume_set(&pa_volume_, pa_volume_.channels, new_volume);
257 }
258
259 void PulseAudioOutputStream::GetVolume(double* volume) {
260 // We do not allow volume changes on a per-channel basis, so all channels will
261 // always have the same volume and the average will reflect this.
262 *volume = pa_cvolume_avg(&pa_volume_);
263 }
264
265 uint32 PulseAudioOutputStream::RunDataCallback(
266 uint8* dest, uint32 max_size, AudioBuffersState buffers_state) {
267 if (source_callback_)
268 return source_callback_->OnMoreData(this, dest, max_size, buffers_state);
269
270 return 0;
271 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698