@@ -6,37 +6,93 @@ app.use(express.json());
6
6
app . use ( express . static ( 'public' ) ) ;
7
7
app . set ( 'view engine' , 'ejs' ) ;
8
8
9
+ const sessions = { } ;
10
+
9
11
const opentok = require ( './services/opentok-api' ) ;
10
12
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
+
11
33
/*
12
34
* Routes
13
35
*/
14
36
app . get ( '/' , ( req , res ) => {
15
37
res . redirect ( '/viewer' ) ;
16
38
} ) ;
17
39
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
+
18
52
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
+ }
28
62
} ) ;
29
63
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
+ }
34
74
} ) ;
35
75
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
+ }
40
96
} ) ;
41
97
42
98
app . get ( '*' , ( req , res ) => {
@@ -47,30 +103,40 @@ app.get('*', (req, res) => {
47
103
* API Endpoints
48
104
*/
49
105
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
+ } ) ;
54
116
} ) ;
55
117
56
118
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 ) ) ;
61
124
} ) ;
62
125
63
126
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 ) ) ;
68
132
} ) ;
69
133
70
134
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 ) ) ;
74
140
} ) ;
75
141
76
142
/*
0 commit comments