C# 在UWP和Unity之间发送视频
我在一个大项目上苦苦挣扎,我需要帮助 给你一个背景。我有一个在UWP中提交的视频提要(来自DJISDK),我尝试在Unity中获取此视频。我尝试了很多东西(模拟网络摄像头、命名管道、UDP),目前我认为最好的解决方案是TCP 因此,我在UWP中构建了一个TCP服务器,并尝试将视频中的字节[]发送到Unity中的TCP客户端 但我无法在这两个应用程序之间建立连接。一分钟后,我收到一个SocketException,说服务器从未响应 以下是UWP代码:C# 在UWP和Unity之间发送视频,c#,sockets,unity3d,tcp,uwp,C#,Sockets,Unity3d,Tcp,Uwp,我在一个大项目上苦苦挣扎,我需要帮助 给你一个背景。我有一个在UWP中提交的视频提要(来自DJISDK),我尝试在Unity中获取此视频。我尝试了很多东西(模拟网络摄像头、命名管道、UDP),目前我认为最好的解决方案是TCP 因此,我在UWP中构建了一个TCP服务器,并尝试将视频中的字节[]发送到Unity中的TCP客户端 但我无法在这两个应用程序之间建立连接。一分钟后,我收到一个SocketException,说服务器从未响应 以下是UWP代码: private Parser vp;
private Parser vp;
private const string PORT = "1337";
private StreamWriter TCP_client_stream = null;
public MainPage() {
this.InitializeComponent();
this.StartServer();
DJISDKManager.Instance.SDKRegistrationStateChanged += Instance_SDKRegistrationEvent;
DJISDKManager.Instance.RegisterApp("29f7d391513c4052daef008d");
}
//######### DJI #########
private async void Instance_SDKRegistrationEvent(SDKRegistrationState state, SDKError resultCode) {
if (resultCode == SDKError.NO_ERROR) {
System.Diagnostics.Debug.WriteLine("Register app successfully");
if (vp == null) {
vp = new Parser();
vp.Initialize(delegate (byte[] data) {
return DJISDKManager.Instance.VideoFeeder.ParseAssitantDecodingInfo(0, data);
});
vp.SetSurfaceAndVideoCallback(0, 0, null, ReceiveDeocodedData);
DJISDKManager.Instance.VideoFeeder.GetPrimaryVideoFeed(0).VideoDataUpdated += OnVideoPush;
}
//DJISDKManager.Instance.ComponentManager.GetFlightControllerHandler(0, 0).AttitudeChanged += OnAttitudUpdate;
DJISDKManager.Instance.ComponentManager.GetCameraHandler(0, 0).CameraTypeChanged += OnCameraTypeChanged;//On ajoute nos fonctions a ce qui se fait deja
var type = await DJISDKManager.Instance.ComponentManager.GetCameraHandler(0, 0).GetCameraTypeAsync();
OnCameraTypeChanged(this, type.value);
} else {
System.Diagnostics.Debug.WriteLine("Register SDK failed : ");
System.Diagnostics.Debug.WriteLine(resultCode.ToString());
}
}
//######### TCP #########
private void StartServer() {
try {
var streamSocketListener = new Windows.Networking.Sockets.StreamSocketListener();
streamSocketListener.ConnectionReceived += this.StreamSocketListener_ConnectionReceived;
streamSocketListener.BindServiceNameAsync(PORT);
System.Diagnostics.Debug.WriteLine("server is listening...");
} catch(Exception e) {
Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(e.GetBaseException().HResult);
System.Diagnostics.Debug.WriteLine(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : e.Message);
}
}
//On va recylcer ca pour que ca devienne un outil pour fermer
private async void StreamSocketListener_ConnectionReceived(Windows.Networking.Sockets.StreamSocketListener sender, Windows.Networking.Sockets.StreamSocketListenerConnectionReceivedEventArgs args) {
string request;
using (var streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead())) {
request = await streamReader.ReadLineAsync();
}
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => System.Diagnostics.Debug.WriteLine(string.Format("server received the request: \"{0}\"", request)));
// Echo the request back as the response.
TCP_client_stream = new StreamWriter(args.Socket.OutputStream.AsStreamForWrite());
System.Diagnostics.Debug.WriteLine("TCP connected");
using (Stream outputStream = args.Socket.OutputStream.AsStreamForWrite()) {
using (var streamWriter = new StreamWriter(outputStream)) {
await streamWriter.WriteLineAsync(request);
await streamWriter.FlushAsync();
}
}
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => System.Diagnostics.Debug.WriteLine(string.Format("server sent back the response: \"{0}\"", request)));
sender.Dispose();
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => System.Diagnostics.Debug.WriteLine("server closed its socket"));
}
错误似乎来自vp.SetSurfaceAndVideoCallback(0,0,null,ReceiveDeocodedData);因为当我移除它时,它就工作了
我试着从调度员那里打电话,但没什么变化
统一法典:
void Start() {
ConnectToTcpServer();
SendMessage();
}
void Update() {
}
private void ConnectToTcpServer() {
try {
Debug.Log("Start thread");
clientReceiveThread = new Thread(new ThreadStart(ListenForData));
clientReceiveThread.IsBackground = true;
clientReceiveThread.Start();
} catch (Exception e) {
Debug.Log("On client connect exception " + e);
}
}
private void ListenForData() {
try {
Debug.Log("Try to connect");
socketConnection = new TcpClient("192.168.1.27", PORT);
Debug.Log("Connected");
Byte[] bytes = new Byte[videoFrameSize];
while (true) {
// Get a stream object for reading
using (NetworkStream stream = socketConnection.GetStream()) {
int length;
// Read incomming stream into byte arrary.
while ((length = stream.Read(bytes, 0, bytes.Length)) != 0) {
//
}
}
}
} catch (SocketException socketException) {
Debug.Log("Socket exception: " + socketException);
}
}
如果你有一些想法来解决这个问题,或者用另一种方法来发送这个视频,那将是非常好的。这个问题我快发疯了你提到一个错误。。什么错误?你应该能知道是否建立了连接,是吗?是否只是数据未按预期发送/接收?否从未建立连接。1分钟后,我收到一条SocketException消息,说服务器从来没有响应好,你确定带侦听器的应用程序正在运行和工作吗?您是否使用其他方式连接到它?是的,我确定,当我拆下连接线时,我已经回答了这个问题。代码缺少侦听器启动方法!!!。你提到一个错误。。什么错误?你应该能知道是否建立了连接,是吗?是否只是数据未按预期发送/接收?否从未建立连接。1分钟后,我收到一条SocketException消息,说服务器从来没有响应好,你确定带侦听器的应用程序正在运行和工作吗?您是否使用其他方式连接到它?是的,我确定,当我拆下连接线时,我已经回答了这个问题。代码缺少侦听器启动方法!!!。