@@ -32,6 +32,10 @@ class Roles(Enum):
32
32
Session object.
33
33
"""
34
34
35
+ class MediaModes (Enum ):
36
+ routed = u ('disabled' )
37
+ relayed = u ('enabled' )
38
+
35
39
class OpenTok (object ):
36
40
"""Use this SDK to create tokens and interface with the server-side portion
37
41
of the Opentok API.
@@ -128,7 +132,7 @@ def generate_token(self, session_id, role=Roles.publisher, expire_time=None, dat
128
132
129
133
sig = self ._sign_string (data_string , self .api_secret )
130
134
decoded_base64_bytes = u ('partner_id={api_key}&sig={sig}:{payload}' ).format (
131
- api_key = self .api_key ,
135
+ api_key = self .api_key ,
132
136
sig = sig ,
133
137
payload = data_string
134
138
)
@@ -140,58 +144,57 @@ def generate_token(self, session_id, role=Roles.publisher, expire_time=None, dat
140
144
)
141
145
return token
142
146
143
- def create_session (self , location = None , p2p = False ):
147
+ def create_session (self , location = None , media_mode = MediaModes . routed ):
144
148
"""
145
149
Creates a new OpenTok session and returns the session ID, which uniquely identifies
146
150
the session.
147
-
151
+
148
152
For example, when using the OpenTok JavaScript library, use the session ID when calling the
149
153
OT.initSession() method (to initialize an OpenTok session).
150
-
154
+
151
155
OpenTok sessions do not expire. However, authentication tokens do expire (see the
152
156
generateToken() method). Also note that sessions cannot explicitly be destroyed.
153
-
157
+
154
158
A session ID string can be up to 255 characters long.
155
-
159
+
156
160
Calling this method results in an OpenTokException in the event of an error.
157
161
Check the error message for details.
158
-
162
+
159
163
You can also create a session using the OpenTok REST API (see
160
164
http://www.tokbox.com/opentok/api/#session_id_production) or the OpenTok dashboard
161
165
(see https://dashboard.tokbox.com/projects).
162
-
166
+
163
167
:param String p2p: The session's streams will be transmitted directly between
164
168
peers (true) or using the OpenTok Media Router (false). By default, sessions use
165
169
the OpenTok Media Router.
166
-
170
+
167
171
The OpenTok Media Router provides benefits not available in peer-to-peer sessions.
168
- For example, the OpenTok Media Router can decrease bandwidth usage in multiparty
172
+ For example, the OpenTok Media Router can decrease bandwidth usage in multiparty
169
173
sessions. Also, the OpenTok Media Router can improve the quality of the user experience
170
174
through dynamic traffic shaping. For more information, see
171
175
http://www.tokbox.com/blog/mantis-next-generation-cloud-technology-for-webrtc and
172
176
http://www.tokbox.com/blog/quality-of-experience-and-traffic-shaping-the-next-step-with-mantis.
173
-
177
+
174
178
For peer-to-peer sessions, the session will attempt to transmit streams directly
175
179
between clients. If clients cannot connect due to firewall restrictions, the session
176
180
uses the OpenTok TURN server to relay audio-video streams.
177
-
181
+
178
182
You will be billed for streamed minutes if you use the OpenTok Media Router or if the
179
183
peer-to-peer session uses the OpenTok TURN server to relay streams. For information on
180
184
pricing, see the OpenTok pricing page (http://www.tokbox.com/pricing).
181
-
185
+
182
186
:param String location: An IP address that the OpenTok servers will use to
183
187
situate the session in its global network. If you do not set a location hint,
184
188
the OpenTok servers will be based on the first client connecting to the session.
185
-
189
+
186
190
:rtype: The Session object. The session_id property of the object is the session ID.
187
191
"""
188
192
189
193
# build options
190
194
options = {}
191
- if p2p :
192
- if not isinstance (p2p , bool ):
193
- raise OpenTokException (u ('Cannot create session. p2p must be a bool {0}' ).format (p2p ))
194
- options [u ('p2p.preference' )] = u ('enabled' )
195
+ if not isinstance (media_mode , MediaModes ):
196
+ raise OpenTokException (u ('Cannot create session, {0} is not a valid media mode' ).format (role ))
197
+ options [u ('p2p.preference' )] = media_mode .value
195
198
if location :
196
199
# validate IP address
197
200
try :
@@ -219,7 +222,7 @@ def create_session(self, location=None, p2p=False):
219
222
raise AuthError ('Failed to create session (code=%s): %s' % (error .attributes ['code' ].value , error .firstChild .attributes ['message' ].value ))
220
223
221
224
session_id = dom .getElementsByTagName ('session_id' )[0 ].childNodes [0 ].nodeValue
222
- return Session (self , session_id , location = location , p2p = p2p )
225
+ return Session (self , session_id , location = location , media_mode = media_mode )
223
226
except Exception as e :
224
227
raise OpenTokException ('Failed to generate session: %s' % str (e ))
225
228
@@ -251,18 +254,18 @@ def archive_url(self, archive_id=None):
251
254
def start_archive (self , session_id , ** kwargs ):
252
255
"""
253
256
Starts archiving an OpenTok 2.0 session.
254
-
257
+
255
258
Clients must be actively connected to the OpenTok session for you to successfully start
256
259
recording an archive.
257
-
260
+
258
261
You can only record one archive at a time for a given session. You can only record archives
259
262
of OpenTok server-enabled sessions; you cannot archive peer-to-peer sessions.
260
-
263
+
261
264
:param String session_id: The session ID of the OpenTok session to archive.
262
265
:param String name: This is the name of the archive. You can use this name
263
266
to identify the archive. It is a property of the Archive object, and it is a property
264
267
of archive-related events in the OpenTok.js library.
265
-
268
+
266
269
:rtype: The Archive object, which includes properties defining the archive,
267
270
including the archive ID.
268
271
"""
@@ -286,12 +289,12 @@ def start_archive(self, session_id, **kwargs):
286
289
def stop_archive (self , archive_id ):
287
290
"""
288
291
Stops an OpenTok archive that is being recorded.
289
-
292
+
290
293
Archives automatically stop recording after 90 minutes or when all clients have disconnected
291
294
from the session being archived.
292
-
295
+
293
296
@param [String] archive_id The archive ID of the archive you want to stop recording.
294
-
297
+
295
298
:rtype: The Archive object corresponding to the archive being stopped.
296
299
"""
297
300
response = requests .post (self .archive_url (archive_id ) + '/stop' , headers = self .archive_headers ())
@@ -310,7 +313,7 @@ def stop_archive(self, archive_id):
310
313
def delete_archive (self , archive_id ):
311
314
"""
312
315
Deletes an OpenTok archive.
313
-
316
+
314
317
You can only delete an archive which has a status of "available" or "uploaded". Deleting an
315
318
archive removes its record from the list of archives. For an "available" archive, it also
316
319
removes the archive file, making it unavailable for download.
@@ -330,9 +333,9 @@ def delete_archive(self, archive_id):
330
333
331
334
def get_archive (self , archive_id ):
332
335
"""Gets an Archive object for the given archive ID.
333
-
336
+
334
337
:param String archive_id: The archive ID.
335
-
338
+
336
339
:rtype: The Archive object.
337
340
"""
338
341
response = requests .get (self .archive_url (archive_id ), headers = self .archive_headers ())
@@ -349,13 +352,13 @@ def get_archive(self, archive_id):
349
352
def get_archives (self , offset = None , count = None ):
350
353
"""Returns an ArchiveList, which is an array of archives that are completed and in-progress,
351
354
for your API key.
352
-
355
+
353
356
:param int: offset Optional. The index offset of the first archive. 0 is offset
354
357
of the most recently started archive. 1 is the offset of the archive that started prior to
355
358
the most recent archive. If you do not specify an offset, 0 is used.
356
359
:param int: count Optional. The number of archives to be returned. The maximum
357
360
number of archives returned is 1000.
358
-
361
+
359
362
:rtype: An ArchiveList object, which is an array of Archive objects.
360
363
"""
361
364
params = {}
@@ -377,4 +380,3 @@ def get_archives(self, offset=None, count=None):
377
380
378
381
def _sign_string (self , string , secret ):
379
382
return hmac .new (secret .encode ('utf-8' ), string .encode ('utf-8' ), hashlib .sha1 ).hexdigest ()
380
-
0 commit comments