Search code examples
c#.netraspberry-pilive-streamingwindows-10-iot-core

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). 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#.

Plus, is it really possible to do such server side programming using C# for live streaming as this Microsoft link say it isn't. View the Microsoft Link

Any help would be deeply appreciated.

Regards, T.S.


Solution

  • 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#.

    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. previewFrame = await MyMediaCapture.GetPreviewFrameAsync(videoFrame);Then send video stream to client through 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. StreamSocketListenerServer should be created as an object and started. At the same time the server ip port is setup.streamSocketSrv = new StreamSocketListenerServer(); await streamSocketSrv.start("22333");Last but not least, the Timer_Tick will send video stream to client every 100ms.

        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. enter image description here Then you could deploy MyVideoClient App on PC. Then enter Raspberry Pi 3 IP Address and click Connect button. The video stream would display on the App. enter image description here

    This is the sample code and you could take a reference.