/// <summary> /// Called once all the subscriptions are established. /// </summary> private void OnPipelineStart() { MediaCaptureDevice.Initialize(); CaptureFormat found = null; foreach (var device in MediaCaptureDevice.AllDevices) { if (!device.Attach(this.configuration.UseInSharedMode)) { continue; } Trace.WriteLine($"MediaCapture - Searching for width={this.configuration.Width} height={this.configuration.Height} deviceId={this.configuration.DeviceId}"); Trace.WriteLine($"MediaCapture - Found: Name: '{device.FriendlyName}' SymLink: {device.SymbolicLink}"); Trace.WriteLine($"MediaCapture - Current - Width: {device.CurrentFormat.nWidth} Height: {device.CurrentFormat.nHeight} Type: {device.CurrentFormat.subType.Name}/{device.CurrentFormat.subType.Guid} Framerate: {device.CurrentFormat.nFrameRateNumerator}/{device.CurrentFormat.nFrameRateDenominator}"); if (string.IsNullOrEmpty(this.configuration.DeviceId) || device.FriendlyName == this.configuration.DeviceId || device.SymbolicLink == this.configuration.DeviceId) { foreach (var format in device.Formats) { Trace.WriteLine($"MediaCapture - Supported - Width: {format.nWidth} Height: {format.nHeight} Type: {format.subType.Name}/{format.subType.Guid} Framerate: {format.nFrameRateNumerator}/{format.nFrameRateDenominator}"); if (this.configuration.Width == format.nWidth && this.configuration.Height == format.nHeight) { // found suitable width/height if (this.configuration.Framerate == format.nFrameRateNumerator / format.nFrameRateDenominator) { // found suitable framerate if (found == null || this.configuration.Framerate == found.nFrameRateNumerator / found.nFrameRateDenominator) { // found first suitable or closer framerate match this.camera = device; found = format; } } } } } if (found != null) { Trace.WriteLine($"MediaCapture - Using - Width: {found.nWidth} Height: {found.nHeight} Type: {found.subType.Name}/{found.subType.Guid} Framerate: {found.nFrameRateNumerator}/{found.nFrameRateDenominator}"); break; } } if (found != null) { this.camera.CurrentFormat = found; this.deviceInfo = new MediaCaptureInfo(this.camera); var width = this.camera.CurrentFormat.nWidth; var height = this.camera.CurrentFormat.nHeight; // Get default settings for other properties. var currentConfig = this.GetDeviceConfiguration(); this.configuration.BacklightCompensation = currentConfig.BacklightCompensation; this.configuration.Brightness = currentConfig.Brightness; this.configuration.ColorEnable = currentConfig.ColorEnable; this.configuration.Contrast = currentConfig.Contrast; this.configuration.Gain = currentConfig.Gain; this.configuration.Gamma = currentConfig.Gamma; this.configuration.Hue = currentConfig.Hue; this.configuration.Saturation = currentConfig.Saturation; this.configuration.Sharpness = currentConfig.Sharpness; this.configuration.WhiteBalance = currentConfig.WhiteBalance; this.configuration.Focus = currentConfig.Focus; this.SetDeviceConfiguration(this.configuration); this.camera.CaptureSample((data, length, timestamp) => { var time = DateTime.FromFileTimeUtc(timestamp); using (var sharedImage = ImagePool.GetOrCreate(this.configuration.Width, this.configuration.Height, Microsoft.Psi.Imaging.PixelFormat.BGR_24bpp)) { sharedImage.Resource.CopyFrom(data); var originatingTime = this.pipeline.GetCurrentTimeFromElapsedTicks(timestamp); this.Out.Post(sharedImage, originatingTime); } }); } else { throw new ArgumentException("Camera specification not found"); } }
/// <inheritdoc/> public void Start(Action <DateTime> notifyCompletionTime) { // notify that this is an infinite source component notifyCompletionTime(DateTime.MaxValue); MediaCaptureDevice.Initialize(); CaptureFormat found = null; foreach (var device in MediaCaptureDevice.AllDevices) { if (!device.Attach(this.configuration.UseInSharedMode)) { continue; } // Trace.WriteLine($"MediaCapture - Searching for width={this.configuration.Width} height={this.configuration.Height} deviceId={this.configuration.DeviceId}"); // Trace.WriteLine($"MediaCapture - Found: Name: '{device.FriendlyName}' SymLink: {device.SymbolicLink}"); // Trace.WriteLine($"MediaCapture - Current - Width: {device.CurrentFormat.nWidth} Height: {device.CurrentFormat.nHeight} Type: {device.CurrentFormat.subType.Name}/{device.CurrentFormat.subType.Guid} Framerate: {device.CurrentFormat.nFrameRateNumerator}/{device.CurrentFormat.nFrameRateDenominator}"); if (string.IsNullOrEmpty(this.configuration.DeviceId) || device.FriendlyName == this.configuration.DeviceId || device.SymbolicLink == this.configuration.DeviceId) { foreach (var format in device.Formats) { // Trace.WriteLine($"MediaCapture - Supported - Width: {format.nWidth} Height: {format.nHeight} Type: {format.subType.Name}/{format.subType.Guid} Framerate: {format.nFrameRateNumerator}/{format.nFrameRateDenominator}"); if (this.configuration.Width == format.nWidth && this.configuration.Height == format.nHeight) { // found suitable width/height if (this.configuration.Framerate == format.nFrameRateNumerator / format.nFrameRateDenominator) { // found suitable framerate if (found == null || this.configuration.Framerate == found.nFrameRateNumerator / found.nFrameRateDenominator) { // found first suitable or closer framerate match this.camera = device; found = format; } } } } } if (found != null) { // Trace.WriteLine($"MediaCapture - Using - Width: {found.nWidth} Height: {found.nHeight} Type: {found.subType.Name}/{found.subType.Guid} Framerate: {found.nFrameRateNumerator}/{found.nFrameRateDenominator}"); break; } } if (found != null) { this.camera.CurrentFormat = found; this.deviceInfo = new MediaCaptureInfo(this.camera); var width = this.camera.CurrentFormat.nWidth; var height = this.camera.CurrentFormat.nHeight; // Get default settings for other properties. var currentConfig = this.GetDeviceConfiguration(); this.configuration.BacklightCompensation = currentConfig.BacklightCompensation; this.configuration.Brightness = currentConfig.Brightness; this.configuration.ColorEnable = currentConfig.ColorEnable; this.configuration.Contrast = currentConfig.Contrast; this.configuration.Gain = currentConfig.Gain; this.configuration.Gamma = currentConfig.Gamma; this.configuration.Hue = currentConfig.Hue; this.configuration.Saturation = currentConfig.Saturation; this.configuration.Sharpness = currentConfig.Sharpness; this.configuration.WhiteBalance = currentConfig.WhiteBalance; this.configuration.Focus = currentConfig.Focus; this.SetDeviceConfiguration(this.configuration); this.camera.CaptureSample((data, length, timestamp) => { var time = DateTime.FromFileTimeUtc(timestamp); using var sharedImage = ImagePool.GetOrCreate(this.configuration.Width, this.configuration.Height, PixelFormat.BGR_24bpp); sharedImage.Resource.CopyFrom(data); var originatingTime = this.pipeline.GetCurrentTimeFromElapsedTicks(timestamp); this.Out.Post(sharedImage, originatingTime); }); } else { // Requested camera capture format was not found. Construct an exception message with a list of supported formats. var exceptionMessageBuilder = new StringBuilder(); if (string.IsNullOrEmpty(this.configuration.DeviceId)) { exceptionMessageBuilder.Append($"No cameras were found that support the requested capture format of {this.configuration.Width}x{this.configuration.Height} @ {this.configuration.Framerate} fps. "); } else { exceptionMessageBuilder.Append($"The specified camera {this.configuration.DeviceId} does not support the requested capture format of {this.configuration.Width}x{this.configuration.Height} @ {this.configuration.Framerate} fps. "); } exceptionMessageBuilder.AppendLine("Use one of the following supported camera capture formats instead:"); this.AppendSupportedCaptureFormats(exceptionMessageBuilder); throw new ArgumentException(exceptionMessageBuilder.ToString()); } }