2017-04-12 00:54:35 +02:00
|
|
|
import { Session, SessionOptions } from '../OpenVidu/Session';
|
|
|
|
import { Stream } from '../OpenVidu/Stream';
|
|
|
|
|
|
|
|
import { OpenViduTokBox } from './OpenViduTokBox';
|
|
|
|
import { PublisherTokBox } from './PublisherTokBox';
|
|
|
|
|
|
|
|
export class SessionTokBox {
|
|
|
|
|
2017-04-17 16:23:30 +02:00
|
|
|
constructor(private session: Session, private openVidu: OpenViduTokBox) {
|
|
|
|
this.session.addEventListener('stream-removed-default', event => {
|
|
|
|
event.stream.removeVideo();
|
|
|
|
});
|
|
|
|
}
|
2017-04-12 00:54:35 +02:00
|
|
|
|
|
|
|
connect(token, callback) {
|
|
|
|
// Early configuration to deactivate automatic subscription to streams
|
|
|
|
this.session.configure({
|
|
|
|
sessionId: this.session.getSessionId(),
|
|
|
|
participantId: token,
|
|
|
|
subscribeToStreams: false
|
|
|
|
});
|
|
|
|
this.session.connect(token, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
this.openVidu.openVidu.close(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
publish(publisher: PublisherTokBox) {
|
2017-04-17 20:28:05 +02:00
|
|
|
publisher.session = this;
|
2017-04-12 00:54:35 +02:00
|
|
|
publisher.stream.publish();
|
|
|
|
}
|
|
|
|
|
|
|
|
unpublish(publisher: PublisherTokBox) {
|
|
|
|
this.session.unpublish(publisher.stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
on(eventName: string, callback) {
|
|
|
|
let realEventName = '';
|
|
|
|
switch (eventName) {
|
|
|
|
case 'streamCreated':
|
2017-04-17 20:28:05 +02:00
|
|
|
realEventName = 'stream-added';
|
2017-04-12 00:54:35 +02:00
|
|
|
break;
|
|
|
|
case 'streamDestroyed':
|
2017-04-17 20:28:05 +02:00
|
|
|
realEventName = 'stream-removed';
|
2017-04-12 00:54:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (realEventName != '') {
|
|
|
|
this.session.addEventListener(realEventName, event => {
|
|
|
|
callback(event);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.warn("That is not a supported event!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:28:05 +02:00
|
|
|
once(eventName: string, callback) {
|
|
|
|
let realEventName = '';
|
|
|
|
switch (eventName) {
|
|
|
|
case 'streamCreated':
|
|
|
|
realEventName = 'stream-added';
|
|
|
|
break;
|
|
|
|
case 'streamDestroyed':
|
|
|
|
realEventName = 'stream-removed';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (realEventName != '') {
|
|
|
|
this.session.addOnceEventListener(realEventName, event => {
|
|
|
|
callback(event);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.warn("That is not a supported event!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
off(eventName: string, eventHandler) {
|
|
|
|
let realEventName = '';
|
|
|
|
switch (eventName) {
|
|
|
|
case 'streamCreated':
|
|
|
|
realEventName = 'stream-added';
|
|
|
|
break;
|
|
|
|
case 'streamDestroyed':
|
|
|
|
realEventName = 'stream-removed';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (realEventName != '') {
|
|
|
|
this.session.removeListener(realEventName, eventHandler);
|
|
|
|
} else {
|
|
|
|
console.warn("That is not a supported event!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 00:54:35 +02:00
|
|
|
subscribe(stream: Stream, htmlId: string, videoOptions: any);
|
|
|
|
subscribe(stream: Stream, htmlId: string);
|
|
|
|
|
|
|
|
subscribe(param1, param2, param3?) {
|
|
|
|
// Subscription
|
|
|
|
this.session.subscribe(param1);
|
|
|
|
param1.playOnlyVideo(param2, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Shortcut event API */
|
|
|
|
|
|
|
|
onStreamCreated(callback) {
|
|
|
|
this.session.addEventListener("stream-added", streamEvent => {
|
|
|
|
callback(streamEvent.stream);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onStreamDestroyed(callback) {
|
|
|
|
this.session.addEventListener("stream-removed", streamEvent => {
|
|
|
|
callback(streamEvent.stream);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onParticipantJoined(callback) {
|
|
|
|
this.session.addEventListener("participant-joined", participantEvent => {
|
|
|
|
callback(participantEvent.participant);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onParticipantLeft(callback) {
|
|
|
|
this.session.addEventListener("participant-left", participantEvent => {
|
|
|
|
callback(participantEvent.participant);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onParticipantPublished(callback) {
|
|
|
|
this.session.addEventListener("participant-published", participantEvent => {
|
|
|
|
callback(participantEvent.participant);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onParticipantEvicted(callback) {
|
|
|
|
this.session.addEventListener("participant-evicted", participantEvent => {
|
|
|
|
callback(participantEvent.participant);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onRoomClosed(callback) {
|
|
|
|
this.session.addEventListener("room-closed", roomEvent => {
|
|
|
|
callback(roomEvent.room);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onLostConnection(callback) {
|
|
|
|
this.session.addEventListener("lost-connection", roomEvent => {
|
|
|
|
callback(roomEvent.room);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onMediaError(callback) {
|
|
|
|
this.session.addEventListener("error-media", errorEvent => {
|
|
|
|
callback(errorEvent.error)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shortcut event API */
|
|
|
|
}
|