/// <summary> /// This is an overloaded method,We set the type of camera type. /// </summary> /// <param name="strID"></param> /// <param name="native"></param> public override void setNativeImpl(string strID, long native) { try{ CRhoRuntime.getInstance().logEvent(" Camera class--> setNativeImpl" + strID); base.setNativeImpl(strID, native); cameraType = strID; cameraInformation = initCameraIDs()[cameraType]; } catch (Exception ex) {} try { var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraInformation.Id }; IReadOnlyList <MediaCaptureVideoProfile> profiles = MediaCapture.FindAllVideoProfiles(cameraInformation.Id); foreach (MediaCaptureVideoProfile element in profiles) { foreach (MediaCaptureVideoProfileMediaDescription description in element.SupportedRecordMediaDescription) { try{ imageResolutions.Add(new Size(description.Width, description.Height)); } catch (Exception ex) { CRhoRuntime.getInstance().logEvent("Camera class->" + ex.Message); } } } } catch (Exception e) { } }
private static bool GetBestProfileAndDescription( DeviceInformation videoDevice, out MediaCaptureVideoProfile mediaProfile, out MediaCaptureVideoProfileMediaDescription mediaDescription) { var profiles = MediaCapture.FindAllVideoProfiles(videoDevice.Id); foreach (var profile in profiles) { foreach (var description in profile.SupportedRecordMediaDescription) { System.Diagnostics.Debug.WriteLine("Supported MF Video Profile Description (width: {0}) (height: {1}) (sub-type: {2}) (frame rate: {3})", description.Width, description.Height, description.Subtype, description.FrameRate); if (description.Width == DesiredWidth && description.Height == DesiredHeight && description.FrameRate == DesiredFramerate) { mediaProfile = profile; mediaDescription = description; return(true); } } } mediaProfile = null; mediaDescription = null; return(false); }
/// <summary> /// Gets all the supported media capture modes supported by the current device. /// </summary> /// <returns>A list of supported media capture modes (profile and description).</returns> private async Task <List <(MediaStreamType Type, MediaCaptureVideoProfile Profile, MediaCaptureVideoProfileMediaDescription Description)> > GetSupportedMediaCaptureModesAsync() { var supportedModes = new List <(MediaStreamType Type, MediaCaptureVideoProfile Profile, MediaCaptureVideoProfileMediaDescription Description)>(); var mediaFrameSourceGroups = await MediaFrameSourceGroup.FindAllAsync(); foreach (var mediaFrameSourceGroup in mediaFrameSourceGroups) { var knownProfiles = MediaCapture.FindAllVideoProfiles(mediaFrameSourceGroup.Id); // Search for Video and Preview stream types foreach (var knownProfile in knownProfiles) { foreach (var knownDesc in knownProfile.SupportedRecordMediaDescription) { supportedModes.Add((MediaStreamType.VideoRecord, knownProfile, knownDesc)); } foreach (var knownDesc in knownProfile.SupportedPreviewMediaDescription) { supportedModes.Add((MediaStreamType.VideoPreview, knownProfile, knownDesc)); } } } return(supportedModes); }
public void RefreshVideoProfiles(VideoCaptureDeviceInfo item, VideoProfileKind kind) { var videoProfiles = new CollectionViewModel <MediaCaptureVideoProfile>(); if (item != null) { IReadOnlyList <MediaCaptureVideoProfile> profiles; if (kind == VideoProfileKind.Unspecified) { profiles = MediaCapture.FindAllVideoProfiles(item.Id); } else { int index = (int)kind; profiles = MediaCapture.FindKnownVideoProfiles(item.Id, (KnownVideoProfile)index); } foreach (var profile in profiles) { videoProfiles.Add(profile); } } VideoProfiles = videoProfiles; // Select first item for convenience VideoProfiles.SelectFirstItemIfAny(); }
public void RefreshVideoProfiles(VideoCaptureDeviceInfo item, VideoProfileKind kind) { var videoProfiles = new CollectionViewModel <MediaCaptureVideoProfile>(); if (item != null) { IReadOnlyList <MediaCaptureVideoProfile> profiles; if (kind == VideoProfileKind.Unspecified) { profiles = MediaCapture.FindAllVideoProfiles(item.Id); } else { // VideoProfileKind and KnownVideoProfile are the same with the exception of // `Unspecified` that takes value 0. var profile = (KnownVideoProfile)((int)kind - 1); profiles = MediaCapture.FindKnownVideoProfiles(item.Id, profile); } foreach (var profile in profiles) { videoProfiles.Add(profile); } } VideoProfiles = videoProfiles; // Select first item for convenience VideoProfiles.SelectFirstItemIfAny(); }
private async void ConnectWebCam() { var device = _devices[_cameras.SelectedIndex - 1]; var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = device.Id }; var profiles = MediaCapture.FindAllVideoProfiles(device.Id); var match = (from profile in profiles from desc in profile.SupportedRecordMediaDescription where desc.Width == 640 && desc.Height == 480 && Math.Round(desc.FrameRate) == 30 select new { profile, desc }).FirstOrDefault(); if (match != null) { mediaInitSettings.VideoProfile = match.profile; mediaInitSettings.RecordMediaDescription = match.desc; } else if (profiles.Count > 0) { mediaInitSettings.VideoProfile = profiles[0]; } _displayRequest = new DisplayRequest(); _mediaCapture = new MediaCapture(); _displayRequest.RequestActive(); await _mediaCapture.InitializeAsync(mediaInitSettings); _captureElement.Source = _mediaCapture; _captureElement.Visibility = Visibility.Visible; _captureElement.SetValue(Windows.UI.Xaml.Controls.Grid.RowProperty, 0); _captureElement.SetValue(Windows.UI.Xaml.Controls.Grid.ColumnProperty, 1); await _mediaCapture.StartPreviewAsync(); _cameras.IsEnabled = false; _startVideo.IsEnabled = false; _stopVideo.IsEnabled = true; }
private async Task <MediaCaptureInitializationSettings> getMediaCaptureSettingsAsync(Panel panel, int width, int height, int frameRate) { string deviceId = string.Empty; // Finds all video capture devices DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); foreach (var device in devices) { // Check if the device on the requested panel supports Video Profile if (MediaCapture.IsVideoProfileSupported(device.Id) && device.EnclosureLocation.Panel == panel) { // We've located a device that supports Video Profiles on expected panel deviceId = device.Id; break; } } MediaCaptureInitializationSettings mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = deviceId, StreamingCaptureMode = StreamingCaptureMode.Video }; if (deviceId != String.Empty) { IReadOnlyList <MediaCaptureVideoProfile> profiles = MediaCapture.FindAllVideoProfiles(deviceId); var match = (from profile in profiles from desc in profile.SupportedRecordMediaDescription where desc.Width == width && desc.Height == height && Math.Round(desc.FrameRate) == frameRate select new { profile, desc }).FirstOrDefault(); if (match != null) { mediaInitSettings.VideoProfile = match.profile; mediaInitSettings.RecordMediaDescription = match.desc; } else { // Could not locate rofile, use default video recording profile (or none if device not found) mediaInitSettings.VideoProfile = profiles[0]; } } return(mediaInitSettings); }
private async Task StartPreviewAsync() { mediaCapture = new MediaCapture(); DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); IReadOnlyList <MediaCaptureVideoProfile> profiles = MediaCapture.FindAllVideoProfiles(devices[1].Id); MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); settings.VideoDeviceId = devices[1].Id; await mediaCapture.InitializeAsync(settings); displayRequest.RequestActive(); MyWebcam.Source = mediaCapture; await mediaCapture.StartPreviewAsync(); timer = new Timer(1); timer.Elapsed += Timer_Elapsed; timer.Start(); }
/// <summary> /// Locates a video profile for the back camera and then queries if /// the discovered profile is a matching custom profile. /// If a custom profile is located, we configure media /// settings to the custom profile. Else we use default profile. /// </summary> /// <param name="sender">Contains information regarding button that fired event</param> /// <param name="e">Contains state information and event data associated with the event</param> private async void InitCustomProfileBtn_Click(object sender, RoutedEventArgs e) { MediaCapture mediaCapture = new MediaCapture(); MediaCaptureInitializationSettings mediaInitSettings = new MediaCaptureInitializationSettings(); string videoDeviceId = string.Empty; // For demonstration purposes, we use the Profile Id from WVGA 640x480 30 FPS profile available // on desktop simulator and phone emulators string customProfileId = "{A0E517E8-8F8C-4F6F-9A57-46FC2F647EC0},0"; videoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back); if (string.IsNullOrEmpty(videoDeviceId)) { await LogStatus("ERROR: No Video Device Id found, verify your device supports profiles", NotifyType.ErrorMessage); return; } await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Found video capture device that supports Video Profile, Device Id:\r\n {0}", videoDeviceId)); await LogStatusToOutputBox("Querying device for custom profile support"); mediaInitSettings.VideoProfile = (from profile in MediaCapture.FindAllVideoProfiles(videoDeviceId) where profile.Id == customProfileId select profile).FirstOrDefault(); // In the event the profile isn't located, we set Video Profile to the default if (mediaInitSettings.VideoProfile == null) { await LogStatus("Could not locate a matching profile, setting to default recording profile", NotifyType.ErrorMessage); mediaInitSettings.VideoProfile = MediaCapture.FindAllVideoProfiles(videoDeviceId).FirstOrDefault(); } await LogStatus(string.Format(CultureInfo.InvariantCulture, "Custom recording profile located: {0}", customProfileId), NotifyType.StatusMessage); await mediaCapture.InitializeAsync(mediaInitSettings); }
/// <summary> /// Creates the initialization settings for the MediaCapture object that will support /// all the requested capture settings specified in the configuration object. This method /// will iterate through all the device's video capture profiles to find one that supports /// the requested capture frame dimensions and frame rate. If both Video and Preview streams /// are selected (e.g. for simultaneous mixed reality capture), then the selected profile must /// support the capture modes for both streams. /// </summary> /// <returns> /// A MediaCaptureInitializationSettings object for the first profile that satisfies all the /// requested capture settings in the configuration object, or null if no such profile was found. /// </returns> private async Task <MediaCaptureInitializationSettings> CreateMediaCaptureSettingsAsync() { MediaFrameSourceGroup selectedSourceGroup = null; MediaCaptureVideoProfile profile = null; MediaCaptureVideoProfileMediaDescription videoDesc = null; MediaCaptureVideoProfileMediaDescription previewDesc = null; var mediaFrameSourceGroups = await MediaFrameSourceGroup.FindAllAsync(); // Search all source groups foreach (var mediaFrameSourceGroup in mediaFrameSourceGroups) { // Search for a profile that supports the requested capture modes var knownProfiles = MediaCapture.FindAllVideoProfiles(mediaFrameSourceGroup.Id); foreach (var knownProfile in knownProfiles) { // If a video stream capture mode was specified if (this.configuration.VideoStreamSettings != null) { // Clear any partial matches and continue searching profile = null; videoDesc = null; selectedSourceGroup = null; // Search the supported video (recording) modes for the requested resolution and frame rate foreach (var knownDesc in knownProfile.SupportedRecordMediaDescription) { if (knownDesc.Width == this.configuration.VideoStreamSettings.ImageWidth && knownDesc.Height == this.configuration.VideoStreamSettings.ImageHeight && knownDesc.FrameRate == this.configuration.VideoStreamSettings.FrameRate) { // Found a match for video. Need to also match the requested preview mode (if any) // within the same profile and source group, otherwise we have to keep searching. profile = knownProfile; videoDesc = knownDesc; selectedSourceGroup = mediaFrameSourceGroup; break; } } if (profile == null) { // This profile does not support the requested video stream capture parameters - try the next profile continue; } } // If a preview stream capture mode was specified if (this.configuration.PreviewStreamSettings != null) { // Clear any partial matches and continue searching profile = null; previewDesc = null; selectedSourceGroup = null; // Search the supported preview modes for the requested resolution and frame rate foreach (var knownDesc in knownProfile.SupportedPreviewMediaDescription) { if (knownDesc.Width == this.configuration.PreviewStreamSettings.ImageWidth && knownDesc.Height == this.configuration.PreviewStreamSettings.ImageHeight && knownDesc.FrameRate == this.configuration.PreviewStreamSettings.FrameRate) { // Found a match profile = knownProfile; previewDesc = knownDesc; selectedSourceGroup = mediaFrameSourceGroup; break; } } if (profile == null) { // This profile does not support the requested preview mode - try the next profile continue; } } if (profile != null) { // Found a valid profile that supports the requested capture settings return(new MediaCaptureInitializationSettings { VideoProfile = profile, RecordMediaDescription = videoDesc, PreviewMediaDescription = previewDesc, VideoDeviceId = selectedSourceGroup.Id, StreamingCaptureMode = StreamingCaptureMode.Video, MemoryPreference = MediaCaptureMemoryPreference.Cpu, SharingMode = MediaCaptureSharingMode.ExclusiveControl, SourceGroup = selectedSourceGroup, }); } } } // No matching settings were found return(null); }
private async Task <bool> InitializeMediaCaptureAsync() { if (captureStatus != CaptureStatus.Clean) { Debug.Log(TAG + ": InitializeMediaCaptureAsync() fails because of incorrect status"); return(false); } if (mediaCapture != null) { return(false); } var allGroups = await MediaFrameSourceGroup.FindAllAsync(); int selectedGroupIndex = -1; for (int i = 0; i < allGroups.Count; i++) { var group = allGroups[i]; Debug.Log(group.DisplayName + ", " + group.Id); // for HoloLens 1 if (group.DisplayName == "MN34150") { selectedGroupIndex = i; HL = 1; Debug.Log(TAG + ": Selected group " + i + " on HoloLens 1"); break; } // for HoloLens 2 else if (group.DisplayName == "QC Back Camera") { selectedGroupIndex = i; HL = 2; Debug.Log(TAG + ": Selected group " + i + " on HoloLens 2"); break; } } if (selectedGroupIndex == -1) { Debug.Log(TAG + ": InitializeMediaCaptureAsyncTask() fails because there is no suitable source group"); return(false); } // Initialize mediacapture with the source group. mediaCapture = new MediaCapture(); MediaStreamType mediaStreamType = MediaStreamType.VideoPreview; if (HL == 1) { var settings = new MediaCaptureInitializationSettings { SourceGroup = allGroups[selectedGroupIndex], // This media capture can share streaming with other apps. SharingMode = MediaCaptureSharingMode.SharedReadOnly, // Only stream video and don't initialize audio capture devices. StreamingCaptureMode = StreamingCaptureMode.Video, // Set to CPU to ensure frames always contain CPU SoftwareBitmap images // instead of preferring GPU D3DSurface images. MemoryPreference = MediaCaptureMemoryPreference.Cpu }; await mediaCapture.InitializeAsync(settings); Debug.Log(TAG + ": MediaCapture is successfully initialized in SharedReadOnly mode for HoloLens 1."); mediaStreamType = MediaStreamType.VideoPreview; } else if (HL == 2) { string deviceId = allGroups[selectedGroupIndex].Id; // Look up for all video profiles IReadOnlyList <MediaCaptureVideoProfile> profileList = MediaCapture.FindAllVideoProfiles(deviceId); //MediaCaptureVideoProfile selectedProfile; //IReadOnlyList<MediaCaptureVideoProfile> profileList = MediaCapture.FindKnownVideoProfiles(deviceId, KnownVideoProfile.VideoConferencing); // Initialize mediacapture with the source group. var settings = new MediaCaptureInitializationSettings { SourceGroup = allGroups[selectedGroupIndex], //VideoDeviceId = deviceId, //VideoProfile = profileList[0], // This media capture can share streaming with other apps. SharingMode = MediaCaptureSharingMode.ExclusiveControl, // Only stream video and don't initialize audio capture devices. StreamingCaptureMode = StreamingCaptureMode.Video, // Set to CPU to ensure frames always contain CPU SoftwareBitmap images // instead of preferring GPU D3DSurface images. MemoryPreference = MediaCaptureMemoryPreference.Cpu }; await mediaCapture.InitializeAsync(settings); Debug.Log(TAG + ": MediaCapture is successfully initialized in ExclusiveControl mode for HoloLens 2."); mediaStreamType = MediaStreamType.VideoRecord; } try { var mediaFrameSourceVideo = mediaCapture.FrameSources.Values.Single(x => x.Info.MediaStreamType == mediaStreamType); MediaFrameFormat targetResFormat = null; float framerateDiffMin = 60f; foreach (var f in mediaFrameSourceVideo.SupportedFormats.OrderBy(x => x.VideoFormat.Width * x.VideoFormat.Height)) { // Check current media frame source resolution versus target resolution if (f.VideoFormat.Width == _targetVideoWidth && f.VideoFormat.Height == _targetVideoHeight) { if (targetResFormat == null) { targetResFormat = f; framerateDiffMin = Mathf.Abs(f.FrameRate.Numerator / f.FrameRate.Denominator - _targetVideoFrameRate); } else if (Mathf.Abs(f.FrameRate.Numerator / f.FrameRate.Denominator - _targetVideoFrameRate) < framerateDiffMin) { targetResFormat = f; framerateDiffMin = Mathf.Abs(f.FrameRate.Numerator / f.FrameRate.Denominator - _targetVideoFrameRate); } } } if (targetResFormat == null) { targetResFormat = mediaFrameSourceVideo.SupportedFormats[0]; Debug.Log(TAG + ": Unable to choose the selected format, fall back"); } // choose the smallest resolution //var targetResFormat = mediaFrameSourceVideoPreview.SupportedFormats.OrderBy(x => x.VideoFormat.Width * x.VideoFormat.Height).FirstOrDefault(); // choose the specific resolution //var targetResFormat = mediaFrameSourceVideoPreview.SupportedFormats.OrderBy(x => (x.VideoFormat.Width == 1344 && x.VideoFormat.Height == 756)).FirstOrDefault(); await mediaFrameSourceVideo.SetFormatAsync(targetResFormat); Debug.Log(TAG + ": mediaFrameSourceVideo.SetFormatAsync()"); frameReader = await mediaCapture.CreateFrameReaderAsync(mediaFrameSourceVideo, targetResFormat.Subtype); Debug.Log(TAG + ": mediaCapture.CreateFrameReaderAsync()"); frameReader.FrameArrived += OnFrameArrived; videoWidth = Convert.ToInt32(targetResFormat.VideoFormat.Width); videoHeight = Convert.ToInt32(targetResFormat.VideoFormat.Height); Debug.Log(TAG + ": FrameReader is successfully initialized, " + videoWidth + "x" + videoHeight + ", Framerate: " + targetResFormat.FrameRate.Numerator + "/" + targetResFormat.FrameRate.Denominator); } catch (Exception e) { Debug.Log(TAG + ": FrameReader is not initialized"); Debug.Log(TAG + ": Exception: " + e); return(false); } captureStatus = CaptureStatus.Initialized; return(true); }
/// <summary> /// Locates a video profile for the back camera and then queries if /// the discovered profile supports 640x480 30 FPS recording. /// If a supported profile is located, then we configure media /// settings to the matching profile. Else we use default profile. /// </summary> /// <param name="sender">Contains information regarding button that fired event</param> /// <param name="e">Contains state information and event data associated with the event</param> private async void InitRecordProfileBtn_Click(object sender, RoutedEventArgs e) { MediaCapture mediaCapture = new MediaCapture(); MediaCaptureInitializationSettings mediaInitSettings = new MediaCaptureInitializationSettings(); string videoDeviceId = string.Empty; // Look for a video capture device Id with a video profile matching panel location videoDeviceId = await GetVideoProfileSupportedDeviceIdAsync(Windows.Devices.Enumeration.Panel.Back); if (string.IsNullOrEmpty(videoDeviceId)) { await LogStatus("ERROR: No Video Device Id found, verify your device supports profiles", NotifyType.ErrorMessage); return; } await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Found video capture device that supports Video Profile, Device Id:\r\n {0}", videoDeviceId)); await LogStatusToOutputBox("Retrieving all Video Profiles"); IReadOnlyList <MediaCaptureVideoProfile> profiles = MediaCapture.FindAllVideoProfiles(videoDeviceId); await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Number of Video Profiles found: {0}", profiles.Count)); // Output all Video Profiles found, frame rate is rounded up for display purposes foreach (var profile in profiles) { var description = profile.SupportedRecordMediaDescription; foreach (var desc in description) { await LogStatusToOutputBox(string.Format(CultureInfo.InvariantCulture, "Video Profile Found: Profile Id: {0}\r\n Width: {1} Height: {2} FrameRate: {3}", profile.Id, desc.Width, desc.Height, Math.Round(desc.FrameRate).ToString())); } } // Look for WVGA 30FPS profile await LogStatusToOutputBox("Querying for matching WVGA 30FPS Profile"); var match = (from profile in profiles from desc in profile.SupportedRecordMediaDescription where desc.Width == 640 && desc.Height == 480 && Math.Round(desc.FrameRate) == 30 select new { profile, desc }).FirstOrDefault(); if (match != null) { mediaInitSettings.VideoProfile = match.profile; mediaInitSettings.RecordMediaDescription = match.desc; await LogStatus(string.Format(CultureInfo.InvariantCulture, "Matching WVGA 30FPS Video Profile found: \r\n Profile Id: {0}\r\n Width: {1} Height: {2} FrameRate: {3}", mediaInitSettings.VideoProfile.Id, mediaInitSettings.RecordMediaDescription.Width, mediaInitSettings.RecordMediaDescription.Height, Math.Round(mediaInitSettings.RecordMediaDescription.FrameRate).ToString()), NotifyType.StatusMessage); } else { // Could not locate a WVGA 30FPS profile, use default video recording profile mediaInitSettings.VideoProfile = profiles[0]; await LogStatus("Could not locate a matching profile, setting to default recording profile", NotifyType.ErrorMessage); } await mediaCapture.InitializeAsync(mediaInitSettings); await LogStatusToOutputBox("Media Capture settings initialized to selected profile"); }