在 .NET Core 项目中使用来自 UWP api 的 Miracast 无法接收视频帧
Using Miracast from UWP api in a .NET core project cannot receive video frames
我们设法使用 UWP 创建了一个可用的 Miracast 接收器,我们希望在 .NET Core 应用程序中使用此功能。
因此,我们按照本指南在 .NET Core 项目中使用 UWP api:
Using UWP apis with WPF and .NET Core
项目运行,我们获得了从智能手机到应用程序的连接,但随后我们没有从 MediaPlayer 对象接收到任何视频帧(与正常工作的原始 UWP 项目不同)
我们观察到在 MediaSource 对象中我们获得了一个 mcrecv url
(示例 -> mcrecv://192.168.137.247:7236/h-0000000c/192.168.137.1)
但是使用它的 MediaPlayer 不会触发任何 VideoFrameAvailable 事件。
我们如何解决这个问题?
以下是我们使用的基本实现:
using System;
using System.Diagnostics;
using System.Windows;
using Windows.Graphics.Imaging;
using Windows.Media.Miracast;
using Windows.Media.Playback;
namespace Miracast_GUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MiracastReceiver receiver;
public MiracastReceiverSession session;
public MediaPlayer mp;
public SoftwareBitmap frameServerDest;
public MiracastReceiverConnection connection;
public MainWindow()
{
InitializeComponent();
// Starts service
StartMiracastService();
}
public void StartMiracastService()
{
receiver = new MiracastReceiver();
receiver.StatusChanged += Receiver_StatusChanged;
MiracastReceiverSettings settings = receiver.GetDefaultSettings();
settings.FriendlyName = "Miracast-Service-Test";
settings.AuthorizationMethod = MiracastReceiverAuthorizationMethod.None;
settings.ModelName = receiver.GetDefaultSettings().ModelName;
settings.ModelNumber = receiver.GetDefaultSettings().ModelNumber;
settings.RequireAuthorizationFromKnownTransmitters = receiver.GetDefaultSettings().RequireAuthorizationFromKnownTransmitters;
receiver.DisconnectAllAndApplySettings(settings);
session = receiver.CreateSession(/*CoreApplication.MainView*/ null);
session.AllowConnectionTakeover = true;
session.ConnectionCreated += Session_ConnectionCreated;
session.MediaSourceCreated += Session_MediaSourceCreated;
session.Disconnected += Session_Disconnected;
MiracastReceiverSessionStartResult result = session.Start();
Debug.WriteLine("Status: " + result.Status);
}
private void Session_Disconnected(MiracastReceiverSession sender, MiracastReceiverDisconnectedEventArgs args)
{
session.Dispose();
}
private void Receiver_StatusChanged(MiracastReceiver sender, object args)
{
Debug.WriteLine(receiver.GetStatus().ListeningStatus);
}
private void Session_ConnectionCreated(MiracastReceiverSession sender, MiracastReceiverConnectionCreatedEventArgs args)
{
connection = args.Connection;
connection.InputDevices.Keyboard.TransmitInput = true;
connection.InputDevices.GameController.Mode =
MiracastReceiverGameControllerDeviceUsageMode.AsMouseAndKeyboard;
Debug.WriteLine("CONNECTION CREATED");
}
private void Session_MediaSourceCreated(MiracastReceiverSession sender, MiracastReceiverMediaSourceCreatedEventArgs args)
{
mp = new MediaPlayer
{
IsVideoFrameServerEnabled = true,
AutoPlay = true,
Source = args.MediaSource,
RealTimePlayback = true
};
mp.VideoFrameAvailable += Mp_VideoFrameAvailable;
Debug.WriteLine(mp.PlaybackSession.PlaybackState);
mp.Play();
Debug.WriteLine("MEDIA SOURCE CREATED");
}
private void Mp_VideoFrameAvailable(MediaPlayer sender, object args)
{
Console.WriteLine("Received frame...");
}
}
}
如果未使用“Internet(客户端和服务器)”功能,UWP 应用程序的 MediaPlayer 不会触发 VideoFrameAvailable 事件。也许一个 WPF 项目没有任何能力?因此,wpf 应用程序无法使用需要此功能的其他服务。我不知道如何在 wpf 中使用 uwp 功能。
我们设法使用 UWP 创建了一个可用的 Miracast 接收器,我们希望在 .NET Core 应用程序中使用此功能。 因此,我们按照本指南在 .NET Core 项目中使用 UWP api:
Using UWP apis with WPF and .NET Core
项目运行,我们获得了从智能手机到应用程序的连接,但随后我们没有从 MediaPlayer 对象接收到任何视频帧(与正常工作的原始 UWP 项目不同)
我们观察到在 MediaSource 对象中我们获得了一个 mcrecv url (示例 -> mcrecv://192.168.137.247:7236/h-0000000c/192.168.137.1)
但是使用它的 MediaPlayer 不会触发任何 VideoFrameAvailable 事件。
我们如何解决这个问题? 以下是我们使用的基本实现:
using System;
using System.Diagnostics;
using System.Windows;
using Windows.Graphics.Imaging;
using Windows.Media.Miracast;
using Windows.Media.Playback;
namespace Miracast_GUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MiracastReceiver receiver;
public MiracastReceiverSession session;
public MediaPlayer mp;
public SoftwareBitmap frameServerDest;
public MiracastReceiverConnection connection;
public MainWindow()
{
InitializeComponent();
// Starts service
StartMiracastService();
}
public void StartMiracastService()
{
receiver = new MiracastReceiver();
receiver.StatusChanged += Receiver_StatusChanged;
MiracastReceiverSettings settings = receiver.GetDefaultSettings();
settings.FriendlyName = "Miracast-Service-Test";
settings.AuthorizationMethod = MiracastReceiverAuthorizationMethod.None;
settings.ModelName = receiver.GetDefaultSettings().ModelName;
settings.ModelNumber = receiver.GetDefaultSettings().ModelNumber;
settings.RequireAuthorizationFromKnownTransmitters = receiver.GetDefaultSettings().RequireAuthorizationFromKnownTransmitters;
receiver.DisconnectAllAndApplySettings(settings);
session = receiver.CreateSession(/*CoreApplication.MainView*/ null);
session.AllowConnectionTakeover = true;
session.ConnectionCreated += Session_ConnectionCreated;
session.MediaSourceCreated += Session_MediaSourceCreated;
session.Disconnected += Session_Disconnected;
MiracastReceiverSessionStartResult result = session.Start();
Debug.WriteLine("Status: " + result.Status);
}
private void Session_Disconnected(MiracastReceiverSession sender, MiracastReceiverDisconnectedEventArgs args)
{
session.Dispose();
}
private void Receiver_StatusChanged(MiracastReceiver sender, object args)
{
Debug.WriteLine(receiver.GetStatus().ListeningStatus);
}
private void Session_ConnectionCreated(MiracastReceiverSession sender, MiracastReceiverConnectionCreatedEventArgs args)
{
connection = args.Connection;
connection.InputDevices.Keyboard.TransmitInput = true;
connection.InputDevices.GameController.Mode =
MiracastReceiverGameControllerDeviceUsageMode.AsMouseAndKeyboard;
Debug.WriteLine("CONNECTION CREATED");
}
private void Session_MediaSourceCreated(MiracastReceiverSession sender, MiracastReceiverMediaSourceCreatedEventArgs args)
{
mp = new MediaPlayer
{
IsVideoFrameServerEnabled = true,
AutoPlay = true,
Source = args.MediaSource,
RealTimePlayback = true
};
mp.VideoFrameAvailable += Mp_VideoFrameAvailable;
Debug.WriteLine(mp.PlaybackSession.PlaybackState);
mp.Play();
Debug.WriteLine("MEDIA SOURCE CREATED");
}
private void Mp_VideoFrameAvailable(MediaPlayer sender, object args)
{
Console.WriteLine("Received frame...");
}
}
}
如果未使用“Internet(客户端和服务器)”功能,UWP 应用程序的 MediaPlayer 不会触发 VideoFrameAvailable 事件。也许一个 WPF 项目没有任何能力?因此,wpf 应用程序无法使用需要此功能的其他服务。我不知道如何在 wpf 中使用 uwp 功能。