10 #include <QNetworkInterface>
11 #include <QCoreApplication>
13 #include <QCryptographicHash>
14 #if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
15 #include <QStringConverter>
39 #define LOC QString("AirPlay: ")
48 static const QString
SERVER_INFO {
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" \
49 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\r\n"\
50 "<plist version=\"1.0\">\r\n"\
52 "<key>deviceid</key>\r\n"\
53 "<string>%1</string>\r\n"\
54 "<key>features</key>\r\n"\
55 "<integer>119</integer>\r\n"\
56 "<key>model</key>\r\n"\
57 "<string>MythTV,1</string>\r\n"\
58 "<key>protovers</key>\r\n"\
59 "<string>1.0</string>\r\n"\
60 "<key>srcvers</key>\r\n"\
61 "<string>%1</string>\r\n"\
65 static const QString
EVENT_INFO {
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\r\n" \
66 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\r\n"\
67 "<plist version=\"1.0\">\r\n"\
69 "<key>category</key>\r\n"\
70 "<string>video</string>\r\n"\
71 "<key>state</key>\r\n"\
72 "<string>%1</string>\r\n"\
76 static const QString
PLAYBACK_INFO {
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" \
77 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\r\n"\
78 "<plist version=\"1.0\">\r\n"\
80 "<key>duration</key>\r\n"\
81 "<real>%1</real>\r\n"\
82 "<key>loadedTimeRanges</key>\r\n"\
85 "\t\t\t<key>duration</key>\r\n"\
86 "\t\t\t<real>%2</real>\r\n"\
87 "\t\t\t<key>start</key>\r\n"\
88 "\t\t\t<real>0.0</real>\r\n"\
91 "<key>playbackBufferEmpty</key>\r\n"\
93 "<key>playbackBufferFull</key>\r\n"\
95 "<key>playbackLikelyToKeepUp</key>\r\n"\
97 "<key>position</key>\r\n"\
98 "<real>%3</real>\r\n"\
99 "<key>rate</key>\r\n"\
100 "<real>%4</real>\r\n"\
101 "<key>readyToPlay</key>\r\n"\
103 "<key>seekableTimeRanges</key>\r\n"\
106 "\t\t\t<key>duration</key>\r\n"\
107 "\t\t\t<real>%1</real>\r\n"\
108 "\t\t\t<key>start</key>\r\n"\
109 "\t\t\t<real>0.0</real>\r\n"\
115 static const QString
NOT_READY {
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" \
116 "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\r\n"\
117 "<plist version=\"1.0\">\r\n"\
119 "<key>readyToPlay</key>\r\n"\
126 QString key =
"AirPlayId";
128 int size =
id.size();
129 if (size == 12 &&
id.toUpper() ==
id)
148 std::array<uint32_t,4> nonceParts {
156 nonce = QString::number(nonceParts[0], 16).toUpper();
157 nonce += QString::number(nonceParts[1], 16).toUpper();
158 nonce += QString::number(nonceParts[2], 16).toUpper();
159 nonce += QString::number(nonceParts[3], 16).toUpper();
164 const QString& nonce,
const QString& password,
167 int authStart = response.indexOf(
"response=\"") + 10;
168 int authLength = response.indexOf(
"\"", authStart) - authStart;
169 auth = response.mid(authStart, authLength).toLatin1();
171 int uriStart = response.indexOf(
"uri=\"") + 5;
172 int uriLength = response.indexOf(
"\"", uriStart) - uriStart;
173 QByteArray uri = response.mid(uriStart, uriLength).toLatin1();
175 int userStart = response.indexOf(
"username=\"") + 10;
176 int userLength = response.indexOf(
"\"", userStart) - userStart;
177 QByteArray
user = response.mid(userStart, userLength).toLatin1();
179 int realmStart = response.indexOf(
"realm=\"") + 7;
180 int realmLength = response.indexOf(
"\"", realmStart) - realmStart;
181 QByteArray realm = response.mid(realmStart, realmLength).toLatin1();
183 QByteArray passwd = password.toLatin1();
185 QCryptographicHash hash(QCryptographicHash::Md5);
186 QByteArray colon(
":", 1);
191 hash.addData(passwd);
192 QByteArray ha1 = hash.result();
197 hash.addData(option.toLatin1());
200 QByteArray ha2 = hash.result().toHex();
206 hash.addData(nonce.toLatin1());
209 return hash.result().toHex();
236 auto samekey = [key](
const auto& query) {
return query.first == key; };;
238 return (query !=
m_queries.cend()) ? query->second :
"";
243 QMap<QByteArray,QByteArray> result;
244 QList<QByteArray> lines =
m_body.split(
'\n');;
245 for (
const QByteArray& line : std::as_const(lines))
247 int index = line.indexOf(
":");
250 result.insert(line.left(index).trimmed(),
251 line.mid(index + 1).trimmed());
266 if (next < 0)
return {};
281 QList<QByteArray>
vals = line.split(
' ');
285 QUrl url = QUrl::fromEncoded(
vals[1].trimmed());
286 m_uri = url.path(QUrl::FullyEncoded).toLocal8Bit();
289 QList<QPair<QString, QString> > items =
290 QUrlQuery(url).queryItems(QUrl::FullyEncoded);
291 QList<QPair<QString, QString> >::ConstIterator it = items.constBegin();
292 for ( ; it != items.constEnd(); ++it)
293 m_queries << qMakePair(it->first.toLatin1(), it->second.toLatin1());
299 while (!(line =
GetLine()).isEmpty())
301 int index = line.indexOf(
":");
304 m_headers.insert(line.left(index).trimmed(),
305 line.mid(index + 1).trimmed());
310 if (
m_headers.contains(
"Content-Length"))
314 if (
m_size > 0 && remaining > 0)
326 LOG(VB_GENERAL, LOG_DEBUG,
LOC +
327 QString(
"HTTP Request:\n%1").arg(
m_data.data()));
331 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
332 QString(
"AP HTTPRequest: Didn't read entire buffer."
333 "Left to receive: %1 (got %2 of %3) body=%4")
361 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to create airplay thread.");
370 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to create airplay object.");
387 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Created airplay objects.");
393 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Cleaning up.");
417 QMutexLocker locker(
m_lock);
435 for (QTcpSocket* connection : std::as_const(
m_sockets))
437 disconnect(connection,
nullptr,
nullptr,
nullptr);
452 QMutexLocker locker(
m_lock);
468 LOG(VB_GENERAL, LOG_ERR,
LOC +
469 "Failed to find a port for incoming connections.");
477 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to create Bonjour object.");
484 m_name += QString::number(multiple);
486 QByteArray name =
m_name.toUtf8();
489 QByteArray
type =
"_airplay._tcp";
491 txt.append(26); txt.append(
"deviceid="); txt.append(
GetMacAddress().toUtf8());
494 txt.append(13); txt.append(
"features=0xF7");
495 txt.append(14); txt.append(
"model=MythTV,1");
500 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to register service.");
527 QMutexLocker locker(
m_lock);
528 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"New connection from %1:%2")
529 .arg(client->peerAddress().toString()).arg(client->peerPort()));
533 connect(client, &QAbstractSocket::disconnected,
540 QMutexLocker locker(
m_lock);
541 auto *socket = qobject_cast<QTcpSocket *>(sender());
554 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Removing connection %1:%2")
555 .arg(socket->peerAddress().toString()).arg(socket->peerPort()));
560 QMutableHashIterator<QByteArray,AirplayConnection> it(
m_connections);
564 if (it.value().m_reverseSocket == socket)
565 it.value().m_reverseSocket =
nullptr;
566 if (it.value().m_controlSocket == socket)
567 it.value().m_controlSocket =
nullptr;
568 if (!it.value().m_reverseSocket &&
569 !it.value().m_controlSocket)
571 if (!it.value().m_stopped)
580 if (!remove.isEmpty())
582 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Removing session '%1'")
583 .arg(remove.data()));
587 tr(
"from %1").arg(socket->peerAddress().toString()));
593 socket->deleteLater();
604 QMutexLocker locker(
m_lock);
605 auto *socket = qobject_cast<QTcpSocket *>(sender());
609 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"Read for %1:%2")
610 .arg(socket->peerAddress().toString()).arg(socket->peerPort()));
612 QByteArray buf = socket->readAll();
651 QHostAddress addr = socket->peerAddress();
656 QByteArray content_type;
658 if (req->
GetURI() !=
"/playback-info")
660 LOG(VB_GENERAL, LOG_INFO,
LOC +
661 QString(
"Method: %1 URI: %2")
666 LOG(VB_GENERAL, LOG_DEBUG,
LOC +
667 QString(
"Method: %1 URI: %2")
674 if (!req->
GetHeaders().contains(
"X-Apple-Session-ID"))
676 LOG(VB_GENERAL, LOG_DEBUG,
LOC +
677 QString(
"No session ID in http request. "
678 "Connection from iTunes? Using IP %1").arg(addr.toString()));
682 session = req->
GetHeaders()[
"X-Apple-Session-ID"];
685 if (session.size() == 0)
688 session = addr.toString().toLatin1();
696 if (req->
GetURI() ==
"/reverse")
699 if (s != socket && s !=
nullptr)
701 LOG(VB_GENERAL, LOG_ERR,
LOC +
702 "Already have a different reverse socket for this connection.");
707 header =
"Upgrade: PTTH/1.0\r\nConnection: Upgrade\r\n";
708 SendResponse(socket, status, header, content_type, body);
713 if (s != socket && s !=
nullptr)
715 LOG(VB_GENERAL, LOG_ERR,
LOC +
716 "Already have a different control socket for this connection.");
730 tr(
"from %1").arg(socket->peerAddress().toString()));
736 double position = 0.0F;
737 double duration = 0.0F;
738 float playerspeed = 0.0F;
739 bool playing =
false;
748 if (playing && duration > 0.01 && position < 0.01)
772 header = QString(
"WWW-Authenticate: Digest realm=\"AirPlay\", "
773 "nonce=\"%1\"\r\n").arg(
m_nonce).toLatin1();
774 if (!req->
GetHeaders().contains(
"Authorization"))
777 header, content_type, body);
786 LOG(VB_GENERAL, LOG_INFO,
LOC +
"AirPlay client authenticated");
790 LOG(VB_GENERAL, LOG_INFO,
LOC +
"AirPlay authentication failed");
792 header, content_type, body);
798 if (req->
GetURI() ==
"/server-info")
800 content_type =
"text/x-apple-plist+xml\r\n";
803 LOG(VB_GENERAL, LOG_INFO, body);
805 else if (req->
GetURI() ==
"/scrub")
811 auto intpos = (uint64_t)pos;
813 LOG(VB_GENERAL, LOG_INFO,
LOC +
814 QString(
"Scrub: (post) seek to %1").arg(intpos));
819 content_type =
"text/parameters\r\n";
820 body = QString(
"duration: %1\r\nposition: %2\r\n")
821 .arg(duration, 0,
'f', 6,
'0')
822 .arg(position, 0,
'f', 6,
'0');
824 LOG(VB_GENERAL, LOG_INFO,
LOC +
825 QString(
"Scrub: (get) returned %1 of %2")
826 .arg(position).arg(duration));
839 else if (req->
GetURI() ==
"/stop")
843 else if (req->
GetURI() ==
"/photo")
848 QImage image = QImage::fromData(req->
GetBody());
852 LOG(VB_GENERAL, LOG_INFO,
LOC +
853 QString(
"Received %1x%2 %3 photo")
854 .arg(image.width()).arg(image.height()).
855 arg(png ?
"jpeg" :
"png"));
872 else if (req->
GetURI() ==
"/slideshow-features")
874 LOG(VB_GENERAL, LOG_INFO,
LOC +
875 "Slideshow functionality not implemented.");
877 else if (req->
GetURI() ==
"/authorize")
879 LOG(VB_GENERAL, LOG_INFO,
LOC +
"Ignoring authorize request.");
881 else if ((req->
GetURI() ==
"/setProperty") ||
882 (req->
GetURI() ==
"/getProperty"))
886 else if (req->
GetURI() ==
"/rate")
893 if (playerspeed > 0.0F)
901 if (playerspeed < 1.0F)
910 else if (req->
GetURI() ==
"/play")
913 double start_pos = 0.0F;
914 if (req->
GetHeaders().contains(
"Content-Type") &&
915 req->
GetHeaders()[
"Content-Type"] ==
"application/x-apple-binary-plist")
920 QVariant start = plist.
GetValue(
"Start-Position");
922 if (start.isValid() && start.canConvert<
double>())
923 start_pos = start.toDouble();
931 start_pos =
headers[
"Start-Position"].toDouble();
945 if (duration * start_pos >= .1)
953 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"File: '%1' start_pos '%2'")
954 .arg(
file.data()).arg(start_pos));
956 else if (req->
GetURI() ==
"/playback-info")
958 content_type =
"text/x-apple-plist+xml\r\n";
968 body.replace(
"%1", QString(
"%1").arg(duration, 0,
'f', 6,
'0'));
969 body.replace(
"%2", QString(
"%1").arg(duration, 0,
'f', 6,
'0'));
970 body.replace(
"%3", QString(
"%1").arg(position, 0,
'f', 6,
'0'));
971 body.replace(
"%4", playerspeed > 0.0F ?
"1.0" :
"0.0");
972 LOG(VB_GENERAL, LOG_DEBUG, body);
977 SendResponse(socket, status, header, content_type, body);
981 uint16_t status,
const QByteArray& header,
982 const QByteArray& content_type,
const QString& body)
984 if (!socket || !
m_incoming.contains(socket) ||
985 socket->state() != QAbstractSocket::ConnectedState)
987 QTextStream response(socket);
988 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
989 response.setCodec(
"UTF-8");
991 response.setEncoding(QStringConverter::Utf8);
994 reply.append(
"HTTP/1.1 ");
995 reply.append(QString::number(status).toUtf8());
998 reply.append(
"\r\n");
999 reply.append(
"DATE: ");
1001 reply.append(
" GMT\r\n");
1002 if (!header.isEmpty())
1003 reply.append(header);
1005 if (!body.isEmpty())
1007 reply.append(
"Content-Type: ");
1008 reply.append(content_type);
1009 reply.append(
"Content-Length: ");
1010 reply.append(QString::number(body.size()).toUtf8());
1014 reply.append(
"Content-Length: 0");
1016 reply.append(
"\r\n\r\n");
1018 if (!body.isEmpty())
1019 reply.append(body.toUtf8());
1024 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"Send: %1 \n\n%2\n")
1025 .arg(socket->flush()).arg(reply.data()));
1049 QTextStream response(
m_connections[session].m_reverseSocket);
1050 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
1051 response.setCodec(
"UTF-8");
1053 response.setEncoding(QStringConverter::Utf8);
1056 reply.append(
"POST /event HTTP/1.1\r\n");
1057 reply.append(
"Content-Type: text/x-apple-plist+xml\r\n");
1058 reply.append(
"Content-Length: ");
1059 reply.append(QString::number(body.size()).toUtf8());
1060 reply.append(
"\r\n");
1061 reply.append(
"x-apple-session-id: ");
1062 reply.append(session);
1063 reply.append(
"\r\n\r\n");
1064 if (!body.isEmpty())
1065 reply.append(body.toUtf8());
1070 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"Send reverse: %1 \n\n%2\n")
1072 .arg(reply.data()));
1090 double &position,
double &duration,
1096 if (state.contains(
"state"))
1097 playing = state[
"state"].toString() !=
"idle";
1098 if (state.contains(
"playspeed"))
1099 speed = state[
"playspeed"].toFloat();
1100 if (state.contains(
"secondsplayed"))
1101 position = state[
"secondsplayed"].toDouble();
1102 if (state.contains(
"totalseconds"))
1103 duration = state[
"totalseconds"].toDouble();
1104 if (state.contains(
"pathname"))
1105 pathname = state[
"pathname"].toString();
1113 for (
int i = 1; i <=
id.size(); i++)
1115 res.append(
id[i-1]);
1116 if (i % 2 == 0 && i !=
id.size())
1139 double position = 0.0F;
1140 double duration = 0.0F;
1141 float playerspeed = 0.0F;
1142 bool playing =
false;
1159 QMutexLocker locker(
m_lock);
1160 QHash<QByteArray,AirplayConnection>::iterator it =
m_connections.begin();
1167 if (it.key() == session ||
1178 if (!(*it).m_stopped)
1185 socket->disconnect();
1188 socket->deleteLater();
1198 socket->disconnect();
1201 socket->deleteLater();
1218 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1219 QString(
"Sending ACTION_HANDLEMEDIA for %1")
1224 std::vector<CoreWaitInfo> sigs {
1228 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1229 QString(
"ACTION_HANDLEMEDIA completed"));
1236 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1237 QString(
"Sending ACTION_STOP for %1")
1240 auto* ke =
new QKeyEvent(QEvent::KeyPress, 0,
1244 std::vector<CoreWaitInfo> sigs {
1248 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1249 QString(
"ACTION_STOP completed"));
1253 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1254 QString(
"Playback not running, nothing to stop"));
1262 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1263 QString(
"Sending ACTION_SEEKABSOLUTE(%1) for %2")
1268 QStringList(QString::number(position)));
1271 std::vector<CoreWaitInfo> sigs {
1276 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1277 QString(
"ACTION_SEEKABSOLUTE completed"));
1281 LOG(VB_PLAYBACK, LOG_WARNING,
LOC +
1282 QString(
"Trying to seek when playback hasn't started"));
1290 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1291 QString(
"Sending ACTION_PAUSE for %1")
1294 auto* ke =
new QKeyEvent(QEvent::KeyPress, 0,
1298 std::vector<CoreWaitInfo> sigs {
1303 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1304 QString(
"ACTION_PAUSE completed"));
1308 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1309 QString(
"Playback not running, nothing to pause"));
1317 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1318 QString(
"Sending ACTION_PLAY for %1")
1321 auto* ke =
new QKeyEvent(QEvent::KeyPress, 0,
1325 std::vector<CoreWaitInfo> sigs {
1330 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1331 QString(
"ACTION_PLAY completed"));
1335 LOG(VB_PLAYBACK, LOG_DEBUG,
LOC +
1336 QString(
"Playback not running, nothing to unpause"));
1343 QHash<QByteArray,AirplayConnection>::iterator it =
m_connections.begin();