public class OpenTok
extends java.lang.Object
To create a new OpenTok object, call the OpenTok constructor with your OpenTok API key and the API secret for your TokBox 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.
Modifier and Type | Class and Description |
---|---|
static class |
OpenTok.Builder |
Constructor and Description |
---|
OpenTok(int apiKey,
java.lang.String apiSecret)
Creates an OpenTok object.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
Session |
createSession()
Creates an OpenTok session with the default settings:
|
Session |
createSession(SessionProperties properties)
Creates a new OpenTok session.
|
void |
deleteArchive(java.lang.String archiveId)
Deletes an OpenTok archive.
|
java.lang.String |
generateToken(java.lang.String sessionId)
Creates a token for connecting to an OpenTok session, using the default settings.
|
java.lang.String |
generateToken(java.lang.String sessionId,
TokenOptions tokenOptions)
Creates a token for connecting to an OpenTok session.
|
Archive |
getArchive(java.lang.String archiveId)
Gets an
Archive object for the given archive ID. |
ArchiveList |
listArchives()
Returns a List of
Archive objects, representing archives that are both
both completed and in-progress, for your API key. |
ArchiveList |
listArchives(int offset,
int count)
Returns a List of
Archive objects, representing archives that are both
both completed and in-progress, for your API key. |
ArchiveList |
listArchives(java.lang.String sessionId)
Returns a List of
Archive objects, representing archives that are both both completed and in-progress,
for your API key. |
Archive |
startArchive(java.lang.String sessionId) |
Archive |
startArchive(java.lang.String sessionId,
ArchiveProperties properties)
Starts archiving an OpenTok session.
|
Archive |
startArchive(java.lang.String sessionId,
java.lang.String name) |
Archive |
stopArchive(java.lang.String archiveId)
Stops an OpenTok archive that is being recorded.
|
public OpenTok(int apiKey, java.lang.String apiSecret)
apiKey
- Your OpenTok API key. (See your TokBox account page.)apiSecret
- Your OpenTok API secret. (See your TokBox account page.)public java.lang.String generateToken(java.lang.String sessionId, TokenOptions tokenOptions) throws OpenTokException
The following example shows how to obtain a token that has a role of "subscriber" and that has a connection metadata string:
import com.opentok.Role; import com.opentok.TokenOptions; class Test { public static void main(String argv[]) throws OpenTokException { int API_KEY = 0; // Replace with your OpenTok API key (see https://tokbox.com/account). String API_SECRET = ""; // Replace with your OpenTok API secret. OpenTok sdk = new OpenTok(API_KEY, API_SECRET); //Generate a basic session. Or you could use an existing session ID. String sessionId = System.out.println(sdk.createSession()); // Replace with meaningful metadata for the connection. String connectionMetadata = "username=Bob,userLevel=4"; // Use the Role value appropriate for the user. String role = Role.SUBSCRIBER; // Generate a token: TokenOptions options = new TokenOptions.Buider().role(role).data(connectionMetadata).build(); String token = sdk.generateToken(sessionId, options); System.out.println(token); } }
For testing, you can also generate tokens by logging in to your TokBox account.
sessionId
- The session ID corresponding to the session to which the user will connect.tokenOptions
- This TokenOptions object defines options for the token.
These include the following:
OpenTokException
public java.lang.String generateToken(java.lang.String sessionId) throws OpenTokException
The following example shows how to generate a token that has the default settings:
import com.opentok.OpenTok; class Test { public static void main(String argv[]) throws OpenTokException { int API_KEY = 0; // Replace with your OpenTok API key (see https://tokbox.com/account). String API_SECRET = ""; // Replace with your OpenTok API secret. OpenTok sdk = new OpenTok(API_KEY, API_SECRET); //Generate a basic session. Or you could use an existing session ID. String sessionId = System.out.println(sdk.createSession().getSessionId()); String token = sdk.generateToken(sessionId); System.out.println(token); } }
sessionId
- The session ID corresponding to the session to which the user will connect.OpenTokException
generateToken(String, TokenOptions)
public Session createSession(SessionProperties properties) throws OpenTokException
For example, when using the OpenTok.js 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(String, TokenOptions)
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.
The following code creates a session that attempts to send streams directly between clients (falling back to use the OpenTok TURN server to relay streams if the clients cannot connect):
import com.opentok.MediaMode; import com.opentok.OpenTok; import com.opentok.Session; import com.opentok.SessionProperties; class Test { public static void main(String argv[]) throws OpenTokException { int API_KEY = 0; // Replace with your OpenTok API key. String API_SECRET = ""; // Replace with your OpenTok API secret. OpenTok sdk = new OpenTok(API_KEY, API_SECRET); SessionProperties sp = new SessionProperties().Builder() .mediaMode(MediaMode.RELAYED).build(); Session session = sdk.createSession(sp); System.out.println(session.getSessionId()); } }You can also create a session using the OpenTok REST API or or by logging in to your TokBox account.
properties
- This SessionProperties object defines options for the session.
These include the following:
getSessionId()
method of the Session object to get the session ID, which uniquely identifies the
session. You will use this session ID in the client SDKs to identify the session.OpenTokException
public Session createSession() throws OpenTokException
The following example creates a session that uses the default settings:
import com.opentok.OpenTok; import com.opentok.SessionProperties; class Test { public static void main(String argv[]) throws OpenTokException { int API_KEY = 0; // Replace with your OpenTok API key. String API_SECRET = ""; // Replace with your OpenTok API secret. OpenTok sdk = new OpenTok(API_KEY, API_SECRET); String sessionId = sdk.createSession(); System.out.println(sessionId); } }
getSessionId()
method of the Session object to get the session ID, which uniquely identifies the
session. You will use this session ID in the client SDKs to identify the session.OpenTokException
createSession(SessionProperties)
public Archive getArchive(java.lang.String archiveId) throws OpenTokException
Archive
object for the given archive ID.archiveId
- The archive ID.Archive
object.OpenTokException
public ArchiveList listArchives() throws OpenTokException
Archive
objects, representing archives that are both
both completed and in-progress, for your API key. This list is limited to 1000 archives
starting with the first archive recorded. For a specific range of archives, call
listArchives(int offset, int count)
.Archive
objects.OpenTokException
public ArchiveList listArchives(int offset, int count) throws OpenTokException
Archive
objects, representing archives that are both
both completed and in-progress, for your API key.offset
- 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.count
- The number of archives to be returned. The maximum number of archives returned
is 1000.Archive
objects.OpenTokException
public ArchiveList listArchives(java.lang.String sessionId) throws RequestException
Archive
objects, representing archives that are both both completed and in-progress,
for your API key.sessionId
- The sessionId for which archives should be retrieved.Archive
objects.RequestException
public Archive startArchive(java.lang.String sessionId, ArchiveProperties properties) throws OpenTokException
startArchive()
method
lets you disable audio or video recording.
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.
sessionId
- The session ID of the OpenTok session to archive.properties
- This ArchiveProperties object defines options for the archive.OpenTokException
public Archive startArchive(java.lang.String sessionId) throws OpenTokException
OpenTokException
public Archive startArchive(java.lang.String sessionId, java.lang.String name) throws OpenTokException
OpenTokException
public Archive stopArchive(java.lang.String archiveId) throws OpenTokException
Archives automatically stop recording after 120 minutes or when all clients have disconnected from the session being archived.
archiveId
- The archive ID of the archive you want to stop recording.OpenTokException
public void deleteArchive(java.lang.String archiveId) throws OpenTokException
You can only delete an archive which has a status of "available" or "uploaded". 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.
archiveId
- The archive ID of the archive you want to delete.OpenTokException
public void close()