/
ClassAirplay.cpp
66 lines (55 loc) · 1.51 KB
/
ClassAirplay.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
#include "ClassAirplay.h"
Airplay :: Airplay()
{
deviceInfo = QAudioDeviceInfo::defaultOutputDevice();
}
void Airplay :: handleStateChange(QAudio::State state)
{
qDebug() << state;
switch (state)
{
case QAudio::ActiveState:
break;
case QAudio::SuspendedState:
qDebug() << "suspended";
break;
case QAudio::IdleState:
audio->stop();
file.close();
qDebug() << "stopped airplay";
delete audio;
break;
case QAudio::StoppedState:
if (audio->error() != QAudio::NoError)
qWarning() << "Error" + audio->error();
break;
}
}
void Airplay :: start()
{
// read the airplay pipe
file.setFileName("/tmp/shairport-sync-pipe");
file.open(QIODevice::ReadOnly);
// set the audio format
// https://github.com/mikebrady/shairport-sync/issues/126
// Playing raw data 'stdin' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
QAudioFormat format;
format.setSampleRate(44100); // rate
format.setChannelCount(2); // stereo
format.setCodec("audio/pcm"); // raw
format.setSampleSize(16); // 16 bit
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
// check format is supported
if (!deviceInfo.isFormatSupported(format))
qWarning() << "Output format not supported";
// connect signals
audio = new QAudioOutput(deviceInfo, format, this);
connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChange(QAudio::State)));
// start playing
audio->start(&file);
}
void Airplay :: stop()
{
handleStateChange(QAudio::IdleState);
}