Git Product home page Git Product logo

mediasource's Introduction

mediasource travis npm downloads javascript style guide

MediaSource API as a node.js Writable stream

Sauce Test Status

Stream video/audio into a <video> or <audio> tag by attaching node.js Writable streams.

This package is used by WebTorrent (along with other approaches) to support media streaming.

install

npm install mediasource

usage

var MediaElementWrapper = require('mediasource')

function createElem (tagName) {
  var elem = document.createElement(tagName)
  elem.controls = true
  elem.autoplay = true // for chrome
  elem.play() // for firefox
  document.body.appendChild(elem)
  return elem
}

var elem = createElem('video')

var readable = // ... get a readable stream from somewhere
var wrapper = new MediaElementWrapper(elem)
// The correct mime type, including codecs, must be provided
var writable = wrapper.createWriteStream('video/webm; codecs="vorbis, vp8"')

elem.addEventListener('error', function () {
  // listen for errors on the video/audio element directly
  var errorCode = elem.error
  var detailedError = wrapper.detailedError
  // wrapper.detailedError will often have a more detailed error message
})

writable.on('error', function (err) {
  // listening to the stream 'error' event is optional
})

readable.pipe(writable)

// media should start playing now!

advanced usage

wrapper.createWriteStream() can be called multiple times if different tracks (e.g. audio and video) need to be passed in separate streams. Each call should be made with the correct mime type.

Instead of a mime type, an existing MediaSourceStream (as returned by wrapper.createWriteStream()) can be passed as the single argument to wrapper.createWriteStream(), which will cause the existing stream to be replaced by the newly returned stream. This is useful when you want to cancel the existing stream and replace it with a new one, e.g. when seeking.

should one use this package?

Naively using this package will not work for many video formats, nor will it support seeking. For an approach that is more likely to work for all video files, and supports seeking, take a look at videostream.

Or for a package that tries multiple approaches, including videostream and this package (mediasource), as well as a Blob API (non-streaming) approach, and works for many non-video file types, consider render-media.

options

opts.bufferDuration

Specify how many seconds of media should be put into the browser's buffer before applying backpressure.

errors

Handle errors by listening to the 'error' event on the <video> or <audio> tag.

Some (but not all) errors will also cause wrapper.detailedError to be set to an error value that has a more informative error message.

license

MIT. Copyright (c) Feross Aboukhadijeh.

mediasource's People

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

mediasource's Issues

What to do about iOS?

Is there an existing solution for lack of MediaSource on iOS? Just checking before I try to WASM an OPUS decoder.

How can I reset the video?

Hi!!

Thanks for the library! Looks awesome to me.

I'm using it to transfer realtime MP4 over the webrtc datachannel and render it in the browser. However when the user goes to another tab, Chrome throttle it and when the user goes back to the tab it starts to play the video of several seconds ago.

How should I fix this?

Some ideas:

  1. Any way to restart the stream and tell the library: forget all old frames and render just from here?
  2. I had the idea of don't write frames that came over my channel, while the document is not focused. However missing frames makes the video freeze for about 2 minutes. After that it start working again.

Thanks in advance!

Inheritance not working as expected?

Currently I am trying to use mediasource along with media-recorder-stream to send audio and video over an rtc datachannel. I looked at the usage section in the README but am running into an issue. We are using react as a frontend framework but I don't believe that could be the root cause of this.

// Create writeable stream for the remote video elem
  var wrapper = new MediaElementWrapper(playbackVideoRemote);
  var writeable = wrapper.createWriteStream("audio/webm; codecs=pcm")
  if(peerReadableStream.locked) {
    console.log("read stream is locked");
  } else {
    console.log("Not locked")
  }
  if(writeable.locked) {
    console.log("Write is locked");
  } else {
    console.log("Not locked");
  }
  try {
    console.log(writeable);
    peerReadableStream.pipeTo(writeable);
  }
  catch(err) {
    console.log(err);
  }

The code above is where I'm running into an error. I am getting an illegal invocation on pipeTo because writeable in this instance is a MediaSourceStream and not a WriteableStream. I looked at the source code and see that MediaSourceStream inherits Writeablestream but is there a reason that the inheritance is not working in this instance? The console.log statements were when I first thought the streams may be locked already but they turned out not to be.

h264 with Chrome

I have the mp4 / h264 codec working with Firefox v49.0.2, using: video/mp4; codecs="avc1.64001e, mp4a.40.2".

However, this doesn't stream for Chrome v55.0.2883.87.

According to window.MediaSource.isTypeSupported('video/mp4; codecs="avc1.64001e, mp4a.40.2"'), the type is supported in the Chrome browser.

Also, test file is fragmented and video is definitely using this version of the codec (tested using http://nickdesaulniers.github.io/mp4info/ and mp4file.

Any ideas what may be the problem?

An in-range update of brfs is breaking the build 🚨

The devDependency brfs was updated from 2.0.1 to 2.0.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

brfs is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 4 commits.

  • 660f6d8 2.0.2
  • eb26fec Merge pull request #93 from jagonzalr/pr/update/static-val-version
  • 434cc8b Update static-module from 3.0.0 to 3.0.2
  • 8fafcf5 Remove stray console.log

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Torrent-stream integration

Hello, I have tested many ways, but I cant get this working in node webkit with the torrent-stream library, do you have any sample of this? I know that this is another torrent lib, not yours.
Thanks.

DOMException: "The quota has been exceeded."

This has been happening in Firefox with high definition files which have a higher bitrate. When we move around the video quick, the buffer fills up very fast and doesn't seem to empty fast enough for Firefox.

I found this issue that might help when I was looking around at other projects. videojs/http-streaming#192

Video freezing, but stream is still receiving data

Hi there! Great tool, it's exactly what I needed. As the title says, however, my videos freeze after 10 or so seconds (it's not the same every time) but the stream is still getting data just fine.

Here's my code snippet. I'm using socket.io to handle the stream from server-to-browser. I logged out the data the stream was receiving and it keeps going just fine after the freeze. There's no error message. Do you have any ideas about how I could debug this? Possible causes?

ss(this.socket).on('vid-back', backStream => {
                        
    console.log("Server opened backwards stream");
    
    var elem = document.querySelector('#stream-video');
    var wrapper = new MediaElementWrapper(elem);
    var writable = wrapper.createWriteStream('video/webm; codecs="vp8"');
    
    elem.addEventListener('error', function () {
        var errorCode = elem.error
        var detailedError = wrapper.detailedError
        console.error(errorCode);
        console.error(detailedError);
    })
    
    backStream.pipe(writable);
    
    backStream.on('data', data => {
        console.log('> Received blob of data');
    })
})

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.