@ThreadSafe public class Session extends Object
When a connection is established with a cast device, communication is only
possible with the "device itself", offering only a very limited interaction.
To make the cast device "do anything", an application must be running (which
can be launched with a command to the device), and a "connection within the
connection" must be made to communicate with the specific remote application.
A Session encapsulates this "inner connection".
| Modifier and Type | Class and Description |
|---|---|
static interface |
Session.SessionClosedListener
An interface defining a callback which is invoked when a
Session
is closed. |
| Modifier and Type | Field and Description |
|---|---|
protected Channel |
channel
|
protected String |
destinationId
The destination ID used by this
Session |
protected String |
id
The session ID used by this
Session |
protected Session.SessionClosedListener |
listener
The listener to invoke if the session is closed
|
protected Object |
listenerLock
The synchronization object used to protect
listener |
protected String |
sourceId
The source ID used by this
Session |
| Constructor and Description |
|---|
Session(String sourceId,
String sessionId,
String destinationId,
Channel channel)
Creates a new instance using the specified parameters.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
close()
Closes this
Session. |
String |
getDestinationId() |
String |
getId() |
MediaStatus |
getMediaStatus()
Requests an updated
MediaStatus from the remote application. |
MediaStatus |
getMediaStatus(long responseTimeout)
Requests an updated
MediaStatus from the remote application. |
Session.SessionClosedListener |
getSessionClosedListener() |
String |
getSourceId() |
boolean |
isClosed() |
MediaStatus |
load(Media.MediaBuilder mediaBuilder,
Boolean autoplay,
Double currentTime,
boolean synchronous)
Asks the remote application to load the resulting
Media created
from the specified Media.MediaBuilder using the specified parameters
and Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. |
MediaStatus |
load(Media.MediaBuilder mediaBuilder,
Boolean autoplay,
Double currentTime,
boolean synchronous,
long responseTimeout)
Asks the remote application to load the resulting
Media created
from the specified Media.MediaBuilder using the specified parameters. |
MediaStatus |
load(Media media,
Boolean autoplay,
Double currentTime,
boolean synchronous)
Asks the remote application to load the specified
Media using the
specified parameters and Channel.DEFAULT_RESPONSE_TIMEOUT as the
timeout value. |
MediaStatus |
load(Media media,
Boolean autoplay,
Double currentTime,
boolean synchronous,
long responseTimeout)
Asks the remote application to load the specified
Media
using the specified parameters. |
MediaStatus |
load(Media media,
List<Integer> activeTrackIds,
Boolean autoplay,
Double currentTime,
Double playbackRate,
boolean synchronous)
Asks the remote application to load the specified
Media using the
specified parameters. |
MediaStatus |
load(Media media,
List<Integer> activeTrackIds,
Boolean autoplay,
Double currentTime,
Double playbackRate,
boolean synchronous,
long responseTimeout)
Asks the remote application to load the specified
Media using the
specified parameters and Channel.DEFAULT_RESPONSE_TIMEOUT as the
timeout value. |
MediaStatus |
load(Media media,
List<Integer> activeTrackIds,
Boolean autoplay,
String credentials,
String credentialsType,
Double currentTime,
Map<String,Object> customData,
LoadOptions loadOptions,
Double playbackRate,
QueueData queueData,
boolean synchronous)
Asks the remote application to load the specified
Media or
QueueDatas using the specified parameters, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. |
MediaStatus |
load(Media media,
List<Integer> activeTrackIds,
Boolean autoplay,
String credentials,
String credentialsType,
Double currentTime,
Map<String,Object> customData,
LoadOptions loadOptions,
Double playbackRate,
QueueData queueData,
boolean synchronous,
long responseTimeout)
|
MediaStatus |
load(StandardRequest.Load loadRequest,
boolean synchronous)
Asks the remote application to execute the specified
StandardRequest.Load
request, using Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout
value. |
MediaStatus |
load(StandardRequest.Load loadRequest,
boolean synchronous,
long responseTimeout)
Asks the remote application to execute the specified
StandardRequest.Load
request. |
MediaStatus |
pause(int mediaSessionId,
boolean synchronous)
Asks the remote application to pause playback of the media referenced by
the specified media session ID, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. |
MediaStatus |
pause(int mediaSessionId,
boolean synchronous,
long responseTimeout)
Asks the remote application to pause playback of the media referenced by
the specified media session ID.
|
MediaStatus |
play(int mediaSessionId,
boolean synchronous)
Asks the remote application to start playing the media referenced by the
specified media session ID, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. |
MediaStatus |
play(int mediaSessionId,
boolean synchronous,
long responseTimeout)
Asks the remote application to start playing the media referenced by the
specified media session ID.
|
MediaStatus |
seek(int mediaSessionId,
double currentTime,
StandardRequest.ResumeState resumeState,
boolean synchronous)
Asks the remote application to move the playback position of the media
referenced by the specified media session ID to the specified position,
using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. |
MediaStatus |
seek(int mediaSessionId,
double currentTime,
StandardRequest.ResumeState resumeState,
boolean synchronous,
long responseTimeout)
Asks the remote application to move the playback position of the media
referenced by the specified media session ID to the specified position.
|
<T extends Response> |
sendGenericRequest(String namespace,
Request request,
Class<T> responseClass)
|
void |
setSessionClosedListener(Session.SessionClosedListener listener)
Sets the listener that is to be invoked if the session is closed.
|
MediaStatus |
setVolume(int mediaSessionId,
MediaVolume volume,
boolean synchronous)
Asks the remote application to change the volume level or mute state of
the stream of the specified media session.
|
MediaStatus |
stop(int mediaSessionId,
boolean synchronous)
Asks the remote application to stop playback and unload the media
referenced by the specified media session ID, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. |
MediaStatus |
stop(int mediaSessionId,
boolean synchronous,
long responseTimeout)
Asks the remote application to stop playback and unload the media
referenced by the specified media session ID.
|
String |
toString() |
@Nonnull protected final Object listenerLock
listener@Nullable protected Session.SessionClosedListener listener
public Session(@Nonnull String sourceId, @Nonnull String sessionId, @Nonnull String destinationId, @Nonnull Channel channel)
sourceId - the source ID to use.sessionId - the session ID to use.destinationId - the destination ID to use.channel - the Channel to use.@Nonnull public String getDestinationId()
Session.public boolean isClosed()
true if this Session is closed, false
otherwise.public boolean close()
throws IOException
Session.true if this Session was closed, false if
it already was.IOException - If an error occurs during the operation.@Nullable public Session.SessionClosedListener getSessionClosedListener()
public void setSessionClosedListener(@Nullable Session.SessionClosedListener listener)
listener - The Session.SessionClosedListener implementation to
invoke.@Nullable public MediaStatus load(@Nonnull StandardRequest.Load loadRequest, boolean synchronous) throws IOException
StandardRequest.Load
request, using Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout
value.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
loadRequest - the StandardRequest.Load request to send.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IllegalArgumentException - If session or
loadRequest is null.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus load(@Nonnull StandardRequest.Load loadRequest, boolean synchronous, long responseTimeout) throws IOException
StandardRequest.Load
request.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
loadRequest - the StandardRequest.Load request to send.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
will be used.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IllegalArgumentException - If session or
loadRequest is null.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus load(@Nonnull Media.MediaBuilder mediaBuilder, @Nullable Boolean autoplay, @Nullable Double currentTime, boolean synchronous) throws IOException
Media created
from the specified Media.MediaBuilder using the specified parameters
and Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaBuilder - the Media.MediaBuilder to use to create the
Media to load.autoplay - true to ask the remote application to start
playback as soon as the Media has been loaded,
false to ask it to transition to a paused state after
loading.currentTime - the position in seconds where playback are to be
started in the loaded Media.synchronous - true to make this call blocking until a
response is received or times out, false to make it
return immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IllegalArgumentException - If mediaBuilder is null.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus load(@Nonnull Media.MediaBuilder mediaBuilder, @Nullable Boolean autoplay, @Nullable Double currentTime, boolean synchronous, long responseTimeout) throws IOException
Media created
from the specified Media.MediaBuilder using the specified parameters.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaBuilder - the Media.MediaBuilder to use to create the
Media to load.autoplay - true to ask the remote application to start
playback as soon as the Media has been loaded,
false to ask it to transition to a paused state after
loading.currentTime - the position in seconds where playback are to be
started in the loaded Media.synchronous - true to make this call blocking until a
response is received or times out, false to make it
return immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
will be used.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IllegalArgumentException - If mediaBuilder is null.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus load(@Nonnull Media media, @Nullable Boolean autoplay, @Nullable Double currentTime, boolean synchronous) throws IOException
Media using the
specified parameters and Channel.DEFAULT_RESPONSE_TIMEOUT as the
timeout value.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
media - the Media to load.autoplay - true to ask the remote application to start
playback as soon as the Media has been loaded,
false to ask it to transition to a paused state after
loading.currentTime - the position in seconds where playback are to be
started in the loaded Media.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IllegalArgumentException - If media is null.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus load(@Nonnull Media media, @Nullable Boolean autoplay, @Nullable Double currentTime, boolean synchronous, long responseTimeout) throws IOException
Media
using the specified parameters.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
media - the Media to load.autoplay - true to ask the remote application to start
playback as soon as the Media has been loaded,
false to ask it to transition to a paused state after
loading.currentTime - the position in seconds where playback are to be
started in the loaded Media.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
will be used.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IllegalArgumentException - If session or media is
null.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus load(@Nonnull Media media, @Nullable List<Integer> activeTrackIds, @Nullable Boolean autoplay, @Nullable Double currentTime, @Nullable Double playbackRate, boolean synchronous) throws IOException
Media using the
specified parameters.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
media - the Media to load.activeTrackIds - the List of track IDs that are active. If
the list is not provided, the default tracks will be active.autoplay - true to ask the remote application to start
playback as soon as the Media has been loaded,
false to ask it to transition to a paused state after
loading.currentTime - the position in seconds from the start for where
playback is to start in the loaded Media. If the
content is live content, and currentTime is not
specified, the stream will start at the live position.playbackRate - the media playback rate.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IllegalArgumentException - If media is null.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus load(@Nonnull Media media, @Nullable List<Integer> activeTrackIds, @Nullable Boolean autoplay, @Nullable Double currentTime, @Nullable Double playbackRate, boolean synchronous, long responseTimeout) throws IOException
Media using the
specified parameters and Channel.DEFAULT_RESPONSE_TIMEOUT as the
timeout value.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
media - the Media to load.activeTrackIds - the List of track IDs that are active. If
the list is not provided, the default tracks will be active.autoplay - true to ask the remote application to start
playback as soon as the Media has been loaded,
false to ask it to transition to a paused state after
loading.currentTime - the position in seconds from the start for where
playback is to start in the loaded Media. If the
content is live content, and currentTime is not
specified, the stream will start at the live position.playbackRate - the media playback rate.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
will be used.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IllegalArgumentException - If media is null.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus load(@Nonnull Media media, @Nullable List<Integer> activeTrackIds, @Nullable Boolean autoplay, @Nullable String credentials, @Nullable String credentialsType, @Nullable Double currentTime, @Nullable Map<String,Object> customData, @Nullable LoadOptions loadOptions, @Nullable Double playbackRate, @Nullable QueueData queueData, boolean synchronous) throws IOException
Media or
QueueDatas using the specified parameters, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. Either
media or queueData must be non-null.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
media - the Media to load.activeTrackIds - the List of track IDs that are active. If
the list is not provided, the default tracks will be active.autoplay - true to ask the remote application to start
playback as soon as the Media has been loaded,
false to ask it to transition to a paused state after
loading.credentials - the user credentials, if any.credentialsType - the credentials type, if any. The type
'cloud' is a reserved type used by load requests that
were originated by voice assistant commands.currentTime - the position in seconds from the start for where
playback is to start in the loaded Media. If the
content is live content, and currentTime is not
specified, the stream will start at the live position.customData - the custom application data to send to the remote
application with the load command.loadOptions - the additional load options, if any.playbackRate - the media playback rate.queueData - the queue data.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus load(@Nonnull Media media, @Nullable List<Integer> activeTrackIds, @Nullable Boolean autoplay, @Nullable String credentials, @Nullable String credentialsType, @Nullable Double currentTime, @Nullable Map<String,Object> customData, @Nullable LoadOptions loadOptions, @Nullable Double playbackRate, @Nullable QueueData queueData, boolean synchronous, long responseTimeout) throws IOException
Media or
QueueDatas using the specified parameters. Either media
or queueData must be non-null.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
media - the Media to load.activeTrackIds - the List of track IDs that are active. If
the list is not provided, the default tracks will be active.autoplay - true to ask the remote application to start
playback as soon as the Media has been loaded,
false to ask it to transition to a paused state after
loading.credentials - the user credentials, if any.credentialsType - the credentials type, if any. The type
'cloud' is a reserved type used by load requests that
were originated by voice assistant commands.currentTime - the position in seconds from the start for where
playback is to start in the loaded Media. If the
content is live content, and currentTime is not
specified, the stream will start at the live position.customData - the custom application data to send to the remote
application with the load command.loadOptions - the additional load options, if any.playbackRate - the media playback rate.queueData - the queue data.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
will be used.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus play(int mediaSessionId, boolean synchronous) throws IOException
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaSessionId - the media session ID for which the play request
applies.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus play(int mediaSessionId, boolean synchronous, long responseTimeout) throws IOException
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaSessionId - the media session ID for which the play request
applies.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
will be used.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus pause(int mediaSessionId, boolean synchronous) throws IOException
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaSessionId - the media session ID for which the pause request
applies.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus pause(int mediaSessionId, boolean synchronous, long responseTimeout) throws IOException
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaSessionId - the media session ID for which the pause request
applies.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
will be used.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus seek(int mediaSessionId, double currentTime, @Nullable StandardRequest.ResumeState resumeState, boolean synchronous) throws IOException
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaSessionId - the media session ID for which the pause request
applies.currentTime - the new playback position in seconds.resumeState - the desired media player state after the seek is
complete. If null, it will retain the state it had
before seeking.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus seek(int mediaSessionId, double currentTime, @Nullable StandardRequest.ResumeState resumeState, boolean synchronous, long responseTimeout) throws IOException
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaSessionId - the media session ID for which the pause request
applies.currentTime - the new playback position in seconds.resumeState - the desired media player state after the seek is
complete. If null, it will retain the state it had
before seeking.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
will be used.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus stop(int mediaSessionId, boolean synchronous) throws IOException
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaSessionId - the media session ID for which the
MediaVolume request applies.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus stop(int mediaSessionId, boolean synchronous, long responseTimeout) throws IOException
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaSessionId - the media session ID for which the
MediaVolume request applies.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
will be used.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus setVolume(int mediaSessionId, @Nonnull MediaVolume volume, boolean synchronous) throws IOException
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
mediaSessionId - the media session ID for which the
MediaVolume request applies.volume - the MediaVolume to set.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.MediaStatus if synchronous is
true and a reply is received in time, null if
synchronous is false.IllegalArgumentException - If volume is null.IOException - If the response times out or an error occurs during
the operation.@Nullable public MediaStatus getMediaStatus() throws IOException
MediaStatus from the remote application. This
method is always blocking.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
MediaStatus if a reply is received in time,
or null if a timeout occurs.IOException - If an error occurs during the operation.@Nullable public MediaStatus getMediaStatus(long responseTimeout) throws IOException
MediaStatus from the remote application. This
method is always blocking.
This can only succeed if the remote application supports the
"urn:x-cast:com.google.cast.media" namespace.
responseTimeout - the response timeout in milliseconds. If zero or
negative, Channel.DEFAULT_RESPONSE_TIMEOUT will be
used.MediaStatus if a reply is received in time,
or null if a timeout occurs.IOException - If an error occurs during the operation.public <T extends Response> T sendGenericRequest(String namespace, Request request, Class<T> responseClass) throws IOException
T - the class of the Response object.namespace - the namespace to use.request - the Request to send.responseClass - the response class to to block and wait for a
response or null to return immediately.Response if the response is received in time, or
null if the responseClass is null or a
timeout occurs.IOException - If an error occurs during the operation.Copyright © 2022. All rights reserved.