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