Skip to main content

gosuto_livekit/room/track/
audio_track.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 gosuto_libwebrtc::prelude::*;
16use livekit_protocol as proto;
17use livekit_protocol::enum_dispatch;
18
19use super::track_dispatch;
20use crate::prelude::*;
21
22#[derive(Clone, Debug)]
23pub enum AudioTrack {
24    Local(LocalAudioTrack),
25    Remote(RemoteAudioTrack),
26}
27
28impl AudioTrack {
29    track_dispatch!([Local, Remote]);
30
31    pub fn rtc_track(&self) -> RtcAudioTrack {
32        match self {
33            Self::Local(track) => track.rtc_track(),
34            Self::Remote(track) => track.rtc_track(),
35        }
36    }
37}
38
39impl From<AudioTrack> for Track {
40    fn from(track: AudioTrack) -> Self {
41        match track {
42            AudioTrack::Local(track) => Self::LocalAudio(track),
43            AudioTrack::Remote(track) => Self::RemoteAudio(track),
44        }
45    }
46}
47
48impl TryFrom<Track> for AudioTrack {
49    type Error = &'static str;
50
51    fn try_from(track: Track) -> Result<Self, Self::Error> {
52        match track {
53            Track::LocalAudio(track) => Ok(Self::Local(track)),
54            Track::RemoteAudio(track) => Ok(Self::Remote(track)),
55            _ => Err("not an audio track"),
56        }
57    }
58}