Skip to content

Commit

Permalink
Merge pull request #37 from nexmo-se/main
Browse files Browse the repository at this point in the history
Update Sample App
  • Loading branch information
abdulajet authored Oct 31, 2022
2 parents 12d9d9c + 38d91be commit c6f334b
Show file tree
Hide file tree
Showing 24 changed files with 5,118 additions and 696 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
.DS_STORE
config.json
141 changes: 56 additions & 85 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Broadcast Sample App",
"description": "This sample app shows how to use the OpenTok HLS Broadcast functionality in a JavaScript app.",
"description": "This sample app shows how to use the Vonage Video API HLS Broadcast functionality in a JavaScript app.",
"website": "https://vonage.com",
"repository": "https://github.com/opentok/broadcast-sample-app",
"image": "https://assets.tokbox.com/img/vonage/Vonage_VideoAPI_black.svg",
Expand Down
4 changes: 0 additions & 4 deletions config.json

This file was deleted.

5 changes: 5 additions & 0 deletions config.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"apiKey": "",
"apiSecret": "",
"broadcastDefaultResolution": "1280x720"
}
130 changes: 98 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,93 @@ app.use(express.json());
app.use(express.static('public'));
app.set('view engine', 'ejs');

const sessions = {};

const opentok = require('./services/opentok-api');

const generateCredentials = async (usertype, roomName) => {
if (sessions[roomName]) {
const token = opentok.createToken(usertype, sessions[roomName]);
credentials = {
apiKey: opentok.apiKey,
sessionId: sessions[roomName],
token: token,
};
return credentials;
} else {
try {
const credentials = await opentok.getCredentials(usertype);
sessions[roomName] = credentials.sessionId;
return credentials;
} catch (e) {
return e;
}
}
};

/*
* Routes
*/
app.get('/', (req, res) => {
res.redirect('/viewer');
});

app.get('/host', async (req, res) => {
const roomName = req.query.room;
try {
const credentials = await generateCredentials('host', roomName);
res.render('pages/host', {
credentials: JSON.stringify(credentials),
});
} catch (e) {
res.status(500).send(error);
}
});

app.get('/viewer', async (req, res) => {
opentok.getCredentials('viewer')
.then(credentials => res.render('pages/viewer', { credentials: JSON.stringify(credentials) }))
.catch(error => res.status(500).send(error));
})

app.get('/host', (req, res) => {
opentok.getCredentials('host')
.then(credentials => res.render('pages/host', { credentials: JSON.stringify(credentials) }))
.catch(error => res.status(500).send(error));
const roomName = req.query.room;
try {
const credentials = await generateCredentials('viewer', roomName);
res.render('pages/viewer', {
credentials: JSON.stringify(credentials),
});
} catch (e) {
res.status(500).send(error);
}
});

app.get('/guest', (req, res) => {
opentok.getCredentials('guest')
.then(credentials => res.render('pages/guest', { credentials: JSON.stringify(credentials) }))
.catch(error => res.status(500).send(error));
app.get('/hls-viewer', async (req, res) => {
const roomName = req.query.room;
try {
const credentials = await generateCredentials('viewer', roomName);
res.render('pages/hls-viewer', {
credentials: JSON.stringify(credentials),
});
} catch (e) {
res.status(500).send(error);
}
});

app.get('/broadcast', (req, res) => {
const url = req.query.url;
const availableAt = req.query.availableAt;
res.render('pages/broadcast', { broadcast: JSON.stringify({ url, availableAt }) });
app.get('/guest', async (req, res) => {
const roomName = req.query.room;
try {
const credentials = await generateCredentials('guest', roomName);
res.render('pages/guest', {
credentials: JSON.stringify(credentials),
});
} catch (e) {
res.status(500).send(error);
}
});

app.get('/broadcast/:room', (req, res) => {
const { room } = req.params;

if (!room) res.status(500);
if (sessions[room] && opentok.activeBroadcast[sessions[room]]) res.json({ url: opentok.activeBroadcast[sessions[room]].url });
else {
res.status(500).send('no broadcast url found');
}
});

app.get('*', (req, res) => {
Expand All @@ -47,30 +103,40 @@ app.get('*', (req, res) => {
* API Endpoints
*/
app.post('/broadcast/start', (req, res) => {
const { streams, rtmp } = req.body;
opentok.startBroadcast(streams, rtmp)
.then(data => res.send(data))
.catch(error => res.status(500).send(error));
const { rtmp, lowLatency, fhd, dvr, sessionId } = req.body;

opentok
.startBroadcast(rtmp, lowLatency, fhd, dvr, sessionId)
.then((data) => res.send(data))
.catch((error) => {
console.log(error);

res.status(500).send(error);
});
});

app.post('/broadcast/layout', (req, res) => {
const { streams, type } = req.body;
opentok.updateLayout(streams, type)
.then(data => res.status(200).send({}))
.catch(error => res.status(500).send(error));
const { streams, type, sessionId } = req.body;
opentok
.updateLayout(streams, type, sessionId)
.then((data) => res.status(200).send({}))
.catch((error) => res.status(500).send(error));
});

app.post('/broadcast/classes', (req, res) => {
const { classList } = req.body;
opentok.updateStreamClassList(classList)
.then(data => res.status(200).send({}))
.catch(error => res.status(500).send(error));
const { classList, sessionId } = req.body;
opentok
.updateStreamClassList(classList, sessionId)
.then((data) => res.status(200).send({}))
.catch((error) => res.status(500).send(error));
});

app.post('/broadcast/end', (req, res) => {
opentok.stopBroadcast()
.then(data => res.send(data))
.catch(error => res.status(500).send(error));
const { sessionId } = req.body;
opentok
.stopBroadcast(sessionId)
.then((data) => res.send(data))
.catch((error) => res.status(500).send(error));
});

/*
Expand Down
Loading

0 comments on commit c6f334b

Please sign in to comment.