-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecodergeneric.cpp
355 lines (291 loc) · 10.7 KB
/
decodergeneric.cpp
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
/*
This file is part of Waver
Copyright (C) 2021 Peter Papp
Please visit https://launchpad.net/waver for details
*/
#include "decodergeneric.h"
DecoderGeneric::DecoderGeneric(RadioTitleCallback::RadioTitleCallbackInfo radioTitleCallbackInfo, QObject *parent) : QObject(parent)
{
this->radioTitleCallbackInfo = radioTitleCallbackInfo;
audioDecoder = nullptr;
file = nullptr;
networkSource = nullptr;
networkDeviceSet = false;
decodedMicroseconds = 0;
decodeDelay = 2500;
waitUnderBytes = 4096;
removeBeginningSilence = false;
silenceThreshold = 0.0;
networkThread.setObjectName("decodernetwork");
}
DecoderGeneric::~DecoderGeneric()
{
if (audioDecoder != nullptr) {
audioDecoder->stop();
audioDecoder->deleteLater();
}
networkThread.quit();
networkThread.wait();
if (file != nullptr) {
file->close();
file->deleteLater();
}
}
void DecoderGeneric::decoderBufferReady()
{
// just to be on the safe side (seems like it happens sometimes when there's a decoder error)
if (!audioDecoder->bufferAvailable()) {
return;
}
QAudioBuffer bufferReady = audioDecoder->read();
if (!bufferReady.isValid()) {
return;
}
char *data = bufferReady.data<char>();
int i = 0;
while (removeBeginningSilence && (silenceThreshold > 0.0) && (i < bufferReady.byteCount())) {
if (decodedFormat.sampleType() == QAudioFormat::SignedInt) {
switch (decodedFormat.sampleSize()) {
case 8:
if (abs(*(qint8 *)(data + i)) > silenceThreshold) {
removeBeginningSilence = false;
continue;
}
i++;
break;
case 16:
if (abs(*(qint16 *)(data + i)) > silenceThreshold) {
removeBeginningSilence = false;
continue;
}
i += 2;
break;
case 32:
if (abs(*(qint32 *)(data + i)) > silenceThreshold) {
removeBeginningSilence = false;
continue;
}
i += 4;
}
}
else if (decodedFormat.sampleType() == QAudioFormat::UnSignedInt) {
switch (decodedFormat.sampleSize()) {
case 8:
if (*(quint8 *)(data + i) > silenceThreshold) {
removeBeginningSilence = false;
continue;
}
i++;
break;
case 16:
if (*(quint16 *)(data + i) > silenceThreshold) {
removeBeginningSilence = false;
continue;
}
i += 2;
break;
case 32:
if (*(quint32 *)(data + i) > silenceThreshold) {
removeBeginningSilence = false;
continue;
}
i += 4;
}
}
}
if (i > 0) {
while (i % decodedFormat.bytesForFrames(1)) {
i++;
}
if (i >= bufferReady.byteCount()) {
return;
}
QByteArray temp(static_cast<const char *>(bufferReady.constData()), bufferReady.byteCount());
temp.remove(0, i);
bufferReady = QAudioBuffer(temp, decodedFormat, decodedMicroseconds);
}
removeBeginningSilence = false;
decodedMicroseconds += bufferReady.format().durationForBytes(bufferReady.byteCount());
QAudioBuffer *copy = new QAudioBuffer(QByteArray(static_cast<const char *>(bufferReady.constData()), bufferReady.byteCount()), bufferReady.format(), bufferReady.startTime());
emit bufferAvailable(copy);
if (decodeDelay > 0) {
QThread::currentThread()->usleep(decodeDelay);
}
// must prevent input underrun, reading 0 bytes is show-stopper for the decoder
if (networkSource != nullptr) {
waitMutex.lock();
while ((networkSource->realBytesAvailable() < waitUnderBytes) && !networkSource->isDownloadFinished() && !QThread::currentThread()->isInterruptionRequested()) {
waitCondition.wait(&waitMutex, 1000);
emit networkBufferChanged();
}
waitMutex.unlock();
}
}
void DecoderGeneric::decoderError(QAudioDecoder::Error error)
{
QString errorStr;
if (audioDecoder != nullptr) {
errorStr = audioDecoder->errorString();
}
if (errorStr.length() == 0) {
switch (error) {
case QAudioDecoder::NoError:
errorStr = tr("No error has occurred.");
break;
case QAudioDecoder::ResourceError:
errorStr = tr("A media resource couldn't be resolved.");
break;
case QAudioDecoder::FormatError:
errorStr = tr("The format of a media resource isn't supported.");
break;
case QAudioDecoder::AccessDeniedError:
errorStr = tr("There are not the appropriate permissions to play a media resource.");
break;
case QAudioDecoder::ServiceMissingError:
errorStr = tr("A valid service was not found, decoding cannot proceed.");
break;
default:
errorStr = tr("Unspecified media decoder error.");
}
}
emit errorMessage(tr("Audio decoder error"), errorStr);
if (audioDecoder != nullptr) {
audioDecoder->stop();
}
}
void DecoderGeneric::decoderFinished()
{
emit finished();
}
double DecoderGeneric::downloadPercent()
{
if (isFile() || (networkSource == nullptr)) {
return 0;
}
qint64 total = isRadio ? networkSource->mostRealBytesAvailable() : networkSource->size();
if (total <= 0) {
return 0;
}
double percent = static_cast<double>(isRadio ? networkSource->realBytesAvailable() : networkSource->downloadedSize()) / total;
if (percent < 0) {
percent = 0;
}
if (percent > 1) {
percent = 1;
}
return percent;
}
qint64 DecoderGeneric::getDecodedMicroseconds()
{
return decodedMicroseconds;
}
bool DecoderGeneric::isFile()
{
return url.isLocalFile();
}
void DecoderGeneric::networkChanged()
{
emit networkBufferChanged();
}
void DecoderGeneric::networkError(QString errorString)
{
emit errorMessage(tr("Network error"), errorString);
emit networkStarting(false);
if (audioDecoder != nullptr) {
audioDecoder->stop();
}
}
void DecoderGeneric::networkSessionExpired()
{
emit sessionExpired();
}
void DecoderGeneric::networkInfo(QString infoString)
{
emit infoMessage(infoString);
}
void DecoderGeneric::networkReady()
{
emit networkStarting(false);
if (audioDecoder->state() == QAudioDecoder::StoppedState) {
networkSource->open(QIODevice::ReadOnly);
audioDecoder->setSourceDevice(networkSource);
audioDecoder->start();
}
}
void DecoderGeneric::run()
{
if (url.isEmpty()) {
emit errorMessage(tr("Can not decode audio"), tr("Decoder URL is empty"));
}
}
void DecoderGeneric::setDecodeDelay(unsigned long microseconds)
{
decodeDelay = microseconds;
}
void DecoderGeneric::setParameters(QUrl url, QAudioFormat decodedFormat, qint64 waitUnderBytes, bool isRadio, bool removeBeginningSilence)
{
// can be set only once
if (this->url.isEmpty()) {
this->url = url;
this->decodedFormat = decodedFormat;
this->waitUnderBytes = waitUnderBytes;
this->isRadio = isRadio;
this->removeBeginningSilence = removeBeginningSilence;
if (decodedFormat.sampleType() == QAudioFormat::SignedInt) {
switch (decodedFormat.sampleSize()) {
case 8:
silenceThreshold = pow(10, SILENCE_THRESHOLD_DB / 10) * std::numeric_limits<qint8>::max();
break;
case 16:
silenceThreshold = pow(10, SILENCE_THRESHOLD_DB / 10) * std::numeric_limits<qint16>::max();
break;
case 32:
silenceThreshold = pow(10, SILENCE_THRESHOLD_DB / 10) * std::numeric_limits<qint32>::max();
}
}
else if (decodedFormat.sampleType() == QAudioFormat::UnSignedInt) {
switch (decodedFormat.sampleSize()) {
case 8:
silenceThreshold = pow(10, SILENCE_THRESHOLD_DB / 10) * std::numeric_limits<quint8>::max();
break;
case 16:
silenceThreshold = pow(10, SILENCE_THRESHOLD_DB / 10) * std::numeric_limits<quint16>::max();
break;
case 32:
silenceThreshold = pow(10, SILENCE_THRESHOLD_DB / 10) * std::numeric_limits<quint32>::max();
}
}
}
}
void DecoderGeneric::start()
{
if (url.isEmpty()) {
emit errorMessage(tr("Can not decode audio"), tr("Decoder URL is empty"));
return;
}
audioDecoder = new QAudioDecoder();
audioDecoder->setAudioFormat(decodedFormat);
connect(audioDecoder, SIGNAL(bufferReady()), this, SLOT(decoderBufferReady()));
connect(audioDecoder, SIGNAL(finished()), this, SLOT(decoderFinished()));
connect(audioDecoder, SIGNAL(error(QAudioDecoder::Error)), this, SLOT(decoderError(QAudioDecoder::Error)));
if (url.isLocalFile()) {
file = new QFile(url.toLocalFile());
file->open(QFile::ReadOnly);
audioDecoder->setSourceDevice(file);
audioDecoder->start();
}
else {
networkSource = new DecoderGenericNetworkSource(url, &waitCondition, radioTitleCallbackInfo);
networkSource->setErrorOnUnderrun(false);
networkSource->moveToThread(&networkThread);
connect(&networkThread, SIGNAL(started()), networkSource, SLOT(run()));
connect(&networkThread, SIGNAL(finished()), networkSource, SLOT(deleteLater()));
connect(networkSource, SIGNAL(ready()), this, SLOT(networkReady()));
connect(networkSource, SIGNAL(changed()), this, SLOT(networkChanged()));
connect(networkSource, SIGNAL(error(QString)), this, SLOT(networkError(QString)));
connect(networkSource, SIGNAL(info(QString)), this, SLOT(networkInfo(QString)));
connect(networkSource, SIGNAL(sessionExpired()), this, SLOT(networkSessionExpired()));
emit networkStarting(true);
networkThread.start();
}
}