Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo for Issue With Canvas and Archive #368

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Archiving/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ body, html {
#archiveLink > a {
padding: 5px;
background-color: white;
}

video {
background: black;
}
2 changes: 1 addition & 1 deletion Archiving/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<title> OpenTok Archiving Sample </title>
<link href="css/app.css" rel="stylesheet" type="text/css">
<script src="https://static.opentok.com/v2/js/opentok.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@opentok/client@2.24.7/dist/js/opentok.min.js"></script>
</head>

<body>
Expand Down
74 changes: 60 additions & 14 deletions Archiving/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,67 @@ function initializeSession() {
console.log('You were disconnected from the session.', event.reason);
});

// initialize the publisher
const publisherOptions = {
insertMode: 'append',
width: '100%',
height: '100%'
const randomColour = () => {
return Math.round(Math.random() * 255);
};
const publisher = OT.initPublisher('publisher', publisherOptions, handleError);

// Connect to the session
session.connect(token, (error) => {
if (error) {
handleError(error);
} else {
// If the connection is successful, publish the publisher to the session
session.publish(publisher, handleError);
}
// Canvas
navigator.mediaDevices.getUserMedia({
audio: true,
video: { height: 480, width: 640}
}).then((stream) => {
const canvas = document.createElement('canvas');
canvas.width = 640;
canvas.height = 480;
const ctx = canvas.getContext('2d');

const video = document.createElement('video');
video.height = 480;
video.width = 640;
video.autoplay = true;
video.setAttribute('playsinline', 'true');
video.srcObject = stream;
document.querySelector('#videos').appendChild(video);

// Draw a random colour in the Canvas every 3 seconds
const interval = setInterval(() => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = `rgb(${randomColour()}, ${randomColour()}, ${randomColour()})`;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(video, 0, 0, video.width, video.height, 0, 0, canvas.width, canvas.height)
}, 33);

// initialize the publisher
const publisherOptions = {
insertMode: 'append',
width: '100%',
height: '100%',
scalableVideo: false,
videoSource: canvas.captureStream(30).getVideoTracks()[0], // Use canvas.captureStream at 3 fps and pass the video track to the Publisher
audioSource: stream.getAudioTracks()[0]
};

const publisher = OT.initPublisher('publisher', publisherOptions, (error) => {
if (error) {
clearInterval(interval);
handleError(error);
alert(error.message);
}
});

publisher.on('destroyed', () => {
clearInterval(interval);
});

// Connect to the session
session.connect(token, (error) => {
if (error) {
handleError(error);
} else {
// If the connection is successful, publish the publisher to the session
session.publish(publisher, handleError);
}
});
});
}

Expand Down Expand Up @@ -132,6 +177,7 @@ if (SAMPLE_SERVER_BASE_URL) {
apiKey = json.apiKey;
sessionId = json.sessionId;
token = json.token;
console.log(json);
// Initialize an OpenTok Session object
initializeSession();
}).catch((error) => {
Expand Down