-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp-media-recorder.js
More file actions
370 lines (313 loc) · 9.53 KB
/
app-media-recorder.js
File metadata and controls
370 lines (313 loc) · 9.53 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import {Base, Polymer} from '@polymer/polymer/polymer-legacy.js';
var mediaRecorderSupported = true;
if (window.MediaRecorder == null) {
Base._warn('Media Recorder API is not supported in this browser!');
mediaRecorderSupported = false;
}
/** @see https://www.w3.org/TR/mediastream-recording/#enumdef-recordingstate */
export const RecordingState = {
INACTIVE: 'inactive',
RECORDING: 'recording',
PAUSED: 'paused'
};
/**
`app-media-recorder` is an element that, when configured with a media stream,
generates recordings based on that stream and produces them as blobs for further
consumption and processing.
NOTE: As of today (January 6th, 2017), Media Recorder API is only supported in
Chrome, Firefox and Opera. This element will politely neglect to register itself
in browsers that do not support the Media Recorder API.
Some polyfill-esque libraries (like
[this one](https://www.webrtc-experiment.com/msr/)) exist at the time of this
writing, but do not work as drop-in substitutes. If you have a polyfill that
you consider to be a suitable substitute, load it first and ensure that
`window.MediaRecorder` points to the correct constructor. For example:
```
<script src="/some/path/MediaStreamRecorder.js"></script>
<script>
// Substitute the MediaStreamRecorder constructor for MediaRecorder before
// <app-media-recorder> is loaded:
window.MediaRecorder = MediaStreamRecorder;
</script>
<script type="module">
import '@polymer/app-media/app-media-recorder.js';
</script>
```
@demo demo/index.html
*/
Polymer({
is: 'app-media-recorder',
/** @override */
_template: null,
properties: {
/**
* The input media stream to base the recordings off of.
* @type {MediaStream}
*/
stream: {type: Object},
/**
* A reference to the media recorder that is used to generate
* recordings of the stream.
*
* @type {MediaRecorder}
*/
recorder: {
type: Object,
notify: true,
computed:
'_computeRecorder(stream, mimeType, audioBitsPerSecond, videoBitsPerSecond, bitsPerSecond)'
},
/**
* The timeslice to use when recording the stream with the media
* recorder.
*/
timeslice: {type: Number, value: 10},
/**
* The duration of the recording, in milliseconds. If set to a value
* greater than 0, the recording will automatically end after the
* configured amount of time (unless there is some manual
* intervention). If set to 0 (the default), recording will continue
* until manually stopped (or your device melts).
*/
duration: {type: Number, value: 0},
/**
* The computed mime type for the output recording.
*/
mimeType: {
type: String,
computed: '_computeMimeType(stream, mpeg, codecs)',
observer: '_mimeTypeChanged'
},
/**
* If true, the computed mime type will be video/mpeg.
*/
mpeg: {
type: Boolean,
value: false,
},
/**
* If a value is given, the computed mime time will include a suffix
* specifying the value as a specific codec e.g., for vp8, the mime
* type will be video/webm\;codecs=vp8.
*/
codecs: {
type: String,
value: null,
},
/**
* The time elapsed since the recorder began recording, in
* milliseconds.
*/
elapsed: {
type: Number,
readOnly: true,
notify: true,
value: 0,
},
/**
* A blob for the most recently completed recording.
*
* @type {Blob}
*/
data: {
type: Object,
readOnly: true,
notify: true,
},
/**
* When set to true, the recorder will start recording. When set to false,
* the recorder will stop recording and data will be updated as the
* recording becomes available. Calling `start` will cause this property
* to be set to true. Calling `stop` will cause this property to be set to
* false.
*/
recording: {
type: Boolean,
value: false,
notify: true,
},
/**
* When set to true, the recorder will only dispatch
* `app-media-recorder-chunk` events, and will not keep a local cache of the
* data chunks that have been recorded. A consequence of this is that a
* final `data` Blob will not be available when the recording has finished.
*/
disableBlobCache: {
type: Boolean,
value: false,
},
/**
* The desired bitrate for the audio tracks of the recorded stream.
* Set to null to use the platform default.
*/
audioBitsPerSecond: {
type: Number,
value: null,
},
/**
* The desired bitrate for the video tracks of the recorded stream.
* Set to null to use the platform default.
*/
videoBitsPerSecond: {
type: Number,
value: null,
},
/**
* The default bitrate for both audio and video tracks of the
* recorded stream. If this is specified, it will be used in place
* of audioBitsPerSecond or videoBitsPerSecond if either or both are
* not specified.
* Set to null to use the platform default.
*/
bitsPerSecond: {
type: Number,
value: null,
}
},
observers: ['_recordingChanged(recording, recorder)'],
/**
* Start recording from the source media stream.
*/
start: function() {
if (!mediaRecorderSupported) {
return;
}
var data = [];
var start = performance.now();
var finished = false;
var onDataAvailable = function(event) {
var elapsed = performance.now() - start;
if (event.data && event.data.size > 0) {
this.fire('app-media-recorder-chunk', event.data);
if (!this.disableBlobCache) {
data.push(event.data);
}
}
if (this.duration > 0 && this.duration < elapsed && !finished) {
elapsed = this.duration;
finished = true;
this.stop();
}
this._setElapsed(elapsed);
}.bind(this);
var onStop = function() {
this.recorder.removeEventListener('dataavailable', onDataAvailable);
this.recorder.removeEventListener('stop', onStop);
this.fire('app-media-recorder-stopped');
if (this.disableBlobCache) {
return;
}
this._setData(new Blob(data, {type: this.mimeType}));
}.bind(this);
this.stop();
this.recorder.addEventListener('dataavailable', onDataAvailable);
this.recorder.addEventListener('stop', onStop);
this.recorder.start(this.timeslice);
this.recording = true;
},
/**
* Stop recording from the source media stream. The result of the
* recording will be made available as a new value for the data
* property.
*/
stop: function() {
if (!mediaRecorderSupported) {
return;
}
this._setElapsed(0);
if (this.recorder.state !== RecordingState.INACTIVE) {
this.recorder.stop();
this.recording = false;
}
},
/**
* Pause recording.
*/
pause: function() {
if (!mediaRecorderSupported)
this.recorder.pause();
},
/**
* Resume recording if paused.
*/
resume: function() {
if (!mediaRecorderSupported) {
return;
}
if (this.recorder.state === RecordingState.PAUSED) {
this.recorder.resume();
}
},
_computeRecorder: function(stream, mimeType, audioBps, videoBps, bps) {
if (!mediaRecorderSupported) {
return;
}
if (mimeType == null || stream == null) {
return this.recorder;
}
var options = {mimeType: mimeType};
if (audioBps != null) {
options.audioBitsPerSecond = audioBps;
}
if (videoBps != null) {
options.videoBitsPerSecond = videoBps;
}
if (bps != null) {
options.bitsPerSecond = bps;
}
return new MediaRecorder(stream, options);
},
_computeMimeType: function(stream, mpeg, codecs) {
if (stream == null || !mediaRecorderSupported) {
return;
}
var candidate;
if (mpeg) {
candidate = 'video/mpeg';
} else if (stream.getVideoTracks().length > 0) {
candidate = 'video/webm';
} else {
candidate = 'audio/webm';
}
if (codecs) {
// NOTE(cdata): This specifies a codec if one is preferred by the user
// configuration of an element.
// A reference for the mimetype format can be found here:
// https://tools.ietf.org/html/rfc2046
// Examples of specifying codecs in mimetypes can be found here:
// https://www.w3.org/TR/mediastream-recording/#check-for-mediarecorder-and-mimetype.x
candidate += '\;codecs=' + codecs;
}
return candidate || '';
},
_recordingChanged: function(recording, recorder) {
if (recorder == null || !mediaRecorderSupported) {
return;
}
if (recording && this.recorder.state === RecordingState.INACTIVE) {
this.start();
} else if (
!recording &&
this.recorder.state !== RecordingState.INACTIVE) {
this.stop();
}
},
_mimeTypeChanged: function(mimeType) {
if (!mimeType || !mediaRecorderSupported) {
return;
}
if (!MediaRecorder.isTypeSupported(mimeType)) {
this._warn(this._logf('Browser does not support mime-type', mimeType));
}
}
});