How to display 2 web camera preview in UWP? - c#

Hello I displayed 1 webcam preview in UWP and that was a success.
But now I want to use 2 camera's preview on my program or be able to choose between the two cameras while connected 2 cameras on computer.
When I run 1 webcam preview, I referred to documentation on using MediaCapture and it was good.
But now I don't know how to display 2 camera previews or select a one between cameras.
Is it impossible?

Yes, it is possible :-) . The MediaCapture class takes the default camera when you call the InitializeAsync method without parameters, but there is another overload that allows you to specify the device ID.
The documentation shows how to discover video capture devices:
DeviceInformationCollection devices =
await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
Now you can initialize multiple MediaCapture instances like this:
foreach ( var device in devices )
{
var mediaInitSettings =
new MediaCaptureInitializationSettings { VideoDeviceId = device.Id };
MediaCapture mediaCapture = new MediaCapture();
mediaCapture.InitializeAsync(mediaInitSettings);
//do something with the media capture
}
Naturally, when you want to display multiple previews, you will need to have multiple CaptureElements, each set to the specific MediaCapture instance you want.
However this approach is quite simplified. To make sure the concurrent capture and preview is supported, you must first ensure to query only cameras that support device profile using MediaCapture.IsVideoProfileSupported method as shown in the documentation and then also check find a concurrency-enabled profile common for both cameras - MediaCapture.FindConcurrentProfiles, see docs. Only then you can safely create the two previews and know the app will not crash.

Related

Capture still images with UWP MediaCapture (PIN_CATEGORY_STILL)

An alternative title could be: What happened to PIN_CATEGORY_STILL?
I am currently comparing images that were captured using DirectShow and PIN_CATEGORY_STILL with images that were captured using UWP MediaCapture.
On the device I am testing/playing around with DirectShow and MediaCapture, DirectShow detects a PIN_CATEGORY_STILL but I am not able to initialize an instance of MediaCapture with anything other than PhotoCaptureSource.VideoPreview.
MediaCaptureInitializationSettings settings = new()
{
VideoDeviceId = someDeviceId,
PhotoCaptureSource = PhotoCaptureSource.Photo
};
MediaCapture capture = new();
// this throws an exception
// "The capture source does not have an independent photo stream."
await capture.InitializeAsync(settings);
At this point I'm not even sure if PhotoCaptureSource.Photo is meant to be used as an equivalent to PIN_CATEGORY_STILL.
Images captured with PIN_CATEGORY_STILL are way brighter in a dark environment and have a much better quality (in file size and resolution) (which is clear to me, since I am using PhotoCaptureSource.VideoPreview for MediaCapture).
Considering this resource Win32 and COM for UWP apps, it seems like UWP MediaCapture does not use DirectShow underneath but MediaFoundation (which is meant to be a successor for DirectShow).
This article led me to this StackOverflow question Media Foundation is incorrectly marking still image capture stream descriptors as video capture, which basically states that MediaFoundation has no PIN_CATEGORY_STILL but returns 1 FPS as video capability for such devices (or profiles).
Since I am not directly using MediaFoundation nor C++, I tried testing this by querying GetAvailableMediaStreamProperties:
private void Foo()
{
var videoPreviewProperties = GetEncodingProperties(MediaStreamType.VideoRecord);
var photoProperties = GetEncodingProperties(MediaStreamType.Photo);
var videoRecordProperties = GetEncodingProperties(MediaStreamType.VideoPreview);
}
private List<VideoEncodingProperties> GetEncodingProperties(MediaStreamType streamType)
{
// MediaCapture was previously initialized with PhotoCaptureSource.VideoPreview
return MediaCapture.VideoDeviceController
.GetAvailableMediaStreamProperties(streamType)
.OfType<VideoEncodingProperties>()
.ToList();
}
None of these returns a VideoEncodingProperties with only 1 FPS.
To test MediaCapture any further I tried some of the sample applications from here UWP sample. I tried CameraAdvancedCapture, CameraOpenCV and CameraManualControls, but the results were not nearly as good as good old PIN_CATEGORY_STILL.
What happened to PIN_CATEGORY_STILL?
Is there any way to capture images without DirectShow/PIN_CATEGORY_STILL and still keeping this level of quality?
Any enlightenment is much appricated.

Playing an HDHomeRun stream on a Tizen.NET Xamarin App

I'm trying to play a HDHomeRun Connect Video source from a url in the following format: http://x.x.x.x:xxxx/auto/v4.1. This video source is an MPEG2 video encoding and AC3 audio encoding.
I've tried using the Samsung Tizen.TV .NET sample with the following source but the video never plays.
_player = new Tizen.Multimedia.Player();
var mediaSource = new Multimedia.MediaUriSource(uri);
_player.SetSource(mediaSource);
var display = new Multimedia.Display(Window.Instance);
_player.Display = display;
await _player.PrepareAsync();
The player state gets stuck in preparing, and the await _player.PrepareAsync() call never finishes. It is worth noting that I'm using the Tizen Samsung TV Emulator. Do I need to transcode the stream from the HDHomeRun to be playable? Are there any other measures I might be missing for the Video to play?
Ultimately, the Display property of the player wasn't being set correctly. The property that worked for me (found from investigating the JuvoPlayer code was this:
var display = new Multimedia.Display(((FormsApplication)Forms.Context).MainWindow);
_player.Display = display;
When you are to develop a Tizen .NET application, please be aware of which UI framework your project is targetted for among 3 different types: Xamarin.Forms, (pure) ElmSharp, and Tizen.NUI.
Unless your project is based on the Tizen.NUI framework, you shouldn't use Tizen.NUI.Window.Instance and types in Tizen.NUI namespace in any case. Instead, you will have to use types of ElmSharp or Xamarin.Forms.Platform.Tizen namespace for platform-specific code in your application.
Since the internal implementation of Xamarin.Forms for Tizen is based on ElmSharp, FormsApplication.MainWindow will return a ElmSharp.Window instance which can be used to instantiate a Tizen.Multimedia.Display object. That's why the code in your answer worked.

