-
Notifications
You must be signed in to change notification settings - Fork 10
/
video-room.ts
125 lines (97 loc) · 3.63 KB
/
video-room.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import JanusPluginMessage from '../../client/misc/plugin-message';
import Connection, { ConnectionOptions } from '../../client/connection';
import { UserMediaResult } from '../../plugin/base/media-plugin';
import { JoinInfo, JoinOptions, RemoteVideo } from '../../plugin/dto/video-room';
import Client from '../../client/client';
import Session from '../../client/session';
import VideoRoomPlugin from '../../plugin/video-room-plugin';
import { MediaDevices, WebRTC } from '../../plugin/base/shims/definitions';
export class VideoRoom {
private readonly address: string;
private readonly clientOptions: ConnectionOptions;
private client: Client;
private connection?: Connection;
private session?: Session;
private plugin?: VideoRoomPlugin;
private joinInfo?: JoinInfo;
constructor(
address: string,
clientOptions: ConnectionOptions = { keepalive: true },
mediaDevices: MediaDevices,
webRTC: WebRTC
) {
this.address = address;
this.clientOptions = clientOptions;
this.client = new Client(this.address, this.clientOptions, mediaDevices, webRTC);
}
public onRoomJoined(f: (e: JoinInfo) => void) {
this._onRoomJoined = f;
}
public onLocalVideo(f: (e: MediaStream) => void) {
this._onLocalVideo = f;
}
public onRemoteVideo(f: (e: RemoteVideo) => void) {
this._onRemoteVideo = f;
}
public onUnpublished(f: (e: number) => void) {
this._onUnpublished = f;
}
public onLeaving(f: (e: number) => void) {
this._onLeaving = f;
}
join = async (options: JoinOptions, constraints: MediaStreamConstraints) => {
options = Object.assign(options, { ptype: 'publisher' });
// Attach video plugin.
await this.attachPlugin();
// Join room.
const data: JanusPluginMessage = await this.plugin?.join(options);
this.joinInfo = data.getPlainMessage().plugindata.data as JoinInfo;
this._onRoomJoined(this.joinInfo);
// Request local video.
await this.plugin?.processLocalVideo(this.joinInfo, constraints);
};
unpublish = async () => {
return this.plugin?.unpublish();
};
private _onRoomJoined = (_: JoinInfo) => {};
private _onLocalVideo = (_: MediaStream) => {};
private _onRemoteVideo = (_: RemoteVideo) => {};
private _onUnpublished = (_: number) => {};
private _onLeaving = (_: number) => {};
private createConnection = async () => {
if (this.connection) return this.connection;
this.connection = await this.client.createConnection('client');
return this.connection;
};
private createSession = async () => {
if (this.session) return this.session;
await this.createConnection();
this.session = await this.connection?.createSession();
return this.session;
};
private attachPlugin = async () => {
await this.createSession();
if (this.plugin) return this.plugin;
//@ts-ignore
this.plugin = await this.session?.attachPlugin(VideoRoomPlugin.NAME);
// Event when user accepts permissions.
this.plugin?.on('consent-dialog:stop', (media: UserMediaResult) => {
if (media.stream) this._onLocalVideo(media.stream);
else console.log(media.error);
});
// Event when remote stream is available.
this.plugin?.on('videoroom-remote-feed:received', (feed: RemoteVideo) => {
this._onRemoteVideo(feed);
});
// Event when remote feed unpublished.
this.plugin?.on('videoroom-remote-feed:unpublished', (feedId: number) => {
this._onUnpublished(feedId);
});
// Event when remote participant left the room.
this.plugin?.on('videoroom-remote-feed:leaving', (feedId: number) => {
this._onLeaving(feedId);
});
return this.plugin;
};
}
export default VideoRoom;