Skip to content

Commit

Permalink
Move to offscreen canvas in setDesktopwithCameraSource
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaboleken committed Nov 25, 2024
1 parent 404c515 commit d3f052e
Showing 1 changed file with 36 additions and 39 deletions.
75 changes: 36 additions & 39 deletions src/main/js/media_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,63 +354,60 @@ export class MediaManager {
*/
setDesktopwithCameraSource(stream, streamId, onEndedCallback) {
this.desktopStream = stream;
return this.navigatorUserMedia({video: true, audio: false}, cameraStream => {
this.smallVideoTrack = cameraStream.getVideoTracks()[0];

//create a canvas element
var canvas = document.createElement("canvas");
var canvasContext = canvas.getContext("2d");
return this.navigatorUserMedia({ video: true, audio: false }, (cameraStream) => {
this.smallVideoTrack = cameraStream.getVideoTracks()[0];

Check warning on line 359 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L358-L359

Added lines #L358 - L359 were not covered by tests

//create video element for screen
//var screenVideo = document.getElementById('sourceVideo');
var screenVideo = document.createElement('video');
// Create an offscreen canvas
const offscreenCanvas = new OffscreenCanvas(1, 1); // Initial dimensions will be updated dynamically
const canvasContext = offscreenCanvas.getContext("2d");

Check warning on line 363 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L362-L363

Added lines #L362 - L363 were not covered by tests

// Create video elements for screen and camera
const screenVideo = document.createElement('video');

Check warning on line 366 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L366

Added line #L366 was not covered by tests
screenVideo.srcObject = stream;
screenVideo.play();
//create video element for camera
var cameraVideo = document.createElement('video');

const cameraVideo = document.createElement('video');

Check warning on line 370 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L370

Added line #L370 was not covered by tests
cameraVideo.srcObject = cameraStream;
cameraVideo.play();
var canvasStream = canvas.captureStream(15);

// Capture stream from the offscreen canvas
const canvasStream = offscreenCanvas.captureStream(15);

Check warning on line 375 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L375

Added line #L375 was not covered by tests

if (onEndedCallback != null) {
stream.getVideoTracks()[0].onended = function (event) {
stream.getVideoTracks()[0].onended = (event) => {

Check warning on line 378 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L378

Added line #L378 was not covered by tests
onEndedCallback(event);
}
}
var promise;
if (this.localStream == null) {
promise = this.gotStream(canvasStream);
} else {
promise = this.updateVideoTrack(canvasStream, streamId, onended, null);
};
}

promise.then(() => {
const promise = this.localStream == null

Check warning on line 383 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L383

Added line #L383 was not covered by tests
? this.gotStream(canvasStream)
: this.updateVideoTrack(canvasStream, streamId, onEndedCallback, null);

//update the canvas
promise.then(() => {

Check warning on line 387 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L387

Added line #L387 was not covered by tests
// Update the offscreen canvas at a regular interval
this.desktopCameraCanvasDrawerTimer = setInterval(() => {
//draw screen to canvas
canvas.width = screenVideo.videoWidth;
canvas.height = screenVideo.videoHeight;
canvasContext.drawImage(screenVideo, 0, 0, canvas.width, canvas.height);

var cameraWidth = screenVideo.videoWidth * (this.camera_percent / 100);
var cameraHeight = (cameraVideo.videoHeight / cameraVideo.videoWidth) * cameraWidth

var positionX = (canvas.width - cameraWidth) - this.camera_margin;
var positionY;

if (this.camera_location == "top") {
positionY = this.camera_margin;
} else { //if not top, make it bottom
//draw camera on right bottom corner
positionY = (canvas.height - cameraHeight) - this.camera_margin;
}
// Set canvas size to match the screen video
offscreenCanvas.width = screenVideo.videoWidth;
offscreenCanvas.height = screenVideo.videoHeight;

Check warning on line 392 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L391-L392

Added lines #L391 - L392 were not covered by tests

// Draw screen video to the canvas
canvasContext.drawImage(screenVideo, 0, 0, offscreenCanvas.width, offscreenCanvas.height);

Check warning on line 395 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L395

Added line #L395 was not covered by tests

// Calculate camera overlay dimensions
const cameraWidth = screenVideo.videoWidth * (this.camera_percent / 100);
const cameraHeight = (cameraVideo.videoHeight / cameraVideo.videoWidth) * cameraWidth;

Check warning on line 399 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L398-L399

Added lines #L398 - L399 were not covered by tests

const positionX = offscreenCanvas.width - cameraWidth - this.camera_margin;
const positionY = this.camera_location === "top"

Check warning on line 402 in src/main/js/media_manager.js

View check run for this annotation

Codecov / codecov/patch

src/main/js/media_manager.js#L401-L402

Added lines #L401 - L402 were not covered by tests
? this.camera_margin
: offscreenCanvas.height - cameraHeight - this.camera_margin;

// Draw camera overlay on the canvas
canvasContext.drawImage(cameraVideo, positionX, positionY, cameraWidth, cameraHeight);
}, 66);
});
}, true)
}, true);
}

/**
Expand Down

0 comments on commit d3f052e

Please sign in to comment.