Skip to content

Clock gap issue during streaming

Summary

I'm having an issue while playing sound from a rtp stream.

The PlayAudio is called well during the process, but randomly it stop and in the vlc logs i'm having a "clock gap, unexpected stream discontinuity" issue.

Minimal project and steps to reproduce

I'm simply starting and MediaPlayer with a PlayAudioCallback

using NAudio.Wave;
using System.Runtime.InteropServices;
using LibVLCSharp.Shared;

bool _threadInprogress = true;
Thread _bufferingThread;
bool _end;
WaveFormat _sourceFormat;
WaveFormat _targetFormat;
WaveStream _sourceStream;
WaveStream _targetStream;
LibVLC _libVLC;
string _url = ""; // Coming from config


_libVLC = new LibVLC(enableDebugLogs: false);
using (var media = new Media(_libVLC, new Uri($"rtp://{_url}"), ":no-video"))
{
    using (var mediaPlayer = new MediaPlayer(media))
    {
        mediaPlayer.SetAudioFormatCallback(AudioSetup, AudioCleanup);
        mediaPlayer.SetAudioCallbacks(PlayAudio, null, null, FlushAudio, null);

        mediaPlayer.Play();

        while (_threadInprogress)
        {
            Thread.Sleep(60000);
        }
    }
}

void PlayAudio(IntPtr data, IntPtr samples, uint count, long pts)
{
    try
    {

        var buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame


        int bytes = (int)count * 2; // (16 bit, 1 channel)
                                    //var buffer = new byte[bytes];
        Marshal.Copy(samples, buffer, 0, bytes);

        if (_sourceFormat.SampleRate == _targetFormat.SampleRate)
        {
            //_waveProvider.AddSamples(buffer, 0, bytes);
        }
        else
        {
            var memoryStream = new MemoryStream(buffer, 0, bytes);
            _sourceStream = new RawSourceWaveStream(memoryStream, _sourceFormat);
            _targetStream = new WaveFormatConversionStream(_targetFormat, _sourceStream);

            byte[] bufferAudioConverted = new byte[_targetStream.Length];
            _targetStream.Read(bufferAudioConverted, 0, bufferAudioConverted.Length);

            //_waveProvider.AddSamples(bufferAudioConverted, 0, bufferAudioConverted.Length);
        }

    }
    catch (Exception ex)
    {

    }
}

void FlushAudio(IntPtr data, long pts)
{
    //_waveProvider.ClearBuffer();
}
int AudioSetup(ref IntPtr opaque, ref IntPtr format, ref uint rate, ref uint channels)
{
    return 0;
}
void AudioCleanup(IntPtr opaque) { }

 WaveFormat ReturnWaveFormat(Mp3Frame frame)
{
    WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2,
        frame.FrameLength, frame.BitRate);

    return waveFormat;
}

What is the current bug behavior?

The playAudio callback is not called anymore.

What is the expected correct behavior?

It should call the callback all the time

Does it work on other plaforms? Does it work with the official VLC apps?

I tried a official vlc on my devise and there is no interupt at all.

Relevant LibVLC logs and/or screenshots

