use crate::l2cap_classic::ClassicL2capChannel;
use futures_core::Stream;
use webbluetooth_core::{AvdtpSignalingPacket, Result, SbcConfiguration, SbcMediaPacket};
pub struct A2dpSession {
channel: ClassicL2capChannel,
configuration: SbcConfiguration,
}
impl A2dpSession {
pub fn new(channel: ClassicL2capChannel, configuration: SbcConfiguration) -> Self {
Self {
channel,
configuration,
}
}
pub fn send(&self, packet: &SbcMediaPacket) -> Result<()> {
let bytes = packet
.encode()
.map_err(|error| webbluetooth_core::Error::InvalidModification(error.to_string()))?;
self.channel.send(&bytes)
}
pub fn parse(&self, bytes: &[u8]) -> Result<SbcMediaPacket> {
SbcMediaPacket::decode(bytes, &self.configuration)
.map_err(|error| webbluetooth_core::Error::InvalidModification(error.to_string()))
}
pub fn send_signaling(&self, packet: &AvdtpSignalingPacket) -> Result<()> {
let bytes = packet
.encode()
.map_err(|error| webbluetooth_core::Error::InvalidModification(error.to_string()))?;
self.channel.send(&bytes)
}
pub fn parse_signaling(&self, bytes: &[u8]) -> Result<AvdtpSignalingPacket> {
AvdtpSignalingPacket::decode(bytes)
.map_err(|error| webbluetooth_core::Error::InvalidModification(error.to_string()))
}
pub fn take_incoming(&self) -> Option<impl Stream<Item = Vec<u8>>> {
self.channel.take_incoming()
}
pub fn channel(&self) -> &ClassicL2capChannel {
&self.channel
}
pub fn close(&self) {
self.channel.close();
}
}