| 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 | |
| 15 | use cxx::SharedPtr; |
| 16 | use webrtc_sys::{ |
| 17 | audio_track::ffi::media_to_audio, media_stream_track as sys_mst, |
| 18 | video_track::ffi::media_to_video, MEDIA_TYPE_AUDIO, MEDIA_TYPE_VIDEO, |
| 19 | }; |
| 20 | |
| 21 | use crate::{ |
| 22 | audio_track, |
| 23 | imp::{audio_track::RtcAudioTrack, video_track::RtcVideoTrack}, |
| 24 | media_stream_track::{MediaStreamTrack, RtcTrackState}, |
| 25 | video_track, |
| 26 | }; |
| 27 | |
| 28 | impl From<sys_mst::ffi::TrackState> for RtcTrackState { |
| 29 | fn from(state: sys_mst::ffi::TrackState) -> Self { |
| 30 | match state { |
| 31 | sys_mst::ffi::TrackState::Live => RtcTrackState::Live, |
| 32 | sys_mst::ffi::TrackState::Ended => RtcTrackState::Ended, |
| 33 | _ => panic!("unknown TrackState"), |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | pub fn new_media_stream_track( |
| 39 | sys_handle: SharedPtr<sys_mst::ffi::MediaStreamTrack>, |
| 40 | ) -> MediaStreamTrack { |
| 41 | if sys_handle.kind() == MEDIA_TYPE_AUDIO { |
| 42 | MediaStreamTrack::Audio(audio_track::RtcAudioTrack { |
| 43 | handle: RtcAudioTrack { sys_handle: unsafe { media_to_audio(sys_handle) } }, |
| 44 | }) |
| 45 | } else if sys_handle.kind() == MEDIA_TYPE_VIDEO { |
| 46 | MediaStreamTrack::Video(video_track::RtcVideoTrack { |
| 47 | handle: RtcVideoTrack::new(unsafe { media_to_video(sys_handle) }), |
| 48 | }) |
| 49 | } else { |
| 50 | panic!("unknown track kind") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | macro_rules! impl_media_stream_track { |
| 55 | ($cast:expr) => { |
| 56 | pub fn id(&self) -> String { |
| 57 | let ptr = $cast(self.sys_handle.clone()); |
| 58 | ptr.id() |
| 59 | } |
| 60 | |
| 61 | pub fn enabled(&self) -> bool { |
| 62 | let ptr = $cast(self.sys_handle.clone()); |
| 63 | ptr.enabled() |
| 64 | } |
| 65 | |
| 66 | pub fn set_enabled(&self, enabled: bool) -> bool { |
| 67 | let ptr = $cast(self.sys_handle.clone()); |
| 68 | ptr.set_enabled(enabled) |
| 69 | } |
| 70 | |
| 71 | pub fn state(&self) -> RtcTrackState { |
| 72 | let ptr = $cast(self.sys_handle.clone()); |
| 73 | ptr.state().into() |
| 74 | } |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | pub(super) use impl_media_stream_track; |