Skip to main content

livekit_datatrack/local/
events.rs

1// Copyright 2025 LiveKit, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16    api::{DataTrackInfo, DataTrackOptions, LocalDataTrack, PublishError},
17    packet::Handle,
18    schema::{DataTrackFrameEncoding, DataTrackSchemaId},
19};
20use bytes::Bytes;
21use from_variants::FromVariants;
22use std::sync::Arc;
23use tokio::sync::oneshot;
24
25/// An external event handled by [`Manager`](super::manager::Manager).
26#[derive(Debug, FromVariants)]
27pub enum InputEvent {
28    PublishRequest(PublishRequest),
29    PublishCancelled(PublishCancelled),
30    QueryPublished(QueryPublished),
31    UnpublishRequest(UnpublishRequest),
32    SfuPublishResponse(SfuPublishResponse),
33    SfuUnpublishResponse(SfuUnpublishResponse),
34    /// Republish all tracks.
35    ///
36    /// This must be sent after a full reconnect in order for existing publications
37    /// to be recognized by the SFU. Each republished track will be assigned a new SID.
38    ///
39    RepublishTracks,
40    /// Shutdown the manager and all associated tracks.
41    Shutdown,
42}
43
44/// An event produced by [`Manager`](super::manager::Manager) requiring external action.
45#[derive(Debug, FromVariants)]
46pub enum OutputEvent {
47    SfuPublishRequest(SfuPublishRequest),
48    SfuUnpublishRequest(SfuUnpublishRequest),
49    /// Serialized packets are ready to be sent over the transport.
50    PacketsAvailable(Vec<Bytes>),
51}
52
53// MARK: - Input events
54
55/// Client requested to publish a track.
56///
57/// Send using [`ManagerInput::publish_track`] and await the result.
58///
59/// [`ManagerInput::publish_track`]: super::manager::ManagerInput::publish_track
60///
61#[derive(Debug)]
62pub struct PublishRequest {
63    /// Publish options.
64    pub(super) options: DataTrackOptions,
65    /// Async completion channel.
66    pub(super) result_tx: oneshot::Sender<Result<LocalDataTrack, PublishError>>,
67}
68
69/// Client request to publish a track has been cancelled (internal).
70#[derive(Debug)]
71pub struct PublishCancelled {
72    /// Publisher handle of the pending publication.
73    pub(super) handle: Handle,
74}
75
76/// Client request to unpublish a track (internal).
77#[derive(Debug)]
78pub struct UnpublishRequest {
79    /// Publisher handle of the track to unpublish.
80    pub(super) handle: Handle,
81}
82
83/// Get information about all currently published tracks.
84///
85/// Send using [`ManagerInput::query_tracks`] and await the result. This is used
86/// to support sync state.
87///
88/// [`ManagerInput::query_tracks`]: super::manager::ManagerInput::query_tracks
89///
90#[derive(Debug)]
91pub struct QueryPublished {
92    pub(super) result_tx: oneshot::Sender<Vec<Arc<DataTrackInfo>>>,
93}
94
95/// SFU responded to a request to publish a data track.
96///
97/// Protocol equivalent: [`livekit_protocol::PublishDataTrackResponse`].
98///
99#[derive(Debug)]
100pub struct SfuPublishResponse {
101    /// Publisher handle of the track.
102    pub handle: Handle,
103    /// Outcome of the publish request.
104    pub result: Result<DataTrackInfo, PublishError>,
105}
106
107/// SFU notification that a track has been unpublished.
108///
109/// Protocol equivalent: [`livekit_protocol::UnpublishDataTrackResponse`].
110///
111#[derive(Debug)]
112pub struct SfuUnpublishResponse {
113    /// Publisher handle of the track that was unpublished.
114    pub handle: Handle,
115}
116
117// MARK: - Output events
118
119/// Request sent to the SFU to publish a track.
120///
121/// Protocol equivalent: [`livekit_protocol::PublishDataTrackRequest`].
122///
123#[derive(Debug)]
124pub struct SfuPublishRequest {
125    pub handle: Handle,
126    pub name: String,
127    pub uses_e2ee: bool,
128    pub schema: Option<DataTrackSchemaId>,
129    pub frame_encoding: Option<DataTrackFrameEncoding>,
130}
131
132/// Request sent to the SFU to unpublish a track.
133///
134/// Protocol equivalent: [`livekit_protocol::UnpublishDataTrackRequest`].
135///
136#[derive(Debug)]
137pub struct SfuUnpublishRequest {
138    /// Publisher handle of the track to unpublish.
139    pub handle: Handle,
140}