A simple javascript library to enable P2P playback / streaming on an HLS source.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tcc f6ff28d780 readme 2 weeks ago
example reset 9 months ago
src reset 9 months ago
.drone.yml reset 9 months ago
.gitignore reset 9 months ago
.releaserc reset 9 months ago
LICENSE reset 9 months ago
README.md readme 2 weeks ago
package-lock.json reset 9 months ago
package.json reset 9 months ago
tsconfig.json init 9 months ago
webpack.config.js reset 9 months ago



A simple javascript library to enable P2P playback / streaming on an HLS source.

How it works

After you have a stream playlist loading with P2P is as simple as with Hls.js. On any html page on your site you simply need to include a single javascript dependency and instantiate the stream with a video tag:


  <script src="https://cdn.jsdelivr.net/npm/bitstream-js@latest"></script>
    #video {
      width: 100%;

  <video id="video" controls></video>

    const video = document.getElementById("video");
    const stream = new Bitstream();


This will then allow viewers of the stream to share chunks with each other when available instead of only downloading them from the stream server.

How to instantiate a simple stream

You send your stream via rtmp to a VPS (eg. using NGINX with the rtmp plugin). Then, start a stream with ffmpeg:

ffmpeg -i /dev/video0 -f hls -hls_time 5 -hls_playlist_type event rtmp://[hostname]:1935/live/stream

Then just add the bitstream script tag, and load the source from wherever you are writing the HLS manifest file to.

Effect on bandwidth

Ideally the bandwidth reduction from P2P will be enough to get this to run on a metered VPS. However, that depends on several factors. If the viewers are geographically distributed then P2P will hit less often.

Take a simple calculation of a 720p stream with 5000 live viewers. Traditionally if you have to stream to every viewer at a cost of $0.01 / GB. Then a 2 hour livestream will cost $100 to run. That's a pretty heafty toll depending on how frequently audience members donate etc. However with Bitstream (depending on geographic distribution, and user connectivity) you can typically expect over 80% p2p, and so the bandwidth would cost around $20.

Why not PeerTube?

We actually use the same P2P streaming technology from peertube and bundle it differently. However we feel like there are many thousands of lines of code in Peertube that are unnecessary when trying to do something simple like get video streamed P2P. A lot of that code is for purposes of integrating ActivityPub, and another huge amount is used to make it "feel" like youtube.

Bitstream's philosophy is different. It asks why blogging is relatively decentralized where video sharing is not, the answer seems to be bandwidth (and thus the need to couple the data to a proprietary platform which can show ads to pay the data transfer bills).


There is a setting in modern Firefox to disable webrtc peers. In this case we fall back to plain old HLS, but you may want to warn users that they are sucking up bandwidth using this setting. The setting is media.peerconnection. Eventually we might allow for a "force P2P setting" to be handled in case you very need to conserve bandwidth.