示例#1
0
        private async Task UpdatePreviewOrientationAsync()
        {
            var rotation = _rotationHelper.GetCameraPreviewOrientation();
            var props    = MediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(rotation));
            await MediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null).AsTask().ConfigureAwait(false);
        }
示例#2
0
        private async Task RecordAsync(CameraForView cameraForView, JObject options, IPromise promise, CancellationToken token)
        {
            var mediaCapture         = cameraForView.MediaCapture;
            var taskCompletionSource = new TaskCompletionSource <bool>();

            using (var cancellationTokenSource = new CancellationTokenSource())
                using (token.Register(cancellationTokenSource.Cancel))
                    using (cancellationTokenSource.Token.Register(async() => await StopRecordingAsync(mediaCapture, taskCompletionSource)))
                    {
                        var quality         = (VideoEncodingQuality)options.Value <int>("quality");
                        var encodingProfile = MediaEncodingProfile.CreateMp4(quality);

                        mediaCapture.AudioDeviceController.Muted = options.Value <bool>("audio");

                        var orientation = await cameraForView.GetCameraCaptureOrientationAsync().ConfigureAwait(false);

                        encodingProfile.Video.Properties.Add(RotationKey, CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(orientation));

                        var stream = default(InMemoryRandomAccessStream);

                        try
                        {
                            var target     = options.Value <int>("target");
                            var outputFile = default(StorageFile);
                            if (target == CameraCaptureTargetMemory)
                            {
                                stream = new InMemoryRandomAccessStream();
                                await mediaCapture.StartRecordToStreamAsync(encodingProfile, stream).AsTask().ConfigureAwait(false);
                            }
                            else
                            {
                                outputFile = await GetOutputStorageFileAsync(MediaTypeVideo, target).AsTask().ConfigureAwait(false);

                                await mediaCapture.StartRecordToStorageFileAsync(encodingProfile, outputFile).AsTask().ConfigureAwait(false);
                            }

                            if (options.ContainsKey("totalSeconds"))
                            {
                                var totalSeconds = options.Value <double>("totalSeconds");
                                if (totalSeconds > 0)
                                {
                                    cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(totalSeconds));
                                }
                            }

                            await taskCompletionSource.Task.ConfigureAwait(false);

                            if (target == CameraCaptureTargetMemory)
                            {
                                var data = await GetBase64DataAsync(stream).ConfigureAwait(false);

                                promise.Resolve(new JObject
                                {
                                    { "data", data },
                                });
                            }
                            else
                            {
                                await UpdateVideoPropertiesAsync(outputFile, options).ConfigureAwait(false);

                                promise.Resolve(new JObject
                                {
                                    { "path", outputFile.Path },
                                });
                            }
                        }
                        finally
                        {
                            stream?.Dispose();
                        }
                    }

            _recordingCancellation.Dispose();
            _recordingTask = null;
        }