I need to playback aac (adts) streaming from WebSocket, I searched the internet and found aac.js can do it but sounds like delay almost 2~5 seconds.
And I found some people use WebAudio stuff to do it. I try it with below code but decoder return error after some packets (not always but repeat error):
Uncaught (in promise) DOMException: Unable to decode audio data
My sample javascript:
var context = new AudioContext();
var nextStartTime = 0;
var audioWs = new WebSocket("ws://" + hostip + ":8084");
audioWs.binaryType = 'arraybuffer';
audioWs.onopen = function(evt) {
console.log("onOpen");
bexit = 0;
};
audioWs.onclose = function(evt) {
console.log("close");
};
audioWs.onmessage = function(evt) {
var blob = evt.data;
if ( blob.slice == undefined) {
console.log("undefined message");
return;
}
decodeAudio(blob);
};
audioWs.onerror = function(evt) {
console.log("error");
};
var decodeAudio = function(blob) {
context.decodeAudioData(
blob,
function(buffer) {
if (!buffer) {
console.log('error decoding file data: ' + url);
return;
}
console.log("playback buffer");
playBuffer(buffer);
},
function(error) {
console.error("decodeAudioData error", error);
});
};
var playBuffer = function(buffer) {
var source = context.createBufferSource();
source.buffer = buffer;
if(nextStartTime == 0){
nextStartTime = context.currentTime + buffer.duration/2;
}
source.connect(context.destination);
source.start(nextStartTime);
nextStartTime += buffer.duration;
};
I not sure if it's caused by data received from websocket incomming too fast then cause decoder error! How should I fix it?
Or maybe it's not good way to do this task with WebAudio API?
WebAudio's decodeAudioData can't decode partial blobs. It needs the whole file, in general, because in many encoded formats, the necessary information (sample rates, channels, sample size, etc.) is only at the beginning.
Related
I am using Laravel, and trying add browser to browser audio calling. I am using Vonage (Tookbox) API for this, but I am getting some error.
here is my code:
async function audioCall() {
var publisher;
var targetElement = 'publisher';
var pubOptions = {publishAudio:true, publishVideo:false};
publisher = OT.initPublisher(targetElement, pubOptions, function(error) {
if (error) {
alert("The client cannot publish.");
} else {
console.log('Publisher initialized.');
}
});
// Setting an audio source to a new MediaStreamTrack
const stream = await OT.getUserMedia({
videoSource: null
});
const [audioSource] = stream.getAudioTracks();
publisher.setAudioSource(audioSource).then(() => console.log('Audio source updated'));
// Cycling through microphone inputs
let audioInputs;
let currentIndex = 0;
OT.getDevices((err, devices) => {
audioInputs = devices.filter((device) => device.kind === 'audioInput');
// Find the right starting index for cycleMicrophone
audioInputs.forEach((device, idx) => {
if (device.label === publisher.getAudioSource().label) {
currentIndex = idx;
}
});
});
const cycleMicrophone = () => {
currentIndex += 1;
let deviceId = audioInputs[currentIndex % audioInputs.length].deviceId;
publisher.setAudioSource(deviceId);
};
}
This code return an error on console:
Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
I believe the issue is that you have
device.kind === 'audioInput'
and I'm pretty sure device.kind comes out like 'audioinput' (all lowercase).
examples:
https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo/kind
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices#examples
That would make audioInputs empty (try to console.log it to verify) and gives you the error because there is no device.
Try:
device.kind.toLowerCase() === 'audioinput'
Hope it works out.
I'm trying to embed a PDF on a web page. The native PDF viewer in Chrome has a size limit of around 2mb, so I'm trying to use Adobe's API (https://www.adobe.io/apis/documentcloud/dcsdk/pdf-embed.html)
Because of the way that a CMS that I'm using stored files, I have a Base64 string to work with.
Using code from https://gist.github.com/kiritodeveloper/06186343e41e21a94411b3987e84c5de, I was able to pass my base64 string to Adobe's API and embed the PDF about 60% of the time in Chrome, but now it doesn't work in Firefox.
I'm using ajax to get to my CMS's API to retrieve the data, but I think that the adobe listener is running before the data is returned; when it does work, it's just chance. How can I get the order right so that this will work? Can I use an asynchronous call to get this data?
const clientID = "ADOBE API KEY HERE";
var queryPath = "$/PDFBase64"
jQuery( document).ready(function() {
var pdfData = '';
getPDF();
function base64ToArrayBuffer(base64) {
var bin = window.atob(base64);
var len = bin.length;
var uInt8Array = new Uint8Array(len);
for (var i = 0; i < len; i++) {
uInt8Array[i] = bin.charCodeAt(i);
}
return uInt8Array.buffer;
}
document.addEventListener("adobe_dc_view_sdk.ready", function () {
var adobeDCView = new AdobeDC.View({
clientId: clientID,
divId: "adobe-dc-view"
});
adobeDCView.previewFile(
{
// convert the Base64 encoded PDF and convert it to an ArrayBuffer
// and pass it as a Promise
content: { promise: Promise.resolve(base64ToArrayBuffer(pdfData)) },
// The filename name to display in the View SDK UI
// and also used as the filename of the PDF when downloaded
metaData: { fileName: "Download.pdf" }
}, {})
});
function getPDF() {
jQuery.ajax("/api/Query?queryname=" + queryPath ,
{
type : "get",
"async":false,
contentType: "application/json",
headers: {"RequestVerificationToken":
document.getElementById("__RequestVerificationToken").value},
success: function(data){
iqa = data.Items.$values;
if(iqa.length>0){
for (var i = 0; i < iqa.length; i++) {
filt = iqa[i].Properties.$values;
pdfData = filt.filter(x => x.Name == "PDF")[0].Value;
};
}
return pdfData;
}, error : function(xhr, textStatus, errorThrown ) {
if (textStatus == 'timeout') {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
//try again
$.ajax(this);
return;
}
return;
}
if (xhr.status == 500) {
console.log(errorThrown);
} else {
//handle error
}
}
});
};
});
This is the code that I have in the afterEach block.
afterEach(function() {
jasmine.getEnv().addReporter(new function () {
this.specDone = function (result) {
console.log("result is :"+result);
if (result.failedExpectations.length > 0) {
browser.takeScreenshot().then(function(png) {
console.log("png is :"+png);
console.dir(png);
var stream = fs.createWriteStream(specName.description+".png");
var stream2 = fs.createWriteStream(specName.description+"2.png");
stream.write(new Buffer(png,'utf8', function (err)
{
if (err) throw err
console.log('File saved.')
}));
stream2.write(new Buffer(png,'base64'));
stream.end();
stream2.end();
});
}
};
});
But none of the photo viewers is able to open the files. Any insights as to what is going wrong is highly appreciated.
System is windows 10 and browser Firefox 46.*
The console.log of the png returned by the browser.takeScreenshot() is as below:
png is :{"name":"screenshot","sessionId":"c18f3e08-9da0-4c71-9a10-391bab714e7a","status":0,"value":"iVBORw0KGgoAAAANSUhEUgAABpAAAAOsCAYAAABNo/WwAAAgAElEQVR4nOzd53fc1rnvcf15996c4xOnJ44TV9lyk6wuqzerd6tSEklR1XK3cxzHLbYTx0WdvZMSSYmdMwNgGjC/+wKDGZS9gQ1gY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HSBNP5CkoXoA0BTU3UY51hZ3bGjieTUErTEIrTOkJQHKJDx6FBSQeMBR9ApDmR/MfkFSNX3H/twhASlbxA099A5LAIwFI87n5AkjiSdbjA5CqeCQRJo/020fGP5fX193zB0hSIEBabQKkNWyA1OkCSJ3VtXWSCY/YAYkVj8ICkl88Shog+VtdxwWQZMr0kd+powoe8QIkdzzSfE4a8cQjjXHyqPr9fACJhEiJACQqIvnAo9xDK97UApCIGOQTkJjxiBGQaj515P+mETseCUAKevfIAkhMfyq+xoAkD5PfLy3gwwpJbIDkAB4bLJVM72ksUOT4munnV+X7XADJWDun5uhARIcjAh4VHqGU5wFHVkAKC0Oq8giq8igyQAqC
I am not sure why takeShotshot() method returns the entire JSON object instead of just the base64 value for the screenshot. In your case, you can check whether the object returned by takeScreenShot methods contains any key with name value. If value is is present, then use it to get base64 value of the screenshot.
browser.takeScreenshot().then(function(png) {
var base64Value = png.hasOwnProperty("value") ? png.value : png;
var stream = fs.createWriteStream(specName.description+".png");
var stream2 = fs.createWriteStream(specName.description+"2.png");
stream.write(new Buffer(png,'utf8', function (err)
{
if (err) throw err
console.log('File saved.')
}));
stream2.write(new Buffer(base64Value,'base64'));
stream.end();
stream2.end();
})
I've been scratching my head over this for a while. What am I doing wrong? Your help is much appreciated :)
I've tried many different image codes, but I think it's a promise issue I'm seeing. With the code below I only see the "Start of loop" log message.
If I move the results push outside the promise structure to underneath then I see the Stage log messages, albeit after all the Start of loops have printed (hence why I put the push in the then function).
Parse.Cloud.job("fetchjson", function(request, status) {
var url = 'some json url';
Parse.Cloud.httpRequest({url: url}).then(function(httpResponse){
//var Image = require("parse-image");
var Seeds = Parse.Object.extend("Seeds");
var jsonobj = JSON.parse(httpResponse.text);
var results = [];
// do NOT iterate arrays with `for... in loops`
for(var i = 0; i < jsonobj.seeds.length; i++){
var seed = new Seed();
var a = new Seed(jsonobj.seeds[i]);
console.log("Start of loop");
Parse.Cloud.httpRequest({url: a.get("image") }).then(function(response) {
console.log("Stage 1");
//var file = new Parse.File('thumb.jpg', { base64: response.buffer.toString('base64', 0, response.buffer.length) });
//return file.save();
return "hi"
}).then(function(thumb) {
console.log("Stage 2");
//a.set("imageFile", thumb);
//a.set("viewsInt", parseInt(a.get("views")));
}, function(error) {
console.log("Error occurred :(");
}).then(function(){
results.push(seed.save(a)); // add to aggregate
});
}
// .when waits for all promises
Parse.Promise.when(results).then(function(data){
status.success("All saved");
});
}, function(error) {
console.error('Request failed with response code ' + httpResponse.status);
status.error("Failed");
});
});
anyone got an idea how to embed a webgl animation into powerpoint. any tools that can be used on server side to capture an animated gif?
I did not make it work to embed webgl html directly in a powerpoint.
You can create images of webgl by calling toDataURL() as in
var canvas = document.createElement("canvas");
var gl = canvas.getContext("experimental-webgl");
function render() {
gl.clearColor(Math.random(), Math.random(), Math.random(), 1);
gl.clear(gl.COLOR_BUFFER_BIT);
// takes a 'screenshot' of the canvas.
var image = canvas.toDataURL();
requestAnimationFrame(render);
}
render();
To make an animation you could send each of those screenshots to a server
...
var image = canvas.toDataURL();
var req = new XMLHTTPRequest();
req.open("POST", "http://localhost:8080", true);
var data = {
cmd: 'screenshot',
dataURL: image,
};
req.setRequestHeader("Content-type", "application/json");
req.send(JSON.stringify(data));
Here's a node.js server that will save the screenshots as .png files. You could then load them into some program to turn them into a gif.
var port = 8080
var screenshotCount = 0;
var http = require('http'),
url = require('url'),
fs = require('fs'),
util = require('util'),
path = require('path'),
querystring = require('querystring');
function postHandler(request, callback) {
var query_ = { };
var content_ = '';
request.addListener('data', function(chunk) {
content_ += chunk;
});
request.addListener('end', function() {
query_ = JSON.parse(content_);
callback(query_);
});
}
function sendJSONResponse(res, object) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify(object), 'utf8');
res.end();
}
function startsWith(str, start) {
return (str.length >= start.length &&
str.substr(0, start.length) == start);
}
function saveScreenshotFromDataURL(dataURL) {
var EXPECTED_HEADER = "data:image/png;base64,";
if (startsWith(dataURL, EXPECTED_HEADER)) {
var filename = "screenshot-" + (screenshotCount++) + ".png";
fs.writeFile(
filename,
dataURL.substr(
EXPECTED_HEADER.length,
dataURL.length - EXPECTED_HEADER.length),
'base64');
util.print("Saved Screenshot: " + filename + "\n");
}
}
server = http.createServer(function(req, res) {
// your normal server code
if (req.method == "POST") {
postHandler(req, function(query) {
switch (query.cmd) {
case 'screenshot':
saveScreenshotFromDataURL(query.dataURL);
sendJSONResponse(res, { ok: true });
break;
default:
util.print("err: unknown post: " + query + "\n");
break;
}
});
}
}),
server.listen(port);
Note that server only saves screenshots, it doesn't serve files (for brevity). So you'll need to either add that functionality or serve the files from another server.