Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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