chengkun
2025-05-26 8f3df543230cd4403368b39b9bbe5726d11a0284
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* global $ TRTC getCameraId getMicrophoneId resetView isHidden shareUserId addMemberView removeView addVideoView setAnimationFrame clearAnimationFrame*/
class RtcClient {
  constructor(options) {
    this.sdkAppId_ = parseInt(options.sdkAppId);
    this.userId_ = options.userId;
    this.userSig_ = options.userSig;
    this.roomId_ = options.roomId;
    this.privateMapKey_ = options.privateMapKey;
 
    this.isJoined_ = false;
    this.isPublished_ = false;
    this.isAudioMuted = false;
    this.isVideoMuted = false;
    this.localStream_ = null;
    this.remoteStreams_ = [];
    this.members_ = new Map();
    this.getAudioLevelTimer_ = -1;
 
    // create a client for RtcClient
    this.client_ = TRTC.createClient({
      mode: 'rtc',
      sdkAppId: this.sdkAppId_,
      userId: this.userId_,
      userSig: this.userSig_
    });
    this.handleEvents();
  }
 
  async join() {
    if (this.isJoined_) {
      console.warn('duplicate RtcClient.join() observed');
      return;
    }
    try {
      // join the room
      await this.client_.join({
        roomId: parseInt(this.roomId_)
      });
      console.log('join room success');
      this.isJoined_ = true;
 
      // create a local stream with audio/video from microphone/camera
      if (getCameraId() && getMicrophoneId()) {
        this.localStream_ = TRTC.createStream({
          audio: true,
          video: true,
          userId: this.userId_,
          cameraId: getCameraId(),
          microphoneId: getMicrophoneId(),
          mirror: true
        });
      } else {
        // not to specify cameraId/microphoneId to avoid OverConstrainedError
        this.localStream_ = TRTC.createStream({
          audio: true,
          video: true,
          userId: this.userId_,
          mirror: true
        });
      }
      try {
        // initialize the local stream and the stream will be populated with audio/video
        await this.localStream_.initialize();
        console.log('initialize local stream success');
        this.localStream_.on('player-state-changed', event => {
          console.log(`local stream ${event.type} player is ${event.state}`);
            if(event.type == 'video' && event.state == 'PLAYING'){
                spviewStart();
            }
        });
        
      } catch (error) {
        console.error('failed to initialize local stream - ' + error);
        switch (error.name) {
          case 'NotReadableError':
            cameraUsed();
            return;
          case 'NotAllowedError':
            if (error.message === 'Permission denied by system') {
              cameraDenied();
            } else {
              console.log('User refused to share the screen');
            }
            return;
          case 'NotFoundError':
            deviceNotFound();
            return;
          default:
            return;
        }
      }
 
      try {
        // publish the local stream
        await this.publish();
        
        this.localStream_.play('mine-video');
        
        this.localStream_.on('error', error => {
          const errorCode = error.getCode();
          if (errorCode === 16451) {
            // PLAY_NOT_ALLOWED,引导用户手势操作并调用 stream.resume 恢复音视频播放
            let self = this;
            
            let replay = () => {
                this.localStream_.stop()
                this.localStream_.play('mine-video', { muted: false })
                // 移除用户鼠标事件监听
                document.removeEventListener('mousedown', replay)
                // 移除用户触屏事件监听
                document.removeEventListener('touchstart', replay)
              }
              
            document.addEventListener('mousedown', replay);
            document.addEventListener('touchstart', replay);
            this.localStream_.play('mine-video', { muted: true });
            
            vant.Toast({
                message: '请点击屏幕开始视频面试',
                duration: 0,
                overlay: true,
                closeOnClick: true,
                closeOnClickOverlay: true
            });
          }
        })
        
        //$('#main-video-btns').show();
        $('#mask_main').appendTo($('#player_' + this.localStream_.getId()));
        
        // 开始获取音量
        this.startGetAudioLevel();
        
      } catch (error) {
        console.error('failed to publish local stream - ', error);
      }
 
      
    } catch (error) {
      joinFail();
      console.error('join room failed! ' + error);
    }
    //更新成员状态
    let states = this.client_.getRemoteMutedState();
    for (let state of states) {
      if (state.audioMuted) {
        $('#' + state.userId)
          .find('.member-audio-btn')
          .attr('src', './img/mic-off.png');
      }
      if (state.videoMuted) {
        $('#' + state.userId)
          .find('.member-video-btn')
          .attr('src', './img/camera-off.png');
        $('#mask_' + this.members_.get(state.userId).getId()).show();
      }
    }
  }
 
