openvidu-browser: promisified Session.unpublish and Session.unsubscribe

pull/621/head
pabloFuente 2021-03-29 14:53:12 +02:00
parent 464ef90c79
commit 59c3a84718
1 changed files with 54 additions and 46 deletions

View File

@ -353,30 +353,35 @@ export class Session extends EventDispatcher {
* *
* See [[VideoElementEvent]] to learn more * See [[VideoElementEvent]] to learn more
*/ */
unsubscribe(subscriber: Subscriber): void { unsubscribe(subscriber: Subscriber): Promise<void> {
if (!this.sessionConnected()) { return new Promise((resolve, reject) => {
throw this.notConnectedError()
}
const connectionId = subscriber.stream.connection.connectionId; if (!this.sessionConnected()) {
reject(this.notConnectedError());
} else {
const connectionId = subscriber.stream.connection.connectionId;
logger.info('Unsubscribing from ' + connectionId); logger.info('Unsubscribing from ' + connectionId);
this.openvidu.sendRequest( this.openvidu.sendRequest(
'unsubscribeFromVideo', 'unsubscribeFromVideo',
{ sender: subscriber.stream.connection.connectionId }, { sender: subscriber.stream.connection.connectionId },
(error, response) => { (error, response) => {
if (error) { if (error) {
logger.error('Error unsubscribing from ' + connectionId, error); logger.error('Error unsubscribing from ' + connectionId);
} else { reject(error);
logger.info('Unsubscribed correctly from ' + connectionId); } else {
} logger.info('Unsubscribed correctly from ' + connectionId);
subscriber.stream.disposeWebRtcPeer(); subscriber.stream.streamManager.removeAllVideos();
subscriber.stream.disposeMediaStream(); subscriber.stream.disposeWebRtcPeer();
subscriber.stream.disposeMediaStream();
resolve();
}
}
);
} }
); });
subscriber.stream.streamManager.removeAllVideos();
} }
@ -455,40 +460,43 @@ export class Session extends EventDispatcher {
* *
* See [[StreamEvent]] and [[VideoElementEvent]] to learn more. * See [[StreamEvent]] and [[VideoElementEvent]] to learn more.
*/ */
unpublish(publisher: Publisher): void { unpublish(publisher: Publisher): Promise<void> {
if (!this.sessionConnected()) { return new Promise((resolve, reject) => {
throw this.notConnectedError()
}
const stream = publisher.stream; if (!this.sessionConnected()) {
throw this.notConnectedError()
}
if (!stream.connection) { const stream = publisher.stream;
logger.error('The associated Connection object of this Publisher is null', stream);
return;
} else if (stream.connection !== this.connection) {
logger.error('The associated Connection object of this Publisher is not your local Connection.' +
"Only moderators can force unpublish on remote Streams via 'forceUnpublish' method", stream);
return;
} else {
logger.info('Unpublishing local media (' + stream.connection.connectionId + ')'); if (!stream.connection) {
reject(new Error('The associated Connection object of this Publisher is null'));
} else if (stream.connection !== this.connection) {
reject(new Error('The associated Connection object of this Publisher is not your local Connection.' +
"Only moderators can force unpublish on remote Streams via 'forceUnpublish' method"));
} else {
this.openvidu.sendRequest('unpublishVideo', (error, response) => { logger.info('Unpublishing local media (' + stream.connection.connectionId + ')');
if (error) {
logger.error(error);
} else {
logger.info('Media unpublished correctly');
}
});
stream.disposeWebRtcPeer(); this.openvidu.sendRequest('unpublishVideo', (error, response) => {
delete stream.connection.stream; if (error) {
reject(error);
} else {
logger.info('Media unpublished correctly');
const streamEvent = new StreamEvent(true, publisher, 'streamDestroyed', publisher.stream, 'unpublish'); stream.disposeWebRtcPeer();
publisher.emitEvent('streamDestroyed', [streamEvent]); delete stream.connection.stream;
streamEvent.callDefaultBehavior();
} const streamEvent = new StreamEvent(true, publisher, 'streamDestroyed', publisher.stream, 'unpublish');
publisher.emitEvent('streamDestroyed', [streamEvent]);
streamEvent.callDefaultBehavior();
resolve();
}
});
}
});
} }