-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathopentok.js
228 lines (199 loc) · 5.94 KB
/
opentok.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var https = require('https')
, querystring = require('querystring')
, crypto = require('crypto')
// tokbox constants:
, TOKEN_SENTINEL = "T1=="
, API_HOST = "api.opentok.com"
, SESSION_API_ENDPOINT = "/hl/session/create"
, GET_MANIFEST = "/archive/getmanifest/"
, GET_URL = "/hl/archive/url/";
var RoleConstants = exports.RoleConstants = {
SUBSCRIBER: "subscriber",
PUBLISHER: "publisher",
MODERATOR: "moderator"
}
// OpenTokSession constructor (only used internally)
var OpenTokSession = function(sessionId){
this.sessionId = sessionId
}
// OpenTokSDK constructor
var OpenTokSDK = exports.OpenTokSDK = function(partnerId, partnerSecret){
this.partnerId = partnerId
this.partnerSecret = partnerSecret
this.api_url = API_HOST;
}
OpenTokSDK.OpenTokArchive = function(sdkObject){
var self = this;
this.resources = [];
this.addVideoResource = function(res){
self.resources.push(res);
};
this.downloadArchiveURL=function(vid, callback){
options = {
host: sdkObject.api_url,
path: GET_URL+sdkObject.archiveId+"/"+vid,
method: 'GET',
headers: {
'x-tb-token-auth':sdkObject.token
}
};
req = https.request(options, function(res){
var chunks = '';
res.setEncoding('utf8')
res.on('data', function(chunk){
chunks += chunk
})
res.on('end', function(){
callback(chunks);
})
})
req.end()
};
};
OpenTokSDK.OpenTokArchiveVideoResource = function(vid,length,name){
this.vid = vid;
this.length = length;
this.name = name;
this.getId = function(){
return vid;
};
};
OpenTokSDK.prototype.getArchiveManifest = function(archiveId, token, callback){
this.get_archive_manifest( archiveId, token, callback );
}
OpenTokSDK.prototype.get_archive_manifest = function(archiveId, token, callback){
this.token = token;
this.archiveId = archiveId;
var self = this;
var parseResponse = function(chunks){
var response = new OpenTokSDK.OpenTokArchive(self);
var start = chunks.match('<resources>')
, end = chunks.match('</resources>')
var videoTags = null
if(start && end){
videoTags = chunks.substr(start.index + 12, (end.index - start.index - 12))
attr = videoTags.split('"');
if(attr.length>5){
vid = attr[1]
length = attr[3]
name = attr[5]
resource = new OpenTokSDK.OpenTokArchiveVideoResource(vid, length, name);
response.addVideoResource(resource);
}
}
callback(response);
};
options = {
host: this.api_url,
path: GET_MANIFEST+archiveId,
method: 'GET',
headers: {
'x-tb-token-auth':token
}
}
req = https.request(options, function(res){
var chunks = '';
res.setEncoding('utf8')
res.on('data', function(chunk){
chunks += chunk
})
res.on('end', function(){
parseResponse(chunks);
})
})
req.end()
}
OpenTokSDK.prototype.generate_token = function(ops){
ops = ops || {};
// At some point in this packages existence, three different forms of Session ID were used
// Fallback to default (last session created using this OpenTokSDK instance)
var sessionId = ops.session_id || ops.sessionId || ops.session || this.sessionId;
var createTime = OpenTokSDK.prototype._getUTCDate()
, sig
, tokenString
, tokenParams
, tokenBuffer
, dataString
, dataParams = {
session_id: sessionId,
create_time: createTime,
nonce: Math.floor(Math.random()*999999),
role: RoleConstants.PUBLISHER // will be overriden below if passed in
};
// pass through any other tokbox parameters:
for(var op in ops){
if(ops.hasOwnProperty(op)){
dataParams[op] = ops[op]
}
}
dataString = querystring.stringify(dataParams)
sig = this._signString(dataString, this.partnerSecret)
tokenParams = ["partner_id=",this.partnerId,"&sig=",sig,":",dataString].join("")
tokenBuffer = new Buffer(tokenParams,"utf8");
return TOKEN_SENTINEL + tokenBuffer.toString('base64');
}
OpenTokSDK.prototype.generateToken = function(ops){
return this.generate_token(ops);
}
OpenTokSDK.prototype.create_session = function(ipPassthru, properties, callback){
// Since properties is optional, we need to check the number of parameters passed in
if(!callback){
callback = properties
}
var sessionId
, params = {
partner_id: this.partnerId,
location_hint: ipPassthru
};
for(var p in properties){
params[p] = properties[p]
}
var self = this;
sessionId = this._doRequest(params, function(chunks){
var start = chunks.match('<session_id>')
, end = chunks.match('</session_id>')
, sessionId;
if(start && end){
self.sessionId = chunks.substr(start.index + 12, (end.index - start.index - 12))
}
callback(self.sessionId)
});
}
OpenTokSDK.prototype.createSession = function(ipPassthru, properties, callback){
this.create_session(ipPassthru, properties, callback);
}
OpenTokSDK.prototype._signString = function(string, secret){
var hmac = crypto.createHmac('sha1',secret)
hmac.update(string)
return hmac.digest(encoding='hex')
}
OpenTokSDK.prototype._doRequest = function(params, callback){
var dataString = querystring.stringify(params);
options = {
host: this.api_url,
path: SESSION_API_ENDPOINT,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': dataString.length,
'X-TB-PARTNER-AUTH': this.partnerId + ":" + this.partnerSecret
}
}
req = https.request(options, function(res){
var chunks = '';
res.setEncoding('utf8')
res.on('data', function(chunk){
chunks += chunk
})
res.on('end', function(){
callback(chunks);
})
})
req.write(dataString)
req.end()
}
OpenTokSDK.prototype._getUTCDate = function(){
var D= new Date();
return Date.UTC(D.getUTCFullYear(), D.getUTCMonth(), D.getUTCDate(), D.getUTCHours(),
D.getUTCMinutes(), D.getUTCSeconds()).toString().substr(0,10)
}