Skip to content

Commit fb073be

Browse files
committed
Doc strings
1 parent 629d405 commit fb073be

2 files changed

Lines changed: 55 additions & 20 deletions

File tree

livekit-rtc/livekit/rtc/data_track.py

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,50 @@
2424

2525

2626
class SubscribeDataTrackError(Exception):
27+
"""An error that can occur when subscribing to a data track."""
28+
2729
def __init__(self, message: str) -> None:
2830
self.message = message
2931

3032

3133
class PushFrameError(Exception):
32-
"""Frame could not be pushed to a data track."""
34+
"""Frame could not be pushed to a data track.
35+
36+
Pushing a frame can fail for several reasons:
37+
38+
- The track has been unpublished by the local participant or SFU
39+
- The room is no longer connected
40+
- Frames are being pushed too fast
41+
"""
3342

3443
def __init__(self, message: str) -> None:
3544
self.message = message
3645

3746

3847
@dataclass
3948
class DataTrackInfo:
40-
"""Metadata about a data track."""
49+
"""Information about a published data track."""
4150

4251
sid: str
52+
"""Unique track identifier assigned by the SFU.
53+
54+
This identifier may change if a reconnect occurs. Use ``name`` if a
55+
stable identifier is needed.
56+
"""
57+
4358
name: str
59+
"""Name of the track assigned by the publisher."""
60+
4461
uses_e2ee: bool
62+
"""Whether or not frames sent on the track use end-to-end encryption."""
4563

4664

4765
@dataclass
4866
class DataTrackOptions:
4967
"""Options for publishing a data track."""
5068

5169
name: str
52-
"""Track name used to identify the track to other participants.
70+
"""The track name is used to identify the track to other participants.
5371
5472
Must not be empty and must be unique per publisher.
5573
"""
@@ -60,24 +78,35 @@ class DataTrackFrame:
6078
"""A frame published on a data track, consisting of a payload and optional metadata."""
6179

6280
payload: bytes
81+
"""The frame's payload."""
82+
6383
user_timestamp: Optional[int] = None
84+
"""The frame's user timestamp, if one is associated."""
6485

6586
@staticmethod
6687
def _timestamp_now() -> int:
6788
return int(time.time() * 1000)
6889

6990
def with_user_timestamp_now(self) -> DataTrackFrame:
70-
"""Returns a copy of this frame with the user timestamp set to the current time."""
91+
"""Associates the current Unix timestamp (in milliseconds) with the frame.
92+
93+
Returns a new ``DataTrackFrame`` with the timestamp set; the original is
94+
not modified.
95+
"""
7196
return DataTrackFrame(
7297
payload=self.payload,
7398
user_timestamp=self._timestamp_now(),
7499
)
75100

76101
def duration_since_timestamp(self) -> Optional[float]:
77-
"""Calculates how long has passed since the frame's user timestamp.
102+
"""If the frame has a user timestamp, calculate how long has passed
103+
relative to the current system time.
104+
105+
The timestamp is assumed to be a Unix timestamp in milliseconds
106+
(as set by :meth:`with_user_timestamp_now` on the publisher side).
78107
79108
Returns:
80-
The elapsed duration in seconds, or None if no timestamp is set.
109+
The elapsed duration in seconds, or ``None`` if no timestamp is set.
81110
"""
82111
if self.user_timestamp is None:
83112
return None
@@ -86,7 +115,7 @@ def duration_since_timestamp(self) -> Optional[float]:
86115

87116

88117
class LocalDataTrack:
89-
"""A locally published data track that can push frames to subscribers."""
118+
"""Data track published by the local participant."""
90119

91120
def __init__(self, owned_info: proto_data_track.OwnedLocalDataTrack) -> None:
92121
self._info = DataTrackInfo(
@@ -98,16 +127,19 @@ def __init__(self, owned_info: proto_data_track.OwnedLocalDataTrack) -> None:
98127

99128
@property
100129
def info(self) -> DataTrackInfo:
130+
"""Information about the data track."""
101131
return self._info
102132

103133
def try_push(self, frame: DataTrackFrame) -> None:
104-
"""Push a frame to subscribers of this track.
134+
"""Try pushing a frame to subscribers of the track.
135+
136+
See :class:`DataTrackFrame` for how to construct a frame and attach metadata.
105137
106138
Args:
107139
frame: The data track frame to send.
108140
109141
Raises:
110-
PushFrameError: If the push fails (e.g. track not published).
142+
PushFrameError: If the push fails.
111143
"""
112144
proto_frame = proto_data_track.DataTrackFrame(payload=bytes(frame.payload))
113145
if frame.user_timestamp is not None:
@@ -122,15 +154,15 @@ def try_push(self, frame: DataTrackFrame) -> None:
122154
raise PushFrameError(resp.local_data_track_try_push.error)
123155

