Commit ea879126 authored by Camilo Higuita's avatar Camilo Higuita

more work on linking socket and initial work on streaming

parent 3663cbdd
......@@ -69,6 +69,11 @@ Babe::Babe(QObject *parent) : CollectionDB(parent)
});
connect(&link, &Linking::parseAsk, this, &Babe::linkDecoder);
connect(&link, &Linking::arrayReady, [this](QByteArray array)
{
qDebug()<<"trying to play the array";
this->player.playBuffer(array);
});
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
this->nof = new Notify(this);
......@@ -243,11 +248,23 @@ void Babe::linkDecoder(QString json)
this->link.deviceName = msg;
emit this->link.serverConReady(msg);
}
else
else if(code == LINK::CODE::QUERY || code == LINK::CODE::FILTER)
{
auto res = this->getDBDataQML(msg);
link.sendToClient(link.packResponse(static_cast<LINK::CODE>(code), res));
}
else if(code == LINK::CODE::SEARCHFOR)
{
auto res = this->searchFor(msg.split(","));
link.sendToClient(link.packResponse(static_cast<LINK::CODE>(code), res));
}else if(code == LINK::CODE::PLAY)
{
QFile file(msg); // sound dir
file.open(QIODevice::ReadOnly);
QByteArray arr = file.readAll();
qDebug()<<"Preparing track array"<<msg<<arr.size();
link.sendArrayToClient(arr);
}
}
QString Babe::albumWiki(const QString &album, const QString &artist)
......
......@@ -6,6 +6,7 @@
#include "utils/bae.h"
#include "db/collectionDB.h"
#include "services/local/linking.h"
#include "services/local/player.h"
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
class Notify;
......@@ -30,6 +31,7 @@ class Babe : public CollectionDB
BabeSettings *settings;
Linking link;
Player player;
// Q_INVOKABLE void runPy();
......
......@@ -53,7 +53,6 @@ int main(int argc, char *argv[])
}
Babe bae;
Player player;
/* Services */
YouTube youtube;
......@@ -68,7 +67,7 @@ int main(int argc, char *argv[])
});
auto context = engine.rootContext();
context->setContextProperty("player", &player);
context->setContextProperty("player", &bae.player);
context->setContextProperty("bae", &bae);
context->setContextProperty("youtube", &youtube);
context->setContextProperty("link", &bae.link);
......
......@@ -14,7 +14,6 @@ ColumnLayout
{
id: linkingViewRoot
property alias linkingConf : linkingConf
signal rowClicked(var track)
signal quickPlayTrack(var track)
signal playAll(var tracks)
......@@ -128,6 +127,7 @@ ColumnLayout
onRowClicked: {}
onQuickPlayTrack:
{
link.collectTrack(filterList.model.get(index).url)
}
onPlayAll: {}
onPulled: {}
......@@ -273,6 +273,7 @@ ColumnLayout
case LINK.FILTER:
appendToExtraList(res.MSG)
break
case LINK.QUERY:
case LINK.SEARCHFOR:
populate(res.MSG)
break
......@@ -290,19 +291,19 @@ ColumnLayout
case "Albums":
var artist = linkingFilter.model.get(index).artist
var query = Q.GET.albumTracks_.arg(tag)
link.ask(LINK.SEARCHFOR, query.arg(artist))
link.ask(LINK.QUERY, query.arg(artist))
break
case "Artists":
query = Q.GET.artistTracks_.arg(tag)
link.ask(LINK.SEARCHFOR, query.arg(tag))
link.ask(LINK.QUERY, query.arg(tag))
break
case "Genres":
query = Q.GET.genreTracks_.arg(tag)
link.ask(LINK.SEARCHFOR, query.arg(tag))
link.ask(LINK.QUERY, query.arg(tag))
break
case "Tags":
query = Q.GET.tagTracks_.arg(tag)
link.ask(LINK.SEARCHFOR, query.arg(tag))
link.ask(LINK.QUERY, query.arg(tag))
break
default: break
}
......
......@@ -36,6 +36,30 @@ Linking::Linking(QObject *parent) : QObject(parent)
qDebug()<<"client recived message"<<msg;
emit this->responseReady(decoded);
});
// connect(&client, &QWebSocket::binaryMessageReceived, [this] (QByteArray array)
// {
// qDebug()<<"array recived"<<array;
// });
trackArray.reserve(8*1024*1024);
connect(&client, &QWebSocket::binaryFrameReceived, [this] (const QByteArray &frame, bool isLastFrame)
{
qDebug()<<"binary frame recived"<<frame;
qDebug()<<"BYTEARRAY SO FAR:"<<arraySize<<trackArray;
arraySize += frame.size();
trackArray.append(frame);
if(isLastFrame)
{
arraySize=0;
trackArray = QByteArray();
emit this->arrayReady(trackArray);
}
});
}
QVariantMap Linking::packResponse(const LINK::CODE &code, const QVariant &content)
......@@ -87,6 +111,12 @@ void Linking::ask(int code, QString msg)
client.sendTextMessage(stringify(packResponse(static_cast<LINK::CODE>(code), msg)));
}
void Linking::collectTrack(QString url)
{
qDebug()<<"Trying to collec track"<<url;
this->ask(LINK::CODE::PLAY, url);
}
QVariantMap Linking::decode(const QString &json)
{
bDebug::Instance()->msg("Decoding client msg");
......@@ -144,6 +174,12 @@ void Linking::sendToClient(QVariantMap map)
server->sendMessageTo(0, json);
}
void Linking::sendArrayToClient(const QByteArray &array)
{
qDebug()<<"Sending array to client";
this->server->sendArray(0, array);
}
void Linking::handleError(QAbstractSocket::SocketError error)
{
qDebug()<<error;
......
......@@ -19,7 +19,9 @@ namespace LINK
DISCONNECTED = 3,
SEARCHFOR = 4,
PLAYLISTS = 5,
FILTER = 6
FILTER = 6,
QUERY = 7,
PLAY = 8,
};
Q_ENUM_NS(CODE);
......@@ -31,6 +33,8 @@ namespace LINK
{CODE::SEARCHFOR, "SEARCHFOR"},
{CODE::PLAYLISTS, "PLAYLISTS"},
{CODE::FILTER, "FILTER"},
{CODE::QUERY, "QUERY"},
{CODE::PLAY, "PLAY"}
};
}
......@@ -43,8 +47,10 @@ class Linking : public QObject
Socket *server;
QWebSocket client;
QString IP;
QString stringify(const QVariantMap &map);
QByteArray trackArray;
int arraySize =0;
public:
explicit Linking(QObject *parent = nullptr);
......@@ -58,11 +64,13 @@ class Linking : public QObject
Q_INVOKABLE QString getPort();
Q_INVOKABLE QString getDeviceName();
Q_INVOKABLE void ask(int code, QString msg);
Q_INVOKABLE void collectTrack(QString url);
QVariantMap decode(const QString &json);
void onConnected();
QStringList checkAddresses();
Q_INVOKABLE void connectTo(QString ip, QString port);
Q_INVOKABLE void sendToClient(QVariantMap map);
void sendArrayToClient(const QByteArray &array);
signals:
void devicesLinked();
......@@ -74,6 +82,7 @@ class Linking : public QObject
void parseAsk(QString json);
void responseReady(QVariantMap res);
void arrayReady(QByteArray array);
public slots:
void handleError(QAbstractSocket::SocketError error);
......
......@@ -5,6 +5,7 @@
Player::Player(QObject *parent) : QObject(parent)
{
this->player = new QMediaPlayer(this);
this->buffer = new QBuffer(this->player);
connect(player, &QMediaPlayer::durationChanged, this, [&](qint64 dur)
{
......@@ -35,9 +36,7 @@ bool Player::play()
if(!updater->isActive())
this->updater->start(150);
if(this->player->isAvailable())
this->player->play();
else return false;
return true;
}
......@@ -85,6 +84,15 @@ QString Player::transformTime(const int &pos)
return time;
}
void Player::playBuffer(QByteArray &array)
{
buffer->setData(array);
buffer->open(QIODevice::ReadOnly);
player->setMedia(QMediaContent(),buffer);
this->sourceurl = "buffer";
this->play();
}
void Player::update()
{
if(this->player->isAvailable())
......
......@@ -4,11 +4,12 @@
#include <QObject>
#include <QtMultimedia/QMediaPlayer>
#include <QTimer>
#include <QBuffer>
class Player : public QObject
{
Q_OBJECT
public:
public:
explicit Player(QObject *parent = nullptr);
Q_INVOKABLE void source(const QString &url);
......@@ -19,22 +20,25 @@ public:
Q_INVOKABLE int duration();
Q_INVOKABLE bool isPaused();
Q_INVOKABLE QString transformTime(const int &pos);
Q_INVOKABLE void playBuffer(QByteArray &array);
private:
private:
QMediaPlayer *player;
QTimer *updater;
void update();
QBuffer *buffer;
QString sourceurl;
signals:
signals:
void pos(int pos);
void finished();
void timing(QString time);
void durationChanged(QString time);
void isPlaying(bool playing);
public slots:
public slots:
};
#endif // PLAYER_H
......@@ -34,6 +34,16 @@ void Socket::sendMessageTo(const int &client, const QString &message)
s_client->sendTextMessage(message);
}
void Socket::sendArray(const int &client, const QByteArray &array)
{
if(this->m_clients.isEmpty() && this->m_clients.size()>=client)
return;
auto s_client = this->m_clients.at(client);
s_client->sendBinaryMessage(array);
qDebug()<<"Array sent to client";
}
void Socket::onNewConnection()
{
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
......
......@@ -16,6 +16,7 @@ public:
explicit Socket(quint16 port, QObject *parent = Q_NULLPTR);
~Socket();
void sendMessageTo(const int &client, const QString &message);
void sendArray(const int &client, const QByteArray &array);
Q_SIGNALS:
void closed();
......
......@@ -1677,7 +1677,7 @@ var Babe = {
"view-media-playlist" : Icon.libraryMusic,
"application-menu": Icon.menu,
"view-media-config" : Icon.menu,
"view-media-config" : Icon.settingsBox,
"games-config-options" : Icon.settings,
"edit-comment" : Icon.comment,
......
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