3 Copyright (C) 2012 Juho Vähä-Herttua
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:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
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.
31 audio_init_prototype
= CFUNCTYPE(py_object
, c_void_p
, c_int
, c_int
, c_int
)
32 audio_set_volume_prototype
= CFUNCTYPE(None, c_void_p
, c_void_p
, c_float
)
33 audio_process_prototype
= CFUNCTYPE(None, c_void_p
, c_void_p
, c_void_p
, c_int
)
34 audio_flush_prototype
= CFUNCTYPE(None, c_void_p
, c_void_p
)
35 audio_destroy_prototype
= CFUNCTYPE(None, c_void_p
, c_void_p
)
37 class RaopNativeCallbacks(Structure
):
38 _fields_
= [("cls", py_object
),
39 ("audio_init", audio_init_prototype
),
40 ("audio_set_volume", audio_set_volume_prototype
),
41 ("audio_process", audio_process_prototype
),
42 ("audio_flush", audio_flush_prototype
),
43 ("audio_destroy", audio_destroy_prototype
)]
45 def LoadShairplay(path
):
46 if sys
.maxsize
< 2**32:
47 libname
= "shairplay32"
49 libname
= "shairplay64"
51 if platform
.system() == "Windows":
52 libname
= libname
+ ".dll"
53 elif platform
.system() == "Darwin":
54 libname
= "lib" + libname
+ ".dylib"
56 libname
= "lib" + libname
+ ".so"
59 fullpath
= os
.path
.join(path
, libname
)
60 libshairplay
= cdll
.LoadLibrary(fullpath
)
62 raise RuntimeError("Couldn't load shairplay library " + libname
)
64 # Initialize dnssd related functions
65 libshairplay
.dnssd_init
.restype
= c_void_p
66 libshairplay
.dnssd_init
.argtypes
= [POINTER(c_int
)]
67 libshairplay
.dnssd_register_raop
.restype
= c_int
68 libshairplay
.dnssd_register_raop
.argtypes
= [c_void_p
, c_char_p
, c_ushort
, POINTER(c_char
), c_int
]
69 libshairplay
.dnssd_register_airplay
.restype
= c_int
70 libshairplay
.dnssd_register_airplay
.argtypes
= [c_void_p
, c_char_p
, c_ushort
, POINTER(c_char
), c_int
]
71 libshairplay
.dnssd_unregister_raop
.restype
= None
72 libshairplay
.dnssd_unregister_raop
.argtypes
= [c_void_p
]
73 libshairplay
.dnssd_unregister_airplay
.restype
= None
74 libshairplay
.dnssd_unregister_airplay
.argtypes
= [c_void_p
]
75 libshairplay
.dnssd_destroy
.restype
= None
76 libshairplay
.dnssd_destroy
.argtypes
= [c_void_p
]
78 # Initialize raop related functions
79 libshairplay
.raop_init
.restype
= c_void_p
80 libshairplay
.raop_init
.argtypes
= [POINTER(RaopNativeCallbacks
), c_char_p
]
81 libshairplay
.raop_start
.restype
= c_int
82 libshairplay
.raop_start
.argtypes
= [c_void_p
, POINTER(c_ushort
), POINTER(c_char
), c_int
]
83 libshairplay
.raop_stop
.restype
= None
84 libshairplay
.raop_stop
.argtypes
= [c_void_p
]
85 libshairplay
.raop_destroy
.restype
= None
86 libshairplay
.raop_destroy
.argtypes
= [c_void_p
]
91 -----BEGIN RSA PRIVATE KEY-----
92 MIIEpQIBAAKCAQEA59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUt
93 wC5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZ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J7OKlGt0FMSdJD5KG0XPIpAVNwgpXXH5MDJg09KHeh0kXo+QA6viFBi21y340NonnEfdf
108 54PX4ZGS/Xac1UK+pLkBB+zRAoGAf0AY3H3qKS2lMEI4bzEFoHeK3G895pDaK3TFBVmD7fV0Zhov
109 17fegFPMwOII8MisYm9ZfT2Z0s5Ro3s5rkt+nvLAdfC/PYPKzTLalpGSwomSNYJcB9HNMlmhkGzc
110 1JnLYT4iyUyx6pcZBmCd8bD0iwY/FzcgNDaUmbX9+XDvRA0CgYEAkE7pIPlE71qvfJQgoA9em0gI
111 LAuE4Pu13aKiJnfft7hIjbK+5kyb3TysZvoyDnb3HOKvInK7vXbKuU4ISgxB2bB3HcYzQMGsz1qJ
112 2gG0N5hvJpzwwhbhXqFKA4zaaSrw622wDniAK5MlIE0tIAKKP4yxNGjoD2QYjhBGuhvkWKY=
113 -----END RSA PRIVATE KEY-----
117 def audio_init(self
, bits
, channels
, samplerate
):
118 raise NotImplementedError()
120 def audio_set_volume(self
, session
, volume
):
123 def audio_process(self
, session
, buffer):
124 raise NotImplementedError()
126 def audio_flush(self
, session
):
129 def audio_destroy(self
, session
):
133 def audio_init_cb(self
, cls
, bits
, channels
, samplerate
):
134 session
= self
.callbacks
.audio_init(bits
, channels
, samplerate
)
135 self
.sessions
.append(session
)
138 def audio_set_volume_cb(self
, cls
, sessionptr
, volume
):
139 session
= cast(sessionptr
, py_object
).value
140 self
.callbacks
.audio_set_volume(session
, volume
)
142 def audio_process_cb(self
, cls
, sessionptr
, buffer, buflen
):
143 session
= cast(sessionptr
, py_object
).value
144 strbuffer
= string_at(buffer, buflen
)
145 self
.callbacks
.audio_process(session
, strbuffer
)
147 def audio_flush_cb(self
, cls
, sessionptr
):
148 session
= cast(sessionptr
, py_object
).value
149 self
.callbacks
.audio_flush(session
)
151 def audio_destroy_cb(self
, cls
, sessionptr
):
152 session
= cast(sessionptr
, py_object
).value
153 self
.callbacks
.audio_destroy(session
)
154 if session
in self
.sessions
:
155 self
.sessions
.remove(session
)
158 def __init__(self
, libshairplay
, callbacks
):
159 self
.libshairplay
= libshairplay
160 self
.callbacks
= callbacks
164 # We need to hold a reference to native_callbacks
165 self
.native_callbacks
= RaopNativeCallbacks()
166 self
.native_callbacks
.audio_init
= audio_init_prototype(self
.audio_init_cb
)
167 self
.native_callbacks
.audio_set_volume
= audio_set_volume_prototype(self
.audio_set_volume_cb
)
168 self
.native_callbacks
.audio_process
= audio_process_prototype(self
.audio_process_cb
)
169 self
.native_callbacks
.audio_flush
= audio_flush_prototype(self
.audio_flush_cb
)
170 self
.native_callbacks
.audio_destroy
= audio_destroy_prototype(self
.audio_destroy_cb
)
172 # Initialize the raop instance with our callbacks
173 self
.instance
= self
.libshairplay
.raop_init(pointer(self
.native_callbacks
), RSA_KEY
)
174 if self
.instance
== None:
175 raise RuntimeError("Initializing library failed")
178 if self
.instance
!= None:
179 self
.libshairplay
.raop_destroy(self
.instance
)
182 def start(self
, port
, hwaddrstr
):
183 port
= c_ushort(port
)
184 hwaddr
= create_string_buffer(hwaddrstr
, len(hwaddrstr
))
186 ret
= self
.libshairplay
.raop_start(self
.instance
, pointer(port
), hwaddr
, c_int(len(hwaddr
)))
188 raise RuntimeError("Starting RAOP instance failed")
192 self
.libshairplay
.raop_stop(self
.instance
)
195 def __init__(self
, libshairplay
):
196 self
.libshairplay
= libshairplay
201 self
.instance
= self
.libshairplay
.dnssd_init(pointer(error
))
202 if self
.instance
== None:
203 raise RuntimeError("Initializing library failed: " + str(error
.value
))
206 if self
.instance
!= None:
207 self
.libshairplay
.dnssd_destroy(self
.instance
)
210 def register_raop(self
, name
, port
, hwaddrstr
):
211 hwaddr
= create_string_buffer(hwaddrstr
, len(hwaddrstr
))
212 self
.libshairplay
.dnssd_register_raop(self
.instance
, name
, c_ushort(port
), hwaddr
, len(hwaddr
))
214 def unregister_raop(self
):
215 self
.libshairplay
.dnssd_unregister_raop(self
.instance
)
217 def register_airplay(self
, name
, port
, hwaddrstr
):
218 hwaddr
= create_string_buffer(hwaddrstr
, len(hwaddrstr
))
219 self
.libshairplay
.dnssd_register_airplay(self
.instance
, name
, c_ushort(port
), hwaddr
, len(hwaddr
))
221 def unregister_airplay(self
):
222 self
.libshairplay
.dnssd_unregister_airplay(self
.instance
)