Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 693 lines (636 sloc) 25.827 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;
213 if (processor_ && processor_->max_output_value() > 1.0) {
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
214 base_stats_.message =
ce22b6e @hzeller o change wording from 'overdrive' to 'clipping' so that
authored
215 StringPrintf("Output clipping! "
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
216 "(max=%.3f; Multiply gain with <= %.5f<br/>in %s)",
2fee1d6 @hzeller o extract sound processor
authored
217 processor_->max_output_value(),
218 1.0 / processor_->max_output_value(),
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
219 config_path_.c_str());
220 }
b5c1e96 @hzeller o more useful output on status server.
authored
221 }
222
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
223 virtual int Stat(struct stat *st) {
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
224 if (output_buffer_->FileSize() > start_estimating_size_) {
2fee1d6 @hzeller o extract sound processor
authored
225 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
226 if (frames_done > 0) {
2fee1d6 @hzeller o extract sound processor
authored
227 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
228 off_t new_size = estimated_end * output_buffer_->FileSize();
229 // Report a bit bigger size which is less harmful than programs
230 // reading short.
231 new_size += 16384;
453489f @hzeller o smallish whitespace changes.
authored
232 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
233 file_stat_.st_size = new_size;
234 }
235 }
236 }
237 *st = file_stat_;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
238 return 0;
239 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
240
e9c07dd @hzeller o add documentation
authored
241 private:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
242 // TODO(hzeller): trim parameter list.
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
243 SndFileHandler(FolveFilesystem *fs, const char *fs_path, int filter_id,
244 const std::string &underlying_file,
245 int filedes, SNDFILE *snd_in,
8268c37 @hzeller o Bubble up error messages in status server.
authored
246 const SF_INFO &in_info, const HandlerStats &file_info,
247 const std::string &config_path)
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
248 : FileHandler(filter_id), fs_(fs),
2fee1d6 @hzeller o extract sound processor
authored
249 filedes_(filedes), snd_in_(snd_in), in_info_(in_info),
8268c37 @hzeller o Bubble up error messages in status server.
authored
250 config_path_(config_path),
2fee1d6 @hzeller o extract sound processor
authored
251 base_stats_(file_info),
b5c1e96 @hzeller o more useful output on status server.
authored
252 error_(false), output_buffer_(NULL),
2fee1d6 @hzeller o extract sound processor
authored
253 snd_out_(NULL), processor_(NULL),
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
254 input_frames_left_(in_info.frames) {
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
255
256 // Initial stat that we're going to report to clients. We'll adapt
257 // the filesize as we see it grow. Some clients continuously monitor
258 // the size of the file to check when to stop.
259 fstat(filedes_, &file_stat_);
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
260 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
261
8e36eff @hzeller o comment update.
authored
262 // The flac header we get is more rich than what we can create via
263 // 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
264 copy_flac_header_verbatim_ = LooksLikeInputIsFlac(in_info, filedes);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
265
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
266 // Create a conversion buffer that creates a soundfile of a particular
8e36eff @hzeller o comment update.
authored
267 // 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
268 // our input is.
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
269 SF_INFO out_info = in_info;
31bd9ce @hzeller o README update.
authored
270 out_info.seekable = 0;
271 if ((in_info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_OGG) {
8e36eff @hzeller o comment update.
authored
272 // If the input was ogg, we're re-coding this to flac, because it
273 // wouldn't let us stream the output.
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
274 out_info.format = SF_FORMAT_FLAC;
f11d8cd @hzeller o implement initial skip mode detection.
authored
275 out_info.format |= SF_FORMAT_PCM_16;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
276 }
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
277 else if ((in_info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) {
278 out_info.format = SF_FORMAT_FLAC; // recode as flac.
279 out_info.format |= SF_FORMAT_PCM_24;
31bd9ce @hzeller o README update.
authored
280 }
281 else { // original format.
282 out_info.format = in_info.format;
283 }
284
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
285 output_buffer_ = new ConversionBuffer(this, out_info);
286 }
287
f8cb9e5 @hzeller o redacting MD5.
authored
288 virtual void SetOutputSoundfile(ConversionBuffer *out_buffer,
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
289 SNDFILE *sndfile) {
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
290 snd_out_ = sndfile;
291 if (snd_out_ == NULL) {
292 error_ = true;
5830c5a @hzeller o Switch logging to syslog()
authored
293 syslog(LOG_ERR, "Opening output: %s", sf_strerror(NULL));
8268c37 @hzeller o Bubble up error messages in status server.
authored
294 base_stats_.message = sf_strerror(NULL);
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
295 return;
296 }
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
297 if (copy_flac_header_verbatim_) {
8e36eff @hzeller o comment update.
authored
298 out_buffer->set_sndfile_writes_enabled(false);
f8cb9e5 @hzeller o redacting MD5.
authored
299 CopyFlacHeader(out_buffer);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
300 } else {
f8cb9e5 @hzeller o redacting MD5.
authored
301 out_buffer->set_sndfile_writes_enabled(true);
8e36eff @hzeller o comment update.
authored
302 GenerateHeaderFromInputFile(out_buffer);
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
303 }
304 // Now flush the header: that way if someone only reads the metadata, then
305 // our AddMoreSoundData() is never called.
8e36eff @hzeller o comment update.
authored
306 // We need to do this even if we copied our own header: that way we make
307 // sure that the sndfile-header is flushed into the nirwana before we
308 // re-enable sndfile_writes.
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
309 sf_command(snd_out_, SFC_UPDATE_HEADER_NOW, NULL, 0);
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
310
311 // -- time for some hackery ...
312 // If we have copied the header over from the original, we need to
313 // redact the values for min/max blocksize and min/max framesize with
314 // what SNDFILE is going to use, otherwise programs will trip over this.
315 // http://flac.sourceforge.net/format.html
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
316 if (copy_flac_header_verbatim_) {
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
317 out_buffer->WriteCharAt((1152 & 0xFF00) >> 8, 8);
318 out_buffer->WriteCharAt((1152 & 0x00FF) , 9);
06a0312 @hzeller o Are we writing bigger blocksizes later while encoding ? To
authored
319 out_buffer->WriteCharAt((32768 & 0xFF00) >> 8, 10);
320 out_buffer->WriteCharAt((32768 & 0x00FF) , 11);
5098e07 @hzeller o improve logging.
authored
321 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
322 } else {
323 // .. and if SNDFILE writes the header, it misses out in writing the
324 // number of samples to be expected. So let's fill that in.
325 // The MD5 sum starts at position strlen("fLaC") + 4 + 18 = 26
326 // The 32 bits before that are the samples (and another 4 bit before that,
327 // ignoring that for now).
2fee1d6 @hzeller o extract sound processor
authored
328 out_buffer->WriteCharAt((in_info_.frames & 0xFF000000) >> 24, 22);
329 out_buffer->WriteCharAt((in_info_.frames & 0x00FF0000) >> 16, 23);
330 out_buffer->WriteCharAt((in_info_.frames & 0x0000FF00) >> 8, 24);
331 out_buffer->WriteCharAt((in_info_.frames & 0x000000FF), 25);
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
332 }
f8cb9e5 @hzeller o redacting MD5.
authored
333
334 out_buffer->set_sndfile_writes_enabled(true); // ready for sound-stream.
5830c5a @hzeller o Switch logging to syslog()
authored
335 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
336 out_buffer->HeaderFinished();
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
337 }
338
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
339 virtual bool AcceptProcessor(SoundProcessor *processor) {
1113409 @hzeller o Diagnostic if gapless cannot be done, because the other file is
authored
340 if (processor_ != NULL || !input_frames_left_) {
341 DebugLogf("Gapless attempt: Cannot bridge gap to alrady open file %s",
342 base_stats_.filename.c_str());
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
343 return false; // We already have one.
1113409 @hzeller o Diagnostic if gapless cannot be done, because the other file is
authored
344 }
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
345 // TODO: check that other parameters such as sampling rate and channels
346 // match (should be a are problem).
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
347 processor_ = processor;
348 if (!processor_->is_input_buffer_complete()) {
349 input_frames_left_ -= processor_->FillBuffer(snd_in_);
350 }
351 base_stats_.in_gapless = true;
352 return true;
353 }
354
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
355 static bool ExtractDirAndSuffix(const std::string &filename,
356 std::string *dir, std::string *suffix) {
357 const std::string::size_type slash_pos = filename.find_last_of('/');
358 if (slash_pos == std::string::npos) return false;
359 *dir = filename.substr(0, slash_pos + 1);
360 const std::string::size_type dot_pos = filename.find_last_of('.');
361 if (dot_pos != std::string::npos && dot_pos > slash_pos) {
362 *suffix = filename.substr(dot_pos);
363 }
364 return true;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
365 }
366
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
367 virtual bool AddMoreSoundData() {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
368 if (processor_ && processor_->pending_writes() > 0) {
369 processor_->WriteProcessed(snd_out_, processor_->pending_writes());
370 return input_frames_left_;
371 }
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
372 if (!input_frames_left_)
373 return false;
2fee1d6 @hzeller o extract sound processor
authored
374 if (!processor_) {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
375 // First time we're called and we don't have any processor yet.
2fee1d6 @hzeller o extract sound processor
authored
376 processor_ = SoundProcessor::Create(config_path_, in_info_.samplerate,
377 in_info_.channels);
378 if (processor_ == NULL) {
379 syslog(LOG_ERR, "Oops - filter-config %s is broken. Please fix. "
23b24f0 @hzeller o Write zita config problems to syslog.
authored
380 "Won't play this stream %s (simulating empty file)",
381 config_path_.c_str(), base_stats_.filename.c_str());
8268c37 @hzeller o Bubble up error messages in status server.
authored
382 base_stats_.message = "Problem parsing " + config_path_;
0c8561b @hzeller o Handle configuration file error gracefully.
authored
383 input_frames_left_ = 0;
a2aa9a4 @hzeller o let error messages make it to the console.
authored
384 Close();
0c8561b @hzeller o Handle configuration file error gracefully.
authored
385 return false;
386 }
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
387 }
2fee1d6 @hzeller o extract sound processor
authored
388 const int r = processor_->FillBuffer(snd_in_);
9c0ba15 @hzeller o work around broken files - don't endlessloop :)
authored
389 if (r == 0) {
2fee1d6 @hzeller o extract sound processor
authored
390 syslog(LOG_ERR, "Expected %d frames left, "
5830c5a @hzeller o Switch logging to syslog()
authored
391 "but got EOF; corrupt file '%s' ?",
2fee1d6 @hzeller o extract sound processor
authored
392 input_frames_left_, base_stats_.filename.c_str());
0706452 @hzeller o Report broken input file in UI.
authored
393 base_stats_.message = "Premature EOF in input file.";
9c0ba15 @hzeller o work around broken files - don't endlessloop :)
authored
394 input_frames_left_ = 0;
395 Close();
396 return false;
397 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
398 input_frames_left_ -= r;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
399 if (!input_frames_left_ && !processor_->is_input_buffer_complete()
400 && fs_->gapless_processing()) {
401 typedef std::set<std::string> DirSet;
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
402 DirSet dirset;
403 std::string fs_dir, file_suffix;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
404 FileHandler *next_file = NULL;
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
405 DirSet::const_iterator found;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
406 const bool passed_processor
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
407 = (ExtractDirAndSuffix(base_stats_.filename, &fs_dir, &file_suffix)
408 && fs_->ListDirectory(fs_dir, file_suffix, &dirset)
409 && (found = dirset.upper_bound(base_stats_.filename)) != dirset.end()
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
410 && (next_file = fs_->GetOrCreateHandler(found->c_str()))
411 && next_file->AcceptProcessor(processor_));
412 if (passed_processor) {
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
413 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
414 base_stats_.filename.c_str(), found->c_str());
415 }
416 processor_->WriteProcessed(snd_out_, r);
417 if (passed_processor) {
418 base_stats_.out_gapless = true;
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
419 LogClipping();
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
420 processor_ = NULL; // we handed over ownership.
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
421 }
422 if (next_file) fs_->Close(found->c_str(), next_file);
423 } else {
424 processor_->WriteProcessed(snd_out_, r);
425 }
31bd9ce @hzeller o README update.
authored
426 if (input_frames_left_ == 0) {
680f590 @hzeller o report opening error.
authored
427 Close();
31bd9ce @hzeller o README update.
authored
428 }
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
429 return input_frames_left_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
430 }
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
431
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
432 // TODO add as a utility function to ConversionBuffer ?
8e36eff @hzeller o comment update.
authored
433 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
434 char buf[256];
435 while (len > 0) {
436 ssize_t r = pread(fd, buf, std::min(sizeof(buf), len), pos);
8e36eff @hzeller o comment update.
authored
437 if (r <= 0) return;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
438 out->Append(buf, r);
439 len -= r;
440 pos += r;
441 }
442 }
443
f8cb9e5 @hzeller o redacting MD5.
authored
444 void CopyFlacHeader(ConversionBuffer *out_buffer) {
5830c5a @hzeller o Switch logging to syslog()
authored
445 DebugLogf("Provide FLAC header from original file %s",
446 base_stats_.filename.c_str());
f8cb9e5 @hzeller o redacting MD5.
authored
447 out_buffer->Append("fLaC", 4);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
448 off_t pos = 4;
449 unsigned char header[4];
f8cb9e5 @hzeller o redacting MD5.
authored
450 bool need_finish_padding = false;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
451 while (pread(filedes_, header, sizeof(header), pos) == sizeof(header)) {
452 pos += sizeof(header);
453 bool is_last = header[0] & 0x80;
454 unsigned int type = header[0] & 0x7F;
455 unsigned int byte_len = (header[1] << 16) + (header[2] << 8) + header[3];
5098e07 @hzeller o improve logging.
authored
456 const char *extra_info = "";
f8cb9e5 @hzeller o redacting MD5.
authored
457 need_finish_padding = false;
458 if (type == FLAC__METADATA_TYPE_STREAMINFO && byte_len == 34) {
459 out_buffer->Append(&header, sizeof(header));
8e36eff @hzeller o comment update.
authored
460 // Copy everything but the MD5 at the end - which we set to empty.
f8cb9e5 @hzeller o redacting MD5.
authored
461 CopyBytes(filedes_, pos, out_buffer, byte_len - 16);
462 for (int i = 0; i < 16; ++i) out_buffer->Append("\0", 1);
5098e07 @hzeller o improve logging.
authored
463 extra_info = "Streaminfo; redact MD5.";
f8cb9e5 @hzeller o redacting MD5.
authored
464 }
465 else if (type == FLAC__METADATA_TYPE_SEEKTABLE) {
466 // The SEEKTABLE header we skip, because it is bogus after encoding.
5830c5a @hzeller o Switch logging to syslog()
authored
467 // TODO append log (skip the seektable)
f8cb9e5 @hzeller o redacting MD5.
authored
468 need_finish_padding = is_last; // if we were last, force finish block.
5098e07 @hzeller o improve logging.
authored
469 extra_info = "Skip seektable.";
f8cb9e5 @hzeller o redacting MD5.
authored
470 }
471 else {
472 out_buffer->Append(&header, sizeof(header));
473 CopyBytes(filedes_, pos, out_buffer, byte_len);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
474 }
5098e07 @hzeller o improve logging.
authored
475 DebugLogf(" %02x %02x %02x %02x type: %d, len: %6u %s %s ",
476 header[0], header[1], header[2], header[3],
477 type, byte_len, is_last ? "(last)" : "(cont)", extra_info);
f8cb9e5 @hzeller o redacting MD5.
authored
478 pos += byte_len;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
479 if (is_last)
480 break;
481 }
f8cb9e5 @hzeller o redacting MD5.
authored
482 if (need_finish_padding) { // if the last block was not is_last: pad.
5830c5a @hzeller o Switch logging to syslog()
authored
483 DebugLogf("write padding");
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
484 memset(&header, 0, sizeof(header));
485 header[0] = 0x80 /* is last */ | FLAC__METADATA_TYPE_PADDING;
f8cb9e5 @hzeller o redacting MD5.
authored
486 out_buffer->Append(&header, sizeof(header));
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
487 }
488 }
489
8e36eff @hzeller o comment update.
authored
490 void GenerateHeaderFromInputFile(ConversionBuffer *out_buffer) {
5830c5a @hzeller o Switch logging to syslog()
authored
491 DebugLogf("Generate header from original ID3-tags.");
8e36eff @hzeller o comment update.
authored
492 out_buffer->set_sndfile_writes_enabled(true);
493 // Copy ID tags that are supported by sndfile.
494 for (int i = SF_STR_FIRST; i <= SF_STR_LAST; ++i) {
495 const char *s = sf_get_string(snd_in_, i);
496 if (s != NULL) {
497 sf_set_string(snd_out_, i, s);
498 }
499 }
500 }
501
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
502 void LogClipping() {
2fee1d6 @hzeller o extract sound processor
authored
503 if (processor_ && processor_->max_output_value() > 1.0) {
ce22b6e @hzeller o change wording from 'overdrive' to 'clipping' so that
authored
504 syslog(LOG_ERR, "Observed output clipping (%s). "
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
505 "Max=%.3f; Multiply gain with <= %.5f in %s",
2fee1d6 @hzeller o extract sound processor
authored
506 base_stats_.filename.c_str(), processor_->max_output_value(),
507 1.0 / processor_->max_output_value(), config_path_.c_str());
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
508 processor_->ResetMaxValues();
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
509 }
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
510 }
511 void Close() {
512 if (snd_out_ == NULL) return; // done.
513 LogClipping();
0b0bd33 @hzeller Fix bug: the last couple of sound-samples were not flushed. We want
authored
514 // We can't disable buffer writes here, because outfile closing will flush
515 // the last couple of sound samples.
680f590 @hzeller o report opening error.
authored
516 if (snd_in_) sf_close(snd_in_);
517 if (snd_out_) sf_close(snd_out_);
518 snd_out_ = NULL;
519 close(filedes_);
520 }
521
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
522 bool LooksLikeInputIsFlac(const SF_INFO &sndinfo, int filedes) {
523 if ((sndinfo.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
524 return false;
525 // However some files contain flac encoded stuff, but are not flac files
526 // by themselve. So we can't copy headers verbatim. Sanity check header.
527 char flac_magic[4];
528 if (pread(filedes, flac_magic, sizeof(flac_magic), 0) != sizeof(flac_magic))
529 return false;
530 return memcmp(flac_magic, "fLaC", sizeof(flac_magic)) == 0;
531 }
532
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
533 FolveFilesystem *const fs_;
e9c07dd @hzeller o add documentation
authored
534 const int filedes_;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
535 SNDFILE *const snd_in_;
2fee1d6 @hzeller o extract sound processor
authored
536 const SF_INFO in_info_;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
537 const std::string config_path_;
538
2fee1d6 @hzeller o extract sound processor
authored
539 HandlerStats base_stats_; // UI information about current file.
540
541 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
542 off_t start_estimating_size_; // essentially const.
543
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
544 bool error_;
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
545 bool copy_flac_header_verbatim_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
546 ConversionBuffer *output_buffer_;
547 SNDFILE *snd_out_;
548
549 // Used in conversion.
2fee1d6 @hzeller o extract sound processor
authored
550 SoundProcessor *processor_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
551 int input_frames_left_;
e9c07dd @hzeller o add documentation
authored
552 };
553 } // namespace
554
8c7e5c1 @hzeller o Only if -D is given, the debug toggle is active in the UI.
authored
555 FolveFilesystem::FolveFilesystem()
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
556 : 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
557 open_file_cache_(3), total_file_openings_(0), total_file_reopen_(0) {
558 config_dirs_.push_back(""); // The first config is special: empty.
559 }
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
560
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
561 FileHandler *FolveFilesystem::CreateFromDescriptor(
562 int filedes,
563 int cfg_idx,
564 const char *fs_path,
565 const std::string &underlying_file) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
566 HandlerStats file_info;
567 file_info.filename = fs_path;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
568 if (cfg_idx != 0) {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
569 FileHandler *filter = SndFileHandler::Create(this, filedes, fs_path,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
570 underlying_file,
571 cfg_idx,
572 config_dirs()[cfg_idx],
573 &file_info);
574 if (filter != NULL) return filter;
575 } else {
576 file_info.message = "No filter config selected.";
577 }
e9c07dd @hzeller o add documentation
authored
578
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
579 // Every other file-type is just passed through as is.
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
580 return new PassThroughFilter(filedes, cfg_idx, file_info);
581 }
582
583 std::string FolveFilesystem::CacheKey(int config_idx, const char *fs_path) {
584 std::string result;
585 Appendf(&result, "%d/%s", config_idx, fs_path);
586 return result;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
587 }
588
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
589 FileHandler *FolveFilesystem::GetOrCreateHandler(const char *fs_path) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
590 const int config_idx = current_cfg_index_;
591 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
592 const std::string underlying_file = underlying_dir() + fs_path;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
593 FileHandler *handler = open_file_cache_.FindAndPin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
594 if (handler == NULL) {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
595 int filedes = open(underlying_file.c_str(), O_RDONLY);
680f590 @hzeller o report opening error.
authored
596 if (filedes < 0)
597 return NULL;
b5c1e96 @hzeller o more useful output on status server.
authored
598 ++total_file_openings_;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
599 handler = CreateFromDescriptor(filedes, config_idx,
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
600 fs_path, underlying_file);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
601 handler = open_file_cache_.InsertPinned(cache_key, handler);
b5c1e96 @hzeller o more useful output on status server.
authored
602 } else {
603 ++total_file_reopen_;
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
604 }
605 return handler;
e9c07dd @hzeller o add documentation
authored
606 }
607
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
608 int FolveFilesystem::StatByFilename(const char *fs_path, struct stat *st) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
609 const std::string cache_key = CacheKey(current_cfg_index_, fs_path);
610 FileHandler *handler = open_file_cache_.FindAndPin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
611 if (handler == 0)
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
612 return -1;
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
613 ssize_t result = handler->Stat(st);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
614 open_file_cache_.Unpin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
615 return result;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
616 }
617
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
618 void FolveFilesystem::Close(const char *fs_path, const FileHandler *handler) {
619 const std::string cache_key = CacheKey(handler->filter_id(), fs_path);
620 open_file_cache_.Unpin(cache_key);
621 }
622
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
623 bool FolveFilesystem::ListDirectory(const std::string &fs_dir,
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
624 const std::string &suffix,
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
625 std::set<std::string> *files) {
626 const std::string real_dir = underlying_dir() + fs_dir;
627 DIR *dp = opendir(real_dir.c_str());
628 if (dp == NULL) return false;
629 struct dirent *dent;
630 while ((dent = readdir(dp)) != NULL) {
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
631 if (!folve::HasSuffix(dent->d_name, suffix))
632 continue;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
633 files->insert(fs_dir + dent->d_name);
634 }
635 closedir(dp);
636 return true;
637 }
638
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
639 void FolveFilesystem::SwitchCurrentConfigIndex(int i) {
640 if (i < 0 || i >= (int) config_dirs_.size())
641 return;
5830c5a @hzeller o Switch logging to syslog()
authored
642 if (i != current_cfg_index_) {
643 if (i == 0) {
644 syslog(LOG_INFO, "Switching to pass-through mode.");
645 } else {
646 syslog(LOG_INFO, "Switching config directory to '%s'",
647 config_dirs()[i].c_str());
648 }
649 current_cfg_index_ = i;
650 }
e9c07dd @hzeller o add documentation
authored
651 }
fd65cee @hzeller o add basic zita filter.
authored
652
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
653 static bool IsDirectory(const std::string &path) {
654 if (path.empty()) return false;
655 struct stat st;
656 if (stat(path.c_str(), &st) != 0)
657 return false;
658 return (st.st_mode & S_IFMT) == S_IFDIR;
659 }
660
5830c5a @hzeller o Switch logging to syslog()
authored
661 void FolveFilesystem::SetDebugMode(bool b) {
249865a @hzeller o Make debug mode switchable in UI.
authored
662 if (b != global_debug) {
663 syslog(LOG_INFO, "Switch debug mode %s.", b ? "on" : "off");
664 global_debug = b;
665 }
5830c5a @hzeller o Switch logging to syslog()
authored
666 }
667 bool FolveFilesystem::IsDebugMode() const { return global_debug; }
668
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
669 bool FolveFilesystem::CheckInitialized() {
670 if (underlying_dir().empty()) {
671 fprintf(stderr, "Don't know the underlying directory to read from.\n");
672 return false;
673 }
674 if (!IsDirectory(underlying_dir())) {
675 fprintf(stderr, "<underlying-dir>: '%s' not a directory.\n",
676 underlying_dir().c_str());
677 return false;
678 }
679
680 for (size_t i = 1; i < config_dirs_.size(); ++i) {
681 if (!IsDirectory(config_dirs_[i])) {
682 fprintf(stderr, "<config-dir>: '%s' not a directory.\n",
683 config_dirs_[i].c_str());
684 return false;
685 }
686 }
687 if (config_dirs_.size() > 1) {
688 // By default, lets set the index to the first filter the user provided.
689 SwitchCurrentConfigIndex(1);
690 }
691 return true;
fd65cee @hzeller o add basic zita filter.
authored
692 }
Something went wrong with that request. Please try again.