-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathairplay.cpp
More file actions
165 lines (137 loc) · 3.65 KB
/
airplay.cpp
File metadata and controls
165 lines (137 loc) · 3.65 KB
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
#include "airplay.h"
#include <QTcpSocket>
#include <QTimer>
AirPlay::AirPlay(QString h, int p, QObject *parent) : QObject(parent)
{
position.duration = 0.0;
position.position = 0.0;
host = h;
port = p;
socket = new QTcpSocket(this);
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(sockStatus(QAbstractSocket::SocketState)));
status = CONNECTING;
connect(&timerTryConnect, SIGNAL(timeout()), this, SLOT(tryConnect()));
timerTryConnect.start(5000);
tryConnect();
}
void AirPlay::tryConnect()
{
// test connection
qDebug() << "attempting connection to" << host << port;
socket->abort();
socket->connectToHost(host, port);
}
bool AirPlay::play(QString url)
{
if (status == CONNECTING) return false;
QString body;
body.append("Content-Location: "+url+"\n");
body.append("Start-Position: 0.0\n");
QTextStream os(socket);
os.setAutoDetectUnicode(true);
os << "POST /play HTTP/1.1" << "\r\n";
os << "Content-Length: " << QString::number(body.length()) << "\r\n";
os << "\r\n";
os << body;
os.flush();
status = PLAYING;
return true;
}
void AirPlay::stop()
{
if (status == CONNECTING) return;
timerPos.stop();
socket->readAll(); // clear buffer
QTextStream os(socket);
os.setAutoDetectUnicode(true);
os << "POST /stop HTTP/1.1" << "\r\n";
os << "\r\n";
os.flush();
socket->waitForReadyRead(1000);
// reset positon
position.duration = 0.0;
position.position = 0.0;
// close socket
socket->close();
}
void AirPlay::pause()
{
if (status == CONNECTING) return;
socket->readAll(); // clear buffer
QTextStream os(socket);
os.setAutoDetectUnicode(true);
os << "POST /rate?value=0.0 HTTP/1.1" << "\r\n";
os << "\r\n";
os.flush();
socket->waitForReadyRead(1000);
status = PAUSED;
}
void AirPlay::resume()
{
if (status == CONNECTING) return;
socket->readAll(); // clear buffer
QTextStream os(socket);
os.setAutoDetectUnicode(true);
os << "POST /rate?value=1.0 HTTP/1.1" << "\r\n";
os << "\r\n";
os.flush();
socket->waitForReadyRead(1000);
status = PLAYING;
}
void AirPlay::pos()
{
if (status == CONNECTING) return;
socket->readAll(); // clear buffer
QTextStream os(socket);
os.setAutoDetectUnicode(true);
os << "GET /scrub HTTP/1.1" << "\r\n";
os << "\r\n";
os.flush();
// read response
if (socket->waitForReadyRead(1000))
{
while(socket->canReadLine())
{
QString ln = socket->readLine().trimmed();
if (ln.startsWith("duration:"))
{
int i = ln.indexOf(":");
position.duration = ln.mid(i+1).trimmed().toInt();
}
else if (ln.startsWith("position:"))
{
int i = ln.indexOf(":");
position.position = ln.mid(i+1).trimmed().toInt();
}
}
}
}
void AirPlay::sockStatus(QAbstractSocket::SocketState s) {
switch(s)
{
case QAbstractSocket::ClosingState:
if (status != CONNECTING) {
timerPos.stop();
break;
}
case QAbstractSocket::ConnectedState:
timerTryConnect.stop();
// get local ip
localIp = socket->localAddress();
// position timer
connect(&timerPos, SIGNAL(timeout()), this, SLOT(pos()));
timerPos.start(1000);
status = CONNECTED;
break;
default:
break;
}
}
AirPlay::~AirPlay()
{
timerTryConnect.stop();
stop();
timerPos.stop();
if (socket->isOpen()) socket->close();
socket->deleteLater();
}