Fix issue 107

pull/108/head
Martin Schleyer 2018-08-13 10:37:55 +02:00
parent b55faf831b
commit d40285ccc5
5 changed files with 36 additions and 12 deletions

View File

@ -189,6 +189,7 @@ export class OpenVidu {
publishVideo: (typeof properties.publishVideo !== 'undefined') ? properties.publishVideo : true, publishVideo: (typeof properties.publishVideo !== 'undefined') ? properties.publishVideo : true,
resolution: this.isMediaStreamTrack(properties.videoSource) ? undefined : ((typeof properties.resolution !== 'undefined') ? properties.resolution : '640x480'), resolution: this.isMediaStreamTrack(properties.videoSource) ? undefined : ((typeof properties.resolution !== 'undefined') ? properties.resolution : '640x480'),
videoSource: (typeof properties.videoSource !== 'undefined') ? properties.videoSource : undefined, videoSource: (typeof properties.videoSource !== 'undefined') ? properties.videoSource : undefined,
keepTracksOnDispose: (typeof properties.videoSource !== 'undefined') ? properties.keepTracksOnDispose : false,
filter: properties.filter filter: properties.filter
}; };
} else { } else {

View File

@ -74,7 +74,7 @@ export class Publisher extends StreamManager {
constructor(targEl: string | HTMLElement, properties: PublisherProperties, openvidu: OpenVidu) { constructor(targEl: string | HTMLElement, properties: PublisherProperties, openvidu: OpenVidu) {
super(new Stream((!!openvidu.session) ? openvidu.session : new Session(openvidu), { publisherProperties: properties, mediaConstraints: {} }), targEl); super(new Stream((!!openvidu.session) ? openvidu.session : new Session(openvidu), { publisherProperties: properties, mediaConstraints: {} }), targEl);
this.properties = properties; this.properties = properties;
this.openvidu = openvidu; this.openvidu = openvidu;
this.stream.ee.on('local-stream-destroyed', (reason: string) => { this.stream.ee.on('local-stream-destroyed', (reason: string) => {
this.stream.isLocalStreamPublished = false; this.stream.isLocalStreamPublished = false;

View File

@ -117,6 +117,16 @@ export class Stream implements EventDispatcher {
} }
} = {}; } = {};
/**
* Keeps tracks unstopped on disposal. Allows to keep tracks running if session is diposed
*
* This property may be useful if a publisher is started with a previously existing
* track which should keep running after disposal, e.g. if you start your webcam
* in a <video> element and want to keep it running after OpenVidu session closed
*
*/
keepTracksOnDispose = false;
/** /**
* @hidden * @hidden
*/ */
@ -205,6 +215,9 @@ export class Stream implements EventDispatcher {
this.typeOfVideo = this.isSendScreen() ? 'SCREEN' : 'CAMERA'; this.typeOfVideo = this.isSendScreen() ? 'SCREEN' : 'CAMERA';
} }
} }
this.keepTracksOnDispose = !!this.outboundStreamOpts.publisherProperties.keepTracksOnDispose;
if (!!this.outboundStreamOpts.publisherProperties.filter) { if (!!this.outboundStreamOpts.publisherProperties.filter) {
this.filter = this.outboundStreamOpts.publisherProperties.filter; this.filter = this.outboundStreamOpts.publisherProperties.filter;
} }
@ -360,7 +373,7 @@ export class Stream implements EventDispatcher {
*/ */
disposeWebRtcPeer(): void { disposeWebRtcPeer(): void {
if (this.webRtcPeer) { if (this.webRtcPeer) {
this.webRtcPeer.dispose(); this.webRtcPeer.dispose(this.keepTracksOnDispose);
} }
if (this.speechEvent) { if (this.speechEvent) {
this.speechEvent.stop(); this.speechEvent.stop();
@ -376,12 +389,14 @@ export class Stream implements EventDispatcher {
*/ */
disposeMediaStream(): void { disposeMediaStream(): void {
if (this.mediaStream) { if (this.mediaStream) {
this.mediaStream.getAudioTracks().forEach((track) => { if (!this.keepTracksOnDispose) {
track.stop(); this.mediaStream.getAudioTracks().forEach((track) => {
}); track.stop();
this.mediaStream.getVideoTracks().forEach((track) => { });
track.stop(); this.mediaStream.getVideoTracks().forEach((track) => {
}); track.stop();
});
}
delete this.mediaStream; delete this.mediaStream;
} }
console.info((!!this.outboundStreamOpts ? 'Local ' : 'Remote ') + "MediaStream from 'Stream' with id [" + this.streamId + '] is now disposed'); console.info((!!this.outboundStreamOpts ? 'Local ' : 'Remote ') + "MediaStream from 'Stream' with id [" + this.streamId + '] is now disposed');

View File

@ -88,4 +88,10 @@ export interface PublisherProperties {
*/ */
filter?: Filter; filter?: Filter;
/**
* Whether to stop the published video tracks after disposal or to keep them (useful if you set your own track as videoSource and you need to keep it aftger disposal)
* @default false
*/
keepTracksOnDispose? : boolean;
} }

View File

@ -101,7 +101,7 @@ export class WebRtcPeer {
/** /**
* This method frees the resources used by WebRtcPeer * This method frees the resources used by WebRtcPeer
*/ */
dispose() { dispose(keepTracksOnDispose = false) {
console.debug('Disposing WebRtcPeer'); console.debug('Disposing WebRtcPeer');
try { try {
if (this.pc) { if (this.pc) {
@ -112,7 +112,7 @@ export class WebRtcPeer {
this.localCandidatesQueue = []; this.localCandidatesQueue = [];
this.pc.getLocalStreams().forEach(str => { this.pc.getLocalStreams().forEach(str => {
this.streamStop(str); this.streamStop(str, keepTracksOnDispose);
}); });
// FIXME This is not yet implemented in firefox // FIXME This is not yet implemented in firefox
@ -244,9 +244,11 @@ export class WebRtcPeer {
}); });
} }
private streamStop(stream: MediaStream): void { private streamStop(stream: MediaStream, keepTracksOnDispose = false): void {
stream.getTracks().forEach(track => { stream.getTracks().forEach(track => {
track.stop(); if (!keepTracksOnDispose) {
track.stop();
}
stream.removeTrack(track); stream.removeTrack(track);
}); });
} }