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