使用 Raspberry Pi 和 C# 的实时视频流

Live Video Streaming using Raspberry Pi and C#

我在一个实时流式传输视频(取自网络摄像头)并使用 C#(UWP,Windows 10 IoT Core)将其流式传输到桌面的 uni 项目中。尽管我发现一些项目在 Java(对于 Rasp)和客户端使用 UWP 进行服务器端实现,但我找不到任何关于 C# 中服务器端编程的项目。

此外,是否真的可以像微软 link 所说的那样使用 C# 进行此类服务器端编程以进行实时流式传输。 View the Microsoft Link

任何帮助将不胜感激。

此致, T.S.

Even though I found some projects doing the server side implementation in Java (For Rasp) and Client side using UWP I couldn't find any Projects regarding Server side programming in C#.

还有一个项目我已经编码并测试成功。如果对你有帮助的话可以参考一下。

在 MyVideoServer 应用程序中,重要的是获取视频的摄像头 ID 和预览帧。 previewFrame = await MyMediaCapture.GetPreviewFrameAsync(videoFrame);然后通过streamSocketClient向客户端发送视频流。await streamSocketClient.sendBuffer(buffer);

    public MainPage()
    {
        this.InitializeComponent();
        InitializeCameraAsync();
        InitSocket();
    }

    MediaCapture MyMediaCapture;
    VideoFrame videoFrame;
    VideoFrame previewFrame;
    IBuffer buffer;

    DispatcherTimer timer;
    StreamSocketListenerServer streamSocketSrv;
    StreamSocketClient streamSocketClient;

    private async void InitializeCameraAsync()
    {
        var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
        DeviceInformation cameraDevice = allVideoDevices.FirstOrDefault();
        var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };
        MyMediaCapture = new MediaCapture();

        try
        {
            await MyMediaCapture.InitializeAsync(mediaInitSettings);
        }
        catch (UnauthorizedAccessException)
        {

        }

        PreviewControl.Height = 180;
        PreviewControl.Width = 240;
        PreviewControl.Source = MyMediaCapture;

        await MyMediaCapture.StartPreviewAsync();
        videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, 240, 180, 0);
        buffer = new Windows.Storage.Streams.Buffer((uint)(240 * 180 * 8));
    }

那么关键的服务器代码就是在InitSocket函数中尝试创建一个服务器并通过套接字通信连接客户端。 StreamSocketListenerServer 应该作为对象创建并启动。同时设置服务器 ip 端口。streamSocketSrv = new StreamSocketListenerServer(); await streamSocketSrv.start("22333");最后但同样重要的是,Timer_Tick 将每 100 毫秒向客户端发送一次视频流。

    private async void InitSocket()
    {
        streamSocketSrv = new StreamSocketListenerServer();
        await streamSocketSrv.start("22333");

        streamSocketClient = new StreamSocketClient();

        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(100);
        timer.Tick += Timer_Tick;
        timer.Start();
    }

接下来您可以在 Raspberry Pi 3 上部署 MyVideoServer App。 然后您可以在 PC 上部署 MyVideoClient 应用程序。然后输入 Raspberry Pi 3 IP 地址并单击连接按钮。视频流将显示在应用程序上。

这是sample代码,您可以参考。