0

I'm trying to stream the audio from the default microphone on a windows host to a Docker container. Mounting the devices through layers pf virtualization can be problematic. I cannot figure out how to correctly consume the incoming stream in my C# (dotnet core) code.

I am using the VLC windows app to create the test stream on my windows host, using the instructions here. I am streaming to rtp://127.0.0.1:5004, since everything will be running on the same machine. The code to consume the stream in my custom C# app looks like this:

LibVLC libvlc = new LibVLC(); 
libvlc.SetLogFile("c:\\temp\\vlc.log");
MediaPlayer player = new MediaPlayer(libvlc);
var media = new Media(libvlc, "rtp://127.0.0.1:5004", FromType.FromLocation);
var status = await media.Parse(MediaParseOptions.ParseNetwork);

player.Playing += (sender, e) =>
{
    //Need to do something here?
};

player.Play(media);

What I was expecting to do is register some event handler on the MediaPlayer, Media, or some other VLC object and get a buffer of audio bytes that I could convert to the expected format (PCM, 1 channel, 16K samples / sec, 16 bits per sample) and then rewrite to the other stream (not shown in the code for simplicity). But, I am obviously missing something.

So, my questions are: for my scenario, should I prefer HTTP, RTSP or RTP streaming from the host? And once I get that set correctly, how do register for incoming audio events so I can process them and write the data to the other stream?

runninggeek
  • 147
  • 2
  • 11
  • If you are using a docker container, don't expect it to be on the same machine. 127.0.0.1 in the docker container is the container itself, not your windows host. – cube45 Oct 02 '19 at 05:42
  • I'd go for rtsp for your purpose, because it handles the rtp session negociation for you. all you need to do is to connect to your host's VLC through its IP. – cube45 Oct 02 '19 at 05:44
  • The part about converting that to PCM can be tricky. I guess you will need to use the audio callbacks, but I've never used them and I don't know if that's PCM as you need – cube45 Oct 02 '19 at 05:48

1 Answers1

0

Checkout the MediaPlayer.SetAudioFormatCallback method.

But do note that libvlc can handle the streaming for you using media options such as

media.AddOption(":sout=#rtp{sdp=rtp://127.0.0.1:5004}");
media.AddOption(":sout-keep");

See the options list at https://wiki.videolan.org/VLC_command-line_help

should I prefer HTTP, RTSP or RTP streaming from the host?

You might be interested in reading more about it here https://stackoverflow.com/a/4303565/4064749

mfkl
  • 1,914
  • 1
  • 11
  • 21
  • The SetAudioFormatCallback option looks promising, but I'm confused about the objects and values I need to pass into that callback. Are there any examples for that method? I have not found any yet. A second follow up question - once I create the Media object using an RTSP feed, is there a way to get a reference to the underlying System.IO.Stream object? I need to pass that reference into another framework to process the audio. – runninggeek Oct 21 '19 at 16:24
  • No libvlcsharp sample, but many similar other on github https://github.com/search?q=libvlc_audio_set_callbacks&type=Code. There is a ctor with Stream for the Media type. – mfkl Oct 22 '19 at 03:33