experiment to run video via websocket

This commit is contained in:
henryruhs
2026-03-20 10:49:32 +01:00
parent 3acb71c44e
commit 87c2eebb2d
7 changed files with 113 additions and 84 deletions
+8 -18
View File
@@ -1,21 +1,11 @@
from aiortc import RTCPeerConnection, VideoStreamTrack
from facefusion.types import RtcOfferSet
import cv2
import numpy
async def create_rtc_offer() -> RtcOfferSet:
rtc_connection = RTCPeerConnection()
rtc_connection.addTrack(VideoStreamTrack())
rtc_offer = await rtc_connection.createOffer()
def create_test_frame_bytes(width : int, height : int) -> bytes:
vision_frame = numpy.zeros((height, width, 3), dtype = numpy.uint8)
is_success, image_buffer = cv2.imencode('.jpg', vision_frame)
await rtc_connection.setLocalDescription(rtc_offer)
rtc_offer_set : RtcOfferSet =\
{
'sdp': rtc_connection.localDescription.sdp,
'type': rtc_connection.localDescription.type
}
await rtc_connection.close()
return rtc_offer_set
if is_success:
return image_buffer.tobytes()
return b''
+8 -11
View File
@@ -1,4 +1,3 @@
import asyncio
import tempfile
from typing import Iterator
@@ -13,7 +12,6 @@ from facefusion.apis.core import create_api
from facefusion.core import common_pre_check, processors_pre_check
from facefusion.download import conditional_download
from .assert_helper import get_test_example_file, get_test_examples_directory
from .stream_helper import create_rtc_offer
@pytest.fixture(scope = 'module', autouse = True)
@@ -101,7 +99,7 @@ def test_stream_image(test_client : TestClient) -> None:
assert output_vision_frame.shape == (1024, 1024, 3)
def test_stream_video(test_client : TestClient) -> None:
def test_stream_live(test_client : TestClient) -> None:
create_session_response = test_client.post('/session', json =
{
'client_version': metadata.get('version')
@@ -129,12 +127,11 @@ def test_stream_video(test_client : TestClient) -> None:
'Authorization': 'Bearer ' + access_token
})
rtc_offer = asyncio.run(create_rtc_offer())
stream_response = test_client.post('/stream', json = rtc_offer, headers =
{
'Authorization': 'Bearer ' + access_token
})
with test_client.websocket_connect('/stream/live', subprotocols =
[
'access_token.' + access_token
]) as websocket:
websocket.send_bytes(source_content)
output_bytes = websocket.receive_bytes()
assert stream_response.status_code == 200
assert stream_response.json().get('type') == 'answer'
assert stream_response.json().get('sdp')
assert len(output_bytes) > 0