-
Notifications
You must be signed in to change notification settings - Fork 10
/
video-room-plugin.ts
150 lines (131 loc) · 4.46 KB
/
video-room-plugin.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import Plugin from '../client/plugin';
import MediaPlugin from './base/media-plugin';
import JanusMessage from '../client/misc/message';
import JanusPluginMessage from '../client/misc/plugin-message';
import { JoinInfo, JoinOptions, Publisher, PublisherJoinResult, RemoteVideo } from './dto/video-room';
const JANUS_VIDEOROOM_OPTIONS = { audio: true, video: true };
/**
* Original documentation of the plugin here:
* https://janus.conf.meetecho.com/docs/videoroom.html
*
* Events:
* videoroom-remote-feed:received
* videoroom-remote-feed:leaving
* videoroom-remote-feed:unpublished
* videoroom-remote-feed:leaving
*/
class VideoRoomPlugin extends MediaPlugin {
static NAME = 'janus.plugin.videoroom';
private joinInfo: JoinInfo | null = null;
processLocalVideo(info: JoinInfo, constraints: MediaStreamConstraints) {
this.joinInfo = info;
return this.getUserMedia(constraints)
.then(stream => {
this.createPeerConnection({});
//@ts-ignore
stream.getTracks().forEach(track => this.addTrack(track, stream));
})
.then(() => this.createOffer({}))
.then(jsep => this.configure(JANUS_VIDEOROOM_OPTIONS, jsep))
.then(resp => {
const jsep = resp.get('jsep');
if (jsep) {
this.setRemoteSDP(jsep);
return jsep;
}
});
}
//@ts-ignore
configure(options, jsep) {
const body = Object.assign({ request: 'configure' }, options);
const message: any = { body };
if (jsep) {
message.jsep = jsep;
}
return this.sendWithTransaction(message);
}
join(options: JoinOptions) {
let body = Object.assign({ request: 'join' }, options);
return this.sendWithTransaction({ janus: 'message', body });
}
//@ts-ignore
start(room, jsep) {
return this.sendWithTransaction({
janus: 'message',
body: { request: 'start', room },
jsep,
});
}
unpublish() {
return this.sendWithTransaction({
janus: 'message',
body: { request: 'unpublish' },
}).then(response => {
this.closePeerConnection();
return response;
});
}
processIncomeMessage(message: JanusMessage) {
return super.processIncomeMessage(message).then(result => {
if (!message.getPlainMessage()) {
return;
}
console.log('Received:', message);
const plainMessage = message.getPlainMessage();
const type = plainMessage.janus;
switch (type) {
case 'event':
const pluginData = plainMessage?.plugindata?.data;
if (!pluginData.videoroom) {
break;
}
let videoroom = pluginData.videoroom;
this.handleRemotePublishers(pluginData.publishers).then(() => console.debug('Success handle publishers.'));
switch (videoroom) {
case 'attached':
this.emit('videoroom-remote-feed:attached', { plainMessage });
break;
case 'event':
if (pluginData.unpublished) {
this.emit('videoroom-remote-feed:unpublished', pluginData.unpublished);
} else if (pluginData.leaving) {
this.emit('videoroom-remote-feed:leaving', pluginData.leaving);
}
break;
}
break;
}
return result;
});
}
getResponseAlias() {
return 'videoroom';
}
private handlerPublisher = async (publisher: Publisher): Promise<PublisherJoinResult> => {
//@ts-ignore
let plugin: VideoRoomPlugin = await this.getSession()?.attachPlugin(VideoRoomPlugin.NAME);
plugin.on('pc:track:remote', (event: any) => {
let feed: RemoteVideo = { feedInfo: publisher, stream: event.streams[0] };
this.emit('videoroom-remote-feed:received', feed);
});
let options: JoinOptions = {
ptype: 'subscriber',
feed: publisher.id,
room: this.joinInfo?.room,
private_id: this.joinInfo?.private_id,
};
let message: JanusPluginMessage = await plugin.join(options);
return { message, plugin };
};
private handleRemotePublishers = async (publishers: Publisher[]) => {
if (!publishers) return;
for (const publisher of publishers) {
let result = await this.handlerPublisher(publisher);
result.plugin.createPeerConnection();
let jsep = await result.plugin.createAnswer(result.message.getPlainMessage().jsep, {});
await result.plugin.start(this.joinInfo?.room, jsep);
}
};
}
Plugin.register(VideoRoomPlugin.NAME, VideoRoomPlugin);
export default VideoRoomPlugin;