RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1110744
Accepted
eastwing
eastwing
Asked:2020-04-15 21:28:12 +0000 UTC2020-04-15 21:28:12 +0000 UTC 2020-04-15 21:28:12 +0000 UTC

在应用程序之间直播视频

  • 772

有一个任务:从设备(例如,从 Android 手机)广播实时视频,而另一个应用程序应该读取流。但我无法以任何方式组织流:远程应用程序最多只能接收和显示第一帧。在这种情况下,如果我理解正确,设备上的应用程序会发送流。

发送类:

using System;
using System.Collections;
using System.Net.Sockets;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using System.IO;
using System.Threading.Tasks;

public class Connector : MonoBehaviour
{
    TcpClient tcp;
    public TextMeshProUGUI label;
    public Button sendButton;
    public Button connectButton;

NetworkStream stream;
BinaryWriter writer;

bool connected = false;

void Awake() => Application.targetFrameRate = 30;

void Start()
{

}

void Update(){
    sendButton.gameObject.SetActive(connected);
    connectButton.gameObject.SetActive(!connected);
}

public void Connect(){
    try {
        label.text = "Connecting...";
        tcp = new TcpClient {NoDelay = true};
        tcp.Connect(
            Options.i.serverAddress, 
            Options.i.serverPort);
        label.text = "Connection established.";

        stream = tcp.GetStream();
        writer = new BinaryWriter(stream);

        connected = true;
    }
    catch (Exception ex){
        label.text = ex.Message;
    }
}

public void Send() => StartCoroutine(ProcessSending());

IEnumerator ProcessSending(){
    var endOfFrame = new WaitForEndOfFrame();
    bool readyToGetFrame;

    while (true){
        yield return endOfFrame;

        byte[] frameBytesLength = new byte[Options.i.sendRecieveCount];
        byte[] rawFrame = GetCameraLastImage();

        ByteLengthToFrameByteArray(rawFrame.Length, frameBytesLength);

        readyToGetFrame = false;
        Task.Run(() => {
            //Send total byte count first
            writer.Write(frameBytesLength, 0, frameBytesLength.Length);
            //label.text = "Sent Image byte Length: " + frameBytesLength.Length;

            //Send the image bytes
            writer.Write(rawFrame, 0, rawFrame.Length);
            label.text = "Sending Image byte array data : " + rawFrame.Length;

            readyToGetFrame = true;
        });

        while (!readyToGetFrame)
            yield return null;
    }
}

void ByteLengthToFrameByteArray(int byteLength, byte[] fullBytes)
{
    Array.Clear(fullBytes, 0, fullBytes.Length);
    byte[] bytesToSendCount = BitConverter.GetBytes(byteLength);
    bytesToSendCount.CopyTo(fullBytes, 0);
}

public byte[] GetCameraLastImage(){
    //получение изображения с камеры устройства
}
}

接收器类:

