Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass in MediaStream object #1

Merged
merged 1 commit into from Sep 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions README.md
Expand Up @@ -9,18 +9,18 @@ npm install media-recorder-stream
## Usage

``` js
var getMedia = require('getusermedia')
var recorder = require('media-recorder-stream')

var stream = recorder({
video: true,
audio: false
})
getMedia({video: true, audio: true}, function (err, media) {
if (err) throw err

stream.on('data', function (data) {
console.log('recorded video data:', data)
})
var stream = recorder(media, {interval: 1000})

stream.on('data', function (data) {
console.log('recorded video data:', data)
})

stream.on('ready', function () {
// lets display the recorded video as well
video.src = URL.createObjectURL(stream.media)
video.autoplay = true
Expand All @@ -30,15 +30,16 @@ stream.on('ready', function () {

## API

#### `var stream = recorder(options)`
#### `var stream = recorder(media, options)`

`media` is a [`MediaStream` object](https://developer.mozilla.org/en-US/docs/Web/API/Media_Streams_API)
from the `getUserMedia` API.

Options include

``` js
{
interval: 1000, // at which ms interval you want to capture video (defaults to 1s)
video: true, // do you want to capture video
audio: false // do you want to capture audio
}
```

Expand All @@ -48,10 +49,6 @@ All other options are passed to the Media Recorder constructor

Will destroy the recording stream

#### `stream.on('ready')`

Emitted when the stream is ready to record. The properties below will be null if accessed before

#### `stream.media`

The media source stream associated with this stream. You can create a blob url from this and pass it to a video tag to play the recorded video locally.
Expand Down
22 changes: 13 additions & 9 deletions example.js
@@ -1,14 +1,18 @@
var record = require('./')
var getMedia = require('getusermedia')
var recorder = require('./')

var stream = record({video: true, audio: false, interval: 1000})
var video = document.createElement('video')
getMedia({video: true, audio: true}, function (err, media) {
if (err) throw err

stream.on('ready', function () {
video.src = window.URL.createObjectURL(stream.media)
var stream = recorder(media, {interval: 1000})

stream.on('data', function (data) {
console.log('recorded video data:', data)
})

// lets display the recorded video as well
var video = document.createElement('video')
video.src = URL.createObjectURL(media)
video.autoplay = true
document.body.appendChild(video)
})

stream.on('data', function (data) {
console.log('recording', data)
})
21 changes: 8 additions & 13 deletions index.js
@@ -1,9 +1,8 @@
var stream = require('readable-stream')
var getMedia = require('getusermedia')

module.exports = createRecordStream

function createRecordStream (opts) {
function createRecordStream (media, opts) {
if (!opts) opts = {}

var rs = stream.Readable()
Expand All @@ -22,20 +21,16 @@ function createRecordStream (opts) {
if (rs.recorder) rs.recorder.stop()
if (err) rs.emit('error', err)
rs.emit('close')
rs.recorder = null
rs.media = null
}

getMedia({video: !!opts.video, audio: !!opts.audio}, function (err, media) {
if (err) return rs.destroy(err)
if (rs.destroyed) return

rs.media = media
rs.recorder = new window.MediaRecorder(media, opts)
rs.recorder.addEventListener('dataavailable', function (ev) {
push(ev.data)
})
rs.recorder.start(opts.interval || 1000)
rs.emit('ready')
rs.media = media
rs.recorder = new window.MediaRecorder(media, opts)
rs.recorder.addEventListener('dataavailable', function (ev) {
push(ev.data)
})
rs.recorder.start(opts.interval || 1000)

return rs

Expand Down