-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreate-room.js
165 lines (144 loc) · 4.64 KB
/
create-room.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
const querystring = require('querystring');
const AccessToken = Twilio.jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
const ChatGrant = AccessToken.ChatGrant;
const MAX_ALLOWED_SESSION_DURATION = 14400;
exports.handler = async function (context, event, callback) {
const authHandler = require(Runtime.getAssets()['/passcode.js'].path);
authHandler(context, event, callback);
const common = require(Runtime.getAssets()['/common.js'].path);
const { axiosClient, response } = common(context, event, callback);
const client = context.getTwilioClient();
const conversationsClient = client.conversations.services(context.CONVERSATIONS_SERVICE_SID);
let room, playerStreamer, mediaProcessor, conversation;
try {
// Create video room
room = await client.video.rooms.create({ uniqueName: event.room_name, type: 'group' });
} catch (e) {
console.error(e);
response.setStatusCode(500);
if (e.code === 53113) {
response.setBody({
error: {
message: 'room already exists',
explanation: e.message,
},
});
} else {
response.setBody({
error: {
message: 'error creating room',
explanation: e.message,
},
});
}
return callback(null, response);
}
try {
const maxStreamDuration = 60 * 30; // 30 minutes
// Create playerStreamer
playerStreamer = await axiosClient('PlayerStreamers', {
method: 'post',
data: querystring.stringify({
MaxDuration: maxStreamDuration,
Video: false,
}),
});
// Create mediaProcessor
mediaProcessor = await axiosClient('MediaProcessors', {
method: 'post',
data: querystring.stringify({
MaxDuration: maxStreamDuration,
Extension: context.MEDIA_EXTENSION,
ExtensionContext: JSON.stringify({
room: { name: room.sid },
outputs: [playerStreamer.data.sid],
video: false
}),
}),
});
} catch (e) {
console.error(e);
response.setStatusCode(500);
response.setBody({
error: {
message: 'error creating stream',
explanation: e.message,
},
});
return callback(null, response);
}
try {
// Here we add a timer to close the conversation after the maximum length of a room (24 hours).
// This helps to clean up old conversations since there is a limit that a single participant
// can not be added to more than 1,000 open conversations.
conversation = await conversationsClient.conversations.create({
uniqueName: room.sid,
friendlyName: event.room_name,
'timers.closed': 'P1D',
attributes: JSON.stringify({
stream_url: playerStreamer.data.playback_url,
playerStreamerSid: playerStreamer.data.sid,
mediaProcessorSid: mediaProcessor.data.sid,
}),
});
} catch (e) {
console.error(e);
response.setStatusCode(500);
response.setBody({
error: {
message: 'error creating conversation',
explanation: e.message,
},
});
return callback(null, response);
}
try {
// Add participant to conversation
await conversationsClient.conversations(room.sid).participants.create({
identity: event.user_identity,
attributes: JSON.stringify({
role: 'moderator',
currently_speaking: false,
muted: false,
}),
});
} catch (e) {
// Ignore "Participant already exists" error (50433)
if (e.code !== 50433) {
response.setStatusCode(500);
response.setBody({
error: {
message: 'error creating conversation participant',
explanation: e.message,
},
});
return callback(null, response);
}
}
// Create token
const token = new AccessToken(context.ACCOUNT_SID, context.TWILIO_API_KEY_SID, context.TWILIO_API_KEY_SECRET, {
ttl: MAX_ALLOWED_SESSION_DURATION,
});
// Add chat grant to token
const chatGrant = new ChatGrant({ serviceSid: context.CONVERSATIONS_SERVICE_SID });
token.addGrant(chatGrant);
// Add participant's identity to token
token.identity = event.user_identity;
// Add video grant to stage token
const videoGrant = new VideoGrant({ room: event.room_name });
token.addGrant(videoGrant);
console.log('Created room:', event.room_name);
console.log('PlayerStreamer SID:', playerStreamer.data.sid);
console.log('MediaProcessor SID:', mediaProcessor.data.sid);
console.log('Conversation SID:', conversation.sid);
// Return token
response.setStatusCode(200);
response.setBody({
token: token.toJwt(),
room_sid: room.sid,
conversation_sid: conversation.sid,
});
callback(null, response);
};