245 Commits

Author SHA1 Message Date
christosts
2424ee7792 Misc fix in gradle build file
Issue: androidx/media#209

#minor-release

PiperOrigin-RevId: 490492223
2022-11-24 14:46:43 +00:00
bachinger
3d8c52f28d Exclude tracks from PlayerInfo if not changed
This change includes a change in the `IMediaController.aidl` file and needs
to provide backwards compatibility for when a client connects that is of an older or
newer version of the current service implementation.

This CL proposes to create a new AIDL method `onPlayerInfoChangedWithExtensions`
that is easier to extend in the future because it does use an `Bundle` rather than
primitives. A `Bundle` can be changed in a backward/forwards compatible way
in case we need further changes.

The compatibility handling is provided in `MediaSessionStub` and `MediaControllerStub`. The approach is not based on specific AIDL/Binder features but implemented fully in application code.

Issue: androidx/media#102
#minor-release
PiperOrigin-RevId: 490483068
2022-11-24 14:45:37 +00:00
bachinger
68908be18d Call future listener on the same handler that created the controller
The direct executor is not the proper way to determine on what thread to run the
`Future.Listener` and the `MediaControllerCreationListener` because the listener
may call the controller passed as argument which must happen on the same thread
that built the controller. This change makes sure this is the case.

PiperOrigin-RevId: 490478587
2022-11-24 14:43:44 +00:00
tonihei
03f0b53cf8 Add helper method to convert platform session token to Media3 token
This avoids that apps have to depend on the legacy compat support
library when they want to make this conversion.

Also add a version to both helper methods that takes a Looper to
give apps the option to use an existing Looper, which should be
much faster than spinning up a new thread for every method call.

Issue: androidx/media#171
PiperOrigin-RevId: 490441913
2022-11-24 14:39:54 +00:00
bachinger
1803d1cdb8 Migrate BROWSER_ROOT_HINTS_KEY_ROOT_CHILDREN_SUPPORTED_FLAGS to Media3
PiperOrigin-RevId: 490376734
2022-11-24 14:38:41 +00:00
tianyifeng
8ce1213ddd Load bitmaps for MediaSessionCompat.QueueItem.
When receiving the `onTimelineChanged` callback, we convert the timeline to the list of `QueueItem`s, where decoding a bitmap is needed for building each of the `QueueItem`s. The strategy is similar to what we did in <unknown commit> for list of `MediaBrowserCompat.MediaItem` - set the queue item list until the bitmaps decoding for all the `MediaItem`s are completed.

PiperOrigin-RevId: 490283587
2022-11-24 14:37:23 +00:00
tonihei
2fd4aac310 Do not require package visibility when connecting to a Media3 session
When we currently call SessionToken.createSessionToken with a legacy
token, we call the package manager to get the process UID. This
requires visiblity to the target package, which may not be available
unless the target runs a service known to the controller app.

However, when connecting to a Media3, this UID doesn't have to be
known, so we can move the call closer to where it's needed to
avoid the unncessary visibility check.

In addition, a legacy session may reply with unknown result code
to the session token request, which we should handle as well.

One of the constructor can be removed since it was only used from
a test.

PiperOrigin-RevId: 489917706
2022-11-22 10:08:08 +00:00
tianyifeng
4ce171a3cf Load bitmaps for MediaBrowserCompat.
* Transforms the `ListenableFuture<LibraryResult<MediaItem>>` and `ListenableFuture<LibraryResult<List<MediaItem>>>` to `ListenableFuture<MediaBrowserCompat.MediaItem>` and `ListenableFuture<List<MediaBrowserCompat.MediaItem>>`, and the result will be sent out when `ListenableFuture` the `MediaBrowserCompat.MediaItem` (or the list of it) is fulfilled.
* Add `artworkData` to the tests in `MediaBrowserCompatWithMediaLibraryServiceTest`.

PiperOrigin-RevId: 489205547
2022-11-22 09:53:40 +00:00
tonihei
1143edc59a Rename getVideoSurfaceSize to getSurfaceSize
This better matches the callback name (onSurfaceSizeChanged) and
probably cause less confusion with getVideoSize.

