Skip to content

Commit

Permalink
JSDK-2353 Adding an example for capturing screen using getDisplayMedi…
Browse files Browse the repository at this point in the history
…a(). (#71)
  • Loading branch information
manjeshbhargav authored May 14, 2019
1 parent b27dd7c commit f737dd5
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ examples/codecpreferences/public/index.js
examples/localvideofilter/public/index.js
examples/localvideosnapshot/public/index.js
examples/mediadevices/public/index.js
examples/screenshare/public/index.js

# The generated helpers.js
examples/bandwidthconstraints/public/helpers.js
examples/codecpreferences/public/helpers.js
examples/localvideofilter/public/helpers.js
examples/localvideosnapshot/public/helpers.js
examples/mediadevices/public/helpers.js
examples/screenshare/public/helpers.js
13 changes: 12 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h4 class="card-header">
</a>
</div>
</div>
<div class="col-sm-6">
<div class="col-sm-12">
<div class="card-deck">
<a class="card" href="/codecpreferences" style="text-decoration: none">
<h4 class="card-header">
Expand All @@ -80,6 +80,17 @@ <h4 class="card-header">
</p>
</div>
</a>
<a class="card" href="/screenshare" style="text-decoration: none">
<h4 class="card-header">
Share Your Screen
</h4>
<div class="card-block">
<p class="card-text">
This app demonstrates how to capture your screen so that you can share it with other Participants
in the Room.
</p>
</div>
</a>
</div>
</div>
</div>
Expand Down
87 changes: 87 additions & 0 deletions examples/screenshare/public/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
@import url('https://fonts.googleapis.com/css?family=Roboto+Mono:300');

html {
height: 100%;
}

body {
height: 100%;
}

div.container-fluid {
height: 100%;
}

div.row {
height: 100%;
}

div.row.thin-gutters {
margin: 0 2px 0 2px;
}

div.row.thin-gutters > .col,
div.row.thin-gutters > [class*="col-"] {
padding: 0 2px;
}

div.col-sm-8, div.col-sm-4 {
height: 100%;
}

pre.language-javascript {
font-family: 'Roboto Mono', monospace;
font-size: 13px;
}

pre.language-javascript a {
color: aquamarine;
text-decoration: underline;
}

pre.language-javascript a:hover {
text-decoration: none;
}

div.card {
border: none;
overflow-y: auto;
}

div.col-sm-8 > .card {
height: 100%;
}

div.col-sm-4 > .card:first-child {
height: 30%;
}

div.col-sm-4 > .card:last-child {
height: 70%;
}

video#screenpreview {
background-color: lightgrey !important;
background-image: url('https://static0.twilio.com/marketing/bundles/archetype/img/logo-wordmark.svg');
background-position: 50%;
background-repeat: no-repeat;
}

@media (max-width: 900px) {
div.col-sm-8, div.col-sm-4 {
max-width: 100%;
flex: 100%;
}

div.col-sm-8 {
height: 40%;
}

div.col-sm-4 {
height: 60%;
}

pre.language-javascript {
font-size: 12px;
}
}
39 changes: 39 additions & 0 deletions examples/screenshare/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Share Your Screen</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" href="prism.css">
<link rel="stylesheet" href="index.css">
</head>
<body>
<div class="container-fluid">
<div class="row thin-gutters">
<div class="col-sm-6">
<div class="card">
<div class="card-block">
<h4 class="card-title">
Share Your Screen
</h4>
<pre class="language-javascript"></pre>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="card">
<div class="card-block">
<h4 class="card-title">Local Screen</h4>
<video id="screenpreview" autoplay height="480" width="640"></video>
<button id="capturescreen" class="btn btn-primary btn-block">Capture Screen</button>
<button id="stopscreencapture" class="btn btn-primary btn-block">Stop Screen Capture</button>
</div>
</div>
</div>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
123 changes: 123 additions & 0 deletions examples/screenshare/public/prism.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* http://prismjs.com/download.html?themes=prism-okaidia&languages=markup+css+clike+javascript */
/**
* okaidia theme for JavaScript, CSS and HTML
* Loosely based on Monokai textmate theme by http://www.monokai.nl/
* @author ocodia
*/

code[class*="language-"],
pre[class*="language-"] {
color: #f8f8f2;
background: none;
text-shadow: 0 1px rgba(0, 0, 0, 0.3);
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
text-align: left;
white-space: pre;
word-spacing: normal;
word-break: normal;
word-wrap: normal;
line-height: 1.5;

-moz-tab-size: 4;
-o-tab-size: 4;
tab-size: 4;

-webkit-hyphens: none;
-moz-hyphens: none;
-ms-hyphens: none;
hyphens: none;
}

/* Code blocks */
pre[class*="language-"] {
padding: 1em;
margin: .5em 0;
overflow: auto;
border-radius: 0.3em;
}

:not(pre) > code[class*="language-"],
pre[class*="language-"] {
background: #272822;
}

/* Inline code */
:not(pre) > code[class*="language-"] {
padding: .1em;
border-radius: .3em;
white-space: normal;
}

.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
color: #87ceeb;
}

.token.operator,
.token.punctuation {
color: #ff5555;
}

.namespace {
opacity: .7;
}

.token.property,
.token.tag,
.token.constant,
.token.symbol,
.token.deleted {
color: #f92672;
}

.token.boolean {
color: #55ff55;
}