124156
def is_published(self) -> bool:
125-
"""Check whether this track is still published."""
157+
"""Whether or not the track is still published."""
126158
req = proto_ffi.FfiRequest()
127159
req.local_data_track_is_published.track_handle = self._ffi_handle.handle
128160

129161
resp = FfiClient.instance.request(req)
130162
return resp.local_data_track_is_published.is_published
131163

132164
def unpublish(self) -> None:
133-
"""Unpublish this track."""
165+
"""Unpublishes the track."""
134166
req = proto_ffi.FfiRequest()
135167
req.local_data_track_unpublish.track_handle = self._ffi_handle.handle
136168
FfiClient.instance.request(req)
@@ -140,7 +172,7 @@ def __repr__(self) -> str:
140172

141173

142174
class RemoteDataTrack:
143-
"""A data track published by a remote participant."""
175+
"""Data track published by a remote participant."""
144176

145177
def __init__(self, owned_info: proto_data_track.OwnedRemoteDataTrack) -> None:
146178
self._info = DataTrackInfo(
@@ -153,17 +185,19 @@ def __init__(self, owned_info: proto_data_track.OwnedRemoteDataTrack) -> None:
153185

154186
@property
155187
def info(self) -> DataTrackInfo:
188+
"""Information about the data track."""
156189
return self._info
157190

158191
@property
159192
def publisher_identity(self) -> str:
193+
"""Identity of the participant who published the track."""
160194
return self._publisher_identity
161195

162196
async def subscribe(self) -> DataTrackSubscription:
163-
"""Subscribe to this data track and receive frames.
197+
"""Subscribes to the data track to receive frames.
164198
165-
Returns:
166-
A DataTrackSubscription that can be used as an async iterator.
199+
Returns a :class:`DataTrackSubscription` that yields
200+
:class:`DataTrackFrame` instances as they arrive.
167201
168202
Raises:
169203
SubscribeDataTrackError: If subscription fails.
@@ -190,7 +224,7 @@ async def subscribe(self) -> DataTrackSubscription:
190224
return DataTrackSubscription(cb.subscribe_data_track.subscription)
191225

192226
def is_published(self) -> bool:
193-
"""Check whether this remote track is still published."""
227+
"""Whether or not the track is still published."""
194228
req = proto_ffi.FfiRequest()
195229
req.remote_data_track_is_published.track_handle = self._ffi_handle.handle
196230

livekit-rtc/livekit/rtc/participant.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,15 @@ async def publish_data_track(
678678
self,
679679
options: Union[str, DataTrackOptions],
680680
) -> LocalDataTrack:
681-
"""Publish a data track to the room.
681+
"""Publishes a data track.
682682
683683
Args:
684-
options: Either a track name string or a DataTrackOptions object.
685-
When a string is provided, it is used as the track name.
684+
options: Either a track name string or a :class:`DataTrackOptions`
685+
object. When a string is provided, it is used as the track name.
686686
687687
Returns:
688-
LocalDataTrack: The published data track.
688+
The published data track. Use :meth:`LocalDataTrack.try_push` to
689+
send data frames on the track.
689690
690691
Raises:
691692
PublishDataTrackError: If there is an error publishing the data track.

0 commit comments

Comments
 (0)