Exemple #1
0
        private async Task StartPreviewAsync()
        {
            try
            {
                mediaCapture = new MediaCapture();
                await mediaCapture.InitializeAsync();

                PreviewControl.Source = mediaCapture;


                //<SnippetAddVideoEffectAsync>
                var videoEffectDefinition = new VideoEffectDefinition("VideoEffectComponent.ExampleVideoEffect");

                IMediaExtension videoEffect =
                    await mediaCapture.AddVideoEffectAsync(videoEffectDefinition, MediaStreamType.VideoPreview);

                videoEffect.SetProperties(new PropertySet()
                {
                    { "FadeValue", .25 }
                });

                await mediaCapture.StartPreviewAsync();

                //</SnippetAddVideoEffectAsync>
            }
            catch (Exception)
            {
                System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed.");
            }
        }
Exemple #2
0
        public async Task StartAsync()
        {
            if (m_mediaCapture != null)
            {
                throw new InvalidOperationException("Cannot start while recording");
            }

            m_mediaCapture = new MediaCapture();
            await m_mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings()
            {
                MediaCategory        = MediaCategory.Speech,
                AudioProcessing      = AudioProcessing.Default,
                MemoryPreference     = MediaCaptureMemoryPreference.Auto,
                SharingMode          = MediaCaptureSharingMode.SharedReadOnly,
                StreamingCaptureMode = StreamingCaptureMode.Audio,
            });

            m_opusSink = await OpusCodec.CreateMediaSinkAsync(m_file);

            var wawEncodingProfile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.High);

            wawEncodingProfile.Audio.BitsPerSample = 16;
            wawEncodingProfile.Audio.SampleRate    = 48000;
            wawEncodingProfile.Audio.ChannelCount  = 1;
            await m_mediaCapture.StartRecordToCustomSinkAsync(wawEncodingProfile, m_opusSink);
        }
                public async Task StopAsync()
                {
                    try
                    {
                        if (m_lowLag != null)
                        {
                            await m_lowLag.StopAsync();

                            await m_lowLag.FinishAsync();
                        }
                        else
                        {
                            await m_mediaCapture.StopRecordAsync();
                        }

                        m_mediaCapture.Dispose();
                        m_mediaCapture = null;

                        if (m_opusSink is IDisposable disposable)
                        {
                            disposable.Dispose();
                            m_opusSink = null;
                        }
                    }
                    catch { }
                }
Exemple #4
0
            public async Task StartAsync()
            {
                m_isRecording = true;

                if (m_isVideo)
                {
                    var profile       = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
                    var rotationAngle = CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(_rotationHelper.GetCameraCaptureOrientation());
                    profile.Video.Properties.Add(new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1"), PropertyValue.CreateInt32(rotationAngle));

                    m_lowLag = await m_mediaCapture.PrepareLowLagRecordToStorageFileAsync(profile, m_file);

                    await m_lowLag.StartAsync();
                }
                else
                {
                    var wavEncodingProfile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.High);
                    wavEncodingProfile.Audio.BitsPerSample = 16;
                    wavEncodingProfile.Audio.SampleRate    = 48000;
                    wavEncodingProfile.Audio.ChannelCount  = 1;

                    m_opusSink = await OpusCodec.CreateMediaSinkAsync(m_file);

                    await m_mediaCapture.StartRecordToCustomSinkAsync(wavEncodingProfile, m_opusSink);
                }
            }
