Skip to content

Commit c6f334b

Browse files
authored
Merge pull request #37 from nexmo-se/main
Update Sample App
2 parents 12d9d9c + 38d91be commit c6f334b

24 files changed

+5118
-696
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
node_modules
2+
.DS_STORE
3+
config.json

README.md

+56-85
Large diffs are not rendered by default.

app.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Broadcast Sample App",
3-
"description": "This sample app shows how to use the OpenTok HLS Broadcast functionality in a JavaScript app.",
3+
"description": "This sample app shows how to use the Vonage Video API HLS Broadcast functionality in a JavaScript app.",
44
"website": "https://vonage.com",
55
"repository": "https://github.com/opentok/broadcast-sample-app",
66
"image": "https://assets.tokbox.com/img/vonage/Vonage_VideoAPI_black.svg",

config.json

-4
This file was deleted.

config.sample.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"apiKey": "",
3+
"apiSecret": "",
4+
"broadcastDefaultResolution": "1280x720"
5+
}

index.js

+98-32
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,93 @@ app.use(express.json());
66
app.use(express.static('public'));
77
app.set('view engine', 'ejs');
88

9+
const sessions = {};
10+
911
const opentok = require('./services/opentok-api');
1012

13+
const generateCredentials = async (usertype, roomName) => {
14+
if (sessions[roomName]) {
15+
const token = opentok.createToken(usertype, sessions[roomName]);
16+
credentials = {
17+
apiKey: opentok.apiKey,
18+
sessionId: sessions[roomName],
19+
token: token,
20+
};
21+
return credentials;
22+
} else {
23+
try {
24+
const credentials = await opentok.getCredentials(usertype);
25+
sessions[roomName] = credentials.sessionId;
26+
return credentials;
27+
} catch (e) {
28+
return e;
29+
}
30+
}
31+
};
32+
1133
/*
1234
* Routes
1335
*/
1436
app.get('/', (req, res) => {
1537
res.redirect('/viewer');
1638
});
1739

40+
app.get('/host', async (req, res) => {
41+
const roomName = req.query.room;
42+
try {
43+
const credentials = await generateCredentials('host', roomName);
44+
res.render('pages/host', {
45+
credentials: JSON.stringify(credentials),
46+
});
47+
} catch (e) {
48+
res.status(500).send(error);
49+
}
50+
});
51+
1852
app.get('/viewer', async (req, res) => {
19-
opentok.getCredentials('viewer')
20-
.then(credentials => res.render('pages/viewer', { credentials: JSON.stringify(credentials) }))
21-
.catch(error => res.status(500).send(error));
22-
})
23-
24-
app.get('/host', (req, res) => {
25-
opentok.getCredentials('host')
26-
.then(credentials => res.render('pages/host', { credentials: JSON.stringify(credentials) }))
27-
.catch(error => res.status(500).send(error));
53+
const roomName = req.query.room;
54+
try {
55+
const credentials = await generateCredentials('viewer', roomName);
56+
res.render('pages/viewer', {
57+
credentials: JSON.stringify(credentials),
58+
});
59+
} catch (e) {
60+
res.status(500).send(error);
61+
}
2862
});
2963

30-
app.get('/guest', (req, res) => {
31-
opentok.getCredentials('guest')
32-
.then(credentials => res.render('pages/guest', { credentials: JSON.stringify(credentials) }))
33-
.catch(error => res.status(500).send(error));
64+
app.get('/hls-viewer', async (req, res) => {
65+
const roomName = req.query.room;
66+
try {
67+
const credentials = await generateCredentials('viewer', roomName);
68+
res.render('pages/hls-viewer', {
69+
credentials: JSON.stringify(credentials),
70+
});
71+
} catch (e) {
72+
res.status(500).send(error);
73+
}
3474
});
3575

36-
app.get('/broadcast', (req, res) => {
37-
const url = req.query.url;
38-
const availableAt = req.query.availableAt;
39-
res.render('pages/broadcast', { broadcast: JSON.stringify({ url, availableAt }) });
76+
app.get('/guest', async (req, res) => {
77+
const roomName = req.query.room;
78+
try {
79+
const credentials = await generateCredentials('guest', roomName);
80+
res.render('pages/guest', {
81+
credentials: JSON.stringify(credentials),
82+
});
83+
} catch (e) {
84+
res.status(500).send(error);
85+
}
86+
});
87+
88+
app.get('/broadcast/:room', (req, res) => {
89+
const { room } = req.params;
90+
91+
if (!room) res.status(500);
92+
if (sessions[room] && opentok.activeBroadcast[sessions[room]]) res.json({ url: opentok.activeBroadcast[sessions[room]].url });
93+
else {
94+
res.status(500).send('no broadcast url found');
95+
}
4096
});
4197

4298
app.get('*', (req, res) => {
@@ -47,30 +103,40 @@ app.get('*', (req, res) => {
47103
* API Endpoints
48104
*/
49105
app.post('/broadcast/start', (req, res) => {
50-
const { streams, rtmp } = req.body;
51-
opentok.startBroadcast(streams, rtmp)
52-
.then(data => res.send(data))
53-
.catch(error => res.status(500).send(error));
106+
const { rtmp, lowLatency, fhd, dvr, sessionId } = req.body;
107+
108+
opentok
109+
.startBroadcast(rtmp, lowLatency, fhd, dvr, sessionId)
110+
.then((data) => res.send(data))
111+
.catch((error) => {
112+
console.log(error);
113+
114+
res.status(500).send(error);
115+
});
54116
});
55117

56118
app.post('/broadcast/layout', (req, res) => {
57-
const { streams, type } = req.body;
58-
opentok.updateLayout(streams, type)
59-
.then(data => res.status(200).send({}))
60-
.catch(error => res.status(500).send(error));
119+
const { streams, type, sessionId } = req.body;
120+
opentok
121+
.updateLayout(streams, type, sessionId)
122+
.then((data) => res.status(200).send({}))
123+
.catch((error) => res.status(500).send(error));
61124
});
62125

63126
app.post('/broadcast/classes', (req, res) => {
64-
const { classList } = req.body;
65-
opentok.updateStreamClassList(classList)
66-
.then(data => res.status(200).send({}))
67-
.catch(error => res.status(500).send(error));
127+
const { classList, sessionId } = req.body;
128+
opentok
129+
.updateStreamClassList(classList, sessionId)
130+
.then((data) => res.status(200).send({}))
131+
.catch((error) => res.status(500).send(error));
68132
});
69133

70134
app.post('/broadcast/end', (req, res) => {
71-
opentok.stopBroadcast()
72-
.then(data => res.send(data))
73-
.catch(error => res.status(500).send(error));
135+
const { sessionId } = req.body;
136+
opentok
137+
.stopBroadcast(sessionId)
138+
.then((data) => res.send(data))
139+
.catch((error) => res.status(500).send(error));
74140
});
75141

76142
/*

0 commit comments

Comments
 (0)