Adopt soundmanager class to manage sound right
[apps/mediaplayer.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016, 2017 Toyota Motor Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <QtCore/QCommandLineParser>
19 #include <QtCore/QDebug>
20 #include <QtCore/QDir>
21 #include <QtCore/QStandardPaths>
22 #include <QtCore/QUrlQuery>
23 #include <QtGui/QGuiApplication>
24 #include <QtQml/QQmlApplicationEngine>
25 #include <QtQml/QQmlContext>
26 #include <QtQml/qqml.h>
27 #include <QtQuickControls2/QQuickStyle>
28
29 #include <QQuickWindow>
30 #include "qlibwindowmanager.h"
31 #include "qlibsoundmanager.h"
32
33 static QLibWindowmanager* qwm;
34 static QLibSoundmanager* smw;
35 static std::string myname = std::string("MediaPlayer");
36
37 int main(int argc, char *argv[])
38 {
39     QGuiApplication app(argc, argv);
40
41     QQuickStyle::setStyle("AGL");
42
43     QQmlApplicationEngine engine;
44     QQmlContext *context = engine.rootContext();
45
46     QCommandLineParser parser;
47     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
48     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
49     parser.addHelpOption();
50     parser.addVersionOption();
51     parser.process(app);
52     QStringList positionalArguments = parser.positionalArguments();
53
54     if (positionalArguments.length() == 2) {
55         int port = positionalArguments.takeFirst().toInt();
56         QString secret = positionalArguments.takeFirst();
57         QUrl bindingAddress;
58         bindingAddress.setScheme(QStringLiteral("ws"));
59         bindingAddress.setHost(QStringLiteral("localhost"));
60         bindingAddress.setPort(port);
61         bindingAddress.setPath(QStringLiteral("/api"));
62         QUrlQuery query;
63         query.addQueryItem(QStringLiteral("token"), secret);
64         bindingAddress.setQuery(query);
65         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
66         qwm = new QLibWindowmanager();
67         smw = new QLibSoundmanager();
68
69         // WindowManager
70         if(qwm->init(port,secret) != 0){
71             exit(EXIT_FAILURE);
72         }
73         if (qwm->requestSurface(myname.c_str()) != 0) {
74             exit(EXIT_FAILURE);
75         }
76         qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm](json_object *object) {
77             fprintf(stderr, "Surface got syncDraw!\n");
78             qwm->endDraw(myname.c_str());
79             });
80         qwm->set_event_handler(QLibWindowmanager::Event_FlushDraw, [&engine, smw](json_object *object) {
81             fprintf(stderr, "Surface got flushDraw!\n");
82             QObject *root = engine.rootObjects().first();
83             int sourceID = root->property("sourceID").toInt();
84             smw->connect(sourceID, "default");
85         // SoundManager, event handler is set inside smw
86         smw->init(port, secret);
87
88         engine.rootContext()->setContextProperty("smw",smw);
89
90     }
91
92     engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
93         QObject *root = engine.rootObjects().first();
94         QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
95         QObject::connect(window, SIGNAL(frameSwapped()),
96             qwm, SLOT(slotActivateSurface()));  // This should be disconnected, but when...
97         QObject::connect(smw, SIGNAL(reply(QVariant)),
98             root, SLOT(slotReply(QVariant)));
99         QObject::connect(smw, SIGNAL(event(QVariant, QVariant)),
100             root, SLOT(slotEvent(QVariant, QVariant)));
101
102     return app.exec();
103 }