01/04/2025 09:50:37 LOG Copyright © 1996-2021 the VideoLAN team
01/04/2025 09:50:37 LOG configured with /home/jenkins/workspace/vlc-release/windows/vlc-release-win32-x86/extras/package/win32/../../../configure  '--enable-update-check' '--enable-lua' '--enable-faad' '--enable-flac' '--enable-theora' '--enable-avcodec' '--enable-merge-ffmpeg' '--enable-dca' '--enable-mpc' '--enable-libass' '--enable-schroedinger' '--enable-realrtsp' '--enable-live555' '--enable-shout' '--enable-goom' '--enable-sse' '--enable-mmx' '--enable-libcddb' '--enable-zvbi' '--disable-telx' '--enable-nls' '--host=i686-w64-mingw32' '--with-contrib=../contrib/i686-w64-mingw32' '--with-breakpad=https://win.crashes.videolan.org' '--enable-qt' '--enable-skins2' '--enable-dvdread' '--enable-caca' 'host_alias=i686-w64-mingw32' 'CFLAGS= ' 'CXXFLAGS= ' 'PKG_CONFIG=pkg-config' 'PKG_CONFIG_LIBDIR=/usr/i686-w64-mingw32/lib/pkgconfig:/usr/lib/i686-w64-mingw32/pkgconfig'
01/04/2025 09:50:37 LOG creating audio output
01/04/2025 09:50:37 LOG using default device
01/04/2025 09:50:37 LOG display name changed: VLC media player (LibVLC 3.0.16)
01/04/2025 09:50:37 LOG volume from -63.500000 dB to +0.000000 dB with 0.500000 dB increments
01/04/2025 09:50:37 LOG keeping audio output
01/04/2025 09:50:37 LOG removing module "mmdevice"
01/04/2025 09:50:37 LOG Creating an input for (removed the ip here)
01/04/2025 09:50:37 LOG using timeshift granularity of 50 MiB
01/04/2025 09:50:37 LOG using access_demux module "rtp"
01/04/2025 09:50:37 LOG no meta reader modules matched
01/04/2025 09:50:37 LOG detected MPEG2 TS
01/04/2025 09:50:37 LOG creating demux: access='' demux='ts' location='' file='(null)'
01/04/2025 09:50:37 LOG first packet for pid=0 cc=0x8
01/04/2025 09:50:37 LOG first packet for pid=32 cc=0x8
01/04/2025 09:50:37 LOG mpga->f32l, bits per sample: 32
01/04/2025 09:50:37 LOG   * pid=20 listening for TDT
01/04/2025 09:50:37 LOG first packet for pid=200 cc=0xb
01/04/2025 09:50:37 LOG removing module "mpg123"
01/04/2025 09:50:37 LOG creating audio output
01/04/2025 09:50:37 LOG output 's16l' 44100 Hz Mono frame=1 samples/2 bytes
01/04/2025 09:50:37 LOG 1.000 scale, 1323.000 stride_in, 1323 stride_out, 1059 standing, 264 overlap, 617 search, 2204 queue, fl32 mode
01/04/2025 09:50:37 LOG looking for audio converter module matching "any": 8 candidates
01/04/2025 09:50:37 LOG Buffering 7%
01/04/2025 09:50:37 LOG Buffering 17%
01/04/2025 09:50:37 LOG Buffering 25%
01/04/2025 09:50:37 LOG Buffering 33%
01/04/2025 09:50:38 LOG Buffering 40%
01/04/2025 09:50:38 LOG Buffering 48%
01/04/2025 09:50:38 LOG Buffering 56%
01/04/2025 09:50:38 LOG Buffering 63%
01/04/2025 09:50:38 LOG Buffering 70%
01/04/2025 09:50:38 LOG Buffering 80%
01/04/2025 09:50:38 LOG Buffering 87%
01/04/2025 09:50:38 LOG Buffering 95%
01/04/2025 09:50:38 LOG Stream buffering done (1022 ms in 1022 ms)
02/04/2025 11:59:25 LOG clock gap, unexpected stream discontinuity
02/04/2025 11:59:25 LOG discarded audio buffer
02/04/2025 11:59:25 LOG Could not convert timestamp 191467123881 for mpg123
02/04/2025 11:59:25 LOG Could not convert timestamp 191467176126 for mpg123
02/04/2025 11:59:25 LOG discarded audio buffer
02/04/2025 11:59:25 LOG Could not convert timestamp 191467254494 for mpg123
02/04/2025 11:59:25 LOG discarded audio buffer
02/04/2025 11:59:25 LOG Timestamp conversion failed (delay 1000000, buffering 0, bound 3000000)
02/04/2025 11:59:25 LOG discarded audio buffer
02/04/2025 11:59:25 LOG Timestamp conversion failed (delay 1000000, buffering 0, bound 3000000)
02/04/2025 11:59:25 LOG Timestamp conversion failed (delay 1000000, buffering 0, bound 3000000)
02/04/2025 11:59:26 LOG Timestamp conversion failed (delay 1000000, buffering 0, bound 3000000)

Environment

  • OS:
  • Version
  • LibVLCSharp version 3.7.0
Edited by Alan Sigal
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information