PiperOrigin-RevId: 488669786
2022-11-16 12:15:05 +00:00
tonihei
33bea391d4 Ensure listener invocations use final state variable.
The MediaControllerImplBase listener invocations currently use the
class member state that can change if one of the listener method
implementations changes the state recursively.

Updating the listener invocations to use a final local variable
ensures all listeners get consistent updates.

PiperOrigin-RevId: 487503373
2022-11-10 15:00:07 +00:00
tonihei
c6a0ba25f9 Split test methods more clearly in arrange, act, assert.
The tests in MediaControllerCompatCallbackWithMediaSessionTest
don't follow this pattern very consistently at the moment.

PiperOrigin-RevId: 487501913
2022-11-10 14:59:07 +00:00
tonihei
c5e071e556 Align PlaybackStateCompat states with logic in MediaSessionConnector
Creating the PlaybackStateCompat from a media3 Player state is done
already by the MediaSessionConnector (and used widely). The media3
session module should set the same states under the same circumstances
to ensure compatiblity and consistency.

PlaybackStateCompat changes made in media3 session:
 - Use STATE_STOPPED when player is ended instead of STATE_PAUSED
 - Use STATE_PLAYING when playback is suppressed temporarily.
 - Set the playback speed to 0 if the player is not playing.
 - Add extras for mediaId and user-set playback speed.

Part of the problem was that Player.isPlaying() was used to check
the state. Unfortunately, MockPlayer.isPlaying() is implemented in
a way that makes it hard to test these changes, because the value
is set independently of playbackState, playWhenReady and suppression
reason. To be able to write consistent, logical tests, this change
also removes the independent setting of isPlaying in MockPlayer to
align it better with a real player. This requires to update some
other tests to use alternative methods.

PiperOrigin-RevId: 487500859
2022-11-10 14:58:06 +00:00
tonihei
0b4ba3e3a6 Wait with PlayerInfo updates until all pending operations are done
Accepting a PlayerInfo while the MediaController is masking its state
means we are reverting all masking changes we've made earlier. This
only makes sense if the update already contains the masked operation.
If multiple operations are in flight (or are sent from the session
while they are in flight), we need to wait until all of them are
handled before accepting new updates.

In cases where a new update from the session excludes the Timeline
and the masked state is incompatible with the new update, we also
risk an exception if we accept the update too early.

PiperOrigin-RevId: 487266899
2022-11-10 14:38:56 +00:00
christosts
c403b4ce7c MediaController: Add missing event flags (2/2)
This is the follow-up commit where the onEvents callback
raised by MediaController contains the missing events, for the
case where MediaController is connected to a legacy MediaSession.

#minor-release

PiperOrigin-RevId: 487231996
2022-11-10 14:36:50 +00:00
christosts
09651ed2e3 MediaControllerListenerWithMediaSessionCompatTest: clean imports
Remove static imports to Player constants to make the code more readable.

#minor-release

PiperOrigin-RevId: 486619911
2022-11-08 11:31:04 +00:00
michaelkatz
9336b95bf4 Changed MediaController to return last estimated position while paused
The method getCurrentPosition() may return a lesser position during pause than the previous retrieved value due to ipc call delay in playerInfo update. Users see track position jump backwards at pause. Fixed to return last estimated position while paused if have not received updated playerInfo. Code is deduped to point getContentPosition() to getCurrentPosition() when !isPlayingAd.

PiperOrigin-RevId: 486617341
2022-11-08 11:30:01 +00:00
Googler
4271056545 Add 'Player.getVideoSurfaceSize' that returns the size of the surface
on which the video is rendered.

Design Doc: go/aaos-mu-media-dd

PiperOrigin-RevId: 485884772
2022-11-08 11:25:43 +00:00
tianyifeng
77fedd8d7d Load bitmaps for MediaMetadataCompat and handle the metadata updates.
* Add `Listener` in `MediaSession` with method `onNotificationRefreshRequired(MediaSession)`.
* Add `MediaSessionService` as the listener of the `MediaSession` when `MediaSession` is added to `MediaSessionService`
* Load bitmap when update metadata in `MediaSessionLegacyStub` and call `onNotificationRefreshRequired` when bitmap asynchronously arrives.

