\OpenTokOpenTok

Contains methods for creating OpenTok sessions, generating tokens, and working with archives.

To create a new OpenTok object, call the OpenTok() constructor with your OpenTok API key and the API secret for your Vonage Video API account. Do not publicly share your API secret. You will use it with the OpenTok() constructor (only on your web server) to create OpenTok sessions.

Be sure to include the entire OpenTok server SDK on your web server.

Summary

Methods
Properties
Constants
generateToken()
createSession()
startArchive()
stopArchive()
getArchive()
deleteArchive()
listArchives()
setArchiveLayout()
setStreamClassLists()
forceDisconnect()
startBroadcast()
stopBroadcast()
getBroadcast()
updateBroadcastLayout()
updateStream()
getStream()
listStreams()
dial()
signal()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

generateToken()

generateToken(string  $sessionId, array  $options = array()) : string

Creates a token for connecting to an OpenTok session. In order to authenticate a user connecting to an OpenTok session, the client passes a token when connecting to the session.

For testing, you generate tokens or by logging in to your Vonage Video API account.

Parameters

string $sessionId

The session ID corresponding to the session to which the user will connect.

array $options

This array defines options for the token. This array includes the following keys, all of which are optional:

  • 'role' (string) — One of the constants defined in the Role class. The default role is publisher.
  • 'expireTime' (int) — The timestamp for when the token expires, in milliseconds since the Unix epoch. The default expiration time is 24 hours after the token creation time. The maximum expiration time is 30 days after the token creation time.
  • 'data' (string) — A string containing connection metadata describing the end-user. For example, you can pass the user ID, name, or other data describing the end-user. The length of the string is limited to 1000 characters. This data cannot be updated once it is set.
  • initialLayoutClassList (array) — An array of class names (strings) to be used as the initial layout classes for streams published by the client. Layout classes are used in customizing the layout of videos in live streaming broadcasts and composed archives.

Returns

string —

The token string.

createSession()

createSession(array  $options = array()) : \OpenTok\Session

Creates a new OpenTok session and returns the session ID, which uniquely identifies the session.

For example, when using the OpenTok JavaScript library, use the session ID when calling the OT.initSession() method (to initialize an OpenTok session).

OpenTok sessions do not expire. However, authentication tokens do expire (see the generateToken() method). Also note that sessions cannot explicitly be destroyed.

A session ID string can be up to 255 characters long.

Calling this method results in an OpenTokException in the event of an error. Check the error message for details.

You can also create a session by logging in to your Vonage Video API account.

Parameters

array $options

(Optional) This array defines options for the session. The array includes the following keys (all of which are optional):

  • 'archiveMode' (ArchiveMode) — Whether the session is automatically archived (ArchiveMode::ALWAYS) or not (ArchiveMode::MANUAL). By default, the setting is ArchiveMode.MANUAL, and you must call the OpenTok->startArchive() method to start archiving. To archive the session (either automatically or not), you must set the mediaMode key to MediaMode::ROUTED.
  • 'location' (String) — An IP address that the OpenTok servers will use to situate the session in its global network. If you do not set a location hint, the OpenTok servers will be based on the first client connecting to the session.
  • 'mediaMode' (MediaMode) — Whether the session will transmit streams using the OpenTok Media Router (MediaMode.ROUTED) or not (MediaMode.RELAYED). By default, the mediaMode property is set to MediaMode.RELAYED.

    With the mediaMode parameter set to MediaMode.RELAYED, the session will attempt to transmit streams directly between clients. If clients cannot connect due to firewall restrictions, the session uses the OpenTok TURN server to relay audio-video streams.

    The OpenTok Media Router provides the following benefits:

    • The OpenTok Media Router can decrease bandwidth usage in multiparty sessions. (When the mediaMode parameter is set to MediaMode.ROUTED, each client must send a separate audio-video stream to each client subscribing to it.)
    • The OpenTok Media Router can improve the quality of the user experience through recovery. With these features, if a client's connectivity degrades to a degree that it does not support video for a stream it's subscribing to, the video is dropped on that client (without affecting other clients), and the client receives audio only. If the client's connectivity improves, the video returns.
    • The OpenTok Media Router supports the archiving feature, which lets you record, save, and retrieve OpenTok sessions.

Returns

\OpenTok\Session

A Session object representing the new session. Call the getSessionId() method of this object to get the session ID. For example, when using the OpenTok.js library, use this session ID when calling the OT.initSession() method.

startArchive()

startArchive(String  $sessionId, array  $options = array()) : \OpenTok\Archive

Starts archiving an OpenTok session.

Clients must be actively connected to the OpenTok session for you to successfully start recording an archive.

You can only record one archive at a time for a given session. You can only record archives of sessions that use the OpenTok Media Router (sessions with the media mode set to routed); you cannot archive sessions with the media mode set to relayed.

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

Parameters

String $sessionId

The session ID of the OpenTok session to archive.

array $options