Exemple #5
0
        /// <summary>
        /// Creates asynchronously the Media Capture which will process the depth stream images
        /// </summary>
        /// <param name="clbk">The Callback object to call when the hand detection status changes.</param>
        /// <returns>The asynchronous task</returns>
        public async Task InitializeAsync(IHDMediaSinkClbk clbk)
        {
            //Create the media capture
            Debug.WriteLine("Creating a media capture...");
            m_mediaCapture = new MediaCapture();
            await m_mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings()
            {
                VideoDeviceId        = m_mediaInfo.DeviceInformation.Id,
                SharingMode          = MediaCaptureSharingMode.SharedReadOnly,
                MemoryPreference     = MediaCaptureMemoryPreference.Auto,      //For the Hololens, MediaCaptureMemoryPreference.CPU does not work
                StreamingCaptureMode = StreamingCaptureMode.Video
            });

            //Find a correct video profile with the best capabilities (resolution)
            Debug.WriteLine("Search a video profile...");
            VideoEncodingProperties videoProfile = null;
            var mediaProperties = m_mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview);

            UInt32 maxHeight = 0;

            foreach (var mediaProp in mediaProperties)
            {
                VideoEncodingProperties videoProp = mediaProp as VideoEncodingProperties;
                Debug.WriteLine($"VideoProp : {videoProp.Type}:{videoProp.Subtype} {videoProp.Width}x{videoProp.Height}");
                if (videoProp.Subtype == "ARGB32" || videoProp.Subtype == "L8" || videoProp.Subtype == "D16" || videoProp.Subtype == "D8" || videoProp.Subtype == "L16" || videoProp.Subtype == "RGB24")
                {
                    if (maxHeight < videoProp.Height)
                    {
                        videoProfile = videoProp;
                        maxHeight    = videoProp.Height;
                    }
                }
            }

            if (videoProfile == null)
            {
                Debug.WriteLine("No video profile found...");
                await Task.FromResult <Windows.Foundation.IAsyncAction>(null);
            }

            else
            {
                Debug.WriteLine($"Starting to preview {m_mediaInfo.DeviceInformation.Name} : {m_mediaInfo.DeviceInformation.Id} at {videoProfile.Width}x{videoProfile.Height}: {videoProfile.Subtype}");

                //Create the video encoding
                MediaEncodingProfile profile = new MediaEncodingProfile();
                profile.Video = videoProfile;
                profile.Audio = null;

                //Create and start preview in the MediaSink
                Debug.WriteLine(m_mediaInfo.DeviceInformation.Name);
                m_mediaSink = new HDMediaSinkProxy();
                IMediaExtension ext = await m_mediaSink.InitializeAsync(clbk, profile.Video);

                await m_mediaCapture.StartPreviewToCustomSinkAsync(profile, ext);
            }

            Debug.WriteLine("End of Create media capture async");
        }
Exemple #6
0
        public async Task StopAsync()
        {
            if (m_mediaCapture == null)
            {
                throw new InvalidOperationException("Cannot stop while not recording");
            }

            await m_mediaCapture.StopRecordAsync();

            m_mediaCapture.Dispose();
            m_mediaCapture = null;

            ((IDisposable)m_opusSink).Dispose();
            m_opusSink = null;
        }
Exemple #7
0
 public async Task StartPreview(IMediaExtension previewSink, double desiredPreviewArea)
 {
     // List of supported video preview formats to be used by the default preview format selector.
     var supportedVideoFormats = new List<string> { "nv12", "rgb32" };
     // Find the supported preview size that's closest to the desired size
     var availableMediaStreamProperties =
     mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview)
     .OfType<VideoEncodingProperties>()
     .Where(p => p != null && !String.IsNullOrEmpty(p.Subtype) && supportedVideoFormats.Contains(p.Subtype.ToLower()))
     .OrderBy(p => Math.Abs(p.Height * p.Width - desiredPreviewArea))
     .ToList();
     var previewFormat = availableMediaStreamProperties.FirstOrDefault();
     // Start Preview stream
     await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, previewFormat);
     await mediaCapture.StartPreviewToCustomSinkAsync(new MediaEncodingProfile { Video = previewFormat }, previewSink);
 }
                public void Dispose()
                {
                    try
                    {
                        m_lowLag = null;

                        m_mediaCapture.Dispose();
                        m_mediaCapture = null;

                        if (m_opusSink is IDisposable disposable)
                        {
                            disposable.Dispose();
                            m_opusSink = null;
                        }
                    }
                    catch { }
                }
        private async void BasicAddEffect()
        {
            //<SnippetBasicAddEffect>
            if (_mediaCapture.MediaCaptureSettings.VideoDeviceCharacteristic == VideoDeviceCharacteristic.AllStreamsIdentical ||
                _mediaCapture.MediaCaptureSettings.VideoDeviceCharacteristic == VideoDeviceCharacteristic.PreviewRecordStreamsIdentical)
            {
                // This effect will modify both the preview and the record streams, because they are the same stream.
                myRecordEffect = await _mediaCapture.AddVideoEffectAsync(myEffectDefinition, MediaStreamType.VideoRecord);
            }
            else
            {
                myRecordEffect = await _mediaCapture.AddVideoEffectAsync(myEffectDefinition, MediaStreamType.VideoRecord);

                myPreviewEffect = await _mediaCapture.AddVideoEffectAsync(myEffectDefinition, MediaStreamType.VideoPreview);
            }
            //</SnippetBasicAddEffect>
        }
