diff --git a/backend/connection/connection.py b/backend/connection/connection.py index 7707af11..9f6c3382 100644 --- a/backend/connection/connection.py +++ b/backend/connection/connection.py @@ -70,6 +70,7 @@ class Connection(ConnectionInterface): _tasks: list[asyncio.Task] _audio_record_handler: RecordHandler _video_record_handler: RecordHandler + _raw_video_record_handler: RecordHandler def __init__( self, @@ -121,7 +122,14 @@ def __init__( self._video_record_handler = RecordHandler( self._incoming_video, record, record_to ) + # Since experimenter's path recording is empty, so we try to avoid adding raw so experimenter will not be recorded + if (record_to != ""): + record_to += "_raw" + self._raw_video_record_handler = RecordHandler( + self._incoming_video, record, record_to + ) + self._dc = None self._tasks = [] @@ -274,13 +282,13 @@ async def set_muted(self, video: bool, audio: bool) -> None: async def start_recording(self) -> None: # For docstring see ConnectionInterface or hover over function declaration await asyncio.gather( - self._video_record_handler.start(), self._audio_record_handler.start() + self._video_record_handler.start(), self._raw_video_record_handler.start(), self._audio_record_handler.start() ) async def stop_recording(self) -> None: # For docstring see ConnectionInterface or hover over function declaration await asyncio.gather( - self._video_record_handler.stop(), self._audio_record_handler.stop() + self._video_record_handler.stop(), self._raw_video_record_handler.stop(), self._audio_record_handler.stop() ) async def set_video_filters(self, filters: list[FilterDict]) -> None: @@ -432,6 +440,7 @@ def _on_track(self, track: MediaStreamTrack): task = asyncio.create_task(self._incoming_video.set_track(track)) sender = self._main_pc.addTrack(self._incoming_video.subscribe()) self._video_record_handler.add_track(self._incoming_video.subscribe()) + self._raw_video_record_handler.add_track(track) self._listen_to_track_close(self._incoming_video, sender) else: self._logger.error(f"Unknown track kind {track.kind}. Ignoring track") diff --git a/backend/hub/record_handler.py b/backend/hub/record_handler.py index 5bfa9d61..408f7928 100644 --- a/backend/hub/record_handler.py +++ b/backend/hub/record_handler.py @@ -3,11 +3,14 @@ from __future__ import annotations import logging import time + from aiortc.contrib.media import MediaRecorder, MediaBlackhole +from pyee.asyncio import AsyncIOEventEmitter + from hub.track_handler import TrackHandler -class RecordHandler: +class RecordHandler(): """Handles audio and video recording of the stream.""" _logger: logging.Logger @@ -15,6 +18,7 @@ class RecordHandler: _record: bool _record_to: str _track: TrackHandler + _start_time: float def __init__( self, track: TrackHandler, record: bool = False, record_to: str = None @@ -40,6 +44,7 @@ def __init__( self._track = track self._record = record self._record_to = record_to + if self._track.kind == "audio": track_format = "mp3" else: @@ -47,13 +52,14 @@ def __init__( timestamp = time.strftime("%Y%m%d_%H%M%S") self._record_to = self._record_to + "_" + timestamp + "." + track_format - if self._record: + if self._record and self._record_to != "": self._recorder = MediaRecorder(self._record_to) else: self._recorder = MediaBlackhole() - + async def start(self) -> None: """Start recorder.""" + self._start_time = time.time() await self._recorder.start() self._logger.debug("Start recording: " + self._record_to) @@ -66,9 +72,11 @@ def add_track(self, track: TrackHandler) -> None: The audio/video track. """ self._recorder.addTrack(track) - self._logger.debug("add_track: " + self._record_to) + self._logger.debug("Add track: " + self._record_to) async def stop(self): """Stop RecordHandler.""" + duration = time.time() - self._start_time + self._logger.debug("Duration recording: " + str(duration)) await self._recorder.stop() self._logger.debug("Stop recording: " + self._record_to)