Commit | Line | Data |
---|---|---|
1f0de05c | 1 | # coding=utf-8 |
094e4ad1 | 2 | ''' |
6b27160f | 3 | Copyright (C) 2012 Juho Vähä-Herttua |
094e4ad1 | 4 | |
6b27160f JVH |
5 | Permission is hereby granted, free of charge, to any person obtaining |
6 | a copy of this software and associated documentation files (the | |
7 | "Software"), to deal in the Software without restriction, including | |
8 | without limitation the rights to use, copy, modify, merge, publish, | |
9 | distribute, sublicense, and/or sell copies of the Software, and to | |
10 | permit persons to whom the Software is furnished to do so, subject to | |
11 | the following conditions: | |
094e4ad1 | 12 | |
6b27160f JVH |
13 | The above copyright notice and this permission notice shall be included |
14 | in all copies or substantial portions of the Software. | |
15 | ||
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
094e4ad1 JVH |
23 | ''' |
24 | ||
8be1424a JVH |
25 | import os |
26 | import sys | |
27 | import platform | |
28 | ||
29 | from ctypes import * | |
30 | ||
23ab90af JVH |
31 | audio_init_prototype = CFUNCTYPE(py_object, c_void_p, c_int, c_int, c_int) |
32 | audio_process_prototype = CFUNCTYPE(None, c_void_p, c_void_p, c_void_p, c_int) | |
33 | audio_destroy_prototype = CFUNCTYPE(None, c_void_p, c_void_p) | |
34 | ||
35 | audio_flush_prototype = CFUNCTYPE(None, c_void_p, c_void_p) | |
36 | audio_set_volume_prototype = CFUNCTYPE(None, c_void_p, c_void_p, c_float) | |
37 | audio_set_metadata_prototype = CFUNCTYPE(None, c_void_p, c_void_p, c_void_p, c_int) | |
38 | audio_set_coverart_prototype = CFUNCTYPE(None, c_void_p, c_void_p, c_void_p, c_int) | |
8be1424a JVH |
39 | |
40 | class RaopNativeCallbacks(Structure): | |
23ab90af JVH |
41 | _fields_ = [("cls", py_object), |
42 | ("audio_init", audio_init_prototype), | |
43 | ("audio_process", audio_process_prototype), | |
44 | ("audio_destroy", audio_destroy_prototype), | |
45 | ("audio_flush", audio_flush_prototype), | |
46 | ("audio_set_volume", audio_set_volume_prototype), | |
47 | ("audio_set_metadata", audio_set_metadata_prototype), | |
48 | ("audio_set_coverart", audio_set_coverart_prototype)] | |
8be1424a | 49 | |
c1b1e2c3 | 50 | def InitShairplay(libshairplay): |
8be1424a JVH |
51 | # Initialize dnssd related functions |
52 | libshairplay.dnssd_init.restype = c_void_p | |
094e4ad1 | 53 | libshairplay.dnssd_init.argtypes = [POINTER(c_int)] |
8be1424a | 54 | libshairplay.dnssd_register_raop.restype = c_int |
a68fedbb | 55 | libshairplay.dnssd_register_raop.argtypes = [c_void_p, c_char_p, c_ushort, POINTER(c_char), c_int, c_int] |
8be1424a | 56 | libshairplay.dnssd_register_airplay.restype = c_int |
094e4ad1 | 57 | libshairplay.dnssd_register_airplay.argtypes = [c_void_p, c_char_p, c_ushort, POINTER(c_char), c_int] |
8be1424a JVH |
58 | libshairplay.dnssd_unregister_raop.restype = None |
59 | libshairplay.dnssd_unregister_raop.argtypes = [c_void_p] | |
60 | libshairplay.dnssd_unregister_airplay.restype = None | |
61 | libshairplay.dnssd_unregister_airplay.argtypes = [c_void_p] | |
62 | libshairplay.dnssd_destroy.restype = None | |
63 | libshairplay.dnssd_destroy.argtypes = [c_void_p] | |
64 | ||
65 | # Initialize raop related functions | |
66 | libshairplay.raop_init.restype = c_void_p | |
23ab90af | 67 | libshairplay.raop_init.argtypes = [c_int, POINTER(RaopNativeCallbacks), c_char_p] |
2528f9b0 JVH |
68 | libshairplay.raop_is_running.restype = c_int |
69 | libshairplay.raop_is_running.argtypes = [c_void_p] | |
8be1424a | 70 | libshairplay.raop_start.restype = c_int |
a68fedbb | 71 | libshairplay.raop_start.argtypes = [c_void_p, POINTER(c_ushort), POINTER(c_char), c_int, c_char_p] |
8be1424a JVH |
72 | libshairplay.raop_stop.restype = None |
73 | libshairplay.raop_stop.argtypes = [c_void_p] | |
74 | libshairplay.raop_destroy.restype = None | |
75 | libshairplay.raop_destroy.argtypes = [c_void_p] | |
76 | ||
c1b1e2c3 JVH |
77 | def LoadShairplay(path): |
78 | if sys.maxsize < 2**32: | |
79 | libname = "shairplay32" | |
80 | else: | |
81 | libname = "shairplay64" | |
82 | ||
83 | if platform.system() == "Windows": | |
84 | libname = libname + ".dll" | |
85 | elif platform.system() == "Darwin": | |
86 | libname = "lib" + libname + ".dylib" | |
87 | else: | |
88 | libname = "lib" + libname + ".so" | |
89 | ||
90 | try: | |
91 | fullpath = os.path.join(path, libname) | |
92 | libshairplay = cdll.LoadLibrary(fullpath) | |
93 | except: | |
94 | raise RuntimeError("Couldn't load shairplay library " + libname) | |
95 | ||
96 | InitShairplay(libshairplay) | |
8be1424a JVH |
97 | return libshairplay |
98 | ||
99 | RSA_KEY = """ | |
100 | -----BEGIN RSA PRIVATE KEY----- | |
101 | MIIEpQIBAAKCAQEA59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUt | |
102 | wC5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZHJ2YCCLlDRKSKv6kDqnw4U | |
103 | wPdpOMXziC/AMj3Z/lUVX1G7WSHCAWKf1zNS1eLvqr+boEjXuBOitnZ/bDzPHrTOZz0Dew0uowxf | |
104 | /+sG+NCK3eQJVxqcaJ/vEHKIVd2M+5qL71yJQ+87X6oV3eaYvt3zWZYD6z5vYTcrtij2VZ9Zmni/ | |
105 | UAaHqn9JdsBWLUEpVviYnhimNVvYFZeCXg/IdTQ+x4IRdiXNv5hEewIDAQABAoIBAQDl8Axy9XfW | |
106 | BLmkzkEiqoSwF0PsmVrPzH9KsnwLGH+QZlvjWd8SWYGN7u1507HvhF5N3drJoVU3O14nDY4TFQAa | |
107 | LlJ9VM35AApXaLyY1ERrN7u9ALKd2LUwYhM7Km539O4yUFYikE2nIPscEsA5ltpxOgUGCY7b7ez5 | |
108 | NtD6nL1ZKauw7aNXmVAvmJTcuPxWmoktF3gDJKK2wxZuNGcJE0uFQEG4Z3BrWP7yoNuSK3dii2jm | |
109 | lpPHr0O/KnPQtzI3eguhe0TwUem/eYSdyzMyVx/YpwkzwtYL3sR5k0o9rKQLtvLzfAqdBxBurciz | |
110 | aaA/L0HIgAmOit1GJA2saMxTVPNhAoGBAPfgv1oeZxgxmotiCcMXFEQEWflzhWYTsXrhUIuz5jFu | |
111 | a39GLS99ZEErhLdrwj8rDDViRVJ5skOp9zFvlYAHs0xh92ji1E7V/ysnKBfsMrPkk5KSKPrnjndM | |
112 | oPdevWnVkgJ5jxFuNgxkOLMuG9i53B4yMvDTCRiIPMQ++N2iLDaRAoGBAO9v//mU8eVkQaoANf0Z | |
113 | oMjW8CN4xwWA2cSEIHkd9AfFkftuv8oyLDCG3ZAf0vrhrrtkrfa7ef+AUb69DNggq4mHQAYBp7L+ | |
114 | k5DKzJrKuO0r+R0YbY9pZD1+/g9dVt91d6LQNepUE/yY2PP5CNoFmjedpLHMOPFdVgqDzDFxU8hL | |
115 | AoGBANDrr7xAJbqBjHVwIzQ4To9pb4BNeqDndk5Qe7fT3+/H1njGaC0/rXE0Qb7q5ySgnsCb3DvA | |
116 | cJyRM9SJ7OKlGt0FMSdJD5KG0XPIpAVNwgpXXH5MDJg09KHeh0kXo+QA6viFBi21y340NonnEfdf | |
117 | 54PX4ZGS/Xac1UK+pLkBB+zRAoGAf0AY3H3qKS2lMEI4bzEFoHeK3G895pDaK3TFBVmD7fV0Zhov | |
118 | 17fegFPMwOII8MisYm9ZfT2Z0s5Ro3s5rkt+nvLAdfC/PYPKzTLalpGSwomSNYJcB9HNMlmhkGzc | |
119 | 1JnLYT4iyUyx6pcZBmCd8bD0iwY/FzcgNDaUmbX9+XDvRA0CgYEAkE7pIPlE71qvfJQgoA9em0gI | |
120 | LAuE4Pu13aKiJnfft7hIjbK+5kyb3TysZvoyDnb3HOKvInK7vXbKuU4ISgxB2bB3HcYzQMGsz1qJ | |
121 | 2gG0N5hvJpzwwhbhXqFKA4zaaSrw622wDniAK5MlIE0tIAKKP4yxNGjoD2QYjhBGuhvkWKY= | |
122 | -----END RSA PRIVATE KEY----- | |
123 | """ | |
124 | ||
125 | class RaopCallbacks: | |
126 | def audio_init(self, bits, channels, samplerate): | |
127 | raise NotImplementedError() | |
128 | ||
8be1424a JVH |
129 | def audio_process(self, session, buffer): |
130 | raise NotImplementedError() | |
131 | ||
23ab90af JVH |
132 | def audio_destroy(self, session): |
133 | raise NotImplementedError() | |
134 | ||
135 | def audio_set_volume(self, session, volume): | |
136 | pass | |
137 | ||
8be1424a JVH |
138 | def audio_flush(self, session): |
139 | pass | |
140 | ||
23ab90af JVH |
141 | def audio_set_metadata(self, session, buffer): |
142 | pass | |
143 | ||
144 | def audio_set_coverart(self, session, buffer): | |
8be1424a JVH |
145 | pass |
146 | ||
147 | class RaopService: | |
148 | def audio_init_cb(self, cls, bits, channels, samplerate): | |
149 | session = self.callbacks.audio_init(bits, channels, samplerate) | |
1f0de05c JVH |
150 | self.sessions.append(session) |
151 | return session | |
8be1424a | 152 | |
8be1424a JVH |
153 | def audio_process_cb(self, cls, sessionptr, buffer, buflen): |
154 | session = cast(sessionptr, py_object).value | |
155 | strbuffer = string_at(buffer, buflen) | |
156 | self.callbacks.audio_process(session, strbuffer) | |
157 | ||
8be1424a JVH |
158 | def audio_destroy_cb(self, cls, sessionptr): |
159 | session = cast(sessionptr, py_object).value | |
160 | self.callbacks.audio_destroy(session) | |
1f0de05c JVH |
161 | if session in self.sessions: |
162 | self.sessions.remove(session) | |
8be1424a | 163 | |
23ab90af JVH |
164 | def audio_flush_cb(self, cls, sessionptr): |
165 | session = cast(sessionptr, py_object).value | |
166 | self.callbacks.audio_flush(session) | |
167 | ||
168 | def audio_set_volume_cb(self, cls, sessionptr, volume): | |
169 | session = cast(sessionptr, py_object).value | |
170 | self.callbacks.audio_set_volume(session, volume) | |
8be1424a | 171 | |
23ab90af JVH |
172 | def audio_set_metadata_cb(self, cls, sessionptr, buffer, buflen): |
173 | session = cast(sessionptr, py_object).value | |
174 | strbuffer = string_at(buffer, buflen) | |
175 | self.callbacks.audio_set_metadata(session, strbuffer) | |
176 | ||
177 | def audio_set_coverart_cb(self, cls, sessionptr, buffer, buflen): | |
178 | session = cast(sessionptr, py_object).value | |
179 | strbuffer = string_at(buffer, buflen) | |
180 | self.callbacks.audio_set_coverart(session, strbuffer) | |
181 | ||
182 | ||
183 | def __init__(self, libshairplay, max_clients, callbacks): | |
8be1424a JVH |
184 | self.libshairplay = libshairplay |
185 | self.callbacks = callbacks | |
1f0de05c | 186 | self.sessions = [] |
8be1424a JVH |
187 | self.instance = None |
188 | ||
189 | # We need to hold a reference to native_callbacks | |
190 | self.native_callbacks = RaopNativeCallbacks() | |
191 | self.native_callbacks.audio_init = audio_init_prototype(self.audio_init_cb) | |
8be1424a | 192 | self.native_callbacks.audio_process = audio_process_prototype(self.audio_process_cb) |
8be1424a | 193 | self.native_callbacks.audio_destroy = audio_destroy_prototype(self.audio_destroy_cb) |
23ab90af JVH |
194 | self.native_callbacks.audio_flush = audio_flush_prototype(self.audio_flush_cb) |
195 | self.native_callbacks.audio_set_volume = audio_set_volume_prototype(self.audio_set_volume_cb) | |
196 | self.native_callbacks.audio_set_metadata = audio_set_metadata_prototype(self.audio_set_metadata_cb) | |
197 | self.native_callbacks.audio_set_coverart = audio_set_coverart_prototype(self.audio_set_coverart_cb) | |
8be1424a JVH |
198 | |
199 | # Initialize the raop instance with our callbacks | |
23ab90af | 200 | self.instance = self.libshairplay.raop_init(max_clients, pointer(self.native_callbacks), RSA_KEY) |
8be1424a JVH |
201 | if self.instance == None: |
202 | raise RuntimeError("Initializing library failed") | |
203 | ||
204 | def __del__(self): | |
205 | if self.instance != None: | |
206 | self.libshairplay.raop_destroy(self.instance) | |
207 | self.instance = None | |
208 | ||
2528f9b0 JVH |
209 | def is_running(self): |
210 | if self.libshairplay.raop_is_running(self.instance): | |
211 | return True | |
212 | else: | |
213 | return False | |
214 | ||
a68fedbb | 215 | def start(self, port, hwaddrstr, password=None): |
8be1424a JVH |
216 | port = c_ushort(port) |
217 | hwaddr = create_string_buffer(hwaddrstr, len(hwaddrstr)) | |
218 | ||
a68fedbb | 219 | ret = self.libshairplay.raop_start(self.instance, pointer(port), hwaddr, c_int(len(hwaddr)), password) |
8be1424a JVH |
220 | if ret < 0: |
221 | raise RuntimeError("Starting RAOP instance failed") | |
222 | return port.value | |
223 | ||
224 | def stop(self): | |
225 | self.libshairplay.raop_stop(self.instance) | |
226 | ||
227 | class DnssdService: | |
228 | def __init__(self, libshairplay): | |
229 | self.libshairplay = libshairplay | |
230 | self.instance = None | |
231 | ||
8be1424a JVH |
232 | error = c_int(0) |
233 | ||
094e4ad1 | 234 | self.instance = self.libshairplay.dnssd_init(pointer(error)) |
8be1424a JVH |
235 | if self.instance == None: |
236 | raise RuntimeError("Initializing library failed: " + str(error.value)) | |
237 | ||
238 | def __del__(self): | |
239 | if self.instance != None: | |
240 | self.libshairplay.dnssd_destroy(self.instance) | |
241 | self.instance = None | |
242 | ||
a68fedbb | 243 | def register_raop(self, name, port, hwaddrstr, password=False): |
094e4ad1 | 244 | hwaddr = create_string_buffer(hwaddrstr, len(hwaddrstr)) |
a68fedbb JVH |
245 | use_pw = c_int(0) |
246 | if password: | |
247 | use_pw = c_int(1) | |
248 | self.libshairplay.dnssd_register_raop(self.instance, name, c_ushort(port), hwaddr, len(hwaddr), use_pw) | |
8be1424a JVH |
249 | |
250 | def unregister_raop(self): | |
251 | self.libshairplay.dnssd_unregister_raop(self.instance) | |
252 | ||
094e4ad1 JVH |
253 | def register_airplay(self, name, port, hwaddrstr): |
254 | hwaddr = create_string_buffer(hwaddrstr, len(hwaddrstr)) | |
255 | self.libshairplay.dnssd_register_airplay(self.instance, name, c_ushort(port), hwaddr, len(hwaddr)) | |
8be1424a JVH |
256 | |
257 | def unregister_airplay(self): | |
258 | self.libshairplay.dnssd_unregister_airplay(self.instance) | |
259 |