mirror of
https://github.com/servo/servo.git
synced 2025-07-25 08:10:21 +01:00
Allow receiving binary data channel messages
This commit is contained in:
parent
f855fbb604
commit
2dedcaeaaf
2 changed files with 59 additions and 24 deletions
|
@ -22,13 +22,15 @@ use crate::dom::rtcerrorevent::RTCErrorEvent;
|
||||||
use crate::dom::rtcpeerconnection::RTCPeerConnection;
|
use crate::dom::rtcpeerconnection::RTCPeerConnection;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use js::conversions::ToJSValConvertible;
|
use js::conversions::ToJSValConvertible;
|
||||||
use js::jsapi::JSAutoRealm;
|
use js::jsapi::{JSAutoRealm, JSObject};
|
||||||
use js::jsval::UndefinedValue;
|
use js::jsval::UndefinedValue;
|
||||||
use js::rust::CustomAutoRooterGuard;
|
use js::rust::CustomAutoRooterGuard;
|
||||||
use js::typedarray::{ArrayBuffer, ArrayBufferView};
|
use js::typedarray::{ArrayBuffer, ArrayBufferView, CreateWith};
|
||||||
|
use script_traits::serializable::BlobImpl;
|
||||||
use servo_media::webrtc::{
|
use servo_media::webrtc::{
|
||||||
DataChannelId, DataChannelInit, DataChannelMessage, DataChannelState, WebRtcError,
|
DataChannelId, DataChannelInit, DataChannelMessage, DataChannelState, WebRtcError,
|
||||||
};
|
};
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct RTCDataChannel {
|
pub struct RTCDataChannel {
|
||||||
|
@ -44,6 +46,7 @@ pub struct RTCDataChannel {
|
||||||
negotiated: bool,
|
negotiated: bool,
|
||||||
id: Option<u16>,
|
id: Option<u16>,
|
||||||
ready_state: DomRefCell<RTCDataChannelState>,
|
ready_state: DomRefCell<RTCDataChannelState>,
|
||||||
|
binary_type: DomRefCell<DOMString>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RTCDataChannel {
|
impl RTCDataChannel {
|
||||||
|
@ -78,6 +81,7 @@ impl RTCDataChannel {
|
||||||
negotiated: options.negotiated,
|
negotiated: options.negotiated,
|
||||||
id: options.id,
|
id: options.id,
|
||||||
ready_state: DomRefCell::new(RTCDataChannelState::Connecting),
|
ready_state: DomRefCell::new(RTCDataChannelState::Connecting),
|
||||||
|
binary_type: DomRefCell::new(DOMString::from("blob")),
|
||||||
};
|
};
|
||||||
|
|
||||||
peer_connection.register_data_channel(servo_media_id, &channel);
|
peer_connection.register_data_channel(servo_media_id, &channel);
|
||||||
|
@ -146,26 +150,46 @@ impl RTCDataChannel {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
pub fn on_message(&self, message: DataChannelMessage) {
|
pub fn on_message(&self, channel_message: DataChannelMessage) {
|
||||||
// XXX(ferjm) Support binary messages
|
unsafe {
|
||||||
match message {
|
let global = self.global();
|
||||||
DataChannelMessage::Text(text) => unsafe {
|
let cx = global.get_cx();
|
||||||
let global = self.global();
|
let _ac = JSAutoRealm::new(*cx, self.reflector().get_jsobject().get());
|
||||||
let cx = global.get_cx();
|
rooted!(in(*cx) let mut message = UndefinedValue());
|
||||||
let _ac = JSAutoRealm::new(*cx, self.reflector().get_jsobject().get());
|
|
||||||
rooted!(in(*cx) let mut message = UndefinedValue());
|
|
||||||
text.to_jsval(*cx, message.handle_mut());
|
|
||||||
|
|
||||||
MessageEvent::dispatch_jsval(
|
match channel_message {
|
||||||
self.upcast(),
|
DataChannelMessage::Text(text) => {
|
||||||
&global,
|
text.to_jsval(*cx, message.handle_mut());
|
||||||
message.handle(),
|
},
|
||||||
Some(&global.origin().immutable().ascii_serialization()),
|
DataChannelMessage::Binary(data) => match &**self.binary_type.borrow() {
|
||||||
None,
|
"blob" => {
|
||||||
vec![],
|
let blob =
|
||||||
);
|
Blob::new(&global, BlobImpl::new_from_bytes(data, "".to_owned()));
|
||||||
},
|
blob.to_jsval(*cx, message.handle_mut());
|
||||||
DataChannelMessage::Binary(_) => {},
|
},
|
||||||
|
"arraybuffer" => {
|
||||||
|
rooted!(in(*cx) let mut array_buffer = ptr::null_mut::<JSObject>());
|
||||||
|
assert!(ArrayBuffer::create(
|
||||||
|
*cx,
|
||||||
|
CreateWith::Slice(&data),
|
||||||
|
array_buffer.handle_mut()
|
||||||
|
)
|
||||||
|
.is_ok());
|
||||||
|
|
||||||
|
(*array_buffer).to_jsval(*cx, message.handle_mut());
|
||||||
|
},
|
||||||
|
_ => unreachable!(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageEvent::dispatch_jsval(
|
||||||
|
self.upcast(),
|
||||||
|
&global,
|
||||||
|
message.handle(),
|
||||||
|
Some(&global.origin().immutable().ascii_serialization()),
|
||||||
|
None,
|
||||||
|
vec![],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,8 +319,19 @@ impl RTCDataChannelMethods for RTCDataChannel {
|
||||||
.close_data_channel(&self.servo_media_id);
|
.close_data_channel(&self.servo_media_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn BinaryType(&self) -> DOMString;
|
// https://www.w3.org/TR/webrtc/#dom-datachannel-binarytype
|
||||||
// fn SetBinaryType(&self, value: DOMString) -> ();
|
fn BinaryType(&self) -> DOMString {
|
||||||
|
self.binary_type.borrow().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://www.w3.org/TR/webrtc/#dom-datachannel-binarytype
|
||||||
|
fn SetBinaryType(&self, value: DOMString) -> Fallible<()> {
|
||||||
|
if value != "blob" || value != "arraybuffer" {
|
||||||
|
return Err(Error::Syntax);
|
||||||
|
}
|
||||||
|
*self.binary_type.borrow_mut() = value;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// https://www.w3.org/TR/webrtc/#dom-rtcdatachannel-send
|
// https://www.w3.org/TR/webrtc/#dom-rtcdatachannel-send
|
||||||
fn Send(&self, data: USVString) -> Fallible<()> {
|
fn Send(&self, data: USVString) -> Fallible<()> {
|
||||||
|
|
|
@ -23,7 +23,7 @@ interface RTCDataChannel : EventTarget {
|
||||||
attribute EventHandler onclose;
|
attribute EventHandler onclose;
|
||||||
void close();
|
void close();
|
||||||
attribute EventHandler onmessage;
|
attribute EventHandler onmessage;
|
||||||
//attribute DOMString binaryType;
|
[SetterThrows] attribute DOMString binaryType;
|
||||||
[Throws] void send(USVString data);
|
[Throws] void send(USVString data);
|
||||||
[Throws] void send(Blob data);
|
[Throws] void send(Blob data);
|
||||||
[Throws] void send(ArrayBuffer data);
|
[Throws] void send(ArrayBuffer data);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue