1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Wrapper around an [RTCRtpCodecCapability].
//!
//! [RTCRtpCodecCapability]: https://w3.org/TR/webrtc#dom-rtcrtpcodeccapability
use std::collections::HashMap;
use dart_sys::Dart_Handle;
use medea_macro::dart_bridge;
use super::utils::{
dart_future::FutureFromDart, dart_string_into_rust, list::DartList,
};
use crate::{
media::MediaKind,
platform::{
codec_capability::CodecCapabilityError as Error,
dart::utils::handle::DartHandle,
},
};
#[dart_bridge("flutter/lib/src/native/platform/codec_capability.g.dart")]
mod codec_capability {
use std::{ffi::c_char, ptr};
use dart_sys::Dart_Handle;
use crate::platform::Error;
extern "C" {
/// Returns [RTCRtpSender]'s available [RTCRtpCodecCapability][1]s.
///
/// [RTCRtpSender]: https://w3.org/TR/webrtc#dom-rtcrtpsender
/// [1]: https://w3.org/TR/webrtc#dom-rtcrtpcodeccapability
pub fn get_sender_codec_capabilities(
kind: i64,
) -> Result<Dart_Handle, Error>;
/// Returns [RTCRtpReceiver]'s available [RTCRtpCodecCapability][1]s.
///
/// [RTCRtpReceiver]: https://w3.org/TR/webrtc#dom-rtcrtpreceiver
/// [1]: https://w3.org/TR/webrtc#dom-rtcrtpcodeccapability
pub fn get_receiver_codec_capabilities(
kind: i64,
) -> Result<Dart_Handle, Error>;
/// Returns [mimeType][2] of the provided [RTCRtpCodec][1].
///
/// [1]: https://w3.org/TR/webrtc#dom-rtcrtpcodec
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodec-mimetype
pub fn mime_type(
codec_capability: Dart_Handle,
) -> Result<ptr::NonNull<c_char>, Error>;
/// Returns [clockRate][2] of the provided [RTCRtpCodec][1].
///
/// [1]: https://w3.org/TR/webrtc#dom-rtcrtpcodec
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodec-clockrate
pub fn clock_rate(codec_capability: Dart_Handle) -> Result<u32, Error>;
/// Returns [channels][2] of the provided [RTCRtpCodec][1].
///
/// [1]: https://w3.org/TR/webrtc#dom-rtcrtpcodec
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodec-channels
pub fn channels(codec_capability: Dart_Handle) -> Result<u32, Error>;
/// Returns [sdpFmtpLine][2] of the provided [RTCRtpCodec][1].
///
/// [1]: https://w3.org/TR/webrtc#dom-rtcrtpcodec
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodec-sdpfmtpline
pub fn parameters(
codec_capability: Dart_Handle,
) -> Result<ptr::NonNull<c_char>, Error>;
}
}
/// Dart side representation of an [RTCRtpCodecCapability].
///
/// [RTCRtpCodecCapability]: https://w3.org/TR/webrtc#dom-rtcrtpcodeccapability
#[derive(Clone, Debug)]
pub struct CodecCapability(DartHandle);
impl From<DartHandle> for CodecCapability {
fn from(value: DartHandle) -> Self {
Self(value)
}
}
impl CodecCapability {
/// Returns available [RTCRtpSender]'s [`CodecCapability`]s.
///
/// # Errors
///
/// With [`Error::FailedToGetCapabilities`] if fails to retrieve
/// capabilities.
///
/// [RTCRtpSender]: https://w3.org/TR/webrtc#dom-rtcrtpsender
pub async fn get_sender_codec_capabilities(
kind: MediaKind,
) -> Result<Vec<Self>, Error> {
let fut = unsafe {
codec_capability::get_sender_codec_capabilities(kind as i64)
}
.unwrap();
#[expect(clippy::map_err_ignore, reason = "not useful")]
let res: DartHandle = unsafe { FutureFromDart::execute(fut) }
.await
.map_err(|_| Error::FailedToGetCapabilities)?;
Ok(Vec::from(DartList::from(res))
.into_iter()
.map(|caps: DartHandle| Self::from(caps))
.collect())
}
/// Returns available [RTCRtpReceiver]'s [`CodecCapability`]s.
///
/// # Errors
///
/// With [`Error::FailedToGetCapabilities`] if fails to retrieve
/// [`CodecCapability`]s.
///
/// [RTCRtpReceiver]: https://w3.org/TR/webrtc#dom-rtcrtpreceiver
pub async fn get_receiver_codec_capabilities(
kind: MediaKind,
) -> Result<Vec<Self>, Error> {
let fut = unsafe {
codec_capability::get_receiver_codec_capabilities(kind as i64)
}
.unwrap();
#[expect(clippy::map_err_ignore, reason = "not useful")]
let res: DartHandle = unsafe { FutureFromDart::execute(fut) }
.await
.map_err(|_| Error::FailedToGetCapabilities)?;
Ok(Vec::from(DartList::from(res))
.into_iter()
.map(|caps: DartHandle| Self::from(caps))
.collect())
}
/// Returns [mimeType][2] of this [`CodecCapability`].
///
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodec-mimetype
#[must_use]
pub fn mime_type(&self) -> String {
let mime_type =
unsafe { codec_capability::mime_type(self.0.get()) }.unwrap();
unsafe { dart_string_into_rust(mime_type) }
}
/// Returns [clockRate][2] of this [`CodecCapability`].
///
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodec-clockrate
#[must_use]
pub fn clock_rate(&self) -> u32 {
unsafe { codec_capability::clock_rate(self.0.get()) }.unwrap()
}
/// Returns [channels][2] of this [`CodecCapability`].
///
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodec-channels
#[expect(clippy::unwrap_in_result, reason = "unrelated and intended")]
#[must_use]
pub fn channels(&self) -> Option<u16> {
let channels =
unsafe { codec_capability::channels(self.0.get()) }.unwrap();
if channels > 0 { u16::try_from(channels).ok() } else { None }
}
/// Returns [sdpFmtpLine][2] of this [`CodecCapability`].
///
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodec-sdpfmtpline
#[must_use]
pub fn parameters(&self) -> HashMap<String, String> {
let params_json_ptr =
unsafe { codec_capability::parameters(self.0.get()) }.unwrap();
let params_json = unsafe { dart_string_into_rust(params_json_ptr) };
serde_json::from_str(¶ms_json).unwrap_or_else(|_| {
log::error!("Failed to parse codec params: {params_json}");
HashMap::new()
})
}
/// Returns the underlying [`Dart_Handle`] of this [`CodecCapability`].
#[must_use]
pub fn handle(&self) -> Dart_Handle {
self.0.get()
}
}