using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class Listener : MonoBehaviour
{
    TcpListener server = null;
    TcpClient client = null;
    public RawImage projector;
    public AspectRatioFitter fitter;
bool running = false;

byte[] frameData { get; set; }
bool needUpdate = false;
bool readyToUpdate = true;

Texture2D texture;

void Start()
{
    texture = new Texture2D(1, 1, TextureFormat.RGB24, false, true);

    server = new TcpListener(Options.i.serverAddress, Options.i.serverPort);

    server.Start();
    Debug.Log("Waiting for connections...");
    running = true;
    Task.Run(async () => await Listen());
}

void Update(){
    if (!needUpdate)
        return;

    texture.LoadImage(frameData);
    texture.Apply();
    projector.texture = texture;
    projector.gameObject.SetActive(true);
    fitter.aspectRatio = (float)texture.width / texture.height;

    needUpdate = false;
    readyToUpdate = true;
}

async Task Listen()
{
    var strem =  client.GetStream();
    while (running){
        var size = Options.i.sendRecieveCount;
        bool disconnected = false;

        client = await server.AcceptTcpClientAsync();
        //HandleClient(await server.AcceptTcpClientAsync());
        var stream = client.GetStream();
        var imageBytesCount = new byte[size];

        var total = 0;
        do {
            var read = stream.Read(imageBytesCount, total, size - total);
            //Debug.LogFormat("Client recieved {0} bytes", total);
            if (read == 0)
            {
                disconnected = true;
                break;
            }
            total += read;
        } 
        while (total != size);

        var imageSize = disconnected
            ? -1
            : GetLength(imageBytesCount);

        await ReadStream(imageSize, stream);
    }
}

int GetLength(byte[] frameBytesLength)
{
    int byteLength = BitConverter.ToInt32(frameBytesLength, 0);
    return byteLength;
}

async Task ReadStream(int size, NetworkStream clientStream)
{
    bool disconnected = false;

    byte[] imageBytes = new byte[size];
    var total = 0;
    do
    {
        var read = clientStream.Read(imageBytes, total, size - total);
        if (read == 0)
        {
            disconnected = true;
            break;
        }
        total += read;
    } 
    while (total != size);
    Debug.LogFormat("{0} bytes recieved", total);

    //Display Image
    if (!disconnected)
    {
        //Display Image on the main Thread
        readyToUpdate = false;
        UpdateFrame(imageBytes);
    }

    //Wait until old Image is displayed
    while (!readyToUpdate)
        await Task.Delay(1);
        //System.Threading.Thread.Sleep(1);
}

private void UpdateFrame(byte[] rawFrame)
{
    frameData = rawFrame;
    needUpdate = true;
}
}

我需要做什么才能使流工作?

