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

Update Basic Example with more options #1732

Merged
merged 2 commits into from
Jun 30, 2021
Merged
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
23 changes: 17 additions & 6 deletions extras/basic_example/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@
</head>

<body>
<button id="startButton" onclick="startBasicExample()" disabled>Start</button>
<button id="testConnection" onclick="testConnection()">Test Connection</button>
<button id="recordButton" onclick="startRecording()" disabled>Toggle Recording</button>
<button id="slideShowMode" onclick="toggleSlideShowMode()" disabled>Toggle Slide Show Mode</button>
<h1 id="startWarning">Press the start buttong to start receiving streams</h1>
<div id="videoContainer"></div>
<div>
<button id="startButton" onclick="startBasicExample()" disabled>Start</button>
<button id="testConnection" onclick="testConnection()">Test Connection</button>
<button id="recordButton" onclick="startRecording()" disabled>Toggle Recording</button>
<button id="slideShowMode" onclick="toggleSlideShowMode()" disabled>Toggle Slide Show Mode</button>
</div>
<div style="margin-top: 5px;">
<label>Publish:</label>
<button id="publishScreen" onclick="publish(true, false, true)" disabled>Screen</button>
<button id="publishScreenWithAudio" onclick="publish(true, true, true)" disabled>Screen+Audio</button>
<button id="publishCamera" onclick="publish(true, true, false)" disabled>Video+Audio</button>
<button id="publishOnlyVideo" onclick="publish(true, false, false)" disabled>Video Only</button>
<button id="publishOnlyAudio" onclick="publish(false, true, false)" disabled>Audio Only</button>
</div>
<h1 id="startWarning">Press the start button to start receiving streams</h1>
<div style="height: 40px; margin-top: 5px;position:relative;float: left;"><label>Subscribe:</label></div><div id="remoteStreamList" style="margin-top: 5px;"></div>
<div id="videoContainer" style="position: relative;clear: both;"></div>
</body>
</html>
131 changes: 120 additions & 11 deletions extras/basic_example/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ let localStream;
let room;
let recording = false;
let recordingId = '';
let localStreamIndex = 0;
const localStreams = new Map();
const configFlags = {
noStart: false, // disable start button when only subscribe
forceStart: false, // force start button in all cases
Expand All @@ -25,6 +27,63 @@ const configFlags = {
unencrypted: false,
};

const createSubscriberContainer = (stream) => {
const container = document.createElement('div');
container.setAttribute('style', 'width: 320px; height: 280px;float:left;');
container.setAttribute('id', `container_${stream.getID()}`);

const videoContainer = document.createElement('div');
videoContainer.setAttribute('style', 'width: 320px; height: 240px;');
videoContainer.setAttribute('id', `test${stream.getID()}`);
container.appendChild(videoContainer);
const unsubscribeButton = document.createElement('button');
unsubscribeButton.textContent = 'Unsubscribe';
unsubscribeButton.setAttribute('style', 'float:left;');
const slideshowButton = document.createElement('button');
slideshowButton.textContent = 'Toggle Slideshow';
slideshowButton.setAttribute('style', 'float:left;');
stream.slideshowMode = false;

container.appendChild(unsubscribeButton);
container.appendChild(slideshowButton);
unsubscribeButton.onclick = () => {
room.unsubscribe(stream);
document.getElementById('videoContainer').removeChild(container);
};
slideshowButton.onclick = () => {
stream.updateConfiguration({ slideShowMode: !stream.slideshowMode }, () => {});
stream.slideshowMode = !stream.slideshowMode;
};
document.getElementById('videoContainer').appendChild(container);
stream.show(`test${stream.getID()}`);

const unsubscribeBtn = document.getElementById(`subscribe_btn_${stream.getID()}`);
if (unsubscribeBtn) {
unsubscribeBtn.disabled = true;
}
};

const createPublisherContainer = (stream, index) => {
const container = document.createElement('div');
container.setAttribute('style', 'width: 320px; height: 280px;float:left;');
container.setAttribute('id', `container_${index}`);
const unpublishButton = document.createElement('button');
unpublishButton.textContent = 'Unpublish';
unpublishButton.setAttribute('style', 'float:left;');

unpublishButton.onclick = () => {
room.unpublish(stream);
document.getElementById('videoContainer').removeChild(container);
};

const div = document.createElement('div');
div.setAttribute('style', 'width: 320px; height: 240px; float:left');
div.setAttribute('id', `myVideo${index}`);
container.appendChild(div);
container.appendChild(unpublishButton);
document.getElementById('videoContainer').appendChild(container);
};

const getParameterByName = (name) => {
// eslint-disable-next-line
name = name.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
Expand Down Expand Up @@ -87,9 +146,36 @@ function toggleSlideShowMode() {
});
}

