From ea8791262af51f7f65cf4439e2f3064c5fa14dc2 Mon Sep 17 00:00:00 2001 From: Camilo Higuita Date: Sun, 11 Mar 2018 21:33:56 -0500 Subject: [PATCH] more work on linking socket and initial work on streaming --- babe.cpp | 19 ++++++++++- babe.h | 2 ++ main.cpp | 3 +- services/local/LinkingView.qml | 11 ++++--- services/local/linking.cpp | 36 ++++++++++++++++++++ services/local/linking.h | 13 ++++++-- services/local/player.cpp | 14 ++++++-- services/local/player.h | 60 ++++++++++++++++++---------------- services/local/socket.cpp | 10 ++++++ services/local/socket.h | 1 + utils/Icons.js | 2 +- 11 files changed, 129 insertions(+), 42 deletions(-) diff --git a/babe.cpp b/babe.cpp index d1b4950..7458a79 100644 --- a/babe.cpp +++ b/babe.cpp @@ -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(code), res)); } + else if(code == LINK::CODE::SEARCHFOR) + { + auto res = this->searchFor(msg.split(",")); + link.sendToClient(link.packResponse(static_cast(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"<setContextProperty("player", &player); + context->setContextProperty("player", &bae.player); context->setContextProperty("bae", &bae); context->setContextProperty("youtube", &youtube); context->setContextProperty("link", &bae.link); diff --git a/services/local/LinkingView.qml b/services/local/LinkingView.qml index 0943eb8..9f8fad7 100644 --- a/services/local/LinkingView.qml +++ b/services/local/LinkingView.qml @@ -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 } diff --git a/services/local/linking.cpp b/services/local/linking.cpp index d62bf4c..832dcc3 100644 --- a/services/local/linking.cpp +++ b/services/local/linking.cpp @@ -36,6 +36,30 @@ Linking::Linking(QObject *parent) : QObject(parent) qDebug()<<"client recived message"<responseReady(decoded); }); + + // connect(&client, &QWebSocket::binaryMessageReceived, [this] (QByteArray array) + // { + // qDebug()<<"array recived"<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(code), msg))); } +void Linking::collectTrack(QString url) +{ + qDebug()<<"Trying to collec track"<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()<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; } @@ -59,7 +58,7 @@ void Player::stop() emit this->isPlaying(false); - this->updater->stop(); + this->updater->stop(); } void Player::seek(const int &pos) @@ -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()) diff --git a/services/local/player.h b/services/local/player.h index 9eb7111..73b0ee6 100644 --- a/services/local/player.h +++ b/services/local/player.h @@ -4,37 +4,41 @@ #include #include #include +#include class Player : public QObject { - Q_OBJECT -public: - explicit Player(QObject *parent = nullptr); - - Q_INVOKABLE void source(const QString &url); - Q_INVOKABLE bool play(); - Q_INVOKABLE void pause(); - Q_INVOKABLE void stop(); - Q_INVOKABLE void seek(const int &pos); - Q_INVOKABLE int duration(); - Q_INVOKABLE bool isPaused(); - Q_INVOKABLE QString transformTime(const int &pos); - -private: - QMediaPlayer *player; - QTimer *updater; - void update(); - - QString sourceurl; - -signals: - void pos(int pos); - void finished(); - void timing(QString time); - void durationChanged(QString time); - void isPlaying(bool playing); - -public slots: + Q_OBJECT + public: + explicit Player(QObject *parent = nullptr); + + Q_INVOKABLE void source(const QString &url); + Q_INVOKABLE bool play(); + Q_INVOKABLE void pause(); + Q_INVOKABLE void stop(); + Q_INVOKABLE void seek(const int &pos); + Q_INVOKABLE int duration(); + Q_INVOKABLE bool isPaused(); + Q_INVOKABLE QString transformTime(const int &pos); + Q_INVOKABLE void playBuffer(QByteArray &array); + + + private: + QMediaPlayer *player; + QTimer *updater; + void update(); + QBuffer *buffer; + + QString sourceurl; + + signals: + void pos(int pos); + void finished(); + void timing(QString time); + void durationChanged(QString time); + void isPlaying(bool playing); + + public slots: }; #endif // PLAYER_H diff --git a/services/local/socket.cpp b/services/local/socket.cpp index c2d227d..eee7d71 100644 --- a/services/local/socket.cpp +++ b/services/local/socket.cpp @@ -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(); diff --git a/services/local/socket.h b/services/local/socket.h index b4ef201..c550dca 100644 --- a/services/local/socket.h +++ b/services/local/socket.h @@ -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(); diff --git a/utils/Icons.js b/utils/Icons.js index 38441ca..3e1ad90 100644 --- a/utils/Icons.js +++ b/utils/Icons.js @@ -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, -- 2.24.1