Skip to content
Permalink
Browse files

Use MediaStreamTracks in MediaStreams

  • Loading branch information...
Manishearth committed May 7, 2019
1 parent 106cc4a commit a9ab13b279d8234a56c58a6907362c4f48dd6138
@@ -840,9 +840,13 @@ impl HTMLMediaElement {
self.fetch_request(None);
},
SrcObject::MediaStream(ref stream) => {
for stream in stream.get_tracks() {
if let Err(_) =
self.player.borrow().as_ref().unwrap().set_stream(&stream)
for stream in &*stream.get_tracks() {
if let Err(_) = self
.player
.borrow()
.as_ref()
.unwrap()
.set_stream(&stream.id())
{
self.queue_dedicated_media_source_failure_steps();
}
@@ -14,6 +14,7 @@ use crate::dom::bindings::root::DomRoot;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::mediastream::MediaStream;
use crate::dom::mediastreamtrack::MediaStreamTrack;
use crate::dom::promise::Promise;
use dom_struct::dom_struct;
use servo_media::streams::capture::{Constrain, ConstrainRange, MediaTrackConstraintSet};
@@ -51,18 +52,20 @@ impl MediaDevicesMethods for MediaDevices {
InCompartment::Already(&in_compartment_proof),
);
let media = ServoMedia::get().unwrap();
let mut tracks = vec![];
let stream = MediaStream::new(&self.global());
if let Some(constraints) = convert_constraints(&constraints.audio) {
if let Some(audio) = media.create_audioinput_stream(constraints) {
tracks.push(audio)
let track = MediaStreamTrack::new(&self.global(), audio);
stream.add_track(&track);
}
}
if let Some(constraints) = convert_constraints(&constraints.video) {
if let Some(video) = media.create_videoinput_stream(constraints) {
tracks.push(video)
let track = MediaStreamTrack::new(&self.global(), video);
stream.add_track(&track);
}
}
let stream = MediaStream::new(&self.global(), tracks);

p.resolve_native(&stream);
p
}
@@ -3,38 +3,53 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::MediaStreamBinding;
use crate::dom::bindings::codegen::Bindings::MediaStreamBinding::{self, MediaStreamMethods};
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::mediastreamtrack::MediaStreamTrack;
use dom_struct::dom_struct;
use servo_media::streams::registry::MediaStreamId;
use std::cell::Ref;

#[dom_struct]
pub struct MediaStream {
eventtarget: EventTarget,
#[ignore_malloc_size_of = "defined in servo-media"]
tracks: DomRefCell<Vec<MediaStreamId>>,
tracks: DomRefCell<Vec<Dom<MediaStreamTrack>>>,
}

impl MediaStream {
pub fn new_inherited(tracks: Vec<MediaStreamId>) -> MediaStream {
pub fn new_inherited() -> MediaStream {
MediaStream {
eventtarget: EventTarget::new_inherited(),
tracks: DomRefCell::new(tracks),
tracks: DomRefCell::new(vec![]),
}
}

pub fn new(global: &GlobalScope, tracks: Vec<MediaStreamId>) -> DomRoot<MediaStream> {
pub fn new(global: &GlobalScope) -> DomRoot<MediaStream> {
reflect_dom_object(
Box::new(MediaStream::new_inherited(tracks)),
Box::new(MediaStream::new_inherited()),
global,
MediaStreamBinding::Wrap,
)
}

pub fn get_tracks(&self) -> Vec<MediaStreamId> {
self.tracks.borrow_mut().clone()
pub fn get_tracks(&self) -> Ref<[Dom<MediaStreamTrack>]> {
Ref::map(self.tracks.borrow(), |tracks| &**tracks)
}

pub fn add_track(&self, track: &MediaStreamTrack) {
self.tracks.borrow_mut().push(Dom::from_ref(track))
}
}

impl MediaStreamMethods for MediaStream {
/// https://w3c.github.io/mediacapture-main/#dom-mediastream-gettracks
fn GetTracks(&self) -> Vec<DomRoot<MediaStreamTrack>> {
self.tracks
.borrow()
.iter()
.map(|x| DomRoot::from_ref(&**x))
.collect()
}
}
@@ -32,4 +32,8 @@ impl MediaStreamTrack {
MediaStreamTrackBinding::Wrap,
)
}

pub fn id(&self) -> MediaStreamId {
self.id
}
}
@@ -585,10 +585,12 @@ impl RTCPeerConnectionMethods for RTCPeerConnection {

// https://w3c.github.io/webrtc-pc/#legacy-interface-extensions
fn AddStream(&self, stream: &MediaStream) {
let mut tracks = stream.get_tracks();

for ref track in tracks.drain(..) {
self.controller.borrow().as_ref().unwrap().add_stream(track);
for track in &*stream.get_tracks() {
self.controller
.borrow()
.as_ref()
.unwrap()
.add_stream(&track.id());
}
}

@@ -13,7 +13,7 @@ interface MediaStream : EventTarget {
// readonly attribute DOMString id;
// sequence<MediaStreamTrack> getAudioTracks();
// sequence<MediaStreamTrack> getVideoTracks();
// sequence<MediaStreamTrack> getTracks();
sequence<MediaStreamTrack> getTracks();
// MediaStreamTrack? getTrackById(DOMString trackId);
// void addTrack(MediaStreamTrack track);
// void removeTrack(MediaStreamTrack track);

0 comments on commit a9ab13b

Please sign in to comment.
You can’t perform that action at this time.