@@ -32,6 +32,10 @@ class Roles(Enum):
3232 Session object.
3333 """
3434
35+ class MediaModes (Enum ):
36+ routed = u ('disabled' )
37+ relayed = u ('enabled' )
38+
3539class OpenTok (object ):
3640 """Use this SDK to create tokens and interface with the server-side portion
3741 of the Opentok API.
@@ -128,7 +132,7 @@ def generate_token(self, session_id, role=Roles.publisher, expire_time=None, dat
128132
129133 sig = self ._sign_string (data_string , self .api_secret )
130134 decoded_base64_bytes = u ('partner_id={api_key}&sig={sig}:{payload}' ).format (
131- api_key = self .api_key ,
135+ api_key = self .api_key ,
132136 sig = sig ,
133137 payload = data_string
134138 )
@@ -140,58 +144,57 @@ def generate_token(self, session_id, role=Roles.publisher, expire_time=None, dat
140144 )
141145 return token
142146
143- def create_session (self , location = None , p2p = False ):
147+ def create_session (self , location = None , media_mode = MediaModes . routed ):
144148 """
145149 Creates a new OpenTok session and returns the session ID, which uniquely identifies
146150 the session.
147-
151+
148152 For example, when using the OpenTok JavaScript library, use the session ID when calling the
149153 OT.initSession() method (to initialize an OpenTok session).
150-
154+
151155 OpenTok sessions do not expire. However, authentication tokens do expire (see the
152156 generateToken() method). Also note that sessions cannot explicitly be destroyed.
153-
157+
154158 A session ID string can be up to 255 characters long.
155-
159+
156160 Calling this method results in an OpenTokException in the event of an error.
157161 Check the error message for details.
158-
162+
159163 You can also create a session using the OpenTok REST API (see
160164 http://www.tokbox.com/opentok/api/#session_id_production) or the OpenTok dashboard
161165 (see https://dashboard.tokbox.com/projects).
162-
166+
163167 :param String p2p: The session's streams will be transmitted directly between
164168 peers (true) or using the OpenTok Media Router (false). By default, sessions use
165169 the OpenTok Media Router.
166-
170+
167171 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
169173 sessions. Also, the OpenTok Media Router can improve the quality of the user experience
170174 through dynamic traffic shaping. For more information, see
171175 http://www.tokbox.com/blog/mantis-next-generation-cloud-technology-for-webrtc and
172176 http://www.tokbox.com/blog/quality-of-experience-and-traffic-shaping-the-next-step-with-mantis.
173-
177+
174178 For peer-to-peer sessions, the session will attempt to transmit streams directly
175179 between clients. If clients cannot connect due to firewall restrictions, the session
176180 uses the OpenTok TURN server to relay audio-video streams.
177-
181+
178182 You will be billed for streamed minutes if you use the OpenTok Media Router or if the
179183 peer-to-peer session uses the OpenTok TURN server to relay streams. For information on
180184 pricing, see the OpenTok pricing page (http://www.tokbox.com/pricing).
181-
185+
182186 :param String location: An IP address that the OpenTok servers will use to
183187 situate the session in its global network. If you do not set a location hint,
184188 the OpenTok servers will be based on the first client connecting to the session.
185-
189+
186190 :rtype: The Session object. The session_id property of the object is the session ID.
187191 """
188192
189193 # build options
190194 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
195198 if location :
196199 # validate IP address
197200 try :
@@ -219,7 +222,7 @@ def create_session(self, location=None, p2p=False):
219222 raise AuthError ('Failed to create session (code=%s): %s' % (error .attributes ['code' ].value , error .firstChild .attributes ['message' ].value ))
220223
221224 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 )
223226 except Exception as e :
224227 raise OpenTokException ('Failed to generate session: %s' % str (e ))
225228
@@ -251,18 +254,18 @@ def archive_url(self, archive_id=None):
251254 def start_archive (self , session_id , ** kwargs ):
252255 """
253256 Starts archiving an OpenTok 2.0 session.
254-
257+
255258 Clients must be actively connected to the OpenTok session for you to successfully start
256259 recording an archive.
257-
260+
258261 You can only record one archive at a time for a given session. You can only record archives
259262 of OpenTok server-enabled sessions; you cannot archive peer-to-peer sessions.
260-
263+
261264 :param String session_id: The session ID of the OpenTok session to archive.
262265 :param String name: This is the name of the archive. You can use this name
263266 to identify the archive. It is a property of the Archive object, and it is a property
264267 of archive-related events in the OpenTok.js library.
265-
268+
266269 :rtype: The Archive object, which includes properties defining the archive,
267270 including the archive ID.
268271 """
@@ -286,12 +289,12 @@ def start_archive(self, session_id, **kwargs):
286289 def stop_archive (self , archive_id ):
287290 """
288291 Stops an OpenTok archive that is being recorded.
289-
292+
290293 Archives automatically stop recording after 90 minutes or when all clients have disconnected
291294 from the session being archived.
292-
295+
293296 @param [String] archive_id The archive ID of the archive you want to stop recording.
294-
297+
295298 :rtype: The Archive object corresponding to the archive being stopped.
296299 """
297300 response = requests .post (self .archive_url (archive_id ) + '/stop' , headers = self .archive_headers ())
@@ -310,7 +313,7 @@ def stop_archive(self, archive_id):
310313 def delete_archive (self , archive_id ):
311314 """
312315 Deletes an OpenTok archive.
313-
316+
314317 You can only delete an archive which has a status of "available" or "uploaded". Deleting an
315318 archive removes its record from the list of archives. For an "available" archive, it also
316319 removes the archive file, making it unavailable for download.
@@ -330,9 +333,9 @@ def delete_archive(self, archive_id):
330333
331334 def get_archive (self , archive_id ):
332335 """Gets an Archive object for the given archive ID.
333-
336+
334337 :param String archive_id: The archive ID.
335-
338+
336339 :rtype: The Archive object.
337340 """
338341 response = requests .get (self .archive_url (archive_id ), headers = self .archive_headers ())
@@ -349,13 +352,13 @@ def get_archive(self, archive_id):
349352 def get_archives (self , offset = None , count = None ):
350353 """Returns an ArchiveList, which is an array of archives that are completed and in-progress,
351354 for your API key.
352-
355+
353356 :param int: offset Optional. The index offset of the first archive. 0 is offset
354357 of the most recently started archive. 1 is the offset of the archive that started prior to
355358 the most recent archive. If you do not specify an offset, 0 is used.
356359 :param int: count Optional. The number of archives to be returned. The maximum
357360 number of archives returned is 1000.
358-
361+
359362 :rtype: An ArchiveList object, which is an array of Archive objects.
360363 """
361364 params = {}
@@ -377,4 +380,3 @@ def get_archives(self, offset=None, count=None):
377380
378381 def _sign_string (self , string , secret ):
379382 return hmac .new (secret .encode ('utf-8' ), string .encode ('utf-8' ), hashlib .sha1 ).hexdigest ()
380-
0 commit comments