【问题标题】:Reduce HLS latency from +30 seconds将 HLS 延迟从 +30 秒减少
【发布时间】:2014-07-25 03:42:59
【问题描述】:

Ubuntu 12.04

nginx 1.2.4

avconv -版本

avconv version 0.8.10-4:0.8.10-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers
  built on Feb  6 2014 20:56:59 with gcc 4.6.3
avconv 0.8.10-4:0.8.10-0ubuntu0.12.04.1
libavutil    51. 22. 2 / 51. 22. 2
libavcodec   53. 35. 0 / 53. 35. 0
libavformat  53. 21. 1 / 53. 21. 1
libavdevice  53.  2. 0 / 53.  2. 0
libavfilter   2. 15. 0 /  2. 15. 0
libswscale    2.  1. 0 /  2.  1. 0
libpostproc  52.  0. 0 / 52.  0. 0

我正在使用 avconv 和 nginx 创建 HLS 流,但现在我的延迟通常超过 30 秒。经过大量阅读后,我意识到 HLS 具有内置延迟,并且 10 秒是预期的,甚至是首选,但 30 秒似乎非常极端。

我在 nginx-rtmp google group 上看到了很多讨论,this 线程特别有很多建议。我试图通过减少hls_fragmenthls_playlist_length 来解决我的问题,但它们没有产生显着的效果。

nginx.conf:

#user  nobody;
worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8888;
        server_name  localhost;

        add_header 'Access-Control-Allow-Origin' "*";

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
        }

        # rtmp stat
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            # you can move stat.xsl to a different location
            root /usr/build/nginx-rtmp-module;
        }

        # rtmp control
        location /control {
            rtmp_control all;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;

        application myapp {
            live on;

            hls on;
            hls_path /tmp/hls;
            hls_base_url http://x.x.x.x:8888/hls/;
            hls_sync 2ms;
            hls_fragment 2s;

            #hls_variant _low BANDWIDTH=160000;
            #hls_variant _mid BANDWIDTH=320000;
            #hls_variant _hi BANDWIDTH=640000;
        }
    }
}

avconv 命令:

avconv -r 30 -y -f image2pipe -codec:v mjpeg -i - -f flv -codec:v libx264 -profile:v baseline -preset ultrafast -tune zerolatency -an -f flv rtmp://127.0.0.1:1935/myapp/mystream

编辑

我知道瓶颈不是 avconv,因为我也在使用 nginx 生成 RTMP 流。此 RTMP 流的延迟非常小(几秒钟)。

【问题讨论】:

  • 如您所见,HLS 的本质是高延迟。 30 秒并不极端……这很常见。当 YouTube 进行直播活动时,几分钟的延迟是常态(尽管他们确实有一个更大的网络可以分发)。您找到的建议也是正确的。如果它们对您的延迟没有影响,则问题可能出在编码端。将 HLS 部分排除在等式之外,并尝试立即播放来自 avconv/FFmpeg 的数据。测量延迟。它应该是一秒钟或更短,但你永远不知道。
  • 我当前的设置产生了两个流,一个使用 HLS,另一个使用 RTMP,RTMP 流的延迟非常小(可能是几秒钟)。
  • @Rick 我看到您现在正在使用 nodejs,这可能会很有趣,但不幸的是,我相信 iOS 要求您使用 HLS 进行流式传输,否则 RTMP 可能是我的首选。您现在的延迟情况如何?
  • @PanamaJack - 警告,我已经有几年没有参与这个项目了。我不认为 IOS 是个问题,我们使用 nodejs 进行流式传输,然后使用 JS 解码器库将帧写入 HTML5 画布。延迟很好(几秒钟) 我们无法使用 RTMP,因为这需要浏览器中的 Flash 播放器,它不会让您访问各个帧(我们需要)。请注意,在我上次从事此工作的几年中,这一切都可能发生变化。

标签: nginx http-live-streaming avconv


【解决方案1】:

详细说明将 hls_playlist 减少到 4s 并将 hls_fragment 减少到 1s 的答案很好。但是需要注意的是,如果源视频的关键帧间隔大于 hls_fragment 长度,那么 nginx 通常会不得不创建更长的片段,并且延迟会增加。

我们实现的最低延迟是 hls_fragment 为 1 秒,源视频关键帧间隔也为 1 秒。通过这些设置,我们实现了不到 7 秒的延迟。

【讨论】:

  • 我觉得这更像是一个评论而不是一个答案。
  • 我给出了这个答案,因为您绝对必须将源关键帧间隔设置为一个较低的值,否则您将不会像预期的原始问题那样获得低于 10 秒的延迟(并且我不允许添加cmets 那么我还能如何做出这个尚未明确提及的重要区别?)
  • @brod 你是如何改变我们的视频关键帧间隔的?我正在尝试使用hls.js 在浏览器中进行流式传输,但尚未找到实现此目的的方法。
  • @hoodsy 这需要设置在生成视频的任何内容上。为了进行测试,您可以尝试在手机平板电脑上使用 Larix Broadcaster 应用程序。 Larix 广播器允许您更改视频关键帧间隔:link
  • 使用 OBS 或 vmix 时也可以。
【解决方案2】:

我曾经遇到过同样的问题。我在 nginx.conf 中添加了两个参数,然后延迟减少到 10 秒甚至更少。这是我的conf:

    application hls {
        hls_playlist_length 4s; 
        hls_fragment 1s
    }

但是,我不得不说 10s 似乎是我们的限制。有更好的解决方案吗?

【讨论】:

  • 最后,我们完全放弃了 HLS,转而使用 nodejs 服务器,它将帧通过套接字传递。 link
猜你喜欢
  • 2018-03-01
  • 2017-09-20
  • 2019-12-01
  • 1970-01-01
  • 2023-03-27
  • 2022-01-20
  • 1970-01-01
  • 2019-10-18
  • 2016-09-27
相关资源
最近更新 更多