Exemple #10
0
        public async Task StartPreview(IMediaExtension previewSink, double desiredPreviewArea)
        {
            // List of supported video preview formats to be used by the default preview format selector.
            var supportedVideoFormats = new List <string> {
                "nv12", "rgb32"
            };
            // Find the supported preview size that's closest to the desired size
            var availableMediaStreamProperties =
                mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview)
                .OfType <VideoEncodingProperties>()
                .Where(p => p != null && !String.IsNullOrEmpty(p.Subtype) && supportedVideoFormats.Contains(p.Subtype.ToLower()))
                .OrderBy(p => Math.Abs(p.Height * p.Width - desiredPreviewArea))
                .ToList();
            var previewFormat = availableMediaStreamProperties.FirstOrDefault();
            // Start Preview stream
            await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, previewFormat);

            await mediaCapture.StartPreviewToCustomSinkAsync(new MediaEncodingProfile { Video = previewFormat }, previewSink);
        }
Exemple #11
0
        private async Task StartPreviewAsync()
        {
            try
            {
                mediaCapture = new MediaCapture();
                var videoEffectDefinition = new VideoEffectDefinition("segm_video_effect_uwp.SegmVideoEffect");

                await mediaCapture.InitializeAsync();

                videoEffect =
                    await mediaCapture.AddVideoEffectAsync(videoEffectDefinition, MediaStreamType.VideoPreview);

                displayRequest.RequestActive();
                DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
            }
            catch (UnauthorizedAccessException)
            {
                // This will be thrown if the user denied access to the camera in privacy settings
                ShowMessageToUser("The app was denied access to the camera");
                return;
            }

            try
            {
                PreviewControl.Source = mediaCapture;
                await mediaCapture.StartPreviewAsync();

                isPreviewing = true;
            }
            catch (System.IO.FileLoadException)
            {
                mediaCapture.CaptureDeviceExclusiveControlStatusChanged += _mediaCapture_CaptureDeviceExclusiveControlStatusChanged;
                return;
            }
            catch (System.Exception)
            {
                return;
            }
        }
Exemple #12
0
        private async void CoreApplication_LeavingBackground(object sender, LeavingBackgroundEventArgs e)
        {
            var Deferral = e.GetDeferral();

            if (ApplicationData.Current.LocalSettings.Values["LastSelectedCameraSource"] is string LastSelectedCameraSource)
            {
                var MediaFraSourceGroup = await MediaFrameSourceGroup.FindAllAsync();

                foreach (var FrameSource in from MediaFrameSourceGroup FrameSource in MediaFraSourceGroup
                         where FrameSource.DisplayName == LastSelectedCameraSource
                         select FrameSource)
                {
                    CaptureControl.Source = Capture = await MediaCaptureProvider.SetFrameSourceAndInitializeCaptureAsync(FrameSource);

                    break;
                }
            }
            else
            {
                CaptureControl.Source = Capture = await MediaCaptureProvider.SetFrameSourceAndInitializeCaptureAsync();
            }

            ApplicationData.Current.LocalSettings.Values["ReturnCosmeticsEffectExcution"] = true;
            VideoEffectDefinition EffectDefinition = new VideoEffectDefinition("CosmeticsEffect.CosmeticsVideoEffect");

            ApplicationData.Current.LocalSettings.Values["ReturnCosmeticsEffectExcution"] = false;
            VideoEffect = await Capture.AddVideoEffectAsync(EffectDefinition, MediaStreamType.VideoPreview);

            CaptureControl.Source = Capture;
            await Capture.StartPreviewAsync();

            VideoEffect.SetProperties(new PropertySet()
            {
                { "LipColor", (CosmeticsControl.SelectedItem as CosmeticsItem).LipColor }
            });
            StayAwake.RequestActive();

            Deferral.Complete();
        }
