Create one QThread per one C thread
[deb_shairplay.git] / src / bindings / qt4 / raopservice.cpp
... / ...
CommitLineData
1#include "raopservice.h"
2#include "raopcallbackhandler.h"
3
4#include <QThread>
5#include <QDebug>
6
7#include <shairplay/raop.h>
8
9#define RSA_KEY \
10"-----BEGIN RSA PRIVATE KEY-----\n"\
11"MIIEpQIBAAKCAQEA59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUt\n"\
12"wC5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZHJ2YCCLlDRKSKv6kDqnw4U\n"\
13"wPdpOMXziC/AMj3Z/lUVX1G7WSHCAWKf1zNS1eLvqr+boEjXuBOitnZ/bDzPHrTOZz0Dew0uowxf\n"\
14"/+sG+NCK3eQJVxqcaJ/vEHKIVd2M+5qL71yJQ+87X6oV3eaYvt3zWZYD6z5vYTcrtij2VZ9Zmni/\n"\
15"UAaHqn9JdsBWLUEpVviYnhimNVvYFZeCXg/IdTQ+x4IRdiXNv5hEewIDAQABAoIBAQDl8Axy9XfW\n"\
16"BLmkzkEiqoSwF0PsmVrPzH9KsnwLGH+QZlvjWd8SWYGN7u1507HvhF5N3drJoVU3O14nDY4TFQAa\n"\
17"LlJ9VM35AApXaLyY1ERrN7u9ALKd2LUwYhM7Km539O4yUFYikE2nIPscEsA5ltpxOgUGCY7b7ez5\n"\
18"NtD6nL1ZKauw7aNXmVAvmJTcuPxWmoktF3gDJKK2wxZuNGcJE0uFQEG4Z3BrWP7yoNuSK3dii2jm\n"\
19"lpPHr0O/KnPQtzI3eguhe0TwUem/eYSdyzMyVx/YpwkzwtYL3sR5k0o9rKQLtvLzfAqdBxBurciz\n"\
20"aaA/L0HIgAmOit1GJA2saMxTVPNhAoGBAPfgv1oeZxgxmotiCcMXFEQEWflzhWYTsXrhUIuz5jFu\n"\
21"a39GLS99ZEErhLdrwj8rDDViRVJ5skOp9zFvlYAHs0xh92ji1E7V/ysnKBfsMrPkk5KSKPrnjndM\n"\
22"oPdevWnVkgJ5jxFuNgxkOLMuG9i53B4yMvDTCRiIPMQ++N2iLDaRAoGBAO9v//mU8eVkQaoANf0Z\n"\
23"oMjW8CN4xwWA2cSEIHkd9AfFkftuv8oyLDCG3ZAf0vrhrrtkrfa7ef+AUb69DNggq4mHQAYBp7L+\n"\
24"k5DKzJrKuO0r+R0YbY9pZD1+/g9dVt91d6LQNepUE/yY2PP5CNoFmjedpLHMOPFdVgqDzDFxU8hL\n"\
25"AoGBANDrr7xAJbqBjHVwIzQ4To9pb4BNeqDndk5Qe7fT3+/H1njGaC0/rXE0Qb7q5ySgnsCb3DvA\n"\
26"cJyRM9SJ7OKlGt0FMSdJD5KG0XPIpAVNwgpXXH5MDJg09KHeh0kXo+QA6viFBi21y340NonnEfdf\n"\
27"54PX4ZGS/Xac1UK+pLkBB+zRAoGAf0AY3H3qKS2lMEI4bzEFoHeK3G895pDaK3TFBVmD7fV0Zhov\n"\
28"17fegFPMwOII8MisYm9ZfT2Z0s5Ro3s5rkt+nvLAdfC/PYPKzTLalpGSwomSNYJcB9HNMlmhkGzc\n"\
29"1JnLYT4iyUyx6pcZBmCd8bD0iwY/FzcgNDaUmbX9+XDvRA0CgYEAkE7pIPlE71qvfJQgoA9em0gI\n"\
30"LAuE4Pu13aKiJnfft7hIjbK+5kyb3TysZvoyDnb3HOKvInK7vXbKuU4ISgxB2bB3HcYzQMGsz1qJ\n"\
31"2gG0N5hvJpzwwhbhXqFKA4zaaSrw622wDniAK5MlIE0tIAKKP4yxNGjoD2QYjhBGuhvkWKY=\n"\
32"-----END RSA PRIVATE KEY-----\n"
33
34typedef struct {
35 QThread * cb_thread;
36 RaopCallbackHandler * cb_handler;
37 void * cb_session;
38} audio_session_t;
39
40static void*
41audio_init_cb(void *cls, int bits, int channels, int samplerate)
42{
43 audio_session_t *audio_session = 0;
44
45 audio_session = (audio_session_t *)calloc(1, sizeof(audio_session_t));
46 audio_session->cb_thread = new QThread();
47 audio_session->cb_thread->start();
48
49 /* This whole hack is required because QAudioOutput
50 * needs to be created in a QThread, threads created
51 * outside Qt are not allowed (they have no eventloop) */
52 audio_session->cb_handler = new RaopCallbackHandler();
53 audio_session->cb_handler->moveToThread(audio_session->cb_thread);
54 audio_session->cb_handler->init((RaopCallbacks *)cls);
55
56 QMetaObject::invokeMethod(audio_session->cb_handler, "audioInit",
57 Qt::BlockingQueuedConnection,
58 Q_ARG(void*, (void*)&audio_session->cb_session),
59 Q_ARG(int, bits),
60 Q_ARG(int, channels),
61 Q_ARG(int, samplerate));
62 return audio_session;
63}
64
65static void
66audio_process_cb(void *cls, void *session, const void *buffer, int buflen)
67{
68 Q_UNUSED(cls)
69 audio_session_t *audio_session = (audio_session_t *)session;
70 QMetaObject::invokeMethod(audio_session->cb_handler, "audioProcess",
71 Qt::BlockingQueuedConnection,
72 Q_ARG(void*, audio_session->cb_session),
73 Q_ARG(void*, (void*)buffer),
74 Q_ARG(int, buflen));
75}
76
77static void
78audio_destroy_cb(void *cls, void *session)
79{
80 Q_UNUSED(cls)
81 audio_session_t *audio_session = (audio_session_t *)session;
82 QMetaObject::invokeMethod(audio_session->cb_handler, "audioDestroy",
83 Qt::BlockingQueuedConnection,
84 Q_ARG(void*, audio_session->cb_session));
85
86 // Wait until the session thread has finished
87 audio_session->cb_thread->quit();
88 audio_session->cb_thread->wait();
89
90 // Delete all session variables
91 delete audio_session->cb_handler;
92 delete audio_session->cb_thread;
93 free(audio_session);
94}
95
96static void
97audio_flush_cb(void *cls, void *session)
98{
99 Q_UNUSED(cls)
100 audio_session_t *audio_session = (audio_session_t *)session;
101 QMetaObject::invokeMethod(audio_session->cb_handler, "audioFlush",
102 Qt::BlockingQueuedConnection,
103 Q_ARG(void*, audio_session->cb_session));
104}
105
106static void
107audio_set_volume_cb(void *cls, void *session, float volume)
108{
109 Q_UNUSED(cls)
110 audio_session_t *audio_session = (audio_session_t *)session;
111 QMetaObject::invokeMethod(audio_session->cb_handler, "audioSetVolume",
112 Qt::BlockingQueuedConnection,
113 Q_ARG(void*, audio_session->cb_session),
114 Q_ARG(float, volume));
115}
116
117static void
118audio_set_metadata_cb(void *cls, void *session, const void *buffer, int buflen)
119{
120 Q_UNUSED(cls)
121 audio_session_t *audio_session = (audio_session_t *)session;
122 QMetaObject::invokeMethod(audio_session->cb_handler, "audioSetVolume",
123 Qt::BlockingQueuedConnection,
124 Q_ARG(void*, audio_session->cb_session),
125 Q_ARG(void*, (void*)buffer),
126 Q_ARG(int, buflen));
127}
128
129static void
130audio_set_coverart_cb(void *cls, void *session, const void *buffer, int buflen)
131{
132 Q_UNUSED(cls)
133 audio_session_t *audio_session = (audio_session_t *)session;
134 QMetaObject::invokeMethod(audio_session->cb_handler, "audioSetVolume",
135 Qt::BlockingQueuedConnection,
136 Q_ARG(void*, audio_session->cb_session),
137 Q_ARG(void*, (void*)buffer),
138 Q_ARG(int, buflen));
139}
140
141RaopService::RaopService(QObject *parent) :
142 QObject(parent),
143 m_raop(0)
144{
145}
146
147RaopService::~RaopService()
148{
149 this->stop();
150 raop_destroy(m_raop);
151}
152
153bool RaopService::init(int max_clients, RaopCallbacks *callbacks)
154{
155 raop_callbacks_t raop_cbs;
156
157 raop_cbs.cls = callbacks;
158 raop_cbs.audio_init = &audio_init_cb;
159 raop_cbs.audio_process = &audio_process_cb;
160 raop_cbs.audio_destroy = &audio_destroy_cb;
161 raop_cbs.audio_flush = &audio_flush_cb;
162 raop_cbs.audio_set_volume = &audio_set_volume_cb;
163 raop_cbs.audio_set_metadata = &audio_set_metadata_cb;
164 raop_cbs.audio_set_coverart = &audio_set_coverart_cb;
165
166 m_raop = raop_init(max_clients, &raop_cbs, RSA_KEY);
167 if (!m_raop) {
168 return false;
169 }
170 return true;
171}
172
173bool RaopService::isRunning()
174{
175 return (raop_is_running(m_raop) != 0);
176}
177
178bool RaopService::start(quint16 port, const QByteArray & hwaddr)
179{
180 int ret;
181 ret = raop_start(m_raop, &port, hwaddr.data(), hwaddr.size(), 0);
182 if (ret < 0) {
183 return false;
184 }
185 return true;
186}
187
188void RaopService::stop()
189{
190 if (m_raop) {
191 raop_stop(m_raop);
192 }
193}