/* * Copyright (C) 2019 Kevin Whitaker * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "simplecasttcpservice.h" #include #include #include class SimpleCastTcpThread : public QThread { Q_OBJECT public: SimpleCastTcpThread(int socketDescriptor, MiniMediaPlayer *player, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor), player(player) { } void run() override { QTcpSocket openSocket; if (!openSocket.setSocketDescriptor(socketDescriptor)) { emit error(openSocket.error()); return; } QByteArray data; openSocket.waitForConnected(); openSocket.waitForReadyRead(); data = openSocket.readAll(); //All Messages start with name header if(QString::fromUtf8(data).startsWith("SIMPLECAST|")) { //Split message into parts QStringList message = QString::fromUtf8(data).split("|"); if(message.size() == 2 && message[1] == "skipToNextTrack") { openSocket.write(player->nextTrack()?"0":"1"); } else if(message.size() == 3 && message[1] == "changePlayState") { openSocket.write(player->playPauseTrack(message[2]=="0"?true:false)?"0":"1"); } else if(message.size() == 3 && message[1] == "addURLToPlaylist") { openSocket.write(player->addURLToPlaylist(QUrl(message[2]))?"0":"1"); } else { openSocket.write("0"); } } if(!openSocket.waitForDisconnected()) openSocket.close(); } int socketDescriptor; MiniMediaPlayer *player; signals: void error(QTcpSocket::SocketError socketError); }; SimpleCastTcpService::SimpleCastTcpService(MiniMediaPlayer *player,QObject *parent) : QTcpServer(parent),player(player) { } bool SimpleCastTcpService::listen(const QHostAddress& address, quint16 port) { bool listening = QTcpServer::listen(address,port); //Setup DNSSD dnsService = new KDNSSD::PublicService(QString(),"_simplecast._tcp",this->serverPort(),"local"); dnsService->setParent(this); dnsService->publishAsync(); return listening; } void SimpleCastTcpService::close() { QTcpServer::close(); dnsService->stop(); } void SimpleCastTcpService::incomingConnection(qintptr handle) { SimpleCastTcpThread *thread = new SimpleCastTcpThread(handle,player,this); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); } #include "simplecasttcpservice.moc"