Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 699 lines (641 sloc) 26.0 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>
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
19 #include <dirent.h>
e9c07dd @hzeller o add documentation
authored
20 #include <errno.h>
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
21 #include <fcntl.h>
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
22 #include <sndfile.h>
5830c5a @hzeller o Switch logging to syslog()
authored
23 #include <stdarg.h>
e9c07dd @hzeller o add documentation
authored
24 #include <stdio.h>
25 #include <string.h>
26 #include <strings.h>
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
27 #include <sys/stat.h>
28 #include <sys/types.h>
5830c5a @hzeller o Switch logging to syslog()
authored
29 #include <syslog.h>
e9c07dd @hzeller o add documentation
authored
30 #include <unistd.h>
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
31
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
32 #include <map>
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
33 #include <string>
6c6dedb @hzeller o Some readme tweaks and readability improvements.
authored
34 #include <zita-convolver.h>
e9c07dd @hzeller o add documentation
authored
35
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
36 #include "conversion-buffer.h"
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
37 #include "file-handler-cache.h"
38 #include "file-handler.h"
2fee1d6 @hzeller o extract sound processor
authored
39 #include "sound-processor.h"
8268c37 @hzeller o Bubble up error messages in status server.
authored
40 #include "util.h"
6c6dedb @hzeller o Some readme tweaks and readability improvements.
authored
41
fd65cee @hzeller o add basic zita filter.
authored
42 #include "zita-config.h"
43
8268c37 @hzeller o Bubble up error messages in status server.
authored
44 using folve::Appendf;
3e883b7 @hzeller o Match filters in sequence
authored
45 using folve::StringPrintf;
0aee3bb @hzeller o Show base dir that is mounted.
authored
46
8268c37 @hzeller o Bubble up error messages in status server.
authored
47 static bool global_debug = false;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
48
0b0bd33 @hzeller Fix bug: the last couple of sound-samples were not flushed. We want
authored
49 static void DebugLogf(const char *format, ...)
50 __attribute__ ((format (printf, 1, 2)));
51
52 void DebugLogf(const char *format, ...) {
5830c5a @hzeller o Switch logging to syslog()
authored
53 if (!global_debug) return;
54 va_list ap;
55 va_start(ap, format);
56 vsyslog(LOG_DEBUG, format, ap);
57 va_end(ap);
58 }
b5c1e96 @hzeller o more useful output on status server.
authored
59
e9c07dd @hzeller o add documentation
authored
60 namespace {
61
5476cc2 @hzeller o provide absolute path.
authored
62 // Very simple filter that just passes the original file through. Used for
63 // everything that is not a sound-file.
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
64 class PassThroughFilter : public FileHandler {
e9c07dd @hzeller o add documentation
authored
65 public:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
66 PassThroughFilter(int filedes, int filter_id,
67 const HandlerStats &known_stats)
68 : FileHandler(filter_id), filedes_(filedes), info_stats_(known_stats) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
69 info_stats_.message.append("; pass through.");
5830c5a @hzeller o Switch logging to syslog()
authored
70 DebugLogf("Creating PassThrough filter for '%s'",
71 known_stats.filename.c_str());
e9c07dd @hzeller o add documentation
authored
72 }
680f590 @hzeller o report opening error.
authored
73 ~PassThroughFilter() { close(filedes_); }
74
e9c07dd @hzeller o add documentation
authored
75 virtual int Read(char *buf, size_t size, off_t offset) {
76 const int result = pread(filedes_, buf, size, offset);
77 return result == -1 ? -errno : result;
78 }
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
79 virtual int Stat(struct stat *st) {
80 return fstat(filedes_, st);
81 }
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
82 virtual void GetHandlerStatus(struct HandlerStats *stats) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
83 *stats = info_stats_;
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
84 }
e9c07dd @hzeller o add documentation
authored
85
86 private:
87 const int filedes_;
8268c37 @hzeller o Bubble up error messages in status server.
authored
88 HandlerStats info_stats_;
e9c07dd @hzeller o add documentation
authored
89 };
90
3e883b7 @hzeller o Match filters in sequence
authored
91 static bool FindFirstAccessiblePath(const std::vector<std::string> &path,
92 std::string *match) {
93 for (size_t i = 0; i < path.size(); ++i) {
94 if (access(path[i].c_str(), R_OK) == 0) {
95 *match = path[i];
96 return true;
97 }
98 }
99 return false;
100 }
101
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
102 class SndFileHandler :
103 public FileHandler,
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
104 public ConversionBuffer::SoundSource {
e9c07dd @hzeller o add documentation
authored
105 public:
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
106 // 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
107 // returns NULL if this is not a sound-file or if there is no available
108 // convolution filter configuration available.
8268c37 @hzeller o Bubble up error messages in status server.
authored
109 // "partial_file_info" will be set to information known so far, including
110 // error message.
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
111 static FileHandler *Create(FolveFilesystem *fs,
112 int filedes, const char *fs_path,
113 const std::string &underlying_file,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
114 int filter_id,
8268c37 @hzeller o Bubble up error messages in status server.
authored
115 const std::string &zita_config_dir,
116 HandlerStats *partial_file_info) {
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
117 SF_INFO in_info;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
118 memset(&in_info, 0, sizeof(in_info));
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
119 SNDFILE *snd = sf_open_fd(filedes, SFM_READ, &in_info, 0);
120 if (snd == NULL) {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
121 DebugLogf("File %s: %s", underlying_file.c_str(), sf_strerror(NULL));
8268c37 @hzeller o Bubble up error messages in status server.
authored
122 partial_file_info->message = sf_strerror(NULL);
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
123 return NULL;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
124 }
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
125
7e756eb @hzeller o Fix problem with re-initializing the convproc. Turns out that the c…
authored
126 int bits = 16;
31bd9ce @hzeller o README update.
authored
127 if ((in_info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_24) bits = 24;
128 if ((in_info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_32) bits = 32;
e2619a6 @hzeller o skipping debug message.
authored
129
8268c37 @hzeller o Bubble up error messages in status server.
authored
130 // Remember whatever we could got to know in the partial file info.
131 Appendf(&partial_file_info->format, "%.1fkHz, %d Bit",
132 in_info.samplerate / 1000.0, bits);
133 partial_file_info->duration_seconds = in_info.frames / in_info.samplerate;
134
3e883b7 @hzeller o Match filters in sequence
authored
135 std::vector<std::string> path_choices;
136 // From specific to non-specific.
137 path_choices.push_back(StringPrintf("%s/filter-%d-%d-%d.conf",
138 zita_config_dir.c_str(),
139 in_info.samplerate,
140 in_info.channels, bits));
141 path_choices.push_back(StringPrintf("%s/filter-%d-%d.conf",
142 zita_config_dir.c_str(),
143 in_info.samplerate,
144 in_info.channels));
145 path_choices.push_back(StringPrintf("%s/filter-%d.conf",
146 zita_config_dir.c_str(),
147 in_info.samplerate));
0b0bd33 @hzeller Fix bug: the last couple of sound-samples were not flushed. We want
authored
148 const int seconds = in_info.frames / in_info.samplerate;
3e883b7 @hzeller o Match filters in sequence
authored
149 const int max_choice = path_choices.size() - 1;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
150 std::string config_path;
3e883b7 @hzeller o Match filters in sequence
authored
151 const bool found_config = FindFirstAccessiblePath(path_choices,
152 &config_path);
a2aa9a4 @hzeller o let error messages make it to the console.
authored
153 if (found_config) {
0b0bd33 @hzeller Fix bug: the last couple of sound-samples were not flushed. We want
authored
154 DebugLogf("File %s, %.1fkHz, %d Bit, %d:%02d: filter config %s",
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
155 underlying_file.c_str(), in_info.samplerate / 1000.0, bits,
0b0bd33 @hzeller Fix bug: the last couple of sound-samples were not flushed. We want
authored
156 seconds / 60, seconds % 60,
5830c5a @hzeller o Switch logging to syslog()
authored
157 config_path.c_str());
a2aa9a4 @hzeller o let error messages make it to the console.
authored
158 } else {
0317e32 @hzeller o More 'benign' problems shouldn't show up in the syslog unless
authored
159 DebugLogf("File %s: couldn't find filter config %s...%s",
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
160 underlying_file.c_str(),
0317e32 @hzeller o More 'benign' problems shouldn't show up in the syslog unless
authored
161 path_choices[0].c_str(), path_choices[max_choice].c_str());
3e883b7 @hzeller o Match filters in sequence
authored
162 partial_file_info->message = "Missing ( " + path_choices[0]
163 + "<br/> ... " + path_choices[max_choice] + " )";
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
164 sf_close(snd);
165 return NULL;
b5c1e96 @hzeller o more useful output on status server.
authored
166 }
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
167 return new SndFileHandler(fs, fs_path, filter_id,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
168 underlying_file, filedes, snd, in_info,
8268c37 @hzeller o Bubble up error messages in status server.
authored
169 *partial_file_info, config_path);
e9c07dd @hzeller o add documentation
authored
170 }
171
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
172 virtual ~SndFileHandler() {
680f590 @hzeller o report opening error.
authored
173 Close();
2fee1d6 @hzeller o extract sound processor
authored
174 delete processor_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
175 delete output_buffer_;
176 }
177
e9c07dd @hzeller o add documentation
authored
178 virtual int Read(char *buf, size_t size, off_t offset) {
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
179 if (error_) return -1;
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
180 // If this is a skip suspiciously at the very end of the file as
181 // reported by stat, we don't do any encoding, just return garbage.
1118ad9 @hzeller o comment update.
authored
182 // (otherwise we'd to convolve up to that point).
183 //
184 // While indexing, media players do this sometimes apparently.
185 // And sometimes not even to the very end but 'almost' at the end.
186 // So add some FudeOverhang
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
187 static const int kFudgeOverhang = 512;
abb0471 @hzeller o remove debug message from last commit.
authored
188 // But of course only if this is really a skip, not a regular approaching
189 // end-of-file.
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
190 if (output_buffer_->FileSize() < offset
191 && (int) (offset + size + kFudgeOverhang) >= file_stat_.st_size) {
192 const int pretended_bytes = std::min((off_t)size,
193 file_stat_.st_size - offset);
194 if (pretended_bytes > 0) {
195 memset(buf, 0x00, pretended_bytes);
196 return pretended_bytes;
680f590 @hzeller o report opening error.
authored
197 } else {
198 return 0;
199 }
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
200 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
201 // The following read might block and call WriteToSoundfile() until the
202 // buffer is filled.
203 return output_buffer_->Read(buf, size, offset);
e9c07dd @hzeller o add documentation
authored
204 }
205
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
206 virtual void GetHandlerStatus(struct HandlerStats *stats) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
207 *stats = base_stats_;
2fee1d6 @hzeller o extract sound processor
authored
208 const int frames_done = in_info_.frames - input_frames_left_;
209 if (frames_done == 0 || in_info_.frames == 0)
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
210 stats->progress = 0.0;
211 else
2fee1d6 @hzeller o extract sound processor
authored
212 stats->progress = 1.0 * frames_done / in_info_.frames;
a5c6382 @hzeller o Remember the max output value in the stats
authored
213 if (base_stats_.max_output_value) {
214 // TODO: the status server could inspect this value and make better
215 // rendering.
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
216 base_stats_.message =
ce22b6e @hzeller o change wording from 'overdrive' to 'clipping' so that
authored
217 StringPrintf("Output clipping! "
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
218 "(max=%.3f; Multiply gain with <= %.5f<br/>in %s)",
a5c6382 @hzeller o Remember the max output value in the stats
authored
219 base_stats_.max_output_value,
220 1.0 / base_stats_.max_output_value,
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
221 config_path_.c_str());
222 }
b5c1e96 @hzeller o more useful output on status server.
authored
223 }
224
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
225 virtual int Stat(struct stat *st) {
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
226 if (output_buffer_->FileSize() > start_estimating_size_) {
2fee1d6 @hzeller o extract sound processor
authored
227 const int frames_done = in_info_.frames - input_frames_left_;
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
228 if (frames_done > 0) {
2fee1d6 @hzeller o extract sound processor
authored
229 const float estimated_end = 1.0 * in_info_.frames / frames_done;
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
230 off_t new_size = estimated_end * output_buffer_->FileSize();
231 // Report a bit bigger size which is less harmful than programs
232 // reading short.
233 new_size += 16384;
453489f @hzeller o smallish whitespace changes.
authored
234 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
235 file_stat_.st_size = new_size;
236 }
237 }
238 }
239 *st = file_stat_;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
240 return 0;
241 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
242
e9c07dd @hzeller o add documentation
authored
243 private:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
244 // TODO(hzeller): trim parameter list.
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
245 SndFileHandler(FolveFilesystem *fs, const char *fs_path, int filter_id,
246 const std::string &underlying_file,
247 int filedes, SNDFILE *snd_in,
8268c37 @hzeller o Bubble up error messages in status server.
authored
248 const SF_INFO &in_info, const HandlerStats &file_info,
249 const std::string &config_path)
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
250 : FileHandler(filter_id), fs_(fs),
2fee1d6 @hzeller o extract sound processor
authored
251 filedes_(filedes), snd_in_(snd_in), in_info_(in_info),
8268c37 @hzeller o Bubble up error messages in status server.
authored
252 config_path_(config_path),
2fee1d6 @hzeller o extract sound processor
authored
253 base_stats_(file_info),
b5c1e96 @hzeller o more useful output on status server.
authored
254 error_(false), output_buffer_(NULL),
2fee1d6 @hzeller o extract sound processor
authored
255 snd_out_(NULL), processor_(NULL),
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
256 input_frames_left_(in_info.frames) {
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
257
258 // Initial stat that we're going to report to clients. We'll adapt
259 // the filesize as we see it grow. Some clients continuously monitor
260 // the size of the file to check when to stop.
261 fstat(filedes_, &file_stat_);
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
262 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
263
8e36eff @hzeller o comment update.
authored
264 // The flac header we get is more rich than what we can create via
265 // 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
266 copy_flac_header_verbatim_ = LooksLikeInputIsFlac(in_info, filedes);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
267
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
268 // Create a conversion buffer that creates a soundfile of a particular
8e36eff @hzeller o comment update.
authored
269 // 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
270 // our input is.
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
271 SF_INFO out_info = in_info;
31bd9ce @hzeller o README update.
authored
272 out_info.seekable = 0;
273 if ((in_info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_OGG) {
8e36eff @hzeller o comment update.
authored
274 // If the input was ogg, we're re-coding this to flac, because it
275 // wouldn't let us stream the output.
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
276 out_info.format = SF_FORMAT_FLAC;
f11d8cd @hzeller o implement initial skip mode detection.
authored
277 out_info.format |= SF_FORMAT_PCM_16;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
278 }
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
279 else if ((in_info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) {
280 out_info.format = SF_FORMAT_FLAC; // recode as flac.
281 out_info.format |= SF_FORMAT_PCM_24;
31bd9ce @hzeller o README update.
authored
282 }
283 else { // original format.
284 out_info.format = in_info.format;
285 }
286
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
287 output_buffer_ = new ConversionBuffer(this, out_info);
288 }
289
f8cb9e5 @hzeller o redacting MD5.
authored
290 virtual void SetOutputSoundfile(ConversionBuffer *out_buffer,
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
291 SNDFILE *sndfile) {
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
292 snd_out_ = sndfile;
293 if (snd_out_ == NULL) {
294 error_ = true;
5830c5a @hzeller o Switch logging to syslog()
authored
295 syslog(LOG_ERR, "Opening output: %s", sf_strerror(NULL));
8268c37 @hzeller o Bubble up error messages in status server.
authored
296 base_stats_.message = sf_strerror(NULL);
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
297 return;
298 }
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
299 if (copy_flac_header_verbatim_) {
8e36eff @hzeller o comment update.
authored
300 out_buffer->set_sndfile_writes_enabled(false);
f8cb9e5 @hzeller o redacting MD5.
authored
301 CopyFlacHeader(out_buffer);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
302 } else {
f8cb9e5 @hzeller o redacting MD5.
authored
303 out_buffer->set_sndfile_writes_enabled(true);
8e36eff @hzeller o comment update.
authored
304 GenerateHeaderFromInputFile(out_buffer);
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
305 }
306 // Now flush the header: that way if someone only reads the metadata, then
307 // our AddMoreSoundData() is never called.
8e36eff @hzeller o comment update.
authored
308 // We need to do this even if we copied our own header: that way we make
309 // sure that the sndfile-header is flushed into the nirwana before we
310 // re-enable sndfile_writes.
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
311 sf_command(snd_out_, SFC_UPDATE_HEADER_NOW, NULL, 0);
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
312
313 // -- time for some hackery ...
314 // If we have copied the header over from the original, we need to
315 // redact the values for min/max blocksize and min/max framesize with
316 // what SNDFILE is going to use, otherwise programs will trip over this.
317 // http://flac.sourceforge.net/format.html
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
318 if (copy_flac_header_verbatim_) {
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
319 out_buffer->WriteCharAt((1152 & 0xFF00) >> 8, 8);
320 out_buffer->WriteCharAt((1152 & 0x00FF) , 9);
06a0312 @hzeller o Are we writing bigger blocksizes later while encoding ? To
authored
321 out_buffer->WriteCharAt((32768 & 0xFF00) >> 8, 10);
322 out_buffer->WriteCharAt((32768 & 0x00FF) , 11);
5098e07 @hzeller o improve logging.
authored
323 for (int i = 12; i < 18; ++i) out_buffer->WriteCharAt(0, i); // MD5
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
324 } else {
325 // .. and if SNDFILE writes the header, it misses out in writing the
326 // number of samples to be expected. So let's fill that in.
327 // The MD5 sum starts at position strlen("fLaC") + 4 + 18 = 26
328 // The 32 bits before that are the samples (and another 4 bit before that,
329 // ignoring that for now).
2fee1d6 @hzeller o extract sound processor
authored
330 out_buffer->WriteCharAt((in_info_.frames & 0xFF000000) >> 24, 22);
331 out_buffer->WriteCharAt((in_info_.frames & 0x00FF0000) >> 16, 23);
332 out_buffer->WriteCharAt((in_info_.frames & 0x0000FF00) >> 8, 24);
333 out_buffer->WriteCharAt((in_info_.frames & 0x000000FF), 25);
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
334 }
f8cb9e5 @hzeller o redacting MD5.
authored
335
336 out_buffer->set_sndfile_writes_enabled(true); // ready for sound-stream.
5830c5a @hzeller o Switch logging to syslog()
authored
337 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
338 out_buffer->HeaderFinished();
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
339 }
340
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
341 virtual bool AcceptProcessor(SoundProcessor *processor) {
1113409 @hzeller o Diagnostic if gapless cannot be done, because the other file is
authored
342 if (processor_ != NULL || !input_frames_left_) {
343 DebugLogf("Gapless attempt: Cannot bridge gap to alrady open file %s",
344 base_stats_.filename.c_str());
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
345 return false; // We already have one.
1113409 @hzeller o Diagnostic if gapless cannot be done, because the other file is
authored
346 }
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
347 // TODO: check that other parameters such as sampling rate and channels
348 // match (should be a are problem).
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
349 processor_ = processor;
350 if (!processor_->is_input_buffer_complete()) {
351 input_frames_left_ -= processor_->FillBuffer(snd_in_);
352 }
353 base_stats_.in_gapless = true;
354 return true;
355 }
356
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
357 static bool ExtractDirAndSuffix(const std::string &filename,
358 std::string *dir, std::string *suffix) {
359 const std::string::size_type slash_pos = filename.find_last_of('/');
360 if (slash_pos == std::string::npos) return false;
361 *dir = filename.substr(0, slash_pos + 1);
362 const std::string::size_type dot_pos = filename.find_last_of('.');
363 if (dot_pos != std::string::npos && dot_pos > slash_pos) {
364 *suffix = filename.substr(dot_pos);
365 }
366 return true;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
367 }
368
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
369 virtual bool AddMoreSoundData() {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
370 if (processor_ && processor_->pending_writes() > 0) {
371 processor_->WriteProcessed(snd_out_, processor_->pending_writes());
372 return input_frames_left_;
373 }
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
374 if (!input_frames_left_)
375 return false;
2fee1d6 @hzeller o extract sound processor
authored
376 if (!processor_) {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
377 // First time we're called and we don't have any processor yet.
2fee1d6 @hzeller o extract sound processor
authored
378 processor_ = SoundProcessor::Create(config_path_, in_info_.samplerate,
379 in_info_.channels);
380 if (processor_ == NULL) {
381 syslog(LOG_ERR, "Oops - filter-config %s is broken. Please fix. "
23b24f0 @hzeller o Write zita config problems to syslog.
authored
382 "Won't play this stream %s (simulating empty file)",
383 config_path_.c_str(), base_stats_.filename.c_str());
8268c37 @hzeller o Bubble up error messages in status server.
authored
384 base_stats_.message = "Problem parsing " + config_path_;
0c8561b @hzeller o Handle configuration file error gracefully.
authored
385 input_frames_left_ = 0;
a2aa9a4 @hzeller o let error messages make it to the console.
authored
386 Close();
0c8561b @hzeller o Handle configuration file error gracefully.
authored
387 return false;
388 }
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
389 }
2fee1d6 @hzeller o extract sound processor
authored
390 const int r = processor_->FillBuffer(snd_in_);
9c0ba15 @hzeller o work around broken files - don't endlessloop :)
authored
391 if (r == 0) {
2fee1d6 @hzeller o extract sound processor
authored
392 syslog(LOG_ERR, "Expected %d frames left, "
5830c5a @hzeller o Switch logging to syslog()
authored
393 "but got EOF; corrupt file '%s' ?",
2fee1d6 @hzeller o extract sound processor
authored
394 input_frames_left_, base_stats_.filename.c_str());
0706452 @hzeller o Report broken input file in UI.
authored
395 base_stats_.message = "Premature EOF in input file.";
9c0ba15 @hzeller o work around broken files - don't endlessloop :)
authored
396 input_frames_left_ = 0;
397 Close();
398 return false;
399 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
400 input_frames_left_ -= r;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
401 if (!input_frames_left_ && !processor_->is_input_buffer_complete()
402 && fs_->gapless_processing()) {
403 typedef std::set<std::string> DirSet;
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
404 DirSet dirset;
405 std::string fs_dir, file_suffix;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
406 FileHandler *next_file = NULL;
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
407 DirSet::const_iterator found;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
408 const bool passed_processor
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
409 = (ExtractDirAndSuffix(base_stats_.filename, &fs_dir, &file_suffix)
410 && fs_->ListDirectory(fs_dir, file_suffix, &dirset)
411 && (found = dirset.upper_bound(base_stats_.filename)) != dirset.end()
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
412 && (next_file = fs_->GetOrCreateHandler(found->c_str()))
413 && next_file->AcceptProcessor(processor_));
414 if (passed_processor) {
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
415 DebugLogf("Gapless pass-on from '%s' to alphabetically next '%s'",
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
416 base_stats_.filename.c_str(), found->c_str());
417 }
418 processor_->WriteProcessed(snd_out_, r);
419 if (passed_processor) {
420 base_stats_.out_gapless = true;
a5c6382 @hzeller o Remember the max output value in the stats
authored
421 ExtractOutputValues();
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
422 processor_ = NULL; // we handed over ownership.
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
423 }
424 if (next_file) fs_->Close(found->c_str(), next_file);
425 } else {
426 processor_->WriteProcessed(snd_out_, r);
427 }
31bd9ce @hzeller o README update.
authored
428 if (input_frames_left_ == 0) {
680f590 @hzeller o report opening error.
authored
429 Close();
31bd9ce @hzeller o README update.
authored
430 }
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
431 return input_frames_left_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
432 }
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
433
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
434 // TODO add as a utility function to ConversionBuffer ?
8e36eff @hzeller o comment update.
authored
435 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
436 char buf[256];
437 while (len > 0) {
438 ssize_t r = pread(fd, buf, std::min(sizeof(buf), len), pos);
8e36eff @hzeller o comment update.
authored
439 if (r <= 0) return;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
440 out->Append(buf, r);
441 len -= r;
442 pos += r;
443 }
444 }
445
f8cb9e5 @hzeller o redacting MD5.
authored
446 void CopyFlacHeader(ConversionBuffer *out_buffer) {
5830c5a @hzeller o Switch logging to syslog()
authored
447 DebugLogf("Provide FLAC header from original file %s",
448 base_stats_.filename.c_str());
f8cb9e5 @hzeller o redacting MD5.
authored
449 out_buffer->Append("fLaC", 4);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
450 off_t pos = 4;
451 unsigned char header[4];
f8cb9e5 @hzeller o redacting MD5.
authored
452 bool need_finish_padding = false;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
453 while (pread(filedes_, header, sizeof(header), pos) == sizeof(header)) {
454 pos += sizeof(header);
455 bool is_last = header[0] & 0x80;
456 unsigned int type = header[0] & 0x7F;
457 unsigned int byte_len = (header[1] << 16) + (header[2] << 8) + header[3];
5098e07 @hzeller o improve logging.
authored
458 const char *extra_info = "";
f8cb9e5 @hzeller o redacting MD5.
authored
459 need_finish_padding = false;
460 if (type == FLAC__METADATA_TYPE_STREAMINFO && byte_len == 34) {
461 out_buffer->Append(&header, sizeof(header));
8e36eff @hzeller o comment update.
authored
462 // Copy everything but the MD5 at the end - which we set to empty.
f8cb9e5 @hzeller o redacting MD5.
authored
463 CopyBytes(filedes_, pos, out_buffer, byte_len - 16);
464 for (int i = 0; i < 16; ++i) out_buffer->Append("\0", 1);
5098e07 @hzeller o improve logging.
authored
465 extra_info = "Streaminfo; redact MD5.";
f8cb9e5 @hzeller o redacting MD5.
authored
466 }
467 else if (type == FLAC__METADATA_TYPE_SEEKTABLE) {
468 // The SEEKTABLE header we skip, because it is bogus after encoding.
5830c5a @hzeller o Switch logging to syslog()
authored
469 // TODO append log (skip the seektable)
f8cb9e5 @hzeller o redacting MD5.
authored
470 need_finish_padding = is_last; // if we were last, force finish block.
5098e07 @hzeller o improve logging.
authored
471 extra_info = "Skip seektable.";
f8cb9e5 @hzeller o redacting MD5.
authored
472 }
473 else {
474 out_buffer->Append(&header, sizeof(header));
475 CopyBytes(filedes_, pos, out_buffer, byte_len);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
476 }
5098e07 @hzeller o improve logging.
authored
477 DebugLogf(" %02x %02x %02x %02x type: %d, len: %6u %s %s ",
478 header[0], header[1], header[2], header[3],
479 type, byte_len, is_last ? "(last)" : "(cont)", extra_info);
f8cb9e5 @hzeller o redacting MD5.
authored
480 pos += byte_len;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
481 if (is_last)
482 break;
483 }
f8cb9e5 @hzeller o redacting MD5.
authored
484 if (need_finish_padding) { // if the last block was not is_last: pad.
5830c5a @hzeller o Switch logging to syslog()
authored
485 DebugLogf("write padding");
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
486 memset(&header, 0, sizeof(header));
487 header[0] = 0x80 /* is last */ | FLAC__METADATA_TYPE_PADDING;
f8cb9e5 @hzeller o redacting MD5.
authored
488 out_buffer->Append(&header, sizeof(header));
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
489 }
490 }
491
8e36eff @hzeller o comment update.
authored
492 void GenerateHeaderFromInputFile(ConversionBuffer *out_buffer) {
5830c5a @hzeller o Switch logging to syslog()
authored
493 DebugLogf("Generate header from original ID3-tags.");
8e36eff @hzeller o comment update.
authored
494 out_buffer->set_sndfile_writes_enabled(true);
495 // Copy ID tags that are supported by sndfile.
496 for (int i = SF_STR_FIRST; i <= SF_STR_LAST; ++i) {
497 const char *s = sf_get_string(snd_in_, i);
498 if (s != NULL) {
499 sf_set_string(snd_out_, i, s);
500 }
501 }
502 }
503
a5c6382 @hzeller o Remember the max output value in the stats
authored
504 void ExtractOutputValues() {
505 if (processor_) {
506 base_stats_.max_output_value = processor_->max_output_value();
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
507 processor_->ResetMaxValues();
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
508 }
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
509 }
a5c6382 @hzeller o Remember the max output value in the stats
authored
510
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
511 void Close() {
512 if (snd_out_ == NULL) return; // done.
a5c6382 @hzeller o Remember the max output value in the stats
authored
513 ExtractOutputValues();
514 if (base_stats_.max_output_value > 1.0) {
515 syslog(LOG_ERR, "Observed output clipping in '%s': "
516 "Max=%.3f; Multiply gain with <= %.5f in %s",
517 base_stats_.filename.c_str(), base_stats_.max_output_value,
518 1.0 / base_stats_.max_output_value, config_path_.c_str());
519 }
0b0bd33 @hzeller Fix bug: the last couple of sound-samples were not flushed. We want
authored
520 // We can't disable buffer writes here, because outfile closing will flush
521 // the last couple of sound samples.
680f590 @hzeller o report opening error.
authored
522 if (snd_in_) sf_close(snd_in_);
523 if (snd_out_) sf_close(snd_out_);
524 snd_out_ = NULL;
525 close(filedes_);
526 }
527
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
528 bool LooksLikeInputIsFlac(const SF_INFO &sndinfo, int filedes) {
529 if ((sndinfo.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
530 return false;
531 // However some files contain flac encoded stuff, but are not flac files
532 // by themselve. So we can't copy headers verbatim. Sanity check header.
533 char flac_magic[4];
534 if (pread(filedes, flac_magic, sizeof(flac_magic), 0) != sizeof(flac_magic))
535 return false;
536 return memcmp(flac_magic, "fLaC", sizeof(flac_magic)) == 0;
537 }
538
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
539 FolveFilesystem *const fs_;
e9c07dd @hzeller o add documentation
authored
540 const int filedes_;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
541 SNDFILE *const snd_in_;
2fee1d6 @hzeller o extract sound processor
authored
542 const SF_INFO in_info_;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
543 const std::string config_path_;
544
2fee1d6 @hzeller o extract sound processor
authored
545 HandlerStats base_stats_; // UI information about current file.
546
547 struct stat file_stat_; // we dynamically report a changing size.
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
548 off_t start_estimating_size_; // essentially const.
549
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
550 bool error_;
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
551 bool copy_flac_header_verbatim_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
552 ConversionBuffer *output_buffer_;
553 SNDFILE *snd_out_;
554
555 // Used in conversion.
2fee1d6 @hzeller o extract sound processor
authored
556 SoundProcessor *processor_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
557 int input_frames_left_;
e9c07dd @hzeller o add documentation
authored
558 };
559 } // namespace
560
8c7e5c1 @hzeller o Only if -D is given, the debug toggle is active in the UI.
authored
561 FolveFilesystem::FolveFilesystem()
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
562 : current_cfg_index_(0), debug_ui_enabled_(false), gapless_processing_(false),
8c7e5c1 @hzeller o Only if -D is given, the debug toggle is active in the UI.
authored
563 open_file_cache_(3), total_file_openings_(0), total_file_reopen_(0) {
564 config_dirs_.push_back(""); // The first config is special: empty.
565 }
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
566
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
567 FileHandler *FolveFilesystem::CreateFromDescriptor(
568 int filedes,
569 int cfg_idx,
570 const char *fs_path,
571 const std::string &underlying_file) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
572 HandlerStats file_info;
573 file_info.filename = fs_path;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
574 if (cfg_idx != 0) {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
575 FileHandler *filter = SndFileHandler::Create(this, filedes, fs_path,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
576 underlying_file,
577 cfg_idx,
578 config_dirs()[cfg_idx],
579 &file_info);
580 if (filter != NULL) return filter;
581 } else {
582 file_info.message = "No filter config selected.";
583 }
e9c07dd @hzeller o add documentation
authored
584
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
585 // Every other file-type is just passed through as is.
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
586 return new PassThroughFilter(filedes, cfg_idx, file_info);
587 }
588
589 std::string FolveFilesystem::CacheKey(int config_idx, const char *fs_path) {
590 std::string result;
591 Appendf(&result, "%d/%s", config_idx, fs_path);
592 return result;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
593 }
594
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
595 FileHandler *FolveFilesystem::GetOrCreateHandler(const char *fs_path) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
596 const int config_idx = current_cfg_index_;
597 const std::string cache_key = CacheKey(config_idx, fs_path);
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
598 const std::string underlying_file = underlying_dir() + fs_path;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
599 FileHandler *handler = open_file_cache_.FindAndPin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
600 if (handler == NULL) {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
601 int filedes = open(underlying_file.c_str(), O_RDONLY);
680f590 @hzeller o report opening error.
authored
602 if (filedes < 0)
603 return NULL;
b5c1e96 @hzeller o more useful output on status server.
authored
604 ++total_file_openings_;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
605 handler = CreateFromDescriptor(filedes, config_idx,
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
606 fs_path, underlying_file);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
607 handler = open_file_cache_.InsertPinned(cache_key, handler);
b5c1e96 @hzeller o more useful output on status server.
authored
608 } else {
609 ++total_file_reopen_;
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
610 }
611 return handler;
e9c07dd @hzeller o add documentation
authored
612 }
613
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
614 int FolveFilesystem::StatByFilename(const char *fs_path, struct stat *st) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
615 const std::string cache_key = CacheKey(current_cfg_index_, fs_path);
616 FileHandler *handler = open_file_cache_.FindAndPin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
617 if (handler == 0)
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
618 return -1;
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
619 ssize_t result = handler->Stat(st);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
620 open_file_cache_.Unpin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
621 return result;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
622 }
623
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
624 void FolveFilesystem::Close(const char *fs_path, const FileHandler *handler) {
625 const std::string cache_key = CacheKey(handler->filter_id(), fs_path);
626 open_file_cache_.Unpin(cache_key);
627 }
628
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
629 bool FolveFilesystem::ListDirectory(const std::string &fs_dir,
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
630 const std::string &suffix,
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
631 std::set<std::string> *files) {
632 const std::string real_dir = underlying_dir() + fs_dir;
633 DIR *dp = opendir(real_dir.c_str());
634 if (dp == NULL) return false;
635 struct dirent *dent;
636 while ((dent = readdir(dp)) != NULL) {
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
637 if (!folve::HasSuffix(dent->d_name, suffix))
638 continue;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
639 files->insert(fs_dir + dent->d_name);
640 }
641 closedir(dp);
642 return true;
643 }
644
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
645 void FolveFilesystem::SwitchCurrentConfigIndex(int i) {
646 if (i < 0 || i >= (int) config_dirs_.size())
647 return;
5830c5a @hzeller o Switch logging to syslog()
authored
648 if (i != current_cfg_index_) {
649 if (i == 0) {
650 syslog(LOG_INFO, "Switching to pass-through mode.");
651 } else {
652 syslog(LOG_INFO, "Switching config directory to '%s'",
653 config_dirs()[i].c_str());
654 }
655 current_cfg_index_ = i;
656 }
e9c07dd @hzeller o add documentation
authored
657 }
fd65cee @hzeller o add basic zita filter.
authored
658
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
659 static bool IsDirectory(const std::string &path) {
660 if (path.empty()) return false;
661 struct stat st;
662 if (stat(path.c_str(), &st) != 0)
663 return false;
664 return (st.st_mode & S_IFMT) == S_IFDIR;
665 }
666
5830c5a @hzeller o Switch logging to syslog()
authored
667 void FolveFilesystem::SetDebugMode(bool b) {
249865a @hzeller o Make debug mode switchable in UI.
authored
668 if (b != global_debug) {
669 syslog(LOG_INFO, "Switch debug mode %s.", b ? "on" : "off");
670 global_debug = b;
671 }
5830c5a @hzeller o Switch logging to syslog()
authored
672 }
673 bool FolveFilesystem::IsDebugMode() const { return global_debug; }
674
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
675 bool FolveFilesystem::CheckInitialized() {
676 if (underlying_dir().empty()) {
677 fprintf(stderr, "Don't know the underlying directory to read from.\n");
678 return false;
679 }
680 if (!IsDirectory(underlying_dir())) {
681 fprintf(stderr, "<underlying-dir>: '%s' not a directory.\n",
682 underlying_dir().c_str());
683 return false;
684 }
685
686 for (size_t i = 1; i < config_dirs_.size(); ++i) {
687 if (!IsDirectory(config_dirs_[i])) {
688 fprintf(stderr, "<config-dir>: '%s' not a directory.\n",
689 config_dirs_[i].c_str());
690 return false;
691 }
692 }
693 if (config_dirs_.size() > 1) {
694 // By default, lets set the index to the first filter the user provided.
695 SwitchCurrentConfigIndex(1);
696 }
697 return true;
fd65cee @hzeller o add basic zita filter.
authored
698 }
Something went wrong with that request. Please try again.