| 1 |
import numbers |
| 2 |
import pytest |
| 3 |
from threading import Timer |
| 4 |
|
| 5 |
import pyglet |
| 6 |
pyglet.options['debug_media'] = False |
| 7 |
|
| 8 |
from pyglet.media.codecs import AudioFormat |
| 9 |
from pyglet.media.synthesis import Silence |
| 10 |
|
| 11 |
try: |
| 12 |
from pyglet.media.drivers.pulse import interface |
| 13 |
except ImportError: |
| 14 |
interface = None |
| 15 |
|
| 16 |
|
| 17 |
pytestmark = pytest.mark.skipif(interface is None, reason='requires PulseAudio') |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture |
| 21 |
def mainloop(): |
| 22 |
return interface.PulseAudioMainloop() |
| 23 |
|
| 24 |
|
| 25 |
def test_mainloop_run(mainloop): |
| 26 |
mainloop.start() |
| 27 |
mainloop.delete() |
| 28 |
|
| 29 |
|
| 30 |
def test_mainloop_lock(mainloop): |
| 31 |
mainloop.start() |
| 32 |
mainloop.lock_() |
| 33 |
mainloop.unlock() |
| 34 |
mainloop.delete() |
| 35 |
|
| 36 |
|
| 37 |
def test_mainloop_signal(mainloop): |
| 38 |
mainloop.start() |
| 39 |
with mainloop.lock: |
| 40 |
mainloop.signal() |
| 41 |
mainloop.delete() |
| 42 |
|
| 43 |
|
| 44 |
def test_mainloop_wait_signal(mainloop): |
| 45 |
mainloop.start() |
| 46 |
|
| 47 |
def signal(): |
| 48 |
with mainloop.lock: |
| 49 |
mainloop.signal() |
| 50 |
t = Timer(.1, signal) |
| 51 |
t.start() |
| 52 |
|
| 53 |
with mainloop.lock: |
| 54 |
mainloop.wait() |
| 55 |
mainloop.delete() |
| 56 |
|
| 57 |
|
| 58 |
@pytest.fixture |
| 59 |
def context(mainloop): |
| 60 |
mainloop.start() |
| 61 |
with mainloop.lock: |
| 62 |
context = mainloop.create_context() |
| 63 |
yield context |
| 64 |
|
| 65 |
with context.mainloop.lock: |
| 66 |
context.delete() |
| 67 |
mainloop.delete() |
| 68 |
|
| 69 |
|
| 70 |
def test_context_not_connected(context): |
| 71 |
assert context.is_ready == False |
| 72 |
assert context.is_failed == False |
| 73 |
assert context.is_terminated == False |
| 74 |
assert context.server == None |
| 75 |
assert isinstance(context.protocol_version, numbers.Integral) |
| 76 |
assert context.server_protocol_version == None |
| 77 |
assert context.is_local == None |
| 78 |
|
| 79 |
with context.mainloop.lock: |
| 80 |
context.delete() |
| 81 |
|
| 82 |
assert context.is_ready == False |
| 83 |
assert context.is_failed == False |
| 84 |
assert context.is_terminated == False |
| 85 |
assert context.server == None |
| 86 |
assert context.protocol_version == None |
| 87 |
assert context.server_protocol_version == None |
| 88 |
assert context.is_local == None |
| 89 |
|
| 90 |
|
| 91 |
def test_context_connect(context): |
| 92 |
assert context.is_ready == False |
| 93 |
assert context.is_failed == False |
| 94 |
assert context.is_terminated == False |
| 95 |
assert context.server == None |
| 96 |
assert isinstance(context.protocol_version, numbers.Integral) |
| 97 |
assert context.server_protocol_version == None |
| 98 |
assert context.is_local == None |
| 99 |
|
| 100 |
context.connect() |
| 101 |
|
| 102 |
assert context.is_ready == True |
| 103 |
assert context.is_failed == False |
| 104 |
assert context.is_terminated == False |
| 105 |
assert isinstance(context.server, str) |
| 106 |
assert isinstance(context.protocol_version, numbers.Integral) |
| 107 |
assert isinstance(context.server_protocol_version, numbers.Integral) |
| 108 |
assert context.is_local == True |
| 109 |
|
| 110 |
with context.mainloop.lock: |
| 111 |
context.delete() |
| 112 |
|
| 113 |
assert context.is_ready == False |
| 114 |
assert context.is_failed == False |
| 115 |
assert context.is_terminated == True |
| 116 |
assert context.server == None |
| 117 |
assert context.protocol_version == None |
| 118 |
assert context.server_protocol_version == None |
| 119 |
assert context.is_local == None |
| 120 |
|
| 121 |
|
| 122 |
@pytest.fixture |
| 123 |
def stream(context): |
| 124 |
context.connect() |
| 125 |
audio_format = AudioFormat(1, 16, 44100) |
| 126 |
with context.mainloop.lock: |
| 127 |
stream = context.create_stream(audio_format) |
| 128 |
return stream |
| 129 |
|
| 130 |
|
| 131 |
@pytest.fixture |
| 132 |
def audio_source(): |
| 133 |
return Silence(10.0, 44100) |
| 134 |
|
| 135 |
|
| 136 |
@pytest.fixture |
| 137 |
def filled_stream(stream, audio_source): |
| 138 |
with stream.mainloop.lock: |
| 139 |
stream.connect_playback() |
| 140 |
|
| 141 |
assert stream.is_ready |
| 142 |
with stream.mainloop.lock: |
| 143 |
writable_size = stream.get_writable_size() |
| 144 |
assert writable_size > 0 |
| 145 |
|
| 146 |
nbytes = min(1024, writable_size) |
| 147 |
audio_data = audio_source.get_audio_data(nbytes) |
| 148 |
with stream.mainloop.lock: |
| 149 |
stream.write(audio_data.pointer, nbytes) |
| 150 |
|
| 151 |
assert stream.is_ready |
| 152 |
return stream |
| 153 |
|
| 154 |
|
| 155 |
def test_stream_create(stream): |
| 156 |
assert stream.is_unconnected == True |
| 157 |
assert stream.is_creating == False |
| 158 |
assert stream.is_ready == False |
| 159 |
assert stream.is_failed == False |
| 160 |
assert stream.is_terminated == False |
| 161 |
|
| 162 |
with stream.mainloop.lock: |
| 163 |
stream.delete() |
| 164 |
|
| 165 |
assert stream.is_unconnected == True |
| 166 |
assert stream.is_creating == False |
| 167 |
assert stream.is_ready == False |
| 168 |
assert stream.is_failed == False |
| 169 |
assert stream.is_terminated == False |
| 170 |
|
| 171 |
|
| 172 |
def test_stream_connect(stream): |
| 173 |
assert stream.is_unconnected == True |
| 174 |
assert stream.is_creating == False |
| 175 |
assert stream.is_ready == False |
| 176 |
assert stream.is_failed == False |
| 177 |
assert stream.is_terminated == False |
| 178 |
assert stream.index is None |
| 179 |
|
| 180 |
with stream.mainloop.lock: |
| 181 |
stream.connect_playback() |
| 182 |
|
| 183 |
assert stream.is_unconnected == False |
| 184 |
assert stream.is_creating == False |
| 185 |
assert stream.is_ready == True |
| 186 |
assert stream.is_failed == False |
| 187 |
assert stream.is_terminated == False |
| 188 |
assert isinstance(stream.index, numbers.Integral) |
| 189 |
|
| 190 |
with stream.mainloop.lock: |
| 191 |
stream.delete() |
| 192 |
|
| 193 |
assert stream.is_unconnected == False |
| 194 |
assert stream.is_creating == False |
| 195 |
assert stream.is_ready == False |
| 196 |
assert stream.is_failed == False |
| 197 |
assert stream.is_terminated == True |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
def test_stream_write(stream, audio_source): |
| 202 |
with stream.mainloop.lock: |
| 203 |
stream.connect_playback() |
| 204 |
|
| 205 |
assert stream.is_ready |
| 206 |
|
| 207 |
with stream.mainloop.lock: |
| 208 |
writable_size = stream.get_writable_size() |
| 209 |
assert writable_size > 0 |
| 210 |
|
| 211 |
nbytes = min(1024, writable_size) |
| 212 |
audio_data = audio_source.get_audio_data(nbytes) |
| 213 |
with stream.mainloop.lock: |
| 214 |
written = stream.write(audio_data.pointer, nbytes) |
| 215 |
assert written == nbytes |
| 216 |
|
| 217 |
assert stream.is_ready |
| 218 |
|
| 219 |
with stream.mainloop.lock: |
| 220 |
stream.delete() |
| 221 |
|
| 222 |
assert stream.is_terminated |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
def test_stream_timing_info(filled_stream): |
| 227 |
with filled_stream.mainloop.lock: |
| 228 |
op = filled_stream.update_timing_info() |
| 229 |
op.wait() |
| 230 |
assert op.is_done |
| 231 |
op.delete() |
| 232 |
|
| 233 |
info = filled_stream.get_timing_info() |
| 234 |
assert info is not None |
| 235 |
|
| 236 |
|
| 237 |
def test_stream_trigger(filled_stream): |
| 238 |
with filled_stream.mainloop.lock: |
| 239 |
op = filled_stream.trigger() |
| 240 |
op.wait() |
| 241 |
assert op.is_done |
| 242 |
op.delete() |
| 243 |
|
| 244 |
|
| 245 |
def test_stream_prebuf(filled_stream): |
| 246 |
with filled_stream.mainloop.lock: |
| 247 |
op = filled_stream.prebuf() |
| 248 |
op.wait() |
| 249 |
assert op.is_done |
| 250 |
op.delete() |
| 251 |
|
| 252 |
|
| 253 |
def test_stream_cork(filled_stream): |
| 254 |
assert filled_stream.is_corked() |
| 255 |
|
| 256 |
with filled_stream.mainloop.lock: |
| 257 |
op = filled_stream.resume() |
| 258 |
op.wait() |
| 259 |
assert op.is_done |
| 260 |
assert not filled_stream.is_corked() |
| 261 |
op.delete() |
| 262 |
|
| 263 |
with filled_stream.mainloop.lock: |
| 264 |
op = filled_stream.pause() |
| 265 |
op.wait() |
| 266 |
assert op.is_done |
| 267 |
assert filled_stream.is_corked() |
| 268 |
op.delete() |
| 269 |
|
| 270 |
|
| 271 |
def test_stream_update_sample_rate(filled_stream): |
| 272 |
with filled_stream.mainloop.lock: |
| 273 |
op = filled_stream.update_sample_rate(44100) |
| 274 |
op.wait() |
| 275 |
assert op.is_done |
| 276 |
op.delete() |
| 277 |
|
| 278 |
|
| 279 |
def test_stream_write_needed(stream, audio_source, event_loop): |
| 280 |
with stream.mainloop.lock: |
| 281 |
stream.connect_playback() |
| 282 |
|
| 283 |
assert stream.is_ready |
| 284 |
|
| 285 |
with stream.mainloop.lock: |
| 286 |
writable_size = stream.get_writable_size() |
| 287 |
assert writable_size > 0 |
| 288 |
|
| 289 |
def on_write_needed(_stream, nbytes, _userdata): |
| 290 |
on_write_needed.nbytes = nbytes |
| 291 |
|
| 292 |
on_write_needed.nbytes = None |
| 293 |
|
| 294 |
def on_underflow(_stream, _userdata): |
| 295 |
on_underflow.underflow = True |
| 296 |
event_loop.interrupt_event_loop() |
| 297 |
|
| 298 |
on_underflow.underflow = False |
| 299 |
|
| 300 |
stream.set_write_callback(on_write_needed) |
| 301 |
stream.set_underflow_callback(on_underflow) |
| 302 |
|
| 303 |
audio_data = audio_source.get_audio_data(writable_size) |
| 304 |
with stream.mainloop.lock: |
| 305 |
stream.write(audio_data.pointer, audio_data.length) |
| 306 |
assert stream.is_ready |
| 307 |
|
| 308 |
with stream.mainloop.lock: |
| 309 |
stream.resume().wait().delete() |
| 310 |
|
| 311 |
event_loop.run_event_loop(duration=0.1) |
| 312 |
|
| 313 |
assert on_write_needed.nbytes > 0 |
| 314 |
assert on_underflow.underflow == False |
| 315 |
|
| 316 |
on_write_needed.nbytes = None |
| 317 |
on_underflow.underflow = None |
| 318 |
|
| 319 |
event_loop.run_event_loop(duration=5.0) |
| 320 |
assert on_write_needed.nbytes > 0 |
| 321 |
assert on_underflow.underflow == True |