virtual-sink: Fix a crash when moving the sink to a new master right after setup.
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / pulsecore / sound-file.c
blobd8c10b1e2ffc23fc440071f402d44bbd9e49dead
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
31 #include <sndfile.h>
33 #include <pulse/sample.h>
34 #include <pulsecore/log.h>
35 #include <pulsecore/macro.h>
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/core-scache.h>
39 #include <pulsecore/sndfile-util.h>
41 #include "sound-file.h"
43 int pa_sound_file_load(
44 pa_mempool *pool,
45 const char *fname,
46 pa_sample_spec *ss,
47 pa_channel_map *map,
48 pa_memchunk *chunk,
49 pa_proplist *p) {
51 SNDFILE *sf = NULL;
52 SF_INFO sfi;
53 int ret = -1;
54 size_t l;
55 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
56 void *ptr = NULL;
57 int fd;
59 pa_assert(fname);
60 pa_assert(ss);
61 pa_assert(chunk);
63 pa_memchunk_reset(chunk);
65 if ((fd = pa_open_cloexec(fname, O_RDONLY, 0)) < 0) {
66 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
67 goto finish;
70 #ifdef HAVE_POSIX_FADVISE
71 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
72 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
73 goto finish;
74 } else
75 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
76 #endif
78 pa_zero(sfi);
79 if (!(sf = sf_open_fd(fd, SFM_READ, &sfi, 1))) {
80 pa_log("Failed to open file %s", fname);
81 goto finish;
84 fd = -1;
86 if (pa_sndfile_read_sample_spec(sf, ss) < 0) {
87 pa_log("Failed to determine file sample format.");
88 goto finish;
91 if ((map && pa_sndfile_read_channel_map(sf, map) < 0)) {
92 if (ss->channels > 2)
93 pa_log("Failed to determine file channel map, synthesizing one.");
94 pa_channel_map_init_extend(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
97 if (p)
98 pa_sndfile_init_proplist(sf, p);
100 if ((l = pa_frame_size(ss) * (size_t) sfi.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
101 pa_log("File too large");
102 goto finish;
105 chunk->memblock = pa_memblock_new(pool, l);
106 chunk->index = 0;
107 chunk->length = l;
109 readf_function = pa_sndfile_readf_function(ss);
111 ptr = pa_memblock_acquire(chunk->memblock);
113 if ((readf_function && readf_function(sf, ptr, sfi.frames) != sfi.frames) ||
114 (!readf_function && sf_read_raw(sf, ptr, (sf_count_t) l) != (sf_count_t) l)) {
115 pa_log("Premature file end");
116 goto finish;
119 ret = 0;
121 finish:
123 if (sf)
124 sf_close(sf);
126 if (ptr)
127 pa_memblock_release(chunk->memblock);
129 if (ret != 0 && chunk->memblock)
130 pa_memblock_unref(chunk->memblock);
132 if (fd >= 0)
133 pa_close(fd);
135 return ret;
138 int pa_sound_file_too_big_to_cache(const char *fname) {
140 SNDFILE*sf = NULL;
141 SF_INFO sfi;
142 pa_sample_spec ss;
144 pa_assert(fname);
146 pa_zero(sfi);
147 if (!(sf = sf_open(fname, SFM_READ, &sfi))) {
148 pa_log("Failed to open file %s", fname);
149 return -1;
152 if (pa_sndfile_read_sample_spec(sf, &ss) < 0) {
153 pa_log("Failed to determine file sample format.");
154 sf_close(sf);
155 return -1;
158 sf_close(sf);
160 if ((pa_frame_size(&ss) * (size_t) sfi.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
161 pa_log("File too large: %s", fname);
162 return 1;
165 return 0;