xxxxxxxxxx
104
// This advanced example combines a video drawn to a canvas with a audio track to send to a peer.
let myVideo;
let myAudio;
let myCanvas;
let otherVideo;
let oldPixels = [];
let xLoc = 0;
let yLoc = 0;
function setup() {
myCanvas = createCanvas(320, 240);
console.log(myCanvas);
oldPixels = [width * height * 4];
showImage = createImage(width,height);
// Requesting audio and video from createCapture
let constraints = {
video: {
mandatory: {
minWidth: width,
minHeight: height
},
optional: [{
maxFrameRate: 30
}]
},
audio: true
};
myVideo = createCapture(constraints, function(stream) {
// Need to use the callback to get at the audio/video stream
// Get a stream from the canvas
let canvasStream = myCanvas.elt.captureStream(15);
// Get the audio tracks from the a/v stream
let audioTracks = stream.getAudioTracks();
// Use the first audio track
if (audioTracks.length > 0) {
// Add it to the canvas stream
canvasStream.addTrack(audioTracks[0]);
}
myVideo.size(width,height);
// Give the canvas stream to SimpleSimplePeer as a "CAPTURE" stream
let ssp = new SimpleSimplePeer(this, "CAPTURE", canvasStream, "SimpleSimplePeerAdvancedTest");
ssp.on('stream', gotStream);
});
myVideo.elt.muted = true;
myVideo.hide();
}
function draw() {
//background(255);
//image(myVideo, 0, 0, width, height);
if (mouseIsPressed) {
xLoc = mouseX;
yLoc = mouseY;
}
ellipse(xLoc, yLoc, 50, 50);
// Do the threshold 1 time in setup
showImage.loadPixels();
myVideo.loadPixels();
for (let i = 0; i < myVideo.pixels.length; i += 4) {
let r = myVideo.pixels[i];
let g = myVideo.pixels[i + 1];
let b = myVideo.pixels[i + 2];
let oldr = oldPixels[i];
let oldg = oldPixels[i + 1];
let oldb = oldPixels[i + 2];
if (dist(r, g, b, oldr, oldg, oldb) > 30) {
showImage.pixels[i] = r;
showImage.pixels[i + 1] = g;
showImage.pixels[i + 2] = b;
showImage.pixels[i + 3] = 255;
} else {
showImage.pixels[i] = 127;
showImage.pixels[i + 1] = 127;
showImage.pixels[i + 2] = 127;
showImage.pixels[i + 3] = 30;
}
oldPixels[i] = myVideo.pixels[i];
oldPixels[i + 1] = myVideo.pixels[i + 1];
oldPixels[i + 2] = myVideo.pixels[i + 2];
}
showImage.updatePixels();
image(showImage,0,0);
}
// We got a new stream!
function gotStream(stream) {
// This is just like a video/stream from createCapture(VIDEO)
otherVideo = stream;
//otherVideo.id is the unique identifier for this peer
//otherVideo.hide();
}