PiperOrigin-RevId: 485376145
2022-11-08 11:19:04 +00:00
bachinger
4c9ca8fad1 Set BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED automatically
This root extra needs to be set by apps manually in media1 and we
can do that automatically in Media3 based on the available session
commands.

#minor-release

PiperOrigin-RevId: 484286833
2022-10-31 11:44:57 +00:00
bachinger
21022c77be Use MediaBrowserCompat.rootHints as connections hints
In Media3 there is the useful concept of connection hints that a
client can set when building the session and that are sent to the
service and passed to the `Callback.onConnect()` method when the
browser connects.

These connection hints are then included in the `ControllerInfo`
object that later will be passed to every callback method and the
implementor can then take decisions specific to these connection
hints.

These connection hints are not available in media1. However, when
an app creates a `MediaBrowserCompat` object, the constructor takes
a rootHint object that is sent to
`MediaBrowserServiceCompat.onGetRoot()`.

This change uses the browser rootHints as the connection hints when
creating the `ControllerInfo` for legacy browsers and makes them
available to the `MediaLibrarySession.Callback` domain methods in
the same way as connection hints of a Media3 browser.

PiperOrigin-RevId: 484220748
2022-10-31 11:43:21 +00:00
christosts
46d5a0e33b MediaController: Add missing event flags (1/2)
This is the first commit out of two. This change adds the missing event
flags for the onEvents() callback when MediaController is connected to a
media3 session (see MediaControllerImplBase). I updated the
MediaControllerListenerTest and MediaControllerStateMaskingTest with
assertions that on onEvents() is called alongside individual
Player.Listener callbacks.

There will be a follow-up change for the case where a MediaController is
connected to a legacy MediaSession (MediaControllerImplLegacy). I've
split this in two separate changes to make the size of the commit
manageable for reviewing.

#minor-release

PiperOrigin-RevId: 481933437
2022-10-24 10:34:30 +00:00
bachinger
006a519a0e Migrate media constants from androidx.media.util.MediaConstants
Adds root extras and metadata extras to MockMediaLibraryService and MockMediaBrowserCompatService and completed test cases for asserting
interoperability with a media1 or Media3 browser.

PiperOrigin-RevId: 480854842
2022-10-17 15:56:30 +00:00
christosts
cb3a672508 MediaControllerWithMediaSessionCompatTest
Fix a race condition making test flaky.

PiperOrigin-RevId: 480104467
2022-10-17 15:47:28 +00:00
christosts
6f8863fc11 Minor naming cleanup in MediaControllerListenerTest
PiperOrigin-RevId: 480041937
2022-10-17 15:45:33 +00:00
christosts
ba1ecafb8f MediaControllerListenerTest: use local MediaController instances
Replace the instance-field MediaController with a local instance created
in each test.

PiperOrigin-RevId: 480039768
2022-10-17 15:43:28 +00:00
bachinger
9e310150a9 Remove unofficial link from test case
PiperOrigin-RevId: 477101201
2022-09-30 18:25:28 +00:00
tonihei
d2887d5237 Remove test that just times out
One of the tests in MediaBrowserListenerTest caused the remote
browser service to crash and then just timed out. As this asserts
nothing useful besides checking that the timeout method is working,
we can remove the test.

Crashing the remote browser service had the side effect of letting
subsequent tests in the same class fail because the previous session
was never released and was still present in the static MediaSession
SESSION_ID_TO_SESSION_MAP instance, which prevented the creation
of new sessions with the same id. This is only an issue in test
runs because a real process would also lose its static variables
when it crashes.

PiperOrigin-RevId: 476905337
2022-09-30 18:22:32 +00:00
christosts
260aabb6e9 MediaControllerListenerTest: add bugs IDs in TODOs
PiperOrigin-RevId: 472974903
2022-09-30 17:43:45 +00:00
bachinger
73f86682e9 Remove assertion that prevents masking of ad periods
The assertion asserts against a `Period` and an `AdPlaybackState` which actually
asserts against a resolved ad which is what `ExoPlayerImplInternal` does later and
what gives us a `SEEK_ADJUSTMENT`. However, this assertion is not required at the
moment of masking, because we are sure that the resolved seek results in a content
period and never an ad period.

