| 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::UniquePtr; |
| 16 | use webrtc_sys::jsep as sys_jsep; |
| 17 | |
| 18 | use crate::session_description::{self, SdpParseError, SdpType}; |
| 19 | |
| 20 | impl From<sys_jsep::ffi::SdpType> for SdpType { |
| 21 | fn from(sdp_type: sys_jsep::ffi::SdpType) -> Self { |
| 22 | match sdp_type { |
| 23 | sys_jsep::ffi::SdpType::Offer => SdpType::Offer, |
| 24 | sys_jsep::ffi::SdpType::PrAnswer => SdpType::PrAnswer, |
| 25 | sys_jsep::ffi::SdpType::Answer => SdpType::Answer, |
| 26 | sys_jsep::ffi::SdpType::Rollback => SdpType::Rollback, |
| 27 | _ => panic!("unknown SdpType"), |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl From<SdpType> for sys_jsep::ffi::SdpType { |
| 33 | fn from(sdp_type: SdpType) -> Self { |
| 34 | match sdp_type { |
| 35 | SdpType::Offer => sys_jsep::ffi::SdpType::Offer, |
| 36 | SdpType::PrAnswer => sys_jsep::ffi::SdpType::PrAnswer, |
| 37 | SdpType::Answer => sys_jsep::ffi::SdpType::Answer, |
| 38 | SdpType::Rollback => sys_jsep::ffi::SdpType::Rollback, |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl From<sys_jsep::ffi::SdpParseError> for SdpParseError { |
| 44 | fn from(e: sys_jsep::ffi::SdpParseError) -> Self { |
| 45 | Self { line: e.line, description: e.description } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub struct SessionDescription { |
| 50 | pub(crate) sys_handle: UniquePtr<sys_jsep::ffi::SessionDescription>, |
| 51 | } |
| 52 | |
| 53 | impl SessionDescription { |
| 54 | pub fn parse( |
| 55 | sdp: &str, |
| 56 | sdp_type: SdpType, |
| 57 | ) -> Result<session_description::SessionDescription, SdpParseError> { |
| 58 | let res = sys_jsep::ffi::create_session_description(sdp_type.into(), sdp.to_owned()); |
| 59 | match res { |
| 60 | Ok(sys_handle) => Ok(session_description::SessionDescription { |
| 61 | handle: SessionDescription { sys_handle }, |
| 62 | }), |
| 63 | Err(e) => Err(unsafe { sys_jsep::ffi::SdpParseError::from(e.what()).into() }), |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | pub fn sdp_type(&self) -> SdpType { |
| 68 | self.sys_handle.sdp_type().into() |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl ToString for SessionDescription { |
| 73 | fn to_string(&self) -> String { |
| 74 | self.sys_handle.stringify() |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl Clone for SessionDescription { |
| 79 | fn clone(&self) -> Self { |
| 80 | SessionDescription { sys_handle: self.sys_handle.clone() } |
| 81 | } |
| 82 | } |