-
Notifications
You must be signed in to change notification settings - Fork 4
/
mediasourcebase.cpp
532 lines (450 loc) · 13.2 KB
/
mediasourcebase.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include "mediasourcebase.h"
/// PUBLIC METHODS ///
QString MediaSourceBase::getId()
{
return id;
}
void MediaSourceBase::setId(QString id)
{
this->id = id;
}
QString MediaSourceBase::getName()
{
return name;
}
int MediaSourceBase::getQueuedSamplesCount()
{
return audioData.size();
}
bool MediaSourceBase::getMonitor()
{
return monitorButton->isChecked();
}
qreal MediaSourceBase::getVolume()
{
return (float)volumeSlider->value() / (float)volumeSlider->maximum();
}
QGst::State MediaSourceBase::getState()
{
if (pipeline.isNull())
{
return QGst::StateNull;
}
return pipeline->currentState();
}
QHash<QString, QString> MediaSourceBase::getSourceInfo()
{
QHash<QString, QString> info;
info["online"] = QString("%1").arg(getState());
info["name"] = name;
info["audiobuffersize"] = QString("%1").arg(audioData.size());
info["volume"] = QString("%1").arg(volumeSlider->value() / 1000.0);
info["opacity"] = QString("%1").arg(opacitySlider->value() / 1000.0);
return info;
}
float MediaSourceBase::getGamma()
{
if (gammaElement.isNull())
{
return 1.0;
}
return gammaElement->property("gamma").get<float>();
}
void MediaSourceBase::setGamma(float gamma)
{
if (gammaElement.isNull())
{
return;
}
gammaElement->setProperty("gamma", gamma);
}
float MediaSourceBase::getBrightness()
{
if (videoBalanceElement.isNull())
{
return 0.0;
}
return videoBalanceElement->property("brightness").get<float>();
}
void MediaSourceBase::setBrightness(float brightness)
{
if (videoBalanceElement.isNull())
{
return;
}
videoBalanceElement->setProperty("brightness", brightness);
}
float MediaSourceBase::getContrast()
{
if (videoBalanceElement.isNull())
{
return 1.0;
}
return videoBalanceElement->property("contrast").get<float>();
}
void MediaSourceBase::setContrast(float contrast)
{
if (videoBalanceElement.isNull())
{
return;
}
videoBalanceElement->setProperty("contrast", contrast);
}
float MediaSourceBase::getHue()
{
if (videoBalanceElement.isNull())
{
return 0.0;
}
return videoBalanceElement->property("hue").get<float>();
}
void MediaSourceBase::setHue(float hue)
{
if (videoBalanceElement.isNull())
{
return;
}
videoBalanceElement->setProperty("hue", hue);
}
float MediaSourceBase::getSaturation()
{
if (videoBalanceElement.isNull())
{
return 1.0;
}
return videoBalanceElement->property("saturation").get<float>();
}
void MediaSourceBase::setSaturation(float saturation)
{
if (videoBalanceElement.isNull())
{
return;
}
videoBalanceElement->setProperty("saturation", saturation);
}
int MediaSourceBase::getFlipMode()
{
if (videoFlipElement.isNull())
{
return 1.0;
}
return videoFlipElement->property("method").get<int>();
}
void MediaSourceBase::setFlipMode(int flipMode)
{
if (videoFlipElement.isNull())
{
return;
}
videoFlipElement->setProperty("method", flipMode);
}
MediaSourceBase::MediaSourceBase(QWidget *parent = 0) :
QGroupBox(parent)
{
}
MediaSourceBase::~MediaSourceBase()
{
if (!pipeline.isNull())
{
pipeline->setState(QGst::StateNull);
}
}
void MediaSourceBase::init(QSlider *leftMeterSlider, QSlider *rightMeterSlider, QLabel *meterLabel,
QPushButton *goButton, QGraphicsView *previewGraphicsView,
QSlider *opacitySlider, QCheckBox *fadeCheckBox, QPushButton *monitorButton,
QSlider *volumeSlider, QCheckBox *syncVolToOpacityCheckBox)
{
this->leftMeterSlider = leftMeterSlider;
this->rightMeterSlider = rightMeterSlider;
this->meterLabel = meterLabel;
this->goButton = goButton;
this->previewGraphicsView = previewGraphicsView;
this->opacitySlider = opacitySlider;
this->fadeCheckBox = fadeCheckBox;
this->monitorButton = monitorButton;
this->volumeSlider = volumeSlider;
this->syncVolToOpacityCheckBox = syncVolToOpacityCheckBox;
volumeSlider->setValue(0);
opacitySlider->setValue(0);
volumeSlider->setEnabled(false);
opacitySlider->setEnabled(false);
monitorButton->setEnabled(false);
goButton->setEnabled(false);
setAutoFillBackground(true);
setAlignment(Qt::AlignHCenter);
connect(opacitySlider, SIGNAL(valueChanged(int)), this, SLOT(opcatiyFaderChanged()));
connect(volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(volumeFaderChanged()));
connect(goButton, SIGNAL(clicked()), this, SLOT(goButtonClicked()));
connect(monitorButton, SIGNAL(toggled(bool)), this, SIGNAL(preListenChanged(bool)));
previewGraphicsView->setScene(&scene);
previewPixmapItem = scene.addPixmap(QPixmap());
connect(&videoSink, SIGNAL(newImage(QImage)), this, SLOT(newVideoFrameFromSink(QImage)));
connect(&audioSink, SIGNAL(newAudioBuffer(QByteArray)), this, SLOT(newAudioBufferFromSink(QByteArray)));
connect(&fadeStepTimer, SIGNAL(timeout()), this, SLOT(fadeTimeEvent()));
connect(&audioPeakTimer, SIGNAL(timeout()), this, SLOT(audioClipOff()));
defaultPalette = meterLabel->palette();
connect(&audioDiscontTimer, SIGNAL(timeout()), this, SLOT(audioDiscontOff()));
}
float MediaSourceBase::dequeueSample()
{
return audioData.dequeue();
}
void MediaSourceBase::clearQueuedSamples()
{
audioData.clear();
}
void MediaSourceBase::trimQueuedSamples(int remainingSamplesCount)
{
int numToTrim = audioData.size() - remainingSamplesCount;
for (int i = 0; i < numToTrim; i++)
{
audioData.dequeue();
}
}
void MediaSourceBase::onBusMessage(const QGst::MessagePtr &message)
{
qDebug() << "SOURCEPIPELINE" << id << "MESSAGE" << message->type() << message->typeName();
switch (message->type()) {
case QGst::MessageEos:
sourceOffline();
emit pipelineEOS();
break;
case QGst::MessageError:
qCritical() << "SOURCEPIPELINE" << id << "ERROR:" << message.staticCast<QGst::ErrorMessage>()->error() << message.staticCast<QGst::ErrorMessage>()->debugMessage();
sourceOffline();
emit pipelineError(message.staticCast<QGst::ErrorMessage>()->error().message(), message.staticCast<QGst::ErrorMessage>()->debugMessage());
break;
case QGst::MessageStateChanged:
qDebug() << "NEWSTATE:" << message.staticCast<QGst::StateChangedMessage>()->newState();
emit pipelineNewState(message.staticCast<QGst::StateChangedMessage>()->newState());
break;
default:
break;
}
updateBackground();
}
/// PUBLIC SLOTS ///
void MediaSourceBase::setVideoOpacity(qreal opacity, bool diff)
{
qreal newValue;
if (diff)
{
qreal currentValue = opacitySlider->value() / 1000.0;
newValue = currentValue + opacity;
}
else
{
newValue = opacity;
}
if (newValue > 1.0) newValue = 1.0;
if (newValue < 0.0) newValue = 0.0;
opacitySlider->setValue(newValue * 1000);
opcatiyFaderChanged();
}
void MediaSourceBase::setVolume(qreal volume, bool diff)
{
qreal newValue;
if (diff)
{
qreal currentValue = volumeSlider->value() / 1000.0;
newValue = currentValue + volume;
}
else
{
newValue = volume;
}
if (newValue > 1.0) newValue = 1.0;
if (newValue < 0.0) newValue = 0.0;
volumeSlider->setValue(newValue * 1000);
volumeFaderChanged();
}
void MediaSourceBase::setPreListen(bool value)
{
monitorButton->setChecked(value);
}
void MediaSourceBase::fadeStart(qreal stepSize, qint16 interval)
{
if (stepSize == 0) {
fadeStepTimer.stop();
return;
}
if (fadeCheckBox->isChecked())
{
// Legacy behavior: Fade
fadeStepSize = stepSize;
fadeStepTimer.start(interval);
}
else
{
// new behaviour: No fade, just GO ;)
if (stepSize > 0)
{
setVideoOpacity(1.0);
}
else
{
setVideoOpacity(0.0);
}
}
}
void MediaSourceBase::audioDiscontOn()
{
QPalette pal = meterLabel->palette();
pal.setColor(QPalette::WindowText, Qt::white);
meterLabel->setPalette(pal);
audioDiscontTimer.start(1000);
}
void MediaSourceBase::opcatiyFaderChanged()
{
if (syncVolToOpacityCheckBox->isChecked())
{
setVolume(opacitySlider->value() / 1000.0);
volumeFaderChanged();
}
updateBackground();
emit opacityChanged(opacitySlider->value() / 1000.0);
}
void MediaSourceBase::volumeFaderChanged()
{
updateBackground();
emit volumeChanged(volumeSlider->value() / 1000.0);
}
void MediaSourceBase::goButtonClicked()
{
emit fadeMeIn(true);
}
void MediaSourceBase::sourceOnline()
{
qDebug() << "SOURCE" << id << "IS NOW ONLINE";
volumeSlider->setValue(0);
opacitySlider->setValue(0);
volumeSlider->setEnabled(true);
opacitySlider->setEnabled(true);
monitorButton->setEnabled(true);
goButton->setEnabled(true);
updateBackground();
}
void MediaSourceBase::sourceOffline()
{
qDebug() << "SOURCE" << id << "LEFT US";
leftMeterSlider->setValue(0);
rightMeterSlider->setValue(0);
volumeSlider->setValue(0);
opacitySlider->setValue(0);
volumeSlider->setEnabled(false);
opacitySlider->setEnabled(false);
monitorButton->setEnabled(false);
goButton->setEnabled(false);
audioData.clear();
name = "";
if (!pipeline.isNull())
{
pipeline->setState(QGst::StateNull);
pipeline.clear();
}
updateBackground();
}
void MediaSourceBase::updateBackground()
{
//qDebug() << "BASE UPDATEBACKGROUND" << id << "Current pipeline state:" << getState();
QPalette p = palette();
switch (getState())
{
case QGst::StateReady:
case QGst::StatePaused:
// Source online but pipeline not PLAYING ... (Still buffering input)
p.setColor(QPalette::Window, Qt::yellow);
goButton->setEnabled(false);
break;
case QGst::StatePlaying:
if ((opacitySlider->value() != 0) || (volumeSlider->value() != 0)) {
// Source online and ONAIR (RED)
p.setColor(QPalette::Window, Qt::red);
goButton->setEnabled(true);
} else {
// Source online but not ONAIR (PALE GREEN)
p.setColor(QPalette::Window, QColor(180, 255, 180));
goButton->setEnabled(true);
}
break;
case QGst::StateNull:
// Source offline (LIGHT GRAY)
p.setColor(QPalette::Window, Qt::lightGray);
break;
default:
break;
}
setPalette(p);
}
void MediaSourceBase::fadeTimeEvent()
{
if ((fadeStepSize > 0) && ((opacitySlider->value() / 1000.0 + fadeStepSize) >= 1.0)) {
setVideoOpacity(1.0);
fadeStepTimer.stop();
} else if ((fadeStepSize < 0) && ((opacitySlider->value() / 1000.0 + fadeStepSize) <= 0.0)) {
setVideoOpacity(0.0);
fadeStepTimer.stop();
} else {
setVideoOpacity(fadeStepSize, true);
}
}
void MediaSourceBase::newVideoFrameFromSink(QImage image)
{
//qDebug() << "MEDIASOURCE" << id << "NEW VIDEO DRAME FROM SINK.";
// display it in the preview
QImage previewImage = image.scaled(320, 180, Qt::KeepAspectRatio, Qt::FastTransformation);
previewPixmapItem->setPixmap(QPixmap::fromImage(previewImage));
// Emit it so that the mainWindow can pick it up for the main image
emit newVideoFrame(image);
}
void MediaSourceBase::newAudioBufferFromSink(QByteArray data)
{
//qDebug() << "MEDIASOURCE" << id << "NEW AUDIO BUFFER FROM SINK. SIZE:" << data.size();
float* buffer = (float*)data.data();
// Most simple peak calculation
float maxleft = 0.0;
float maxright = 0.0;
if (audioData.size() < 131070)
{
// No use of omp parallel for here since the .enqueue() needs to be done in the correct order!
for (int i = 0; i < (data.size() / (int)sizeof(float)); i = i + 2)
{
maxleft = std::max(maxleft, buffer[i]);
maxright = std::max(maxright, buffer[i + 1]);
audioData.enqueue(buffer[i]);
audioData.enqueue(buffer[i+1]);
}
}
else
{
// The samples are not saved anywhere and are dropped. THIS IS AUDIBLE!
qWarning() << "AUDIO BUFFER OVERFLOW source" << getId() << "Samples have been dropped";
audioDiscontOn();
}
// Set the new value to the avarage of the old and the new value. This makes the display less twitchy
leftMeterSlider->setValue(((maxleft * 100) + leftMeterSlider->value()) / 2);
rightMeterSlider->setValue(((maxright * 100) + rightMeterSlider->value()) / 2);
if ((maxleft >= 1.0) || (maxright >= 1.0))
{
audioClipOn();
}
}
void MediaSourceBase::audioClipOn()
{
QFont font = meterLabel->font();
font.setBold(true);
meterLabel->setFont(font);
audioPeakTimer.start(1000);
}
void MediaSourceBase::audioClipOff()
{
QFont font = meterLabel->font();
font.setBold(false);
meterLabel->setFont(font);
audioPeakTimer.stop();
}
void MediaSourceBase::audioDiscontOff()
{
meterLabel->setPalette(defaultPalette);
audioDiscontTimer.stop();
}