mirror of
https://github.com/lexmount/moli.git
synced 2026-11-22 00:02:21 +00:00
feat(webrtc): add native RTP sender operations
This commit is contained in:
@@ -211,7 +211,9 @@ pub(super) fn constructor<'s>(
|
||||
}
|
||||
}
|
||||
|
||||
fn identifier<'s>(scope: &mut v8::PinScope<'s, '_>) -> Option<v8::Local<'s, v8::String>> {
|
||||
pub(super) fn identifier<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
) -> Option<v8::Local<'s, v8::String>> {
|
||||
let mut bytes = [0_u8; 16];
|
||||
if let Err(error) = moli_crypto::fill_secure_random(&mut bytes) {
|
||||
super::throw_error_exception(scope, &format!("Media identity failed: {error}"));
|
||||
@@ -556,6 +558,15 @@ pub(super) fn stream_id<'s>(
|
||||
.expect("stream id string")
|
||||
}
|
||||
|
||||
pub(super) fn track_id<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
track: v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::String> {
|
||||
let track = target(scope, track);
|
||||
v8::Local::try_from(get_private_value(scope, track, ID).expect("native track id"))
|
||||
.expect("track id string")
|
||||
}
|
||||
|
||||
/// The UA's queued ended steps differ from the author's synchronous stop().
|
||||
pub(super) fn end_track<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
|
||||
use moli_webapi::rtc::peer_connection::{
|
||||
RTC_PEER_CONNECTION_SIGNALING_STATE_SLOT, rtp_capabilities,
|
||||
complete_create_offer, complete_set_local_description,
|
||||
};
|
||||
|
||||
mod operations;
|
||||
mod rtp_sender;
|
||||
mod rtp_offer;
|
||||
mod rtp_parameters;
|
||||
pub(crate) mod rtp_transceivers;
|
||||
@@ -18,10 +21,21 @@ pub(super) fn install<'s>(
|
||||
}
|
||||
|
||||
pub(super) fn install_runtime(scope: &mut v8::PinScope<'_, '_>) {
|
||||
moli_webapi::rtc::peer_connection::operations::install_runtime(
|
||||
scope,
|
||||
moli_webapi::rtc::peer_connection::operations::OperationsRuntime {
|
||||
initialize: operations::initialize,
|
||||
enqueue: operations::enqueue,
|
||||
payload: operations::payload,
|
||||
resolve: operations::resolve,
|
||||
reject: operations::reject,
|
||||
close: operations::close,
|
||||
},
|
||||
);
|
||||
moli_webapi::rtc::peer_connection::rtp_transceivers::install_runtime(
|
||||
scope,
|
||||
moli_webapi::rtc::peer_connection::rtp_transceivers::RtpRuntime {
|
||||
initialize_pc: rtp_transceivers::initialize_pc,
|
||||
initialize_pc,
|
||||
close: rtp_transceivers::close,
|
||||
create_offer,
|
||||
},
|
||||
@@ -38,3 +52,11 @@ fn create_offer<'s>(
|
||||
rtp_offer::build(§ions, data)
|
||||
})
|
||||
}
|
||||
|
||||
fn initialize_pc<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) -> Option<()> {
|
||||
rtp_transceivers::initialize_pc(scope, pc);
|
||||
rtp_sender::initialize_pc(scope, pc)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,295 @@
|
||||
//! The connection's native operations chain. Networking tasks retain their
|
||||
//! exact owner; promise reactions retain the operation's calling realm.
|
||||
|
||||
use crate::{
|
||||
native_bridge::throw_dom_exception,
|
||||
page_task_queue::RendererPageWebRtcTaskKind,
|
||||
util::{
|
||||
context_host_ptr_from_global_bridge, get_private_object, get_private_value,
|
||||
set_private_value,
|
||||
},
|
||||
};
|
||||
use moli_webapi_declare::WebApiObject;
|
||||
|
||||
const OPERATIONS: &str = "__moliRtcOperations";
|
||||
const DEFER_NEGOTIATION: &str = "__moliRtcNegotiateOnEmptyChain";
|
||||
const OWNER: &str = "__moliRtcOperationOwner";
|
||||
const KIND: &str = "__moliRtcOperationKind";
|
||||
const PAYLOAD: &str = "__moliRtcOperationPayload";
|
||||
const INNER: &str = "__moliRtcOperationInner";
|
||||
const OUTER: &str = "__moliRtcOperationOuter";
|
||||
|
||||
pub(super) use moli_webapi::rtc::peer_connection::operations::Kind;
|
||||
|
||||
fn operation_task(kind: Kind) -> RendererPageWebRtcTaskKind {
|
||||
match kind {
|
||||
Kind::CreateOffer => RendererPageWebRtcTaskKind::CreateOffer,
|
||||
Kind::SetLocalDescription => RendererPageWebRtcTaskKind::SetLocalDescription,
|
||||
Kind::ReplaceTrack => RendererPageWebRtcTaskKind::ReplaceTrack,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(WebApiObject)]
|
||||
#[webapi(plain)]
|
||||
struct Request<'s> {
|
||||
#[webapi(slot = OWNER)]
|
||||
owner: v8::Local<'s, v8::Object>,
|
||||
#[webapi(slot = KIND)]
|
||||
kind: v8::Local<'s, v8::Integer>,
|
||||
#[webapi(slot = PAYLOAD)]
|
||||
payload: v8::Local<'s, v8::Value>,
|
||||
#[webapi(slot = INNER)]
|
||||
inner: v8::Local<'s, v8::PromiseResolver>,
|
||||
#[webapi(slot = OUTER)]
|
||||
outer: v8::Local<'s, v8::PromiseResolver>,
|
||||
}
|
||||
|
||||
pub(super) fn initialize<'s>(scope: &mut v8::PinScope<'s, '_>, pc: v8::Local<'s, v8::Object>) {
|
||||
let list = v8::Array::new(scope, 0);
|
||||
set_private_value(scope, pc, OPERATIONS, list.into());
|
||||
set_private_value(
|
||||
scope,
|
||||
pc,
|
||||
DEFER_NEGOTIATION,
|
||||
v8::Boolean::new(scope, false).into(),
|
||||
);
|
||||
}
|
||||
fn list<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::Array> {
|
||||
v8::Local::try_from(get_private_value(scope, pc, OPERATIONS).expect("operations chain"))
|
||||
.expect("operations array")
|
||||
}
|
||||
pub(super) fn is_empty<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) -> bool {
|
||||
list(scope, pc).length() == 0
|
||||
}
|
||||
pub(super) fn defer_negotiation<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) {
|
||||
set_private_value(
|
||||
scope,
|
||||
pc,
|
||||
DEFER_NEGOTIATION,
|
||||
v8::Boolean::new(scope, true).into(),
|
||||
);
|
||||
}
|
||||
pub(super) fn owner<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::Object> {
|
||||
get_private_object(scope, request, OWNER).expect("operation owner")
|
||||
}
|
||||
pub(super) fn payload<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::Value> {
|
||||
get_private_value(scope, request, PAYLOAD).expect("operation payload")
|
||||
}
|
||||
fn resolver<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
slot: &str,
|
||||
) -> v8::Local<'s, v8::PromiseResolver> {
|
||||
let object = get_private_object(scope, request, slot).expect("operation resolver");
|
||||
// Only the native Request declaration can populate these private slots.
|
||||
unsafe { v8::Local::cast_unchecked(object) }
|
||||
}
|
||||
pub(super) fn resolve<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
value: v8::Local<'s, v8::Value>,
|
||||
) {
|
||||
resolver(scope, request, INNER).resolve(scope, value);
|
||||
}
|
||||
pub(super) fn reject<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
reason: v8::Local<'s, v8::Value>,
|
||||
) {
|
||||
resolver(scope, request, INNER).reject(scope, reason);
|
||||
}
|
||||
pub(super) fn queue<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
payload: v8::Local<'s, v8::Object>,
|
||||
kind: RendererPageWebRtcTaskKind,
|
||||
) -> bool {
|
||||
context_host_ptr_from_global_bridge(scope).is_some_and(|host| {
|
||||
unsafe { &mut *host }.queue_webrtc_task_for_owner(scope, pc, payload, kind)
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn enqueue<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
kind: Kind,
|
||||
payload: v8::Local<'s, v8::Value>,
|
||||
) -> Option<v8::Local<'s, v8::Promise>> {
|
||||
if super::rtp_transceivers::closed(scope, pc) {
|
||||
throw_dom_exception(scope, "InvalidStateError", 11, "The connection is closed.");
|
||||
return None;
|
||||
}
|
||||
let inner = v8::PromiseResolver::new(scope)?;
|
||||
let outer = v8::PromiseResolver::new(scope)?;
|
||||
let request = Request::new(
|
||||
pc,
|
||||
v8::Integer::new(scope, kind as i32),
|
||||
payload,
|
||||
inner,
|
||||
outer,
|
||||
)
|
||||
.bind(scope)
|
||||
.ok()?;
|
||||
let fulfill = v8::Function::builder(fulfilled)
|
||||
.data(request.into())
|
||||
.build(scope)?;
|
||||
let reject = v8::Function::builder(rejected)
|
||||
.data(request.into())
|
||||
.build(scope)?;
|
||||
inner.get_promise(scope).then2(scope, fulfill, reject)?;
|
||||
let pending = list(scope, pc);
|
||||
pending.set_index(scope, pending.length(), request.into())?;
|
||||
if pending.length() == 1 {
|
||||
start(scope, request);
|
||||
}
|
||||
Some(outer.get_promise(scope))
|
||||
}
|
||||
|
||||
fn start<'s>(scope: &mut v8::PinScope<'s, '_>, request: v8::Local<'s, v8::Object>) {
|
||||
let context = request
|
||||
.get_creation_context(scope)
|
||||
.expect("operation realm");
|
||||
let scope = &mut v8::ContextScope::new(scope, context);
|
||||
let pc = owner(scope, request);
|
||||
if super::rtp_transceivers::closed(scope, pc) {
|
||||
return;
|
||||
}
|
||||
let kind = match get_private_value(scope, request, KIND)
|
||||
.expect("operation kind")
|
||||
.int32_value(scope)
|
||||
.expect("operation tag")
|
||||
{
|
||||
0 => Kind::CreateOffer,
|
||||
1 => Kind::SetLocalDescription,
|
||||
2 => Kind::ReplaceTrack,
|
||||
_ => unreachable!("native operation tag"),
|
||||
};
|
||||
if matches!(kind, Kind::ReplaceTrack) && !super::rtp_sender::start_replace_track(scope, request)
|
||||
{
|
||||
return;
|
||||
}
|
||||
queue(scope, pc, request, operation_task(kind));
|
||||
}
|
||||
|
||||
fn fulfilled<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
_rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
finish(scope, &args, false);
|
||||
}
|
||||
fn rejected<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
_rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
finish(scope, &args, true);
|
||||
}
|
||||
fn finish<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: &v8::FunctionCallbackArguments<'s>,
|
||||
rejected: bool,
|
||||
) {
|
||||
let request =
|
||||
v8::Local::<v8::Object>::try_from(args.data()).expect("native operation reaction");
|
||||
let pc = owner(scope, request);
|
||||
if super::rtp_transceivers::closed(scope, pc) {
|
||||
return;
|
||||
}
|
||||
let outer = resolver(scope, request, OUTER);
|
||||
let settled = v8::Function::builder(advance)
|
||||
.data(request.into())
|
||||
.build(scope)
|
||||
.expect("operation completion reaction");
|
||||
outer.get_promise(scope).then2(scope, settled, settled);
|
||||
if rejected {
|
||||
outer.reject(scope, args.get(0));
|
||||
} else {
|
||||
outer.resolve(scope, args.get(0));
|
||||
}
|
||||
}
|
||||
fn advance<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
_rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
let request =
|
||||
v8::Local::<v8::Object>::try_from(args.data()).expect("native operation reaction");
|
||||
let pc = owner(scope, request);
|
||||
if super::rtp_transceivers::closed(scope, pc) {
|
||||
return;
|
||||
}
|
||||
let pending = list(scope, pc);
|
||||
let rest: Vec<_> = (1..pending.length())
|
||||
.map(|i| pending.get_index(scope, i).expect("queued operation"))
|
||||
.collect();
|
||||
let next = v8::Array::new_with_elements(scope, &rest);
|
||||
set_private_value(scope, pc, OPERATIONS, next.into());
|
||||
if let Some(first) = rest.first() {
|
||||
start(
|
||||
scope,
|
||||
v8::Local::try_from(*first).expect("operation object"),
|
||||
);
|
||||
} else if get_private_value(scope, pc, DEFER_NEGOTIATION)
|
||||
.expect("deferred negotiation")
|
||||
.boolean_value(scope)
|
||||
{
|
||||
set_private_value(
|
||||
scope,
|
||||
pc,
|
||||
DEFER_NEGOTIATION,
|
||||
v8::Boolean::new(scope, false).into(),
|
||||
);
|
||||
super::rtp_transceivers::update_negotiation_needed(scope, pc);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn close<'s>(scope: &mut v8::PinScope<'s, '_>, pc: v8::Local<'s, v8::Object>) {
|
||||
// Retired/closed operations deliberately never complete their promises.
|
||||
initialize(scope, pc);
|
||||
}
|
||||
|
||||
pub(super) fn apply<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
kind: RendererPageWebRtcTaskKind,
|
||||
) -> bool {
|
||||
let pc = owner(scope, request);
|
||||
if super::rtp_transceivers::closed(scope, pc) {
|
||||
return false;
|
||||
}
|
||||
let context = request
|
||||
.get_creation_context(scope)
|
||||
.expect("operation realm");
|
||||
let scope = &mut v8::ContextScope::new(scope, context);
|
||||
match kind {
|
||||
RendererPageWebRtcTaskKind::CreateOffer => super::complete_create_offer(scope, request, pc),
|
||||
RendererPageWebRtcTaskKind::SetLocalDescription => {
|
||||
super::complete_set_local_description(scope, request, pc)
|
||||
}
|
||||
RendererPageWebRtcTaskKind::ReplaceTrack => {
|
||||
super::rtp_sender::apply_replace_track(scope, request, pc)
|
||||
}
|
||||
RendererPageWebRtcTaskKind::CompleteReplaceTrack => {
|
||||
let value = v8::undefined(scope);
|
||||
resolve(scope, request, value.into());
|
||||
true
|
||||
}
|
||||
_ => unreachable!("connection operation task"),
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,14 @@ use moli_webapi::rtc::peer_connection::{
|
||||
RTC_AUDIO_OFFER_SECTION, RTC_DATA_OFFER_SECTION, RTC_VIDEO_OFFER_SECTION,
|
||||
};
|
||||
|
||||
pub(super) struct Section {
|
||||
pub kind: String,
|
||||
pub direction: String,
|
||||
pub codecs: Vec<Codec>,
|
||||
pub streams: Vec<String>,
|
||||
pub track: String,
|
||||
}
|
||||
|
||||
struct Format<'a> {
|
||||
payload: &'a str,
|
||||
codec: Codec,
|
||||
@@ -102,15 +110,29 @@ fn media_section(kind: &str, direction: &str, codecs: &[Codec], mid: usize) -> S
|
||||
result
|
||||
}
|
||||
|
||||
pub(super) fn build(sections: &[(String, String, Vec<Codec>)], data: bool) -> String {
|
||||
pub(super) fn build(sections: &[Section], data: bool) -> String {
|
||||
let count = sections.len() + usize::from(data);
|
||||
let mids: Vec<_> = (0..count).map(|mid| mid.to_string()).collect();
|
||||
let mut sdp = format!(
|
||||
"v=0\r\no=- 0 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE {}\r\na=extmap-allow-mixed\r\na=msid-semantic: WMS\r\n",
|
||||
mids.join(" ")
|
||||
);
|
||||
for (mid, (kind, direction, codecs)) in sections.iter().enumerate() {
|
||||
sdp.push_str(&media_section(kind, direction, codecs, mid));
|
||||
for (mid, section) in sections.iter().enumerate() {
|
||||
sdp.push_str(&media_section(
|
||||
§ion.kind,
|
||||
§ion.direction,
|
||||
§ion.codecs,
|
||||
mid,
|
||||
));
|
||||
if matches!(section.direction.as_str(), "sendrecv" | "sendonly") {
|
||||
if section.streams.is_empty() {
|
||||
sdp.push_str(&format!("a=msid:- {}\r\n", section.track));
|
||||
} else {
|
||||
for stream in §ion.streams {
|
||||
sdp.push_str(&format!("a=msid:{stream} {}\r\n", section.track));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if data {
|
||||
sdp.push_str(
|
||||
@@ -138,8 +160,20 @@ mod tests {
|
||||
.clone();
|
||||
let sdp = build(
|
||||
&[
|
||||
("video".into(), "sendonly".into(), vec![h264.clone(), rtx]),
|
||||
("video".into(), "inactive".into(), vec![h264]),
|
||||
Section {
|
||||
kind: "video".into(),
|
||||
direction: "sendonly".into(),
|
||||
codecs: vec![h264.clone(), rtx],
|
||||
streams: vec!["s1".into(), "s2".into()],
|
||||
track: "track".into(),
|
||||
},
|
||||
Section {
|
||||
kind: "video".into(),
|
||||
direction: "inactive".into(),
|
||||
codecs: vec![h264],
|
||||
streams: vec![],
|
||||
track: "inactive".into(),
|
||||
},
|
||||
],
|
||||
true,
|
||||
);
|
||||
@@ -148,5 +182,7 @@ mod tests {
|
||||
assert!(!sdp.contains("VP8"));
|
||||
assert!(sdp.contains("a=fmtp:104 apt=103\r\n"));
|
||||
assert_eq!(sdp.matches("a=mid:").count(), 3);
|
||||
assert!(sdp.contains("a=msid:s1 track\r\na=msid:s2 track\r\n"));
|
||||
assert!(!sdp.contains("a=msid:- inactive"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,6 +121,71 @@ impl Encoding {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, webidl::WebIdlDictionary)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[webidl(prefix = "RTCRtcpParameters")]
|
||||
pub(super) struct Rtcp {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub cname: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub reduced_size: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, webidl::WebIdlDictionary)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[webidl(prefix = "RTCRtpHeaderExtensionParameters")]
|
||||
pub(super) struct HeaderExtension {
|
||||
#[webidl(default = false)]
|
||||
encrypted: bool,
|
||||
#[webidl(required)]
|
||||
id: u16,
|
||||
#[webidl(required)]
|
||||
uri: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, webidl::WebIdlDictionary)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[webidl(prefix = "RTCRtpCodecParameters")]
|
||||
pub(super) struct CodecParameters {
|
||||
#[webidl(inherit)]
|
||||
#[serde(flatten)]
|
||||
codec: Codec,
|
||||
#[webidl(required, converter = "octet")]
|
||||
payload_type: u8,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, webidl::WebIdlDictionary)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[webidl(prefix = "RTCRtpParameters")]
|
||||
pub(super) struct Parameters {
|
||||
#[webidl(required, sequence, converter = "dictionary")]
|
||||
codecs: Vec<CodecParameters>,
|
||||
#[webidl(required, sequence, converter = "dictionary")]
|
||||
header_extensions: Vec<HeaderExtension>,
|
||||
#[webidl(required, dictionary)]
|
||||
rtcp: Rtcp,
|
||||
}
|
||||
|
||||
#[derive(webidl::WebIdlDictionary)]
|
||||
#[webidl(prefix = "RTCRtpSendParameters")]
|
||||
pub(super) struct SendParameters {
|
||||
#[webidl(inherit)]
|
||||
pub parameters: Parameters,
|
||||
#[webidl(required, sequence, converter = "dictionary")]
|
||||
pub encodings: Vec<Encoding>,
|
||||
#[webidl(required)]
|
||||
pub transaction_id: String,
|
||||
}
|
||||
|
||||
impl SendParameters {
|
||||
pub(super) fn snapshot(&self) -> serde_json::Value {
|
||||
let mut result = serde_json::to_value(&self.parameters).expect("RTP parameters");
|
||||
result["transactionId"] = serde_json::json!(self.transaction_id);
|
||||
result["encodings"] = self.encodings.iter().map(Encoding::snapshot).collect();
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -0,0 +1,514 @@
|
||||
//! Native sender state before transport negotiation. Parameter transactions and
|
||||
//! track changes are real local state; no RTP encoder or transport is started.
|
||||
|
||||
use super::{
|
||||
operations,
|
||||
rtp_parameters::{SendParameters, capabilities},
|
||||
rtp_transceivers,
|
||||
};
|
||||
use crate::{
|
||||
context_bootstrap::media_streams,
|
||||
native_bridge::throw_dom_exception,
|
||||
page_task_queue::RendererPageWebRtcTaskKind,
|
||||
util::{
|
||||
get_private_object, get_private_value, set_private_value, throw_range_error,
|
||||
throw_type_error, v8_string,
|
||||
},
|
||||
web_api_interfaces, webidl,
|
||||
};
|
||||
use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
|
||||
|
||||
const OWNER: &str = "__moliRtpOwner";
|
||||
const TRANSCEIVER: &str = "__moliRtpSenderTransceiver";
|
||||
const KIND: &str = "__moliRtpSenderKind";
|
||||
const TRACK: &str = "__moliRtpTrack";
|
||||
const TRACK_ID: &str = "__moliRtpSdpTrackId";
|
||||
const ENCODINGS: &str = "__moliRtpEncodings";
|
||||
const STREAMS: &str = "__moliRtpAssociatedStreamIds";
|
||||
const LAST_PARAMETERS: &str = "__moliRtpLastReturnedParameters";
|
||||
const CNAME: &str = "__moliRtcRtcpCname";
|
||||
const TASK_SENDER: &str = "__moliRtpTaskSender";
|
||||
const TASK_PARAMETERS: &str = "__moliRtpTaskParameters";
|
||||
const TASK_RESOLVER: &str = "__moliRtpTaskResolver";
|
||||
const TASK_TRACK: &str = "__moliRtpTaskTrack";
|
||||
|
||||
#[derive(WebApiObject)]
|
||||
#[webapi(interface = web_api_interfaces::RTCRtpSender)]
|
||||
struct SenderSlots<'s> {
|
||||
#[webapi(slot = OWNER)]
|
||||
owner: v8::Local<'s, v8::Object>,
|
||||
#[webapi(slot = TRANSCEIVER)]
|
||||
transceiver: v8::Local<'s, v8::Object>,
|
||||
#[webapi(slot = KIND)]
|
||||
kind: v8::Local<'s, v8::String>,
|
||||
#[webapi(slot = TRACK)]
|
||||
track: v8::Local<'s, v8::Value>,
|
||||
#[webapi(slot = TRACK_ID)]
|
||||
track_id: v8::Local<'s, v8::String>,
|
||||
#[webapi(slot = ENCODINGS)]
|
||||
encodings: v8::Local<'s, v8::String>,
|
||||
#[webapi(slot = STREAMS)]
|
||||
streams: v8::Local<'s, v8::Array>,
|
||||
#[webapi(slot = LAST_PARAMETERS, init = "null")]
|
||||
last_parameters: (),
|
||||
}
|
||||
#[derive(WebApiObject)]
|
||||
#[webapi(plain)]
|
||||
struct ParameterTask<'s> {
|
||||
#[webapi(slot = TASK_SENDER)]
|
||||
sender: v8::Local<'s, v8::Object>,
|
||||
#[webapi(slot = TASK_PARAMETERS)]
|
||||
parameters: v8::Local<'s, v8::String>,
|
||||
#[webapi(slot = TASK_RESOLVER)]
|
||||
resolver: v8::Local<'s, v8::Value>,
|
||||
}
|
||||
#[derive(WebApiObject)]
|
||||
#[webapi(plain)]
|
||||
struct Replacement<'s> {
|
||||
#[webapi(slot = TASK_SENDER)]
|
||||
sender: v8::Local<'s, v8::Object>,
|
||||
#[webapi(slot = TASK_TRACK)]
|
||||
track: v8::Local<'s, v8::Value>,
|
||||
}
|
||||
|
||||
#[derive(webidl::WebIdlDictionary)]
|
||||
#[webidl(prefix = "RTCSetParameterOptions")]
|
||||
struct SetParameterOptions {}
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "RTCRtpSender.setParameters")]
|
||||
struct SetParametersArgs {
|
||||
#[webidl(required, dictionary)]
|
||||
parameters: SendParameters,
|
||||
#[webidl(dictionary)]
|
||||
_options: SetParameterOptions,
|
||||
}
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "RTCRtpSender.replaceTrack")]
|
||||
struct ReplaceTrackArgs<'s> {
|
||||
#[webidl(required, nullable, interface = web_api_interfaces::MediaStreamTrack)]
|
||||
track: Option<v8::Local<'s, v8::Object>>,
|
||||
}
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "RTCRtpSender.setStreams")]
|
||||
struct SetStreamsArgs<'s> {
|
||||
#[webidl(variadic, interface = web_api_interfaces::MediaStream)]
|
||||
streams: Vec<v8::Local<'s, v8::Object>>,
|
||||
}
|
||||
|
||||
#[derive(WebApiFunctionTemplate)]
|
||||
#[webapi(interface = web_api_interfaces::RTCRtpSender, enumerable)]
|
||||
struct SenderPrototype {
|
||||
#[webapi(accessor_property, getter = track)]
|
||||
track: (),
|
||||
#[webapi(accessor_property, getter = transport)]
|
||||
transport: (),
|
||||
#[webapi(method, length = 0, callback = get_parameters)]
|
||||
get_parameters: (),
|
||||
#[webapi(method, returns_promise, length = 1, callback = set_parameters)]
|
||||
set_parameters: (),
|
||||
#[webapi(method, returns_promise, length = 1, callback = replace_track)]
|
||||
replace_track: (),
|
||||
#[webapi(method, length = 0, callback = set_streams)]
|
||||
set_streams: (),
|
||||
}
|
||||
pub(super) fn install<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_, ()>,
|
||||
prototype: v8::Local<'s, v8::ObjectTemplate>,
|
||||
) {
|
||||
SenderPrototype::initialize_prototype_template(scope, prototype);
|
||||
}
|
||||
pub(super) fn initialize_pc<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) -> Option<()> {
|
||||
let cname = media_streams::identifier(scope)?;
|
||||
set_private_value(scope, pc, CNAME, cname.into());
|
||||
Some(())
|
||||
}
|
||||
pub(super) struct Init<'s, 'a> {
|
||||
pub pc: v8::Local<'s, v8::Object>,
|
||||
pub transceiver: v8::Local<'s, v8::Object>,
|
||||
pub kind: &'a str,
|
||||
pub track: Option<v8::Local<'s, v8::Object>>,
|
||||
pub encodings: serde_json::Value,
|
||||
pub streams: &'a [v8::Local<'s, v8::Value>],
|
||||
}
|
||||
pub(super) fn initialize<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
sender: v8::Local<'s, v8::Object>,
|
||||
init: Init<'s, '_>,
|
||||
) -> Option<()> {
|
||||
let Init {
|
||||
pc,
|
||||
transceiver,
|
||||
kind,
|
||||
track,
|
||||
encodings,
|
||||
streams,
|
||||
} = init;
|
||||
let track_id = if let Some(track) = track {
|
||||
media_streams::track_id(scope, track)
|
||||
} else {
|
||||
media_streams::identifier(scope)?
|
||||
};
|
||||
SenderSlots::new(
|
||||
pc,
|
||||
transceiver,
|
||||
v8_string(scope, kind)?,
|
||||
track
|
||||
.map(Into::into)
|
||||
.unwrap_or_else(|| v8::null(scope).into()),
|
||||
track_id,
|
||||
v8_string(scope, &encodings.to_string())?,
|
||||
v8::Array::new_with_elements(scope, streams),
|
||||
)
|
||||
.initialize(scope, sender)
|
||||
.expect("RTP sender slots");
|
||||
Some(())
|
||||
}
|
||||
fn owner<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
sender: v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::Object> {
|
||||
get_private_object(scope, sender, OWNER).expect("sender owner")
|
||||
}
|
||||
fn transceiver<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
sender: v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::Object> {
|
||||
get_private_object(scope, sender, TRANSCEIVER).expect("sender transceiver")
|
||||
}
|
||||
fn json<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
object: v8::Local<'s, v8::Object>,
|
||||
slot: &str,
|
||||
) -> serde_json::Value {
|
||||
let snapshot = get_private_value(scope, object, slot)
|
||||
.expect("RTP snapshot")
|
||||
.to_rust_string_lossy(scope);
|
||||
serde_json::from_str(&snapshot).expect("native RTP JSON")
|
||||
}
|
||||
fn invalid_modification(scope: &mut v8::PinScope<'_, '_>) {
|
||||
throw_dom_exception(
|
||||
scope,
|
||||
"InvalidModificationError",
|
||||
13,
|
||||
"Read-only RTP parameters were modified.",
|
||||
);
|
||||
}
|
||||
fn track<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
mut rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
let sender = rtp_transceivers::target(scope, args.this());
|
||||
rv.set(get_private_value(scope, sender, TRACK).expect("sender track"));
|
||||
}
|
||||
fn transport<'s>(
|
||||
_scope: &mut v8::PinScope<'s, '_>,
|
||||
_args: v8::FunctionCallbackArguments<'s>,
|
||||
mut rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
rv.set_null();
|
||||
}
|
||||
|
||||
fn get_parameters<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
mut rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
let sender = rtp_transceivers::target(scope, args.this());
|
||||
let pc = owner(scope, sender);
|
||||
let last = get_private_value(scope, sender, LAST_PARAMETERS).expect("last returned parameters");
|
||||
let snapshot = if let Ok(snapshot) = v8::Local::<v8::String>::try_from(last) {
|
||||
snapshot
|
||||
} else {
|
||||
let Some(id) = media_streams::identifier(scope) else {
|
||||
return;
|
||||
};
|
||||
let cname = get_private_value(scope, pc, CNAME)
|
||||
.expect("connection CNAME")
|
||||
.to_rust_string_lossy(scope);
|
||||
let value = serde_json::json!({
|
||||
"transactionId": id.to_rust_string_lossy(scope),
|
||||
"codecs": [], "headerExtensions": [],
|
||||
"rtcp": {"cname": cname, "reducedSize": false},
|
||||
"encodings": json(scope, sender, ENCODINGS),
|
||||
});
|
||||
let Some(snapshot) = v8_string(scope, &value.to_string()) else {
|
||||
return;
|
||||
};
|
||||
set_private_value(scope, sender, LAST_PARAMETERS, snapshot.into());
|
||||
let task = ParameterTask::new(sender, snapshot, v8::null(scope).into())
|
||||
.bind(scope)
|
||||
.expect("parameter cache task");
|
||||
operations::queue(
|
||||
scope,
|
||||
pc,
|
||||
task,
|
||||
RendererPageWebRtcTaskKind::ClearRtpParameters,
|
||||
);
|
||||
snapshot
|
||||
};
|
||||
// WebIDL returns a fresh dictionary, never the native transaction snapshot.
|
||||
// V8's intrinsic JSON parser ignores replaced global constructors/functions.
|
||||
if let Some(value) = v8::json::parse(scope, snapshot) {
|
||||
rv.set(value);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_parameters<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
mut rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
let Some(mut parsed) = webidl::parse_args::<SetParametersArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
let sender = rtp_transceivers::target(scope, args.this());
|
||||
let transceiver = transceiver(scope, sender);
|
||||
if rtp_transceivers::stopping(scope, transceiver)
|
||||
|| get_private_value(scope, sender, LAST_PARAMETERS)
|
||||
.expect("parameter cache")
|
||||
.is_null()
|
||||
{
|
||||
throw_dom_exception(
|
||||
scope,
|
||||
"InvalidStateError",
|
||||
11,
|
||||
"The sender is stopped or the parameter transaction expired.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
let previous = json(scope, sender, LAST_PARAMETERS);
|
||||
let supplied = parsed.parameters.snapshot();
|
||||
let previous_encodings = previous["encodings"].as_array().expect("encoding list");
|
||||
if ["transactionId", "codecs", "headerExtensions", "rtcp"]
|
||||
.iter()
|
||||
.any(|key| previous[*key] != supplied[*key])
|
||||
|| previous_encodings.len() != parsed.parameters.encodings.len()
|
||||
|| previous_encodings
|
||||
.iter()
|
||||
.zip(&parsed.parameters.encodings)
|
||||
.any(|(old, new)| old.get("rid") != new.snapshot().get("rid"))
|
||||
{
|
||||
invalid_modification(scope);
|
||||
return;
|
||||
}
|
||||
let kind = get_private_value(scope, sender, KIND)
|
||||
.expect("sender kind")
|
||||
.to_rust_string_lossy(scope);
|
||||
let supported = capabilities(&kind);
|
||||
for encoding in &mut parsed.parameters.encodings {
|
||||
if let Some(codec) = &encoding.codec {
|
||||
if !supported.iter().any(|candidate| codec.matches(candidate)) {
|
||||
invalid_modification(scope);
|
||||
return;
|
||||
}
|
||||
throw_dom_exception(
|
||||
scope,
|
||||
"OperationError",
|
||||
0,
|
||||
"Per-encoding codec selection requires a media backend.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
if kind == "audio" {
|
||||
encoding.max_framerate = None;
|
||||
encoding.scale_resolution_down_by = None;
|
||||
} else {
|
||||
let scale = encoding
|
||||
.scale_resolution_down_by
|
||||
.get_or_insert(webidl::Double(1.0));
|
||||
if scale.0 < 1.0 || encoding.max_framerate.as_ref().is_some_and(|v| v.0 < 0.0) {
|
||||
throw_range_error(scope, "Invalid video encoding rate or scale.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
let encodings: Vec<_> = parsed
|
||||
.parameters
|
||||
.encodings
|
||||
.iter()
|
||||
.map(super::rtp_parameters::Encoding::snapshot)
|
||||
.collect();
|
||||
let Some(snapshot) = v8_string(
|
||||
scope,
|
||||
&serde_json::to_string(&encodings).expect("encoding snapshot"),
|
||||
) else {
|
||||
return;
|
||||
};
|
||||
let Some(resolver) = v8::PromiseResolver::new(scope) else {
|
||||
return;
|
||||
};
|
||||
let task = ParameterTask::new(sender, snapshot, resolver.into())
|
||||
.bind(scope)
|
||||
.expect("setParameters task");
|
||||
let pc = owner(scope, sender);
|
||||
operations::queue(
|
||||
scope,
|
||||
pc,
|
||||
task,
|
||||
RendererPageWebRtcTaskKind::SetRtpParameters,
|
||||
);
|
||||
rv.set(resolver.get_promise(scope).into());
|
||||
}
|
||||
|
||||
pub(super) fn apply_parameters<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
task: v8::Local<'s, v8::Object>,
|
||||
kind: RendererPageWebRtcTaskKind,
|
||||
) -> bool {
|
||||
let sender = get_private_object(scope, task, TASK_SENDER).expect("parameter task sender");
|
||||
let snapshot =
|
||||
get_private_value(scope, task, TASK_PARAMETERS).expect("parameter task snapshot");
|
||||
match kind {
|
||||
RendererPageWebRtcTaskKind::ClearRtpParameters => {
|
||||
if !get_private_value(scope, sender, LAST_PARAMETERS)
|
||||
.expect("parameter cache")
|
||||
.strict_equals(snapshot)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
set_private_value(scope, sender, LAST_PARAMETERS, v8::null(scope).into());
|
||||
}
|
||||
RendererPageWebRtcTaskKind::SetRtpParameters => {
|
||||
set_private_value(scope, sender, ENCODINGS, snapshot);
|
||||
set_private_value(scope, sender, LAST_PARAMETERS, v8::null(scope).into());
|
||||
let object =
|
||||
get_private_object(scope, task, TASK_RESOLVER).expect("parameter resolver");
|
||||
let resolver = unsafe { v8::Local::<v8::PromiseResolver>::cast_unchecked(object) };
|
||||
resolver.resolve(scope, v8::undefined(scope).into());
|
||||
}
|
||||
_ => unreachable!("sender parameter task"),
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn replace_track<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
mut rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
let Some(parsed) = webidl::parse_args::<ReplaceTrackArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
let sender = rtp_transceivers::target(scope, args.this());
|
||||
let kind = get_private_value(scope, sender, KIND)
|
||||
.expect("sender kind")
|
||||
.to_rust_string_lossy(scope);
|
||||
if parsed
|
||||
.track
|
||||
.is_some_and(|track| media_streams::track_kind(scope, track) != kind)
|
||||
{
|
||||
throw_type_error(scope, "The replacement track has a different kind.");
|
||||
return;
|
||||
}
|
||||
let payload = Replacement::new(
|
||||
sender,
|
||||
parsed
|
||||
.track
|
||||
.map(Into::into)
|
||||
.unwrap_or_else(|| v8::null(scope).into()),
|
||||
)
|
||||
.bind(scope)
|
||||
.expect("replacement snapshot");
|
||||
let pc = owner(scope, sender);
|
||||
if let Some(promise) =
|
||||
operations::enqueue(scope, pc, operations::Kind::ReplaceTrack, payload.into())
|
||||
{
|
||||
rv.set(promise.into());
|
||||
}
|
||||
}
|
||||
pub(super) fn start_replace_track<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
) -> bool {
|
||||
let payload = v8::Local::<v8::Object>::try_from(operations::payload(scope, request))
|
||||
.expect("replacement payload");
|
||||
let sender = get_private_object(scope, payload, TASK_SENDER).expect("replacement sender");
|
||||
let transceiver = transceiver(scope, sender);
|
||||
let error = if rtp_transceivers::stopping(scope, transceiver) {
|
||||
Some(("InvalidStateError", 11, "The transceiver is stopped."))
|
||||
} else if rtp_transceivers::sending(scope, transceiver) {
|
||||
Some((
|
||||
"InvalidModificationError",
|
||||
13,
|
||||
"Track replacement while sending requires a media backend.",
|
||||
))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some((name, code, message)) = error {
|
||||
v8::tc_scope!(let caught, scope);
|
||||
throw_dom_exception(caught, name, code, message);
|
||||
let reason = caught.exception().expect("replacement error");
|
||||
caught.reset();
|
||||
operations::reject(caught, request, reason);
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
pub(super) fn apply_replace_track<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) -> bool {
|
||||
let payload = v8::Local::<v8::Object>::try_from(operations::payload(scope, request))
|
||||
.expect("replacement payload");
|
||||
let sender = get_private_object(scope, payload, TASK_SENDER).expect("replacement sender");
|
||||
let track = get_private_value(scope, payload, TASK_TRACK).expect("replacement track");
|
||||
set_private_value(scope, sender, TRACK, track);
|
||||
operations::queue(
|
||||
scope,
|
||||
pc,
|
||||
request,
|
||||
RendererPageWebRtcTaskKind::CompleteReplaceTrack,
|
||||
)
|
||||
}
|
||||
fn set_streams<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
_rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
let Some(parsed) = webidl::parse_args::<SetStreamsArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
let sender = rtp_transceivers::target(scope, args.this());
|
||||
let pc = owner(scope, sender);
|
||||
if rtp_transceivers::closed(scope, pc) {
|
||||
throw_dom_exception(scope, "InvalidStateError", 11, "The connection is closed.");
|
||||
return;
|
||||
}
|
||||
let mut ids: Vec<v8::Local<v8::Value>> = Vec::new();
|
||||
for stream in parsed.streams {
|
||||
let id = media_streams::stream_id(scope, stream);
|
||||
if !ids.iter().any(|other| other.strict_equals(id.into())) {
|
||||
ids.push(id.into());
|
||||
}
|
||||
}
|
||||
let streams = v8::Array::new_with_elements(scope, &ids);
|
||||
set_private_value(scope, sender, STREAMS, streams.into());
|
||||
rtp_transceivers::update_negotiation_needed(scope, pc);
|
||||
}
|
||||
|
||||
pub(super) fn sdp_identity<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
sender: v8::Local<'s, v8::Object>,
|
||||
) -> (Vec<String>, String) {
|
||||
let ids = v8::Local::<v8::Array>::try_from(
|
||||
get_private_value(scope, sender, STREAMS).expect("associated stream IDs"),
|
||||
)
|
||||
.expect("stream ID array");
|
||||
let streams = (0..ids.length())
|
||||
.map(|i| {
|
||||
ids.get_index(scope, i)
|
||||
.expect("native stream ID")
|
||||
.to_rust_string_lossy(scope)
|
||||
})
|
||||
.collect();
|
||||
let track = get_private_value(scope, sender, TRACK_ID)
|
||||
.expect("SDP track ID")
|
||||
.to_rust_string_lossy(scope);
|
||||
(streams, track)
|
||||
}
|
||||
@@ -25,8 +25,6 @@ const MID: &str = "__moliRtpMid";
|
||||
const STOPPING: &str = "__moliRtpStopping";
|
||||
const STOPPED: &str = "__moliRtpStopped";
|
||||
const CODECS: &str = "__moliRtpPreferredCodecs";
|
||||
const ENCODINGS: &str = "__moliRtpEncodings";
|
||||
const STREAMS: &str = "__moliRtpAssociatedStreamIds";
|
||||
const NEGOTIATION_TASK: &str = "__moliRtcNegotiationTask";
|
||||
const NEGOTIATION_NEEDED: &str = "__moliRtcNegotiationNeeded";
|
||||
pub(super) use moli_webapi::rtc::peer_connection::rtp_transceivers::NEGOTIATION_HANDLER;
|
||||
@@ -133,16 +131,6 @@ struct TransceiverSlots<'s> {
|
||||
codecs: (),
|
||||
}
|
||||
#[derive(WebApiObject)]
|
||||
#[webapi(interface = web_api_interfaces::RTCRtpSender)]
|
||||
struct SenderSlots<'s> {
|
||||
#[webapi(slot = TRACK)]
|
||||
track: v8::Local<'s, v8::Value>,
|
||||
#[webapi(slot = ENCODINGS)]
|
||||
encodings: v8::Local<'s, v8::String>,
|
||||
#[webapi(slot = STREAMS)]
|
||||
streams: v8::Local<'s, v8::Array>,
|
||||
}
|
||||
#[derive(WebApiObject)]
|
||||
#[webapi(interface = web_api_interfaces::RTCRtpReceiver)]
|
||||
struct ReceiverSlots<'s> {
|
||||
#[webapi(slot = TRACK)]
|
||||
@@ -186,14 +174,6 @@ struct TransceiverPrototype {
|
||||
set_codec_preferences: (),
|
||||
}
|
||||
#[derive(WebApiFunctionTemplate)]
|
||||
#[webapi(interface = web_api_interfaces::RTCRtpSender, enumerable)]
|
||||
struct SenderPrototype {
|
||||
#[webapi(accessor_property, getter = slot_getter, data = v8str(scope, TRACK))]
|
||||
track: (),
|
||||
#[webapi(accessor_property, getter = transport_getter)]
|
||||
transport: (),
|
||||
}
|
||||
#[derive(WebApiFunctionTemplate)]
|
||||
#[webapi(interface = web_api_interfaces::RTCRtpReceiver, enumerable)]
|
||||
struct ReceiverPrototype {
|
||||
#[webapi(accessor_property, getter = slot_getter, data = v8str(scope, TRACK))]
|
||||
@@ -212,7 +192,7 @@ pub(super) fn install<'s>(
|
||||
"RTCRtpTransceiver" => {
|
||||
TransceiverPrototype::initialize_prototype_template(scope, prototype)
|
||||
}
|
||||
"RTCRtpSender" => SenderPrototype::initialize_prototype_template(scope, prototype),
|
||||
"RTCRtpSender" => super::rtp_sender::install(scope, prototype),
|
||||
"RTCRtpReceiver" => ReceiverPrototype::initialize_prototype_template(scope, prototype),
|
||||
_ => (),
|
||||
}
|
||||
@@ -229,7 +209,7 @@ pub(super) fn has_transceivers<'s>(
|
||||
) -> bool {
|
||||
list(scope, pc).length() != 0
|
||||
}
|
||||
fn target<'s>(
|
||||
pub(super) fn target<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
object: v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::Object> {
|
||||
@@ -252,11 +232,26 @@ fn set_bool<'s>(
|
||||
) {
|
||||
set_private_value(scope, object, slot, v8::Boolean::new(scope, value).into());
|
||||
}
|
||||
fn closed<'s>(scope: &mut v8::PinScope<'s, '_>, pc: v8::Local<'s, v8::Object>) -> bool {
|
||||
pub(super) fn closed<'s>(scope: &mut v8::PinScope<'s, '_>, pc: v8::Local<'s, v8::Object>) -> bool {
|
||||
get_private_value(scope, pc, super::RTC_PEER_CONNECTION_SIGNALING_STATE_SLOT)
|
||||
.expect("PC state")
|
||||
.strict_equals(v8str(scope, "closed").into())
|
||||
}
|
||||
pub(super) fn stopping<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
transceiver: v8::Local<'s, v8::Object>,
|
||||
) -> bool {
|
||||
flag(scope, transceiver, STOPPING)
|
||||
}
|
||||
pub(super) fn sending<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
transceiver: v8::Local<'s, v8::Object>,
|
||||
) -> bool {
|
||||
let direction =
|
||||
get_private_value(scope, transceiver, CURRENT_DIRECTION).expect("current RTP direction");
|
||||
direction.strict_equals(v8str(scope, "sendrecv").into())
|
||||
|| direction.strict_equals(v8str(scope, "sendonly").into())
|
||||
}
|
||||
fn invalid_state(scope: &mut v8::PinScope<'_, '_>) {
|
||||
throw_dom_exception(
|
||||
scope,
|
||||
@@ -413,7 +408,11 @@ fn add_transceiver<'s>(
|
||||
return;
|
||||
};
|
||||
let encodings = if parsed.init.send_encodings.is_empty() {
|
||||
serde_json::json!([{"active":true}])
|
||||
if kind == "video" {
|
||||
serde_json::json!([{"active":true,"scaleResolutionDownBy":1.0}])
|
||||
} else {
|
||||
serde_json::json!([{"active":true}])
|
||||
}
|
||||
} else {
|
||||
serde_json::Value::Array(
|
||||
parsed
|
||||
@@ -436,15 +435,6 @@ fn add_transceiver<'s>(
|
||||
streams.push(id.into());
|
||||
}
|
||||
}
|
||||
SenderSlots::new(
|
||||
track
|
||||
.map(Into::into)
|
||||
.unwrap_or_else(|| v8::null(scope).into()),
|
||||
v8_string(scope, &encodings.to_string()).expect("encoding snapshot"),
|
||||
v8::Array::new_with_elements(scope, &streams),
|
||||
)
|
||||
.initialize(scope, sender)
|
||||
.expect("RTP sender slots");
|
||||
ReceiverSlots::new(receiver_track)
|
||||
.initialize(scope, receiver)
|
||||
.expect("RTP receiver slots");
|
||||
@@ -456,6 +446,22 @@ fn add_transceiver<'s>(
|
||||
)
|
||||
.initialize(scope, transceiver)
|
||||
.expect("RTP transceiver slots");
|
||||
if super::rtp_sender::initialize(
|
||||
scope,
|
||||
sender,
|
||||
super::rtp_sender::Init {
|
||||
pc,
|
||||
transceiver,
|
||||
kind: &kind,
|
||||
track,
|
||||
encodings,
|
||||
streams: &streams,
|
||||
},
|
||||
)
|
||||
.is_none()
|
||||
{
|
||||
return;
|
||||
}
|
||||
let array = list(scope, pc);
|
||||
if array.set_index(scope, array.length(), transceiver.into()) != Some(true) {
|
||||
return;
|
||||
@@ -643,7 +649,10 @@ fn queue_task<'s>(
|
||||
context_host_ptr_from_global_bridge(scope)
|
||||
.is_some_and(|host| unsafe { &mut *host }.queue_webrtc_task(scope, object, kind))
|
||||
}
|
||||
fn update_negotiation_needed<'s>(scope: &mut v8::PinScope<'s, '_>, pc: v8::Local<'s, v8::Object>) {
|
||||
pub(super) fn update_negotiation_needed<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) {
|
||||
if !closed(scope, pc) && !flag(scope, pc, NEGOTIATION_TASK) {
|
||||
let queued = queue_task(scope, pc, RendererPageWebRtcTaskKind::NegotiationNeeded);
|
||||
set_bool(scope, pc, NEGOTIATION_TASK, queued);
|
||||
@@ -669,6 +678,16 @@ pub(crate) fn apply_task<'s>(
|
||||
kind: RendererPageWebRtcTaskKind,
|
||||
) -> bool {
|
||||
let event_type = match kind {
|
||||
RendererPageWebRtcTaskKind::CreateOffer
|
||||
| RendererPageWebRtcTaskKind::SetLocalDescription
|
||||
| RendererPageWebRtcTaskKind::ReplaceTrack
|
||||
| RendererPageWebRtcTaskKind::CompleteReplaceTrack => {
|
||||
return super::operations::apply(scope, object, kind);
|
||||
}
|
||||
RendererPageWebRtcTaskKind::ClearRtpParameters
|
||||
| RendererPageWebRtcTaskKind::SetRtpParameters => {
|
||||
return super::rtp_sender::apply_parameters(scope, object, kind);
|
||||
}
|
||||
RendererPageWebRtcTaskKind::TrackEnded => {
|
||||
if !media_streams::end_track(scope, object) {
|
||||
return false;
|
||||
@@ -677,6 +696,10 @@ pub(crate) fn apply_task<'s>(
|
||||
}
|
||||
RendererPageWebRtcTaskKind::NegotiationNeeded => {
|
||||
set_bool(scope, object, NEGOTIATION_TASK, false);
|
||||
if !super::operations::is_empty(scope, object) {
|
||||
super::operations::defer_negotiation(scope, object);
|
||||
return false;
|
||||
}
|
||||
if closed(scope, object)
|
||||
|| flag(scope, object, NEGOTIATION_NEEDED)
|
||||
|| !get_private_value(
|
||||
@@ -717,7 +740,7 @@ pub(crate) fn apply_task<'s>(
|
||||
pub(super) fn offer_sections<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) -> Vec<(String, String, Vec<Codec>)> {
|
||||
) -> Vec<super::rtp_offer::Section> {
|
||||
entries(scope, pc)
|
||||
.into_iter()
|
||||
.filter_map(|object| {
|
||||
@@ -735,15 +758,19 @@ pub(super) fn offer_sections<'s>(
|
||||
.to_rust_string_lossy(scope);
|
||||
let codecs: Vec<Codec> =
|
||||
serde_json::from_str(&snapshot).expect("native codec snapshot");
|
||||
Some((
|
||||
kind.clone(),
|
||||
let sender = get_private_object(scope, object, SENDER).expect("RTP sender");
|
||||
let (streams, track) = super::rtp_sender::sdp_identity(scope, sender);
|
||||
Some(super::rtp_offer::Section {
|
||||
kind: kind.clone(),
|
||||
direction,
|
||||
if codecs.is_empty() {
|
||||
codecs: if codecs.is_empty() {
|
||||
capabilities(&kind)
|
||||
} else {
|
||||
codecs
|
||||
},
|
||||
))
|
||||
streams,
|
||||
track,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -15,7 +15,19 @@ impl PageScriptHost {
|
||||
object: v8::Local<'s, v8::Object>,
|
||||
kind: RendererPageWebRtcTaskKind,
|
||||
) -> bool {
|
||||
let Some(context) = object.get_creation_context(scope) else {
|
||||
self.queue_webrtc_task_for_owner(scope, object, object, kind)
|
||||
}
|
||||
|
||||
pub(crate) fn queue_webrtc_task_for_owner<'s>(
|
||||
&mut self,
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
owner: v8::Local<'s, v8::Object>,
|
||||
object: v8::Local<'s, v8::Object>,
|
||||
kind: RendererPageWebRtcTaskKind,
|
||||
) -> bool {
|
||||
// Payloads (including PromiseResolvers) can belong to a callee realm
|
||||
// different from the connection's exact owning Window/Document.
|
||||
let Some(context) = owner.get_creation_context(scope) else {
|
||||
return false;
|
||||
};
|
||||
let scope = &mut v8::ContextScope::new(scope, context);
|
||||
|
||||
@@ -17,6 +17,12 @@ impl RendererPageWebRtcTaskId {
|
||||
pub(crate) enum RendererPageWebRtcTaskKind {
|
||||
TrackEnded,
|
||||
NegotiationNeeded,
|
||||
CreateOffer,
|
||||
SetLocalDescription,
|
||||
ReplaceTrack,
|
||||
CompleteReplaceTrack,
|
||||
ClearRtpParameters,
|
||||
SetRtpParameters,
|
||||
}
|
||||
pub(crate) type RendererPageWebRtcOwner = RendererPageWindowDocumentTaskOwner;
|
||||
pub(crate) type RendererPageWebRtcTask =
|
||||
|
||||
@@ -46,3 +46,79 @@ async fn rtp_transceiver_tasks_cannot_dispatch_or_checkpoint_after_document_open
|
||||
Ok::<_, anyhow::Error>(())
|
||||
}).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn rtp_sender_replacement_mutates_track_then_resolves_on_distinct_networking_turns() {
|
||||
run_page_vm_async_test(async move {
|
||||
let loader = crate::network::ResourceRequestClient::new(&FetchConfig::default())?;
|
||||
let (mut page, _, _) = page_vm_with_bound_task_sources_and_owner_wake(&loader, Url::parse("https://rtp.test/sender-fifo")?);
|
||||
page.vm_mut().eval(r#"
|
||||
globalThis.trace=[];globalThis.pc=new RTCPeerConnection();
|
||||
globalThis.t=pc.addTransceiver('audio');globalThis.s=t.sender;globalThis.track=t.receiver.track;
|
||||
globalThis.id=s.getParameters().transactionId;
|
||||
s.replaceTrack(track).then(()=>trace.push('replaced'));
|
||||
pc.createOffer().then(()=>trace.push('offer'));'queued'
|
||||
"#)?;
|
||||
assert!(!page.vm().has_ready_timeout());
|
||||
// Negotiation-needed defers while the operations chain is occupied.
|
||||
assert!(page.run_exact_selected_page_task_for_test(PageSelectedTaskTestSelector::WebRtc, &loader).await?);
|
||||
assert_eq!(page.vm_mut().eval("s.track===null && trace.length===0 && s.getParameters().transactionId===id")?, "true");
|
||||
// getParameters expiry is an independent networking task, not a microtask.
|
||||
assert!(page.run_exact_selected_page_task_for_test(PageSelectedTaskTestSelector::WebRtc, &loader).await?);
|
||||
assert_eq!(page.vm_mut().eval("s.track===null && trace.length===0")?, "true");
|
||||
assert!(page.run_exact_selected_page_task_for_test(PageSelectedTaskTestSelector::WebRtc, &loader).await?);
|
||||
assert_eq!(page.vm_mut().eval("s.track===track && trace.length===0")?, "true");
|
||||
assert!(page.run_exact_selected_page_task_for_test(PageSelectedTaskTestSelector::WebRtc, &loader).await?);
|
||||
assert_eq!(page.vm_mut().eval("trace.join('|')")?, "replaced");
|
||||
assert!(page.run_exact_selected_page_task_for_test(PageSelectedTaskTestSelector::WebRtc, &loader).await?);
|
||||
assert_eq!(page.vm_mut().eval("trace.join('|')")?, "replaced|offer");
|
||||
Ok::<_, anyhow::Error>(())
|
||||
}).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn rtp_sender_retired_document_tasks_cannot_mutate_resolve_or_checkpoint() {
|
||||
run_page_vm_async_test(async move {
|
||||
let loader = crate::network::ResourceRequestClient::new(&FetchConfig::default())?;
|
||||
let (mut page, _, _) = page_vm_with_bound_task_sources_and_owner_wake(&loader, Url::parse("https://rtp.test/sender-retirement")?);
|
||||
page.vm_mut().eval(r#"
|
||||
globalThis.trace=[];globalThis.pc=new RTCPeerConnection();
|
||||
globalThis.t=pc.addTransceiver('audio');globalThis.s=t.sender;
|
||||
const params=s.getParameters();params.encodings[0].active=false;
|
||||
s.setParameters(params).then(()=>trace.push('retired parameters'));
|
||||
s.replaceTrack(t.receiver.track).then(()=>trace.push('retired replacement'));
|
||||
pc.createOffer().then(()=>trace.push('retired offer'));
|
||||
document.open();document.write('<!doctype html><body>replacement');document.close();'replaced'
|
||||
"#)?;
|
||||
page.vm_mut().eval_without_microtask_checkpoint_for_test("Promise.resolve().then(()=>trace.push('replacement microtask'));'queued'")?;
|
||||
for _ in 0..4 {
|
||||
let task = page.claim_exact_selected_page_task_for_test(PageSelectedTaskTestSelector::WebRtc).expect("retired sender tasks retain their FIFO residence");
|
||||
page.run_claimed_selected_page_task_for_test(task, &loader).await?;
|
||||
}
|
||||
assert_eq!(page.vm_mut().eval_without_microtask_checkpoint_for_test("trace.join('|')")?, "");
|
||||
assert_eq!(page.vm_mut().eval_without_microtask_checkpoint_for_test("s.track===null")?, "true");
|
||||
Ok::<_, anyhow::Error>(())
|
||||
}).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn rtp_sender_close_aborts_pending_connection_operations_without_completion() {
|
||||
run_page_vm_async_test(async move {
|
||||
let loader = crate::network::ResourceRequestClient::new(&FetchConfig::default())?;
|
||||
let (mut page, _, _) = page_vm_with_bound_task_sources_and_owner_wake(&loader, Url::parse("https://rtp.test/sender-close")?);
|
||||
page.vm_mut().eval(r#"
|
||||
globalThis.trace=[];globalThis.pc=new RTCPeerConnection();
|
||||
globalThis.t=pc.addTransceiver('audio');globalThis.s=t.sender;
|
||||
s.replaceTrack(t.receiver.track).then(()=>trace.push('replacement'),()=>trace.push('replacement rejected'));
|
||||
pc.createOffer().then(()=>trace.push('offer'),()=>trace.push('offer rejected'));
|
||||
pc.close();'closed'
|
||||
"#)?;
|
||||
for _ in 0..3 {
|
||||
let task = page.claim_exact_selected_page_task_for_test(PageSelectedTaskTestSelector::WebRtc).expect("queued closed-connection tasks");
|
||||
page.run_claimed_selected_page_task_for_test(task, &loader).await?;
|
||||
}
|
||||
assert_eq!(page.vm_mut().eval("trace.length===0 && s.track===null && t.receiver.track.readyState==='ended'")?, "true");
|
||||
assert!(!page.run_exact_selected_page_task_for_test(PageSelectedTaskTestSelector::WebRtc, &loader).await?);
|
||||
Ok::<_, anyhow::Error>(())
|
||||
}).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn webrtc_signaling_surface_stays_explicit_about_the_missing_ice_transport() {
|
||||
let mut vm = new_storage_test_vm("https://webrtc-surface.test/");
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn webrtc_signaling_surface_stays_explicit_about_the_missing_ice_transport() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
|
||||
let mut vm =
|
||||
new_storage_page_task_executor_test_vm_with_loader("https://webrtc-surface.test/", &loader);
|
||||
|
||||
vm.exec(
|
||||
r#"
|
||||
@@ -142,6 +144,14 @@ fn webrtc_signaling_surface_stays_explicit_about_the_missing_ice_transport() {
|
||||
)
|
||||
.expect("WebRTC signaling probe should execute");
|
||||
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"String(__webrtcProbe.settled)",
|
||||
"true",
|
||||
"signaling must complete through selected networking tasks",
|
||||
)
|
||||
.await;
|
||||
let result = vm
|
||||
.eval("JSON.stringify(globalThis.__webrtcProbe)")
|
||||
.expect("WebRTC signaling probe should be readable");
|
||||
|
||||
@@ -2488,6 +2488,7 @@ mod response_blob_mime;
|
||||
mod retained_child_window;
|
||||
mod rtc_certificate;
|
||||
mod rtc_encoded_frames;
|
||||
mod rtp_sender;
|
||||
mod rtp_transceivers;
|
||||
mod script_terminal_completion;
|
||||
mod service_worker_interfaces;
|
||||
|
||||
@@ -1,17 +1,26 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rtc_certificate_values_normalization_brands_and_clone_use_native_state() {
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn rtc_certificate_values_normalization_brands_and_clone_use_native_state() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
|
||||
for url in [
|
||||
"https://rtc-certificate.test/",
|
||||
"http://rtc-certificate.test/",
|
||||
"data:text/html,<body>",
|
||||
] {
|
||||
let mut vm = new_storage_page_task_executor_test_vm(url);
|
||||
let mut vm = new_storage_page_task_executor_test_vm_with_loader(url, &loader);
|
||||
vm.eval("document.body.innerHTML='<iframe></iframe>'")
|
||||
.unwrap();
|
||||
vm.eval(include_str!("../../../tests/fixtures/rtc-certificate.js"))
|
||||
.unwrap();
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"String(globalThis.__uiEventResults?.complete)",
|
||||
"true",
|
||||
"certificate offers must finish through selected networking tasks",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(vm.eval("__uiEventResults.complete").unwrap(), "true");
|
||||
assert_eq!(
|
||||
vm.eval("JSON.stringify(__uiEventResults.checks.filter(row=>!row.passed))")
|
||||
|
||||
@@ -123,9 +123,13 @@ fn rtc_configuration_applies_reentrant_updates_before_validating_the_outer_call(
|
||||
})()"#).unwrap(), "true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rtc_configuration_cannot_resize_the_pool_after_setting_a_local_description() {
|
||||
let mut vm = new_storage_page_task_executor_test_vm("https://rtc-configuration.test/");
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn rtc_configuration_cannot_resize_the_pool_after_setting_a_local_description() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
|
||||
let mut vm = new_storage_page_task_executor_test_vm_with_loader(
|
||||
"https://rtc-configuration.test/",
|
||||
&loader,
|
||||
);
|
||||
vm.eval(r#"globalThis.pc = new RTCPeerConnection({iceCandidatePoolSize:2});
|
||||
globalThis.configResult = false;
|
||||
pc.createOffer().then(offer => pc.setLocalDescription(offer)).then(() => {
|
||||
@@ -135,5 +139,13 @@ fn rtc_configuration_cannot_resize_the_pool_after_setting_a_local_description()
|
||||
catch (error) {configResult=error.name === 'InvalidModificationError' && pc.getConfiguration().iceCandidatePoolSize === 2 && before !== JSON.stringify(pc.getConfiguration());}
|
||||
pc.close();
|
||||
});"#).unwrap();
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"String(configResult)",
|
||||
"true",
|
||||
"local-description updates must finish through selected networking tasks",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(vm.eval("configResult").unwrap(), "true");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
use super::*;
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn rtp_sender_parameters_tracks_streams_and_operations_use_native_state() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
|
||||
for url in [
|
||||
"https://rtp-sender.test/",
|
||||
"http://rtp-sender.test/",
|
||||
"data:text/html,<body>",
|
||||
] {
|
||||
let mut vm = new_storage_page_task_executor_test_vm_with_loader(url, &loader);
|
||||
vm.eval("document.body.innerHTML='<iframe></iframe>'")
|
||||
.unwrap();
|
||||
vm.eval(include_str!("../../../tests/fixtures/rtp-sender.js"))
|
||||
.unwrap();
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"String(globalThis.__uiEventResults?.complete)",
|
||||
"true",
|
||||
"sender promises must finish through the production selected Page dispatcher",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
vm.eval("JSON.stringify(__uiEventResults.checks.filter(row=>!row.passed))")
|
||||
.unwrap(),
|
||||
"[]",
|
||||
"{url}"
|
||||
);
|
||||
assert_eq!(vm.eval("__uiEventResults.total===164 && new Set(__uiEventResults.checks.map(row=>row.name)).size===164").unwrap(), "true");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn rtp_sender_registered_native_proxies_keep_identity_before_argument_conversion() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
|
||||
let mut vm =
|
||||
new_storage_page_task_executor_test_vm_with_loader("https://rtp-proxy.test/", &loader);
|
||||
vm.eval("globalThis.pc=new RTCPeerConnection();globalThis.sender=pc.addTransceiver('audio').sender;globalThis.track=pc.addTransceiver('audio').receiver.track;globalThis.stream=new MediaStream()").unwrap();
|
||||
let context_ptr = &vm.current_main_world().unwrap().context as *const _;
|
||||
vm.with_context_scope_by_ptr_and_checkpoint_for_test(context_ptr, |scope, _| {
|
||||
let global = scope.get_current_context().global(scope);
|
||||
for (original, native) in [
|
||||
("pc", "nativePC"),
|
||||
("sender", "nativeSender"),
|
||||
("track", "nativeTrack"),
|
||||
("stream", "nativeStream"),
|
||||
] {
|
||||
let key = crate::util::v8_string(scope, original).unwrap();
|
||||
let object =
|
||||
v8::Local::<v8::Object>::try_from(global.get(scope, key.into()).unwrap()).unwrap();
|
||||
let handler = crate::util::new_null_prototype_object(scope);
|
||||
let proxy = v8::Proxy::new(scope, object, handler).unwrap();
|
||||
moli_webapi_declare::register_web_api_proxy(scope, proxy).unwrap();
|
||||
let key = crate::util::v8_string(scope, native).unwrap();
|
||||
assert_eq!(
|
||||
global.create_data_property(scope, key.into(), proxy.into()),
|
||||
Some(true)
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
vm.eval(r#"globalThis.nativeProxyDone=false;(async()=>{
|
||||
const p=nativeSender.getParameters();p.encodings[0].active=false;await nativeSender.setParameters(p);
|
||||
if(sender.getParameters().encodings[0].active)throw Error('native sender update');
|
||||
await nativeSender.replaceTrack(nativeTrack);if(sender.track!==nativeTrack)throw Error('native track identity');
|
||||
nativeSender.setStreams(nativeStream,nativeStream);
|
||||
const offer=await nativePC.createOffer();if(offer.sdp.split('a=msid:'+stream.id+' ').length!==2)throw Error('native stream ID');
|
||||
let reads=0;try{await RTCRtpSender.prototype.setParameters.call(new Proxy(nativeSender,{}),{get codecs(){reads++;return [];}});throw Error('author proxy accepted');}catch(e){if(!(e instanceof TypeError)||reads!==0)throw e;}
|
||||
nativePC.close();if(pc.signalingState!=='closed')throw Error('native PC state');nativeProxyDone=true;
|
||||
})().catch(e=>globalThis.nativeProxyError=String(e));"#).unwrap();
|
||||
advance_page_task_executor_until_eval_equals(
|
||||
&mut vm,
|
||||
&loader,
|
||||
"String(nativeProxyDone||!!globalThis.nativeProxyError)",
|
||||
"true",
|
||||
"native proxy operations must complete through networking tasks",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
vm.eval("String(globalThis.nativeProxyError??'')").unwrap(),
|
||||
""
|
||||
);
|
||||
assert_eq!(vm.eval("nativeProxyDone").unwrap(), "true");
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
(async () => {
|
||||
const checks=[];
|
||||
const assert=(ok,message='assertion failed')=>{if(!ok)throw Error(message);};
|
||||
const equal=(a,b)=>assert(JSON.stringify(a)===JSON.stringify(b),JSON.stringify({actual:a,expected:b}));
|
||||
const raises=(f,C,name)=>{let e;try{f();}catch(error){e=error;}assert(e instanceof C&&(!name||e.name===name),'expected '+(name||C.name)+', got '+e);};
|
||||
const rejects=async(f,C,name)=>{let p;try{p=f();}catch(e){throw Error('synchronous throw: '+e);}assert(p&&typeof p.then==='function');let e;try{await p;}catch(error){e=error;}assert(e instanceof C&&(!name||e.name===name),'expected rejection '+(name||C.name)+', got '+e);};
|
||||
for(const [index,w] of [window,document.querySelector('iframe').contentWindow].entries()) {
|
||||
const P=w.RTCPeerConnection,S=w.RTCRtpSender;
|
||||
const check=async(name,action)=>{const key=index+':'+name;globalThis.__codecCurrentCheck=key;try{await action();checks.push({name:key,passed:true});}catch(error){checks.push({name:key,passed:false,error:{name:error.name,message:error.message}});}};
|
||||
const withPC=async(action)=>{const pc=new P();try{await action(pc);}finally{pc.close();}};
|
||||
const sender=(pc,kind='audio',init)=>pc.addTransceiver(kind,init).sender;
|
||||
for(const [name,length] of [['getParameters',0],['setParameters',1],['replaceTrack',1],['setStreams',0]])await check(name+' descriptor',()=>{const d=Object.getOwnPropertyDescriptor(S.prototype,name);assert(d.enumerable&&d.configurable&&d.writable&&typeof d.value==='function'&&d.value.name===name&&d.value.length===length);});
|
||||
for(const kind of ['audio','video']) {
|
||||
await check(kind+' initial native parameters',()=>withPC(pc=>{const s=sender(pc,kind),p=s.getParameters();assert(p instanceof w.Object&&p.encodings instanceof w.Array&&p.rtcp instanceof w.Object);assert(typeof p.transactionId==='string'&&p.transactionId.length>0);equal(p.codecs,[]);equal(p.headerExtensions,[]);assert(p.encodings.length===1&&p.encodings[0].active===true&&p.rtcp.reducedSize===false);if(kind==='audio')assert(!('scaleResolutionDownBy' in p.encodings[0]));}));
|
||||
await check(kind+' independent returned objects stable same-task transaction',()=>withPC(pc=>{const s=sender(pc,kind),a=s.getParameters(),b=s.getParameters();assert(a!==b&&a.encodings!==b.encodings&&a.encodings[0]!==b.encodings[0]&&a.rtcp!==b.rtcp&&a.transactionId===b.transactionId);a.encodings[0].active=false;a.rtcp.cname='forged';const c=s.getParameters();assert(c.encodings[0].active===true&&c.rtcp.cname!=='forged');}));
|
||||
await check(kind+' transaction survives promise microtasks',()=>withPC(async pc=>{const s=sender(pc,kind),a=s.getParameters().transactionId;await Promise.resolve();await Promise.resolve();assert(s.getParameters().transactionId===a);}));
|
||||
await check(kind+' transaction expires across createOffer networking task',()=>withPC(async pc=>{const s=sender(pc,kind),old=s.getParameters();await pc.createOffer();assert(old.transactionId!==s.getParameters().transactionId);await rejects(()=>s.setParameters(old),w.DOMException,'InvalidModificationError');}));
|
||||
await check(kind+' setParameters unchanged',()=>withPC(async pc=>{const s=sender(pc,kind),p=s.getParameters();assert(await s.setParameters(p)===undefined);assert(p.transactionId!==s.getParameters().transactionId);}));
|
||||
await check(kind+' setParameters local encoding update',()=>withPC(async pc=>{const s=sender(pc,kind),p=s.getParameters();p.encodings[0].active=false;p.encodings[0].maxBitrate=64000;let settled=false;const promise=s.setParameters(p);promise.then(()=>settled=true);assert(!settled&&s.getParameters().encodings[0].active);await promise;const next=s.getParameters();assert(settled&&!next.encodings[0].active&&next.encodings[0].maxBitrate===64000);}));
|
||||
await check(kind+' converted snapshot ignores later input mutation',()=>withPC(async pc=>{const s=sender(pc,kind),p=s.getParameters();p.encodings[0].active=false;const done=s.setParameters(p);p.encodings[0].active=true;p.encodings[0].maxBitrate=1;await done;assert(s.getParameters().encodings[0].active===false&&!('maxBitrate' in s.getParameters().encodings[0]));}));
|
||||
await check(kind+' optional maxBitrate can be removed',()=>withPC(async pc=>{const s=sender(pc,kind);let p=s.getParameters();p.encodings[0].maxBitrate=100;await s.setParameters(p);p=s.getParameters();delete p.encodings[0].maxBitrate;await s.setParameters(p);assert(!('maxBitrate' in s.getParameters().encodings[0]));}));
|
||||
for(const [name,modify] of [['transaction',p=>p.transactionId='forged'],['codecs',p=>p.codecs=[{mimeType:'audio/opus',clockRate:48000,payloadType:111,channels:2}]],['header extensions',p=>p.headerExtensions=[{id:1,uri:'urn:forged'}]],['RTCP CNAME',p=>p.rtcp.cname='forged'],['RTCP reducedSize',p=>p.rtcp.reducedSize=true],['add encoding',p=>p.encodings.push({active:true})],['remove encoding',p=>p.encodings=[]],['add RID',p=>p.encodings[0].rid='a']])await check(kind+' immutable '+name,()=>withPC(async pc=>{const s=sender(pc,kind),p=s.getParameters();modify(p);await rejects(()=>s.setParameters(p),w.DOMException,'InvalidModificationError');}));
|
||||
await check(kind+' parameters require a fresh transaction',()=>withPC(async pc=>{const s=sender(pc,kind);await rejects(()=>s.setParameters({transactionId:'missing',codecs:[],headerExtensions:[],rtcp:{},encodings:[{}]}),w.DOMException,'InvalidStateError');}));
|
||||
await check(kind+' stopped sender rejects after conversion',()=>withPC(async pc=>{const t=pc.addTransceiver(kind),s=t.sender,p=s.getParameters();t.stop();await rejects(()=>s.setParameters(p),w.DOMException,'InvalidStateError');const sentinel={};try{await s.setParameters({get codecs(){throw sentinel;}});assert(false);}catch(e){assert(e===sentinel);}assert(s.getParameters().encodings.length===1);}));
|
||||
for(const field of ['maxFramerate','scaleResolutionDownBy'])for(const value of [Infinity,-Infinity,NaN])await check(kind+' finite '+field+' '+String(value),()=>withPC(async pc=>{const s=sender(pc,kind),p=s.getParameters();p.encodings[0][field]=value;await rejects(()=>s.setParameters(p),w.TypeError);}));
|
||||
await check(kind+' getter exceptions preserve identity in rejected promises',()=>withPC(async pc=>{const s=sender(pc,kind),sentinel={};const p=s.setParameters({get codecs(){throw sentinel;}});assert(p instanceof w.Promise);try{await p;assert(false);}catch(e){assert(e===sentinel);}}));
|
||||
}
|
||||
await check('video default scale is one',()=>withPC(pc=>{assert(sender(pc,'video').getParameters().encodings[0].scaleResolutionDownBy===1);}));
|
||||
await check('video accepts zero maxFramerate',()=>withPC(async pc=>{const s=sender(pc,'video'),p=s.getParameters();p.encodings[0].maxFramerate=0;await s.setParameters(p);assert(s.getParameters().encodings[0].maxFramerate===0);}));
|
||||
for(const [field,value] of [['scaleResolutionDownBy',.5],['scaleResolutionDownBy',0],['maxFramerate',-1]])await check('video range '+field+' '+value,()=>withPC(async pc=>{const s=sender(pc,'video'),p=s.getParameters();p.encodings[0][field]=value;await rejects(()=>s.setParameters(p),w.RangeError);}));
|
||||
await check('audio ignores video-only encoding constraints',()=>withPC(async pc=>{const s=sender(pc),p=s.getParameters();p.encodings[0].maxFramerate=-1;p.encodings[0].scaleResolutionDownBy=.5;await s.setParameters(p);const e=s.getParameters().encodings[0];assert(!('maxFramerate' in e)&&!('scaleResolutionDownBy' in e));}));
|
||||
await check('video encodings retain RID order',()=>withPC(async pc=>{const s=sender(pc,'video',{sendEncodings:[{rid:'a'},{rid:'b'}]}),p=s.getParameters();equal(p.encodings.map(e=>e.rid),['a','b']);p.encodings.reverse();await rejects(()=>s.setParameters(p),w.DOMException,'InvalidModificationError');}));
|
||||
await check('one connection shares RTCP CNAME',()=>withPC(pc=>{assert(sender(pc).getParameters().rtcp.cname===sender(pc,'video').getParameters().rtcp.cname);}));
|
||||
await check('setParameters required dictionaries and empty options conversion',()=>withPC(async pc=>{const s=sender(pc);for(const value of [undefined,null,{},[],{codecs:[]},{codecs:[],headerExtensions:[],rtcp:{},encodings:[]}])await rejects(()=>s.setParameters(value),w.TypeError);await rejects(()=>s.setParameters(),w.TypeError);await rejects(()=>s.setParameters(s.getParameters(),1),w.TypeError);}));
|
||||
await check('setParameters dictionary conversion order',()=>withPC(async pc=>{const s=sender(pc),base=s.getParameters(),reads=[],p={};for(const key of ['codecs','headerExtensions','rtcp','encodings','transactionId'])Object.defineProperty(p,key,{get(){reads.push(key);return base[key];}});await s.setParameters(p);equal(reads,['codecs','headerExtensions','rtcp','encodings','transactionId']);}));
|
||||
for(const kind of ['audio','video'])await check(kind+' same-kind replaceTrack and stable old tracks',()=>withPC(async pc=>{const source=pc.addTransceiver(kind).receiver.track,s=sender(pc,kind);let settled=false;const promise=s.replaceTrack(source);promise.then(()=>settled=true);assert(s.track===null&&!settled);await promise;assert(s.track===source&&source.readyState==='live');const newer=source.clone();await s.replaceTrack(newer);assert(s.track===newer&&source.readyState==='live'&&newer.readyState==='live');await s.replaceTrack(null);assert(s.track===null&&newer.readyState==='live');}));
|
||||
await check('replaceTrack explicit undefined is nullable, missing rejected',()=>withPC(async pc=>{const s=sender(pc);await s.replaceTrack(undefined);assert(s.track===null);await rejects(()=>s.replaceTrack(),w.TypeError);}));
|
||||
await check('replaceTrack wrong kind precedes closed state',async()=>{const pc=new P(),audio=pc.addTransceiver('audio'),video=pc.addTransceiver('video');pc.close();await rejects(()=>audio.sender.replaceTrack(video.receiver.track),w.TypeError);await rejects(()=>audio.sender.replaceTrack(audio.receiver.track),w.DOMException,'InvalidStateError');});
|
||||
await check('replaceTrack stopped sender rejects',()=>withPC(async pc=>{const t=pc.addTransceiver('audio');t.stop();await rejects(()=>t.sender.replaceTrack(null),w.DOMException,'InvalidStateError');}));
|
||||
await check('replaceTrack accepts ended native tracks',()=>withPC(async pc=>{const t=pc.addTransceiver('audio'),s=sender(pc),track=t.receiver.track;track.stop();await s.replaceTrack(track);assert(s.track===track&&track.readyState==='ended');}));
|
||||
await check('replaceTrack sequential operations complete in order',()=>withPC(async pc=>{const s=sender(pc),a=pc.addTransceiver('audio').receiver.track,b=a.clone(),events=[];const first=s.replaceTrack(a).then(()=>{assert(s.track===a);events.push('first');});const second=s.replaceTrack(b).then(()=>{assert(s.track===b);events.push('second');});const offer=pc.createOffer().then(()=>events.push('offer'));await Promise.all([first,second,offer]);equal(events,['first','second','offer']);}));
|
||||
await check('replaceTrack preserves SDP track identity',()=>withPC(async pc=>{const original=pc.addTransceiver('audio').receiver.track,t=pc.addTransceiver(original),s=t.sender;const before=(await pc.createOffer()).sdp;await s.replaceTrack(original.clone());const after=(await pc.createOffer()).sdp;equal(before.split('\r\n').filter(l=>l.startsWith('a=msid:')),after.split('\r\n').filter(l=>l.startsWith('a=msid:')));}));
|
||||
await check('setStreams deduplicates native IDs and updates SDP',()=>withPC(async pc=>{const track=pc.addTransceiver('audio').receiver.track,s=pc.addTransceiver(track).sender,a=new w.MediaStream(),b=new w.MediaStream(),aId=a.id,bId=b.id;s.setStreams(a,a,b);const offer=await pc.createOffer(),lines=offer.sdp.split('\r\n').filter(l=>l.startsWith('a=msid:')&&(l.includes(aId)||l.includes(bId)));assert(lines.length===2&&lines[0]==='a=msid:'+aId+' '+track.id&&lines[1]==='a=msid:'+bId+' '+track.id);s.setStreams();const cleared=(await pc.createOffer()).sdp;assert(!cleared.includes(aId)&&!cleared.includes(bId)&&cleared.includes('a=msid:- '+track.id));}));
|
||||
await check('setStreams uses native ID rather than author getter',()=>withPC(async pc=>{const track=pc.addTransceiver('audio').receiver.track,s=pc.addTransceiver(track).sender,stream=new w.MediaStream(),id=stream.id;Object.defineProperty(stream,'id',{get(){throw Error('author id getter');}});s.setStreams(stream);assert((await pc.createOffer()).sdp.includes('a=msid:'+id+' '));}));
|
||||
await check('setStreams variadic brands before closed state',async()=>{const pc=new P(),s=sender(pc),stream=new w.MediaStream();pc.close();raises(()=>s.setStreams(stream),w.DOMException,'InvalidStateError');for(const bad of [[],[stream],{},Object.create(stream),new Proxy(stream,{})])raises(()=>s.setStreams(bad),w.TypeError);});
|
||||
await check('sender receiver brands and callee-realm promise rejections',()=>withPC(async pc=>{const s=sender(pc);for(const fake of [{},Object.create(S.prototype),Object.create(s),new Proxy(s,{})]){raises(()=>S.prototype.getParameters.call(fake),w.TypeError);raises(()=>S.prototype.setStreams.call(fake),w.TypeError);for(const name of ['setParameters','replaceTrack']){const p=S.prototype[name].call(fake);assert(p instanceof w.Promise);await rejects(()=>p,w.TypeError);}}}));
|
||||
await check('author proxy traps are not used for receiver or track brands',()=>withPC(async pc=>{const t=pc.addTransceiver('audio'),s=t.sender;let traps=0;const handler={get(){traps++;throw Error('proxy trap');},getPrototypeOf(){traps++;throw Error('prototype trap');}};await rejects(()=>S.prototype.replaceTrack.call(new Proxy(s,handler),null),w.TypeError);await rejects(()=>s.replaceTrack(new Proxy(t.receiver.track,handler)),w.TypeError);raises(()=>s.setStreams(new Proxy(new w.MediaStream(),handler)),w.TypeError);assert(traps===0);}));
|
||||
await check('revoked native-looking proxies are rejected',()=>withPC(async pc=>{const t=pc.addTransceiver('audio'),s=t.sender,r=Proxy.revocable(t.receiver.track,{});r.revoke();await rejects(()=>s.replaceTrack(r.proxy),w.TypeError);const q=Proxy.revocable(s,{});q.revoke();await rejects(()=>S.prototype.setParameters.call(q.proxy,{}),w.TypeError);}));
|
||||
await check('createOffer conversion errors reject in callee realm',()=>withPC(async pc=>{let reads=0;await rejects(()=>P.prototype.createOffer.call({}, {get iceRestart(){reads++;return false;}}),w.TypeError);assert(reads===0);const sentinel={};try{await pc.createOffer({get offerToReceiveAudio(){throw sentinel;}});assert(false);}catch(e){assert(e===sentinel);}await rejects(()=>pc.createOffer(1),w.TypeError);}));
|
||||
await check('local description dictionary copied before networking task',()=>withPC(async pc=>{sender(pc);const offer=await pc.createOffer(),input={...offer};const done=pc.setLocalDescription(input);input.type='rollback';input.sdp='forged';assert(pc.localDescription===null);await done;assert(pc.localDescription instanceof w.RTCSessionDescription&&pc.localDescription.type==='offer'&&pc.localDescription.sdp===offer.sdp&&pc.localDescription!==input);}));
|
||||
await check('cross-realm genuine sender uses the callee realm',()=>withPC(async pc=>{const s=sender(pc),other=index===0?document.querySelector('iframe').contentWindow:window,api=other.RTCRtpSender.prototype,p=api.getParameters.call(s);assert(p instanceof other.Object&&p.encodings instanceof other.Array);const done=api.setParameters.call(s,p);assert(done instanceof other.Promise);await done;const track=pc.addTransceiver('audio').receiver.track,replaced=api.replaceTrack.call(s,track);assert(replaced instanceof other.Promise);await replaced;assert(s.track===track);api.setStreams.call(s,new other.MediaStream());}));
|
||||
await check('implicit local offer and rollback',()=>withPC(async pc=>{sender(pc);await pc.setLocalDescription();assert(pc.signalingState==='have-local-offer'&&pc.localDescription.type==='offer');await pc.setLocalDescription({type:'rollback'});assert(pc.signalingState==='stable'&&pc.localDescription===null&&pc.pendingLocalDescription===null);}));
|
||||
}
|
||||
globalThis.__uiEventResults={complete:true,total:checks.length,passed:checks.filter(row=>row.passed).length,checks};
|
||||
return checks.every(row=>row.passed);
|
||||
})()
|
||||
@@ -12,6 +12,7 @@ use moli_webidl::{self as webidl, WebIdlDictionarySerialize};
|
||||
|
||||
pub mod certificate;
|
||||
mod configuration;
|
||||
pub mod operations;
|
||||
mod peer_options;
|
||||
pub mod rtp_capabilities;
|
||||
pub mod rtp_transceivers;
|
||||
@@ -134,9 +135,9 @@ struct RtcPeerConnectionPrototypeDeclaration {
|
||||
set_configuration: (),
|
||||
#[webapi(method = "createDataChannel", length = 1, callback = rtc_peer_connection_create_data_channel_callback)]
|
||||
create_data_channel: (),
|
||||
#[webapi(method = "createOffer", length = 0, callback = rtc_peer_connection_create_offer_callback, returns_promise)]
|
||||
#[webapi(method = "createOffer", returns_promise, length = 0, callback = rtc_peer_connection_create_offer_callback)]
|
||||
create_offer: (),
|
||||
#[webapi(method = "setLocalDescription", length = 0, callback = rtc_peer_connection_set_local_description_callback, returns_promise)]
|
||||
#[webapi(method = "setLocalDescription", returns_promise, length = 0, callback = rtc_peer_connection_set_local_description_callback)]
|
||||
set_local_description: (),
|
||||
#[webapi(method, length = 0, callback = rtc_peer_connection_close_callback)]
|
||||
close: (),
|
||||
@@ -223,7 +224,10 @@ pub fn rtc_peer_connection_constructor_callback<'s>(
|
||||
rv.set_undefined();
|
||||
return;
|
||||
}
|
||||
rtp_transceivers::initialize_pc(scope, args.this());
|
||||
if rtp_transceivers::initialize_pc(scope, args.this()).is_none() {
|
||||
return;
|
||||
}
|
||||
operations::initialize(scope, args.this());
|
||||
rv.set(args.this().into());
|
||||
}
|
||||
|
||||
@@ -241,9 +245,8 @@ fn rtc_peer_connection_state_getter<'s>(
|
||||
rv.set_undefined();
|
||||
return;
|
||||
};
|
||||
rv.set(
|
||||
get_private_value(scope, args.this(), slot).unwrap_or_else(|| v8::undefined(scope).into()),
|
||||
);
|
||||
let target = rtp_transceivers::target(scope, args.this());
|
||||
rv.set(get_private_value(scope, target, slot).unwrap_or_else(|| v8::undefined(scope).into()));
|
||||
}
|
||||
|
||||
fn rtc_peer_connection_description_getter<'s>(
|
||||
@@ -260,7 +263,8 @@ fn rtc_peer_connection_description_getter<'s>(
|
||||
rv.set_null();
|
||||
return;
|
||||
};
|
||||
rv.set(get_private_value(scope, args.this(), slot).unwrap_or_else(|| v8::null(scope).into()));
|
||||
let target = rtp_transceivers::target(scope, args.this());
|
||||
rv.set(get_private_value(scope, target, slot).unwrap_or_else(|| v8::null(scope).into()));
|
||||
}
|
||||
|
||||
fn ensure_peer_open<'s>(scope: &mut v8::PinScope<'s, '_>, peer: v8::Local<'s, v8::Object>) -> bool {
|
||||
@@ -284,7 +288,8 @@ fn rtc_peer_connection_create_data_channel_callback<'s>(
|
||||
let Some(parsed) = webidl::parse_args::<peer_options::DataChannelArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
if !ensure_peer_open(scope, args.this()) {
|
||||
let target = rtp_transceivers::target(scope, args.this());
|
||||
if !ensure_peer_open(scope, target) {
|
||||
return;
|
||||
}
|
||||
let options = &parsed.options;
|
||||
@@ -305,7 +310,7 @@ fn rtc_peer_connection_create_data_channel_callback<'s>(
|
||||
};
|
||||
set_private_value(
|
||||
scope,
|
||||
args.this(),
|
||||
target,
|
||||
RTC_PEER_CONNECTION_HAS_DATA_CHANNEL_SLOT,
|
||||
v8::Boolean::new(scope, true).into(),
|
||||
);
|
||||
@@ -320,7 +325,8 @@ fn rtc_peer_connection_create_offer_callback<'s>(
|
||||
let Some(parsed) = webidl::parse_args::<peer_options::OfferArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
if !ensure_peer_open(scope, args.this()) {
|
||||
let target = rtp_transceivers::target(scope, args.this());
|
||||
if !ensure_peer_open(scope, target) {
|
||||
return;
|
||||
}
|
||||
let audio = parsed.options.offer_to_receive_audio.unwrap_or(-1);
|
||||
@@ -333,33 +339,54 @@ fn rtc_peer_connection_create_offer_callback<'s>(
|
||||
);
|
||||
return;
|
||||
}
|
||||
let data = get_private_value(
|
||||
scope,
|
||||
args.this(),
|
||||
RTC_PEER_CONNECTION_HAS_DATA_CHANNEL_SLOT,
|
||||
)
|
||||
.is_some_and(|value| value.boolean_value(scope));
|
||||
let target = moli_webapi_declare::web_api_object_target(scope, args.this())
|
||||
.expect("RTCPeerConnection receiver");
|
||||
let options = i32::from(audio == 1) | (i32::from(video == 1) << 1);
|
||||
let payload = v8::Integer::new(scope, options);
|
||||
if let Some(promise) =
|
||||
operations::enqueue(scope, target, operations::Kind::CreateOffer, payload.into())
|
||||
{
|
||||
rv.set(promise.into());
|
||||
}
|
||||
}
|
||||
|
||||
fn offer_sdp<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
target: v8::Local<'s, v8::Object>,
|
||||
audio: bool,
|
||||
video: bool,
|
||||
) -> String {
|
||||
let data = get_private_value(scope, target, RTC_PEER_CONNECTION_HAS_DATA_CHANNEL_SLOT)
|
||||
.expect("data channel flag")
|
||||
.boolean_value(scope);
|
||||
let mut sdp = rtp_transceivers::create_offer(scope, target, data)
|
||||
.unwrap_or_else(|| build_signaling_only_offer(audio == 1, video == 1, data));
|
||||
.unwrap_or_else(|| build_signaling_only_offer(audio, video, data));
|
||||
let config = configuration::configuration(scope, target);
|
||||
// Fingerprints are session-level attributes and therefore cover every
|
||||
// bundled media section. Author-visible certificate getters are not read.
|
||||
let config = moli_webapi_declare::WebApiValue::to_v8_value(&config, scope)
|
||||
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
|
||||
.expect("native config record");
|
||||
let mut fingerprints = String::new();
|
||||
certificate::connection_fingerprints(scope, config, &mut fingerprints);
|
||||
let at = sdp.find("m=").unwrap_or(sdp.len());
|
||||
sdp.insert_str(at, &fingerprints);
|
||||
sdp
|
||||
}
|
||||
pub fn complete_create_offer<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) -> bool {
|
||||
let options = operations::payload(scope, request)
|
||||
.int32_value(scope)
|
||||
.expect("native offer options");
|
||||
let sdp = offer_sdp(scope, pc, options & 1 != 0, options & 2 != 0);
|
||||
let Some(sdp) = v8_string(scope, &sdp) else {
|
||||
rv.set_undefined();
|
||||
return;
|
||||
return false;
|
||||
};
|
||||
let offer = RtcSessionDescriptionInitDeclaration::new(v8str(scope, "offer").into(), sdp)
|
||||
.bind(scope)
|
||||
.expect("RTC offer declaration should bind");
|
||||
set_resolved_promise(scope, &mut rv, offer.into());
|
||||
.expect("offer dictionary");
|
||||
operations::resolve(scope, request, offer.into());
|
||||
true
|
||||
}
|
||||
|
||||
fn rtc_peer_connection_set_local_description_callback<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
@@ -369,50 +396,127 @@ fn rtc_peer_connection_set_local_description_callback<'s>(
|
||||
else {
|
||||
return;
|
||||
};
|
||||
if !ensure_peer_open(scope, args.this()) {
|
||||
return;
|
||||
}
|
||||
let pc = rtp_transceivers::target(scope, args.this());
|
||||
let sdp = parsed.description.sdp.into_value(scope);
|
||||
let kind = parsed
|
||||
.description
|
||||
.r#type
|
||||
.unwrap_or(session_description::SdpType::Offer);
|
||||
let mut sdp = parsed.description.sdp.into_value(scope);
|
||||
if kind == session_description::SdpType::Offer && sdp.length() == 0 {
|
||||
let data = get_private_value(
|
||||
scope,
|
||||
args.this(),
|
||||
RTC_PEER_CONNECTION_HAS_DATA_CHANNEL_SLOT,
|
||||
)
|
||||
.is_some_and(|value| value.is_true());
|
||||
sdp = v8_string(scope, &build_signaling_only_offer(false, false, data))
|
||||
.expect("default offer SDP");
|
||||
.map(|kind| v8str(scope, webidl::WebIdlEnumSnapshot::snapshot_token(&kind)))
|
||||
.unwrap_or_else(|| v8str(scope, ""));
|
||||
// Copy the dictionary now, before entering the operations chain. Author
|
||||
// getters and subsequent mutations never run inside a networking task.
|
||||
let snapshot = RtcSessionDescriptionInitDeclaration::new(kind.into(), sdp)
|
||||
.bind(scope)
|
||||
.expect("local description snapshot");
|
||||
if let Some(promise) = operations::enqueue(
|
||||
scope,
|
||||
pc,
|
||||
operations::Kind::SetLocalDescription,
|
||||
snapshot.into(),
|
||||
) {
|
||||
rv.set(promise.into());
|
||||
}
|
||||
let Some(description) = session_description::native_description(scope, kind, sdp) else {
|
||||
return;
|
||||
}
|
||||
pub fn complete_set_local_description<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) -> bool {
|
||||
let snapshot = v8::Local::<v8::Object>::try_from(operations::payload(scope, request))
|
||||
.expect("description snapshot");
|
||||
let kind = snapshot
|
||||
.get(scope, v8str(scope, "type").into())
|
||||
.expect("copied type")
|
||||
.to_rust_string_lossy(scope);
|
||||
let mut sdp = v8::Local::<v8::String>::try_from(
|
||||
snapshot
|
||||
.get(scope, v8str(scope, "sdp").into())
|
||||
.expect("copied SDP"),
|
||||
)
|
||||
.expect("SDP string");
|
||||
let kind = if kind.is_empty() {
|
||||
"offer"
|
||||
} else {
|
||||
kind.as_str()
|
||||
};
|
||||
set_private_value(
|
||||
scope,
|
||||
args.this(),
|
||||
RTC_PEER_CONNECTION_LOCAL_DESCRIPTION_SLOT,
|
||||
description.into(),
|
||||
);
|
||||
set_private_value(
|
||||
scope,
|
||||
args.this(),
|
||||
RTC_PEER_CONNECTION_PENDING_LOCAL_DESCRIPTION_SLOT,
|
||||
description.into(),
|
||||
);
|
||||
set_string_slot(
|
||||
scope,
|
||||
args.this(),
|
||||
RTC_PEER_CONNECTION_SIGNALING_STATE_SLOT,
|
||||
"have-local-offer",
|
||||
);
|
||||
// Moli has no UDP/STUN/DTLS transport. Keep the ICE gatherer in its
|
||||
// initial state and publish no candidate events rather than fabricating
|
||||
// host/server-reflexive addresses or synchronously completing an
|
||||
// operation that Chromium starts on a later networking task.
|
||||
set_resolved_promise(scope, &mut rv, v8::undefined(scope).into());
|
||||
let state = get_private_value(scope, pc, RTC_PEER_CONNECTION_SIGNALING_STATE_SLOT)
|
||||
.expect("signaling state")
|
||||
.to_rust_string_lossy(scope);
|
||||
if !(kind == "offer" && matches!(state.as_str(), "stable" | "have-local-offer")
|
||||
|| kind == "rollback" && state == "have-local-offer")
|
||||
{
|
||||
v8::tc_scope!(let caught, scope);
|
||||
crate::embedding::throw_dom_exception_value(
|
||||
caught,
|
||||
"The description is incompatible with the signaling state.",
|
||||
"InvalidStateError",
|
||||
);
|
||||
let reason = caught.exception().expect("description error");
|
||||
caught.reset();
|
||||
operations::reject(caught, request, reason);
|
||||
return true;
|
||||
}
|
||||
if kind == "rollback" {
|
||||
set_private_value(
|
||||
scope,
|
||||
pc,
|
||||
RTC_PEER_CONNECTION_PENDING_LOCAL_DESCRIPTION_SLOT,
|
||||
v8::null(scope).into(),
|
||||
);
|
||||
let current = get_private_value(
|
||||
scope,
|
||||
pc,
|
||||
RTC_PEER_CONNECTION_CURRENT_LOCAL_DESCRIPTION_SLOT,
|
||||
)
|
||||
.expect("current local description");
|
||||
set_private_value(
|
||||
scope,
|
||||
pc,
|
||||
RTC_PEER_CONNECTION_LOCAL_DESCRIPTION_SLOT,
|
||||
current,
|
||||
);
|
||||
set_string_slot(
|
||||
scope,
|
||||
pc,
|
||||
RTC_PEER_CONNECTION_SIGNALING_STATE_SLOT,
|
||||
"stable",
|
||||
);
|
||||
} else {
|
||||
if sdp.length() == 0 {
|
||||
let text = offer_sdp(scope, pc, false, false);
|
||||
let Some(value) = v8_string(scope, &text) else {
|
||||
return false;
|
||||
};
|
||||
sdp = value;
|
||||
}
|
||||
let Some(kind) = v8_string(scope, kind) else {
|
||||
return false;
|
||||
};
|
||||
let Some(description) = session_description::from_parts(scope, kind, sdp) else {
|
||||
return false;
|
||||
};
|
||||
set_private_value(
|
||||
scope,
|
||||
pc,
|
||||
RTC_PEER_CONNECTION_LOCAL_DESCRIPTION_SLOT,
|
||||
description.into(),
|
||||
);
|
||||
set_private_value(
|
||||
scope,
|
||||
pc,
|
||||
RTC_PEER_CONNECTION_PENDING_LOCAL_DESCRIPTION_SLOT,
|
||||
description.into(),
|
||||
);
|
||||
set_string_slot(
|
||||
scope,
|
||||
pc,
|
||||
RTC_PEER_CONNECTION_SIGNALING_STATE_SLOT,
|
||||
"have-local-offer",
|
||||
);
|
||||
}
|
||||
// No ICE/DTLS transport or fabricated candidates are produced.
|
||||
operations::resolve(scope, request, v8::undefined(scope).into());
|
||||
true
|
||||
}
|
||||
|
||||
fn rtc_peer_connection_close_callback<'s>(
|
||||
@@ -423,12 +527,13 @@ fn rtc_peer_connection_close_callback<'s>(
|
||||
let target = moli_webapi_declare::web_api_object_target(scope, args.this())
|
||||
.expect("RTCPeerConnection receiver");
|
||||
rtp_transceivers::close(scope, target);
|
||||
operations::close(scope, target);
|
||||
for (slot, state) in [
|
||||
(RTC_PEER_CONNECTION_SIGNALING_STATE_SLOT, "closed"),
|
||||
(RTC_PEER_CONNECTION_ICE_CONNECTION_STATE_SLOT, "closed"),
|
||||
(RTC_PEER_CONNECTION_CONNECTION_STATE_SLOT, "closed"),
|
||||
] {
|
||||
set_string_slot(scope, args.this(), slot, state);
|
||||
set_string_slot(scope, target, slot, state);
|
||||
}
|
||||
rv.set_undefined();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
//! Integration with the host's native peer connection operations chain.
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Kind {
|
||||
CreateOffer,
|
||||
SetLocalDescription,
|
||||
ReplaceTrack,
|
||||
}
|
||||
|
||||
pub type ConnectionOperation = for<'s, 'p> fn(&mut v8::PinScope<'s, 'p>, v8::Local<'s, v8::Object>);
|
||||
pub type EnqueueOperation = for<'s, 'p> fn(
|
||||
&mut v8::PinScope<'s, 'p>,
|
||||
v8::Local<'s, v8::Object>,
|
||||
Kind,
|
||||
v8::Local<'s, v8::Value>,
|
||||
) -> Option<v8::Local<'s, v8::Promise>>;
|
||||
pub type OperationPayload = for<'s, 'p> fn(
|
||||
&mut v8::PinScope<'s, 'p>,
|
||||
v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::Value>;
|
||||
pub type SettleOperation =
|
||||
for<'s, 'p> fn(&mut v8::PinScope<'s, 'p>, v8::Local<'s, v8::Object>, v8::Local<'s, v8::Value>);
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct OperationsRuntime {
|
||||
pub initialize: ConnectionOperation,
|
||||
pub enqueue: EnqueueOperation,
|
||||
pub payload: OperationPayload,
|
||||
pub resolve: SettleOperation,
|
||||
pub reject: SettleOperation,
|
||||
pub close: ConnectionOperation,
|
||||
}
|
||||
|
||||
pub fn install_runtime(scope: &mut v8::PinScope<'_, '_>, runtime: OperationsRuntime) {
|
||||
scope.get_current_context().set_slot(runtime.into());
|
||||
}
|
||||
|
||||
fn runtime(scope: &mut v8::PinScope<'_, '_>) -> OperationsRuntime {
|
||||
*scope
|
||||
.get_current_context()
|
||||
.get_slot::<OperationsRuntime>()
|
||||
.expect("install the operations runtime before invoking RTC bindings")
|
||||
}
|
||||
|
||||
pub(super) fn initialize<'s>(scope: &mut v8::PinScope<'s, '_>, pc: v8::Local<'s, v8::Object>) {
|
||||
(runtime(scope).initialize)(scope, pc);
|
||||
}
|
||||
|
||||
pub(super) fn enqueue<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
kind: Kind,
|
||||
payload: v8::Local<'s, v8::Value>,
|
||||
) -> Option<v8::Local<'s, v8::Promise>> {
|
||||
(runtime(scope).enqueue)(scope, pc, kind, payload)
|
||||
}
|
||||
|
||||
pub(super) fn payload<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::Value> {
|
||||
(runtime(scope).payload)(scope, request)
|
||||
}
|
||||
|
||||
pub(super) fn resolve<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
value: v8::Local<'s, v8::Value>,
|
||||
) {
|
||||
(runtime(scope).resolve)(scope, request, value);
|
||||
}
|
||||
|
||||
pub(super) fn reject<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
reason: v8::Local<'s, v8::Value>,
|
||||
) {
|
||||
(runtime(scope).reject)(scope, request, reason);
|
||||
}
|
||||
|
||||
pub(super) fn close<'s>(scope: &mut v8::PinScope<'s, '_>, pc: v8::Local<'s, v8::Object>) {
|
||||
(runtime(scope).close)(scope, pc);
|
||||
}
|
||||
@@ -6,6 +6,8 @@ pub type ConnectionOperation = for<'s, 'p> fn(
|
||||
&mut v8::PinScope<'s, 'p>,
|
||||
v8::Local<'s, v8::Object>,
|
||||
);
|
||||
pub type InitializeConnection =
|
||||
for<'s, 'p> fn(&mut v8::PinScope<'s, 'p>, v8::Local<'s, v8::Object>) -> Option<()>;
|
||||
pub type CreateOffer = for<'s, 'p> fn(
|
||||
&mut v8::PinScope<'s, 'p>,
|
||||
v8::Local<'s, v8::Object>,
|
||||
@@ -14,7 +16,7 @@ pub type CreateOffer = for<'s, 'p> fn(
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct RtpRuntime {
|
||||
pub initialize_pc: ConnectionOperation,
|
||||
pub initialize_pc: InitializeConnection,
|
||||
pub close: ConnectionOperation,
|
||||
pub create_offer: CreateOffer,
|
||||
}
|
||||
@@ -31,8 +33,8 @@ fn runtime(scope: &mut v8::PinScope<'_, '_>) -> RtpRuntime {
|
||||
pub(super) fn initialize_pc<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
pc: v8::Local<'s, v8::Object>,
|
||||
) {
|
||||
(runtime(scope).initialize_pc)(scope, pc);
|
||||
) -> Option<()> {
|
||||
(runtime(scope).initialize_pc)(scope, pc)
|
||||
}
|
||||
|
||||
pub(super) fn close<'s>(scope: &mut v8::PinScope<'s, '_>, pc: v8::Local<'s, v8::Object>) {
|
||||
@@ -46,3 +48,11 @@ pub(super) fn create_offer<'s>(
|
||||
) -> Option<String> {
|
||||
(runtime(scope).create_offer)(scope, pc, data)
|
||||
}
|
||||
|
||||
pub(super) fn target<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
object: v8::Local<'s, v8::Object>,
|
||||
) -> v8::Local<'s, v8::Object> {
|
||||
moli_webapi_declare::web_api_object_target(scope, object)
|
||||
.expect("RTCPeerConnection receiver")
|
||||
}
|
||||
|
||||
@@ -77,6 +77,14 @@ pub fn install_session_description_template_bindings<'s>(
|
||||
);
|
||||
}
|
||||
|
||||
pub fn from_parts<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
kind: v8::Local<'s, v8::String>,
|
||||
sdp: v8::Local<'s, v8::String>,
|
||||
) -> Option<v8::Local<'s, v8::Object>> {
|
||||
SessionDescriptionObjectDeclaration::new(kind.into(), sdp).bind(scope).ok()
|
||||
}
|
||||
|
||||
pub fn rtc_session_description_constructor_callback<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
|
||||
Reference in New Issue
Block a user