-
Notifications
You must be signed in to change notification settings - Fork 113
Data tracks support #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ladvoc
wants to merge
27
commits into
main
Choose a base branch
from
jacobgelman/bot-242-python-client-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Data tracks support #586
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c4e12dd
Pin submodule
ladvoc 61f4815
Update proto
ladvoc 159a198
Add initial examples
ladvoc 1c603d5
Initial FFI implementation
ladvoc 00c00ca
Hide proto type
ladvoc 0d4c5d2
Improve error reporting
ladvoc 671777d
More generic type signature for try_push
ladvoc 7bea8cb
Data class for frame
ladvoc 10e6561
Data class for track info
ladvoc 629d405
Frame convenience methods
ladvoc fb073be
Doc strings
ladvoc 3a0a171
generated protobuf
github-actions[bot] 7707e40
Refine example
ladvoc 869dcbb
Format
ladvoc 50a9a95
Pin RTC
ladvoc 19a592a
End-to-end test for data tracks
ladvoc 96ed624
Patch proto generation script
ladvoc b98deb3
generated protobuf
github-actions[bot] dcc4639
Pin submodule
ladvoc 17008fa
Generate proto
ladvoc f2adba9
Expose buffer size option
ladvoc 9e6784e
Rename event
ladvoc b54ac64
Expose unpublished event
ladvoc 38a5ebb
Pin submodule
ladvoc b75cb34
Generate proto
ladvoc 509fe32
Add explicit request to receive next frame for subscription
ladvoc 8d9c838
Format
ladvoc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import os | ||
| import logging | ||
| import asyncio | ||
| from signal import SIGINT, SIGTERM | ||
| from livekit import rtc | ||
|
|
||
| # Set the following environment variables with your own values | ||
| TOKEN = os.environ.get("LIVEKIT_TOKEN") | ||
| URL = os.environ.get("LIVEKIT_URL") | ||
|
|
||
|
|
||
| async def read_sensor() -> bytes: | ||
| # Dynamically read some sensor data... | ||
| return bytes([0xFA] * 256) | ||
|
|
||
|
|
||
| async def push_frames(track: rtc.LocalDataTrack): | ||
| while True: | ||
| logging.info("Pushing frame") | ||
| data = await read_sensor() | ||
| try: | ||
| frame = rtc.DataTrackFrame(payload=data).with_user_timestamp_now() | ||
| track.try_push(frame) | ||
| except rtc.PushFrameError as e: | ||
| logging.error("Failed to push frame: %s", e) | ||
| await asyncio.sleep(0.5) | ||
|
|
||
|
|
||
| async def main(room: rtc.Room): | ||
| logging.basicConfig(level=logging.INFO) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| await room.connect(URL, TOKEN) | ||
| logger.info("connected to room %s", room.name) | ||
|
|
||
| track = await room.local_participant.publish_data_track("my_sensor_data") | ||
| await push_frames(track) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| logging.basicConfig( | ||
| level=logging.INFO, | ||
| handlers=[ | ||
| logging.FileHandler("publisher.log"), | ||
| logging.StreamHandler(), | ||
| ], | ||
| ) | ||
|
|
||
| loop = asyncio.get_event_loop() | ||
| room = rtc.Room(loop=loop) | ||
|
|
||
| main_task = asyncio.ensure_future(main(room)) | ||
|
|
||
| async def cleanup(): | ||
| main_task.cancel() | ||
| try: | ||
| await main_task | ||
| except asyncio.CancelledError: | ||
| pass | ||
| await room.disconnect() | ||
| loop.stop() | ||
|
|
||
| for signal in [SIGINT, SIGTERM]: | ||
| loop.add_signal_handler(signal, lambda: asyncio.ensure_future(cleanup())) | ||
|
|
||
| try: | ||
| loop.run_forever() | ||
| finally: | ||
| loop.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import os | ||
| import logging | ||
| import asyncio | ||
| from signal import SIGINT, SIGTERM | ||
| from livekit import rtc | ||
|
|
||
| # Set the following environment variables with your own values | ||
| TOKEN = os.environ.get("LIVEKIT_TOKEN") | ||
| URL = os.environ.get("LIVEKIT_URL") | ||
|
|
||
|
|
||
| async def subscribe(track: rtc.RemoteDataTrack): | ||
| logging.info( | ||
| "Subscribing to '%s' published by '%s'", | ||
| track.info.name, | ||
| track.publisher_identity, | ||
| ) | ||
| subscription = await track.subscribe() | ||
| async for frame in subscription: | ||
| logging.info("Received frame (%d bytes)", len(frame.payload)) | ||
|
|
||
| latency = frame.duration_since_timestamp() | ||
| if latency is not None: | ||
| logging.info("Latency: %.3f s", latency) | ||
|
|
||
|
|
||
| async def main(room: rtc.Room): | ||
| logging.basicConfig(level=logging.INFO) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| active_tasks = [] | ||
|
|
||
| @room.on("data_track_published") | ||
| def on_data_track_published(track: rtc.RemoteDataTrack): | ||
| task = asyncio.create_task(subscribe(track)) | ||
| active_tasks.append(task) | ||
| task.add_done_callback(lambda _: active_tasks.remove(task)) | ||
|
|
||
| await room.connect(URL, TOKEN) | ||
| logger.info("connected to room %s", room.name) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| logging.basicConfig( | ||
| level=logging.INFO, | ||
| handlers=[ | ||
| logging.FileHandler("subscriber.log"), | ||
| logging.StreamHandler(), | ||
| ], | ||
| ) | ||
|
|
||
| loop = asyncio.get_event_loop() | ||
| room = rtc.Room(loop=loop) | ||
|
|
||
| async def cleanup(): | ||
| await room.disconnect() | ||
| loop.stop() | ||
|
|
||
| asyncio.ensure_future(main(room)) | ||
| for signal in [SIGINT, SIGTERM]: | ||
| loop.add_signal_handler(signal, lambda: asyncio.ensure_future(cleanup())) | ||
|
|
||
| try: | ||
| loop.run_forever() | ||
| finally: | ||
| loop.close() | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have the same API as AudioTrack/VideoTrack for subscription?
data_track_subscribedevent andset_subscribed