| 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 std::{ |
| 16 | fmt::{Debug, Display}, |
| 17 | str::FromStr, |
| 18 | }; |
| 19 | |
| 20 | use thiserror::Error; |
| 21 | |
| 22 | use crate::imp::session_description as sd_imp; |
| 23 | |
| 24 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 25 | pub enum SdpType { |
| 26 | Offer, |
| 27 | PrAnswer, |
| 28 | Answer, |
| 29 | Rollback, |
| 30 | } |
| 31 | |
| 32 | impl FromStr for SdpType { |
| 33 | type Err = &'static str; |
| 34 | |
| 35 | fn from_str(sdp_type: &str) -> Result<Self, Self::Err> { |
| 36 | match sdp_type { |
| 37 | "offer" => Ok(Self::Offer), |
| 38 | "pranswer" => Ok(Self::PrAnswer), |
| 39 | "answer" => Ok(Self::Answer), |
| 40 | "rollback" => Ok(Self::Rollback), |
| 41 | _ => Err("invalid SdpType"), |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl Display for SdpType { |
| 47 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 48 | let s = match self { |
| 49 | SdpType::Offer => "offer", |
| 50 | SdpType::PrAnswer => "pranswer", |
| 51 | SdpType::Answer => "answer", |
| 52 | SdpType::Rollback => "rollback", |
| 53 | }; |
| 54 | write!(f, "{}", s) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #[derive(Clone)] |
| 59 | pub struct SessionDescription { |
| 60 | pub(crate) handle: sd_imp::SessionDescription, |
| 61 | } |
| 62 | |
| 63 | #[derive(Clone, Error, Debug)] |
| 64 | #[error("Failed to parse sdp: {line} - {description}")] |
| 65 | pub struct SdpParseError { |
| 66 | pub line: String, |
| 67 | pub description: String, |
| 68 | } |
| 69 | |
| 70 | impl SessionDescription { |
| 71 | pub fn parse(sdp: &str, sdp_type: SdpType) -> Result<Self, SdpParseError> { |
| 72 | sd_imp::SessionDescription::parse(sdp, sdp_type) |
| 73 | } |
| 74 | |
| 75 | pub fn sdp_type(&self) -> SdpType { |
| 76 | self.handle.sdp_type() |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | impl ToString for SessionDescription { |
| 81 | fn to_string(&self) -> String { |
| 82 | self.handle.to_string() |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl Debug for SessionDescription { |
| 87 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 88 | f.debug_struct("SessionDescription").field("sdp_type", &self.sdp_type()).finish() |
| 89 | } |
| 90 | } |