1

In a C# application I use LibVLC via NuGet packages.

I have created a MemoryStream and gave it to Media:

MemoryStream buffer = new MemoryStream();
bool playing = false;

public void dataArrived(byte[] d) {
  buffer.Write(d, 0, d.Length);
  if (!playing) {
    playing = true;
    mediaPlayer.Play(new Media(LibVLC, buffer));
  }
}

Now dataArrived is called when data is arrived from the network (this is a live stream). Data is a valid mp4 stream (fragmented, plays nicely in HTML5 Video).

However VLC is not displaying it. In the log I see a lot of messages about prefetch:

prefetch stream debug: end of stream

VLC can read and dump the boxes, however there is a message (debug, not error!) which might be interesting:

mp4 demux debug: unrecognized major media specification (iso5).

Then there is another interesting batch of messages:

mp4 demux warning: cannot select track[Id 0x1]
main input debug: EOF reached
main decoder debug: killing decoder fourcc `h264'
main decoder debug: removing module "avcodec"
main demux debug: removing module "mp4"
mp4 demux debug: freeing all memory
main input debug: Program doesn't contain anymore ES

Is there some trick (config) for LibVLC to play fragmented mp4 on the fly (data is not available in advance)?

Or maybe MemoryStream is a wrong choice for this purpose?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Daniel
  • 2,318
  • 2
  • 22
  • 53

1 Answers1

1

The MemoryStream is not a good fit for a live stream, because it signals the end of the stream, because it can't know that new data will be added later.

Use your own implementation of Stream or create your own MediaInput.

Be careful, you seem to use an older version of LibVLCSharp. The newer API does not use a Stream directly, but needs a MediaInput to work.

If you manage to make it work, would you like to submit a live stream sample at LibVLCSharp? This is a feature that we are asked about.

cube45
  • 3,429
  • 2
  • 24
  • 35
  • I want to remove the data that already have been read by VLC. Is it possible? If I write my own MediaInput I can exclude the seek function (or return false). I just don't want to have a continuously increasing buffer holding the whole stream from the beginning. Once I succeed I'll put it here, and I can submit it to LibVLCSharp too, yes. – Daniel Apr 10 '20 at 17:57
  • One more detail: LibVLC calls int Read(IntPtr buf, uint len) when it needs new data. Is it okay if I return with 0 (if I don't yet have new data)? Will VLC try again, or I shall force it once I have new data? – Daniel Apr 10 '20 at 18:02
  • 0 means EOF. As stated [here](https://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__media.html#gabeece3802e4b17a655e45c4ff4a2bbda), your implementation should wait for data to be available. As for your other question, If you can, I think it would be better to handle that with a [Pipe](https://learn.microsoft.com/en-us/dotnet/api/system.io.pipelines.pipe?view=dotnet-plat-ext-2.1). Feel free to come and chat on gitter if you need help to implement that. – cube45 Apr 10 '20 at 20:52