• This repository has been archived on 08/Jan/2019
  • Stars
    star
    233
  • Rank 172,230 (Top 4 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross-browser getUserMedia shim with a node.js style error-first API

getUserMedia

What is this?

A tiny browser module that gives us a simple API for getting access to a user's camera or microphone by wrapping the navigator.getUserMedia API in modern browsers.

This is important because as of right now, all browsers return errors differently. More on that below.

This lib also gracefully handles the lack of support in a browser. So you never have to check first (see error handling below).

It gives us a cleaner node.js-style, error-first API and cross-browser handling. No browser support checking necessary, lack of support is treated in the same way as when the user rejects the request: the callback gets passed an error as the first argument.

Suitable for use with browserify/CommonJS on the client.

If you're not using browserify or you want AMD support use getusermedia.bundle.js. Note that if no module system is detected it simply attaches a function called getUserMedia to window.

Installing

npm install getusermedia

How to use it

With this helper it's clean/simple to get access to a user's camera, mic, etc.

var getUserMedia = require('getusermedia');

getUserMedia(function (err, stream) {
    // if the browser doesn't support user media
    // or the user says "no" the error gets passed
    // as the first argument.
    if (err) {
       console.log('failed');
    } else {
       console.log('got a stream', stream);  
    }
});

Passing in options is optional. It defaults to {video: true, audio: true};

// optionally pass constraints as the first argument
// they just passed through.
getUserMedia({video: true, audio: false}, function (err, stream) { ... });

Why? Because it's super ugly without this tool

// first deal with browser prefixes
var getUserMedia = navigator.getUserMedia || 
    navigator.mozGetUserMedia || 
    navigator.webkitGetUserMedia;

// make sure it's supported and bind to navigator
if (getUserMedia) {
    getUserMedia = getUserMedia.bind(navigator);
} else {
    // have to figure out how to handle the error somehow
}

// then deal with a weird, positional error handling API
getUserMedia(
    // media constraints
    {video: true, audio: true}, 
    // success callback
    function (stream) {
        // gets stream if successful
    }, 
    // error callback
    function (error) {
        // called if failed to get media
    }
)

Handling errors (summary)

All failed calls to getusermedia in this library will return an error object (of type NavigatorUserMediaError) as the first argument to the callback. All will have a .name according to the specification

License

MIT

Created By

If you like this, follow: @HenrikJoreteg on twitter.

More Repositories

1

hark

Converts an audio stream to speech events in the browser
JavaScript
566
star
2

otalk-im-client

Stanza.io webrtc capable xmpp client <3
JavaScript
306
star
3

RTCPeerConnection

A tiny browser module that gives normalizes and simplifies the API for WebRTC peer connections.
JavaScript
200
star
4

getScreenMedia

A browser module for attempting to get access to a MediaStream of a user's screen. With a nice node-like API.
JavaScript
154
star
5

restund

Restund. Forked from http://www.creytiv.com/
C
127
star
6

jingle.js

A generic Jingle session manager implementation, suitable for integration by other XMPP libraries.
JavaScript
89
star
7

attachMediaStream

Simple abstraction for handling differences between browsers for attaching a media stream to a video element
JavaScript
48
star
8

iOS-demo

Objective-C
46
star
9

sdp

SDP parsing and serialization utlities
JavaScript
46
star
10

mediastream-gain

A tiny browser module for creating a gain/volume controller for the audio channels in a MediaStream.
JavaScript
44
star
11

webrtc-ios

Objective-C
44
star
12

sdp-jingle-json

A parser/serializer for SDP to JSON. Useful for converting SDP to other formats like Jingle for WebRTC signalling
JavaScript
41
star
13

webrtc-tester

WebRTC Deployment Testing Toolkit
Shell
34
star
14

TLKSimpleWebRTC

Objective-C
28
star
15

jxt

JSON/XML Translation for the Browser
JavaScript
26
star
16

jxt-xmpp

JXT definitions for XMPP
JavaScript
23
star
17

stunturncheck

Check WebRTC STUN and TURN connectivity
JavaScript
17
star
18

localmedia

WebRTC abstraction for creating and managing local media streams.
JavaScript
16
star
19

rtcpeerconnection-shim

Implementation of the RTCPeerConnection API ontop of ORTC
JavaScript
15
star
20

TLKWebRTC

Objective-C
13
star
21

filetransfer

filetransfer via a webrtc datachannel
JavaScript
13
star
22

mod_muc_focus

Prosody + Jingle/COLIBRI = multimedia MUC!
Lua
10
star
23

traceablepeerconnection

Lowlevel RTCPeerConnection wrapper that traces API calls
JavaScript
10
star
24

rtt-buffer

Realtime Text Buffer
JavaScript
9
star
25

jingle-media-session

Jingle Media Session
JavaScript
8
star
26

xmpp-jid

Parse & manage XMPP addresses
JavaScript
7
star
27

otalk-media-controller

Track local & remote media streams
JavaScript
6
star
28

xmpp-uri

Parse XMPP URIs
JavaScript
5
star
29

jxt-xmpp-types

JXT types for working with XMPP data
JavaScript
5
star
30

otalk-media-stream-view

Video/audio box, with controls
JavaScript
5
star
31

sdp-jingle-table

A parser/serializer for SDP to Lua tables. Useful for converting SDP to other formats like Jingle for WebRTC signaling.
Lua
4
star
32

xmpp-constants

Protocol constants for XMPP
JavaScript
4
star
33

rtcpeerconnection-jingle

rtcpeerconnection, extended with a JSON format. And easy to map to Jingle
JavaScript
3
star
34

hostmeta.js

Easily retrieve and parse a host's /.well-known/host-meta[.json] file.
JavaScript
3
star
35

mod_influxdb

Prosody Metrics to Influxdb
Lua
3
star
36

jingle-session

Generic base Jingle session
JavaScript
3
star
37

simplewebrtc.com

This site is DEPRECATED. simplewebrtc.com is a project of &yet and has been moved to andyet/simplewebrtc.com
CSS
3
star
38

jingle-filetransfer-session

Jingle FileTransfer Session
JavaScript
3
star
39

otalk-model-media

A model for WebRTC media streams
JavaScript
2
star
40

otalk-model-message

Ampersand model for XMPP messages
JavaScript
2
star
41

otalk-specs

XMPP extensions for Otalk functionality
XSLT
2
star
42

otalk-model-peer

A base model for XMPP peer entities
JavaScript
1
star
43

mod_muc_allowners

Lua
1
star
44

otalk-media-devices

Track user media devices
JavaScript
1
star
45

mod_turncredentials

XEP-0215 implementation for the prosody xmpp server
Lua
1
star
46

otalk-model-disco

Model for XMPP service discovery information
JavaScript
1
star
47

mod_muc_allhidden

Make all MUC rooms hidden by default
Lua
1
star
48

otalk.org

otalk.org website
CSS
1
star