mirror of
https://github.com/facefusion/facefusion.git
synced 2026-07-30 22:07:23 +02:00
Push based receive with queue (#1146)
* move to push based receive * move to push based receive, fix mocks * fix tests * add todos * remove asyncio * remove asyncio * resolve todos * move to queue without events * prevent debug spam * concurrent stream inference stream_video.py: pipeline face-swap inference across execution_thread_count workers (ThreadPoolExecutor + bounded in-flight deque, ordered encode) to keep the GPU busy during encode Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * add todos * add todos * add missing state --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
1f494f54db
commit
775985645e
@@ -1,13 +1,14 @@
|
||||
import ctypes
|
||||
import threading
|
||||
from collections import deque
|
||||
from functools import partial
|
||||
from queue import Queue
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import numpy
|
||||
import pytest
|
||||
|
||||
from facefusion import rtc, rtc_store, state_manager
|
||||
from facefusion.apis.stream_audio import fill_audio_deque, receive_audio_frames, run_audio_encode_loop
|
||||
from facefusion.apis.stream_audio import handle_audio_frame, receive_audio_frames, run_audio_encode_loop
|
||||
from facefusion.download import conditional_download
|
||||
from facefusion.ffmpeg import read_audio_buffer
|
||||
from facefusion.hash_helper import create_hash
|
||||
@@ -52,54 +53,36 @@ def test_run_audio_encode_loop() -> None:
|
||||
'receiver_bitrate': ctypes.c_uint(0)
|
||||
}
|
||||
|
||||
audio_deque : deque[AudioPack] = deque()
|
||||
audio_event = threading.Event()
|
||||
audio_queue : Queue[AudioPack] = Queue(maxsize = 300)
|
||||
|
||||
audio_deque.append((audio_frame, 0.100))
|
||||
audio_event.set()
|
||||
audio_queue.put((audio_frame, 0.100))
|
||||
|
||||
encoder_mock = MagicMock()
|
||||
encoder_mock.encode.return_value = bytes([ 1 ] * 32)
|
||||
|
||||
with patch('facefusion.apis.stream_audio.opus_encoder.encode', encoder_mock.encode):
|
||||
with patch('facefusion.apis.stream_audio.rtc.send_audio') as send_audio_mock:
|
||||
audio_loop_thread = threading.Thread(target = run_audio_encode_loop, args = (rtc_peer, audio_deque, audio_event), daemon = True)
|
||||
audio_loop_thread = threading.Thread(target = run_audio_encode_loop, args = (rtc_peer, audio_queue), daemon = True)
|
||||
audio_loop_thread.start()
|
||||
audio_deque.append((numpy.empty(0), 0.0))
|
||||
audio_event.set()
|
||||
audio_queue.put((numpy.empty(0), 0.0))
|
||||
audio_loop_thread.join(timeout = 5.0)
|
||||
|
||||
assert encoder_mock.encode.called is True
|
||||
assert send_audio_mock.called is True
|
||||
|
||||
|
||||
def test_fill_audio_deque() -> None:
|
||||
audio_buffer = read_audio_buffer(get_test_example_file('source.mp3'), 48000, 16, 2)
|
||||
audio_frame = numpy.frombuffer(audio_buffer, dtype = numpy.int16).astype(numpy.float32) / 32768.0
|
||||
audio_decoder_mock = MagicMock()
|
||||
audio_deque : deque[AudioPack] = deque()
|
||||
audio_event = threading.Event()
|
||||
|
||||
with patch('facefusion.apis.stream_audio.decode_audio_frame', return_value = audio_frame.tobytes()):
|
||||
fill_audio_deque('opus', audio_decoder_mock, audio_frame.tobytes(), audio_deque, audio_event)
|
||||
|
||||
buffer_frame, _ = audio_deque.popleft()
|
||||
|
||||
assert audio_event.is_set()
|
||||
assert create_hash(buffer_frame.tobytes()) == create_hash(audio_frame.tobytes())
|
||||
|
||||
|
||||
@pytest.mark.parametrize('audio_codec', [ 'opus' ])
|
||||
def test_receive_audio_frames(audio_codec : AudioCodec) -> None:
|
||||
audio_buffer = read_audio_buffer(get_test_example_file('source.mp3'), 48000, 16, 2)
|
||||
audio_frame = numpy.frombuffer(audio_buffer, dtype = numpy.int16).astype(numpy.float32) / 32768.0
|
||||
audio_deque : deque[AudioPack] = deque()
|
||||
audio_event = threading.Event()
|
||||
audio_queue : Queue[AudioPack] = Queue(maxsize = 300)
|
||||
|
||||
datachannel_library_mock = MagicMock()
|
||||
datachannel_library_mock.rtcReceiveMessage.side_effect = [ 0, -1 ]
|
||||
datachannel_mock = MagicMock()
|
||||
ready_event = threading.Event()
|
||||
# todo: lambda not allowed
|
||||
datachannel_mock.rtcSetClosedCallback.side_effect = partial(lambda event, *args: event.set(), ready_event)
|
||||
|
||||
with patch('facefusion.apis.stream_audio.datachannel_module.create_static_library', return_value = datachannel_library_mock):
|
||||
with patch('facefusion.libraries.datachannel.create_static_library', return_value = datachannel_mock):
|
||||
with patch('facefusion.apis.stream_audio.decode_audio_frame', return_value = audio_frame.tobytes()):
|
||||
rtc_peer_audio : RtcPeerAudio =\
|
||||
{
|
||||
@@ -107,10 +90,27 @@ def test_receive_audio_frames(audio_codec : AudioCodec) -> None:
|
||||
'receiver_track': 0,
|
||||
'codec': audio_codec
|
||||
}
|
||||
audio_receiver_thread = threading.Thread(target = receive_audio_frames, args = (rtc_peer_audio, audio_deque, audio_event), daemon = True)
|
||||
audio_receiver_thread = threading.Thread(target = receive_audio_frames, args = (rtc_peer_audio, audio_queue), daemon = True)
|
||||
audio_receiver_thread.start()
|
||||
ready_event.wait(timeout = 5.0)
|
||||
datachannel_mock.rtcSetFrameCallback.call_args[0][1](0, bytes([ 0 ]), 1, None, None)
|
||||
datachannel_mock.rtcSetClosedCallback.call_args[0][1](0, None)
|
||||
audio_receiver_thread.join(timeout = 5.0)
|
||||
|
||||
buffer_frame, _ = audio_deque.popleft()
|
||||
buffer_frame, _ = audio_queue.get_nowait()
|
||||
|
||||
assert create_hash(buffer_frame.tobytes()) == create_hash(audio_frame.tobytes())
|
||||
|
||||
|
||||
def test_handle_audio_frame() -> None:
|
||||
audio_buffer = read_audio_buffer(get_test_example_file('source.mp3'), 48000, 16, 2)
|
||||
audio_frame = numpy.frombuffer(audio_buffer, dtype = numpy.int16).astype(numpy.float32) / 32768.0
|
||||
audio_decoder_mock = MagicMock()
|
||||
audio_queue : Queue[AudioPack] = Queue(maxsize = 300)
|
||||
|
||||
with patch('facefusion.apis.stream_audio.decode_audio_frame', return_value = audio_frame.tobytes()):
|
||||
handle_audio_frame('opus', audio_decoder_mock, audio_queue, 0, ctypes.c_void_p(), 1, ctypes.c_void_p(), ctypes.c_void_p())
|
||||
|
||||
buffer_frame, _ = audio_queue.get_nowait()
|
||||
|
||||
assert create_hash(buffer_frame.tobytes()) == create_hash(audio_frame.tobytes())
|
||||
|
||||
Reference in New Issue
Block a user