Commit f96e1ea1 authored by Kevin Whitaker's avatar Kevin Whitaker

Add simple TCP socket listener.(and actual class) Fix next track accidentally...

Add simple TCP socket listener.(and actual class) Fix next track accidentally playing last track again.
parent 9888ba9d
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
......@@ -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();
}
}
......
......@@ -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)
......
/*
* Copyright (C) 2019 Kevin Whitaker <eyecreate@eyecreate.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "simplecasttcpservice.h"
#include <QThread>
#include <QTcpSocket>
#include <QDataStream>
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"
/*
* Copyright (C) 2019 Kevin Whitaker <eyecreate@eyecreate.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef SIMPLECASTTCPSERVICE_H
#define SIMPLECASTTCPSERVICE_H
#include <QTcpServer>
#include <KDNSSD/DNSSD/PublicService>
#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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment