From f96e1ea12321abed788d6f869f21657cb227fae2 Mon Sep 17 00:00:00 2001 From: Kevin Whitaker Date: Mon, 8 Apr 2019 21:36:19 -0400 Subject: [PATCH] Add simple TCP socket listener.(and actual class) Fix next track accidentally playing last track again. --- README | 10 +++- src/minimediaplayer.cpp | 4 +- src/simplecastengine.cpp | 2 +- src/simplecasttcpservice.cpp | 108 +++++++++++++++++++++++++++++++++++ src/simplecasttcpservice.h | 46 +++++++++++++++ 5 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 src/simplecasttcpservice.cpp create mode 100644 src/simplecasttcpservice.h diff --git a/README b/README index 26821f5..6eec45d 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 03678c4..b4ed120 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 d24e548..040e801 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 0000000..1de36d2 --- /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 0000000..f9e12e6 --- /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 -- GitLab