Skip to content

opentok/Opentok-Python-SDK

Repository files navigation

OpenTok Python SDK

image

Tokbox is now known as Vonage

The OpenTok Python SDK lets you generate sessions and tokens for OpenTok applications, and archive OpenTok sessions.

Note!

This library is designed to work with the Tokbox/OpenTok platform, part of the Vonage Video API. If you are looking to use the Vonage Video API and are using the Vonage Customer Dashboard, you will want to install the Vonage Server SDK for Python, which includes support for the Vonage Video API.

Not sure which exact platform you are using? Take a look at this guide.

If you are using the Tokbox platform, do not worry! The Tokbox platform is not going away, and this library will continue to be updated. While we encourage customers to check out the new Unified platform, there is no rush to switch. Both platforms run the exact same infrastructure and capabilities. The main difference is a unified billing interface and easier access to Vonage's other CPaaS APIs.

If you are new to the Vonage Video API, head on over to the Vonage Customer Dashboard to sign up for a developer account and check out the Vonage Server SDK for Python.

Pip helps manage dependencies for Python projects using the PyPI index. Find more info here: http://www.pip-installer.org/en/latest/

Add the opentok package as a dependency in your project. The most common way is to add it to your requirements.txt file:

opentok>=3.0

Next, install the dependencies:

$ pip install -r requirements.txt

Usage

Initializing

Import the package at the top of any file where you will use it. At the very least you will need the Client class. Then initialize a Client instance with your own API Key and API Secret.

Creating Sessions

To create an OpenTok Session, use the opentok.create_session() method. There are optional keyword parameters for this method:

  • location which can be set to a string containing an IP address.
  • media_mode which is a String (defined by the MediaModes class). This determines whether the session will use the OpenTok Media Router or attempt to send streams directly between clients. A routed session is required for some OpenTok features (such as archiving).
  • archive_mode which specifies whether the session will be automatically archived (always) or not (manual).
  • archive_name which indicates the archive name for all the archives in auto archived session. A session that begins with archive mode 'always' will be using this archive name for all archives of that session. Passing 'archive_name' with archive mode 'manual' will cause an error response.
  • archive_resolution which indicates the archive resolution for all the archives in auto archived session. Valid values are '640x480', '480x640', '1280x720', '720x1280', '1920x1080' and '1080x1920'. A session that begins with archive mode 'always' will be using this resolution for all archives of that session. Passing 'archive_resolution' with archive mode 'manual' will cause an error response.
  • e2ee which is a boolean. This specifies whether to enable end-to-end encryption for the OpenTok session.

This method returns a Session object. Its session_id attribute is useful when saving to a persistent store (such as a database).

Generating Tokens

Once a Session is created, you can start generating Tokens for clients to use when connecting to it. You can generate a token either by calling the opentok.generate_token(session_id) method or by calling the session.generate_token() method on a Session instance after creating it. Both have a set of optional keyword parameters: role, expire_time, data, and initial_layout_class_list.

Working with Archives

You can only archive sessions that use the OpenTok Media Router (sessions with the media mode set to routed).

You can start the recording of an OpenTok Session using the opentok.start_archive(session_id) method. This method takes an optional keyword argument name to assign a name to the archive. This method will return an Archive instance. Note that you can only start an Archive on a Session that has clients connected.

You can also disable audio or video recording by setting the has_audio or has_video property of the options parameter to `false`:

By default, all streams are recorded to a single (composed) file. You can record the different streams in the session to individual files (instead of a single composed file) by setting the output_mode parameter of the opentok.start_archive() method OutputModes.individual.

To add an individual stream to an archive, use the opentok.add_archive_stream(archive_id, stream_id, has_audio, has_video) method:

To remove a stream from an archive, use the opentok.remove_archive_stream() method:

Composed archives (output_mode=OutputModes.composed) have an optional resolution parameter. If no value is supplied, the archive will use the default resolution, "640x480". You can set this to another resolution by setting the resolution parameter of the opentok.start_archive() method.

