Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
W

wasp-hls

> 开发工具
Open source

WebAssembly-based (Rust) & in-worker HLS Media Player for the web

184 stars0 likes1 views
WebsiteGitHub

About

WebAssembly-based (Rust) & in-worker HLS Media Player for the web

Getting Started

⏯ Demo

API documentation

Wasp-hls is an HLS media player (the library, streaming engine part, not the UI/application part which has to be built on top of it) for the web which:

  1. Relies the most possible on WebAssembly (Written in the Rust language before being compiled).

  2. Runs mostly in a Web Worker (even for media buffering when APIs are available), to reduce the influence an heavy UI can have on playback (and in some situations vice-versa).

Note that this is only a personal project as well as a proof of concept and it is still heavily in development.

What's this exactly? HLS?

Streaming protocols and HLS

To provide media contents at large scale to their customers, most streaming actors (Netflix, Amazon Prime Video, YouTube, Twitch, Canal+, Disney+ etc. you got it), rely on the few same HTTP-based adaptive bitrate streaming protocols: majoritarily MPEG-DASH and Apple's HLS (some rely on only one like Twitch with HLS, others may rely on both depending on the case).

Those protocols all have similar concepts: all expose a central file listing the characteristics of the content (for example, the video qualities, audio tracks, the available subtitles etc.) and allow a client to request the media through small chunks, each containing only the wanted quality and track for a specific time period.

Schema of how HLS basically works, found on Apple's Website which is the one behind HLS

This architecture allows to:

  • only load the wanted media data (thus not e.g. also loading all unwanted audio tracks with it)
  • allows efficient seeking on the content, by allowing to load the content non-sequentially (e.g. you can directly load the data corresponding to the end of the content if you want to).
  • facilitate live streaming by continuously encoding those chunk and adding it to the central file progressively
  • profit from all the goodies of relying on HTTP(S) for content distribution (compatibility with the web, firewall traversal, lots of tools available etc.)

For HLS specifically, this so-called central file is called the "Multivariant Playlist" (a.k.a. "Master Playlist") and is in the M3U8 file format. HLS also has the concept of secondary "Media Playlists", also as M3U8 files, which describe specific tracks and qualities.

The Media Source Extensions™

To allow the implementation of such adaptive streaming media players on the web (and thus with JavaScript), a W3C recommendation was written: the Media Source Extensions™ recommendation, generally just abbreviated to "MSE".

Basically, it builds on top of the HTML5 `` element, adding a new set of browser API accessible from JavaScript to create media buffers, called SourceBuffers, and allowing to push aforementioned small chunks of media data to it for later decoding.

The media player library

The role of an HLS media player library like this one is thus to load the Multivariant Playlist, detect which characteristics (bandwidth, quality, codecs, preferred language, accessibility etc.) are wanted and to load the right media data at the right time, then communicating it to the browser through the MSE APIs so it can be decoded.

The Wasp-hls player reading a Multivariant playlist from Twitch. You can see on the top right the requests performed - mostly of media segments, and some logs on the bottom right. You can also see a cog logo before the requests' url indicating that they are all performed in a WebWorker.

This may look relatively simple at first, but there's a lot of potential optimizations to make and special cases to handle which make quasi-mandatory the need to develop separately the media streaming library (the part "understanding" the streaming protocol and pushing chunks) and the application (the part relying on the library and implementing an UI and the business logic on top).

Most people generally mean the latter when they talk about a "player", here, I'm "only" implementing the former and the application has to be developped separately.

The demo page do also implement a UI part, but this is just to showcase the library and is not actually what's exposed by this package, only the library is (though you can copy the demo's code if you want to).

What's WebWorker and WebAssembly doing with all that?

Amongst the main characteristics of this player is that it relies on a WebWorker, to run concurrently with the application, and WebAssembly, to do so optimally. It thus allows to have a theoretically efficient media player, allowing to avoid stalling in the content if the UI do some heavy lifting and vice-versa.

This is even more important when playing what is called "low-latency contents" (contents with a small-ish latency between the recording and the watch-ing, in the few seconds range) which have amongst its characteristics the fact that only very small data buffers are constructed - allowing to play closer to live but more exposed to rebuffering risk if the segment loading pipeline takes more time than expected).

Playing low-latency contents is one of the main goal of this project. On that matter as an amusing note, playing low-latency contents through a media player with a WebWorker + WebAssembly combination is exactly what Twitch is doing, though their players isn't open-source. This one is (it's also able to play Twitch contents if you succeed to work-around their CORS policy, only not with low-latency for now)!

