-
Notifications
You must be signed in to change notification settings - Fork 206
Data tracks core #862
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
Merged
Merged
Data tracks core #862
Changes from 6 commits
Commits
Show all changes
75 commits
Select commit
Hold shift + click to select a range
16d0294
Upgrade protocol to v1.44.0
ladvoc cb8f8a8
Integrate
ladvoc ca66c69
generated protobuf
github-actions[bot] 2ccb534
Data tracks core module
ladvoc fd1a92e
Merge remote-tracking branch 'origin/main' into ladvoc/data-tracks-core
ladvoc 7a90118
Use workspace dependencies
ladvoc afc10b4
Add to workspace
ladvoc c320670
Format
ladvoc ab8bb25
Add release-plz configuration
ladvoc d7b07a5
Used checked add
ladvoc 7fb9c8f
Fix naming typo
ladvoc e951f4a
Fix inverted logic
ladvoc 9452218
Fix potential integer overflow
ladvoc 1347840
Add crate as workspace dependency
ladvoc da85847
Fix typo
ladvoc 8239631
Apply Clippy suggestions
ladvoc 9e1b6c7
Detect duplicate packets
ladvoc b4862cf
Compare with >=
ladvoc 5ec6439
Calculate payload length during finalize
ladvoc 01c550a
Cast earlier in expression
ladvoc 8d65bbe
Handle edge case in packet reassembly
ladvoc c117c1c
Format
ladvoc b920d42
Refactor extension deserialization
ladvoc 301aedf
Use wrapping add in tests
ladvoc d2afd21
Merge remote-tracking branch 'origin/main' into ladvoc/data-tracks-core
ladvoc 8e4f0bc
generated protobuf
github-actions[bot] ca40c9f
Address comment
ladvoc 037fafc
Add packet specification
ladvoc cf1f9be
Format
ladvoc 1438639
Implement drop for manager input
ladvoc 2d7d219
Increment frame number after fallible ops
ladvoc ccdc012
Handle publisher side full reconnect
ladvoc ad94670
Merge remote-tracking branch 'origin/main' into ladvoc/data-tracks-core
ladvoc 15f3359
Update protobuf
ladvoc 1ce2601
generated protobuf
github-actions[bot] e2583ab
Add missing clone derive
ladvoc cc4f741
Rename error reason case
ladvoc ba3636b
Unpublish on unknown handle
ladvoc 3432333
Update protocol
ladvoc 586dc9d
Format
ladvoc 8529950
generated protobuf
github-actions[bot] 9923c14
Add missing await
ladvoc e5d9549
Drop guard for manager input
ladvoc fc7e146
Remove timeout
ladvoc 86924dd
Add doc comment
ladvoc 38eba78
Avoid hardcoded constants
ladvoc 0aa9412
Increase buffer counts
ladvoc 45531ef
Merge remote-tracking branch 'origin' into ladvoc/data-tracks-core
ladvoc dac3f02
Add invalid name error case
ladvoc ac7b3f8
Ignore extensions with unknown length
ladvoc bef4a10
Format
ladvoc 211313e
Merge remote-tracking branch 'origin/main' into ladvoc/data-tracks-core
ladvoc 20439ea
Add knope configuration
ladvoc 7efd0a8
Two byte TLV packet format
ladvoc 296bfbe
Merge remote-tracking branch 'origin/main' into ladvoc/data-tracks-core
ladvoc c513c70
Add changeset
ladvoc eb1d7d8
Remove comment
ladvoc 920544c
Use workspace props, add description
ladvoc 4337555
Use actual bytes for extension TLV encoding
ladvoc 0faf7c3
Skip extra bytes for future extension versions
ladvoc 5ba7822
User customizable frame buffer size for subscriptions
ladvoc 61f8f29
Format
ladvoc a7181f3
Add new method
ladvoc 41b25dd
Additional unit tests for track managers
ladvoc 37030fe
Fix publish/unpublish detection
ladvoc de06a2d
Concrete stream type for subscription
ladvoc 7fa0bc6
Consistent public type prefixing
ladvoc d5339be
Avoid intermediate collect
ladvoc f615772
Format
ladvoc fff3d9d
Rename event
ladvoc ab51dc5
Add track unpublished event
ladvoc 7b07b1d
Test track unpublished event
ladvoc bb358f3
Clamp invalid buffer size instead of panic
ladvoc 49ed111
Do not consume track on unpublish
ladvoc e81eb60
Merge remote-tracking branch 'origin/main' into ladvoc/data-tracks-core
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,25 @@ | ||
| [package] | ||
| name = "livekit-datatrack" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| license = "Apache-2.0" | ||
| repository = "https://github.com/livekit/rust-sdks" | ||
| readme = "README.md" | ||
|
|
||
| [dependencies] | ||
| livekit-protocol = { workspace = true } | ||
| livekit-runtime = { workspace = true } | ||
| log = { workspace = true } | ||
| thiserror = "2.0.17" # TODO: upgrade workspace version to 2.x.x | ||
|
ladvoc marked this conversation as resolved.
|
||
| tokio = { workspace = true, default-features = false, features = ["sync"] } | ||
| futures-util = { workspace = true, default-features = false, features = ["sink"] } | ||
| futures-core = { workspace = true } | ||
| bytes = { workspace = true } | ||
| from_variants = { workspace = true } | ||
| tokio-stream = { workspace = true, features = ["sync"] } | ||
| anyhow = { workspace = true } # For internal error handling only | ||
| rand = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| test-case = "3.3" | ||
| fake = { version = "4.4", features = ["derive"] } | ||
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,6 @@ | ||
| # LiveKit Data Track | ||
|
|
||
| **Important**: | ||
| This is an internal crate that powers the data tracks feature in LiveKit client SDKs (including [Rust](https://crates.io/crates/livekit) and others) and is not usable directly. | ||
|
|
||
| To use data tracks in your application, please use the public APIs provided by the client SDKs. |
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,57 @@ | ||
| // Copyright 2025 LiveKit, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use bytes::Bytes; | ||
| use core::fmt::Debug; | ||
| use thiserror::Error; | ||
|
|
||
| // TODO: If a core module for end-to-end encryption is created in the future | ||
| // (livekit-e2ee), these traits should be moved to there. | ||
|
|
||
| /// Encrypted payload and metadata required for decryption. | ||
| pub struct EncryptedPayload { | ||
| pub payload: Bytes, | ||
| pub iv: [u8; 12], | ||
| pub key_index: u8, | ||
| } | ||
|
|
||
| /// An error indicating a payload could not be encrypted. | ||
| #[derive(Debug, Error)] | ||
| #[error("Encryption failed")] | ||
| pub struct EncryptionError; | ||
|
|
||
| /// An error indicating a payload could not be decrypted. | ||
| #[derive(Debug, Error)] | ||
| #[error("Decryption failed")] | ||
| pub struct DecryptionError; | ||
|
|
||
| /// Provider for encrypting payloads for E2EE. | ||
| pub trait EncryptionProvider: Send + Sync + Debug { | ||
| /// Encrypts the given payload being sent by the local participant. | ||
| fn encrypt(&self, payload: Bytes) -> Result<EncryptedPayload, EncryptionError>; | ||
| } | ||
|
|
||
| /// Provider for decrypting payloads for E2EE. | ||
| pub trait DecryptionProvider: Send + Sync + Debug { | ||
| /// Decrypts the given payload received from a remote participant. | ||
| /// | ||
| /// Sender identity is required in order for the proper key to be used | ||
| /// for decryption. | ||
| /// | ||
| fn decrypt( | ||
| &self, | ||
| payload: EncryptedPayload, | ||
| sender_identity: &str, | ||
| ) -> Result<Bytes, DecryptionError>; | ||
| } |
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,24 @@ | ||
| // Copyright 2025 LiveKit, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use thiserror::Error; | ||
|
|
||
| /// Internal data track error. | ||
| /// | ||
| /// Occurrences of this error type are unexpected and likely indicate | ||
| /// a bug. If encountered, please report on GitHub and include the full error description. | ||
| /// | ||
| #[derive(Debug, Error)] | ||
| #[error(transparent)] | ||
| pub struct InternalError(#[from] anyhow::Error); |
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,122 @@ | ||
| // Copyright 2025 LiveKit, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use bytes::Bytes; | ||
| use core::fmt; | ||
| use std::time::{Duration, SystemTime, UNIX_EPOCH}; | ||
|
|
||
| /// A frame published on a data track, consisting of a payload and optional metadata. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// Create a frame from a [`Vec`] payload: | ||
| /// | ||
| /// ``` | ||
| /// # use livekit_datatrack::api::DataTrackFrame; | ||
| /// let some_payload = vec![0xFA; 256]; | ||
| /// let frame: DataTrackFrame = some_payload.into(); | ||
| /// | ||
| /// assert_eq!(frame.payload().len(), 256); | ||
| /// ``` | ||
| /// | ||
| #[derive(Clone, Default)] | ||
| pub struct DataTrackFrame { | ||
| pub(crate) payload: Bytes, | ||
| pub(crate) user_timestamp: Option<u64>, | ||
| } | ||
|
|
||
| impl DataTrackFrame { | ||
| /// Returns the frame's payload. | ||
| pub fn payload(&self) -> Bytes { | ||
| self.payload.clone() // Cheap clone | ||
| } | ||
|
|
||
| /// Returns the frame's user timestamp, if one is associated. | ||
| pub fn user_timestamp(&self) -> Option<u64> { | ||
| self.user_timestamp | ||
| } | ||
|
|
||
| /// If the frame has a user timestamp, calculate how long has passed | ||
| /// relative to the current system time. | ||
| /// | ||
| /// If a timestamp is present, it is assumed it is a UNIX timestamp in milliseconds | ||
| /// (as can be set with [`Self::with_user_timestamp_now`] on the publisher side). | ||
| /// If the timestamp is invalid or not present, the result is none. | ||
| /// | ||
| pub fn duration_since_timestamp(&self) -> Option<Duration> { | ||
| SystemTime::now() | ||
| .duration_since(UNIX_EPOCH + Duration::from_millis(self.user_timestamp?)) | ||
| .inspect_err(|err| log::error!("Failed to calculate duration: {err}")) | ||
| .ok() | ||
| } | ||
|
ladvoc marked this conversation as resolved.
|
||
| } | ||
|
|
||
| impl DataTrackFrame { | ||
| /// Creates a frame from the given payload. | ||
| pub fn new(payload: impl Into<Bytes>) -> Self { | ||
| Self { payload: payload.into(), ..Default::default() } | ||
| } | ||
|
|
||
| /// Associates a user timestamp with the frame. | ||
| pub fn with_user_timestamp(mut self, value: u64) -> Self { | ||
| self.user_timestamp = Some(value); | ||
| self | ||
| } | ||
|
|
||
| /// Associates the current Unix timestamp (in milliseconds) with the frame. | ||
| pub fn with_user_timestamp_now(mut self) -> Self { | ||
| let timestamp = SystemTime::now() | ||
| .duration_since(std::time::UNIX_EPOCH) | ||
| .map(|d| d.as_millis() as u64) | ||
| .inspect_err(|err| log::error!("Failed to get system time: {err}")) | ||
| .ok(); | ||
| self.user_timestamp = timestamp; | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Debug for DataTrackFrame { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("DataTrackFrame") | ||
| .field("payload_len", &self.payload.len()) | ||
| .field("user_timestamp", &self.user_timestamp) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| // MARK: - From implementations | ||
|
|
||
| impl From<Bytes> for DataTrackFrame { | ||
| fn from(bytes: Bytes) -> Self { | ||
| Self { payload: bytes, ..Default::default() } | ||
| } | ||
| } | ||
|
|
||
| impl From<&'static [u8]> for DataTrackFrame { | ||
| fn from(slice: &'static [u8]) -> Self { | ||
| Self { payload: slice.into(), ..Default::default() } | ||
| } | ||
| } | ||
|
|
||
| impl From<Vec<u8>> for DataTrackFrame { | ||
| fn from(vec: Vec<u8>) -> Self { | ||
| Self { payload: vec.into(), ..Default::default() } | ||
| } | ||
| } | ||
|
|
||
| impl From<Box<[u8]>> for DataTrackFrame { | ||
| fn from(slice: Box<[u8]>) -> Self { | ||
| Self { payload: slice.into(), ..Default::default() } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.