EasyRTC Documentation

Documentation

Source: easyrtc_recorder.js

1
/* global define, module, require, console */
2
/*!
3
Script: easyrtc_recorder.js
4
5
This code demonstrate recording of local and remote streams.
6
7
About: License
8
9
Copyright (c) 2016, Priologic Software Inc.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without
13
modification, are permitted provided that the following conditions are met:
14
15
* Redistributions of source code must retain the above copyright notice,
16
this list of conditions and the following disclaimer.
17
* Redistributions in binary form must reproduce the above copyright
18
notice, this list of conditions and the following disclaimer in the
19
documentation and/or other materials provided with the distribution.
20
21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
"use strict";
35
36
(function (root, factory) {
37
if (typeof define === 'function' && define.amd) {
38
//RequireJS (AMD) build system
39
define(['easyrtc'], factory);
40
} else if (typeof module === 'object' && module.exports) {
41
//CommonJS build system
42
module.exports = factory(require('easyrtc'));
43
} else {
44
//Vanilla JS, ensure dependencies are loaded correctly
45
if (typeof window.easyrtc !== 'object' || !window.easyrtc) {
46
throw new Error("easyrtc_recorder requires easyrtc");
47
}
48
root.easyrtc = factory(window.easyrtc);
49
}
50
}(this, function (easyrtc, undefined) {
51
52
/**
53
* Provides methods for building MediaStream recorders.
54
* @class Easyrtc_Recorder
55
*/
56
57
58
/**
59
* Determines if recording is supported by the browser. 
60
* @function
61
* @memberOf Easyrtc_Recorder
62
* @returns true if recording is supported.
63
*/
64
easyrtc.supportsRecording = function() {
65
return (typeof MediaRecorder !== "undefined" && navigator.getUserMedia );
66
};
67
68
/**
69
* Check if a particular codec can be used for recording.
70
* @function
71
* @memberOf Easyrtc_Recorder
72
* @param {String} codecName, either "vp8" or "vp9 or "h264"
73
* @returns true if the type can be used, or if the browser doesn't
74
*  support a method to find out.
75
*/ 
76
easyrtc.isRecordingTypeSupported = function(videoCodecName) {
77
var mimeType = "video/webm;codecs=" + videoCodecName;
78
if( MediaRecorder.isTypeSupported ) {
79
// chrome definitely, maybe firefox
80
return MediaRecorder.isTypeSupported(mimeType);
81
}
82
else if( MediaRecorder.isMimeTypeSupported ) {
83
// maybe firefox
84
return MediaRecorder.isMimeTypeSupported(mimeType);
85
}
86
else {
87
if( typeof easyrtc.hasNoRecordTypeCheck === "undefined") {
88
easyrtc.hasNoRecordTypeCheck = true;
89
window.alert("This browser doesn't know what media types it supports. Assuming all types.");
90
}
91
return true;
92
}
93
};
94
95
var mimeType;
96
97
/**
98
* Set the desired codec for the video encoding. 
99
* @function
100
* @memberOf Easyrtc_Recorder
101
* @param {String} codecName, either "vp8" or "vp9 or "h264"
102
* @returns true if the type can be used.
103
*/ 
104
easyrtc.setRecordingVideoCodec = function(videoCodecName) {
105
if( !easyrtc.supportsRecording ) {
106
return false;
107
}
108
if(easyrtc.isRecordingTypeSupported(videoCodecName)) {
109
mimeType = "video/webm;codecs=" + videoCodecName;
110
return true;
111
}
112
else {
113
return false;
114
}
115
};
116
117
if( easyrtc.supportsRecording()) {
118
easyrtc.setRecordingVideoCodec("vp8");
119
}
120
121
/**
122
* Create a recording object and attach a media stream to it.
123
* @function
124
* @memberOf Easyrtc_Recorder
125
* @param  {HTMLMediaStream} mediaStream 
126
* @returns a recorder object or null if recording not supported.
127
*/
128
function startRecording( mediaStream) {
129
130
if( !easyrtc.supportsRecording ) {
131
console.log("recording not supported by your browser");
132
return null;
133
}
134
135
var mediaRecorder = new MediaRecorder(mediaStream, {mimeType: mimeType});
136
if( !mediaRecorder ) {
137
console.log("no media recorder");
138
return;
139
}
140
mediaRecorder.start();
141
142
mediaRecorder.onerror = function(e) {
143
console.log("Media recording error:", e);
144
}
145
146
mediaRecorder.onwarning = function(e) {
147
console.log("Media recording error:", e);
148
}
149
150
mediaRecorder.onstart = function(e) {
151
console.log("Media recording started");
152
}
153
154
mediaRecorder.onstop = function(e) {
155
console.log("Media recording stopped");
156
}
157
158
return mediaRecorder;
159
};
160
161
/** This method creates a media recorder and populates it's ondataavailable
162
* method so that your own callback gets called with the data.
163
* Use the media recorder's start(), stop(), pause() and resume() methods
164
* on the returned object.
165
* @function
166
* @memberOf Easyrtc_Recorder
167
* @param {HTMLMediaStream} mediaStream a local or remote media stream.
168
* @param {Function} dataCallback a function to receive the webm data from.
169
*/
170
easyrtc.recordToCallback = function (mediaStream, dataCallback) {
171
var mediaRecorder = startRecording(mediaStream);
172
if( !mediaRecorder) {
173
return null;
174
}
175
mediaRecorder.ondataavailable = function(e) {
176
dataCallback(e.data);
177
}
178
return mediaRecorder;
179
};
180
181
/** This method creates a media recorder that builds a blob 
182
* Use the media recorder's start(), stop(), pause() and resume() methods
183
* on the returned object.
184
* @function
185
* @memberOf Easyrtc_Recorder
186
* @param  {HTMLMediaStream} mediaStream a local or remote media stream.
187
* @param {Function} blobCallback a callback function that gets called with a
188
*    blob once you invoke the stop method.
189
**/
190
easyrtc.recordToBlob = function(mediaStream, blobCallback) {
191
var chunks = [];
192
193
function dataConsumer(chunk) {
194
chunks.push(chunk);
195
}
196
197
var mediaRecorder = easyrtc.recordToCallback(mediaStream,
198
dataConsumer);
199
200
if( !mediaRecorder) {
201
return null;
202
}
203
204
mediaRecorder.onstop = function() {
205
blobCallback( new Blob(chunks, {type:"video/webm"}));
206
chunks = [];
207
}
208
return mediaRecorder;
209
};
210
211
/** This method creates a media recorder that builds a file.
212
* Use the media recorder's start(), stop(), pause() and resume() methods
213
* on the returned object.
214
* @function
215
* @memberOf Easyrtc_Recorder
216
* @param {HTMLMediaStream} a local or remote media stream.
217
* @param {Object} downloadLink an anchor tag to attach the file to.
218
* @param {String} basename the name of the file. A .webm will be appended
219
*    to the file if its not already present. The file doesn't get written
220
*    until you call the mediaRecorder's stop method.
221
**/
222
easyrtc.recordToFile = function(mediaStream, downloadLink, basename) {
223
function blobCallback( blob ) {
224
var videoURL = window.URL.createObjectURL(blob);
225
226
downloadLink.href = videoURL;
227
downloadLink.appendChild(document.createTextNode(basename));
228
229
var name = basename + ((basename.indexOf(".webm")>0)?"": ".webm") ;
230
downloadLink.setAttribute( "download", name);
231
downloadLink.setAttribute( "name", name);
232
}
233
234
downloadLink.innerHTML = "";
235
var mediaRecorder = easyrtc.recordToBlob(mediaStream, blobCallback);
236
return mediaRecorder;
237
};
238
239
return easyrtc;
240
241
}));
242