]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file-stream.c
move flat volume logic into the core. while doing so add n_volume_steps field to...
[pulseaudio] / src / pulsecore / sound-file-stream.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2008 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32
33 #include <sndfile.h>
34
35 #include <pulse/xmalloc.h>
36 #include <pulse/util.h>
37
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/sink-input.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/thread-mq.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/sample-util.h>
44
45 #include "sound-file-stream.h"
46
47 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
48
49 typedef struct file_stream {
50 pa_msgobject parent;
51 pa_core *core;
52 pa_sink_input *sink_input;
53
54 SNDFILE *sndfile;
55 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
56
57 /* We need this memblockq here to easily fulfill rewind requests
58 * (even beyond the file start!) */
59 pa_memblockq *memblockq;
60 } file_stream;
61
62 enum {
63 FILE_STREAM_MESSAGE_UNLINK
64 };
65
66 PA_DECLARE_CLASS(file_stream);
67 #define FILE_STREAM(o) (file_stream_cast(o))
68 static PA_DEFINE_CHECK_TYPE(file_stream, pa_msgobject);
69
70 /* Called from main context */
71 static void file_stream_unlink(file_stream *u) {
72 pa_assert(u);
73
74 if (!u->sink_input)
75 return;
76
77 pa_sink_input_unlink(u->sink_input);
78 pa_sink_input_unref(u->sink_input);
79 u->sink_input = NULL;
80
81 /* Make sure we don't decrease the ref count twice. */
82 file_stream_unref(u);
83 }
84
85 /* Called from main context */
86 static void file_stream_free(pa_object *o) {
87 file_stream *u = FILE_STREAM(o);
88 pa_assert(u);
89
90 if (u->memblockq)
91 pa_memblockq_free(u->memblockq);
92
93 if (u->sndfile)
94 sf_close(u->sndfile);
95
96 pa_xfree(u);
97 }
98
99 /* Called from main context */
100 static int file_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
101 file_stream *u = FILE_STREAM(o);
102 file_stream_assert_ref(u);
103
104 switch (code) {
105 case FILE_STREAM_MESSAGE_UNLINK:
106 file_stream_unlink(u);
107 break;
108 }
109
110 return 0;
111 }
112
113 /* Called from main context */
114 static void sink_input_kill_cb(pa_sink_input *i) {
115 file_stream *u;
116
117 pa_sink_input_assert_ref(i);
118 u = FILE_STREAM(i->userdata);
119 file_stream_assert_ref(u);
120
121 file_stream_unlink(u);
122 }
123
124 /* Called from IO thread context */
125 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
126 file_stream *u;
127
128 pa_sink_input_assert_ref(i);
129 u = FILE_STREAM(i->userdata);
130 file_stream_assert_ref(u);
131
132 /* If we are added for the first time, ask for a rewinding so that
133 * we are heard right-away. */
134 if (PA_SINK_INPUT_IS_LINKED(state) &&
135 i->thread_info.state == PA_SINK_INPUT_INIT)
136 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
137 }
138
139 /* Called from IO thread context */
140 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
141 file_stream *u;
142
143 pa_sink_input_assert_ref(i);
144 pa_assert(chunk);
145 u = FILE_STREAM(i->userdata);
146 file_stream_assert_ref(u);
147
148 if (!u->memblockq)
149 return -1;
150
151 for (;;) {
152 pa_memchunk tchunk;
153 size_t fs;
154 void *p;
155 sf_count_t n;
156
157 if (pa_memblockq_peek(u->memblockq, chunk) >= 0) {
158 chunk->length = PA_MIN(chunk->length, length);
159 pa_memblockq_drop(u->memblockq, chunk->length);
160 return 0;
161 }
162
163 if (!u->sndfile)
164 break;
165
166 tchunk.memblock = pa_memblock_new(i->sink->core->mempool, length);
167 tchunk.index = 0;
168
169 p = pa_memblock_acquire(tchunk.memblock);
170
171 if (u->readf_function) {
172 fs = pa_frame_size(&i->sample_spec);
173 n = u->readf_function(u->sndfile, p, (sf_count_t) (length/fs));
174 } else {
175 fs = 1;
176 n = sf_read_raw(u->sndfile, p, (sf_count_t) length);
177 }
178
179 pa_memblock_release(tchunk.memblock);
180
181 if (n <= 0) {
182 pa_memblock_unref(tchunk.memblock);
183
184 sf_close(u->sndfile);
185 u->sndfile = NULL;
186 break;
187 }
188
189 tchunk.length = (size_t) n * fs;
190
191 pa_memblockq_push(u->memblockq, &tchunk);
192 pa_memblock_unref(tchunk.memblock);
193 }
194
195 if (pa_sink_input_safe_to_remove(i)) {
196 pa_memblockq_free(u->memblockq);
197 u->memblockq = NULL;
198
199 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), FILE_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
200 }
201
202 return -1;
203 }
204
205 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
206 file_stream *u;
207
208 pa_sink_input_assert_ref(i);
209 u = FILE_STREAM(i->userdata);
210 file_stream_assert_ref(u);
211
212 if (!u->memblockq)
213 return;
214
215 pa_memblockq_rewind(u->memblockq, nbytes);
216 }
217
218 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
219 file_stream *u;
220
221 pa_sink_input_assert_ref(i);
222 u = FILE_STREAM(i->userdata);
223 file_stream_assert_ref(u);
224
225 if (!u->memblockq)
226 return;
227
228 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
229 }
230
231 int pa_play_file(
232 pa_sink *sink,
233 const char *fname,
234 const pa_cvolume *volume) {
235
236 file_stream *u = NULL;
237 SF_INFO sfinfo;
238 pa_sample_spec ss;
239 pa_sink_input_new_data data;
240 int fd;
241
242 pa_assert(sink);
243 pa_assert(fname);
244
245 u = pa_msgobject_new(file_stream);
246 u->parent.parent.free = file_stream_free;
247 u->parent.process_msg = file_stream_process_msg;
248 u->core = sink->core;
249 u->sink_input = NULL;
250 u->sndfile = NULL;
251 u->readf_function = NULL;
252 u->memblockq = NULL;
253
254 memset(&sfinfo, 0, sizeof(sfinfo));
255
256 if ((fd = open(fname, O_RDONLY
257 #ifdef O_NOCTTY
258 |O_NOCTTY
259 #endif
260 )) < 0) {
261 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
262 goto fail;
263 }
264
265 /* FIXME: For now we just use posix_fadvise to avoid page faults
266 * when accessing the file data. Eventually we should move the
267 * file reader into the main event loop and pass the data over the
268 * asyncmsgq. */
269
270 #ifdef HAVE_POSIX_FADVISE
271 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
272 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
273 goto fail;
274 } else
275 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
276
277 if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED) < 0) {
278 pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno));
279 goto fail;
280 } else
281 pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
282 #endif
283
284 if (!(u->sndfile = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
285 pa_log("Failed to open file %s", fname);
286 pa_close(fd);
287 goto fail;
288 }
289
290 switch (sfinfo.format & 0xFF) {
291 case SF_FORMAT_PCM_16:
292 case SF_FORMAT_PCM_U8:
293 case SF_FORMAT_PCM_S8:
294 ss.format = PA_SAMPLE_S16NE;
295 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
296 break;
297
298 case SF_FORMAT_ULAW:
299 ss.format = PA_SAMPLE_ULAW;
300 break;
301
302 case SF_FORMAT_ALAW:
303 ss.format = PA_SAMPLE_ALAW;
304 break;
305
306 case SF_FORMAT_FLOAT:
307 default:
308 ss.format = PA_SAMPLE_FLOAT32NE;
309 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
310 break;
311 }
312
313 ss.rate = (uint32_t) sfinfo.samplerate;
314 ss.channels = (uint8_t) sfinfo.channels;
315
316 if (!pa_sample_spec_valid(&ss)) {
317 pa_log("Unsupported sample format in file %s", fname);
318 goto fail;
319 }
320
321 pa_sink_input_new_data_init(&data);
322 data.sink = sink;
323 data.driver = __FILE__;
324 pa_sink_input_new_data_set_sample_spec(&data, &ss);
325 pa_sink_input_new_data_set_virtual_volume(&data, volume);
326 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, pa_path_get_filename(fname));
327 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_FILENAME, fname);
328
329 u->sink_input = pa_sink_input_new(sink->core, &data, 0);
330 pa_sink_input_new_data_done(&data);
331
332 if (!u->sink_input)
333 goto fail;
334
335 u->sink_input->pop = sink_input_pop_cb;
336 u->sink_input->process_rewind = sink_input_process_rewind_cb;
337 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
338 u->sink_input->kill = sink_input_kill_cb;
339 u->sink_input->state_change = sink_input_state_change_cb;
340 u->sink_input->userdata = u;
341
342 u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
343
344 pa_sink_input_put(u->sink_input);
345
346 /* The reference to u is dangling here, because we want to keep
347 * this stream around until it is fully played. */
348
349 return 0;
350
351 fail:
352 if (u)
353 file_stream_unref(u);
354
355 return -1;
356 }