Stream.ts refactoring (only one MediaStream object)

pull/30/head
pabloFuente 2018-01-11 11:45:03 +01:00
parent 450205eb50
commit a8ec4ae497
2 changed files with 32 additions and 44 deletions

View File

@ -88,7 +88,7 @@ export class OpenViduInternal {
// If the user wants to send audio with the screen capturing // If the user wants to send audio with the screen capturing
navigator.mediaDevices.getUserMedia({ audio: true, video: false }) navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(userStream => { .then(userStream => {
this.camera.getWrStream().addTrack(userStream.getAudioTracks()[0]); this.camera.getMediaStream().addTrack(userStream.getAudioTracks()[0]);
this.camera.isScreenRequestedReady = true; this.camera.isScreenRequestedReady = true;
this.camera.ee.emitEvent('screen-ready'); this.camera.ee.emitEvent('screen-ready');
if (callback) { if (callback) {

View File

@ -54,7 +54,7 @@ export class Stream {
public typeOfVideo: string; // 'CAMERA' or 'SCREEN' public typeOfVideo: string; // 'CAMERA' or 'SCREEN'
ee = new EventEmitter(); ee = new EventEmitter();
private wrStream: MediaStream; private mediaStream: MediaStream;
private wp: any; private wp: any;
private video: HTMLVideoElement; private video: HTMLVideoElement;
private speechEvent: any; private speechEvent: any;
@ -72,7 +72,6 @@ export class Stream {
private activeAudio = true; private activeAudio = true;
private activeVideo = true; private activeVideo = true;
private videoSrcObject: MediaStream;
private parentId: string; private parentId: string;
public isReadyToPublish: boolean = false; public isReadyToPublish: boolean = false;
public isPublisherPublished: boolean = false; public isPublisherPublished: boolean = false;
@ -89,27 +88,16 @@ export class Stream {
this.isScreenRequested = true; this.isScreenRequested = true;
this.connection = this.room.getLocalParticipant(); this.connection = this.room.getLocalParticipant();
} }
this.addEventListener('src-added', (srcEvent) => { this.addEventListener('mediastream-updated', () => {
this.videoSrcObject = srcEvent.srcObject; if (this.video) this.video.srcObject = this.mediaStream;
if (this.video) this.video.srcObject = srcEvent.srcObject; console.debug("Video srcObject [" + this.mediaStream + "] added to stream [" + this.streamId + "]");
console.debug("Video srcObject [" + srcEvent.srcObject + "] added to stream [" + this.streamId + "]");
}); });
} }
emitSrcEvent(wrstream) {
this.ee.emitEvent('src-added', [{
srcObject: wrstream
}]);
}
emitStreamReadyEvent() { emitStreamReadyEvent() {
this.ee.emitEvent('stream-ready'), [{}]; this.ee.emitEvent('stream-ready'), [{}];
} }
getVideoSrcObject() {
return this.videoSrcObject;
}
removeVideo(parentElement: string); removeVideo(parentElement: string);
removeVideo(parentElement: Element); removeVideo(parentElement: Element);
removeVideo(); removeVideo();
@ -171,8 +159,8 @@ export class Stream {
this.showMyRemote = true; this.showMyRemote = true;
this.localMirrored = true; this.localMirrored = true;
if (wr) { if (wr) {
this.wrStream = wr; this.mediaStream = wr;
this.emitSrcEvent(this.wrStream); this.ee.emitEvent('mediastream-updated');
} }
} }
@ -215,8 +203,8 @@ export class Stream {
this.wp.send(data); this.wp.send(data);
} }
getWrStream() { getMediaStream() {
return this.wrStream; return this.mediaStream;
} }
getWebRtcPeer() { getWebRtcPeer() {
@ -257,7 +245,7 @@ export class Stream {
this.video.id = (this.local ? 'local-' : 'remote-') + 'video-' + this.streamId; this.video.id = (this.local ? 'local-' : 'remote-') + 'video-' + this.streamId;
this.video.autoplay = true; this.video.autoplay = true;
this.video.controls = false; this.video.controls = false;
this.video.srcObject = this.videoSrcObject; this.ee.emitEvent('mediastream-updated');
if (this.local && !this.displayMyRemote()) { if (this.local && !this.displayMyRemote()) {
this.video.muted = true; this.video.muted = true;
@ -393,8 +381,8 @@ export class Stream {
userStream.getVideoTracks()[0].enabled = this.activeVideo; userStream.getVideoTracks()[0].enabled = this.activeVideo;
} }
this.wrStream = userStream; this.mediaStream = userStream;
this.emitSrcEvent(this.wrStream); this.ee.emitEvent('mediastream-updated');
callback(undefined, this); callback(undefined, this);
} }
@ -469,7 +457,7 @@ export class Stream {
} }
let options: any = { let options: any = {
videoStream: this.wrStream, videoStream: this.mediaStream,
mediaConstraints: userMediaConstraints, mediaConstraints: userMediaConstraints,
onicecandidate: this.connection.sendIceCandidate.bind(this.connection), onicecandidate: this.connection.sendIceCandidate.bind(this.connection),
} }
@ -562,16 +550,16 @@ export class Stream {
// Avoids to subscribe to your own stream remotely // Avoids to subscribe to your own stream remotely
// except when showMyRemote is true // except when showMyRemote is true
if (!this.local || this.displayMyRemote()) { if (!this.local || this.displayMyRemote()) {
this.wrStream = pc.getRemoteStreams()[0]; this.mediaStream = pc.getRemoteStreams()[0];
console.debug("Peer remote stream", this.wrStream); console.debug("Peer remote stream", this.mediaStream);
if (this.wrStream != undefined) { if (this.mediaStream != undefined) {
this.emitSrcEvent(this.wrStream); this.ee.emitEvent('mediastream-updated');
if (this.wrStream.getAudioTracks()[0] != null) { if (this.mediaStream.getAudioTracks()[0] != null) {
this.speechEvent = kurentoUtils.WebRtcPeer.hark(this.wrStream, { threshold: this.room.thresholdSpeaker }); this.speechEvent = kurentoUtils.WebRtcPeer.hark(this.mediaStream, { threshold: this.room.thresholdSpeaker });
this.speechEvent.on('speaking', () => { this.speechEvent.on('speaking', () => {
//this.room.addParticipantSpeaking(participantId); //this.room.addParticipantSpeaking(participantId);
@ -592,7 +580,6 @@ export class Stream {
} }
} }
// let thumbnailId = this.video.thumb; // let thumbnailId = this.video.thumb;
this.video.srcObject = this.wrStream;
this.video.oncanplay = () => { this.video.oncanplay = () => {
if (this.local && this.displayMyRemote()) { if (this.local && this.displayMyRemote()) {
console.info("Your own remote 'Stream' with id [" + this.streamId + "] video is now playing"); console.info("Your own remote 'Stream' with id [" + this.streamId + "] video is now playing");
@ -622,11 +609,11 @@ export class Stream {
if (this.wp) { if (this.wp) {
this.wp.dispose(); this.wp.dispose();
} else { } else {
if (this.wrStream) { if (this.mediaStream) {
this.wrStream.getAudioTracks().forEach(function (track) { this.mediaStream.getAudioTracks().forEach(function (track) {
track.stop && track.stop() track.stop && track.stop()
}) })
this.wrStream.getVideoTracks().forEach(function (track) { this.mediaStream.getVideoTracks().forEach(function (track) {
track.stop && track.stop() track.stop && track.stop()
}) })
} }
@ -652,11 +639,11 @@ export class Stream {
if (this.wp) { if (this.wp) {
this.wp.dispose(); this.wp.dispose();
} else { } else {
if (this.wrStream) { if (this.mediaStream) {
this.wrStream.getAudioTracks().forEach(function (track) { this.mediaStream.getAudioTracks().forEach(function (track) {
track.stop && track.stop() track.stop && track.stop()
}) })
this.wrStream.getVideoTracks().forEach(function (track) { this.mediaStream.getVideoTracks().forEach(function (track) {
track.stop && track.stop() track.stop && track.stop()
}) })
} }
@ -670,11 +657,6 @@ export class Stream {
} }
private configureOptions(options) { private configureOptions(options) {
if (options.id) {
this.streamId = options.id;
} else {
this.streamId = "WEBCAM";
}
this.connection = options.connection; this.connection = options.connection;
this.recvVideo = options.recvVideo || false; this.recvVideo = options.recvVideo || false;
this.recvAudio = options.recvAudio || false; this.recvAudio = options.recvAudio || false;
@ -688,6 +670,12 @@ export class Stream {
this.hasAudio = ((this.recvAudio || this.sendAudio) != undefined) ? (this.recvAudio || this.sendAudio) : false; this.hasAudio = ((this.recvAudio || this.sendAudio) != undefined) ? (this.recvAudio || this.sendAudio) : false;
this.hasVideo = ((this.recvVideo || this.sendVideo) != undefined) ? (this.recvVideo || this.sendVideo) : false; this.hasVideo = ((this.recvVideo || this.sendVideo) != undefined) ? (this.recvVideo || this.sendVideo) : false;
this.typeOfVideo = options.typeOfVideo; this.typeOfVideo = options.typeOfVideo;
if (options.id) {
this.streamId = options.id;
} else {
this.streamId = this.sendVideo ? "WEBCAM" : "MICRO";
}
} }
configureScreenOptions(options) { configureScreenOptions(options) {