forked from muaz-khan/RecordRTC
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added dev/WebAssemblyRecorder.js via muaz-khan#468
- Loading branch information
Showing
13 changed files
with
306 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// All Copyrights goes to: Google Inc. | ||
// Original: https://github.com/GoogleChromeLabs/webm-wasm | ||
|
||
// _________________ | ||
// WebAssemblyRecorder.js | ||
|
||
/** | ||
* WebAssemblyRecorder lets you create webm videos in JavaScript via WebAssembly. The library consumes raw RGBA32 buffers (4 bytes per pixel) and turns them into a webm video with the given framerate and quality. This makes it compatible out-of-the-box with ImageData from a CANVAS. With realtime mode you can also use webm-wasm for streaming webm videos. | ||
* @summary Video recording feature in Chrome. | ||
* @license {@link https://github.com/muaz-khan/RecordRTC#license|MIT} | ||
* @author {@link http://www.MuazKhan.com|Muaz Khan} | ||
* @typedef WebAssemblyRecorder | ||
* @class | ||
* @example | ||
* var recorder = new WebAssemblyRecorder(mediaStream); | ||
* recorder.record(); | ||
* recorder.stop(function(blob) { | ||
* video.src = URL.createObjectURL(blob); | ||
* }); | ||
* @see {@link https://github.com/muaz-khan/RecordRTC|RecordRTC Source Code} | ||
* @param {MediaStream} mediaStream - MediaStream object fetched using getUserMedia API or generated using captureStreamUntilEnded or WebAudio API. | ||
* @param {object} config - {webAssemblyPath:'webm-wasm.wasm',workerPath: 'webm-worker.js', framerate: 30, width: 1920, height: 1080} | ||
*/ | ||
function WebAssemblyRecorder(stream, config) { | ||
// todo: remove all async/wait | ||
|
||
/* jshint ignore:start */ | ||
|
||
config = config || {}; | ||
|
||
config.width = config.width || 640; | ||
config.height = config.height || 480; | ||
config.framerate = config.framerate || 30; | ||
// config.bitrate = config.bitrate || 200; | ||
|
||
function createBufferURL(buffer, type = '') { | ||
return URL.createObjectURL(new Blob([buffer], { | ||
type | ||
})); | ||
} | ||
|
||
function cameraStream() { | ||
return new ReadableStream({ | ||
async start(controller) { | ||
const cvs = document.createElement('canvas'); | ||
const video = document.createElement('video'); | ||
video.srcObject = stream; | ||
video.play(); | ||
await nextEvent(video, 'playing'); | ||
[cvs.width, cvs.height] = [config.width, config.height]; | ||
const ctx = cvs.getContext('2d'); | ||
const frameTimeout = 1000 / config.framerate; | ||
setTimeout(async function f() { | ||
ctx.drawImage(video, 0, 0); | ||
await controller.enqueue( | ||
ctx.getImageData(0, 0, config.width, config.height) | ||
); | ||
setTimeout(f, frameTimeout); | ||
}, frameTimeout); | ||
} | ||
}); | ||
} | ||
|
||
function nextEvent(target, name) { | ||
return new Promise(function(resolve) { | ||
target.addEventListener(name, resolve, { | ||
once: true | ||
}); | ||
}); | ||
} | ||
|
||
var worker; | ||
|
||
async function startRecording(stream) { | ||
if (!config.workerPath) { | ||
// is it safe to use @latest ? | ||
const buffer = await fetch( | ||
'https://unpkg.com/webm-wasm@0.3.2/dist/webm-worker.js' | ||
).then(function(r) { | ||
return r.arrayBuffer(); | ||
}); | ||
|
||
worker = new Worker( | ||
URL.createObjectURL(new Blob([buffer], { | ||
type: 'text/javascript' | ||
})) | ||
); | ||
} else { | ||
worker = new Worker(config.workerPath); | ||
} | ||
|
||
worker.postMessage(config.webAssemblyPath || 'https://unpkg.com/webm-wasm@0.3.2/dist/webm-wasm.wasm'); | ||
|
||
await nextEvent(worker, 'message'); | ||
worker.postMessage({ | ||
width: config.width, | ||
height: config.height, | ||
realtime: true | ||
}); | ||
cameraStream().pipeTo(new WritableStream({ | ||
write(image) { | ||
worker.postMessage(image.data.buffer, [image.data.buffer]); | ||
} | ||
})); | ||
|
||
worker.onmessage = function(event) { | ||
if (!!event.data) { | ||
if (!isPaused) { | ||
arrayOfBuffers.push(event.data); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* This method records video. | ||
* @method | ||
* @memberof WebAssemblyRecorder | ||
* @example | ||
* recorder.record(); | ||
*/ | ||
this.record = function() { | ||
arrayOfBuffers = []; | ||
isPaused = false; | ||
startRecording(stream); | ||
}; | ||
|
||
var isPaused; | ||
|
||
/** | ||
* This method pauses the recording process. | ||
* @method | ||
* @memberof WebAssemblyRecorder | ||
* @example | ||
* recorder.pause(); | ||
*/ | ||
this.pause = function() { | ||
isPaused = true; | ||
}; | ||
|
||
/** | ||
* This method resumes the recording process. | ||
* @method | ||
* @memberof WebAssemblyRecorder | ||
* @example | ||
* recorder.resume(); | ||
*/ | ||
this.resume = function() { | ||
isPaused = false; | ||
}; | ||
|
||
async function terminate() { | ||
worker.postMessage(null); | ||
worker.terminate(); | ||
} | ||
|
||
var arrayOfBuffers = []; | ||
|
||
/** | ||
* This method stops recording video. | ||
* @param {function} callback - Callback function, that is used to pass recorded blob back to the callee. | ||
* @method | ||
* @memberof WebAssemblyRecorder | ||
* @example | ||
* recorder.stop(function(blob) { | ||
* video.src = URL.createObjectURL(blob); | ||
* }); | ||
*/ | ||
this.stop = function(callback) { | ||
terminate(); | ||
var blob = new Blob(arrayOfBuffers, { | ||
type: 'video/webm' | ||
}); | ||
callback(blob); | ||
}; | ||
|
||
/* jshint ignore:end */ | ||
} | ||
|
||
if (typeof RecordRTC !== 'undefined') { | ||
RecordRTC.WebAssemblyRecorder = WebAssemblyRecorder; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<!-- | ||
All Copyrights goes to: Google Inc. | ||
Original: https://github.com/GoogleChromeLabs/webm-wasm | ||
--> | ||
<title>WebAssembly Recorder using RecordRTC</title> | ||
<style type="text/css"> | ||
body { | ||
text-align: center; | ||
} | ||
|
||
video { | ||
border-radius: 5px; | ||
border: 1px solid black; | ||
} | ||
</style> | ||
|
||
<h1>WebAssembly Recorder using RecordRTC</h1> | ||
<button id="start">Start Recording</button> | ||
<button id="stop" disabled>Stop Recording</button> | ||
<br><br> | ||
<video id="video" controls autoplay playsinline></video> | ||
|
||
<script src="../dev/WebAssemblyRecorder.js"></script> | ||
|
||
<script src="https://cdn.webrtc-experiment.com/RecordRTC.js"></script> | ||
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script> | ||
|
||
<script> | ||
var recorder; | ||
|
||
document.getElementById('start').onclick = function() { | ||
this.disabled = true; | ||
navigator.mediaDevices.getUserMedia({ | ||
video: { | ||
width: { | ||
ideal: 640 | ||
}, | ||
height: { | ||
ideal: 480 | ||
} | ||
}, | ||
audio: false | ||
}).then(function(stream) { | ||
document.getElementById('video').srcObject = stream; | ||
|
||
recorder = RecordRTC(stream, { | ||
recorderType: WebAssemblyRecorder, | ||
// workerPath: '../libs/webm-worker.js', | ||
// webAssemblyPath: '../libs/webm-wasm.wasm', | ||
width: 640, | ||
height: 480, | ||
framerate: 30 | ||
}); | ||
recorder.startRecording(); | ||
|
||
document.getElementById('stop').disabled = false; | ||
}); | ||
}; | ||
|
||
document.getElementById('stop').onclick = function() { | ||
document.getElementById('video').srcObject = null; | ||
|
||
this.disabled = true; | ||
recorder.stopRecording(function(blob) { | ||
document.getElementById('video').src = URL.createObjectURL(recorder.getBlob()); | ||
document.getElementById('start').disabled = false; | ||
}); | ||
} | ||
</script> | ||
|
||
<footer style="margin-top: 20px;"><small id="send-message"></small></footer> | ||
<script src="https://cdn.webrtc-experiment.com/common.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters