Skip to content

Commit

Permalink
Add a streaming audio input endpoint
Browse files Browse the repository at this point in the history
Outline handling of client audio stream
  • Loading branch information
NeonDaniel committed Sep 21, 2024
1 parent 3bdeabc commit 8df1130
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions neon_hana/app/routers/node_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ async def node_v1_endpoint(websocket: WebSocket, token: str):
disconnect_event.set()


@node_route.websocket("/v1/stream")
async def node_v1_endpoint(websocket: WebSocket, token: str):
"""
Endpoint to handle a stream of raw audio bytes. A client using this endpoint
must first establish a connection to the `/v1` endpoint.
"""
client_id = client_manager.get_client_id(token)
if not socket_api.get_session(client_id):
raise HTTPException(status_code=401,
detail=f"Client not known ({client_id})")
await websocket.accept()
disconnect_event = Event()

while not disconnect_event.is_set():
try:
client_in: bytes = await websocket.receive_bytes()
socket_api.handle_audio_stream(client_in, client_id)
except WebSocketDisconnect:
disconnect_event.set()


@node_route.get("/v1/doc")
async def node_v1_doc(_: Optional[Union[NodeAudioInput, NodeGetStt,
NodeGetTts]]) -> \
Expand Down
4 changes: 4 additions & 0 deletions neon_hana/mq_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ def _update_session_data(self, message: Message):
if user_config:
self._sessions[session_id]['user'] = user_config

def handle_audio_stream(self, audio: bytes, session_id: str):
LOG.info(f"Got {len(audio)} bytes from {session_id}")
# TODO: Do something with this audio stream

def handle_client_input(self, data: dict, session_id: str):
"""
Handle some client input data.
Expand Down

0 comments on commit 8df1130

Please sign in to comment.