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};
19use bytes::Bytes;
20use from_variants::FromVariants;
21use std::sync::Arc;
22use tokio::sync::oneshot;
23
24/// An external event handled by [`Manager`](super::manager::Manager).
25#[derive(Debug, FromVariants)]
26pub enum InputEvent {
27    PublishRequest(PublishRequest),
28    PublishCancelled(PublishCancelled),
29    QueryPublished(QueryPublished),
30    UnpublishRequest(UnpublishRequest),
31    SfuPublishResponse(SfuPublishResponse),
32    SfuUnpublishResponse(SfuUnpublishResponse),
33    /// Republish all tracks.
34    ///
35    /// This must be sent after a full reconnect in order for existing publications
36    /// to be recognized by the SFU. Each republished track will be assigned a new SID.
37    ///
38    RepublishTracks,
39    /// Shutdown the manager and all associated tracks.
40    Shutdown,
41}
42
43/// An event produced by [`Manager`](super::manager::Manager) requiring external action.
44#[derive(Debug, FromVariants)]
45pub enum OutputEvent {
46    SfuPublishRequest(SfuPublishRequest),
47    SfuUnpublishRequest(SfuUnpublishRequest),
48    /// Serialized packets are ready to be sent over the transport.
49    PacketsAvailable(Vec<Bytes>),
50}
51
52// MARK: - Input events
53
54/// Client requested to publish a track.
55///
56/// Send using [`ManagerInput::publish_track`] and await the result.
57///
58/// [`ManagerInput::publish_track`]: super::manager::ManagerInput::publish_track
59///
60#[derive(Debug)]
61pub struct PublishRequest {
62    /// Publish options.
63    pub(super) options: DataTrackOptions,
64    /// Async completion channel.
65    pub(super) result_tx: oneshot::Sender<Result<LocalDataTrack, PublishError>>,
66}
67
68/// Client request to publish a track has been cancelled (internal).
69#[derive(Debug)]
70pub struct PublishCancelled {
71    /// Publisher handle of the pending publication.
72    pub(super) handle: Handle,
73}
74
75/// Client request to unpublish a track (internal).
76#[derive(Debug)]
77pub struct UnpublishRequest {
78    /// Publisher handle of the track to unpublish.
79    pub(super) handle: Handle,
80}
81
82/// Get information about all currently published tracks.
83///
84/// Send using [`ManagerInput::query_tracks`] and await the result. This is used
85/// to support sync state.
86///
87/// [`ManagerInput::query_tracks`]: super::manager::ManagerInput::query_tracks
88///
89#[derive(Debug)]
90pub struct QueryPublished {
91    pub(super) result_tx: oneshot::Sender<Vec<Arc<DataTrackInfo>>>,
92}
93
94/// SFU responded to a request to publish a data track.
95///
96/// Protocol equivalent: [`livekit_protocol::PublishDataTrackResponse`].
97///
98#[derive(Debug)]
99pub struct SfuPublishResponse {
100    /// Publisher handle of the track.
101    pub handle: Handle,
102    /// Outcome of the publish request.
103    pub result: Result<DataTrackInfo, PublishError>,
104}
105
106/// SFU notification that a track has been unpublished.
107///
108/// Protocol equivalent: [`livekit_protocol::UnpublishDataTrackResponse`].
109///
110#[derive(Debug)]
111pub struct SfuUnpublishResponse {
112    /// Publisher handle of the track that was unpublished.
113    pub handle: Handle,
114}
115
116// MARK: - Output events
117
118/// Request sent to the SFU to publish a track.
119///
120/// Protocol equivalent: [`livekit_protocol::PublishDataTrackRequest`].
121///
122#[derive(Debug)]
123pub struct SfuPublishRequest {
124    pub handle: Handle,
125    pub name: String,
126    pub uses_e2ee: bool,
127}
128
129/// Request sent to the SFU to unpublish a track.
130///
131/// Protocol equivalent: [`livekit_protocol::UnpublishDataTrackRequest`].
132///
133#[derive(Debug)]
134pub struct SfuUnpublishRequest {
135    /// Publisher handle of the track to unpublish.
136    pub handle: Handle,
137}