Skip to content

Commit

Permalink
Add raw video recording
Browse files Browse the repository at this point in the history
  • Loading branch information
normaps committed Nov 8, 2023
1 parent fb39bf6 commit 79fc85e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 11 additions & 2 deletions backend/connection/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = []

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down
16 changes: 12 additions & 4 deletions backend/hub/record_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
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
_recorder: MediaRecorder | MediaBlackhole
_record: bool
_record_to: str
_track: TrackHandler
_start_time: float

def __init__(
self, track: TrackHandler, record: bool = False, record_to: str = None
Expand All @@ -40,20 +44,22 @@ def __init__(
self._track = track
self._record = record
self._record_to = record_to

if self._track.kind == "audio":
track_format = "mp3"
else:
track_format = "mp4"

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)

Expand All @@ -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)

0 comments on commit 79fc85e

Please sign in to comment.