Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Video streaming acceleration with Varnish

Video streaming acceleration with Varnish

In this presentation, I'm explaining how video streaming via HTTP works. We'll focus on the HLS standard.

The goal of the presentation is to explain the challenges when delivering video via the web and how Varnish can accelerate that delivery.

See https://feryn.eu/speaking/video-streaming-acceleration-varnish/ for more details

Ca901ddcea38854b9783781c91fc87c9?s=128

Thijs Feryn

May 08, 2019
Tweet

Transcript

  1. None
  2. VIDEO IS BIG !

  3. ONLINE VIDEO IS DOMINATING

  4. OVERTTHETOP

  5. None
  6. PACKAGING

  7. HTTP VIDEO PACKAGING FORMATS ✓ HLS ✓ MPEG-DASH ✓ CMAF

  8. HTTP LIVE STREAMING (HLS)

  9. VIDEO.MP4 STREAM.M3U8 STREAM_01.TS STREAM_02.TS STREAM_03.TS ENCODING & PACKAGING VIDEO: H264

    AUDIO: AAC
  10. #EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:6 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-PLAYLIST-TYPE:VOD #EXTINF:6.000000, stream_00.ts #EXTINF:6.000000, stream_01.ts #EXTINF:6.000000,

    stream_02.ts #EXTINF:6.000000, stream_03.ts #EXTINF:6.000000, stream_04.ts #EXTINF:6.000000, stream_05.ts #EXTINF:6.000000, stream_06.ts #EXTINF:6.000000, stream_07.ts #EXTINF:6.000000, stream_08.ts #EXTINF:6.000000, stream_09.ts #EXTINF:5.280000, stream_010.ts #EXT-X-ENDLIST STREAM.M3U8
  11. PLAYER NEEDS TO SUPPORT HLS

  12. <html> <head> <link href="https://vjs.zencdn.net/7.5.4/video-js.css" rel="stylesheet"> </head> <body> <video id='my-video' class='video-js'

    controls preload='auto' height='640' data- setup='{}'> <source src="/live/stream.m3u8" type="application/vnd.apple.mpegurl"> </video> <script src='https://vjs.zencdn.net/7.5.4/video.js'></script> </body> </html>
  13. WHAT TOOL DO WE USE TO ENCODE & PACKAGE?

  14. None
  15. ffmpeg -i video.mp4 \ -c:v libx264 -crf 21 \ -preset

    veryfast -g 25 -sc_threshold 0 \ -c:a aac -b:a 128k -ac 2 \ -f hls -hls_time 4 -hls_playlist_type vod stream.m3u8
  16. None
  17. HI, I'M THIJS

  18. I'M AN EVANGELIST AT

  19. None
  20. None
  21. +-5,000,000 WEBSITES 20% OF THE TOP 10K WEBSITES

  22. None
  23. I'M @THIJSFERYN

  24. None
  25. CONTENT DELIVERY CHALLENGES

  26. HEAVY COMPUTING

  27. NETWORK CAPACITY

  28. SERVER CAPACITY

  29. THROWING SERVERS AT THE PROBLEM

  30. MO' MONEY MO' SERVERS MO' PROBLEMS

  31. CACHE

  32. DON’T RECOMPUTE IF THE DATA HASN’T CHANGED

  33. REVERSE CACHING PROXY

  34. NORMALLY USER SERVER

  35. WITH REVERSE CACHING PROXY USER PROXY SERVER

  36. None
  37. WHY NOT USE A CDN?

  38. VARNISH IS CDN SOFTWARE!

  39. CDNS WITH MANY POINTS OF PRESENCE WILL DDOS YOUR ORIGIN

  40. VARNISH VARNISH VARNISH ORIGIN

  41. CDN CDN CDN ORIGIN VARNISH

  42. WHY IS VARNISH SO POWERFUL?

  43. WHY IS VARNISH SO POWERFUL? ✓ EXTREMELY LOW RESOURCE ✓

    EXTREMELY STABLE ✓ 100 GBIT PER SERVER ✓ NO DISK ACCESS AT RUNTIME ✓ REQUEST COALESCING ✓ VARNISH CONFIGURATION LANGUAGE ✓ VMODS ✓ COMPLIES TO HTTP BEST PRACTICES ✓ ENTERPRISE FEATURES*
  44. ORIGIN VARNISH REQUEST COALESCING

  45. LIVE STREAMING VS VIDEO ON DEMAND

  46. LIVE ✓ LOW LATENCY ✓ CONSTANT PLAYLIST UPDATES ✓ LOW

    TTL ON PLAYLISTS ✓ ONLY THE LAST X SEGMENTS ARE REQUIRED ✓ SEGMENT SIZE TRADEOFF
  47. VOD ✓ NO PLAYLIST UPDATES ✓ HIGH TTL ON PLAYLISTS

    ✓ ALL SEGMENTS ARE REQUIRED ✓ STORAGE REQUIREMENTS ✓ PRE-FETCHING POSSIBLE
  48. HEAD VS LONG TAIL HEAD LONG TAIL

  49. STORAGE

  50. WHERE VARNISH STORES ITS OBJECTS ✓ MEMORY ✓ DISK ✓

    MASSIVE STORAGE ENGINE (ENTERPRISE ONLY)
  51. MEMORY MEMORY ORIGIN ORIGIN MEMORY MEMORY MEMORY MEMORY MEMORY

  52. MEMORY MEMORY ORIGIN ORIGIN DISK DISK DISK

  53. ADAPTIVE BITRATE STREAMS

  54. STREAM_0.M3U8 STREAM_01.TS STREAM_02.TS STREAM_03.TS STREAM_1.M3U8 STREAM_11.TS STREAM_12.TS STREAM_13.TS MASTER.M3U8 LOW

    BITRATE HIGH BITRATE ADAPTIVE BITRATE STREAMS
  55. #EXTM3U #EXT-X-VERSION:3 #EXT-X-STREAM- INF:BANDWIDTH=1020800,RESOLUTION=1080x1920,CODECS="avc1.640028,mp4a.40.2" stream_0.m3u8 #EXT-X-STREAM- INF:BANDWIDTH=2890800,RESOLUTION=1080x1920,CODECS="avc1.640028,mp4a.40.2" stream_1.m3u8 MASTER.M3U8

  56. ffmpeg -i video.mp4 -g 25 -sc_threshold 0 \ -map v:0

    -c:v:0 libx264 -b:v:0 800k \ -map v:0 -c:v:1 libx264-b:v:1 2500k \ -map a:0 -c:a aac -b:a 128k -ac 2 \ -f hls -hls_time 6 hls_playlist_type vod \ -master_pl_name master.m3u8 \ -var_stream_map "v:0,a:0 v:1,a:0" stream_%v.m3u8
  57. LIVE STREAMING

  58. ffmpeg -listen 1 -i rtmp://0.0.0.0/live/stream \ -threads 0 -preset veryfast

    -g 25 -sc_threshold 0 \ -map v:0 -c:v:0 libx264 -b:v:0 800k \ -map v:0 -c:v:1 libx264 -b:v:1 2500k \ -map a:0 -c:a aac -b:a 128k -ac 2 \ -f hls -hls_time 6 -hls_flags delete_segments \ -hls_list_size 5 -hls_delete_threshold 1 \ -master_pl_name master.m3u8 \ -var_stream_map "v:0,a:0 v:1,a:1" /storage/hls/live/ stream_%v.m3u8
  59. FFMPEG WEBSERVER VARNISH RTMP STREAM HLS FILES HTTP HTTP GOPRO

  60. DIGITAL RIGHTS MANAGEMENT

  61. $ openssl rand 16 > enc.key $ openssl rand -hex

    16 230f26620bfafa3cd420cb68c0647415 ENC.KEYINFO /live/enc.key /storage/hls/live/enc.key 230f26620bfafa3cd420cb68c0647415
  62. ffmpeg -listen 1 -i rtmp://0.0.0.0/live/stream \ -threads 0 -preset veryfast

    -g 25 -sc_threshold 0 \ -map v:0 -c:v:0 libx264 -b:v:0 800k \ -map v:0 -c:v:1 libx264 -b:v:1 2500k \ -map a:0 -c:a aac -b:a 128k -ac 2 \ -f hls -hls_time 6 -hls_flags delete_segments \ -hls_list_size 5 -hls_delete_threshold 1 \ -master_pl_name master.m3u8 \ -hls_key_info_file /storage/hls/live/enc.keyinfo \ -var_stream_map "v:0,a:0 v:1,a:1" /storage/hls/live/ stream_%v.m3u8
  63. #EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:5 #EXT-X-MEDIA-SEQUENCE:15 #EXT-X-KEY:METHOD=AES-128,URI="/live/ enc.key",IV=0x230f26620bfafa3cd420cb68c0647415 #EXTINF:3.200000, stream_015.ts #EXTINF:4.800000, stream_016.ts

    #EXTINF:3.200000, stream_017.ts #EXTINF:4.800000, stream_018.ts #EXTINF:1.088000, stream_019.ts #EXT-X-ENDLIST STREAM_0.M3U8 PLAYER SUPPORTS DRM
  64. .TS FILES ARE ENCODED AND CAN ONLY BE DECODED BY

    THE PLAYER
  65. ENC.KEY SHOULD BE PROTECTED

  66. vcl 4.1; import http; backend default { .host = "1.2.3.4";

    .port = "80"; } sub vcl_recv { if(req.url ~ "^/live") { if (req.http.Authorization != "Basic YWRtaW46YWRtaW4=") { return (synth(401, "Restricted")); } } unset req.http.Authorization; } sub vcl_backend_response { if(bereq.url ~ "^/live/stream_[0-9]+\.m3u8$" || bereq.url == "/live/master.m3u8") { set beresp.grace = 0s; set beresp.ttl = 1s; } } sub vcl_synth { if (resp.status == 401) { set resp.http.WWW-Authenticate = {"Basic realm="Restricted area""}; } }
  67. None
  68. VARNISH ENTERPRISE ✓ CLIENT SSL TERMINATION ✓ BACKEND SSL CONNECTIONS

    ✓ PARALLEL ESI ✓ MASSIVE STORAGE ENGINE ✓ ENCRYPTION ✓ THROTTLING ✓ RATE LIMITING ✓ PREFETCHING ✓ GEOLOCATION ✓ AUTHENTICATION ✓ EDGESTASH ✓ CUSTOM STATISTICS ✓ ADMIN MODULE ✓ SUPPORT ✓ HIGH AVAILABILITY
  69. HTTPS://AWS.AMAZON.COM/QUICKSTART/ARCHITECTURE/VARNISH/

  70. MASSIVE STORAGE ENGINE

  71. MSE ✓ MIX OF MEMORY AND DISK ✓ SMARTER LRU

    ✓ LESS DISK FRAGMENTATION ✓ INTELLIGENT STORAGE ROUTING ✓ MODULAR
  72. MEMORY MEMORY ORIGIN ORIGIN DISK DISK DISK

  73. env: { id = "myenv"; memcache_size = "100G"; }; ENVIRONMENT

    CONFIGURATION
  74. env: { id = "myenv"; memcache_size = "100G"; books =

    ( { id = "book1"; directory = "/var/lib/mse/book1"; database_size = "1G"; stores = ( { id = "store-1-1"; filename = "/var/lib/mse/stores/disk1/store-1-1.dat"; size = "1T"; }, { id = "store-1-2"; filename = "/var/lib/mse/stores/disk2/store-1-2.dat"; size = "1T"; } ); } };
  75. HIGH PERFORMANCE HTTP LOGIC BOX

  76. NO MORE ORIGIN

  77. vcl 4.1; import file; backend default { .host = "origin";

    .port = "80"; } sub vcl_init { new root = file.init("/var/www/html/"); } sub vcl_backend_fetch { set bereq.backend = root.backend(); } sub vcl_backend_response { if(bereq.url ~ "^/live/stream_[0-9]+\.m3u8$" || bereq.url == "/live/master.m3u8") { set beresp.grace = 0s; set beresp.ttl = 1s; } }
  78. DEVICE DETECTION & BITRATE LIMITATION

  79. vcl 4.1; import xbody; include "devicedetect.vcl"; backend default { .host

    = "origin"; .port = "80"; } sub vcl_recv { call devicedetect; set req.http.X-UA-Device-Prefix = regsub(req.http.X-UA-Device,"(\w+)\-.*","\1"); } sub vcl_backend_response { if(bereq.url == "/live/master.m3u8" ){ set beresp.http.vary = "X-UA-Device-Prefix"; if(bereq.http.X-UA-Device-Prefix == "mobile") { xbody.regsub("\#EXT-X-STREAM-INF:BANDWIDTH=2890800.*\nstream_.*", ""); } } }
  80. VIDEO SEGMENT PREFETCHING

  81. vcl 4.1; import http; import std; backend default { .host

    = "origin"; .port = "80"; } sub vcl_recv { if (req.url ~ "^/(live|vod)/.+\.ts$") { http.init(0); http.req_set_max_loops(0,1); http.req_copy_headers(0); http.req_set_method(0, "HEAD"); set req.http.x-next-url = http.prefetch_next_url(); std.log("Prefetching " + req.http.x-next-url); http.req_set_url(0, req.http.x-next-url); http.req_send_and_finish(0); } }
  82. THE VMOD ECOSYSTEM HTTPS://VARNISH-CACHE.ORG/VMODS/ HTTPS://DOCS.VARNISH-SOFTWARE.COM/VARNISH-CACHE-PLUS/VMODS/

  83. DEMO TIME?

  84. None
  85. None
  86. HTTPS://FERYN.EU HTTPS://TWITTER.COM/THIJSFERYN HTTPS://INSTAGRAM.COM/THIJSFERYN