0

I am looking into screen casting through Miracast in an application but am unsure of how to use the Windows.Media.Miracast namespace. Limited information exists on the internet due to the short age of the Windows 10 1903 update that the namespace comes as a part of.

The only thing I've found so far is this documentation.

My question is does anybody know what the proper way of using this namespace is? Any examples or resources found online would be a great help.

Cheers.

eocsap
  • 92
  • 10
  • 1
    The API seems only appropriate for building a Miracast sink, not a Miracast source. What kind of application are you wanting to build? – Dai May 09 '19 at 12:33
  • 1
    Hmm okay, It is an application that would be a source. I want to build an application that will cast from the source to a defined receiver without having to go through the Windows 10 Connect menu. – eocsap May 09 '19 at 12:36

1 Answers1

3

These three sample projects demonstrate various MiraCast source apis that can be used from UWP applications. Not sure about outside UWP.

I'm personally using code like the following, on Windows IoT Core, to cast my whole screen

Scan for devices:

miraDeviceWatcher = DeviceInformation.CreateWatcher(CastingDevice.GetDeviceSelector(CastingPlaybackTypes.Video)); 
miraHandlerAdded = new TypedEventHandler<DeviceWatcher, DeviceInformation>(async (watcher, deviceInfo) =>
{
   await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
   {
      //Add each discovered device to our listbox
      CastingDevice addedDevice = await CastingDevice.FromIdAsync(deviceInfo.Id);
      var disp = new CastingDisplay(addedDevice); //my viewmodel
      MiraDevices.Add(disp); //ObservableCollection
   });
});
miraDeviceWatcher.Added += miraHandlerAdded;

Connect to selected device:

public async Task StartCasting(CastingDisplay castee)
{
   //When a device is selected, first thing we do is stop the watcher so it's search doesn't conflict with streaming
   if (miraDeviceWatcher.Status != DeviceWatcherStatus.Stopped)
   {
      miraDeviceWatcher.Stop();
   }

   //Create a new casting connection to the device that's been selected
   connection = castee.Device.CreateCastingConnection();
   //Register for events
   connection.ErrorOccurred += Connection_ErrorOccurred;
   connection.StateChanged += Connection_StateChangedAsync;

   var image = new Windows.UI.Xaml.Controls.Image();
   await connection.RequestStartCastingAsync(image.GetAsCastingSource());
}

This Image is just used as a casting source. Once the connection is made, my whole screen is broadcast. The behavior is not documented. Hopefully it won't get 'fixed' in a future update.

Thomas
  • 3,348
  • 4
  • 35
  • 49