2017-03-08 16:50:29 +01:00
|
|
|
import { Component } from '@angular/core';
|
|
|
|
import { OpenVidu, Session, Stream } from 'openvidu-browser';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-root',
|
|
|
|
templateUrl: './app.component.html'
|
|
|
|
})
|
|
|
|
export class AppComponent {
|
|
|
|
|
|
|
|
private openVidu: OpenVidu;
|
|
|
|
|
2017-03-09 19:46:07 +01:00
|
|
|
// Join form
|
2017-03-08 16:50:29 +01:00
|
|
|
sessionId: string;
|
|
|
|
participantId: string;
|
|
|
|
|
2017-03-09 19:46:07 +01:00
|
|
|
// Session
|
2017-03-08 16:50:29 +01:00
|
|
|
session: Session;
|
|
|
|
streams: Stream[] = [];
|
|
|
|
|
2017-03-09 19:46:07 +01:00
|
|
|
// Publish options
|
|
|
|
joinWithVideo: boolean = true;
|
|
|
|
joinWithAudio: boolean = true;
|
|
|
|
toggleVideo: boolean;
|
|
|
|
toggleAudio: boolean;
|
|
|
|
|
2017-03-08 16:50:29 +01:00
|
|
|
constructor() {
|
|
|
|
this.generateParticipantInfo();
|
|
|
|
window.onbeforeunload = () => {
|
|
|
|
this.openVidu.close(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 19:46:07 +01:00
|
|
|
private generateParticipantInfo() {
|
2017-03-08 16:50:29 +01:00
|
|
|
this.sessionId = "SessionA";
|
2017-03-09 19:46:07 +01:00
|
|
|
this.participantId = "Participant" + Math.floor(Math.random() * 100);
|
2017-03-08 16:50:29 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 19:46:07 +01:00
|
|
|
private addVideoTag(stream: Stream) {
|
2017-03-08 16:50:29 +01:00
|
|
|
console.log("Stream added");
|
|
|
|
this.streams.push(stream);
|
|
|
|
}
|
|
|
|
|
2017-03-09 19:46:07 +01:00
|
|
|
private removeVideoTag(stream: Stream) {
|
2017-03-08 16:50:29 +01:00
|
|
|
console.log("Stream removed");
|
|
|
|
this.streams.slice(this.streams.indexOf(stream), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
joinSession() {
|
2017-03-09 19:46:07 +01:00
|
|
|
var cameraOptions = {
|
|
|
|
audio: this.joinWithAudio,
|
|
|
|
video: this.joinWithVideo,
|
|
|
|
data: true,
|
|
|
|
mediaConstraints: {}
|
|
|
|
}
|
|
|
|
this.joinSessionShared(cameraOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
joinSessionShared(cameraOptions) {
|
|
|
|
|
|
|
|
this.toggleVideo = this.joinWithVideo;
|
|
|
|
this.toggleAudio = this.joinWithAudio;
|
2017-03-08 16:50:29 +01:00
|
|
|
|
|
|
|
this.openVidu = new OpenVidu("wss://127.0.0.1:8443/");
|
|
|
|
|
|
|
|
this.openVidu.connect((error, openVidu) => {
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return console.log(error);
|
|
|
|
|
2017-03-09 19:46:07 +01:00
|
|
|
let camera = openVidu.getCamera(cameraOptions);
|
2017-03-08 16:50:29 +01:00
|
|
|
|
|
|
|
camera.requestCameraAccess((error, camera) => {
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return console.log(error);
|
|
|
|
|
|
|
|
var sessionOptions = {
|
|
|
|
sessionId: this.sessionId,
|
|
|
|
participantId: this.participantId
|
|
|
|
}
|
|
|
|
|
|
|
|
openVidu.joinSession(sessionOptions, (error, session) => {
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return console.log(error);
|
|
|
|
|
|
|
|
this.session = session;
|
|
|
|
|
|
|
|
this.addVideoTag(camera);
|
|
|
|
|
|
|
|
camera.publish();
|
|
|
|
|
|
|
|
session.addEventListener("stream-added", streamEvent => {
|
2017-03-09 19:46:07 +01:00
|
|
|
this.addVideoTag(streamEvent.stream);
|
2017-03-08 16:50:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
session.addEventListener("stream-removed", streamEvent => {
|
2017-03-09 19:46:07 +01:00
|
|
|
this.removeVideoTag(streamEvent.stream);
|
2017-03-08 16:50:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
leaveSession() {
|
|
|
|
this.session = null;
|
|
|
|
this.streams = [];
|
|
|
|
this.openVidu.close(true);
|
|
|
|
this.generateParticipantInfo();
|
|
|
|
}
|
|
|
|
|
2017-03-09 19:46:07 +01:00
|
|
|
updateToggleVideo(event) {
|
|
|
|
this.toggleVideoTrack(event.target.checked);
|
|
|
|
let msg = (event.target.checked) ? 'Publishing video...' : 'Unpublishing video...'
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateToggleAudio(event) {
|
|
|
|
this.toggleAudioTrack(event.target.checked);
|
|
|
|
let msg = (event.target.checked) ? 'Publishing audio...' : 'Unpublishing audio...'
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleVideoTrack(activate: boolean) {
|
|
|
|
this.openVidu.getCamera().getWebRtcPeer().videoEnabled = activate;
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleAudioTrack(activate: boolean) {
|
|
|
|
this.openVidu.getCamera().getWebRtcPeer().audioEnabled = activate;
|
|
|
|
}
|
|
|
|
|
|
|
|
publishVideoAudio() {
|
|
|
|
this.toggleVideoTrack(true);
|
|
|
|
this.toggleAudioTrack(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
unpublishVideoAudio() {
|
|
|
|
this.toggleVideoTrack(false);
|
|
|
|
this.toggleAudioTrack(false);
|
|
|
|
}
|
|
|
|
|
2017-03-08 16:50:29 +01:00
|
|
|
}
|