Skip to content

HTTPS clone URL

Subversion checkout URL

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