Exemple #13
0
        private async Task StartPreviewAsyncWin2D()
        {
            try
            {
                mediaCapture = new MediaCapture();
                await mediaCapture.InitializeAsync();

                PreviewControl.Source = mediaCapture;

                //TEMP
                IEnumerable <StreamPropertiesHelper> allStreamProperties =
                    mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).Select(x => new StreamPropertiesHelper(x));
                allStreamProperties = allStreamProperties.OrderByDescending(x => x.Height * x.Width);
                await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, allStreamProperties.Last().EncodingProperties);


                //<SnippetAddVideoEffectAsyncWin2D>
                var videoEffectDefinition = new VideoEffectDefinition("VideoEffectComponent.ExampleVideoEffectWin2D");

                IMediaExtension videoEffect =
                    await mediaCapture.AddVideoEffectAsync(videoEffectDefinition, MediaStreamType.VideoPreview);

                videoEffect.SetProperties(new PropertySet()
                {
                    { "BlurAmount", 1.0 }
                });

                await mediaCapture.StartPreviewAsync();

                //</SnippetAddVideoEffectAsyncWin2D>
            }
            catch (Exception)
            {
                System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed.");
            }
        }
Exemple #14
0
            public async Task StartAsync()
            {
                m_isRecording = true;

                if (m_isVideo)
                {
                    var profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);

                    m_lowLag = await m_mediaCapture.PrepareLowLagRecordToStorageFileAsync(profile, m_file);

                    await m_lowLag.StartAsync();
                }
                else
                {
                    var wavEncodingProfile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.High);
                    wavEncodingProfile.Audio.BitsPerSample = 16;
                    wavEncodingProfile.Audio.SampleRate    = 48000;
                    wavEncodingProfile.Audio.ChannelCount  = 1;

                    m_opusSink = await OpusCodec.CreateMediaSinkAsync(m_file);

                    await m_mediaCapture.StartRecordToCustomSinkAsync(wavEncodingProfile, m_opusSink);
                }
            }
Exemple #15
0
        public async Task StartRecordToSinkAsync(MediaEncodingProfile encodingProfile)
        {
            lock (this)
            {
                isInitialized.CheckIfFulfills("Camera", "initialized", true);
                isRecording.CheckIfFulfills("Camera", "recording", false);
                isRecording = true;
            }

            CleanupSink();
            mediaSink = new StspMediaSinkProxy();
            mediaSink.IncomingConnectionEvent += MediaSink_IncomingConnectionEvent;
            try
            {
                IMediaExtension mfExtension = await mediaSink.InitializeAsync(encodingProfile.Audio, encodingProfile.Video);

                await mediaCapture.StartRecordToCustomSinkAsync(encodingProfile, mfExtension);
            }
            catch (Exception ex)
            {
                CleanupSink();
                throw new Exception($"Camera could not start record to sink: { ex.Message }. { Logger.GetExceptionLocalization(this) }");
            }
        }
