HTML5 Video and MSE Explained
In this article
HTML5 video and Media Source Extensions (MSE) let browsers play video without plugins, and MSE specifically enables adaptive streaming and live video by letting JavaScript feed data to the video element in chunks. This article explains what each technology does, how they work together, and what you need to know to use them effectively.
Background: From Plugins to Native Video
Before HTML5, watching video in a browser usually required a plugin like Adobe Flash or Microsoft Silverlight. These plugins handled decoding and playback but were a security risk, a performance drain, and a compatibility headache across devices. HTML5 introduced the <video> element in 2010, giving browsers a native way to play video files. Initially, the element only worked with a complete file—you pointed it at a single MP4 or WebM URL, and the browser downloaded and played it.
That simple model was fine for short clips but broke down for long videos, live streams, or adaptive bitrate playback. If you wanted to switch quality mid-stream, or start playback near the end of a file without downloading everything, you needed something more. That something is Media Source Extensions, a W3C specification that became widely supported in browsers around 2016.
What Media Source Extensions Do
MSE is a JavaScript API that lets you build a media stream piece by piece. Instead of giving the video element a single URL, you create a MediaSource object, attach it to the video element via URL.createObjectURL(), and then append chunks of data (called "segments") to it using SourceBuffer objects. The browser plays the data as soon as enough is buffered, and you can keep appending more as the user watches.
This enables three key features:
- Adaptive bitrate streaming: You can measure the network speed and switch to a lower or higher quality segment mid-playback.
- Live streaming: You can append new segments as they are produced, and the video element plays them with low latency.
- Seeking without full download: You can jump to a specific time and only append the segments needed to play from that point.
MSE does not handle the network requests itself. You still use fetch() or XMLHttpRequest to download segments, then pass them to the SourceBuffer. This separation gives you full control over the streaming logic.
How HTML5 Video and MSE Work Together
Here’s a simplified flow of how a typical MSE-based player works:
- The page loads a JavaScript player (like Shaka Player, hls.js, or a custom one).
- The player fetches a manifest file (e.g., MPD for DASH, m3u8 for HLS) that lists available qualities and segment URLs.
- The player creates a
MediaSourceand sets it as the video element’s source. - For each track (video, audio, subtitles), the player adds a
SourceBufferwith the correct MIME type (e.g.,video/mp4; codecs="avc1.42E01E"). - The player downloads the first few segments, appends them to the SourceBuffer, and the video starts playing.
- During playback, the player monitors the buffer and downloads more segments, possibly switching to a different quality based on bandwidth.
The video element itself remains unchanged—it just plays whatever data the SourceBuffer feeds it. This is why MSE is often described as "the plumbing" for video: it provides the pipes, and JavaScript decides what goes through them.
Choosing Between MSE, HLS, and DASH
MSE is not a streaming protocol; it’s a building block. In practice, you’ll use it with either HLS (HTTP Live Streaming) or DASH (Dynamic Adaptive Streaming over HTTP).
- DASH is an international standard (ISO/IEC 23009) that works in all modern browsers via MSE. It uses MP4 or WebM segments and an XML manifest.
- HLS is Apple’s protocol, originally for iOS and Safari. Safari supports HLS natively without MSE, but other browsers need a JavaScript library like hls.js that converts HLS to MSE-compatible segments.
For most web applications, you don’t need to choose manually. Libraries like Shaka Player (DASH) or hls.js (HLS) handle the MSE details for you. You just give them a manifest URL and they manage the rest. If you’re building a custom solution, you’ll need to implement the MSE logic yourself, which is complex but doable for simple cases.
Common Problems and Misconceptions
One common misconception is that MSE is the same as adaptive streaming. It’s not—MSE is the mechanism, and adaptive streaming is one use case. Another is that MSE works with any video format. It only works with formats the browser can decode, and the segments must be in a container the browser supports (typically MP4 or WebM).
Here are typical issues you might encounter:
- Codec mismatches: The MIME type you specify in
addSourceBuffer()must match the actual codec in the segments. If they don’t, the browser throws an error. - Buffer overflows: If you append data too fast, the SourceBuffer can fill up. You need to remove old data (using
remove()) or wait for theupdateendevent. - Seeking delays: If the player doesn’t pre-buffer the right segments, seeking can be slow. You need to handle the
seekingevent and append the appropriate data. - Live streams stalling: For live content, the buffer should be short (a few seconds) to keep latency low. If you append too much, you’ll have high latency; too little, and you’ll get rebuffering.
Another misconception is that MSE is only for streaming. You can also use it to concatenate video files, add text tracks, or even generate video entirely in JavaScript (e.g., using WebCodecs).
Comparison: MSE vs. Native HLS vs. WebRTC
MSE is not the only way to play video in a browser. Here’s how it compares to other approaches:
- Native HLS (Safari): Safari has built-in HLS support, so you don’t need MSE or JavaScript. It’s simpler but only works in Safari.
- WebRTC: For low-latency live streaming (under 1 second), WebRTC is better than MSE because it uses UDP and peer-to-peer connections. MSE is HTTP-based and typically has 3-10 seconds of latency.
- Plain
<video>with a single file: This is the simplest option for short clips. It works everywhere but doesn’t support adaptive quality or live streaming.
In practice, most professional streaming services use MSE with DASH or HLS, and reserve WebRTC for real-time communication like video calls.
Costs and Performance Considerations
MSE itself is free—it’s a browser API. The costs come from the infrastructure and development effort. Using MSE with a library like hls.js adds a small JavaScript overhead (around 100 KB gzipped) but saves you from writing complex logic. If you build a custom player, you’ll need to handle edge cases like codec support, buffer management, and error recovery, which can be time-consuming.
Performance-wise, MSE can be more efficient than downloading a full file because you only download what you watch. However, it does use more CPU than simple playback because JavaScript is doing the heavy lifting. On low-end devices, this can cause dropped frames, so you should test on a range of hardware.
Frequently Asked Questions
Do I need MSE to play video in HTML5?
No. You can use the <video> element with a simple file URL for basic playback. MSE is only needed for adaptive streaming, live video, or when you need to manipulate the stream in JavaScript.
Is MSE supported in all browsers?
MSE is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge. Safari had partial support for a while but now works with MP4 segments. Internet Explorer 11 also supports it, but with some limitations.
Can I use MSE with YouTube or Netflix?
Yes, YouTube and Netflix use MSE (or similar proprietary APIs) to deliver adaptive streaming. You don’t need to do anything special as a viewer—the site’s JavaScript handles it.
What is the difference between MSE and EME?
MSE handles the transport of media data, while Encrypted Media Extensions (EME) handles DRM (digital rights management). EME is what allows protected content like movies to be played. They are separate APIs but often used together for commercial streaming.
How do I know which codec to use?
Check the browser’s MediaSource.isTypeSupported() method. It returns true if the browser can play a given MIME type and codec. For example, MediaSource.isTypeSupported('video/mp4; codecs="avc1.42E01E"') tells you if H.264 MP4 is supported.
Can MSE play live streams with very low latency?
MSE can achieve latency of a few seconds, but not sub-second. For truly low latency (under 1 second), you need WebRTC or a specialized protocol like LL-HLS (Low-Latency HLS) with MSE, which can get down to 1-3 seconds with careful tuning.