首页 > 解决方案 > 使用 FFMPEG / FFMediaElement (FFME) 在 WPF 应用程序中以低延迟播放 RTSP

问题描述

我正在尝试使用FFMediaElement(FFME,基于 FFmpeg 的 WPF MediaElement 替换)组件在我的 WPF 应用程序中播放 RSTP 实时视频。

我的相机连接良好,我想以最小的可用延迟播放它。

我通过更改ProbeSize为最小值来减少延迟:

private void Media_OnMediaInitializing(object Sender, MediaInitializingRoutedEventArgs e)
{
  e.Configuration.GlobalOptions.ProbeSize = 32;
}

但是自流开始以来,我仍然有大约 1 秒的延迟。我的意思是,当我开始播放时,我必须等待 1 秒,直到视频出现,然后我有 1 秒的延迟。

我还尝试更改以下参数:

e.Configuration.GlobalOptions.EnableReducedBuffering = true;
e.Configuration.GlobalOptions.FlagNoBuffer = true;
e.Configuration.GlobalOptions.MaxAnalyzeDuration = TimeSpan.Zero;

但它没有结果。

我测量了 FFmpeg 输出行之间的时间间隔(第一列中的数字是时间,从上一行经过,毫秒)

 ----     OpenCommand: Entered
   39     FFInterop.Initialize: FFmpeg v4.0
    0     EVENT START: MediaInitializing
    0     EVENT DONE : MediaInitializing
  379     EVENT START: MediaOpening
    0     EVENT DONE : MediaOpening
    0     COMP VIDEO: Start Offset:      0,000; Duration:        N/A
   41     SYNC-BUFFER: Started.
  609     SYNC-BUFFER: Finished. Clock set to 1534932751,634
    0     EVENT START: MediaOpened
    0     EVENT DONE : MediaOpened
    0     EVENT START: BufferingStarted
    0     EVENT DONE : BufferingStarted
    0     OpenCommand: Completed
    0     V BLK: 1534932751,634 | CLK: 1534932751,634 | DFT:    0 | IX:   0 | PQ:     0,0k | TQ:     0,0k
    0     Command Queue (1 commands): Before ProcessNext
    0        Play - ID: 404 Canceled: False; Completed: False; Status: WaitingForActivation; State: 
   94     V BLK: 1534932751,675 | CLK: 1534932751,699 | DFT:   24 | IX:   1 | PQ:     0,0k | TQ:     0,0k

因此,“同步缓冲”过程花费的时间最多。

FFmpeg 是否有任何参数可以减小缓冲区的大小?

标签: c#wpfffmpegrtsp

解决方案


I am the author of FFME. It's a common question. In addition to the container configuration options in your MediaInitializing event, you could handle the MediaOpening event and change the following options: (This only applies to version 4.1.280 and above)

    // You can render audio and video as it becomes available but the downside of disabling time
    // synchronization is that video and audio will run on their own independent clocks.
    // Do not disable Time Sync for streams that need synchronized audio and video.
    e.Options.IsTimeSyncDisabled =
        e.Info.Format == "libndi_newtek" ||
        e.Info.InputUrl.StartsWith("rtsp://uno");

    // You can disable the requirement of buffering packets by setting the playback
    // buffer percent to 0. Values of less than 0.5 for live or network streams are not recommended.
    e.Options.MinimumPlaybackBufferPercent = e.Info.Format == "libndi_newtek" ? 0 : 0.5;

    // The audio renderer will try to keep the audio hardware synchronized
    // to the playback position by default.
    // A few WMV files I have tested don't have continuous enough audio packets to support
    // perfect synchronization between audio and video so we simply disable it.
    // Also if time synchronization is disabled, the recommendation is to also disable audio synchronization.
    Media.RendererOptions.AudioDisableSync =
        e.Options.IsTimeSyncDisabled ||
        e.Info.InputUrl.EndsWith(".wmv");


推荐阅读