Exemple #16
0
        private async void Cosmetics_Loaded(object sender, RoutedEventArgs e)
        {
            ExitLocker    = new AutoResetEvent(false);
            CosmeticsList = new ObservableCollection <CosmeticsItem>();
            Cancellation  = new CancellationTokenSource();
            CosmeticsControl.ItemsSource = CosmeticsList;

            //以下为加载美妆图片和信息的过程
            StorageFolder LipFolder = await(await Package.Current.InstalledLocation.GetFolderAsync("Cosmetics")).GetFolderAsync("LipLogo");
            var           LipLogo   = await LipFolder.GetFilesAsync();

            for (int i = 0; i < LipLogo.Count; i++)
            {
                Color  LipColor = Colors.Red;
                string Describe = string.Empty;
                switch (LipLogo[i].DisplayName)
                {
                case "Dior": LipColor = Color.FromArgb(1, 100, 0, 30); Describe = "法国著名时尚消费品牌"; break;

                case "CHANEL": LipColor = Color.FromArgb(1, 70, 0, 0); Describe = "法国著名奢侈品品牌"; break;

                case "GIVENCHY": LipColor = Color.FromArgb(1, 100, 30, 30); Describe = "法国著名时尚消费品牌"; break;

                case "M.A.C": LipColor = Color.FromArgb(1, 50, 0, 50); Describe = "美国著名化妆品品牌"; break;

                case "LANCOME": LipColor = Color.FromArgb(1, 100, 0, 0); Describe = "法国著名化妆品品牌"; break;
                }
                CosmeticsList.Add(new CosmeticsItem(new Uri("ms-appx:///Cosmetics/LipLogo/" + LipLogo[i].Name), LipLogo[i].DisplayName, Describe, LipColor));
            }

            //读取设置模块中指定的摄像头,并设置为当前使用的摄像头
            if (ApplicationData.Current.LocalSettings.Values["LastSelectedCameraSource"] is string LastSelectedCameraSource)
            {
                var MediaFraSourceGroup = await MediaFrameSourceGroup.FindAllAsync();

                foreach (var FrameSource in from MediaFrameSourceGroup FrameSource in MediaFraSourceGroup
                         where FrameSource.DisplayName == LastSelectedCameraSource
                         select FrameSource)
                {
                    CaptureControl.Source = Capture = await MediaCaptureProvider.SetFrameSourceAndInitializeCaptureAsync(FrameSource);

                    break;
                }
            }
            else
            {
                CaptureControl.Source = Capture = await MediaCaptureProvider.SetFrameSourceAndInitializeCaptureAsync();
            }

            if (Capture != null)
            {
                await Task.Run(async() =>
                {
                    ApplicationData.Current.LocalSettings.Values["ReturnCosmeticsEffectExcution"] = true;
                    VideoEffectDefinition EffectDefinition = new VideoEffectDefinition("CosmeticsEffect.CosmeticsVideoEffect");

                    ApplicationData.Current.LocalSettings.Values["ReturnCosmeticsEffectExcution"] = false;
                    VideoEffect = await Capture.AddVideoEffectAsync(EffectDefinition, MediaStreamType.VideoPreview);
                });

                if (!Cancellation.IsCancellationRequested)
                {
                    await Capture.StartPreviewAsync();

                    CosmeticsControl.SelectedIndex = 0;

                    LoadingControl.IsLoading = false;
                }
            }
            else
            {
                ContentDialog dialog = new ContentDialog
                {
                    Title           = "错误",
                    Content         = "无可用的摄像头设备或设备异常,请检查摄像头连接",
                    CloseButtonText = "返回",
                    Background      = Application.Current.Resources["DialogAcrylicBrush"] as Brush
                };
                _ = await dialog.ShowAsync();

                if (MainPage.ThisPage.NavFrame.CanGoBack)
                {
                    MainPage.ThisPage.NavFrame.GoBack();
                }
            }

            StayAwake = new DisplayRequest();
            StayAwake.RequestActive();

            ExitLocker.Set();
        }
        public async Task ChangeLiveStream()
        {
            Debug.WriteLine("ChangeLiveStream");
            await CleanupCameraAsync();

            SaveEnabled = false;

            // If webcam hasn't been initialized, bail.
            if ((_devices == null) || (_devices.Count == 0))
            {
                return;
            }

            try
            {
                // Check that SCI hasn't < 0
                // Probably -1 when doesn't find camera, or when list gone?
                if (_appModel.SelectedCameraIndex < 0)
                {
                    Debug.WriteLine("selectedCamera < 0");
                    NotifyUser(false, "Invalid Camera selected, using default");
                    _appModel.SelectedCameraIndex = 0;
                    return;
                }
                var device = _devices.ToList().ElementAt(_appModel.SelectedCameraIndex);
                // Create MediaCapture and its settings
                var settings = new MediaCaptureInitializationSettings
                {
                    VideoDeviceId        = device.Id,
                    PhotoCaptureSource   = PhotoCaptureSource.Auto,
                    MemoryPreference     = UseGpu ? MediaCaptureMemoryPreference.Auto : MediaCaptureMemoryPreference.Cpu,
                    StreamingCaptureMode = StreamingCaptureMode.Video,
                    MediaCategory        = MediaCategory.Communications,
                };
                _mediaCapture = new MediaCapture();
                await _mediaCapture.InitializeAsync(settings);

                _displayrequest.RequestActive();

                var capture = new CaptureElement();
                capture.Source = _mediaCapture;
                _appModel.OutputCaptureElement = capture;

                var modelPath = Path.GetFullPath($"./Assets/{_appModel.ModelSource}.onnx");
                VideoEffectDefinition videoEffectDefinition = new VideoEffectDefinition(StyleTransferEffectId, new PropertySet()
                {
                    { "ModelName", modelPath },
                    { "UseGpu", UseGpu },
                    { "Notifier", _notifier },
                    { "NumThreads", NumThreads }
                });
                IMediaExtension videoEffect = await _mediaCapture.AddVideoEffectAsync(videoEffectDefinition, MediaStreamType.VideoPreview);

                var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
                CaptureFPS = props.FrameRate.Numerator / props.FrameRate.Denominator;

                await _mediaCapture.StartPreviewAsync();

                _isPreviewing = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }