简体   繁体   English

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

[英]Live Video Streaming using Raspberry Pi and C#

I'm in a uni project of live Streaming the video (Taken from a Web Cam) and stream it to the desktop using C# (UWP, Windows 10 IoT Core). 我在一个直播项目中,通过视频将视频(从网络摄像头拍摄)流式传输并使用C#(UWP,Windows 10 IoT核心版)将其流式传输到桌面。 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#. 即使我发现有些项目使用UWP在Java(对于Rasp)和客户端方面进行服务器端实现,我也找不到关于C#中服务器端编程的任何项目。

Plus, is it really possible to do such server side programming using C# for live streaming as this Microsoft link say it isn't. 另外,是否真的有可能使用C#进行此类服务器端编程以进行实时流传输,因为此Microsoft链接说并非如此。 View the Microsoft Link 查看Microsoft链接

Any help would be deeply appreciated. 任何帮助将不胜感激。

Regards, TS 问候,TS

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#. 即使我发现有些项目使用UWP在Java(对于Rasp)和客户端方面进行服务器端实现,我也找不到关于C#中服务器端编程的任何项目。

There is another project I have coded and tested successfully. 我已经编码并成功测试了另一个项目。 You could have a reference if it could help you. 如果可以帮助您,您可以参考。

In the MyVideoServer App the important is getting the camera id and previewFrame of the video. 在MyVideoServer应用程序中,重要的是获取视频的摄像机ID和PreviewFrame。 previewFrame = await MyMediaCapture.GetPreviewFrameAsync(videoFrame); Then send video stream to client through streamSocketClient. 然后通过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));
    }

Then the key server code is trying to create a server and connect client by socket communication in InitSocket function. 然后,关键服务器代码试图在InitSocket函数中通过套接字通信创建服务器并连接客户端。 StreamSocketListenerServer should be created as an object and started. StreamSocketListenerServer应该作为对象创建并启动。 At the same time the server ip port is setup. 同时设置服务器IP端口。 streamSocketSrv = new StreamSocketListenerServer(); await streamSocketSrv.start("22333"); Last but not least, the Timer_Tick will send video stream to client every 100ms. 最后但并非最不重要的一点,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();
    }

Following you could deploy MyVideoServer App on Raspberry Pi 3. 接下来,您可以在Raspberry Pi 3上部署MyVideoServer App。 在此处输入图片说明 Then you could deploy MyVideoClient App on PC. 然后,您可以在PC上部署MyVideoClient App。 Then enter Raspberry Pi 3 IP Address and click Connect button. 然后输入Raspberry Pi 3 IP地址并单击“连接”按钮。 The video stream would display on the App. 视频流将显示在应用程序上。 在此处输入图片说明

This is the sample code and you could take a reference. 这是示例代码,您可以参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM