| 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::{rtc_error as sys_err, rtp_transceiver as sys_rt, webrtc as sys_webrtc}; |
| 17 | |
| 18 | use crate::{ |
| 19 | imp::{rtp_receiver::RtpReceiver, rtp_sender::RtpSender}, |
| 20 | rtp_parameters::RtpCodecCapability, |
| 21 | rtp_receiver, rtp_sender, |
| 22 | rtp_transceiver::{RtpTransceiverDirection, RtpTransceiverInit}, |
| 23 | RtcError, |
| 24 | }; |
| 25 | |
| 26 | impl From<sys_webrtc::ffi::RtpTransceiverDirection> for RtpTransceiverDirection { |
| 27 | fn from(value: sys_webrtc::ffi::RtpTransceiverDirection) -> Self { |
| 28 | match value { |
| 29 | sys_webrtc::ffi::RtpTransceiverDirection::SendRecv => Self::SendRecv, |
| 30 | sys_webrtc::ffi::RtpTransceiverDirection::SendOnly => Self::SendOnly, |
| 31 | sys_webrtc::ffi::RtpTransceiverDirection::RecvOnly => Self::RecvOnly, |
| 32 | sys_webrtc::ffi::RtpTransceiverDirection::Inactive => Self::Inactive, |
| 33 | sys_webrtc::ffi::RtpTransceiverDirection::Stopped => Self::Stopped, |
| 34 | _ => panic!("unknown RtpTransceiverDirection"), |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl From<RtpTransceiverDirection> for sys_webrtc::ffi::RtpTransceiverDirection { |
| 40 | fn from(value: RtpTransceiverDirection) -> Self { |
| 41 | match value { |
| 42 | RtpTransceiverDirection::SendRecv => Self::SendRecv, |
| 43 | RtpTransceiverDirection::SendOnly => Self::SendOnly, |
| 44 | RtpTransceiverDirection::RecvOnly => Self::RecvOnly, |
| 45 | RtpTransceiverDirection::Inactive => Self::Inactive, |
| 46 | RtpTransceiverDirection::Stopped => Self::Stopped, |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl From<RtpTransceiverInit> for sys_rt::ffi::RtpTransceiverInit { |
| 52 | fn from(value: RtpTransceiverInit) -> Self { |
| 53 | Self { |
| 54 | direction: value.direction.into(), |
| 55 | stream_ids: value.stream_ids, |
| 56 | send_encodings: value.send_encodings.into_iter().map(Into::into).collect(), |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[derive(Clone)] |
| 62 | pub struct RtpTransceiver { |
| 63 | pub(crate) sys_handle: SharedPtr<sys_rt::ffi::RtpTransceiver>, |
| 64 | } |
| 65 | |
| 66 | impl RtpTransceiver { |
| 67 | pub fn mid(&self) -> Option<String> { |
| 68 | // The C++ shim returns "" when libwebrtc has no mid yet (instead of |
| 69 | // calling std::optional::value() which would abort under -fno-exceptions). |
| 70 | self.sys_handle.mid().ok().filter(|s| !s.is_empty()) |
| 71 | } |
| 72 | |
| 73 | pub fn current_direction(&self) -> Option<RtpTransceiverDirection> { |
| 74 | self.sys_handle.current_direction().ok().map(Into::into) |
| 75 | } |
| 76 | |
| 77 | pub fn direction(&self) -> RtpTransceiverDirection { |
| 78 | self.sys_handle.direction().into() |
| 79 | } |
| 80 | |
| 81 | pub fn sender(&self) -> rtp_sender::RtpSender { |
| 82 | rtp_sender::RtpSender { handle: RtpSender { sys_handle: self.sys_handle.sender() } } |
| 83 | } |
| 84 | |
| 85 | pub fn receiver(&self) -> rtp_receiver::RtpReceiver { |
| 86 | rtp_receiver::RtpReceiver { handle: RtpReceiver { sys_handle: self.sys_handle.receiver() } } |
| 87 | } |
| 88 | |
| 89 | pub fn set_codec_preferences(&self, codecs: Vec<RtpCodecCapability>) -> Result<(), RtcError> { |
| 90 | self.sys_handle |
| 91 | .set_codec_preferences(codecs.into_iter().map(Into::into).collect()) |
| 92 | .map_err(|e| unsafe { sys_err::ffi::RtcError::from(e.what()).into() }) |
| 93 | } |
| 94 | |
| 95 | pub fn stop(&self) -> Result<(), RtcError> { |
| 96 | self.sys_handle |
| 97 | .stop_standard() |
| 98 | .map_err(|e| unsafe { sys_err::ffi::RtcError::from(e.what()).into() }) |
| 99 | } |
| 100 | } |