Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 593 lines (537 sloc) 21.346 kb
e9c07dd @hzeller o add documentation
authored
1 // Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
16 #include "folve-filesystem.h"
17
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
18 #include <FLAC/metadata.h>
e9c07dd @hzeller o add documentation
authored
19 #include <errno.h>
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
20 #include <fcntl.h>
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
21 #include <sndfile.h>
5830c5a @hzeller o Switch logging to syslog()
authored
22 #include <stdarg.h>
e9c07dd @hzeller o add documentation
authored
23 #include <stdio.h>
24 #include <string.h>
25 #include <strings.h>
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
26 #include <sys/stat.h>
27 #include <sys/types.h>
5830c5a @hzeller o Switch logging to syslog()
authored
28 #include <syslog.h>
e9c07dd @hzeller o add documentation
authored
29 #include <unistd.h>
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
30
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
31 #include <map>
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
32 #include <string>
6c6dedb @hzeller o Some readme tweaks and readability improvements.
authored
33 #include <zita-convolver.h>
e9c07dd @hzeller o add documentation
authored
34
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
35 #include "conversion-buffer.h"
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
36 #include "file-handler-cache.h"
37 #include "file-handler.h"
8268c37 @hzeller o Bubble up error messages in status server.
authored
38 #include "util.h"
6c6dedb @hzeller o Some readme tweaks and readability improvements.
authored
39
fd65cee @hzeller o add basic zita filter.
authored
40 #include "zita-config.h"
41
8268c37 @hzeller o Bubble up error messages in status server.
authored
42 using folve::Appendf;
0aee3bb @hzeller o Show base dir that is mounted.
authored
43
8268c37 @hzeller o Bubble up error messages in status server.
authored
44 static bool global_debug = false;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
45
5830c5a @hzeller o Switch logging to syslog()
authored
46 static void DebugLogf(const char *format, ...) {
47 if (!global_debug) return;
48 va_list ap;
49 va_start(ap, format);
50 vsyslog(LOG_DEBUG, format, ap);
51 va_end(ap);
52 }
b5c1e96 @hzeller o more useful output on status server.
authored
53
e9c07dd @hzeller o add documentation
authored
54 namespace {
55
5476cc2 @hzeller o provide absolute path.
authored
56 // Very simple filter that just passes the original file through. Used for
57 // everything that is not a sound-file.
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
58 class PassThroughFilter : public FileHandler {
e9c07dd @hzeller o add documentation
authored
59 public:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
60 PassThroughFilter(int filedes, int filter_id,
61 const HandlerStats &known_stats)
62 : FileHandler(filter_id), filedes_(filedes), info_stats_(known_stats) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
63 info_stats_.message.append("; pass through.");
5830c5a @hzeller o Switch logging to syslog()
authored
64 DebugLogf("Creating PassThrough filter for '%s'",
65 known_stats.filename.c_str());
e9c07dd @hzeller o add documentation
authored
66 }
680f590 @hzeller o report opening error.
authored
67 ~PassThroughFilter() { close(filedes_); }
68
e9c07dd @hzeller o add documentation
authored
69 virtual int Read(char *buf, size_t size, off_t offset) {
70 const int result = pread(filedes_, buf, size, offset);
71 return result == -1 ? -errno : result;
72 }
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
73 virtual int Stat(struct stat *st) {
74 return fstat(filedes_, st);
75 }
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
76 virtual void GetHandlerStatus(struct HandlerStats *stats) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
77 *stats = info_stats_;
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
78 }
e9c07dd @hzeller o add documentation
authored
79
80 private:
81 const int filedes_;
8268c37 @hzeller o Bubble up error messages in status server.
authored
82 HandlerStats info_stats_;
e9c07dd @hzeller o add documentation
authored
83 };
84
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
85 class SndFileHandler :
86 public FileHandler,
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
87 public ConversionBuffer::SoundSource {
e9c07dd @hzeller o add documentation
authored
88 public:
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
89 // Attempt to create a SndFileHandler from the given file descriptor. This
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
90 // returns NULL if this is not a sound-file or if there is no available
91 // convolution filter configuration available.
8268c37 @hzeller o Bubble up error messages in status server.
authored
92 // "partial_file_info" will be set to information known so far, including
93 // error message.
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
94 static FileHandler *Create(int filedes, const char *fs_path,
8268c37 @hzeller o Bubble up error messages in status server.
authored
95 const char *underlying_file,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
96 int filter_id,
8268c37 @hzeller o Bubble up error messages in status server.
authored
97 const std::string &zita_config_dir,
98 HandlerStats *partial_file_info) {
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
99 SF_INFO in_info;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
100 memset(&in_info, 0, sizeof(in_info));
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
101 SNDFILE *snd = sf_open_fd(filedes, SFM_READ, &in_info, 0);
102 if (snd == NULL) {
5830c5a @hzeller o Switch logging to syslog()
authored
103 syslog(LOG_ERR, "File %s: %s", underlying_file, sf_strerror(NULL));
8268c37 @hzeller o Bubble up error messages in status server.
authored
104 partial_file_info->message = sf_strerror(NULL);
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
105 return NULL;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
106 }
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
107
7e756eb @hzeller o Fix problem with re-initializing the convproc. Turns out that the conf...
authored
108 int bits = 16;
31bd9ce @hzeller o README update.
authored
109 if ((in_info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_24) bits = 24;
110 if ((in_info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_32) bits = 32;
e2619a6 @hzeller o skipping debug message.
authored
111
8268c37 @hzeller o Bubble up error messages in status server.
authored
112 // Remember whatever we could got to know in the partial file info.
113 Appendf(&partial_file_info->format, "%.1fkHz, %d Bit",
114 in_info.samplerate / 1000.0, bits);
115 partial_file_info->duration_seconds = in_info.frames / in_info.samplerate;
116
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
117 std::string config_path;
118 Appendf(&config_path, "%s/filter-%d-%d-%d.conf", zita_config_dir.c_str(),
119 in_info.samplerate, bits, in_info.channels);
8268c37 @hzeller o Bubble up error messages in status server.
authored
120 const bool found_config = (access(config_path.c_str(), R_OK) == 0);
a2aa9a4 @hzeller o let error messages make it to the console.
authored
121 if (found_config) {
5830c5a @hzeller o Switch logging to syslog()
authored
122 DebugLogf("File %s: filter config %s", underlying_file,
123 config_path.c_str());
a2aa9a4 @hzeller o let error messages make it to the console.
authored
124 } else {
5830c5a @hzeller o Switch logging to syslog()
authored
125 syslog(LOG_ERR, "File %s: couldn't find filter config %s",
126 underlying_file, config_path.c_str());
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
127 partial_file_info->message = "Missing [" + config_path + "]";
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
128 sf_close(snd);
129 return NULL;
b5c1e96 @hzeller o more useful output on status server.
authored
130 }
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
131 return new SndFileHandler(fs_path, filter_id,
132 underlying_file, filedes, snd, in_info,
8268c37 @hzeller o Bubble up error messages in status server.
authored
133 *partial_file_info, config_path);
e9c07dd @hzeller o add documentation
authored
134 }
135
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
136 virtual ~SndFileHandler() {
680f590 @hzeller o report opening error.
authored
137 Close();
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
138 if (zita_.convproc) {
d10c1b0 @hzeller o test static requirement.
authored
139 zita_.convproc->stop_process();
140 zita_.convproc->cleanup();
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
141 delete zita_.convproc;
142 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
143 delete output_buffer_;
144 delete [] raw_sample_buffer_;
145 }
146
e9c07dd @hzeller o add documentation
authored
147 virtual int Read(char *buf, size_t size, off_t offset) {
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
148 if (error_) return -1;
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
149 // If this is a skip suspiciously at the very end of the file as
150 // reported by stat, we don't do any encoding, just return garbage.
1118ad9 @hzeller o comment update.
authored
151 // (otherwise we'd to convolve up to that point).
152 //
153 // While indexing, media players do this sometimes apparently.
154 // And sometimes not even to the very end but 'almost' at the end.
155 // So add some FudeOverhang
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
156 static const int kFudgeOverhang = 512;
abb0471 @hzeller o remove debug message from last commit.
authored
157 // But of course only if this is really a skip, not a regular approaching
158 // end-of-file.
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
159 if (output_buffer_->FileSize() < offset
160 && (int) (offset + size + kFudgeOverhang) >= file_stat_.st_size) {
161 const int pretended_bytes = std::min((off_t)size,
162 file_stat_.st_size - offset);
163 if (pretended_bytes > 0) {
164 memset(buf, 0x00, pretended_bytes);
165 return pretended_bytes;
680f590 @hzeller o report opening error.
authored
166 } else {
167 return 0;
168 }
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
169 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
170 // The following read might block and call WriteToSoundfile() until the
171 // buffer is filled.
172 return output_buffer_->Read(buf, size, offset);
e9c07dd @hzeller o add documentation
authored
173 }
174
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
175 virtual void GetHandlerStatus(struct HandlerStats *stats) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
176 *stats = base_stats_;
b5c1e96 @hzeller o more useful output on status server.
authored
177 const int frames_done = total_frames_ - input_frames_left_;
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
178 if (frames_done == 0 || total_frames_ == 0)
179 stats->progress = 0.0;
180 else
181 stats->progress = 1.0 * frames_done / total_frames_;
b5c1e96 @hzeller o more useful output on status server.
authored
182 }
183
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
184 virtual int Stat(struct stat *st) {
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
185 if (output_buffer_->FileSize() > start_estimating_size_) {
186 const int frames_done = total_frames_ - input_frames_left_;
187 if (frames_done > 0) {
188 const float estimated_end = 1.0 * total_frames_ / frames_done;
189 off_t new_size = estimated_end * output_buffer_->FileSize();
190 // Report a bit bigger size which is less harmful than programs
191 // reading short.
192 new_size += 16384;
453489f @hzeller o smallish whitespace changes.
authored
193 if (new_size > file_stat_.st_size) { // Only go forward in size.
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
194 file_stat_.st_size = new_size;
195 }
196 }
197 }
198 *st = file_stat_;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
199 return 0;
200 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
201
e9c07dd @hzeller o add documentation
authored
202 private:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
203 // TODO(hzeller): trim parameter list.
204 SndFileHandler(const char *fs_path, int filter_id,
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
205 const char *underlying_file, int filedes, SNDFILE *snd_in,
8268c37 @hzeller o Bubble up error messages in status server.
authored
206 const SF_INFO &in_info, const HandlerStats &file_info,
207 const std::string &config_path)
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
208 : FileHandler(filter_id),
209 filedes_(filedes), snd_in_(snd_in), total_frames_(in_info.frames),
8268c37 @hzeller o Bubble up error messages in status server.
authored
210 channels_(in_info.channels), base_stats_(file_info),
211 config_path_(config_path),
b5c1e96 @hzeller o more useful output on status server.
authored
212 error_(false), output_buffer_(NULL),
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
213 snd_out_(NULL),
214 raw_sample_buffer_(NULL), input_frames_left_(in_info.frames) {
215
216 // Initial stat that we're going to report to clients. We'll adapt
217 // the filesize as we see it grow. Some clients continuously monitor
218 // the size of the file to check when to stop.
219 fstat(filedes_, &file_stat_);
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
220 start_estimating_size_ = 0.4 * file_stat_.st_size;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
221
8e36eff @hzeller o comment update.
authored
222 // The flac header we get is more rich than what we can create via
223 // sndfile. So if we have one, just copy it.
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
224 copy_flac_header_verbatim_ = LooksLikeInputIsFlac(in_info, filedes);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
225
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
226 // Initialize zita config, but don't allocate converter quite yet.
227 memset(&zita_, 0, sizeof(zita_));
228 zita_.fsamp = in_info.samplerate;
229 zita_.ninp = in_info.channels;
230 zita_.nout = in_info.channels;
231
232 // Create a conversion buffer that creates a soundfile of a particular
8e36eff @hzeller o comment update.
authored
233 // format that we choose here. Essentially we want to generate mostly what
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
234 // our input is.
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
235 SF_INFO out_info = in_info;
31bd9ce @hzeller o README update.
authored
236 out_info.seekable = 0;
237 if ((in_info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_OGG) {
8e36eff @hzeller o comment update.
authored
238 // If the input was ogg, we're re-coding this to flac, because it
239 // wouldn't let us stream the output.
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
240 out_info.format = SF_FORMAT_FLAC;
f11d8cd @hzeller o implement initial skip mode detection.
authored
241 out_info.format |= SF_FORMAT_PCM_16;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
242 }
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
243 else if ((in_info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) {
244 out_info.format = SF_FORMAT_FLAC; // recode as flac.
245 out_info.format |= SF_FORMAT_PCM_24;
31bd9ce @hzeller o README update.
authored
246 }
247 else { // original format.
248 out_info.format = in_info.format;
249 }
250
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
251 output_buffer_ = new ConversionBuffer(this, out_info);
252 }
253
f8cb9e5 @hzeller o redacting MD5.
authored
254 virtual void SetOutputSoundfile(ConversionBuffer *out_buffer,
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
255 SNDFILE *sndfile) {
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
256 snd_out_ = sndfile;
257 if (snd_out_ == NULL) {
258 error_ = true;
5830c5a @hzeller o Switch logging to syslog()
authored
259 syslog(LOG_ERR, "Opening output: %s", sf_strerror(NULL));
8268c37 @hzeller o Bubble up error messages in status server.
authored
260 base_stats_.message = sf_strerror(NULL);
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
261 return;
262 }
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
263 if (copy_flac_header_verbatim_) {
8e36eff @hzeller o comment update.
authored
264 out_buffer->set_sndfile_writes_enabled(false);
f8cb9e5 @hzeller o redacting MD5.
authored
265 CopyFlacHeader(out_buffer);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
266 } else {
f8cb9e5 @hzeller o redacting MD5.
authored
267 out_buffer->set_sndfile_writes_enabled(true);
8e36eff @hzeller o comment update.
authored
268 GenerateHeaderFromInputFile(out_buffer);
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
269 }
270 // Now flush the header: that way if someone only reads the metadata, then
271 // our AddMoreSoundData() is never called.
8e36eff @hzeller o comment update.
authored
272 // We need to do this even if we copied our own header: that way we make
273 // sure that the sndfile-header is flushed into the nirwana before we
274 // re-enable sndfile_writes.
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
275 sf_command(snd_out_, SFC_UPDATE_HEADER_NOW, NULL, 0);
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
276
277 // -- time for some hackery ...
278 // If we have copied the header over from the original, we need to
279 // redact the values for min/max blocksize and min/max framesize with
280 // what SNDFILE is going to use, otherwise programs will trip over this.
281 // http://flac.sourceforge.net/format.html
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
282 if (copy_flac_header_verbatim_) {
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
283 out_buffer->WriteCharAt((1152 & 0xFF00) >> 8, 8);
284 out_buffer->WriteCharAt((1152 & 0x00FF) , 9);
06a0312 @hzeller o Are we writing bigger blocksizes later while encoding ? To
authored
285 out_buffer->WriteCharAt((32768 & 0xFF00) >> 8, 10);
286 out_buffer->WriteCharAt((32768 & 0x00FF) , 11);
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
287 for (int i = 12; i < 18; ++i) out_buffer->WriteCharAt(0, i);
288 } else {
289 // .. and if SNDFILE writes the header, it misses out in writing the
290 // number of samples to be expected. So let's fill that in.
291 // The MD5 sum starts at position strlen("fLaC") + 4 + 18 = 26
292 // The 32 bits before that are the samples (and another 4 bit before that,
293 // ignoring that for now).
294 out_buffer->WriteCharAt((total_frames_ & 0xFF000000) >> 24, 22);
295 out_buffer->WriteCharAt((total_frames_ & 0x00FF0000) >> 16, 23);
296 out_buffer->WriteCharAt((total_frames_ & 0x0000FF00) >> 8, 24);
297 out_buffer->WriteCharAt((total_frames_ & 0x000000FF), 25);
298 }
f8cb9e5 @hzeller o redacting MD5.
authored
299
300 out_buffer->set_sndfile_writes_enabled(true); // ready for sound-stream.
5830c5a @hzeller o Switch logging to syslog()
authored
301 DebugLogf("Header init done (%s).", base_stats_.filename.c_str());
550c4e0 @hzeller o allow short read in header area, but not in the stream
authored
302 out_buffer->HeaderFinished();
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
303 }
304
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
305 virtual bool AddMoreSoundData() {
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
306 if (!input_frames_left_)
307 return false;
308 if (!zita_.convproc) {
8e36eff @hzeller o comment update.
authored
309 // First time we're called.
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
310 zita_.convproc = new Convproc();
0c8561b @hzeller o Handle configuration file error gracefully.
authored
311 if (config(&zita_, config_path_.c_str()) != 0) {
5830c5a @hzeller o Switch logging to syslog()
authored
312 syslog(LOG_ERR, "filter-config %s is broken. Please fix."
313 "Won't play this stream **\n", config_path_.c_str());
8268c37 @hzeller o Bubble up error messages in status server.
authored
314 base_stats_.message = "Problem parsing " + config_path_;
0c8561b @hzeller o Handle configuration file error gracefully.
authored
315 input_frames_left_ = 0;
a2aa9a4 @hzeller o let error messages make it to the console.
authored
316 Close();
0c8561b @hzeller o Handle configuration file error gracefully.
authored
317 return false;
318 }
f11d8cd @hzeller o implement initial skip mode detection.
authored
319 raw_sample_buffer_ = new float[zita_.fragm * channels_];
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
320 zita_.convproc->start_process(0, 0);
321 }
0706452 @hzeller o Report broken input file in UI.
authored
322 const int r = sf_readf_float(snd_in_, raw_sample_buffer_, zita_.fragm);
9c0ba15 @hzeller o work around broken files - don't endlessloop :)
authored
323 if (r == 0) {
5830c5a @hzeller o Switch logging to syslog()
authored
324 syslog(LOG_ERR, "Expected %d frames left, gave buffer sized %d, "
325 "but got EOF; corrupt file '%s' ?",
326 input_frames_left_, zita_.fragm, base_stats_.filename.c_str());
0706452 @hzeller o Report broken input file in UI.
authored
327 base_stats_.message = "Premature EOF in input file.";
9c0ba15 @hzeller o work around broken files - don't endlessloop :)
authored
328 input_frames_left_ = 0;
329 Close();
330 return false;
331 }
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
332 if (r < (int) zita_.fragm) {
8e36eff @hzeller o comment update.
authored
333 // Zero out the rest of the buffer.
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
334 const int missing = zita_.fragm - r;
335 memset(raw_sample_buffer_ + r * channels_, 0,
336 missing * channels_ * sizeof(float));
fd65cee @hzeller o add basic zita filter.
authored
337 }
338
339 // Separate channels.
340 for (int ch = 0; ch < channels_; ++ch) {
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
341 float *dest = zita_.convproc->inpdata(ch);
fd65cee @hzeller o add basic zita filter.
authored
342 for (int j = 0; j < r; ++j) {
343 dest[j] = raw_sample_buffer_[j * channels_ + ch];
344 }
345 }
346
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
347 zita_.convproc->process();
fd65cee @hzeller o add basic zita filter.
authored
348
349 // Join channels again.
350 for (int ch = 0; ch < channels_; ++ch) {
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
351 float *source = zita_.convproc->outdata(ch);
fd65cee @hzeller o add basic zita filter.
authored
352 for (int j = 0; j < r; ++j) {
353 raw_sample_buffer_[j * channels_ + ch] = source[j];
354 }
355 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
356 sf_writef_float(snd_out_, raw_sample_buffer_, r);
357 input_frames_left_ -= r;
31bd9ce @hzeller o README update.
authored
358 if (input_frames_left_ == 0) {
680f590 @hzeller o report opening error.
authored
359 Close();
31bd9ce @hzeller o README update.
authored
360 }
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
361 return input_frames_left_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
362 }
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
363
8e36eff @hzeller o comment update.
authored
364 void CopyBytes(int fd, off_t pos, ConversionBuffer *out, size_t len) {
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
365 char buf[256];
366 while (len > 0) {
367 ssize_t r = pread(fd, buf, std::min(sizeof(buf), len), pos);
8e36eff @hzeller o comment update.
authored
368 if (r <= 0) return;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
369 out->Append(buf, r);
370 len -= r;
371 pos += r;
372 }
373 }
374
f8cb9e5 @hzeller o redacting MD5.
authored
375 void CopyFlacHeader(ConversionBuffer *out_buffer) {
5830c5a @hzeller o Switch logging to syslog()
authored
376 DebugLogf("Provide FLAC header from original file %s",
377 base_stats_.filename.c_str());
f8cb9e5 @hzeller o redacting MD5.
authored
378 out_buffer->Append("fLaC", 4);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
379 off_t pos = 4;
380 unsigned char header[4];
f8cb9e5 @hzeller o redacting MD5.
authored
381 bool need_finish_padding = false;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
382 while (pread(filedes_, header, sizeof(header), pos) == sizeof(header)) {
383 pos += sizeof(header);
384 bool is_last = header[0] & 0x80;
385 unsigned int type = header[0] & 0x7F;
386 unsigned int byte_len = (header[1] << 16) + (header[2] << 8) + header[3];
5830c5a @hzeller o Switch logging to syslog()
authored
387 DebugLogf(" %02x %02x %02x %02x type: %d, len: %6u %s ",
388 header[0], header[1], header[2], header[3],
389 type, byte_len, is_last ? "(last)" : "(cont)");
f8cb9e5 @hzeller o redacting MD5.
authored
390 need_finish_padding = false;
391 if (type == FLAC__METADATA_TYPE_STREAMINFO && byte_len == 34) {
392 out_buffer->Append(&header, sizeof(header));
8e36eff @hzeller o comment update.
authored
393 // Copy everything but the MD5 at the end - which we set to empty.
f8cb9e5 @hzeller o redacting MD5.
authored
394 CopyBytes(filedes_, pos, out_buffer, byte_len - 16);
395 for (int i = 0; i < 16; ++i) out_buffer->Append("\0", 1);
5830c5a @hzeller o Switch logging to syslog()
authored
396 // TODO append log (copy streaminfo, but redacted MD5)
f8cb9e5 @hzeller o redacting MD5.
authored
397 }
398 else if (type == FLAC__METADATA_TYPE_SEEKTABLE) {
399 // The SEEKTABLE header we skip, because it is bogus after encoding.
5830c5a @hzeller o Switch logging to syslog()
authored
400 // TODO append log (skip the seektable)
f8cb9e5 @hzeller o redacting MD5.
authored
401 need_finish_padding = is_last; // if we were last, force finish block.
402 }
403 else {
404 out_buffer->Append(&header, sizeof(header));
405 CopyBytes(filedes_, pos, out_buffer, byte_len);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
406 }
f8cb9e5 @hzeller o redacting MD5.
authored
407 pos += byte_len;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
408 if (is_last)
409 break;
410 }
f8cb9e5 @hzeller o redacting MD5.
authored
411 if (need_finish_padding) { // if the last block was not is_last: pad.
5830c5a @hzeller o Switch logging to syslog()
authored
412 DebugLogf("write padding");
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
413 memset(&header, 0, sizeof(header));
414 header[0] = 0x80 /* is last */ | FLAC__METADATA_TYPE_PADDING;
f8cb9e5 @hzeller o redacting MD5.
authored
415 out_buffer->Append(&header, sizeof(header));
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
416 }
417 }
418
8e36eff @hzeller o comment update.
authored
419 void GenerateHeaderFromInputFile(ConversionBuffer *out_buffer) {
5830c5a @hzeller o Switch logging to syslog()
authored
420 DebugLogf("Generate header from original ID3-tags.");
8e36eff @hzeller o comment update.
authored
421 out_buffer->set_sndfile_writes_enabled(true);
422 // Copy ID tags that are supported by sndfile.
423 for (int i = SF_STR_FIRST; i <= SF_STR_LAST; ++i) {
424 const char *s = sf_get_string(snd_in_, i);
425 if (s != NULL) {
426 sf_set_string(snd_out_, i, s);
427 }
428 }
429 }
430
680f590 @hzeller o report opening error.
authored
431 void Close() {
432 if (snd_out_ == NULL) return; // done.
433 output_buffer_->set_sndfile_writes_enabled(false);
434 if (snd_in_) sf_close(snd_in_);
435 if (snd_out_) sf_close(snd_out_);
436 snd_out_ = NULL;
437 close(filedes_);
438 }
439
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
440 bool LooksLikeInputIsFlac(const SF_INFO &sndinfo, int filedes) {
441 if ((sndinfo.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
442 return false;
443 // However some files contain flac encoded stuff, but are not flac files
444 // by themselve. So we can't copy headers verbatim. Sanity check header.
445 char flac_magic[4];
446 if (pread(filedes, flac_magic, sizeof(flac_magic), 0) != sizeof(flac_magic))
447 return false;
448 return memcmp(flac_magic, "fLaC", sizeof(flac_magic)) == 0;
449 }
450
e9c07dd @hzeller o add documentation
authored
451 const int filedes_;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
452 SNDFILE *const snd_in_;
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
453 const unsigned int total_frames_;
b5c1e96 @hzeller o more useful output on status server.
authored
454 const int channels_;
8268c37 @hzeller o Bubble up error messages in status server.
authored
455 HandlerStats base_stats_;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
456 const std::string config_path_;
457
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
458 struct stat file_stat_; // we dynamically report a changing size.
459 off_t start_estimating_size_; // essentially const.
460
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
461 bool error_;
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
462 bool copy_flac_header_verbatim_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
463 ConversionBuffer *output_buffer_;
464 SNDFILE *snd_out_;
465
466 // Used in conversion.
467 float *raw_sample_buffer_;
468 int input_frames_left_;
d10c1b0 @hzeller o test static requirement.
authored
469 ZitaConfig zita_;
e9c07dd @hzeller o add documentation
authored
470 };
471 } // namespace
472
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
473
8268c37 @hzeller o Bubble up error messages in status server.
authored
474 FileHandler *FolveFilesystem::CreateFromDescriptor(int filedes,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
475 int cfg_idx,
8268c37 @hzeller o Bubble up error messages in status server.
authored
476 const char *fs_path,
477 const char *underlying_file) {
478 HandlerStats file_info;
479 file_info.filename = fs_path;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
480 if (cfg_idx != 0) {
481 FileHandler *filter = SndFileHandler::Create(filedes, fs_path,
482 underlying_file,
483 cfg_idx,
484 config_dirs()[cfg_idx],
485 &file_info);
486 if (filter != NULL) return filter;
487 } else {
488 file_info.message = "No filter config selected.";
489 }
e9c07dd @hzeller o add documentation
authored
490
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
491 // Every other file-type is just passed through as is.
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
492 return new PassThroughFilter(filedes, cfg_idx, file_info);
493 }
494
495 std::string FolveFilesystem::CacheKey(int config_idx, const char *fs_path) {
496 std::string result;
497 Appendf(&result, "%d/%s", config_idx, fs_path);
498 return result;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
499 }
500
501 // Implementation of the C functions in filter-interface.h
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
502 FileHandler *FolveFilesystem::CreateHandler(const char *fs_path,
503 const char *underlying_path) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
504 const int config_idx = current_cfg_index_;
505 const std::string cache_key = CacheKey(config_idx, fs_path);
506 FileHandler *handler = open_file_cache_.FindAndPin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
507 if (handler == NULL) {
508 int filedes = open(underlying_path, O_RDONLY);
680f590 @hzeller o report opening error.
authored
509 if (filedes < 0)
510 return NULL;
b5c1e96 @hzeller o more useful output on status server.
authored
511 ++total_file_openings_;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
512 handler = CreateFromDescriptor(filedes, config_idx,
513 fs_path, underlying_path);
514 handler = open_file_cache_.InsertPinned(cache_key, handler);
b5c1e96 @hzeller o more useful output on status server.
authored
515 } else {
516 ++total_file_reopen_;
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
517 }
518 return handler;
e9c07dd @hzeller o add documentation
authored
519 }
520
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
521 int FolveFilesystem::StatByFilename(const char *fs_path, struct stat *st) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
522 const std::string cache_key = CacheKey(current_cfg_index_, fs_path);
523 FileHandler *handler = open_file_cache_.FindAndPin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
524 if (handler == 0)
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
525 return -1;
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
526 ssize_t result = handler->Stat(st);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
527 open_file_cache_.Unpin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
528 return result;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
529 }
530
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
531 void FolveFilesystem::Close(const char *fs_path, const FileHandler *handler) {
532 const std::string cache_key = CacheKey(handler->filter_id(), fs_path);
533 open_file_cache_.Unpin(cache_key);
534 }
535
536 FolveFilesystem::FolveFilesystem()
537 : current_cfg_index_(0),
538 open_file_cache_(3), total_file_openings_(0), total_file_reopen_(0) {
539 config_dirs_.push_back(""); // The first config is special: empty.
540 }
541
542 void FolveFilesystem::SwitchCurrentConfigIndex(int i) {
543 if (i < 0 || i >= (int) config_dirs_.size())
544 return;
5830c5a @hzeller o Switch logging to syslog()
authored
545 if (i != current_cfg_index_) {
546 if (i == 0) {
547 syslog(LOG_INFO, "Switching to pass-through mode.");
548 } else {
549 syslog(LOG_INFO, "Switching config directory to '%s'",
550 config_dirs()[i].c_str());
551 }
552 current_cfg_index_ = i;
553 }
e9c07dd @hzeller o add documentation
authored
554 }
fd65cee @hzeller o add basic zita filter.
authored
555
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
556 static bool IsDirectory(const std::string &path) {
557 if (path.empty()) return false;
558 struct stat st;
559 if (stat(path.c_str(), &st) != 0)
560 return false;
561 return (st.st_mode & S_IFMT) == S_IFDIR;
562 }
563
5830c5a @hzeller o Switch logging to syslog()
authored
564 void FolveFilesystem::SetDebugMode(bool b) {
565 global_debug = b;
566 }
567 bool FolveFilesystem::IsDebugMode() const { return global_debug; }
568
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
569 bool FolveFilesystem::CheckInitialized() {
570 if (underlying_dir().empty()) {
571 fprintf(stderr, "Don't know the underlying directory to read from.\n");
572 return false;
573 }
574 if (!IsDirectory(underlying_dir())) {
575 fprintf(stderr, "<underlying-dir>: '%s' not a directory.\n",
576 underlying_dir().c_str());
577 return false;
578 }
579
580 for (size_t i = 1; i < config_dirs_.size(); ++i) {
581 if (!IsDirectory(config_dirs_[i])) {
582 fprintf(stderr, "<config-dir>: '%s' not a directory.\n",
583 config_dirs_[i].c_str());
584 return false;
585 }
586 }
587 if (config_dirs_.size() > 1) {
588 // By default, lets set the index to the first filter the user provided.
589 SwitchCurrentConfigIndex(1);
590 }
591 return true;
fd65cee @hzeller o add basic zita filter.
authored
592 }
Something went wrong with that request. Please try again.