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
//! WASM side representation of an [RTCRtpCodecCapability].
//!
//! [RTCRtpCodecCapability]: https://w3.org/TR/webrtc#dom-rtcrtpcodeccapability
use js_sys::{Array, JsString, Reflect};
use web_sys::{RtcRtpCodecCapability, RtcRtpSender};
use crate::{
media::MediaKind, platform::codec_capability::CodecCapabilityError as Error,
};
/// WASM side representation of an [RTCRtpCodecCapability].
///
/// [RTCRtpCodecCapability]: https://w3.org/TR/webrtc#dom-rtcrtpcodeccapability
#[derive(Clone, Debug)]
pub struct CodecCapability {
/// Actual JS-side [`RtcRtpCodecCapability`].
codec_cap: RtcRtpCodecCapability,
/// [MIME media type/subtype][2] of the codec.
///
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodeccapability-mimetype
mime_type: String,
}
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
#[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
pub async fn get_sender_codec_capabilities(
kind: MediaKind,
) -> Result<Vec<Self>, Error> {
let mut result = Vec::new();
let Some(caps) = RtcRtpSender::get_capabilities(&kind.to_string())
else {
return Err(Error::FailedToGetCapabilities);
};
// TODO: Get rid of reflection in #183 which updates `web-sys` to
// 0.3.70.
let Ok(codecs) = Reflect::get(&caps, &JsString::from("codecs")) else {
return Err(Error::FailedToGetCapabilities);
};
for codec in Array::from(&codecs).values() {
let Ok(codec) = codec else {
continue;
};
let codec_cap = RtcRtpCodecCapability::from(codec);
let Some(mime_type) =
Reflect::get(&codec_cap, &JsString::from("mimeType"))
.ok()
.and_then(|v| v.as_string())
else {
return Err(Error::FailedToGetCapabilities);
};
result.push(Self {
codec_cap,
mime_type,
});
}
Ok(result)
}
/// Returns [MIME media type][2] of this [`CodecCapability`].
///
/// [2]: https://w3.org/TR/webrtc#dom-rtcrtpcodeccapability-mimetype
#[must_use]
pub fn mime_type(&self) -> String {
self.mime_type.clone()
}
/// Returns the underlying [`RtcRtpCodecCapability`] of this
/// [`CodecCapability`].
#[must_use]
pub const fn handle(&self) -> &RtcRtpCodecCapability {
&self.codec_cap
}
}