-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
133 lines (98 loc) · 3.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var plugin = require('rtc-core/plugin');
var extend = require('cog/extend');
/**
# rtc-attach
Roughly equivalent to the
[`attachMediaStream`](https://www.npmjs.org/package/attachmediastream)
package but with support for rtc.io plugins. Also uses an error first
async API to allow plugins time to initialize.
## Example Usage
<<< examples/simple.js
## Example using Plugins
<<< examples/plugins.js
## Reference
### `attach(stream, opts?, callback)`
Attach `stream` to a HTML element that will render the content. The provided
`callback` follows the format of `fn(err, element)`. While the async nature
of this package may seem odd, because a plugin may need time to initialize
this caters for this case in addition to standard usage in the browser.
- `autoplay` (default: `true`) - by default after the stream has been
attached to the element it will be played. This is done by calling
the `play()` function on the element rather than relying on `autoplay`
attribute functionality.
- `el` (default: `null`) - if you with to supply an element to be used
instead of creating a new element to receive the stream specify it here.
- `muted` (default: `false`) - whether the created element should be muted
or not. For local streams this should almost always, be true so consider
using the `attach.local` helper function for simple cases.
- `plugins` (default: `[]`) - specify one or more plugins that can be used
to render the media stream appropriate to the current platform in the
event that WebRTC and/or media capture is supported via a browser plugin.
**/
var attach = module.exports = function(stream, opts, callback) {
var pinst;
if (typeof opts == 'function') {
callback = opts;
opts = {};
}
function applyModifications(el, o) {
if ((o || {}).muted) {
el.muted = true;
el.setAttribute('muted', '');
}
if ((o || {}).mirror) {
el.setAttribute('data-mirrored', true);
}
return el;
}
function attachToElement(s, o) {
var autoplay = (o || {}).autoplay;
var elType = 'audio';
var el = (o || {}).el || (o || {}).target;
// check the stream is valid
var isValid = s && typeof s.getVideoTracks == 'function';
// determine the element type
if (isValid && s.getVideoTracks().length > 0) {
elType = 'video';
}
// if we have been passed an "unplayable" target create a new element
if (el && typeof el.play != 'function') {
el = null;
}
// prepare the element
el = el || document.createElement(elType);
// attach the stream
el.srcObject = stream;
if (autoplay === undefined || autoplay) {
el.setAttribute('autoplay', '');
el.play();
}
return applyModifications(el, o);
}
// see if we are using a plugin
pinst = plugin((opts || {}).plugins);
if (pinst) {
return pinst.init(opts, function(err) {
if (err) {
return callback(err);
}
if (typeof pinst.attach != 'function') {
return callback(new Error('plugin must support the attach function'));
}
callback(null, applyModifications(pinst.attach(stream, opts), opts));
});
}
callback(null, attachToElement(stream, opts));
};
/**
### `attach.local(stream, opts?, callback)`
Attach a local stream with options appropriate for local streams:
- `muted`: `true`
**/
attach.local = function(stream, opts, callback) {
if (typeof opts == 'function') {
callback = opts;
opts = {};
}
attach(stream, extend({ muted: true, mirror: true }, opts), callback);
};