diff --git a/README b/README index 26821f546258f67b839b2ab5f4f7a004f7c9a50c..6eec45d88a5ac55fa653e43bcb9bc6e93d63af4f 100644 --- a/README +++ b/README @@ -1,5 +1,10 @@ -How To Build This Template --=-=-=-=-=-=-=-=-=-=-=-=-= +About +===== +KDE Plasma DataEngine and Service that can queue Youtube and Bandcamp songs(by way of youtube-dl) and play them on the device. Also supports accepting remote commands on TCP socket(54634), discoverable through DNS-SD. + + +How To Build +-=-=-=-=-=-= --- On Unix: @@ -20,3 +25,4 @@ Requires: * KDNSSD * youtube-dl * QtMultimedia +* QtNetwork diff --git a/src/minimediaplayer.cpp b/src/minimediaplayer.cpp index 03678c416512b5ab95f33d0747effbd061da6c26..b4ed1209072b06390721b10bef80f86e8f07dc0a 100644 --- a/src/minimediaplayer.cpp +++ b/src/minimediaplayer.cpp @@ -173,8 +173,8 @@ void MiniMediaPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status) } else if(status == QMediaPlayer::MediaStatus::LoadedMedia) { - //Start playing media - m_player->play(); + //Start playing media if more in playlsit + if(playlist.size() > 0) m_player->play(); } } diff --git a/src/simplecastengine.cpp b/src/simplecastengine.cpp index d24e5488a4023f3531e13c765f200b0f6df36d35..040e801d6dc36a9dd168ba2fa4d3e306e0e0b7e9 100644 --- a/src/simplecastengine.cpp +++ b/src/simplecastengine.cpp @@ -30,7 +30,7 @@ SimpleCastEngine::SimpleCastEngine(QObject *parent, const QVariantList &args) connect(player,SIGNAL(trackDurationChanged(qint64)),this,SLOT(durationChanged(qint64))); connect(player,SIGNAL(trackPositionChanged(qint64)),this,SLOT(positionChanged(qint64))); connect(player,SIGNAL(playlistChanged(QStringList)),this,SLOT(playlistChanged(QStringList))); - tcpService.listen(); + tcpService.listen(QHostAddress::Any,54634); } void SimpleCastEngine::durationChanged(qint64 length) diff --git a/src/simplecasttcpservice.cpp b/src/simplecasttcpservice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1de36d2fc5d663d222ac13ff7168f1049f962a3c --- /dev/null +++ b/src/simplecasttcpservice.cpp @@ -0,0 +1,108 @@ +/* + * 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" + diff --git a/src/simplecasttcpservice.h b/src/simplecasttcpservice.h new file mode 100644 index 0000000000000000000000000000000000000000..f9e12e60eafba114b41ca61a90bab83b52afad53 --- /dev/null +++ b/src/simplecasttcpservice.h @@ -0,0 +1,46 @@ +/* + * 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 . + */ + +#ifndef SIMPLECASTTCPSERVICE_H +#define SIMPLECASTTCPSERVICE_H + +#include +#include +#include "minimediaplayer.h" + +/** + * Tcp Server that responds to simple client requests and can be found over DNSSD. + */ +class SimpleCastTcpService : public QTcpServer +{ + Q_OBJECT + +public: + SimpleCastTcpService(MiniMediaPlayer *player,QObject *parent = 0); + bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0); + void close(); + +protected: + void incomingConnection(qintptr handle) override; + +private: + MiniMediaPlayer *player; + KDNSSD::PublicService *dnsService; + +}; + +#endif // SIMPLECASTTCPSERVICE_H