Music Hub ..
A session-wide music playback service
Loading...
Searching...
No Matches
server.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014 Canonical Ltd
3 * Copyright © 2022 UBports Foundation.
4 *
5 * Contact: Alberto Mardegan <mardy@users.sourceforge.net>
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Authored by: Jim Hodapp <jim.hodapp@canonical.com>
20 */
21
22#include "logging.h"
24#include "service_skeleton.h"
25
26#include <QCoreApplication>
27#include <QDBusConnection>
28#include <QSharedPointer>
29#include <QTextStream>
30
31#include <hybris/media/media_codec_layer.h>
32#include <signal.h>
33
35
36namespace
37{
38void logger_init()
39{
40 QTextStream out(stdout);
41 QTextStream err(stderr);
42
43 const char *level = ::getenv("MH_LOG_LEVEL");
44 // Default level is info
45 QString severity = "info";
46 if (level)
47 {
48 if (strcmp(level, "trace") == 0)
49 severity = "debug";
50 else if (strcmp(level, "info") == 0 ||
51 strcmp(level, "debug") == 0 ||
52 strcmp(level, "warning") == 0 ||
53 strcmp(level, "fatal") == 0)
54 severity = level;
55 else if (strcmp(level, "error") == 0)
56 severity = "critical";
57 else
58 err << "Invalid log level \"" << level
59 << "\", setting to info. Valid options: [trace, debug, info, warning, error, fatal].\n";
60 }
61 else
62 out << "Using default log level: info\n";
63
64 out << "Log level: " << severity << '\n';
65}
66
67// All platform-specific initialization routines go here.
68void platform_init()
69{
71 switch (b)
72 {
74 MH_DEBUG("Found hybris backend");
75 decoding_service_init();
76 break;
78 MH_DEBUG("Found mir backend");
79 break;
81 MH_WARNING("No video backend selected. Video functionality won't work.");
82 break;
83 default:
84 MH_INFO("Invalid or no A/V backend specified, using \"hybris\" as a default.");
85 decoding_service_init();
86 }
87}
88}
89
91{
92 Q_OBJECT
93public:
94 Server(int &argc, char **argv): QCoreApplication(argc, argv) {
95 struct sigaction sa;
96 sa.sa_flags = 0;
97 sa.sa_handler = signalHandler;
98 sigemptyset(&sa.sa_mask);
99 sigaction(SIGTERM, &sa, 0);
100 }
101
102 static void signalHandler(int signum) {
103 if (signum == SIGTERM) {
104 QCoreApplication::quit();
105 }
106 }
107
108public Q_SLOTS:
110 MH_INFO() << "Got disconnected from D-Bus, terminating...";
111 QCoreApplication::exit(EXIT_FAILURE);
112 }
113};
114
115int main(int argc, char **argv)
116{
117 logger_init();
118
119 // Init platform-specific functionality.
120 platform_init();
121
122 Server app(argc, argv);
123
124 auto bus = QDBusConnection::sessionBus();
125
127
128 auto skeleton = new media::ServiceSkeleton(
130 bus,
131 }, &impl, &impl);
132
133 bool ok =
134 bus.registerObject(QStringLiteral("/com/lomiri/MediaHub/Service"),
135 skeleton,
136 QDBusConnection::ExportAllSlots |
137 QDBusConnection::ExportScriptableSignals |
138 QDBusConnection::ExportAllProperties);
139 if (!ok) {
140 MH_ERROR("Failed to register service object");
141 return EXIT_FAILURE;
142 }
143
144 ok = bus.registerService("com.lomiri.MediaHub.Service");
145 if (!ok) {
146 MH_ERROR("Failed to register service name");
147 return EXIT_FAILURE;
148 }
149
150 ok = bus.registerService("org.mpris.MediaPlayer2.MediaHub");
151 if (!ok) {
152 MH_ERROR("Failed to register MPRIS service name");
153 return EXIT_FAILURE;
154 }
155
156 bus.connect(QString(),
157 QStringLiteral("/org/freedesktop/DBus/Local"),
158 QStringLiteral("org.freedesktop.DBus.Local"),
159 QStringLiteral("Disconnected"),
160 &app, SLOT(onDisconnected()));
161
162 int exitCode = app.exec();
163
164 return exitCode;
165}
166
167#include "server.moc"
void onDisconnected()
Definition server.cpp:109
Server(int &argc, char **argv)
Definition server.cpp:94
static void signalHandler(int signum)
Definition server.cpp:102
#define MH_ERROR(...)
Definition logging.h:41
#define MH_INFO(...)
Definition logging.h:39
#define MH_WARNING(...)
Definition logging.h:40
#define MH_DEBUG(...)
Definition logging.h:38
int main(int argc, char **argv)
Definition server.cpp:115
static Backend get_backend_type()
Returns the type of audio/video decoding/encoding backend being used.
Definition backend.cpp:28