remotivelabs.broker.recording_session
There is a RecordingSessionClient for managing recording session playback on the broker.
import asyncio
from remotivelabs.broker.recording_session import RecordingSessionClient
async def main() -> None:
# Create a recording session client
async with RecordingSessionClient(url="http://127.0.0.1:50051") as rs_client:
# List available recording files on the broker
files = await rs_client.list_recording_files()
print(f"Available recording files: {files}")
# open a recording session with the first file
rs_ref = rs_client.get_session(str(files[0]))
async with rs_ref as session:
# start playback
status = await session.play()
print(f"Playback started with status: {status}")
# exiting the async with block will close the session, but playback will continue if other sessions are open
if __name__ == "__main__":
asyncio.run(main())
1""" 2There is a `RecordingSessionClient` for managing recording session playback on the broker. 3 4```python 5.. include:: ../_docs/snippets/recording_session.py 6``` 7""" 8 9from remotivelabs.broker.recording_session.client import RecordingSession, RecordingSessionClient 10from remotivelabs.broker.recording_session.file import File 11from remotivelabs.broker.recording_session.repeat import PlaybackRepeat 12from remotivelabs.broker.recording_session.status import PlaybackMode, RecordingSessionPlaybackError, RecordingSessionPlaybackStatus 13 14__all__ = [ 15 "File", 16 "PlaybackRepeat", 17 "PlaybackMode", 18 "RecordingSession", 19 "RecordingSessionClient", 20 "RecordingSessionPlaybackError", 21 "RecordingSessionPlaybackStatus", 22]
31@dataclass 32class File: 33 """ 34 Represents a file or folder on the broker's file system. 35 """ 36 37 path: str 38 type: FileType 39 created_time: datetime 40 modified_time: datetime 41 size: int
Represents a file or folder on the broker's file system.
5@dataclass 6class PlaybackRepeat: 7 """Playback repeat settings.""" 8 9 start_offset: int = 0 10 """Current cycle start in micro seconds.""" 11 end_offset: int = 0 12 """Current cycle end in micro seconds."""
Playback repeat settings.
26class PlaybackMode(Enum): 27 """Playback modes for a recording session.""" 28 29 PLAYBACK_PLAYING = 0 30 """Playing a file.""" 31 PLAYBACK_PAUSED = 1 32 """Playback is paused.""" 33 PLAYBACK_CLOSED = 2 34 """Playback is closed."""
Playback modes for a recording session.
83class RecordingSession: 84 """ 85 Represents a recording session playback on the broker. 86 """ 87 88 def __init__(self, client: RecordingSessionClient, path: str, force_reopen: bool = False): 89 self._client = client 90 self.path = path 91 self._force_reopen = force_reopen 92 93 async def __aenter__(self) -> RecordingSession: 94 await self.open(force_reopen=self._force_reopen) 95 return self 96 97 async def __aexit__(self, exc_type: type | None, exc_val: BaseException | None, exc_tb: object | None) -> None: 98 await self.close() 99 100 async def open(self, force_reopen: bool = False) -> RecordingSessionPlaybackStatus: 101 """ 102 Open recording session for playback. 103 If force_reopen is True, close any existing session before opening. 104 105 Args: 106 force_reopen: Whether to force close any existing session before opening. 107 108 Returns: 109 The playback status after opening the session. 110 111 Raises: 112 RecordingSessionPlaybackError: On failures to open the session. 113 """ 114 grpc_command = open_to_grpc(self.path, force_reopen) 115 try: 116 res = await self._client._recording_session_service.PlaybackOpen(grpc_command) 117 return status_from_grpc(res) 118 except AioRpcError as e: 119 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred") 120 121 async def play(self, offset: int | None = None) -> RecordingSessionPlaybackStatus: 122 """ 123 Start or continue playback of an open recording session. 124 125 Args: 126 offset: The offset in microseconds from which to start playback. If None, playback continues from the current position. 127 128 Returns: 129 The playback status after starting playback. 130 131 Raises: 132 RecordingSessionPlaybackError: On playback errors 133 """ 134 grpc_command = play_to_grpc(self.path, offset) 135 try: 136 res = await self._client._recording_session_service.PlaybackPlay(grpc_command) 137 return status_from_grpc(res) 138 except AioRpcError as e: 139 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred") 140 141 async def pause(self, offset: int | None = None) -> RecordingSessionPlaybackStatus: 142 """ 143 Pause playback of an open recording session. 144 145 Args: 146 offset: Target position in microseconds at where the playback cursor is put after pausing. 147 148 Returns: 149 The playback status after pausing playback. 150 151 Raises: 152 RecordingSessionPlaybackError: On playback errors 153 """ 154 grpc_command = pause_to_grpc(self.path, offset) 155 try: 156 res = await self._client._recording_session_service.PlaybackPause(grpc_command) 157 return status_from_grpc(res) 158 except AioRpcError as e: 159 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred") 160 161 async def seek(self, offset: int) -> RecordingSessionPlaybackStatus: 162 """ 163 Move playback to a target offset within the currently open recording session. 164 165 Args: 166 offset: Target position in microseconds. 167 168 Returns: 169 The playback status after seeking. 170 171 Raises: 172 RecordingSessionPlaybackError: On playback errors 173 """ 174 grpc_command = seek_to_grpc(self.path, offset) 175 try: 176 res = await self._client._recording_session_service.PlaybackSeek(grpc_command) 177 return status_from_grpc(res) 178 except AioRpcError as e: 179 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred") 180 181 async def close(self) -> RecordingSessionPlaybackStatus: 182 """ 183 Close an open recording session. 184 """ 185 grpc_command = close_to_grpc(self.path) 186 try: 187 res = await self._client._recording_session_service.PlaybackClose(grpc_command) 188 return status_from_grpc(res) 189 except AioRpcError as e: 190 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred") 191 192 async def set_repeat(self, start_offset: int | None = None, end_offset: int | None = None) -> RecordingSessionPlaybackStatus: 193 """ 194 Set repeat mode for the currently open recording session. 195 196 Args: 197 start_offset: Start offset in microseconds for the repeat segment. If None, repeat from the start of the recording. 198 end_offset: End offset in microseconds for the repeat segment. If None, repeat until the end of the recording. 199 200 Note: 201 If both `start_offset` and `end_offset` are None, repeat mode will be removed. 202 203 Returns: 204 The playback status after setting repeat mode. 205 """ 206 grpc_command = set_repeat_to_grpc(self.path, start_offset, end_offset) 207 try: 208 res = await self._client._recording_session_service.PlaybackRepeat(grpc_command) 209 return status_from_grpc(res) 210 except AioRpcError as e: 211 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred")
Represents a recording session playback on the broker.
100 async def open(self, force_reopen: bool = False) -> RecordingSessionPlaybackStatus: 101 """ 102 Open recording session for playback. 103 If force_reopen is True, close any existing session before opening. 104 105 Args: 106 force_reopen: Whether to force close any existing session before opening. 107 108 Returns: 109 The playback status after opening the session. 110 111 Raises: 112 RecordingSessionPlaybackError: On failures to open the session. 113 """ 114 grpc_command = open_to_grpc(self.path, force_reopen) 115 try: 116 res = await self._client._recording_session_service.PlaybackOpen(grpc_command) 117 return status_from_grpc(res) 118 except AioRpcError as e: 119 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred")
Open recording session for playback. If force_reopen is True, close any existing session before opening.
Arguments:
- force_reopen: Whether to force close any existing session before opening.
Returns:
The playback status after opening the session.
Raises:
- RecordingSessionPlaybackError: On failures to open the session.
121 async def play(self, offset: int | None = None) -> RecordingSessionPlaybackStatus: 122 """ 123 Start or continue playback of an open recording session. 124 125 Args: 126 offset: The offset in microseconds from which to start playback. If None, playback continues from the current position. 127 128 Returns: 129 The playback status after starting playback. 130 131 Raises: 132 RecordingSessionPlaybackError: On playback errors 133 """ 134 grpc_command = play_to_grpc(self.path, offset) 135 try: 136 res = await self._client._recording_session_service.PlaybackPlay(grpc_command) 137 return status_from_grpc(res) 138 except AioRpcError as e: 139 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred")
Start or continue playback of an open recording session.
Arguments:
- offset: The offset in microseconds from which to start playback. If None, playback continues from the current position.
Returns:
The playback status after starting playback.
Raises:
- RecordingSessionPlaybackError: On playback errors
141 async def pause(self, offset: int | None = None) -> RecordingSessionPlaybackStatus: 142 """ 143 Pause playback of an open recording session. 144 145 Args: 146 offset: Target position in microseconds at where the playback cursor is put after pausing. 147 148 Returns: 149 The playback status after pausing playback. 150 151 Raises: 152 RecordingSessionPlaybackError: On playback errors 153 """ 154 grpc_command = pause_to_grpc(self.path, offset) 155 try: 156 res = await self._client._recording_session_service.PlaybackPause(grpc_command) 157 return status_from_grpc(res) 158 except AioRpcError as e: 159 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred")
Pause playback of an open recording session.
Arguments:
- offset: Target position in microseconds at where the playback cursor is put after pausing.
Returns:
The playback status after pausing playback.
Raises:
- RecordingSessionPlaybackError: On playback errors
161 async def seek(self, offset: int) -> RecordingSessionPlaybackStatus: 162 """ 163 Move playback to a target offset within the currently open recording session. 164 165 Args: 166 offset: Target position in microseconds. 167 168 Returns: 169 The playback status after seeking. 170 171 Raises: 172 RecordingSessionPlaybackError: On playback errors 173 """ 174 grpc_command = seek_to_grpc(self.path, offset) 175 try: 176 res = await self._client._recording_session_service.PlaybackSeek(grpc_command) 177 return status_from_grpc(res) 178 except AioRpcError as e: 179 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred")
Move playback to a target offset within the currently open recording session.
Arguments:
- offset: Target position in microseconds.
Returns:
The playback status after seeking.
Raises:
- RecordingSessionPlaybackError: On playback errors
181 async def close(self) -> RecordingSessionPlaybackStatus: 182 """ 183 Close an open recording session. 184 """ 185 grpc_command = close_to_grpc(self.path) 186 try: 187 res = await self._client._recording_session_service.PlaybackClose(grpc_command) 188 return status_from_grpc(res) 189 except AioRpcError as e: 190 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred")
Close an open recording session.
192 async def set_repeat(self, start_offset: int | None = None, end_offset: int | None = None) -> RecordingSessionPlaybackStatus: 193 """ 194 Set repeat mode for the currently open recording session. 195 196 Args: 197 start_offset: Start offset in microseconds for the repeat segment. If None, repeat from the start of the recording. 198 end_offset: End offset in microseconds for the repeat segment. If None, repeat until the end of the recording. 199 200 Note: 201 If both `start_offset` and `end_offset` are None, repeat mode will be removed. 202 203 Returns: 204 The playback status after setting repeat mode. 205 """ 206 grpc_command = set_repeat_to_grpc(self.path, start_offset, end_offset) 207 try: 208 res = await self._client._recording_session_service.PlaybackRepeat(grpc_command) 209 return status_from_grpc(res) 210 except AioRpcError as e: 211 raise RecordingSessionPlaybackError(e.details() or "Unknown error occurred")
Set repeat mode for the currently open recording session.
Arguments:
- start_offset: Start offset in microseconds for the repeat segment. If None, repeat from the start of the recording.
- end_offset: End offset in microseconds for the repeat segment. If None, repeat until the end of the recording.
Note:
If both
start_offsetandend_offsetare None, repeat mode will be removed.
Returns:
The playback status after setting repeat mode.
25class RecordingSessionClient(BrokerClient): 26 """ 27 Client for managing recording session playback on the broker. 28 """ 29 30 # TODO: We probably dont want to inherit from BrokerClient, but rather use composition to hide functionality not relevant for recording 31 # session operations. However, this will do for now. 32 33 async def __aenter__(self) -> RecordingSessionClient: 34 await super().__aenter__() 35 return self 36 37 async def __aexit__(self, exc_type, exc_value, traceback) -> None: 38 await super().__aexit__(exc_type, exc_value, traceback) 39 40 async def list_recording_files(self, path: str | None = None) -> list[File]: 41 """ 42 List recording files in a directory. 43 44 Args: 45 path: Optional path to the subdirectory containing the recording files. 46 47 Returns: 48 A list of File objects representing the recording files. 49 """ 50 res = await self._recording_session_service.ListRecordingFiles(recordingsession__api__pb2.FileListingRequest(path=path)) 51 return [file_from_grpc(file) for file in res.files] 52 53 def playback_status(self) -> AsyncIterator[list[RecordingSessionPlaybackStatus]]: 54 """ 55 Get continuous status of all open recording sessions. 56 57 Returns: 58 An async iterator yielding lists of `RecordingSessionPlaybackStatus` objects. 59 """ 60 stream = self._recording_session_service.PlaybackStatus(common_pb2.Empty()) 61 62 async def async_generator() -> AsyncIterator[list[RecordingSessionPlaybackStatus]]: 63 async for statuses in stream: 64 status_list: list[recordingsession__api__pb2.RecordingSessionPlaybackStatus] = statuses.items 65 yield [status_from_grpc(status) for status in status_list] 66 67 return async_generator() 68 69 def get_session(self, path: str, force_reopen: bool = False) -> RecordingSession: 70 """ 71 Return a RecordingSession for the given path. 72 73 Args: 74 path: The path to the recording session file. 75 force_reopen: Whether to force close any existing session before opening. 76 77 Returns: 78 A `RecordingSession` object. 79 """ 80 return RecordingSession(self, path, force_reopen=force_reopen)
Client for managing recording session playback on the broker.
40 async def list_recording_files(self, path: str | None = None) -> list[File]: 41 """ 42 List recording files in a directory. 43 44 Args: 45 path: Optional path to the subdirectory containing the recording files. 46 47 Returns: 48 A list of File objects representing the recording files. 49 """ 50 res = await self._recording_session_service.ListRecordingFiles(recordingsession__api__pb2.FileListingRequest(path=path)) 51 return [file_from_grpc(file) for file in res.files]
List recording files in a directory.
Arguments:
- path: Optional path to the subdirectory containing the recording files.
Returns:
A list of File objects representing the recording files.
53 def playback_status(self) -> AsyncIterator[list[RecordingSessionPlaybackStatus]]: 54 """ 55 Get continuous status of all open recording sessions. 56 57 Returns: 58 An async iterator yielding lists of `RecordingSessionPlaybackStatus` objects. 59 """ 60 stream = self._recording_session_service.PlaybackStatus(common_pb2.Empty()) 61 62 async def async_generator() -> AsyncIterator[list[RecordingSessionPlaybackStatus]]: 63 async for statuses in stream: 64 status_list: list[recordingsession__api__pb2.RecordingSessionPlaybackStatus] = statuses.items 65 yield [status_from_grpc(status) for status in status_list] 66 67 return async_generator()
Get continuous status of all open recording sessions.
Returns:
An async iterator yielding lists of
RecordingSessionPlaybackStatusobjects.
69 def get_session(self, path: str, force_reopen: bool = False) -> RecordingSession: 70 """ 71 Return a RecordingSession for the given path. 72 73 Args: 74 path: The path to the recording session file. 75 force_reopen: Whether to force close any existing session before opening. 76 77 Returns: 78 A `RecordingSession` object. 79 """ 80 return RecordingSession(self, path, force_reopen=force_reopen)
Return a RecordingSession for the given path.
Arguments:
- path: The path to the recording session file.
- force_reopen: Whether to force close any existing session before opening.
Returns:
A
RecordingSessionobject.
22class RecordingSessionPlaybackError(Exception): 23 """Exception raised for errors during recording session playback."""
Exception raised for errors during recording session playback.
10@dataclass 11class RecordingSessionPlaybackStatus: 12 """ 13 Represents the playback status of a recording session. 14 """ 15 16 path: str 17 mode: PlaybackMode 18 offset: int 19 repeat: PlaybackRepeat | None = None
Represents the playback status of a recording session.