75 lines
3.0 KiB
Python
75 lines
3.0 KiB
Python
import asyncio
|
|
import base64
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
import wave
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
import websockets
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
|
|
def token():
|
|
value = os.environ.get('LAN_VAD_TOKEN')
|
|
if not value:
|
|
raise RuntimeError('provide LAN_VAD_TOKEN in process environment')
|
|
return value
|
|
|
|
|
|
async def check():
|
|
url = 'ws://192.168.31.61:5010/ws/vad'
|
|
try:
|
|
async with websockets.connect(url, proxy=None):
|
|
raise AssertionError('unauthenticated websocket accepted')
|
|
except websockets.exceptions.InvalidStatus as exc:
|
|
assert exc.response.status_code == 403
|
|
headers = {'Authorization': 'Bearer ' + token()}
|
|
events = []
|
|
with wave.open(str(ROOT / 'tmp/public_tts_16k.wav')) as w:
|
|
pcm = bytes(16000) + w.readframes(w.getnframes()) + bytes(32000)
|
|
started = time.perf_counter()
|
|
async with websockets.connect(url, additional_headers=headers, proxy=None) as a, websockets.connect(url, additional_headers=headers, proxy=None) as b:
|
|
for ws in (a, b):
|
|
await ws.send(json.dumps({'type': 'session.start', 'include_preroll_audio': ws is a}))
|
|
assert json.loads(await ws.recv())['type'] == 'session.ready'
|
|
await b.send(bytes(32000))
|
|
await b.send(json.dumps({'type': 'ping'}))
|
|
assert json.loads(await b.recv())['type'] == 'pong'
|
|
|
|
async def receive():
|
|
while True:
|
|
data = json.loads(await a.recv())
|
|
if data['type'] == 'speech.start':
|
|
decoded = base64.b64decode(data.pop('preroll_pcm16_base64'))
|
|
expected = pcm[data['preroll_start_sample'] * 2:data['audio_until_sample'] * 2]
|
|
assert decoded == expected
|
|
data['preroll_bytes_verified'] = len(decoded)
|
|
events.append(data)
|
|
if data['type'] == 'session.ended': return
|
|
|
|
task = asyncio.create_task(receive())
|
|
for i in range(0, len(pcm), 640):
|
|
await a.send(pcm[i:i + 640])
|
|
await asyncio.sleep(0.02)
|
|
await a.send(json.dumps({'type': 'audio.end'}))
|
|
await asyncio.wait_for(task, 10)
|
|
assert any(e['type'] == 'speech.start' for e in events)
|
|
assert any(e['type'] == 'speech.end' and e['reason'] == 'silence' for e in events)
|
|
await a.send(json.dumps({'type': 'reset'}))
|
|
assert json.loads(await a.recv())['type'] == 'session.reset'
|
|
await a.send(bytes(32000))
|
|
await a.send(json.dumps({'type': 'ping'}))
|
|
assert json.loads(await a.recv())['type'] == 'pong'
|
|
result = {'status': 'passed', 'checks': ['auth_rejected', 'real_silero_speech_start_end', 'preroll_bytes', 'two_session_isolation', 'reset', 'real_time_20ms_pacing'], 'elapsed_seconds': round(time.perf_counter() - started, 3), 'events': events}
|
|
print(json.dumps(result, ensure_ascii=False))
|
|
(ROOT / 'tmp/ws_integration.json').write_text(json.dumps(result, ensure_ascii=False, indent=2) + '\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(check())
|