  async leave() {
    if (!this.isJoined_) {
      console.warn('leave() - please join() firstly');
      return;
    }
    // ensure the local stream is unpublished before leaving.
    await this.unpublish();
 
    // leave the room
    await this.client_.leave();
 
    this.localStream_.stop();
    this.localStream_.close();
    this.localStream_ = null;
    this.isJoined_ = false;
    // 停止获取音量
    this.stopGetAudioLevel();
    //resetView();
  }
  async playAllowed(){
    return new Promise((resolve, reject) => {
          
    });
  }
  async publish() {
    if (!this.isJoined_) {
      console.warn('publish() - please join() firstly');
      return;
    }
    if (this.isPublished_) {
      console.warn('duplicate RtcClient.publish() observed');
      return;
    }
    try {
      await this.client_.publish(this.localStream_);
    } catch (error) {
      console.error('failed to publish local stream ' + error);
      this.isPublished_ = false;
    }
 
    this.isPublished_ = true;
  }
 
  async unpublish() {
    if (!this.isJoined_) {
      console.warn('unpublish() - please join() firstly');
      return;
    }
    if (!this.isPublished_) {
      console.warn('RtcClient.unpublish() called but not published yet');
      return;
    }
 
    await this.client_.unpublish(this.localStream_);
    this.isPublished_ = false;
  }
 
  muteLocalAudio() {
    this.localStream_.muteAudio();
  }
 
  unmuteLocalAudio() {
    this.localStream_.unmuteAudio();
  }
 
  muteLocalVideo() {
    this.localStream_.muteVideo();
  }
 
  unmuteLocalVideo() {
    this.localStream_.unmuteVideo();
  }
 
  resumeStreams() {
    this.localStream_.resume();
    for (let stream of this.remoteStreams_) {
      stream.resume();
    }
  }
 