#minor-release
Issue: androidx/media#122
PiperOrigin-RevId: 471827072
2022-09-30 17:24:00 +00:00
tonihei
59895646e0 Define CueGroup.EMPTY_TIME_ZERO for convenience
We create an empty CueGroup in many places as default or
where none is needed. Instead, we can define a constant
for this purpose and reuse it.

PiperOrigin-RevId: 467944841
2022-09-30 16:43:22 +00:00
christosts
7a7e1eb23b MediaNotificationManager: handle playback ended
When the player finishes playback and reaches the STATE_ENDED,
the notification remains visible with a pause button and the
service is kept in the foreground. This is a bug.

With this change, when the player reaches the STATE_ENDED, the
service is stopped from the foreground and a notification is shown
with a play button. If the play icon is tapped, the player will restart
playback of the last played item. Playing the last played item again
is the existing behavior when play/pause commands are received from
the legacy MediaSession (e.g. BT headset buttons).

#minor-release

Issue: google/ExoPlayer#112
PiperOrigin-RevId: 467231509
2022-09-30 16:36:20 +00:00
bachinger
a28b3ef778 Add interface version of MediaSessionStub
PiperOrigin-RevId: 464052708
2022-08-08 08:32:29 +00:00
bachinger
c8089ead42 Add interface version of MediaControllerStub
PiperOrigin-RevId: 463930162
2022-08-08 08:25:47 +00:00
tonihei
ac42b593cc Delay unbinding the controller until pending commands are handled.
Once a controller bound to a MediaSessionService unbinds, this service
may be destroyed immediately if this was the last bound controller and
the service isn't started in the foreground.

At the time of releasing the controller, there may still be pending
commands that need to be handled by the session service. These commands
may cause the session service to post a foreground notification to
keep it alive. So to avoid the destruction of the service while these
commands are still handled, we need to keep the controller bound.

We also add a timeout in case the session tasks are never completed
by the session implementation. In case the controller is destroyed,
the unbinding happens automatically by the system.

PiperOrigin-RevId: 463802220
2022-08-08 08:19:35 +00:00
ibaker
2deb435625 Annotate methods that always return this with @CanIgnoreReturnValue
It's always safe to ignore the result of these methods, because the
caller already has a reference to the returned value.

PiperOrigin-RevId: 462388947
2022-08-08 07:55:25 +00:00
tonihei
ee209690cb Ensure pending commands are still sent in MediaController.release()
We currently clear all pending messages, including the one that flushes
pending commands to the MediaSession. To ensure all commands that have
been called before controller.release() are still sent, we can manually
trigger the flush message from the release call.

Related to handling the final flush because disconnecting the controller,
MediaSessionStub didn't post the removal of the controller to the
session thread, creating a race condition between removing the controller
and actually handling the flush.

Issue: androidx/media#99
PiperOrigin-RevId: 462342860
2022-07-21 13:00:20 +00:00
tonihei
7cb7636ed9 Run MediaSessionStub commands in order
Some commands are run asynchronously and subsequent commands need
to wait until the previous one finished. This can be supported
by returning a Future for each command and using the existing
command execution logic to wait for each Future to complete.

As some MediaSessionStub code is now executed delayed to when it
was originally created, we also need to check if the session is
not released before triggering any actions or sending result codes.

Issue: androidx/media#85
PiperOrigin-RevId: 462101136
2022-07-21 12:54:06 +00:00
bachinger
5bf9e2fb31 Exclude tracks if COMMAND_GET_TRACKS is not available
Issue: androidx/media#102
#minor-release
PiperOrigin-RevId: 461891031
2022-07-21 12:50:29 +00:00
bachinger
9a895cd18f Implement getCurrentTracks in MediaController
After this change the current tracks are sent to the controller as part of
`PlayerInfo` and call `Listener.onTracksChanged()` in case of a change in tracks.

