Music Hub  ..
A session-wide music playback service
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"
23 #include "service_implementation.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 
34 namespace media = lomiri::MediaHubService;
35 
36 namespace
37 {
38 void 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.
68 void platform_init()
69 {
71  switch (b)
72  {
73  case media::AVBackend::Backend::hybris:
74  MH_DEBUG("Found hybris backend");
75  decoding_service_init();
76  break;
77  case media::AVBackend::Backend::mir:
78  MH_DEBUG("Found mir backend");
79  break;
80  case media::AVBackend::Backend::none:
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 
90 class Server: public QCoreApplication
91 {
92  Q_OBJECT
93 public:
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 
108 public Q_SLOTS:
109  void onDisconnected() {
110  MH_INFO() << "Got disconnected from D-Bus, terminating...";
111  QCoreApplication::exit(EXIT_FAILURE);
112  }
113 };
114 
115 int 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(
129  media::ServiceSkeleton::Configuration {
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"
main
int main(int argc, char **argv)
Definition: server.cpp:115
lomiri::MediaHubService::ServiceSkeleton
Definition: service_skeleton.h:41
QCoreApplication
lomiri::MediaHubService::AVBackend::Backend
Backend
Definition: player.h:36
Server::signalHandler
static void signalHandler(int signum)
Definition: server.cpp:102
Server::Server
Server(int &argc, char **argv)
Definition: server.cpp:94
lomiri::MediaHubService::AVBackend::get_backend_type
static Backend get_backend_type()
Returns the type of audio/video decoding/encoding backend being used.
Definition: backend.cpp:28
MH_ERROR
#define MH_ERROR(...)
Definition: logging.h:41
Server::onDisconnected
void onDisconnected()
Definition: server.cpp:109
lomiri::MediaHubService
Definition: context.h:28
lomiri::MediaHubService::ServiceImplementation
Definition: service_implementation.h:37
service_skeleton.h
MH_DEBUG
#define MH_DEBUG(...)
Definition: logging.h:38
MH_INFO
#define MH_INFO(...)
Definition: logging.h:39
Server
Definition: server.cpp:90
MH_WARNING
#define MH_WARNING(...)
Definition: logging.h:40
service_implementation.h