Browse Source

Handle cases when new media devices are added/removed more precisely and more predictable

master
Kostiantyn Tsaregradskyi 9 years ago
parent
commit
c1807c3649
1 changed files with 47 additions and 15 deletions
  1. 47
    15
      conference.js

+ 47
- 15
conference.js View File

22
 const TrackErrors = JitsiMeetJS.errors.track;
22
 const TrackErrors = JitsiMeetJS.errors.track;
23
 
23
 
24
 let room, connection, localAudio, localVideo, roomLocker;
24
 let room, connection, localAudio, localVideo, roomLocker;
25
+let currentAudioInputDevices, currentVideoInputDevices;
25
 
26
 
26
 import {VIDEO_CONTAINER_TYPE} from "./modules/UI/videolayout/LargeVideo";
27
 import {VIDEO_CONTAINER_TYPE} from "./modules/UI/videolayout/LargeVideo";
27
 
28
 
197
     });
198
     });
198
 }
199
 }
199
 
200
 
201
+/**
202
+ * Stores lists of current 'audioinput' and 'videoinput' devices
203
+ * @param {MediaDeviceInfo[]} devices
204
+ */
205
+function setCurrentMediaDevices(devices) {
206
+    currentAudioInputDevices = devices.filter(
207
+        d => d.kind === 'audioinput');
208
+    currentVideoInputDevices = devices.filter(
209
+        d => d.kind === 'videoinput');
210
+}
211
+
200
 class ConferenceConnector {
212
 class ConferenceConnector {
201
     constructor(resolve, reject) {
213
     constructor(resolve, reject) {
202
         this._resolve = resolve;
214
         this._resolve = resolve;
408
                             localVideo.getDeviceId());
420
                             localVideo.getDeviceId());
409
                     }
421
                     }
410
 
422
 
423
+                    setCurrentMediaDevices(devices);
424
+
411
                     APP.UI.onAvailableDevicesChanged(devices);
425
                     APP.UI.onAvailableDevicesChanged(devices);
412
                 });
426
                 });
413
 
427
 
419
                         window.setTimeout(() => {
433
                         window.setTimeout(() => {
420
                             checkLocalDevicesAfterDeviceListChanged(devices)
434
                             checkLocalDevicesAfterDeviceListChanged(devices)
421
                                 .then(() => {
435
                                 .then(() => {
436
+                                    setCurrentMediaDevices(devices);
437
+
422
                                     APP.UI.onAvailableDevicesChanged(devices);
438
                                     APP.UI.onAvailableDevicesChanged(devices);
423
                                 });
439
                                 });
424
                         }, 0);
440
                         }, 0);
453
             }
469
             }
454
 
470
 
455
             function checkLocalDevicesAfterDeviceListChanged(newDevices) {
471
             function checkLocalDevicesAfterDeviceListChanged(newDevices) {
472
+                // Event handler can be fire before direct enumerateDevices()
473
+                // call, so handle this situation here.
474
+                if (!currentAudioInputDevices && !currentVideoInputDevices) {
475
+                    setCurrentMediaDevices(newDevices);
476
+                }
477
+
456
                 checkAudioOutputDeviceAfterDeviceListChanged(newDevices);
478
                 checkAudioOutputDeviceAfterDeviceListChanged(newDevices);
457
 
479
 
458
                 let availableAudioInputDevices = newDevices.filter(
480
                 let availableAudioInputDevices = newDevices.filter(
555
                 }
577
                 }
556
 
578
 
557
                 function onTracksCreated(tracks) {
579
                 function onTracksCreated(tracks) {
558
-                    tracks && tracks.forEach(track => {
580
+                    return Promise.all((tracks || []).map(track => {
559
                         if (track.isAudioTrack()) {
581
                         if (track.isAudioTrack()) {
560
-                            self.useAudioStream(track).then(() => {
582
+                            let audioWasMuted = self.audioMuted;
583
+
584
+                            return self.useAudioStream(track).then(() => {
561
                                 console.log('switched local audio');
585
                                 console.log('switched local audio');
562
 
586
 
563
-                                // If we have more than 1 device - mute.
564
-                                // We check with 2 for audio, because
565
-                                // it always has 'default' if device is
566
-                                // available at all.
567
-                                // TODO: this is not 100% solution - need
568
-                                // to investigate more
569
-                                if (availableAudioInputDevices.length > 2) {
587
+                                // If we plugged-in new device (and switched to
588
+                                // it), but video was muted before, or we
589
+                                // unplugged current device and selected new
590
+                                // one, then mute new video track.
591
+                                if (audioWasMuted ||
592
+                                    currentAudioInputDevices.length >
593
+                                    availableAudioInputDevices.length) {
570
                                     muteLocalAudio(true);
594
                                     muteLocalAudio(true);
571
                                 }
595
                                 }
572
                             });
596
                             });
573
                         } else if (track.isVideoTrack()) {
597
                         } else if (track.isVideoTrack()) {
574
-                            self.useVideoStream(track).then(() => {
598
+                            let videoWasMuted = self.videoMuted;
599
+
600
+                            return self.useVideoStream(track).then(() => {
575
                                 console.log('switched local video');
601
                                 console.log('switched local video');
576
 
602
 
577
                                 // TODO: maybe make video large if we
603
                                 // TODO: maybe make video large if we
578
                                 // are not in conference yet
604
                                 // are not in conference yet
579
-                                // If we have more than 1 device - mute.
580
-                                // TODO: this is not 100% solution - need
581
-                                // to investigate more
582
-                                if (availableVideoInputDevices.length > 1) {
605
+
606
+                                // If we plugged-in new device (and switched to
607
+                                // it), but video was muted before, or we
608
+                                // unplugged current device and selected new
609
+                                // one, then mute new video track.
610
+                                if (videoWasMuted ||
611
+                                    (currentVideoInputDevices.length >
612
+                                    availableVideoInputDevices.length)) {
583
                                     muteLocalVideo(true);
613
                                     muteLocalVideo(true);
584
                                 }
614
                                 }
585
                             });
615
                             });
586
                         } else {
616
                         } else {
587
                             console.error("Ignored not an audio nor a "
617
                             console.error("Ignored not an audio nor a "
588
                                 + "video track: ", track);
618
                                 + "video track: ", track);
619
+
620
+                            return Promise.resolve();
589
                         }
621
                         }
590
-                    });
622
+                    }));
591
                 }
623
                 }
592
             }
624
             }
593
         });
625
         });

Loading…
Cancel
Save