c#
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    eastwing
    2020-04-17T14:49:33Z2020-04-17T14:49:33Z

    所以问题是我的代码中的一个愚蠢的错误,当侦听器收到 0 个字节时,它导致流停止处理。这是代码的更正版本。请注意,不应“按原样”复制此代码。它不考虑连接丢失,也不提供定期断开连接。这只是一个草图,一个如何实现广播的示例,并且仅在一个方向和 1x1 模式下:)

    听众:

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading.Tasks;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class Listener : MonoBehaviour
    {
        TcpListener server = null;
        TcpClient client = null;
        public RawImage projector;
        public AspectRatioFitter fitter;
    
        bool running = false;
    
        byte[] frameData { get; set; }
        bool needUpdate = false;
        bool readyToUpdate = true;
    
        Texture2D texture;
    
        void Start()
        {
            texture = new Texture2D(1, 1, TextureFormat.RGB24, false, true);
    
            server = new TcpListener(Options.i.serverAddress, Options.i.serverPort);
    
            server.Start();
            Debug.Log("Waiting for connections...");
            running = true;
            Task.Run(async () => await Listen());
        }
    
        void Update(){
            if (!needUpdate)
                return;
    
            texture.LoadImage(frameData);
            texture.Apply();
            projector.texture = texture;
            projector.gameObject.SetActive(true);
            fitter.aspectRatio = (float)texture.width / texture.height;
    
            needUpdate = false;
            readyToUpdate = true;
        }
    
        async Task Listen()
        {
            client = await server.AcceptTcpClientAsync();
            var stream = client.GetStream();
            Debug.Log($"{((IPEndPoint)client.Client.RemoteEndPoint).Address} connected");
    
            while (running){
                var size = Options.i.sendRecieveCount;
                bool noData = false;
    
                var imageBytesCount = new byte[size];
    
                var total = 0;
                do {
                    var read = stream.Read(imageBytesCount, total, size - total);
                    noData = read == 0;
    
                    total += read;
                } 
                while (total != size);
    
                var imageSize = noData
                    ? -1
                    : GetLength(imageBytesCount);
    
                await ReadStream(imageSize, stream);
            }
        }
    
        int GetLength(byte[] frameBytesLength)
        {
            int byteLength = BitConverter.ToInt32(frameBytesLength, 0);
            return byteLength;
        }
    
        async Task ReadStream(int size, NetworkStream clientStream)
        {
            bool disconnected = false;
    
            byte[] imageBytes = new byte[size];
            var total = 0;
            do
            {
                var read = clientStream.Read(imageBytes, total, size - total);
                if (read == 0)
                {
                    disconnected = true;
                    break;
                }
                total += read;
            } 
            while (total != size);
            Debug.LogFormat("{0} bytes recieved", total);
    
            //Display Image
            if (!disconnected)
            {
                //Display Image on the main Thread
                readyToUpdate = false;
                UpdateFrame(imageBytes);
            }
    
            //Wait until old Image is displayed
            while (!readyToUpdate)
                await Task.Delay(1);
        }
    
        private void UpdateFrame(byte[] rawFrame)
        {
            frameData = rawFrame;
            needUpdate = true;
        }
    }
    

    连接器/发送器:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Sockets;
    using UnityEngine;
    using TMPro;
    using UnityEngine.UI;
    using System.IO;
    using System.Threading.Tasks;
    using Unity.Collections;
    
    public class Connector : MonoBehaviour
    {
        TcpClient tcp;
        public TextMeshProUGUI label;
        public Button sendButton;
        public Button connectButton;
    
        public ARCameraBackground cameraBackground;
        public ARCameraManager cameraManager;
        public RenderTexture renderTexture;
    
        NetworkStream stream;
        BinaryWriter writer;
    
        bool connected = false;
    
        void Awake() => Application.targetFrameRate = 30;
    
        void Update(){
            sendButton.gameObject.SetActive(connected);
            connectButton.gameObject.SetActive(!connected);
        }
    
        public void Connect(){
            try {
                label.text = "Connecting...";
                tcp = new TcpClient {NoDelay = true};
                tcp.Connect(
                    Options.i.serverAddress, 
                    Options.i.serverPort);
                label.text = "Connection established.";
    
                stream = tcp.GetStream();
                writer = new BinaryWriter(stream);
    
                connected = true;
            }
            catch (Exception ex){
                label.text = ex.Message;
            }
        }
    
        public void Send() => StartCoroutine(ProcessSending());
    
        IEnumerator ProcessSending(){
            var endOfFrame = new WaitForEndOfFrame();
            bool readyToGetFrame;
            byte[] frameBytesLength = new byte[Options.i.sendRecieveCount];
    
            while (true){
                yield return endOfFrame;
    
                byte[] rawFrame = GetCameraLastImage();
                if (rawFrame == null)
                    continue;
    
                ByteLengthToFrameByteArray(rawFrame.Length, frameBytesLength);
    
                readyToGetFrame = false;
                Task.Run(() => {
                    writer.Write(frameBytesLength, 0, frameBytesLength.Length);
    
                    //Send the image bytes
                    writer.Write(rawFrame, 0, rawFrame.Length);
                    label.text = "Sending Image byte array data : " + rawFrame.Length;
    
                    readyToGetFrame = true;
                });
    
                while (!readyToGetFrame)
                    yield return null;
            }
        }
    
        void ByteLengthToFrameByteArray(int byteLength, byte[] fullBytes)
        {
            //Clear old data
            Array.Clear(fullBytes, 0, fullBytes.Length);
            //Convert int to bytes
            byte[] bytesToSendCount = BitConverter.GetBytes(byteLength);
            //Copy result to fullBytes
            bytesToSendCount.CopyTo(fullBytes, 0);
        }
    
        unsafe byte[] GetCameraLastImage(){
            // получение картинки с камеры
        }
    }
    

    来自选项的示例值:

    sendRecieveCount = 15;
    serverAddress => IPAddress.Parse("192.168.1.5");
    serverPort = 5665;
    
    • 0

相关问题

  • 使用嵌套类导出 xml 文件

  • 分层数据模板 [WPF]

  • 如何在 WPF 中为 ListView 手动创建列?

  • 在 2D 空间中,Collider 2D 挂在玩家身上,它对敌人的重量相同,我需要它这样当它们碰撞时,它们不会飞向不同的方向。统一

  • 如何在 c# 中使用 python 神经网络来创建语音合成?

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5