You can specify the following archive resolutions:

  • "640x480" (SD landscape, default resolution)
  • "480x640" (SD portrait)
  • "1280x720" (HD landscape)
  • "720x1280" (HD portrait)
  • "1920x1080" (FHD landscape)
  • "1080x1920" (FHD portrait)

Setting the resolution parameter while setting the output_mode parameter to OutputModes.individual results in an error.

You can enable multiple simultaneous archives by specifying a unique value for the multi_archive_tag parameter in the start_archive method.

You can stop the recording of a started Archive using the opentok.stop_archive(archive_id)method. You can also do this using the archive.stop() method of an Archive instance.

To get an Archive instance (and all the information about it) from an archive ID, use the opentok.get_archive(archive_id) method.

To delete an Archive, you can call the opentok.delete_archive(archive_id) method or the archive.delete() method of an Archive instance.

You can also get a list of all the Archives you've created (up to 1000) with your API Key. This is done using the opentok.list_archives() method. There are three optional keyword parameters: count, offset and session_id; they can help you paginate through the results and filter by session ID. This method returns an instance of the ArchiveList class.

Note that you can also create an automatically archived session, by passing in ArchiveModes.always as the archive_mode parameter when you call the opentok.create_session() method (see "Creating Sessions," above).

For composed archives, you can change the layout dynamically, using the opentok.set_archive_layout(archive_id, type, stylesheet) method:

Setting the layout of composed archives is optional. By default, composed archives use the best fit layout. Other valid values are: custom, horizontalPresentation, pip and verticalPresentation. If you specify a custom layout type, set the stylesheet parameter:

For other layout types, do not set the stylesheet property. For more information see Customizing the video layout for composed archives.

For more information on archiving, see the OpenTok archiving programming guide.

Sending Signals

Once a Session is created, you can send signals to everyone in the session or to a specific connection. You can send a signal by calling the signal(session_id, payload) method of the Client class. The payload parameter is a dictionary used to set the type, data fields. Ỳou can also call the method with the parameter connection_id to send a signal to a specific connection signal(session_id, data, connection_id).

Working with Streams

You can get information about a stream by calling the get_stream(session_id, stream_id) method of the Client class.

The method returns a Stream object that contains information of an OpenTok stream:

  • id: The stream ID
  • videoType: "camera" or "screen"
  • name: The stream name (if one was set when the client published the stream)
  • layoutClassList: It's an array of the layout classes for the stream

Also, you can get information about all the streams in a session by calling the list_streams(session_id) method of the Client class.

The method returns a StreamList object that contains a list of all the streams

You can change the layout classes for streams in a session by calling the set_stream_class_lists(session_id, stream_list) method of the Client class.

The layout classes define how the stream is displayed in the layout of a composed OpenTok archive.

For more information see Changing the composed archive layout classes for an OpenTok stream.

Force Disconnect

Your application server can disconnect a client from an OpenTok session by calling the force_disconnect(session_id, connection_id) method of the Client class, or the force_disconnect(connection_id) method of the Session class.

Working with SIP Interconnect

You can connect your SIP platform to an OpenTok session, the audio from your end of the SIP call is added to the OpenTok session as an audio-only stream. The OpenTok Media Router mixes audio from other streams in the session and sends the mixed audio to your SIP endpoint.

For more information, including technical details and security considerations, see the OpenTok SIP interconnect developer guide.

Working with Broadcasts

OpenTok broadcast lets you share live OpenTok sessions with many viewers.

You can use the opentok.start_broadcast() method to start a live stream for an OpenTok session. This broadcasts the session to HLS (HTTP live streaming) or to RTMP streams.

To successfully start broadcasting a session, at least one client must be connected to the session.

The live streaming broadcast can target one HLS endpoint and up to five RTMP servers simulteneously for a session. You can only start live streaming for sessions that use the OpenTok Media Router; you cannot use live streaming with sessions that have the media mode set to relayed.