// eslint-disable-next-line no-unused-vars
const publish = (video, audio, screen) => {
const config = { audio,
video,
data: true,
screen,
attributes: {} };
const stream = Erizo.Stream(config);
const index = localStreamIndex;
localStreamIndex += 1;
localStreams[index] = stream;
createPublisherContainer(stream, index);

stream.addEventListener('access-accepted', () => {
const options = { metadata: { type: 'publisher' } };
if (configFlags.simulcast) options.simulcast = { numSpatialLayers: 3 };
room.publish(stream, options);
stream.show(`myVideo${index}`);
});
stream.init();
};

const startBasicExample = () => {
document.getElementById('startButton').disabled = true;
document.getElementById('slideShowMode').disabled = false;
document.getElementById('publishCamera').disabled = false;
document.getElementById('publishScreen').disabled = false;
document.getElementById('publishScreenWithAudio').disabled = false;
document.getElementById('publishOnlyVideo').disabled = false;
document.getElementById('publishOnlyAudio').disabled = false;
document.getElementById('startWarning').hidden = true;
document.getElementById('startButton').hidden = true;
recording = false;
Expand Down Expand Up @@ -136,6 +222,22 @@ const startBasicExample = () => {
window.room = room;

const subscribeToStreams = (streams) => {
streams.forEach((stream) => {
if (!stream.local) {
const streamContainer = document.createElement('div');
streamContainer.setAttribute('id', `stream_element_${stream.getID()}`);
const subscribeButton = document.createElement('button');
subscribeButton.textContent = stream.getID();
subscribeButton.setAttribute('style', 'float:left;');
subscribeButton.setAttribute('id', `subscribe_btn_${stream.getID()}`);
subscribeButton.onclick = () => {
room.subscribe(stream);
};
streamContainer.appendChild(subscribeButton);
document.getElementById('remoteStreamList').appendChild(streamContainer);
}
});

if (configFlags.autoSubscribe) {
return;
}
Expand Down Expand Up @@ -171,12 +273,15 @@ const startBasicExample = () => {

room.addEventListener('stream-subscribed', (streamEvent) => {
const stream = streamEvent.stream;
const div = document.createElement('div');
div.setAttribute('style', 'width: 320px; height: 240px;float:left;');
div.setAttribute('id', `test${stream.getID()}`);
createSubscriberContainer(stream);
});

document.getElementById('videoContainer').appendChild(div);
stream.show(`test${stream.getID()}`);
room.addEventListener('stream-unsubscribed', (streamEvent) => {
const stream = streamEvent.stream;
const unsubscribeBtn = document.getElementById(`subscribe_btn_${stream.getID()}`);
if (unsubscribeBtn) {
unsubscribeBtn.disabled = false;
}
});

room.addEventListener('stream-added', (streamEvent) => {
Expand All @@ -193,8 +298,15 @@ const startBasicExample = () => {
// Remove stream from DOM
const stream = streamEvent.stream;
if (stream.elementID !== undefined) {
const element = document.getElementById(stream.elementID);
document.getElementById('videoContainer').removeChild(element);
const element = document.getElementById(`container_${stream.getID()}`);
if (element) {
document.getElementById('videoContainer').removeChild(element);
}
}
const streamContainer = document.getElementById(`stream_element_${stream.getID()}`);
console.log('Removing', `stream_element_${stream.getID()}`);
if (streamContainer) {
document.getElementById('remoteStreamList').removeChild(streamContainer);
}
});

Expand All @@ -205,10 +317,7 @@ const startBasicExample = () => {
if (configFlags.onlySubscribe) {
room.connect({ singlePC: configFlags.singlePC });
} else {
const div = document.createElement('div');
div.setAttribute('style', 'width: 320px; height: 240px; float:left');
div.setAttribute('id', 'myVideo');
document.getElementById('videoContainer').appendChild(div);
createPublisherContainer(localStream, '');

localStream.addEventListener('access-accepted', () => {
room.connect({ singlePC: configFlags.singlePC });
Expand Down