Even without taking into account low-latency contents, I consider WebAssembly to be particularly adapted for an adaptive media player:

  • Such software generally handle large objects (not only talking about media segments here). The relatively few controls JavaScript offers in terms of memory management can be problematic (I've seen GC-pressure related issues on another player I work on for example).

    With WebAssembly and a source language with enough control over memory management, GC-linked issues disappear (because no GC :p!) and the application's memory usage is much more explicit and controlled, which is usually what we want.

  • Even if there's a lot of input/output and thus the need to interact with JavaScript, the performance profile is completely different than your usual JS front-end application: very few DOM access here, a lot of time is spent on parsing/transmuxing and other logic that can be completely implemented in WebAssembly.

Generating an HLS content

Because this is just the player part, the HLS content has to be prepared - through what we call the packaging step - separately by using what's called a "packager".

For example the shaka-packager is a relatively easy to use packager. With it and FFmpeg, you are armed with the right tools to produce HLS contents.

Why starting this project?

I'm currently working as the lead developper of another, featureful adaptive media player library, the open-source RxPlayer so this is not something totally out of the blue.

The reasons why I started this project are mainly:

  • to see how IO-heavy logic (like we have here with many requests, media segments streaming, playback observation, network metrics etc.) using web APIs only exposed to JavaScript could be conjugated with WebAssembly and Rust.

  • to experiment with a big-enough WebAssembly and Web Worker-based library: how should it interact with applications written in JavaScript?

  • to learn more about the HLS streaming protocol, for which the RxPlayer does not provide first class support (yet?)

  • to see how I would write a complex beast like a Media player if restarting from scratch (though the situation is very different here, due to the difference in the language used).

  • to see if there's any real performance and/or memory related advantage (or disadvantage) in relying on WebAssembly for the core logic of a media player, in various situations (multiple players on the same page, large 4k segments, web workers, mse-in-worker).

  • to play with a Web Worker-based media player, and find out its influence in terms of API definition, synchronization difficulties, performance issues etc.

  • to work on and improve my Rust skills

What's done

It already has a lot of features but there's still some left work:

Type of contents:

  • Support MultiVariant playlists
  • Support media playlist URL directly
  • Play HLS VoD/Live/Event contents
  • Proper support of HLS low-latency contents. Priority: average

Worker-related features:

  • Load content-related resources and run main logic loop in worker
  • Use MSE-in-Worker when available
  • Rely on main thread for MSE when MSE-in-Worker is not available

Adaptive BitRate:

  • Choose variant based on throughtput-based estimates
  • Allow application to list and select its own variant (quality) and know the current one
  • Automatically filter out codecs not supported by the current environment.
  • Urgent/non-urgent quality switches (Some quality switches lead to request for segments of the previous quality to be immediately interrupted, others await them before actually switching).
  • Fast-switching (Push on top of already-loaded segments if they prove to be of higher quality - and are sufficiently far from playback to prevent rebuffering).
  • Smart-switching (I just made-up the name here :D, but basically it's for the opposite situation than the one in which fast-switching is active: don't re-load segments who're already loaded or being pushed with a higher quality).
  • Also choose variant based on buffer-based estimates. Priority: average
  • Logic to detect sudden large fall in bandwidth before the end of a current request. Priority: average

Request Scheduling:

  • Lazy Media Playlist downloading (only fetch and refresh them once they are needed)
  • Media Playlist refreshing for live contents
  • Buffer goal implementation (as in: stop loading segments once enough to fill the buffer up to a certain - configurable - point are loaded)
  • Parallel audio and video segment loading
  • Priorization between audio and video segment requests (to e.g. stop doing audio segment requests when video ones become urgent).
  • Retry of failed requests with an exponential backoff.
  • Perform range requests for segments if needed
  • Parallel initialization segment and first media segment loading. Priority: average

Media demuxing:

  • Media Segment Format: MPEG-2 Transport Streams
  • Media Segment Format: Fragmented MPEG-4
  • Media Segment Format: Packed Audio AAC with ADTS framing
  • Transmux MPEG-2 Transport Streams to fmp4 on platforms not supporting the former like mostChrome or Firefox (through JS for now, Rust implementation pending).
  • Infer mime-type and codec from container if missing from playlist (only for direct media playlist for now - not when a multivariant playlist is present but has missing information).
  • WebAssembly-based mpeg2-ts transmuxer. Priority: average
  • Media Segment Format: Packed Audio MP3 Priority: low
  • Media Segment Format: Packed Audio AC-3 Priority: low
  • Media Segment Format: Packed Audio EAC-3 Priority: low
  • Media Segment Format: WebVTT (subtitles not handled for now) Priority: low

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rusthlsmediasourcemediasource-extensionsmse

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具