-
Notifications
You must be signed in to change notification settings - Fork 200
/
index.js
366 lines (360 loc) · 10.5 KB
/
index.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
AgoraRTC.onAutoplayFailed = () => {
alert("click to start autoplay!");
};
$(function () {
Vue.component("stream-player", {
template: `
<div class="player" ref="videoRef">
<div class="card-text" class="remote-player-name" v-if="uid">uid:{{uid}}</div>
</div>`,
props: {
videoTrack: {
type: Object,
default: null,
},
// local stream player not need play audioTrack
audioTrack: {
type: Object,
default: null,
},
uid: {
type: [String, Number],
default: "",
},
options: {
type: Object,
default: {},
},
},
data() {
return {};
},
mounted() {},
watch: {
videoTrack: {
handler(newVideoTrack, oldVideoTrack) {
if (newVideoTrack) {
newVideoTrack?.play(this.$refs.videoRef, this.options);
}
},
immediate: true,
},
audioTrack: {
handler(newAudioTrack) {
if (newAudioTrack) {
newAudioTrack.play();
}
},
},
},
});
Vue.component("cam-select", {
template: `<select class="form-select cam-list" v-model="curValue" required="">
<option v-for="(item,index) in options" :key="index" :value="item.value">{{item.label}}</option>
</select>`,
props: {
videoTrack: {
default: null,
type: Object,
},
},
data() {
return {
options: [
{
label: "Default",
value: "default",
deviceId: "",
},
],
curValue: "default",
};
},
mounted() {
this.listen();
},
watch: {
videoTrack: {
async handler(videoTrack) {
if (videoTrack) {
await this.init();
const label = videoTrack.getTrackLabel();
this.curValue = this.options.find((item) => item.label === label).value;
}
},
immediate: true,
},
async curValue(value) {
const target = this.options.find((item) => item.label === value);
if (this.videoTrack && target) {
// switch device of local audio track.
await this.videoTrack.setDevice(target.deviceId);
}
},
},
methods: {
async init() {
// get cameras
const cams = await AgoraRTC.getCameras();
this.options = cams.map((item) => ({
value: item.label,
label: item.label,
deviceId: item.deviceId,
}));
},
listen() {
AgoraRTC.onCameraChanged = async (changedDevice) => {
await this.init();
if (this.videoTrack) {
if (changedDevice.state === "ACTIVE") {
// When plugging in a device, switch to a device that is newly plugged in.
await this.videoTrack.setDevice(changedDevice.device.deviceId);
} else if (changedDevice.device.label === this.videoTrack.getTrackLabel()) {
// Switch to an existing device when the current device is unplugged.
this.options[0] && (await this.videoTrack.setDevice(this.options[0].deviceId));
}
// get new label from track
const label = this.videoTrack.getTrackLabel();
this.curValue = this.options.find((item) => item.label === label).value;
}
};
},
},
});
Vue.component("mic-select", {
template: `<select class="form-select mic-list" v-model="curValue">
<option v-for="(item,index) in options" :key="index" :value="item.value">{{item.label}}</option>
</select>`,
props: {
audioTrack: {
default: null,
type: Object,
},
},
data() {
return {
options: [
{
label: "Default",
value: "default",
},
],
curValue: "default",
};
},
mounted() {
this.listen();
},
watch: {
audioTrack: {
async handler(audioTrack) {
if (audioTrack) {
await this.init();
const label = audioTrack.getTrackLabel();
this.curValue = this.options.find((item) => item.label === label).value;
}
},
immediate: true,
},
async curValue(value) {
const target = this.options.find((item) => item.label === value);
if (this.audioTrack && target) {
// switch device of local audio track.
await this.audioTrack.setDevice(target.deviceId);
}
},
},
methods: {
async init() {
// get cameras
const list = await AgoraRTC.getMicrophones();
this.options = list.map((item) => ({
value: item.label,
label: item.label,
deviceId: item.deviceId,
}));
},
listen() {
AgoraRTC.onMicrophoneChanged = async (changedDevice) => {
await this.init();
if (this.audioTrack) {
// When plugging in a device, switch to a device that is newly plugged in.
if (changedDevice.state === "ACTIVE") {
await this.audioTrack.setDevice(changedDevice.device.deviceId);
// Switch to an existing device when the current device is unplugged.
} else if (changedDevice.device.label === localTracks.audioTrack.getTrackLabel()) {
this.options[0] && (await this.audioTrack.setDevice(this.options[0].deviceId));
}
// get new label from track
const label = this.audioTrack.getTrackLabel();
this.curValue = this.options.find((item) => item.label === label).value;
}
};
},
},
});
new Vue({
el: "#vue-wrapper",
data() {
return {
stepCreateDisabled: false,
stepJoinDisabled: true,
stepPublishDisabled: true,
stepSubscribeDisabled: true,
stepLeaveDisabled: true,
mirrorCheckDisabled: true,
client: null,
options: Object.assign({}, getOptionsFromLocal(), {
uid: "",
}),
audioTrack: null,
videoTrack: null,
localMirror: true,
remoteUid: "",
remoteUsers: {},
audioChecked: true,
videoChecked: true,
micList: [
{
value: "default",
label: "Default",
},
],
camList: [
{
value: "default",
label: "Default",
},
],
};
},
methods: {
stepCreate() {
this.createClient();
addSuccessIcon("#step-create");
message.success("Create client success!");
this.stepCreateDisabled = true;
this.stepJoinDisabled = false;
},
async stepJoin() {
try {
this.options.channel = $("#channel").val();
this.options.uid = Number($("#uid").val());
this.options.token = await agoraGetAppData(this.options);
await this.join();
setOptionsToLocal(this.options);
addSuccessIcon("#step-join");
message.success("Join channel success!");
this.stepJoinDisabled = true;
this.stepPublishDisabled = false;
this.stepSubscribeDisabled = false;
this.stepLeaveDisabled = false;
this.mirrorCheckDisabled = false;
} catch (error) {
message.error(error.message);
console.error(error);
}
},
async stepPublish() {
await this.createTrackAndPublish();
addSuccessIcon("#step-publish");
message.success("Create tracks and publish success!");
this.stepPublishDisabled = true;
this.mirrorCheckDisabled = true;
},
stepSubscribe() {
const user = this.remoteUsers[this.remoteUid];
if (!user) {
return message.error(`User:${this.remoteUid} not found!`);
}
if (this.audioChecked) {
this.subscribe(user, "audio");
}
if (this.videoChecked) {
this.subscribe(user, "video");
}
addSuccessIcon("#step-subscribe");
message.success("Subscribe and Play success!");
},
async stepLeave() {
await this.leave();
message.success("Leave channel success!");
removeAllIcons();
this.stepJoinDisabled = false;
this.stepLeaveDisabled = true;
this.stepPublishDisabled = true;
this.stepSubscribeDisabled = true;
this.mirrorCheckDisabled = true;
this.stepCreateDisabled = false;
this.remoteUid = "";
},
async createTrackAndPublish() {
// create local audio and video tracks
const tracks = await Promise.all([
AgoraRTC.createMicrophoneAudioTrack({
encoderConfig: "music_standard",
}),
AgoraRTC.createCameraVideoTrack(),
]);
this.audioTrack = tracks[0];
this.videoTrack = tracks[1];
// publish local tracks to channel
await this.client.publish(tracks);
},
createClient() {
// create Agora client
this.client = AgoraRTC.createClient({
mode: "rtc",
codec: "vp8",
});
},
async join() {
this.client.on("user-published", this.handleUserPublished);
this.client.on("user-unpublished", this.handleUserUnpublished);
// start Proxy if needed
const mode = Number(this.options.proxyMode);
if (mode != 0 && !isNaN(mode)) {
this.client.startProxyServer(mode);
}
this.options.uid = await this.client.join(
this.options.appid,
this.options.channel,
this.options.token || null,
this.options.uid || null,
);
},
async leave() {
if (this.videoTrack) {
this.videoTrack.close();
this.videoTrack = null;
}
if (this.audioTrack) {
this.audioTrack.close();
this.audioTrack = null;
}
// Remove remote users and player views.
this.remoteUsers = {};
// leave the channel
await this.client.leave();
},
handleUserPublished(user, mediaType) {
this.remoteUid = user.uid;
let remoteUsers = {
...this.remoteUsers,
[user.uid]: user,
};
this.remoteUsers = remoteUsers;
},
handleUserUnpublished(user, mediaType) {
if (mediaType === "video") {
this.$delete(this.remoteUsers, user.uid);
}
},
async subscribe(user, mediaType) {
// subscribe to a remote user
await this.client.subscribe(user, mediaType);
console.log("subscribe success");
},
},
});
});