From bb2f598771790c1e92b552f32a5c96946cd42801 Mon Sep 17 00:00:00 2001 From: strongstrongyiwen <63120519+strongstrongyiwen@users.noreply.github.com> Date: Thu, 20 Aug 2020 20:42:55 -0700 Subject: [PATCH 1/2] Update camera.js Added reset media stream properties before preview to avoid crash when sharing from MJPG stream --- archived/CameraStarterKit/js/js/camera.js | 31 +++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/archived/CameraStarterKit/js/js/camera.js b/archived/CameraStarterKit/js/js/camera.js index a2d7e9025a..6ee34c5e6a 100644 --- a/archived/CameraStarterKit/js/js/camera.js +++ b/archived/CameraStarterKit/js/js/camera.js @@ -138,7 +138,9 @@ return oMediaCapture.initializeAsync(settings) .then(function () { isInitialized = true; - startPreview(); + // Get all available media stream properties and select the first one as default + var allProperties = oMediaCapture.videoDeviceController.getAvailableMediaStreamProperties(Capture.MediaStreamType.videoPreview); + startPreview(oMediaCapture, Capture.MediaStreamType.videoPreview, allProperties[0]); }); }, function (error) { console.log(error.message); @@ -207,9 +209,10 @@ } /// + /// Sets encoding properties on a camera stream. Ensures VideoElement and preview stream are stopped before setting properties. /// Starts the preview and adjusts it for for rotation and mirroring after making a request to keep the screen on /// - function startPreview() { + function startPreview(mediaCapture, streamType, encodingProperties) { // Prevent the device from sleeping while the preview is running oDisplayRequest.requestActive(); @@ -219,15 +222,21 @@ cameraPreview.style.transform = "scale(-1, 1)"; } - var previewUrl = URL.createObjectURL(oMediaCapture); - previewVidTag.src = previewUrl; - previewVidTag.play(); - - previewVidTag.addEventListener("playing", function () { - isPreviewing = true; - updateCaptureControls(); - setPreviewRotationAsync(); - }); + // Apply desired stream properties + return mediaCapture.videoDeviceController.setMediaStreamPropertiesAsync(streamType, encodingProperties) + .then(function () { + // Recreate pipeline and restart the preview + previewVidTag = document.getElementById("cameraPreview"); + var previewUrl = URL.createObjectURL(mediaCapture); + previewVidTag.src = previewUrl; + previewVidTag.play(); + + previewVidTag.addEventListener("playing", function () { + isPreviewing = true; + updateCaptureControls(); + setPreviewRotationAsync(); + }); + }); } /// From 02c7cefedad5b5f946b03cc518d4db66c505900d Mon Sep 17 00:00:00 2001 From: strongstrongyiwen <63120519+strongstrongyiwen@users.noreply.github.com> Date: Fri, 28 Aug 2020 11:21:57 -0700 Subject: [PATCH 2/2] Update camera.js Added showErrorMessage function to pop up error message dialog when media capture failed (in most cases camera is being used by another app) to avoid exception caused crash of the app. --- archived/CameraStarterKit/js/js/camera.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/archived/CameraStarterKit/js/js/camera.js b/archived/CameraStarterKit/js/js/camera.js index 6ee34c5e6a..66946a01c9 100644 --- a/archived/CameraStarterKit/js/js/camera.js +++ b/archived/CameraStarterKit/js/js/camera.js @@ -234,7 +234,6 @@ previewVidTag.addEventListener("playing", function () { isPreviewing = true; updateCaptureControls(); - setPreviewRotationAsync(); }); }); } @@ -565,12 +564,30 @@ function mediaCapture_failed(errorEventArgs) { console.log("MediaCapture_Failed: 0x" + errorEventArgs.code + ": " + errorEventArgs.message); + showErrorMessage(errorEventArgs.message); cleanupCameraAsync() .done(function() { updateCaptureControls(); }); } + + /// + /// Show error message when media capture failed + /// Click on try again button will re-initialize and have another try + /// + function showErrorMessage(errormessage) { + var msg = new Windows.UI.Popups.MessageDialog( + "Looks like your camera is being used by another app! If you need it, here's the error message: " + + errormessage); + msg.commands.append(new Windows.UI.Popups.UICommand( + "Try again", + function () { + initializeCameraAsync(); + }) + ); + msg.showAsync(); + } app.start(); })();