Branch data Line data Source code
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.1 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 <unistd.h>
29 : : #include <fcntl.h>
30 : : #include <errno.h>
31 : :
32 : : #include <sndfile.h>
33 : :
34 : : #include <pulse/xmalloc.h>
35 : : #include <pulse/util.h>
36 : :
37 : : #include <pulsecore/core-error.h>
38 : : #include <pulsecore/sink-input.h>
39 : : #include <pulsecore/log.h>
40 : : #include <pulsecore/thread-mq.h>
41 : : #include <pulsecore/core-util.h>
42 : : #include <pulsecore/sndfile-util.h>
43 : :
44 : : #include "sound-file-stream.h"
45 : :
46 : : #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
47 : :
48 : : typedef struct file_stream {
49 : : pa_msgobject parent;
50 : : pa_core *core;
51 : : pa_sink_input *sink_input;
52 : :
53 : : SNDFILE *sndfile;
54 : : sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
55 : :
56 : : /* We need this memblockq here to easily fulfill rewind requests
57 : : * (even beyond the file start!) */
58 : : pa_memblockq *memblockq;
59 : : } file_stream;
60 : :
61 : : enum {
62 : : FILE_STREAM_MESSAGE_UNLINK
63 : : };
64 : :
65 [ # # ][ # # ]: 0 : PA_DEFINE_PRIVATE_CLASS(file_stream, pa_msgobject);
[ # # ][ # # ]
[ # # ]
66 : : #define FILE_STREAM(o) (file_stream_cast(o))
67 : :
68 : : /* Called from main context */
69 : 0 : static void file_stream_unlink(file_stream *u) {
70 [ # # ]: 0 : pa_assert(u);
71 : :
72 [ # # ]: 0 : if (!u->sink_input)
73 : 0 : return;
74 : :
75 : 0 : pa_sink_input_unlink(u->sink_input);
76 : 0 : pa_sink_input_unref(u->sink_input);
77 : 0 : u->sink_input = NULL;
78 : :
79 : : /* Make sure we don't decrease the ref count twice. */
80 : : file_stream_unref(u);
81 : : }
82 : :
83 : : /* Called from main context */
84 : 0 : static void file_stream_free(pa_object *o) {
85 : 0 : file_stream *u = FILE_STREAM(o);
86 [ # # ]: 0 : pa_assert(u);
87 : :
88 [ # # ]: 0 : if (u->memblockq)
89 : 0 : pa_memblockq_free(u->memblockq);
90 : :
91 [ # # ]: 0 : if (u->sndfile)
92 : 0 : sf_close(u->sndfile);
93 : :
94 : 0 : pa_xfree(u);
95 : 0 : }
96 : :
97 : : /* Called from main context */
98 : 0 : static int file_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
99 : 0 : file_stream *u = FILE_STREAM(o);
100 : 0 : file_stream_assert_ref(u);
101 : :
102 [ # # ]: 0 : switch (code) {
103 : : case FILE_STREAM_MESSAGE_UNLINK:
104 : 0 : file_stream_unlink(u);
105 : 0 : break;
106 : : }
107 : :
108 : 0 : return 0;
109 : : }
110 : :
111 : : /* Called from main context */
112 : 0 : static void sink_input_kill_cb(pa_sink_input *i) {
113 : : file_stream *u;
114 : :
115 : 0 : pa_sink_input_assert_ref(i);
116 : 0 : u = FILE_STREAM(i->userdata);
117 : 0 : file_stream_assert_ref(u);
118 : :
119 : 0 : file_stream_unlink(u);
120 : 0 : }
121 : :
122 : : /* Called from IO thread context */
123 : 0 : static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
124 : : file_stream *u;
125 : :
126 : 0 : pa_sink_input_assert_ref(i);
127 : 0 : u = FILE_STREAM(i->userdata);
128 : 0 : file_stream_assert_ref(u);
129 : :
130 : : /* If we are added for the first time, ask for a rewinding so that
131 : : * we are heard right-away. */
132 [ # # ][ # # ]: 0 : if (PA_SINK_INPUT_IS_LINKED(state) &&
133 : 0 : i->thread_info.state == PA_SINK_INPUT_INIT)
134 : 0 : pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
135 : 0 : }
136 : :
137 : : /* Called from IO thread context */
138 : 0 : static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
139 : : file_stream *u;
140 : :
141 : 0 : pa_sink_input_assert_ref(i);
142 [ # # ]: 0 : pa_assert(chunk);
143 : 0 : u = FILE_STREAM(i->userdata);
144 : 0 : file_stream_assert_ref(u);
145 : :
146 [ # # ]: 0 : if (!u->memblockq)
147 : : return -1;
148 : :
149 : : for (;;) {
150 : : pa_memchunk tchunk;
151 : : size_t fs;
152 : : void *p;
153 : : sf_count_t n;
154 : :
155 [ # # ]: 0 : if (pa_memblockq_peek(u->memblockq, chunk) >= 0) {
156 : 0 : chunk->length = PA_MIN(chunk->length, length);
157 : 0 : pa_memblockq_drop(u->memblockq, chunk->length);
158 : 0 : return 0;
159 : : }
160 : :
161 [ # # ]: 0 : if (!u->sndfile)
162 : : break;
163 : :
164 : 0 : tchunk.memblock = pa_memblock_new(i->sink->core->mempool, length);
165 : 0 : tchunk.index = 0;
166 : :
167 : 0 : p = pa_memblock_acquire(tchunk.memblock);
168 : :
169 [ # # ]: 0 : if (u->readf_function) {
170 : 0 : fs = pa_frame_size(&i->sample_spec);
171 : 0 : n = u->readf_function(u->sndfile, p, (sf_count_t) (length/fs));
172 : : } else {
173 : 0 : fs = 1;
174 : 0 : n = sf_read_raw(u->sndfile, p, (sf_count_t) length);
175 : : }
176 : :
177 : 0 : pa_memblock_release(tchunk.memblock);
178 : :
179 [ # # ]: 0 : if (n <= 0) {
180 : 0 : pa_memblock_unref(tchunk.memblock);
181 : :
182 : 0 : sf_close(u->sndfile);
183 : 0 : u->sndfile = NULL;
184 : 0 : break;
185 : : }
186 : :
187 : 0 : tchunk.length = (size_t) n * fs;
188 : :
189 : 0 : pa_memblockq_push(u->memblockq, &tchunk);
190 : 0 : pa_memblock_unref(tchunk.memblock);
191 : 0 : }
192 : :
193 [ # # ]: 0 : if (pa_sink_input_safe_to_remove(i)) {
194 : 0 : pa_memblockq_free(u->memblockq);
195 : 0 : u->memblockq = NULL;
196 : :
197 : 0 : pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), FILE_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
198 : : }
199 : :
200 : : return -1;
201 : : }
202 : :
203 : 0 : static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
204 : : file_stream *u;
205 : :
206 : 0 : pa_sink_input_assert_ref(i);
207 : 0 : u = FILE_STREAM(i->userdata);
208 : 0 : file_stream_assert_ref(u);
209 : :
210 [ # # ]: 0 : if (!u->memblockq)
211 : 0 : return;
212 : :
213 : 0 : pa_memblockq_rewind(u->memblockq, nbytes);
214 : : }
215 : :
216 : 0 : static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
217 : : file_stream *u;
218 : :
219 : 0 : pa_sink_input_assert_ref(i);
220 : 0 : u = FILE_STREAM(i->userdata);
221 : 0 : file_stream_assert_ref(u);
222 : :
223 [ # # ]: 0 : if (!u->memblockq)
224 : 0 : return;
225 : :
226 : 0 : pa_memblockq_set_maxrewind(u->memblockq, nbytes);
227 : : }
228 : :
229 : 0 : int pa_play_file(
230 : : pa_sink *sink,
231 : : const char *fname,
232 : : const pa_cvolume *volume) {
233 : :
234 : 0 : file_stream *u = NULL;
235 : : pa_sample_spec ss;
236 : : pa_channel_map cm;
237 : : pa_sink_input_new_data data;
238 : : int fd;
239 : : SF_INFO sfi;
240 : : pa_memchunk silence;
241 : :
242 [ # # ]: 0 : pa_assert(sink);
243 [ # # ]: 0 : pa_assert(fname);
244 : :
245 : 0 : u = pa_msgobject_new(file_stream);
246 : 0 : u->parent.parent.free = file_stream_free;
247 : 0 : u->parent.process_msg = file_stream_process_msg;
248 : 0 : u->core = sink->core;
249 : 0 : u->sink_input = NULL;
250 : 0 : u->sndfile = NULL;
251 : 0 : u->readf_function = NULL;
252 : 0 : u->memblockq = NULL;
253 : :
254 [ # # ]: 0 : if ((fd = pa_open_cloexec(fname, O_RDONLY, 0)) < 0) {
255 : 0 : pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
256 : 0 : goto fail;
257 : : }
258 : :
259 : : /* FIXME: For now we just use posix_fadvise to avoid page faults
260 : : * when accessing the file data. Eventually we should move the
261 : : * file reader into the main event loop and pass the data over the
262 : : * asyncmsgq. */
263 : :
264 : : #ifdef HAVE_POSIX_FADVISE
265 [ # # ]: 0 : if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
266 : 0 : pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
267 : 0 : goto fail;
268 : : } else
269 : 0 : pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
270 : :
271 [ # # ]: 0 : if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED) < 0) {
272 : 0 : pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno));
273 : 0 : goto fail;
274 : : } else
275 : 0 : pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
276 : : #endif
277 : :
278 : : pa_zero(sfi);
279 [ # # ]: 0 : if (!(u->sndfile = sf_open_fd(fd, SFM_READ, &sfi, 1))) {
280 : 0 : pa_log("Failed to open file %s", fname);
281 : 0 : goto fail;
282 : : }
283 : :
284 : 0 : fd = -1;
285 : :
286 [ # # ]: 0 : if (pa_sndfile_read_sample_spec(u->sndfile, &ss) < 0) {
287 : 0 : pa_log("Failed to determine file sample format.");
288 : 0 : goto fail;
289 : : }
290 : :
291 [ # # ]: 0 : if (pa_sndfile_read_channel_map(u->sndfile, &cm) < 0) {
292 [ # # ]: 0 : if (ss.channels > 2)
293 : 0 : pa_log_info("Failed to determine file channel map, synthesizing one.");
294 : 0 : pa_channel_map_init_extend(&cm, ss.channels, PA_CHANNEL_MAP_DEFAULT);
295 : : }
296 : :
297 : 0 : u->readf_function = pa_sndfile_readf_function(&ss);
298 : :
299 : 0 : pa_sink_input_new_data_init(&data);
300 : 0 : pa_sink_input_new_data_set_sink(&data, sink, FALSE);
301 : 0 : data.driver = __FILE__;
302 : 0 : pa_sink_input_new_data_set_sample_spec(&data, &ss);
303 : 0 : pa_sink_input_new_data_set_channel_map(&data, &cm);
304 : 0 : pa_sink_input_new_data_set_volume(&data, volume);
305 : 0 : pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, pa_path_get_filename(fname));
306 : 0 : pa_proplist_sets(data.proplist, PA_PROP_MEDIA_FILENAME, fname);
307 : 0 : pa_sndfile_init_proplist(u->sndfile, data.proplist);
308 : :
309 : 0 : pa_sink_input_new(&u->sink_input, sink->core, &data);
310 : 0 : pa_sink_input_new_data_done(&data);
311 : :
312 [ # # ]: 0 : if (!u->sink_input)
313 : : goto fail;
314 : :
315 : 0 : u->sink_input->pop = sink_input_pop_cb;
316 : 0 : u->sink_input->process_rewind = sink_input_process_rewind_cb;
317 : 0 : u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
318 : 0 : u->sink_input->kill = sink_input_kill_cb;
319 : 0 : u->sink_input->state_change = sink_input_state_change_cb;
320 : 0 : u->sink_input->userdata = u;
321 : :
322 : 0 : pa_sink_input_get_silence(u->sink_input, &silence);
323 : 0 : u->memblockq = pa_memblockq_new("sound-file-stream memblockq", 0, MEMBLOCKQ_MAXLENGTH, 0, &ss, 1, 1, 0, &silence);
324 : 0 : pa_memblock_unref(silence.memblock);
325 : :
326 : 0 : pa_sink_input_put(u->sink_input);
327 : :
328 : : /* The reference to u is dangling here, because we want to keep
329 : : * this stream around until it is fully played. */
330 : :
331 : 0 : return 0;
332 : :
333 : : fail:
334 : : file_stream_unref(u);
335 : :
336 [ # # ]: 0 : if (fd >= 0)
337 : 0 : pa_close(fd);
338 : :
339 : : return -1;
340 : : }
|