Using AudioRender device as Input for AudioGraph UWP

I have a fully functional AudioGraph system in place for a realtime voice chat. You can select the AudioCapture and AudioRender devices before and during the chat however I'd also like to be able to use Audio from the AudioRender devices for input on the AudioCapture graphs. How can I go about that?
I currently have tried just selecting the device with the device Id however it gives the error:
"DeviceNotAvailable".
for
CreateAudioDeviceInputNodeResult result = await ingraph.CreateDeviceInputNodeAsync(MediaCategory.Media, nodesettings.EncodingProperties, selectedDevice);
selectedDevice being
selectedDevice = await DeviceInformation.CreateFromIdAsync(InputDeviceID);
Derive from official document remarks segment,
Call FindAllAsync, passing in the value returned by GetAudioCaptureSelector, to get the list of DeviceInformation objects representing the available capture devices. For more information, see Audio graphs.
Windows.Devices.Enumeration.DeviceInformationCollection devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Media.Devices.MediaDevice.GetAudioRenderSelector());
For more please refer Audio graphs document.

How can i create Flashlight app in uwp C#

I have Lumia 830 and i've tried to create flashlight app in UWP C#. my device Flashlight works great but i don't know why i can't create my own app turn on / off Torch of my phone.
I use Lamp class :
var lamp = await Lamp.GetDefaultAsync();
if (lamp == null)
{
ShowErrorMessage("No Lamp device found");
return;
}
lamp.IsEnabled = true;
when run this code on my phone "lamp" is null and it couldn't find my FlashLED. I've got this code from MSDN.Microsoft.com and thay said
If the returned object is null, the Lamp API is unsupported on the
device. Some devices may not support the Lamp API even if there is a
lamp physically present on the device.
This class doesn't work on my Lumia 830 i don't kno why? :(
also i use this code :
var mediaDev = new MediaCapture();
await mediaDev.InitializeAsync();
var videoDev = mediaDev.VideoDeviceController;
var tc = videoDev.TorchControl;
if (tc.Supported)
{
// But wait, for this to work with Blue camera drivers, we have to Start a recording session
// Create video encoding profile as MP4
var videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
// Start Video Recording
var videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync("tempVideo.mp4", CreationCollisionOption.GenerateUniqueName);
await mediaDev.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile);
// Turn on Torch
mediaDev.VideoDeviceController.TorchControl.Enabled = true;
}
this code works and my FlashLED turn on but it record video and it take's user SD Card memory.
please help me if you know there is best way to turn on/off Torch or FlashLED.
thanks in advance
Hussein Habibi Juybari
The Lamp API is designed for Windows 10 devices only and you can only use it on devices which support the API. Currently supported devices are 950/950XL, 650, and 550. Older devices will not be updated to support this API. You should fall back to the Windows 8.1 method of turning on flash when you detect that the lamp device is not available on older devices.
From: https://wpdev.uservoice.com/forums/110705/suggestions/15846967
Please test default sample named LampDevice (cf. https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/LampDevice). Do you have the same behavior ? I just tested on Lumia 930 it works fine. No Lumia 830 to test it.

BLE on Xamarin Cross Platform

In Xamarin.Form cross platform , is there any way to show the list of available BLE devices (after scan) in a pop-up dialog box or in menu list and then connect the desired BLE device from the pop-up dialog box and show the data on the UI created for displaying data ??????
You will need DependencyService to retrieve the list of available BLE devices and just display to the ListView as usual.
Besides, below component made by Xamarin may help you to find BLE devices easily for both iOS and Android.
https://components.xamarin.com/view/Monkey.Robotics
Xamarin doesn't provide cross-platform BLE support out of the box. There are a few 3rd-party libraries providing cross-platform BLE functionality, one of which I wrote after spending 1-2 years on a native BLE project: https://github.com/nexussays/ble.net
There is a complete Xamarin.Forms project for iOS, Android, and UWP (https://github.com/nexussays/ble.net/tree/master/test/ble.net.sampleapp) that provides the exact functionality you asked for -- scan, display results, connect, display dialog, etc. But to briefly cover the API here...
You can scan thusly:
await adapter.ScanForDevices(
( IBlePeripheral peripheral ) =>
{
// check if this is the device you want to connect to
// e.g., query peripheral.Advertisement.Services
},
cancellationTokenSource.Token );
And to connect to read/write/notify characteristics:
var connection = await adapter.ConnectToDevice( peripheral, TimeSpan.FromSeconds( 5 ));
if(connection.IsSuccessful())
{
var gatt = connection.GattServer;
var value = await gatt.ReadCharacteristicValue( someServiceGuid, someCharacteristicGuid );
await gatt.WriteCharacteristicValue( someServiceGuid, someCharacteristicGuid, new byte[]{ 1, 2, 3 } );
// etc...
}
else
{
Debug.WriteLine( "Error connecting to device. result={0:g}", connection.ConnectionResult );
}

Categories