MediaStreamRecorder - Recording Audio Issue - html

I've just rummaged through and put together an audio-video recorder that will record audio and video streams separately and upload them to my server where they'll get joined.
BUT, my implementation has the audio dropping off after a few seconds mostly 7 seconds and 14 seconds.
I'm using RecordRTC javascript library and here's the link: https://www.webrtc-experiment.com/RecordRTC.js
And here's the code:
var record = document.getElementById('replyfallback_record');
var stop = document.getElementById('replyfallback_cancel');
var audio = document.querySelector('audio');
var recordVideo = document.getElementById('record-video');
var preview = document.getElementById('replyfallback_video');
var recordAudio, recordVideo, progress;
$('#replyfallback_record').click(function(){
switch($('#replyfallback_record').text()){
case "Record":
//setup some variables
var video_constraints = {
mandatory: { },
optional: []
};
//trigger navigator.getUserMedia
navigator.getUserMedia({
audio: true,
video: true
}, function(stream) {
preview.src = window.URL.createObjectURL(stream);
preview.play();
// var legalBufferValues = [256, 512, 1024, 2048, 4096, 8192, 16384];
// sample-rates in at least the range 22050 to 96000.
recordAudio = RecordRTC(stream, {
type: 'audio',
bufferSize: 16384,
sampleRate: 45000
});
/*recordVideo = RecordRTC(stream, {
type: 'video'
});*/
recordAudio.startRecording();
//recordVideo.startRecording();
$('#replyfallback_record').text("Stop & Submit");
});
break;
case "Stop & Submit":
$('#replyfallback_record').attr('disable','disable');
fileName = uid();
recordAudio.stopRecording(function(url){
window.open(url);
});
PostBlob(recordAudio.getBlob(), 'HTML5UploadAudio', fileName);
//recordVideo.stopRecording();
//PostBlob(recordVideo.getBlob(), 'HTML5UploadVideo', fileName);
preview.src = '';
$('#replyfallback_record').text("submitting...");
break;
}
});
//basic ajax request object function
function xhr(url, data, progress, callback) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
callback(request.responseText);
}
};
request.onprogress = function(e) {
if(!progress) return;
if (e.lengthComputable) {
progress = (e.loaded / e.total) * 100;
}
$('#replyfallback_record').text("submitting..."+progress);
if(progress == 100){
progress = 0;
}
};
request.open('POST', url);
request.send(data);
}
function PostBlob(blob, fileType, fileName) {
// FormData
var formData = new FormData();
formData.append('filename', fileName);
formData.append('blob', blob);
formData.append("function",fileType);
if(fileType=="HTML5UploadVideo"){
formData.append("CN_UL_title",$('#replyfallback_title').val());
formData.append("CN_UL_description",$('#replyfallback_desc').val());
formData.append("CN_UL_category","1");
}
// POST the Blob
xhr(SITE.api, formData, progress, function(data) {
$('#replyfallback_record').text("Record");
alert(data+" | "+getReadableFileSizeString(recordAudio.getBlob().size));
});
}

It is a little late reply, but may be help future visitor.
Please try PostBlob(recordAudio.getBlob(), 'HTML5UploadAudio', fileName); inside stopRecording callback function.
recordAudio.stopRecording(function(url){
PostBlob(recordAudio.getBlob(), 'HTML5UploadAudio', fileName);
window.open(url);
});

Related

"Debugging connection was closed: Render process was gone", when trying to download a 7gb from cdn