.token.number {
color: #cd5c5c;
}

.token.selector,
.token.attr-name,
.token.string,
.token.char,
.token.builtin,
.token.inserted {
color: #ff55ff;
}

.token.entity,
.token.url,
.language-css .token.string,
.style .token.string,
.token.variable {
color: #ff55ff;
}

.token.function {
color: #ccc;
}

.token.keyword {
color: #55ff55;
}

.token.regex,
.token.important {
color: #fd971f;
}

.token.important,
.token.bold {
font-weight: bold;
}
.token.italic {
font-style: italic;
}

.token.entity {
cursor: help;
}
28 changes: 28 additions & 0 deletions examples/screenshare/src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const Video = require('twilio-video');

/**
* Create a LocalVideoTrack for your screen. You can then share it
* with other Participants in the Room.
* @param {number} height - Desired vertical resolution in pixels
* @param {number} width - Desired horizontal resolution in pixels
* @returns {Promise<LocalVideoTrack>}
*/
function createScreenTrack(height, width) {
if (typeof navigator === 'undefined'
|| !navigator.mediaDevices
|| !navigator.mediaDevices.getDisplayMedia) {
return Promise.reject(new Error('getDisplayMedia is not supported'));
}
return navigator.mediaDevices.getDisplayMedia({
video: {
height: height,
width: width
}
}).then(function(stream) {
return new Video.LocalVideoTrack(stream.getVideoTracks()[0]);
});
}

exports.createScreenTrack = createScreenTrack;
46 changes: 46 additions & 0 deletions examples/screenshare/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const Prism = require('prismjs');
const getSnippet = require('../../util/getsnippet');
const helpers = require('./helpers');
const createScreenTrack = helpers.createScreenTrack;
const captureScreen = document.querySelector('button#capturescreen');
const screenPreview = document.querySelector('video#screenpreview');
const stopScreenCapture = document.querySelector('button#stopscreencapture');

(async function() {
// Load the code snippet.
const snippet = await getSnippet('./helpers.js');
const pre = document.querySelector('pre.language-javascript');
pre.innerHTML = Prism.highlight(snippet, Prism.languages.javascript);

// Hide the "Stop Capture Screen" button.
stopScreenCapture.style.display = 'none';

// The LocalVideoTrack for your screen.
let screenTrack;

captureScreen.onclick = async function() {
try {
// Create and preview your local screen.
screenTrack = await createScreenTrack(720, 1280);
screenTrack.attach(screenPreview);
// Show the "Capture Screen" button after screen capture stops.
screenTrack.on('stopped', toggleButtons);
// Show the "Stop Capture Screen" button.
toggleButtons();
} catch (e) {
alert(e.message);
}
};

stopScreenCapture.onclick = function() {
// Stop capturing your screen.
screenTrack.stop();
}
}());

function toggleButtons() {
captureScreen.style.display = captureScreen.style.display === 'none' ? '' : 'none';
stopScreenCapture.style.display = stopScreenCapture.style.display === 'none' ? '' : 'none';
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"build:examples:localvideofilter": "copyfiles -f examples/localvideofilter/src/helpers.js examples/localvideofilter/public && browserify examples/localvideofilter/src/index.js > examples/localvideofilter/public/index.js",
"build:examples:localvideosnapshot": "copyfiles -f examples/localvideosnapshot/src/helpers.js examples/localvideosnapshot/public && browserify examples/localvideosnapshot/src/index.js > examples/localvideosnapshot/public/index.js",
"build:examples:mediadevices": "copyfiles -f examples/mediadevices/src/helpers.js examples/mediadevices/public && browserify examples/mediadevices/src/index.js > examples/mediadevices/public/index.js",
"build:examples:screenshare": "copyfiles -f examples/screenshare/src/helpers.js examples/screenshare/public && browserify examples/screenshare/src/index.js > examples/screenshare/public/index.js",
"build:quickstart": "browserify quickstart/src/index.js > quickstart/public/index.js",
"clean": "npm-run-all clean:*",
"clean:examples": "npm-run-all clean:examples:*",
Expand All @@ -19,6 +20,7 @@
"clean:examples:localvideofilter": "rimraf examples/localvideofilter/public/index.js examples/localvideofilter/public/helpers.js",
"clean:examples:localvideosnapshot": "rimraf examples/localvideosnapshot/public/index.js examples/localvideosnapshot/public/helpers.js",
"clean:examples:mediadevices": "rimraf examples/mediadevices/public/index.js examples/mediadevices/public/helpers.js",
"clean:examples:screenshare": "rimraf examples/screenshare/public/index.js examples/screenshare/public/helpers.js",
"clean:quickstart": "rimraf quickstart/public/index.js",
"start": "npm run clean && npm run build && node server"
},
Expand Down
3 changes: 2 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var app = express();
'codecpreferences',
'localvideofilter',
'localvideosnapshot',
'mediadevices'
'mediadevices',
'screenshare'
].forEach(function(example) {
var examplePath = path.join(__dirname, `../examples/${example}/public`);
app.use(`/${example}`, express.static(examplePath));
Expand Down

1 comment on commit f737dd5

@psiservices-bshet
Copy link

@psiservices-bshet psiservices-bshet commented on f737dd5 May 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can we specify track name for screensharing?
createLocalTracks({
audio: { name: 'microphone' },
video: { name: 'camera' }
});

Please sign in to comment.