PiperOrigin-RevId: 461578695
2022-07-21 12:40:39 +00:00
rohks
74d61bbffb Add timestamp to CueGroup
`TextRenderer` is updated to output `CueGroup`, which contains the presentation time of the cues, in microseconds.

PiperOrigin-RevId: 456531399
2022-06-27 10:38:20 +01:00
bachinger
8f844b32fd Avoid out of bounds when setting less media items than in playlist
Issue: androidx/media#86
#minor-release
PiperOrigin-RevId: 455182232
2022-06-15 18:55:54 +00:00
tonihei
21d4e85817 Remove setMediaUri/onSetMediaUri
This code path is now completely redundant as the same effect can be
achieved by using player.setMediaItem.

#minor-release

PiperOrigin-RevId: 455115567
2022-06-15 15:32:14 +00:00
tonihei
b475f1f2da Support setMediaItem(s) in MediaControllerImplLegacy
These calls were not implemented so far as they require a mix of
initial prepareFrom/playFrom calls and addQueueItem. We can also
support clients without queue handling to set single MediaItems.

To make the calls consistent and predictable in the session,
we need to ensure that none of the play/pause/addQueueItem/
removeQueueItem/prepare/playFromXYZ/prepareFromXYZ are called
before the controller is prepared and has media.

#minor-release

PiperOrigin-RevId: 455110246
2022-06-15 15:30:05 +00:00
tonihei
5333c67d08 Add COMMAND_SET_MEDIA_ITEM to Player.Commands
Some Player implementations have no playlist capability but can still
set a MediaItem for playback. Examples are a MediaController connected
to a legacy MediaSession, ExoPlayer up to 2.12 or MediaPlayer.

To indicate this capability, we need an allowed command in addition
to COMMAND_CHANGE_MEDIA_ITEMS that just allows to set a single item
that replaces everything that is currently played.

#minor-release

PiperOrigin-RevId: 453879626
2022-06-09 17:46:14 +00:00
tonihei
bd126ec5c5 Forward legacy controller onPlay/PrepareFromXY calls to onAddMediaItems
These legacy callbacks are currently forwarded to onSetMediaUri which
will be removed in the future.

Also make sure to only call player.prepare/play after the items have
been set.

The calls to onAddQueueItem are also forwarded to onAddMediaItems to
actually allow a session to resolve these items to playable media, which
wasn't possible so far.

PiperOrigin-RevId: 453625204
2022-06-09 17:40:26 +00:00
christosts
8d326312fb Ignore flaky test
PiperOrigin-RevId: 453168843
2022-06-07 16:28:11 +00:00
christosts
34ca3bc3c3 De-flake test in MediaSessionPermissionTest
This change makes MediaSessionPermissionTest.removeMediaItems()
non-flaky. This is a quick fix though it'd be good to refactor this
test file.

PiperOrigin-RevId: 453162764
2022-06-07 16:27:21 +00:00
ibaker
b8ca5b8951 Add @deprecated javadoc to all @Deprecated @Override methods
This ensures the 'use X instead' message is easily visible in the
generated HTML for the overriding method. Currently it's not, e.g.:
https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/BasePlayer.html#getCurrentWindowIndex()

#minor-release

PiperOrigin-RevId: 452002224
2022-05-31 10:44:48 +00:00
bachinger
85a936ecb1 Implement session extras for Media3 and legacy controllers
This provides an (unstable) API for apps to broadcast session extras
Bundle to all connected controllers and set the extras in the legacy
session.

Similar to the custom layout, the extras Bundle is not part of the
Media3 session state. This means that when a Media3 controller
connects to the session after the broadcast, the extras needs to be
sent to that controller in  `MediaSession.Callback.onPostConnect(MediaSession session, ControllerInfo controller)`.

PiperOrigin-RevId: 451871731
2022-05-30 16:52:43 +00:00
bachinger
bf3d81c262 Add missing tests for custom layout notification
PiperOrigin-RevId: 451859199
2022-05-30 16:51:08 +00:00