  handleEvents() {
    this.client_.on('error', err => {
      console.error(err);
      alert(err);
      location.reload();
    });
    this.client_.on('client-banned', err => {
      console.error('client has been banned for ' + err);
      if (!isHidden()) {
        alert('您已被踢出房间');
        location.reload();
      } else {
        document.addEventListener(
          'visibilitychange',
          () => {
            if (!isHidden()) {
              alert('您已被踢出房间');
              location.reload();
            }
          },
          false
        );
      }
    });
    // fired when a remote peer is joining the room
    this.client_.on('peer-join', evt => {
      const userId = evt.userId;
      console.log('peer-join ' + userId);
       /*
      if (userId !== shareUserId) {
        addMemberView(userId);
      }
      */
    });
    // fired when a remote peer is leaving the room
    // 对方离开房间
    this.client_.on('peer-leave', evt => {
      const userId = evt.userId;
      removeView(userId);
      console.log('peer-leave ' + userId);
    });
    // fired when a remote stream is added
    this.client_.on('stream-added', evt => {
      const remoteStream = evt.stream;
      const id = remoteStream.getId();
      const userId = remoteStream.getUserId();
      this.members_.set(userId, remoteStream);
      console.log(`remote stream added: [${userId}] ID: ${id} type: ${remoteStream.getType()}`);
      if (remoteStream.getUserId() === shareUserId) {
        // don't need screen shared by us
        this.client_.unsubscribe(remoteStream);
      } else {
        console.log('subscribe to this remote stream');
        this.client_.subscribe(remoteStream);
      }
    });
    // fired when a remote stream has been subscribed
    this.client_.on('stream-subscribed', evt => {
      const uid = evt.userId;
      const remoteStream = evt.stream;
      const id = remoteStream.getId();
      this.remoteStreams_.push(remoteStream);
      remoteStream.on('player-state-changed', event => {
        console.log(`${event.type} player is ${event.state}`);
      });
      addVideoView(id);
      if (remoteStream.userId_ && remoteStream.userId_.indexOf('share_') > -1) {
        remoteStream.play(id, { objectFit: 'contain' }).then(() => {
          // Firefox,当video的controls设置为true的时候,video-box无法监听到click事件
          // if (getBrowser().browser === 'Firefox') {
          //   return;
          // }
          remoteStream.videoPlayer_.element_.controls = true;
        });
      } else {
        remoteStream.play(id);
      }
      remoteStream.on('error', error => {
        const errorCode = error.getCode();
        if (errorCode === 16451) {
          // PLAY_NOT_ALLOWED,引导用户手势操作并调用 stream.resume 恢复音视频播放
          let self = this;
          
          let replay = () => {
              remoteStream.stop()
              remoteStream.play(id, { muted: false })
              // 移除用户鼠标事件监听
              document.removeEventListener('mousedown', replay)
              // 移除用户触屏事件监听
              document.removeEventListener('touchstart', replay)
            }
            
          document.addEventListener('mousedown', replay);
          document.addEventListener('touchstart', replay);
          remoteStream.play(id, { muted: true });
          
          vant.Toast({
              message: '请点击屏幕开始视频面试',
              duration: 0,
              overlay: true,
              closeOnClick: true,
              closeOnClickOverlay: true
          });
        }
      })
      //添加“摄像头未打开”遮罩
      let mask = $('#mask_main').clone();
      mask.attr('id', 'mask_' + id);
      mask.appendTo($('#player_' + id));
      mask.hide();
      if (!remoteStream.hasVideo()) {
        mask.show();
        $('#' + remoteStream.getUserId())
          .find('.member-video-btn')
          .attr('src', 'img/camera-off.png');
      }
      console.log('stream-subscribed ID: ', id);
    });
    // fired when the remote stream is removed, e.g. the remote user called Client.unpublish()
    this.client_.on('stream-removed', evt => {
      const remoteStream = evt.stream;
      const id = remoteStream.getId();
      remoteStream.stop();
      this.remoteStreams_ = this.remoteStreams_.filter(stream => {
        return stream.getId() !== id;
      });
      removeView(id);
      $('#' + remoteStream.getUserId())
        .find('.member-audio-btn')
        .attr('src', 'img/mic-off.png');
      $('#' + remoteStream.getUserId())
        .find('.member-video-btn')
        .attr('src', 'img/camera-off.png');
      console.log(`stream-removed ID: ${id}  type: ${remoteStream.getType()}`);
    });
 
    this.client_.on('stream-updated', evt => {
      const remoteStream = evt.stream;
      let uid = this.getUidByStreamId(remoteStream.getId());
      if (!remoteStream.hasVideo()) {
        $('#' + uid)
          .find('.member-video-btn')
          .attr('src', 'img/camera-off.png');
      }
      console.log(
        'type: ' +
          remoteStream.getType() +
          ' stream-updated hasAudio: ' +
          remoteStream.hasAudio() +
          ' hasVideo: ' +
          remoteStream.hasVideo() +
          ' uid: ' +
          uid
      );
    });
 
    this.client_.on('mute-audio', evt => {
      console.log(evt.userId + ' mute audio');
      $('#' + evt.userId)
        .find('.member-audio-btn')
        .attr('src', 'img/mic-off.png');
    });
    this.client_.on('unmute-audio', evt => {
      console.log(evt.userId + ' unmute audio');
      $('#' + evt.userId)
        .find('.member-audio-btn')
        .attr('src', 'img/mic-on.png');
    });
    this.client_.on('mute-video', evt => {
      console.log(evt.userId + ' mute video');
      $('#' + evt.userId)
        .find('.member-video-btn')
        .attr('src', 'img/camera-off.png');
      const remoteStream = this.members_.get(evt.userId);
      if (remoteStream) {
        let streamId = remoteStream.getId();
        if (streamId) {
          $('#mask_' + streamId).show();
        }
      }
    });
    this.client_.on('unmute-video', evt => {
      console.log(evt.userId + ' unmute video');
      $('#' + evt.userId)
        .find('.member-video-btn')
        .attr('src', 'img/camera-on.png');
      const stream = this.members_.get(evt.userId);
      if (stream) {
        let streamId = stream.getId();
        if (streamId) {
          $('#mask_' + streamId).hide();
        }
      }
    });
  }
 
  showStreamState(stream) {
    console.log('has audio: ' + stream.hasAudio() + ' has video: ' + stream.hasVideo());
  }
 
  getUidByStreamId(streamId) {
    for (let [uid, stream] of this.members_) {
      if (stream.getId() == streamId) {
        return uid;
      }
    }
  }
 
  startGetAudioLevel() {
    // 监听音量回调事件,更新每个用户的音量图标
    this.client_.on('audio-volume', ({ result }) => {
      result.forEach(({ userId, audioVolume }) => {
        if (audioVolume >= 10) {
          console.warn(`userId: ${userId} is speaking audioVolume: ${audioVolume}`);
          $(`#${userId === this.userId_ ? 'member-me' : userId}`)
            .find('.volume-level')
            .css('height', `${audioVolume * 4}%`);
        } else {
          $(`#${userId === this.userId_ ? 'member-me' : userId}`)
            .find('.volume-level')
            .css('height', `0%`);
        }
      });
    });
    this.client_.enableAudioVolumeEvaluation(100);
  }
 
  // 停止获取流音量
  stopGetAudioLevel() {
    this.client_.enableAudioVolumeEvaluation(-1);
  }
}