在UWP和Unity之间发送视频

时间:2019-03-23 11:42:06

标签: c# sockets unity3d tcp uwp

我正在为一个大项目而挣扎,我需要帮助。

为您提供背景信息。我有一个来自UWP(来自DJI SDK)的视频供稿,我尝试在Unity中获取该视频。我确实尝试了很多事情(模拟网络摄像头,NamedPipes和UDP),目前我认为最好的解决方案是TCP。

因此,我在UWP中构建了一个TCP服务器,然后尝试将字节[]从视频发送到Unity中的TCP客户端。

但是我无法在两个应用程序之间建立连接。一分钟后,我刚收到一个SocketException,说服务器永不响应

这是UWP代码:

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);
    }
}

如果您有解决此问题的想法或另一种发送此视频的方法,那么对您很好。我对这个问题发疯了

0 个答案:

没有答案