(Optional) This array defines options for the archive. The array includes the following keys (all of which are optional):

  • 'name' (String) — The name of the archive. You can use this name to identify the archive. It is a property of the Archive object, and it is a property of archive-related events in the OpenTok client SDKs.
  • 'hasVideo' (Boolean) — Whether the archive will record video (true, the default) or not (false). If you set both hasAudio and hasVideo to false, the call to the startArchive() method results in an error.
  • 'hasAudio' (Boolean) — Whether the archive will record audio (true, the default) or not (false). If you set both hasAudio and hasVideo to false, the call to the startArchive() method results in an error.
  • 'outputMode' (OutputMode) — Whether all streams in the archive are recorded to a single file (OutputMode::COMPOSED, the default) or to individual files (OutputMode::INDIVIDUAL).

    Returns

    \OpenTok\Archive

    The Archive object, which includes properties defining the archive, including the archive ID.

    stopArchive()

    stopArchive(String  $archiveId) : \OpenTok\Archive

    Stops an OpenTok archive that is being recorded.

    Archives automatically stop recording after 120 minutes or when all clients have disconnected from the session being archived.

    Parameters

    String $archiveId

    The archive ID of the archive you want to stop recording.

    Returns

    \OpenTok\Archive

    The Archive object corresponding to the archive being stopped.

    getArchive()

    getArchive(String  $archiveId) : \OpenTok\Archive

    Gets an Archive object for the given archive ID.

    Parameters

    String $archiveId

    The archive ID.

    Throws

    \OpenTok\ArchiveException

    There is no archive with the specified ID.

    \OpenTok\Exception\InvalidArgumentException

    The archive ID provided is null or an empty string.

    Returns

    \OpenTok\Archive

    The Archive object.

    deleteArchive()

    deleteArchive(String  $archiveId) : Boolean

    Deletes an OpenTok archive.

    You can only delete an archive which has a status of "available", "uploaded", or "deleted". Deleting an archive removes its record from the list of archives. For an "available" archive, it also removes the archive file, making it unavailable for download. For a "deleted" archive, the archive remains deleted.

    Parameters

    String $archiveId

    The archive ID of the archive you want to delete.

    Throws

    \OpenTok\ArchiveException

    There archive status is not "available", "updated", or "deleted".

    Returns

    Boolean —

    Returns true on success.

    listArchives()

    listArchives(integer  $offset, integer  $count = null, string  $sessionId = null) : \OpenTok\ArchiveList

    Returns an ArchiveList. The <code>items()</code> method of this object returns a list of archives that are completed and in-progress, for your API key.

    Parameters

    integer $offset

    Optional. The index offset of the first archive. 0 is offset of the most recently started archive. 1 is the offset of the archive that started prior to the most recent archive. If you do not specify an offset, 0 is used.

    integer $count

    Optional. The number of archives to be returned. The maximum number of archives returned is 1000.

    string $sessionId

    Optional. The OpenTok session Id for which you want to retrieve Archives for. If no session Id is specified, the method will return archives from all sessions created with the API key.

    Returns

    \OpenTok\ArchiveList

    An ArchiveList object. Call the items() method of the ArchiveList object to return an array of Archive objects.

    setArchiveLayout()

    setArchiveLayout(string  $archiveId,   $layoutType) 

    Updates the stream layout in an OpenTok Archive.

    Parameters

    string $archiveId

    The OpenTok archive ID.

    $layoutType

    setStreamClassLists()

    setStreamClassLists(string  $sessionId, array  $classListArray = array()) 

    Sets the layout class list for streams in a session. Layout classes are used in the layout for composed archives and live streaming broadcasts. For more information, see <a href="https://tokbox.com/developer/guides/archiving/layout-control.html">Customizing the video layout for composed archives</a> and <a href="https://tokbox.com/developer/guides/broadcast/live-streaming/#configuring-video-layout-for-opentok-live-streaming-broadcasts">Configuring video layout for OpenTok live streaming broadcasts</a>.

    Parameters

    string $sessionId

    The session ID of the session the streams belong to.

    array $classListArray

    The connectionId of the connection in a session.

    forceDisconnect()

    forceDisconnect(string  $sessionId, string  $connectionId) 

    Disconnects a specific client from an OpenTok session.

    Parameters

    string $sessionId

    The OpenTok session ID that the client is connected to.

    string $connectionId

    The connection ID of the connection in the session.

    startBroadcast()

    startBroadcast(String  $sessionId, Array  $options = array()) : \OpenTok\Broadcast

    Starts a live streaming broadcast of an OpenTok session.

    Parameters

    String $sessionId

    The session ID of the session to be broadcast.

    Array $options

    (Optional) An array with options for the broadcast. This array has the following properties:

    Returns

    \OpenTok\Broadcast

    An object with properties defining the broadcast.

    stopBroadcast()

    stopBroadcast(String  $broadcastId) 

    Stops a broadcast.

    Parameters

    String $broadcastId

    The ID of the broadcast.

    getBroadcast()

    getBroadcast(String  $broadcastId) : \OpenTok\Broadcast

    Gets information about an OpenTok broadcast.

    Parameters

    String $broadcastId

    The ID of the broadcast.

    Returns

    \OpenTok\Broadcast

    An object with properties defining the broadcast.

    updateStream()

    updateStream(string  $sessionId, string  $streamId, array  $properties = array()) 

    Sets the layout class list for a stream. Layout classes are used in the layout for composed archives and live streaming broadcasts.

    For more information, see Customizing the video layout for composed archives and Configuring video layout for OpenTok live streaming broadcasts.

    You can set the initial layout class list for streams published by a client when you generate the token used by the client to connect to the session. See the OpenTok->generateToken() method.

    Parameters

    string $sessionId

    The session ID of the session the stream belongs to.

    string $streamId

    The stream ID.

    array $properties

    An array containing one property: $layoutClassList. This property is an array of class names (strings) to apply to the stream. Set $layoutClassList to an empty array to clear the layout class list for a stream. For example, this code sets the stream to use two classes:

    $streamProperties = array(
      '$layoutClassList' => array('bottom', 'right')
    );
    $opentok->updateStream($sessionId, $streamId, $streamProperties);
    

    getStream()

    getStream(String  $sessionId, String  $streamId) : \OpenTok\Stream

    Gets an Stream object, providing information on a given stream.

    Parameters

    String $sessionId

    The session ID for the OpenTok session containing the stream.

    String $streamId

    The stream ID.

    Returns

    \OpenTok\Stream

    The Stream object.

    listStreams()

    listStreams(String  $sessionId) : \OpenTok\StreamList

    Returns a StreamList Object for the given session ID.

    Parameters

    String $sessionId

    The session ID.

    Returns

    \OpenTok\StreamList

    A StreamList object. Call the items() method of the StreamList object to return an array of Stream objects.

    dial()

    dial(string  $sessionId, string  $token, string  $sipUri, array  $options = array()) : \OpenTok\SipCall

    Initiates an outgoing SIP call.

    For more information, see the OpenTok SIP Interconnect developer guide.

    Parameters

    string $sessionId

    The ID of the OpenTok session that the participant being called will join.

    string $token

    The OpenTok token to be used for the participant being called. You can add token data to identify that the participant is on a SIP endpoint or for other identifying data, such as phone numbers. Generate a token using the OpenTok->generateToken() or Session->generateToken() method.

    string $sipUri

    The SIP URI to be used as destination of the SIP Call initiated from OpenTok to the Third Party SIP Platform.

    If the URI contains a transport=tlsheader, the negotiation between Vonage and the SIP endpoint will be done securely. Note that this will only apply to the negotiation itself, and not to the transmission of audio. To have audio transmission be encrypted, see the "secure" property of the options parameter.

    This is an example of secure call negotiation: "sip:access@thirparty.com;transport=tls".

    This is an example of insecure call negotiation: "sip:access@thirparty.com".

    array $options

    This array defines options for the token. It includes the following keys, all of which are optional:

    • 'headers' (array) — Headers​: Custom Headers to be added to the SIP INVITE request initiated from OpenTok to the Third Party SIP Platform.
    • 'auth' (array) — Auth​: Username and Password to be used in the SIP INVITE request for HTTP Digest authentication in case this is required by the Third Party SIP Platform.
      • 'username' (string) — The username to be used in the the SIP INVITE​ request for HTTP digest authentication (if one is required).
      • 'password' (string) — The password to be used in the the SIP INVITE​ request for HTTP digest authentication.
    • 'secure' (Boolean) — Indicates whether the media must be transmitted encrypted (true, the default) or not (false).
    • 'from' (string) — The number or string that will be sent to the final SIP number as the caller. It must be a string in the form of "from@example.com", where from can be a string or a number. If from is set to a number (for example, "14155550101@example.com"), it will show up as the incoming number on PSTN phones. If from is undefined or set to a string (for example, "joe@example.com"), +00000000 will show up as the incoming number on PSTN phones.

    Returns

    \OpenTok\SipCall

    An object contains the OpenTok connection ID and stream ID for the SIP call's connection in the OpenTok session. You can use the connection ID to terminate the SIP call, using the OpenTok->forceDisconnect() method.

    signal()

    signal(string  $sessionId, array  $payload, string  $connectionId = null) 

    Sends a signal to clients (or a specific client) connected to an OpenTok session.

    Parameters

    string $sessionId

    The OpenTok session ID where the signal will be sent.

    array $payload

    This array defines the payload for the signal. This array includes the following keys, of which type is optional:

    • 'data' (string) — The data string for the signal. You can send a maximum of 8kB.
    • 'type' (string) — (Optional) The type string for the signal. You can send a maximum of 128 characters, and only the following characters are allowed: A-Z, a-z, numbers (0-9), '-', '_', and '~'.
    string $connectionId

    An optional parameter used to send the signal to a specific connection in a session.