|
| 1 | +# Copyright 2026 LiveKit, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from livekit import rtc |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.asyncio |
| 24 | +async def test_audio_source_aclose_releases_waiter_and_cancels_timer() -> None: |
| 25 | + source = rtc.AudioSource(48000, 1) |
| 26 | + frame = rtc.AudioFrame.create(48000, 1, 48000) |
| 27 | + await source.capture_frame(frame) |
| 28 | + |
| 29 | + assert source._join_fut is not None |
| 30 | + assert source._join_handle is not None |
| 31 | + |
| 32 | + await source.aclose() |
| 33 | + |
| 34 | + assert source._ffi_handle.disposed |
| 35 | + assert source._join_fut is None |
| 36 | + assert source._join_handle is None |
| 37 | + |
| 38 | + # After aclose, wait_for_playout should return immediately without hanging |
| 39 | + await source.wait_for_playout() |
| 40 | + |
| 41 | + # Repeated calls are idempotent |
| 42 | + await source.aclose() |
| 43 | + |
| 44 | + |
| 45 | +def test_audio_source_sync_close() -> None: |
| 46 | + source = rtc.AudioSource(48000, 1) |
| 47 | + assert not source._ffi_handle.disposed |
| 48 | + |
| 49 | + source.close() |
| 50 | + assert source._ffi_handle.disposed |
| 51 | + |
| 52 | + # Repeated close is idempotent |
| 53 | + source.close() |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_audio_source_capture_after_close_is_noop() -> None: |
| 58 | + source = rtc.AudioSource(48000, 1) |
| 59 | + source.close() |
| 60 | + |
| 61 | + frame = rtc.AudioFrame.create(48000, 1, 480) |
| 62 | + # Should safely return without error |
| 63 | + await source.capture_frame(frame) |
| 64 | + |
| 65 | + |
| 66 | +def test_video_source_sync_close() -> None: |
| 67 | + source = rtc.VideoSource(640, 480) |
| 68 | + assert not source._ffi_handle.disposed |
| 69 | + |
| 70 | + source.close() |
| 71 | + assert source._ffi_handle.disposed |
| 72 | + |
| 73 | + # Repeated close is idempotent |
| 74 | + source.close() |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.asyncio |
| 78 | +async def test_video_source_aclose_and_capture_after_close() -> None: |
| 79 | + source = rtc.VideoSource(640, 480) |
| 80 | + assert not source._ffi_handle.disposed |
| 81 | + |
| 82 | + await source.aclose() |
| 83 | + assert source._ffi_handle.disposed |
| 84 | + |
| 85 | + # Capture after close is safely ignored |
| 86 | + frame = rtc.VideoFrame( |
| 87 | + 640, |
| 88 | + 480, |
| 89 | + rtc.VideoBufferType.RGBA, |
| 90 | + b"\x00" * (640 * 480 * 4), |
| 91 | + ) |
| 92 | + source.capture_frame(frame) |
| 93 | + |
| 94 | + # Repeated aclose is idempotent |
| 95 | + await source.aclose() |
0 commit comments