We are trying to download a 7 GB from CDN using JSZip.js. The chrome browser suddenly seems to close the connection when the download reaches 3.5gb every time. The approximate time is around 15 mins. Is there a way we increase the tolerant time to 1 hr say?
$("#downloadJSZip").on('click', function () {
var result = [{ "cdn": "url....", "filename": "7.84 gb.zip", "size": 4194304, "path": "7.84 gb" }];
var Promise = window.Promise;
if (!Promise) {
Promise = JSZip.external.Promise;
}
function urlToPromise(url) {
return new Promise(function(resolve, reject) {
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
}
var fileNameArray = [];
function changeFileName(fileName,j){
var i = fileName.lastIndexOf('.');
var newfilename = fileName.slice(0,i)+"--"+j+fileName.slice(i);
if(fileNameArray.indexOf(newfilename) != -1){
j = j+1;
changeFileName(fileName,j);
}
return newfilename;
}
var zip = new JSZip();
// find every checked item
result.forEach(function(file){
var filename = file.filename;
if(fileNameArray.indexOf(filename) != -1){
var newfilename = changeFileName(filename,1);
filename = newfilename;
}
fileNameArray.push(filename);
var url = file.cdn;
var folder = (file.path);
zip.folder(folder).file(filename, urlToPromise(url), {binary:true});
// zip.file(filename, urlToPromise(url), {binary:true});
});
// when everything has been downloaded, we can trigger the dl
zip.generateAsync({type:"blob",
}, function updateCallback(metadata) {
var msg = "progression : " + metadata.percent.toFixed(2) + " %";
if(metadata.currentFile) {
msg += ", current file = " + metadata.currentFile;
}
console.log(msg);
console.log(metadata.percent|0);
})
.then(function callback(blob) {
// see FileSaver.js
//console.log("blob=====>");
//console.dir(blob);
//saveAs(blob, "example.zip") ;
saveAs(blob, $scope.folderName+".zip") ;
//console.log("done !");
}, function (e) {
});
});
Is this chrome browser configuration?

MediaSourceExtension fMP4 streaming playback fail

Following this link, I just change the webm format to fMP4 as below. But it doesn't work. The original test.webm file works fine.
for webm, the config as below:
//source: http://html5-demos.appspot.com/static/test.webm
var FILE = "test.webm"
var codec = 'video/webm; codecs="vorbis,vp8"';
for fMP4, change the config as below:
//source http://yt-dash-mse-test.commondatastorage.googleapis.com/media/car-20120827-85.mp4
var FILE = "car-20120827-85.mp4";
var codec = 'video/mp4; codecs="mp4a,avc"';
//var codec = 'video/mp4; codecs="mp4a.40.2,avc1.640028"';
I guess the mime for fMP4 maybe wrong but I can not figure out it after many search from internet.
<!DOCTYPE html>
<html>
<body>
<section>
<video controls autoplay width="320" height="240"></video>
<pre id="log"></pre>
</section>
<script>
//source: http://html5-demos.appspot.com/static/test.webm
//var FILE = "test.webm"
//var codec = 'video/webm; codecs="vorbis,vp8"';
//source http://yt-dash-mse-test.commondatastorage.googleapis.com/media/car-20120827-85.mp4
var FILE = "car-20120827-85.mp4";
var codec = 'video/mp4; codecs="mp4a,avc"';
//var codec = 'video/mp4; codecs="mp4a.40.2,avc1.640028"';
var NUM_CHUNKS = 5;
var video = document.querySelector('video');
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
if (!!!window.MediaSource) {
alert('MediaSource API is not available');
}
var mediaSource = new MediaSource();
video.src = window.URL.createObjectURL(mediaSource);
function callback(e) {
var sourceBuffer = mediaSource.addSourceBuffer(codec);
logger.log('mediaSource readyState: ' + this.readyState);
GET(FILE, function(uInt8Array) {
var file = new Blob([uInt8Array], {type: 'video/webm'});
var chunkSize = Math.ceil(file.size / NUM_CHUNKS);
logger.log('num chunks:' + NUM_CHUNKS);
logger.log('chunkSize:' + chunkSize + ', totalSize:' + file.size);
// Slice the video into NUM_CHUNKS and append each to the media element.
var i = 0;
(function readChunk_(i) {
var reader = new FileReader();
// Reads aren't guaranteed to finish in the same order they're started in,
// so we need to read + append the next chunk after the previous reader
// is done (onload is fired).
reader.onload = function(e) {
sourceBuffer.appendBuffer(new Uint8Array(e.target.result));
logger.log('appending chunk:' + i);
if (i == NUM_CHUNKS - 1) {
mediaSource.endOfStream();
} else {
if (video.paused) {
video.play(); // Start playing after 1st chunk is appended.
}
readChunk_(++i);
}
};
var startByte = chunkSize * i;
var chunk = file.slice(startByte, startByte + chunkSize);
reader.readAsArrayBuffer(chunk);
})(i); // Start the recursive call by self calling.
});
}
mediaSource.addEventListener('sourceopen', callback, false);
mediaSource.addEventListener('webkitsourceopen', callback, false);
mediaSource.addEventListener('webkitsourceended', function(e) {
logger.log('mediaSource readyState: ' + this.readyState);
}, false);
function GET(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.send();
xhr.onload = function(e) {
if (xhr.status != 200) {
alert("Unexpected status code " + xhr.status + " for " + url);
return false;
}
callback(new Uint8Array(xhr.response));
};
}
</script>
<script>
function Logger(id) {
this.el = document.getElementById('log');
}
Logger.prototype.log = function(msg) {
var fragment = document.createDocumentFragment();
fragment.appendChild(document.createTextNode(msg));
fragment.appendChild(document.createElement('br'));
this.el.appendChild(fragment);
};
var logger = new Logger('log');
</script>
</body>
</html>
The problem is that fMP4 is a different format than webm. While you can split a webm file in chunks defined arbitrarily, you can't do so with fMP4.
The fMP4 file is just a bunch of "boxes" which are basically structures with a type, length and content (which can contain other boxes, making it hierarchical). You must parse the file and give the sourceBuffer a moov box first (an initialization segment), and then a sequence of moof mdat boxes that can be decoded and played out.
You probably have to read a bit on the fMP4 format (aka ISO BMFF).

nodejs: parsing chunks of json

I created a test server that sends chunks of stringified JSON. When I connect to the server it sends invalid JSON and for the life of me I can't figure out why. The output adds an extra double quotation mark.
Server code:
const net = require('net'),
server = net.createServer(function(connection) {
console.log('subscriber connected.');
// send first chunk immediately
connection.write('{"type":"changed","file":"targ"');
let timer = setTimeout(function() {
connection.write('et.txt","timestamp":1358175758495}' + '\n');
connection.end();
}, 1000);
connection.on('end', function() {
clearTimeout(timer);
console.log('subscriber disconnected');
});
});
server.listen(5432, function() {
console.log('test server listening for subs...')
});
ldj.js
'use strict';
const
events = require('events'),
util = require('util'),
// client constructor
LDJClient = function(stream) {
events.EventEmitter.call(this);
let self = this;
let buffer = '';
stream.on('data', function(data) {
buffer += data;
console.log(buffer)
let boundary = buffer.indexOf('\n');
while(boundary !== -1) {
let input = buffer.substr(0, boundary);
buffer = buffer.substr(boundary + 1);
//self.emit('message', JSON.parse(input));
boundary = buffer.indexOf('\n');
}
});
};
util.inherits(LDJClient, events.EventEmitter);
// expose module methods
exports.LDJClient = LDJClient;
exports.connect = function(stream) {
return new LDJClient(stream);
};
Output:
{"type":"changed","file":"targ"
{"type":"changed","file":"targ"et.txt","timestamp":1358175758495}
That extra " should not be in "target.txt" value. Any ideas?
TIA
rathern than splitting string manualy try to get whole string and split it to chunks and then send it:
var data = '{"type":"changed","file":"target.txt","timestamp":1358175758495}';
var chunks = data.match(/.{1,10}/g); // 10 symbol chunks
for(var i = 0; i < chunks.length; i++) {
var chunk = chunks[i];
setTimeout(function() {
if(connection) {
connection.write(chunk+'\n');
if(i + 1 == chunks.length) {
connection.end();
}
}
}, i*1000);
}
connection.on('end', function() {
console.log('subscriber disconnected');
});

broken image after writing Base64 image using FileWriter

I want to add some text over image taken from device camera using cordova camera plugin.
For that i used canvas and draw text over the image and saved using FileWriter.writer() method, but while i checked image in gallery, image is shown as broken and, in proprties resolution is -1x-1.
while i debug, before calling write() i am able see base64 image and when i clicked,image is opened in new tab.
Please find my code and please provide your comments.
var gImageURI = '';
var gFileSystem = {};
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, errorHandler);
function getPhoto(source, type) {
navigator.camera.getPicture(function (imageURI) { onPhotoURISuccess(imageURI, type) }, onFail, {
quality: 35,
destinationType: navigator.camera.DestinationType.FILE_URI,
saveToPhotoAlbum: false,
sourceType: source,
allowEdit: false,
targetWidth: 600,
targetHeight: 800
});
}
// Called when a photo is successfully retrieved
function onPhotoURISuccess(imageURI, type) {
if(type=="camera")
canvasimage(imageURI);
}
// Called if something bad happens.
function onFail(message) {
console.log('Failed because: ' + message);
}
// sets the filesystem to the global var gFileSystem
function gotFS(fileSystem) {
gFileSystem = fileSystem;
}
// send the full URI of the moved image to the updateImageSrc() method which does some DOM manipulation
function movedImageSuccess(fileEntry, type) {
debugger;
updateImageSrc(fileEntry.fullPath, type);
}
// simple error handler
function errorHandler(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = e.code;
break;
};
console.log('Error: ' + msg);
}
function btnCameraClick() {
$("#divAttachments").show();
$("#divLandmarks").hide();
getPhoto(navigator.camera.PictureSourceType.CAMERA, 'camera');
}
function updateImageSrc(filepath, type) {
try {
var filenamewithextensn = filepath.substring(filepath.lastIndexOf('/') + 1);
var strfilename = filenamewithextensn.split('.');
var filename = strfilename[0];
var tag = filepath.substring(filepath.lastIndexOf('/') + 1);
// alert('File Path after moving : ' + filepath);
// alert('tag :' + tag);
var fullpath = gFileSystem.root.toURL() + tag;
// alert('full path to dataabase '+fullpath);
var query = "";
if (type == "camera") {
//query= insert query
}
}
catch (err) {
ErrorMessageDB("something");
}
}
function canvasimage(src) {
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
var imgdata = new Image;
imgdata.onload = function () {
canvas.width = this.width;
canvas.height = this.height;
ctx.drawImage(this, 0, 0);
ctx.font = "11pt Verdana";
ctx.fillStyle = "black";
ctx.fillText("19-11-2014", 22, 42);
ctx.fillStyle = "white";
ctx.fillText("19-11-2014", 20, 40);
var dataURL = canvas.toDataURL();
//alert(dataURL);
//dataURL=dataURL.substring(dataURL.indexOf("base64\,") + 7);
gotfilesystem(dataURL);
}
imgdata.src = src;
//var index = dataURL.indexOf(',');
//return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
// return dataURL;
}
function gotfilesystem(dataURL) {
var d = new Date();
var s = d.getDate().toString() + d.getMonth().toString() + d.getMinutes().toString() + d.getSeconds().toString()
var fname="thumbnail_" + s + ".png";
gFileSystem.root.getFile(fname, { create: true, exclusive: false },
function(entry) {
gotfileentrysuccess(entry, dataURL);
}, function() {
});
}
function gotfileentrysuccess(entry, dataURL) {
entry.createWriter( function(fileWriter) {gotFileWriter(fileWriter,dataURL,entry)});
}
function gotFileWriter(writer, dataURL,entry) {
writer.onwriteend = function(evt) {
movedImageSuccess(entry, 'camera');
};
writer.write(dataURL);
}
You need to create a Blob using your base64 string then pass the Blob to the FileWriter.writer() method.
There is a nice example of how to do this here:
Convert Data URI to File then append to FormData

How to save a image to HTML5 filesystem with the url of image

I am trying to use HTML5 system to store images of my website, and I find there are many example to show how to store a local image to your chrome file system but I can't find the way to get a image by web url and then store it in HTML5 file system.
This is my code, but it's wrong.
lib.ajax.get(file , function(xhr, data){
if(xhr.status == 200){
fs.root.getFile("test.jpg", {create: true}, function(fileEntry) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
console.log('Write completed.');
};
fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString());
};
// Create a new Blob and write it to log.txt.
var bb = new BlobBuilder(); // Note: window.WebKitBlobBuilder in Chrome 12.
bb.append(data);
fileWriter.write(bb.getBlob('image/jpeg'));
callback && callback("test.jpg");
}, errorHandler);
}, errorHandler);
}
});
The problem is that browser will parse xhr response data as UTF-8,
So the point is to override MimeType:
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var xhr = new XMLHttpRequest();
var photoUrl = 'http://localhost:3000/image.jpg';
xhr.open('GET', photoUrl, true);
// This stops the browser from parsing the data as UTF-8:
xhr.overrideMimeType('text/plain; charset=x-user-defined');
function stringToBinary(response) {
var byteArray = new Uint8Array(response.length);
for (var i = 0; i < response.length; i++) {
byteArray[i] = response.charCodeAt(i) & 0xff;
}
return byteArray
}
function onInitFs(fs) {
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
fs.root.getFile('image.jpg', {'create': true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(event) {
$('body').append('<img src="' + fileEntry.toURL() + '"/>');
}
buffer = stringToBinary(xhr.response);
var blob = new Blob([ buffer ], { type: 'image/jpeg' } )
fileWriter.write(blob);
}, errorHandler );
});
}
}
xhr.send();
}
var errorHandler = function(err) {
console.log(err);
}
$(function() {
webkitStorageInfo.requestQuota(PERSISTENT, 5*1024*1024, function(grantedBytes) {
requestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler)
}, errorHandler)
})
Here the function I use.
It use Blob constructor so it works on latest Chrome (thats lacks deprecated BlobBuilder) and works also on old iOS 6 that lacks 'blob' for xhr.responseType.
In comments you also see code for the deprecated BlobBuilder.
Notice: you are using XHR so CORS must be enabled!
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.PERSISTENT, 2*1024*1024, onFileSystemSuccess, fail);
function onFileSystemSuccess(fileSystem) {
fs = fileSystem;
console.log('File system initialized');
saveAsset('http://www.example-site-with-cors.com/test.png');
}
function saveAsset(url, callback, failCallback) {
var filename = url.substring(url.lastIndexOf('/')+1);
// Set callback when not defined
if (!callback) {
callback = function(cached_url) {
console.log('download ok: ' + cached_url);
};
}
if (!failCallback) {
failCallback = function() {
console.log('download failed');
};
}
// Set lookupTable if not defined
if (!window.lookupTable)
window.lookupTable = {};
// BlobBuilder shim
// var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
// xhr.responseType = 'blob';
xhr.responseType = 'arraybuffer';
xhr.addEventListener('load', function() {
fs.root.getFile(filename, {create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.onwrite = function(e) {
// Save this file in the path to URL lookup table.
lookupTable[filename] = fileEntry.toURL();
callback(fileEntry.toURL());
};
writer.onerror = failCallback;
// var bb = new BlobBuilder();
var blob = new Blob([xhr.response], {type: ''});
// bb.append(xhr.response);
writer.write(blob);
// writer.write(bb.getBlob());
}, failCallback);
}, failCallback);
});
xhr.addEventListener('error', failCallback);
xhr.send();
return filename;
}
function fail(evt) {
console.log(evt.target.error.code);
}
On a modern browser supporting XMLHttpRequest Level 2 the method documented in this answer should work.
The relevant standard is explained in this blog post
The trick is to use xhr.responseType = 'blob'
var fs = .... // your fileSystem
function download(fs,url,file,win,fail) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = "blob";
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if(xhr.status === 200){
fs.root.getFile(file,{create:true},function(fileEntry){
fileEntry.createWriter(function(writer){
writer.onwriteend = win;
writer.onerror = fail;
writer.write(xhr.response);
})
},fail)
} else {
fail(xhr.status);
}
}
};
xhr.send();
return xhr;
};
Based on cordova-promise-fs (disclosure: I'm the author)
I find a way to do this.
use canvans.toDataURL to transfer data format.
var img = new Image();
var cvs = document.createElement('canvas');
var ctx = cvs.getContext("2d");
img.src = file;
img.onload = function(){
cvs.width = img.width;
cvs.height = img.height;
ctx.drawImage(img, 0, 0);
var imd = cvs.toDataURL(contentType[extname]);
var ui8a = convertDataURIToBinary(imd);
var bb = new BlobBuilder();
bb.append(ui8a.buffer);
fs.root.getFile(path, {create: true}, function(fileEntry) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
console.log('Write completed.');
callback && callback("test.jpg");
};
fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString());
};
fileWriter.write(bb.getBlob(contentType[extname]));
});
});
};
function convertDataURIToBinary(dataURI) {
var BASE64_MARKER = ';base64,';
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for (i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
I get help from here jsfiddle