From 4153b3eef4907427a27e318a21f976132ca9da3b Mon Sep 17 00:00:00 2001 From: Kazumasa Mitsunari Date: Mon, 23 Oct 2017 21:37:30 +0900 Subject: Adopt sound manager feature to get sound right Adopt sound manager feature to get sound right In the future, it is better to be compatible to switch AAAA and Sound Manager qlibsoundmanager will be removed when qlibsoundmanager repository is created It is OK to replace library to QML websocket class Change-Id: I29501e68923e95a2ca889b87f5d59ea5195c71bd Signed-off-by: Kazumasa Mitsunari --- app/app.pro | 6 +- app/main.cpp | 20 +++++- app/qlibsoundmanager.cpp | 154 +++++++++++++++++++++++++++++++++++++++++++++++ app/qlibsoundmanager.h | 60 ++++++++++++++++++ package/config.xml | 1 + 5 files changed, 236 insertions(+), 5 deletions(-) create mode 100644 app/qlibsoundmanager.cpp create mode 100644 app/qlibsoundmanager.h diff --git a/app/app.pro b/app/app.pro index fd777a3..093b67b 100644 --- a/app/app.pro +++ b/app/app.pro @@ -1,11 +1,11 @@ TARGET = radio QT = quickcontrols2 -HEADERS = PresetDataObject.h qlibwindowmanager.h -SOURCES = main.cpp PresetDataObject.cpp qlibwindowmanager.cpp +HEADERS = PresetDataObject.h qlibwindowmanager.h qlibsoundmanager.h +SOURCES = main.cpp PresetDataObject.cpp qlibwindowmanager.cpp qlibsoundmanager.cpp CONFIG += link_pkgconfig -PKGCONFIG += windowmanager +PKGCONFIG += soundmanager windowmanager RESOURCES += \ radio.qrc \ diff --git a/app/main.cpp b/app/main.cpp index 23966ca..7c967c2 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -27,8 +27,10 @@ #include #include "PresetDataObject.h" #include "qlibwindowmanager.h" +#include "qlibsoundmanager.h" static QLibWindowmanager* qwm; +static QLibSoundmanager* smw; static std::string myname = std::string("Radio"); int main(int argc, char *argv[]) @@ -91,6 +93,8 @@ int main(int argc, char *argv[]) context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress); qwm = new QLibWindowmanager(); + smw = new QLibSoundmanager(); + // WindowManager if(qwm->init(port,secret) != 0){ exit(EXIT_FAILURE); @@ -102,16 +106,28 @@ int main(int argc, char *argv[]) fprintf(stderr, "Surface got syncDraw!\n"); qwm->endDraw(myname.c_str()); }); - qwm->set_event_handler(QLibWindowmanager::Event_FlushDraw, [](json_object *object) { - fprintf(stderr, "Surface got flushDraw!\n");; + qwm->set_event_handler(QLibWindowmanager::Event_FlushDraw, [&engine, smw](json_object *object) { + fprintf(stderr, "Surface got flushDraw!\n"); + QObject *root = engine.rootObjects().first(); + int sourceID = root->property("sourceID").toInt(); + smw->connect(sourceID, "default"); }); + // SoundManager, event handler is set inside smw + smw->init(port, secret); + + engine.rootContext()->setContextProperty("smw",smw); + // Loading QML File engine.load(QUrl(QStringLiteral("qrc:/Radio.qml"))); QObject *root = engine.rootObjects().first(); QQuickWindow *window = qobject_cast(root); QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface())); // This should be disconnected, but when... + QObject::connect(smw, SIGNAL(reply(QVariant)), + root, SLOT(slotReply(QVariant))); + QObject::connect(smw, SIGNAL(event(QVariant, QVariant)), + root, SLOT(slotEvent(QVariant, QVariant))); } return app.exec(); diff --git a/app/qlibsoundmanager.cpp b/app/qlibsoundmanager.cpp new file mode 100644 index 0000000..9615f64 --- /dev/null +++ b/app/qlibsoundmanager.cpp @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "qlibsoundmanager.h" +#include +using namespace std; + +static int create_json_object(const QJsonObject& obj, struct json_object* jobj); +static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj); +QLibSoundmanager* me; + +static void cbEvent_static(const std::string& event, struct json_object* event_contents) +{ + const QString event_name = QString(event.c_str()); + QString str = QString(json_object_get_string(event_contents)); + QJsonParseError error; + QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error); + const QJsonObject jobj = jdoc.object(); + emit me->event(event_name, jobj); +} + +static void cbReply_static(struct json_object* replyContents) +{ + if(me == nullptr){ + return; + } + QString str = QString(json_object_get_string(replyContents)); + QJsonParseError error; + QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error); + QJsonObject jobj = jdoc.object(); + emit me->reply(jobj); +} + +QLibSoundmanager::QLibSoundmanager(QObject *parent) : + QObject(parent) +{ + /* This is not enabled */ + libsm = new LibSoundmanager(); +} + +QLibSoundmanager::~QLibSoundmanager() +{ + delete libsm; +} + +int QLibSoundmanager::init(int port, const QString& token) +{ + if(libsm == nullptr){ + return -1; + } + string ctoken = token.toStdString(); + int rc = libsm->init(port, ctoken); + if(rc != 0){ + return rc; + } + me = this; + + libsm->register_callback( + cbEvent_static, + cbReply_static); + return rc; +} + +int QLibSoundmanager::call(const QString &verb, const QJsonObject &arg) +{ + // translate QJsonObject to struct json_object + struct json_object* jobj = json_object_new_object(); + int ret = create_json_object(arg, jobj); + if(ret < 0) + { + return -1; + } + return libsm->call(verb.toStdString().c_str(), jobj); +} + +int QLibSoundmanager::connect(int sourceID, const QString& sinkName){ + string str = sinkName.toStdString(); + return libsm->connect(sourceID, str); +} +int QLibSoundmanager::disconnect(int connectionID){ + return libsm->disconnect(connectionID); +} +int QLibSoundmanager::ackSetSourceState(int handle, int errorcode){ + return libsm->ackSetSourceState(handle, errorcode); +} +int QLibSoundmanager::registerSource(const QString& name){ + string str = name.toStdString(); + return libsm->registerSource(str); +} + +static int create_json_object(const QJsonObject& obj, struct json_object* jobj) +{ + try{ + for(auto itr = obj.begin(); itr != obj.end();++itr) + { + string key = itr.key().toStdString(); + //const char* key = itr.key().toStdString().c_str(); // Do not code like this. string is removed if size is over 16!! + + bool ret = put_val_to_jobj(key.c_str(), itr.value(),jobj); + if(!ret){ + /*This is not implemented*/ + qDebug("JsonArray can't parse for now"); + return -1; + } + } + } + catch(...){ + qDebug("Json parse error occured"); + return -1; + } + return 0; +} + +static bool put_val_to_jobj(const char* key, const QJsonValue& val, struct json_object* jobj) +{ + if(val.isArray()){ + return false; // Array can't input + } + if(val.isString()){ + string value = val.toString().toStdString(); + json_object_object_add(jobj, key, json_object_new_string(value.c_str())); + } + else{ + const int value = val.toInt(); + json_object_object_add(jobj, key, json_object_new_int(value)); + } + return true; +} + + +void QLibSoundmanager::subscribe(const QString event_name) +{ + std::string str = event_name.toStdString(); + libsm->subscribe(str); +} + +void QLibSoundmanager::unsubscribe(const QString event_name) +{ + std::string str = event_name.toStdString(); + libsm->unsubscribe(str); +} diff --git a/app/qlibsoundmanager.h b/app/qlibsoundmanager.h new file mode 100644 index 0000000..57d8c6a --- /dev/null +++ b/app/qlibsoundmanager.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef QLIBSOUNDMANAGER_H +#define QLIBSOUNDMANAGER_H + + #include + #include + #include + #include + #include + #include + + +class QLibSoundmanager : public QObject +{ + Q_OBJECT +public: // method + explicit QLibSoundmanager(QObject *parent = nullptr); + ~QLibSoundmanager(); + int init(int port, const QString& token); + + using sm_event_handler = std::function; + + void subscribe(const QString event_name); + void unsubscribe(const QString event_name); + + void emit_event(const QString &event, const QJsonObject &msg); + void emit_reply(const QJsonObject &msg); + +public: + + Q_INVOKABLE int call(const QString &verb, const QJsonObject &arg); + Q_INVOKABLE int connect(int sourceID, const QString& sinkName); + Q_INVOKABLE int disconnect(int connectionID); + Q_INVOKABLE int ackSetSourceState(int handle, int errorcode); + Q_INVOKABLE int registerSource(const QString& name); + +signals: + void reply(const QVariant &msg); + void event(const QVariant &event, const QVariant &msg); + +private: + LibSoundmanager* libsm; +}; + + +#endif /*QLIBSOUNDMANAGER_H*/ diff --git a/package/config.xml b/package/config.xml index 698598c..bdfe8b6 100644 --- a/package/config.xml +++ b/package/config.xml @@ -8,6 +8,7 @@ APL 2.0 + -- cgit 1.2.3-korg