@flashphoner/websdk 2.0.276 → 2.0.277

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -601,6 +601,14 @@ var createConnection = function (options) {
601
601
  };
602
602
 
603
603
  var switchCam = function (deviceId) {
604
+ const releaseCam = function(stream) {
605
+ if (stream) {
606
+ stream.getTracks().forEach(function (track) {
607
+ track.stop();
608
+ stream.removeTrack(track);
609
+ });
610
+ }
611
+ };
604
612
  return new Promise(function (resolve, reject) {
605
613
  if (localVideo && localVideo.srcObject && videoCams.length > 1 && !customStream && !screenShare) {
606
614
  connection.getSenders().forEach(function (sender) {
@@ -627,17 +635,22 @@ var createConnection = function (options) {
627
635
  clonedConstraints.video.deviceId = {exact: cameraId};
628
636
  clonedConstraints.audio = false;
629
637
  navigator.mediaDevices.getUserMedia(clonedConstraints).then(function (newStream) {
630
- var newVideoTrack = newStream.getVideoTracks()[0];
631
- newVideoTrack.enabled = localVideo.srcObject.getVideoTracks()[0].enabled;
632
- var audioTrack = localVideo.srcObject.getAudioTracks()[0];
633
- sender.replaceTrack(newVideoTrack);
634
- localVideo.srcObject = newStream;
635
- // On Safari mobile _newStream_ doesn't contain audio track, so we need to add track from previous stream
636
- if (localVideo.srcObject.getAudioTracks().length == 0 && audioTrack) {
637
- localVideo.srcObject.addTrack(audioTrack);
638
+ if (localVideo && localVideo.srcObject) {
639
+ var newVideoTrack = newStream.getVideoTracks()[0];
640
+ newVideoTrack.enabled = localVideo.srcObject.getVideoTracks()[0].enabled;
641
+ var audioTrack = localVideo.srcObject.getAudioTracks()[0];
642
+ sender.replaceTrack(newVideoTrack);
643
+ localVideo.srcObject = newStream;
644
+ // On Safari mobile _newStream_ doesn't contain audio track, so we need to add track from previous stream
645
+ if (localVideo.srcObject.getAudioTracks().length == 0 && audioTrack) {
646
+ localVideo.srcObject.addTrack(audioTrack);
647
+ }
648
+ logger.info(LOG_PREFIX, "Switch camera to " + cameraId);
649
+ resolve(cameraId);
650
+ } else {
651
+ releaseCam(newStream);
652
+ reject("Can't switch camera to " + cameraId + ", video renderer is already closed");
638
653
  }
639
- logger.info(LOG_PREFIX, "Switch camera to " + cameraId);
640
- resolve(cameraId);
641
654
  }).catch(function (reason) {
642
655
  logger.error(LOG_PREFIX, reason);
643
656
  reject(reason);