Giter Site home page Giter Site logo

gitsummore / nile.js Goto Github PK

View Code? Open in Web Editor NEW
1.8K 55.0 103.0 1.36 MB

Scalable peer to peer live video streaming built on torrents and webRTC

Home Page: https://nilejs.com/

License: MIT License

JavaScript 91.23% HTML 5.40% CSS 3.38%
webtorrent webrtc livestream distributed peer-to-peer torrent

nile.js's Introduction

nile.js

A tool for scalable peer-to-peer video streaming using WebTorrent.

NOTE: This project is no longer being actively maintained

Why WebTorrent?

By using the collective power of WebTorrent, video streams get progressively stronger as more peers contribute to a torrent. With torrents, it is also possible for users to access previous parts of a stream unlike traditional WebRTC video streaming.

About

Server

This is the plug-and-play middleware that receives the torrent link from the broadcasting client and sets up the proper Socket.io connections for the viewing clients.

Broadcaster

This is the client component that records video from a device's camera, saving it to generated torrent files, and sending those torrents' magnet link out to the viewing clients.

Viewer

This is the client which views what the Broadcaster is recording. It receives a torrent magnet link and renders the video from that torrent to an injected video tag using WebTorrent.

Usage

Server

Nile.js utilizes Express middleware and socket.io to receive torrent information, broadcast it to as many clients it can comfortably handle who will then send it out to the rest of the clients.

To use it, require nileServer from our package and pass in the Node Server instance you're using. In Express, you can get this instance by calling app.listen.

You'll also need to pass in the number of WebSockets the server has to maintain. Let's use 10 for now.

Here's how you would use it in your server:

const server = app.listen(8000);
const socketLimit = 10;
const nileServer = require('nile.js/nileServer')(server, 10);

Now add the nile.js middleware w/ app.use:

app.use('/', nileServer);

Note that this middleware will use a "magnet" route to accept POST requests with the magnet link from the Broadcaster.

Client

Broadcaster

If using a file bundler e.g. (webpack), you may import the module.

import { Broadcaster } from 'nile.js'

If you just want to test the module without bundling, it is currently being hosted on unpkg CDN. Use it as a script in your html file.

https://unpkg.com/[email protected]/client/dist/nile.Broadcaster.min.js

4 parameters:

  1. recordInterval - The Interval that the webcam recording should seed each segment of the video (ms)
  2. videoNodeIDForPlayback - The id of the video node in the html where the broadcaster can see their own recording
  3. startStreamID - The id of the button node that BEGINS the recording/live streaming
  4. stopStreamID - The id of the button node that ENDS the recording/live streaming

The Broadcaster object is used to stream video to a torrent and send the torrent link to the server and then to the network of viewers.

Because torrents are immutable, we approximate streaming with torrents by setting a recordInterval, in milliseconds. This sets how long each clip will be before being sent via torrent. From our experience, we recommend an interval 6000-10000 (6-10 seconds).

Next, pass in the ID of the video tag you'd like to view your recording playback from as well as button IDs for the starting and stopping the stream.

Example:

const broadcaster = new Broadcaster(8000, 'video', 'button-play-gum', 'button-stop-gum');

Viewer

If using a file bundler e.g. (webpack), you may import the module.

import { Viewer } from 'nile.js'

If you just want to test the module without bundling, it is currently being hosted on unpkg CDN. Use it as a script in your html file.

https://unpkg.com/[email protected]/client/dist/nile.Viewer.min.js

2 parameters:

  1. ID_of_NodeToRenderVideo - ID of DOM element to render live feed to
  2. addedIceServers - Array of extra WebRTC ICE servers, based on this interface laid out by W3C

The Viewer object receives torrent links from Socket.io or RTCDataChannel connections and progressively renders the videos from the torrents to the supplied ID, ID_of_NodeToRenderVideo.

Example:

const viewer = new Viewer('videos');

The Viewer maintains two WebRTC connections, one to a parent (client closest to server) and a child client (farther from server). These two connections create a chain of clients that propagate server-sent torrent information down to subsequent viewers down the chain.

In the event of a client disconnecting, the disconnecting Viewer will let its immediate child client know and tell it to reconnect to its parent. This maintains network integrity and ensures that the stream will still reach every client in that chain.

nile.js's People

Contributors

derekmiranda avatar frozzare avatar imvetri avatar joshterrill avatar jpmitchellpierson avatar kbbqiu avatar robawilkinson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nile.js's Issues

Browser crash upon consecutive seeds, possible memory leak?

As we rack up the number WebTorrent seeds on the broadcaster side, there seems to be a client side memory leak which eventually causes the browser to crash. We’ve checked the CPU and memory usage but have found no error messages or anomalies.

Unsuccessful approaches that we have taken to solve the issue:

  • Server side seeding in place of client side seeding
  • Seeding in web workers
  • Seeding in iframes
  • Destroying WebTorrent client on each new seed
  • Destroying WebTorrent file on each new seed

One solution that we have found, is browser specific. If using Chrome Canary to test the library, it seems to run indefinitely. If anyone in the open source community is able to provide some insight, please make a pull request on our GitHub repo.

Events

Hi there,

Congrats, this is a great project. It would be nice to have event handlers for the socket.io features, like emiting a broadcast notification when a broadcaster is on, so it wouldn't require a second socket.io instance.

Possible to stream video without recording feature?

I understand that you can use regular WebRTC to stream video to clients without recording, but I was wondering if it would be possible to integrate that as a flag in this library as well. That way I can use one library and keep everything in one place. If that's possible, I could probably work on it, just point me in the right direction.

Lag

What sort of lag should I expect when using this with:

  • 10 concurrent
  • 100 concurrent
  • 1000 concurrent
  • 10,000 concurrent

Latency for live stream ?

I have not tried your solution yet, but I guess since you record some chunks, you do create some latency.
Any idea on what would be the minimal latency your solution can handle ?

Regards

Add ability to specify custom broadcast server port

Right now, it doesn't appear that I can run the broadcast server separately from viewer servers since it looks for the magnet endpoint on the same port.

It'd be nice to be able to specify a particular server/port to connect to for the magnet endpoint.

Tried example, didnt work

I started the demo-server.js, opened the client/index.html, click "start streaming", it asks for permission then nothing happens for a while and then these errors.
test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.