@@ -6,37 +6,93 @@ app.use(express.json());
66app . use ( express . static ( 'public' ) ) ;
77app . set ( 'view engine' , 'ejs' ) ;
88
9+ const sessions = { } ;
10+
911const 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 */
1436app . 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+
1852app . 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
4298app . get ( '*' , ( req , res ) => {
@@ -47,30 +103,40 @@ app.get('*', (req, res) => {
47103 * API Endpoints
48104 */
49105app . 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
56118app . 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
63126app . 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
70134app . 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