Search code examples
c#wpftcpvlcvlc.dotnet

Vlc.Dotnet Play streaming h264 raw data on WPF


all. I'm a beginner of programming and I met some difficulties on video decoding/playing.

I have raw h264 data stream from tcpsocket, and I want to show the video on a WPF usercontrol.

Since I have few knowledge of C++ & video decoding/encoding, it will be hard to use ffmpeg for me. So I'm considering if VLC can do this.

The background is: I want to use Scrcpy server build my own "Android screen cast & remote control" WPF application.

So far, I've implemented:

  • Push server to device and start the server
  • Establish TCP connection between PC and Android device
  • Can see h264 raw data streaming in the socket

Then the next step is: show video on a WPF usercontrol

Actually I've tried another solution before and can get what I want

  • Use MPV as a media player
  • Start mpv.exe process in my app with specific arguments
  • Embed mpv window in a WPF host element

But I think <WindowsFormsHost/> is not perfect for a WPF application, So I'm trying to find a WPF-style way.

When I searching the Github, I found it is easier if I want to play a media file from disc or internet, I just need to pass the file location (e.g. D:/MyFolder/mySampleVideo.mp4 or http://somesite/aSampleVideo.flv) and no need to care about how the component/element work. Like this project and this project

If I use the VLC, how can I directly play the raw h264 data stream? Is there a method like VlcPlayer.Play(NetworkStream myh264stram) {...} ?


Solution

  • But I think is not perfect for a WPF application, So I'm trying to find a WPF-style way.

    The solution of using a WindowsFormsHost in a WPF application is the best we've found for WPF, because implementing a true-WPF solution doesn't have great perfs: https://github.com/ZeBobo5/Vlc.DotNet#writing-a-wpf-app--migrating-wpf-control-from-2x

    That said, if you still want to go ahead with Vlc.DotNet (which has been placed in maintenance mode), you will probably need to specify the demux you want libvlc to be using with "--demux", "h264" in the VlcMediaPlayerOptions

    Then, you could indeed call

       mediaPlayer.Play(stream);
    

    With LibVLCSharp, the procedure is quite the same, but we are using a MediaInput class to hold the reference to the Stream, see : https://github.com/mfkl/lvst/blob/master/LVST/Program.cs#L72

    using var mediaInput = new StreamMediaInput(stream);
    using var media = new Media(libVLC, mediaInput);
    using var mediaPlayer = new MediaPlayer(media);
    

    see also : C# LibVLCSharp player direct feed media