1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
// Copyright 2005-2016 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "mumble_pch.hpp"
#include "VoiceRecorder.h"
#include "AudioOutput.h"
#include "ClientUser.h"
#include "Global.h"
#include "ServerHandler.h"
#include "../Timer.h"
VoiceRecorder::RecordBuffer::RecordBuffer(
int recordInfoIndex_,
boost::shared_array<float> buffer_,
int samples_,
quint64 absoluteStartSample_)
: recordInfoIndex(recordInfoIndex_)
, buffer(buffer_)
, samples(samples_)
, absoluteStartSample(absoluteStartSample_) {
// Nothing
}
VoiceRecorder::RecordInfo::RecordInfo(const QString& userName_)
: userName(userName_)
, soundFile(NULL)
, lastWrittenAbsoluteSample(0) {
}
VoiceRecorder::RecordInfo::~RecordInfo() {
if (soundFile) {
// Close libsndfile's handle if we have one.
sf_close(soundFile);
}
}
VoiceRecorder::VoiceRecorder(QObject *p, const Config& config)
: QThread(p)
, m_recordUser(new RecordUser())
, m_timestamp(new Timer())
, m_config(config)
, m_recording(false)
, m_abort(false)
, m_recordingStartTime(QDateTime::currentDateTime())
, m_absoluteSampleEstimation(0) {
// Nothing
}
VoiceRecorder::~VoiceRecorder() {
stop();
wait();
}
QString VoiceRecorder::sanitizeFilenameOrPathComponent(const QString &str) const {
// Trim leading/trailing whitespaces
QString res = str.trimmed();
if (res.isEmpty())
return QLatin1String("_");
#ifdef Q_OS_WIN
// Rules according to http://en.wikipedia.org/wiki/Filename#Comparison_of_file_name_limitations
// and http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
// Make sure name doesn't end in "."
if (res.at(res.length() - 1) == QLatin1Char('.'))
if (res.length() == 255) // Prevents possible infinite recursion later on
res[254] = QLatin1Char('_');
else
res = res.append(QLatin1Char('_'));
// Replace < > : " / \ | ? * as well as chr(0) to chr(31)
res = res.replace(QRegExp(QLatin1String("[<>:\"/\\\\|\\?\\*\\x00-\\x1F]")), QLatin1String("_"));
// Prepend reserved filenames CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9
res = res.replace(QRegExp(QLatin1String("^((CON|PRN|AUX|NUL|COM[1-9]|LPT1[1-9])(\\.|$))"), Qt::CaseInsensitive), QLatin1String("_\\1"));
// Make sure we do not exceed 255 characters
if (res.length() > 255) {
res.truncate(255);
// Call ourselves recursively to make sure we do not end up violating any of our rules because of this
res = sanitizeFilenameOrPathComponent(res);
}
#else
// For the rest just make sure the string doesn't contain a \0 or any forward-slashes
res = res.replace(QRegExp(QLatin1String("\\x00|/")), QLatin1String("_"));
#endif
return res;
}
QString VoiceRecorder::expandTemplateVariables(const QString &path, const QString& userName) const {
// Split path into components
QString res;
QStringList comp = path.split(QLatin1Char('/'));
Q_ASSERT(!comp.isEmpty());
// Create a readable representation of the start date.
QString date(m_recordingStartTime.date().toString(Qt::ISODate));
QString time(m_recordingStartTime.time().toString(QLatin1String("hh-mm-ss")));
QString hostname(QLatin1String("Unknown"));
if (g.sh && g.uiSession != 0) {
unsigned short port;
QString uname, pw;
g.sh->getConnectionInfo(hostname, port, uname, pw);
}
// Create hash which stores the names of the variables with the corresponding values.
// Valid variables are:
// %user Inserts the users name
// %date Inserts the current date
// %time Inserts the current time
// %host Inserts the hostname
QHash<const QString, QString> vars;
vars.insert(QLatin1String("user"), userName);
vars.insert(QLatin1String("date"), date);
vars.insert(QLatin1String("time"), time);
vars.insert(QLatin1String("host"), hostname);
// Reassemble and expand
bool first = true;
foreach(QString str, comp) {
bool replacements = false;
QString tmp;
tmp.reserve(str.length() * 2);
for (int i = 0; i < str.size(); ++i) {
bool replaced = false;
if (str[i] == QLatin1Char('%')) {
QHashIterator<const QString, QString> it(vars);
while (it.hasNext()) {
it.next();
if (str.midRef(i + 1, it.key().length()) == it.key()) {
i += it.key().length();
tmp += it.value();
replaced = true;
replacements = true;
break;
}
}
}
if (!replaced)
tmp += str[i];
}
str = tmp;
if (replacements)
str = sanitizeFilenameOrPathComponent(str);
if (first) {
first = false;
res.append(str);
} else {
res.append(QLatin1Char('/') + str);
}
}
return res;
}
int VoiceRecorder::indexForUser(const ClientUser *clientUser) const {
Q_ASSERT(!m_config.mixDownMode || clientUser == NULL);
return (m_config.mixDownMode) ? 0 : clientUser->uiSession;
}
SF_INFO VoiceRecorder::createSoundFileInfo() const {
Q_ASSERT(m_config.sampleRate != 0);
// When adding new formats make sure to properly configure needed additional
// behavior after opening the file handle (e.g. to enable clipping).
// Convert |fmFormat| to a SF_INFO structure for libsndfile.
SF_INFO sfinfo;
switch (m_config.recordingFormat) {
case VoiceRecorderFormat::WAV:
default:
sfinfo.frames = 0;
sfinfo.samplerate = m_config.sampleRate;
sfinfo.channels = 1;
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_24;
sfinfo.sections = 0;
sfinfo.seekable = 0;
qWarning() << "VoiceRecorder: recording started to" << m_config.fileName << "@" << m_config.sampleRate << "hz in WAV format";
break;
#ifndef NO_VORBIS_RECORDING
case VoiceRecorderFormat::VORBIS:
sfinfo.frames = 0;
sfinfo.samplerate = m_config.sampleRate;
sfinfo.channels = 1;
sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS;
sfinfo.sections = 0;
sfinfo.seekable = 0;
qWarning() << "VoiceRecorder: recording started to" << m_config.fileName << "@" << m_config.sampleRate << "hz in OGG/Vorbis format";
break;
#endif
case VoiceRecorderFormat::AU:
sfinfo.frames = 0;
sfinfo.samplerate = m_config.sampleRate;
sfinfo.channels = 1;
sfinfo.format = SF_ENDIAN_CPU | SF_FORMAT_AU | SF_FORMAT_FLOAT;
sfinfo.sections = 0;
sfinfo.seekable = 0;
qWarning() << "VoiceRecorder: recording started to" << m_config.fileName << "@" << m_config.sampleRate << "hz in AU format";
break;
case VoiceRecorderFormat::FLAC:
sfinfo.frames = 0;
sfinfo.samplerate = m_config.sampleRate;
sfinfo.channels = 1;
sfinfo.format = SF_FORMAT_FLAC | SF_FORMAT_PCM_24;
sfinfo.sections = 0;
sfinfo.seekable = 0;
qWarning() << "VoiceRecorder: recording started to" << m_config.fileName << "@" << m_config.sampleRate << "hz in FLAC format";
break;
}
Q_ASSERT(sf_format_check(&sfinfo));
return sfinfo;
}
bool VoiceRecorder::ensureFileIsOpenedFor(SF_INFO& soundFileInfo, boost::shared_ptr<RecordInfo>& ri) {
if (ri->soundFile != NULL) {
// Nothing to do
return true;
}
QString filename = expandTemplateVariables(m_config.fileName, ri->userName);
// Try to find a unique filename.
{
int cnt = 1;
QString nf(filename);
QFileInfo tfi(filename);
while (QFile::exists(nf)) {
nf = tfi.path()
+ QLatin1Char('/')
+ tfi.completeBaseName()
+ QString(QLatin1String(" (%1).")).arg(cnt)
+ tfi.suffix();
++cnt;
}
filename = nf;
}
qWarning() << "Recorder opens file" << filename;
QFileInfo fi(filename);
// Create the target path.
if (!QDir().mkpath(fi.absolutePath())) {
qWarning() << "Failed to create target directory: " << fi.absolutePath();
m_recording = false;
emit error(CreateDirectoryFailed, tr("Recorder failed to create directory '%1'").arg(fi.absolutePath()));
emit recording_stopped();
return false;
}
#ifdef Q_OS_WIN
// This is needed for unicode filenames on Windows.
ri->soundFile = sf_wchar_open(filename.toStdWString().c_str(), SFM_WRITE, &soundFileInfo);
#else
ri->soundFile = sf_open(qPrintable(filename), SFM_WRITE, &soundFileInfo);
#endif
if (ri->soundFile == NULL) {
qWarning() << "Failed to open file for recorder: "<< sf_strerror(NULL);
m_recording = false;
emit error(CreateFileFailed, tr("Recorder failed to open file '%1'").arg(filename));
emit recording_stopped();
return false;
}
// Store the username in the title attribute of the file (if supported by the format).
sf_set_string(ri->soundFile, SF_STR_TITLE, qPrintable(ri->userName));
// Enable hard-clipping for non-float formats to prevent wrapping
if ((soundFileInfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_FLOAT &&
(soundFileInfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_VORBIS) {
sf_command(ri->soundFile, SFC_SET_CLIPPING, NULL, SF_TRUE);
}
return true;
}
void VoiceRecorder::run() {
Q_ASSERT(!m_recording);
if (g.sh && g.sh->uiVersion < 0201003)
return;
SF_INFO soundFileInfo = createSoundFileInfo();
m_recording = true;
emit recording_started();
forever {
// Sleep until there is new data for us to process.
m_sleepLock.lock();
m_sleepCondition.wait(&m_sleepLock);
if (!m_recording || m_abort || (g.sh && g.sh->uiVersion < 0201003)) {
m_sleepLock.unlock();
break;
}
while (!m_abort && !m_recordBuffer.isEmpty()) {
boost::shared_ptr<RecordBuffer> rb;
{
QMutexLocker l(&m_bufferLock);
rb = m_recordBuffer.takeFirst();
}
// Create the file for this RecordInfo instance if it's not yet open.
Q_ASSERT(m_recordInfo.contains(rb->recordInfoIndex));
boost::shared_ptr<RecordInfo> ri = m_recordInfo.value(rb->recordInfoIndex);
if (!ensureFileIsOpenedFor(soundFileInfo, ri)) {
return;
}
const qint64 missingSamples = rb->absoluteStartSample - ri->lastWrittenAbsoluteSample;
static const qint64 heuristicSilenceThreshold = m_config.sampleRate / 10; // 100ms
if (missingSamples > heuristicSilenceThreshold) {
static const qint64 maxSamplesPerIteration = m_config.sampleRate * 1; // 1s
const bool requeue = missingSamples > maxSamplesPerIteration;
// Write |missingSamples| samples of silence up to |maxSamplesPerIteration|
const float buffer[1024] = {};
const qint64 silenceToWrite = std::min(missingSamples, maxSamplesPerIteration);
qint64 rest = silenceToWrite;
for (; rest > 1024; rest -= 1024)
sf_write_float(ri->soundFile, buffer, 1024);
if (rest > 0)
sf_write_float(ri->soundFile, buffer, rest);
ri->lastWrittenAbsoluteSample += silenceToWrite;
if (requeue) {
// Requeue the writing for this buffer to keep thread responsive
QMutexLocker l(&m_bufferLock);
m_recordBuffer.prepend(rb);
continue;
}
}
// Write the audio buffer and update the timestamp in |ri|.
sf_write_float(ri->soundFile, rb->buffer.get(), rb->samples);
ri->lastWrittenAbsoluteSample += rb->samples;
}
m_sleepLock.unlock();
}
m_recording = false;
{
QMutexLocker l(&m_bufferLock);
m_recordInfo.clear();
m_recordBuffer.clear();
}
emit recording_stopped();
qWarning() << "VoiceRecorder: recording stopped";
}
void VoiceRecorder::stop(bool force) {
// Tell the main loop to terminate and wake up the sleep lock.
m_recording = false;
m_abort = force;
m_sleepCondition.wakeAll();
}
void VoiceRecorder::prepareBufferAdds() {
// Should be ms accurat
m_absoluteSampleEstimation =
(m_timestamp->elapsed() / 1000) * (m_config.sampleRate / 1000);
}
void VoiceRecorder::addBuffer(const ClientUser *clientUser,
boost::shared_array<float> buffer,
int samples) {
Q_ASSERT(!m_config.mixDownMode || clientUser == NULL);
if (!m_recording)
return;
// Create a new RecordInfo object if this is a new user.
const int index = indexForUser(clientUser);
if (!m_recordInfo.contains(index)) {
boost::shared_ptr<RecordInfo> ri = boost::make_shared<RecordInfo>(
m_config.mixDownMode ? QLatin1String("Mixdown")
: clientUser->qsName);
m_recordInfo.insert(index, ri);
}
{
// Save the buffer in |qlRecordBuffer|.
QMutexLocker l(&m_bufferLock);
boost::shared_ptr<RecordBuffer> rb = boost::make_shared<RecordBuffer>(
index, buffer, samples, m_absoluteSampleEstimation);
m_recordBuffer << rb;
}
// Tell the main loop that we have new audio data.
m_sleepCondition.wakeAll();
}
quint64 VoiceRecorder::getElapsedTime() const {
return m_timestamp->elapsed();
}
RecordUser &VoiceRecorder::getRecordUser() const {
return *m_recordUser;
}
bool VoiceRecorder::isInMixDownMode() const {
return m_config.mixDownMode;
}
QString VoiceRecorderFormat::getFormatDescription(VoiceRecorderFormat::Format fm) {
switch (fm) {
case VoiceRecorderFormat::WAV:
return VoiceRecorder::tr(".wav - Uncompressed");
#ifndef NO_VORBIS_RECORDING
case VoiceRecorderFormat::VORBIS:
return VoiceRecorder::tr(".ogg (Vorbis) - Compressed");
#endif
case VoiceRecorderFormat::AU:
return VoiceRecorder::tr(".au - Uncompressed");
case VoiceRecorderFormat::FLAC:
return VoiceRecorder::tr(".flac - Lossless compressed");
default:
return QString();
}
}
QString VoiceRecorderFormat::getFormatDefaultExtension(VoiceRecorderFormat::Format fm) {
switch (fm) {
case VoiceRecorderFormat::WAV:
return QLatin1String("wav");
#ifndef NO_VORBIS_RECORDING
case VoiceRecorderFormat::VORBIS:
return QLatin1String("ogg");
#endif
case VoiceRecorderFormat::AU:
return QLatin1String("au");
case VoiceRecorderFormat::FLAC:
return QLatin1String("flac");
default:
return QString();
}
}
|