bitstream-js

1.3.0 • Public • Published

Bitstream.js

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:

<html>

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

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

  <script>
    const video = document.getElementById("video");
    const stream = new Bitstream();
    stream.attachMedia(video);
    stream.loadSource("stream.m3u8");
  </script>
</body>

</html>

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.

Affect 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).

Note

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.

Package Sidebar

Install

npm i bitstream-js

Weekly Downloads

0

Version

1.3.0

License

GPL-3.0-or-later

Unpacked Size

1.56 MB

Total Files

4

Last publish

Collaborators

  • codeconqueror