You can specify the following broadcast resolutions:

  • "640x480" (SD landscape, default resolution)
  • "480x640" (SD portrait)
  • "1280x720" (HD landscape)
  • "720x1280" (HD portrait)
  • "1920x1080" (FHD landscape)
  • "1080x1920" (FHD portrait)

You can specify a maximum bitrate between 100000 and 6000000.

To enable multiple simultaneous broadcasts on the same session, specify a unique value for the multiBroadcastTag parameter in options when calling the opentok.start_broadcast method.

You can broadcast only audio, or only video, for a stream by setting hasAudio or hasVideo to False as required. These fields are True by default.

You can stop a started Broadcast using the opentok.stop_broadcast(broadcast_id) method.

You can get details on a broadcast that is in-progress using the method opentok.get_broadcast(broadcast_id).

You can dynamically change the layout type of a live streaming broadcast.

You can add streams to a broadcast using the opentok.add_broadcast_stream() method:

Conversely, streams can be removed from a broadcast with the opentok.remove_broadcast_stream() method.

For more information about OpenTok live streaming broadcasts, see the Broadcast developer guide.

Connecting audio to a WebSocket

You can send audio to a WebSocket with the opentok.connect_audio_to_websocket method. For more information, see the Audio Connector developer guide.

Additionally, you can list only the specific streams you want to send to the WebSocket, and/or the additional headers that are sent, by adding these fields to the websocket_options object.

Using the Live Captions API

You can enable live captioning for an OpenTok session with the opentok.start_captions method. For more information, see the Live Captions API developer guide <https://tokbox.com/developer/guides/live-captions/>.

You can also specify optional parameters, as shown below.

You can stop an ongoing live captioning session by calling the opentok.stop_captions method.

Configuring Timeout

Timeout is passed in the Client constructor:

self.timeout = timeout

In order to configure timeout, first create an instance:

opentok = Client(...., timeout=value)

And then proceed to change the value with

opentok.timeout = value

Muting streams

You can mute all streams in a session using the opentok.mute_all() method:

In addition to existing streams, any streams that are published after the call to this method are published with audio muted. You can remove the mute state of a session by calling the opentok.disableForceMute() method:

After calling the opentok.disableForceMute() method, new streams that are published to the session will not be muted.

You can mute a single stream using the opentok.mute_stream() method:

DTMF

You can send dual-tone multi-frequency (DTMF) digits to SIP endpoints. You can play DTMF tones to all clients connected to session or to a specific connection:

Appending to the User Agent

You can append a string to the user agent that is sent with requests:

Samples

There are two sample applications included in this repository. To get going as fast as possible, clone the whole repository and follow the Walkthroughs:

Documentation

Reference documentation is available at https://tokbox.com/developer/sdks/python/reference/.

Requirements

You need an OpenTok API key and API secret, which you can obtain at https://dashboard.tokbox.com/

The OpenTok Python SDK requires Python 3.5 or higher

Release Notes

See the Releases page for details about each release.

Important changes since v2.2

Changes in v2.2.1:

The default setting for the create_session() method is to create a session with the media mode set to relayed. In previous versions of the SDK, the default setting was to use the OpenTok Media Router (media mode set to routed). In a relayed session, clients will attempt to send streams directly between each other (peer-to-peer); if clients cannot connect due to firewall restrictions, the session uses the OpenTok TURN server to relay audio-video streams.

Changes in v2.2.0:

This version of the SDK includes support for working with OpenTok archives.

The Client.create_session() method now includes a media_mode parameter, instead of a p2p parameter.

Changes in v3.X.X:

This version of the SDK includes significant improvements such as top level entity naming, where the Opentok class is now Client. We also implemented a standardised logging module, improved naming conventions and JWT generation to make developer experience more rewarding.

For details, see the reference documentation at https://tokbox.com/developer/sdks/python/reference/.

Development and Contributing

Interested in contributing? We ❤️ pull requests! See the Development and Contribution guidelines.

Getting Help

We love to hear from you so if you have questions, comments or find a bug in the project, let us know! You can either: