456 Commits

Author SHA1 Message Date
tonihei
3fc61bb7c7 Deprecate MediaMetadata folderType and stabilize mediaType+isBrowsable
PiperOrigin-RevId: 522058915
2023-04-11 10:28:39 +01:00
tonihei
5b1370e686 Set duration in QueueTimeline
If the duration is reported in MediaMetadataCompat, it should
also be set in the QueueTimeline to match controller.getDuration()

PiperOrigin-RevId: 522022953
2023-04-05 13:17:58 +01:00
tonihei
502969a42b Implement missing broadcastCustomCommand to legacy controller
And also mention a few restrictions regarding legacy components
in Javadoc.

#minor-release

Issue: androidx/media#293
PiperOrigin-RevId: 522005562
2023-04-05 11:30:55 +01:00
tonihei
219967c5a3 Allow duplicated MediaItems in a legacy session
MediaItems are not meant to be unique in a playlist. If a legacy
session publishes multiple items that get converted to equal MediaItems,
the current code fails because we look up queue ids in a Map (that
doesn't allow duplicate entries).

Fix this by storing a simple list of items with additional data.

#minor-release

Issue: androidx/media#290
PiperOrigin-RevId: 521993802
2023-04-05 15:49:43 +01:00
tonihei
ff919fe74d Mark MediaSession methods final to prevent accidental overrides
It's currently not possible to even subclass MediaSession because
the constructor is package-private. To avoid any accidental usage or
future indirect subclassing, all methods can be marked as final.

PiperOrigin-RevId: 521775373
2023-04-05 15:46:40 +01:00
tonihei
694d690bc0 Improve check for invalid connection request
The check currently relies on the default value of 0 returned if the
Bundle doesn't define a pid. But in some cases, like Robolectric unit tests,
0 is a possible pid. The check can be improved by directly asserting that
the value is defined.

PiperOrigin-RevId: 521414649
2023-04-05 15:40:55 +01:00
tonihei
b3788ce568 Remove deprecated stop(boolean)
This method has been deprecated for over 2 years.

PiperOrigin-RevId: 520586238
2023-03-30 17:24:35 +00:00
tonihei
fc54a7a487 Add COMMAND_RELEASE to set whether Player instances can be released
This wasn't added so far because releasing is always allowed from a
MediaController (as it just releases the connection, not the session
player). But Player instances can be created for other purposes and
the receiver of a Player instance should not always be allowed to
call release if it doesn't own the player resource.

PiperOrigin-RevId: 519121122
2023-03-30 17:09:28 +00:00
tianyifeng
df558b59db Deprecate onUpdateNotification(MediaSession)
PiperOrigin-RevId: 518612625
2023-03-30 17:03:28 +00:00
tonihei
20669fca27 Document that custom commands can only be sent if they are defined
Custom commands from controller to session are blocked if they are
not listed in the available session command list. This isn't well
documented in the Javadoc currently.

#minor-release

PiperOrigin-RevId: 514733568
2023-03-14 07:32:36 +00:00
ibaker
cb7d565fd4 Add missing @param tags
Dackka generates a warning if a method has at least one `@param` tag,
but not all of them are documented.

#minor-release

PiperOrigin-RevId: 513873453
2023-03-07 11:49:24 +00:00
bachinger
6042bec18a Fix some JavaDoc in the Media3 session module
#minor-release

PiperOrigin-RevId: 513501046
2023-03-02 13:26:01 +00:00
tonihei
a7faa5bfd8 Update translations
#minor-release

PiperOrigin-RevId: 512890813
2023-03-01 17:22:55 +00:00
Googler
fc642eb45f Fix error in documentation string
The current javadoc refers to the SessionCallback#onConnected, which doesn't exist.

PiperOrigin-RevId: 510261965
2023-02-17 11:55:35 +00:00
tianyifeng
e1d12fc395 Update notification play/pause button with matching player state
Issue: androidx/media#192
PiperOrigin-RevId: 508649684
2023-02-10 14:57:53 +00:00
christosts
ebe7ece1eb Session: advertise legacy FLAG_HANDLES_QUEUE_COMMANDS
This change includes 3 things:
- when the legacy media session is created, FLAG_HANDLES_QUEUE_COMMANDS
  is advertised if the player has the COMMAND_CHANGE_MEDIA_ITEMS
  available.
- when the player changes its available commands, a new
  PlaybackStateCompat is sent to the remote media controller to
  advertise the updated PlyabackStateCompat actions.
- when the player changes its available commands, the legacy media
  session flags are sent accoridingly: FLAG_HANDLES_QUEUE_COMMANDS is
  set only if the COMMAND_CHANGE_MEDIA_ITEMS is available.

#minor-release

PiperOrigin-RevId: 506605905
2023-02-02 15:38:04 +00:00
bachinger
4a9cf7d069 Do not assume a valid queue in 3rd party sessions
This change fixes an issue that can be reproduced when
a controller `onConnect` creates a `QueueTimeline` out
of the state of a legacy session and then `prepare` is called.

`activeQueueItemId`, `metadata` and the `queue` of the legacy
session are used when a `QueueTimeline` is created. The change
adds unit tests to cover the different combinatoric cases these
properties being set or unset.

PiperOrigin-RevId: 505731288
2023-02-01 10:34:25 +00:00
bachinger
5c82d6bc18 Double tap detection for Bluetooth media button events only
Issue: androidx/media#233
#minor-release
PiperOrigin-RevId: 505078751
2023-02-01 10:24:01 +00:00
tonihei
067340cb0a Filter available commands based on PlaybackStateCompat actions
This allows a MediaController to understand which methods calls
are available on a legacy session.

PiperOrigin-RevId: 504306806
2023-01-25 18:42:51 +00:00
michaelkatz
7fbdbeb6ca Deduplicate onSetMediaItem handler logic
Created unified MediaUtils method to handle various logic for calling Player.setMediaItems from MediaSessionStub and MediaSessionLegacyStub

PiperOrigin-RevId: 504271877
2023-01-25 18:39:56 +00:00
michaelkatz
bb11e0286e Add onSetMediaItems listener with access to start index and position
Added onSetMediaItems callback listener to allow the session to modify/set MediaItem list, starting index and position before call to Player.setMediaItem(s).

Added conditional check in MediaSessionStub.setMediaItem methods to only call player.setMediaItem rather than setMediaItems if player does not support COMMAND_CHANGE_MEDIA_ITEMS

PiperOrigin-RevId: 503427927
2023-01-23 12:34:44 +00:00
bachinger
e690802e9e Add the MediaSession as an argument to getMediaButtons()
Issue: androidx/media#216
#minor-release
PiperOrigin-RevId: 503406474
2023-01-23 12:33:37 +00:00
tonihei
052c4b3c1a Add command check for metadata in DefaultMediaNotificationProvider
PiperOrigin-RevId: 503172986
2023-01-23 12:29:14 +00:00
tonihei
e961c1b5e9 Update media controller position before pausing.
We stop estimating new position when pausing until we
receive a new position from the player. However, this
means that we will continue to return a possible stale
previous position. Updating the current position before
pausing solves this issue.

PiperOrigin-RevId: 503153982
2023-01-23 12:28:09 +00:00
tonihei
f15b752543 Extend command GET_CURRENT_MEDIA_ITEM to more methods.
We currently only document it for the getCurrentMediaItem(), but
the command was always meant to cover all information about the
current media item and the position therein.

To correctly hide information for controllers, we need to filter
the Timeline when bundling the PlayerInfo class if only this
command is available.

PiperOrigin-RevId: 503098124
2023-01-23 12:25:48 +00:00
tonihei
69cfba7c53 Correctly filter PlayerInfo by available getter commands.
When bundling PlayerInfo, we need to remove information if the
controller is not allowed to access it. This was only partially
done at the moment.

PiperOrigin-RevId: 502852798
2023-01-23 12:22:33 +00:00
tonihei
c90ca7ba5f Make availableCommands known when bundling PlayerInfo
When bundling PlayerInfo, we remove data when the controller is not
allowed to access this data via getters. We also remove data for
performance reasons. In the toBundle() method, it's currently hard to
make the connection between allowed commands and filtering, because
the values are checked at a different place. This can be made more
readable by forwarding the applicable Commands directly.

The only functional fix is to filter the Timeline when sending the
first PlayerInfo after a connecting a controller if the command to
get the Timeline is not available. This also allows us to remove a
path to filter MediaItems from Timelines as it isn't used.

PiperOrigin-RevId: 502607391
2023-01-18 12:02:09 +00:00
christosts
39f4a17ad4 Filter what PlaybackStateCompat actions are advertised
PlayerWrapper advertises PlaybackStateCompat actions to the legacy
MediaSession based on the player's available commands.

PiperOrigin-RevId: 502559162
2023-01-18 11:58:10 +00:00
tonihei
cfcce9aec9 Fix command check in MediaControllerImplBase
The command check for setDeviceMuted was wrong.

PiperOrigin-RevId: 502355332
2023-01-17 02:08:47 +00:00
tonihei
a2a44cdc02 Add missing command checks to MediaSessionLegacyStub and PlayerWrapper
This player didn't fully check all player commands before calling the
respective methods.

PiperOrigin-RevId: 502353704
2023-01-17 02:08:00 +00:00
tonihei
664ab72d09 Remove unneccesary parameter taking Player.Command
The method to dispatch actions in MediaControllerImplBase takes
a Player.Command, but the value is only used to check if we
are setting a surface and need to handle the special blocking
call. This can be cleaned up by removing the parameter and calling
a dedicated blocking method where needed. This also ensures we
have to mention the relevant Player.Command only once in each
method.

PiperOrigin-RevId: 502341862
2023-01-17 02:07:14 +00:00
tonihei
86a95c2a4a Correctly map deprecated methods in MediaController to replacement
This avoids throwing exceptions for correct (but deprecated) Player
method invocations.

PiperOrigin-RevId: 502341428
2023-01-17 02:06:29 +00:00
tianyifeng
0d0cd78626 Catch FgSStartNotAllowedException when playback resumes
This fix applies to Android 12 and above.

In this fix, the `MediaSessionService` will try to start in the foreground before the session playback resumes, if ForegroundServiceStartNotAllowedException is thrown, then the app can handle the exception with their customized implementation of MediaSessionService.Listener.onForegroundServiceStartNotAllowedException. If no exception thrown, the a media notification corresponding to paused state will be sent as the consequence of successfully starting in the foreground. And when the player actually resumes, another media notification corresponding to playing state will be sent.

PiperOrigin-RevId: 501803930
2023-01-17 02:00:03 +00:00
bachinger
a2cf222117 Improve Java doc about how to override notification drawables
Issue: androidx/media#140
PiperOrigin-RevId: 501288267
2023-01-17 01:51:42 +00:00
tofunmi
a9135e2eff Move BitmapLoader to common.
The migration strategy is to deprecate `androidx.media3.session.BitmapLoader` and copy the file into common since BitmapLoader is a public interface that apps could be relying on.

PiperOrigin-RevId: 501266521
2023-01-17 01:50:18 +00:00
bachinger
375299bf36 Use onMediaMetadataChanged for updating the legacy session
Issue: androidx/media#219
PiperOrigin-RevId: 501080612
2023-01-17 01:46:39 +00:00
rohks
578f2de48f Initialise fields used for bundling as String directly
Initialising the fields as Integer and then getting a String on compute time is slow. Instead we directly initialise these fields as String. Improves the time taken in bundling PlayerInfo further to less than 200ms from ~300ms.

Also modified a test to improve productive coverage.

PiperOrigin-RevId: 500003935
2023-01-10 18:33:20 +00:00
tianyifeng
d848d3358a Add BitmapLoader injection in MediaController
Also clean up the strict mode violations of using `BitmapFactory.convertToByteArray` on the main thread.

PiperOrigin-RevId: 496422355
2022-12-21 15:23:38 +00:00
ibaker
14947539e5 Remove TODO from ControllerInfo - the existing approach is fine
PiperOrigin-RevId: 496398934
2022-12-21 15:21:37 +00:00
tonihei
0749b05923 Avoid sending periodic position updates while paused and not loading
The period updates were introduced to ensure the buffered position is
updated regularly and that any playback position drift is corrected.

None of these updates need to happen while the player is paused or
not loading and we can avoid the constant binder interactions.

PiperOrigin-RevId: 496329800
2022-12-21 15:11:28 +00:00
tonihei
a1954f7e0a Clarify behavior for out-of-bounds indices and align implementations
Some Player methods operate relative to existing indices in the
playlist (add,remove,move,seek). As these operations may be issued
from a place with a stale playlist (e.g. a controller that sends
a command while the playlist is changing), we have to handle out-
of-bounds indices gracefully. In most cases this is already
documented and implemented correctly. However, some cases are not
documented and the existing player implementations don't handle
these cases consistently (or in some cases not even correctly).

PiperOrigin-RevId: 495856295
2022-12-21 15:07:20 +00:00
tonihei
927b2d6a43 Clarify and correct allowed multi-threading for some Player methods
Some Player methods like getting the Looper and adding listeners
were always allowed to be called from any thread, but this is
undocumented. This change makes the threading rules of these
methods more explicit.

Removing listeners was never meant to be called from another thread
and we also don't support it safely because final callbacks may
be triggered from the wrong thread. To find potential issues, we
can assert the correct thread when releasing listeners.

Finally, there is a potential race condition when calling addListener
from a different thread at the same time as release, which may lead to
a registered listener that could receive callbacks after the player is
released.

PiperOrigin-RevId: 493843981
2022-12-12 11:38:06 +00:00
tonihei
ae8000aeca Replace MediaMetadata folderType by isBrowsable
The folder type has a mix of information about the item. It shows
whether the item is browsable (type != FOLDER_TYPE_NONE) and
which Bluetooth folder type to set for legacy session information.

It's a lot clearer to split this into a boolean isBrowsable and
use the existing mediaType to map back to the bluetooth folder type
where required.

folderType is not marked as deprecated yet as this would be an API
change, which will be done later.

PiperOrigin-RevId: 493544589
2022-12-12 11:32:57 +00:00
tonihei
ca4c6efdb7 Write media type with a custom key to legacy components.
This allows legacy media controllers and browsers to access this
information and legacy sessions and browser services to set this
information.

PiperOrigin-RevId: 492414716
2022-12-12 11:02:38 +00:00
christosts
f768ff970c Reduce log output for failing bitmap loads
Do not log the exception stack traces raised by the BitmapLoader when a
bitmap fails to load, e.g. when the artwork's URI scheme is not
supported by the SimpleBitmapLoader. The logs are kept in place but only
a single line is printed.

#minor-release

PiperOrigin-RevId: 492191461
2022-12-12 10:58:23 +00:00
Googler
69093db7f5 Decomission ControllerInfoProxy in favor of ControllerInfo.
This CL makes it possible to create a media3 ControllerInfo in test code, which is needed to test several aspects of a media3-based media app. It does this by exposing a test-only static factory method. This is a hacky low-effort approach; a better solution could be to split ControllerInfo up into a public interface that was exposed to client logic, and that they could extend, and a package-private implementation with internal fields like the callback. That's a much bigger change, however.

PiperOrigin-RevId: 491978830
2022-12-12 10:45:07 +00:00
bachinger
2a07a0b445 Use the artist as the subtitle of the legacy media description
The Bluetooth AVRCP service expects the metadata of the item currently
being played to be in sync with the corresponding media description in
the active item of the queue. The comparison expects the metadata values
of `METADATA_KEY_TITLE` and `METADATA_KEY_ARTIST` [1] to be equal to the
`title` and `subtitle` field of the `MediaDescription` [2] of the
corresponding queue item.

Hence we need to populate the media description accordingly to avoid the
BT service to delay the update for two seconds and log an exception.

[1] https://cs.android.com/android/platform/superproject/+/master:packages/modules/Bluetooth/android/app/src/com/android/bluetooth/audio_util/helpers/Metadata.java;l=120
[2] https://cs.android.com/android/platform/superproject/+/master:packages/modules/Bluetooth/android/app/src/com/android/bluetooth/audio_util/MediaPlayerWrapper.java;l=258

Issue: androidx/media#148
PiperOrigin-RevId: 491877806
2022-12-12 10:39:58 +00:00
rohks
f3e450e783 Add public constructors to DefaultMediaNotificationProvider
Issue: androidx/media#213

Without a public constructor, it is not possible to extend this class and override its method.

PiperOrigin-RevId: 491673111
2022-11-29 18:54:38 +00:00
tianyifeng
80927260fd Handle the bitmap loading result with applicationHandler
Before this change, the bitmap loading result with mainHandler, in which we set the metadata to `MediaSessionCompat`. However, the `MediaSessionCompat` is not thread safe, all calls should be made from the same thread. In the other calls to `MediaSessionCompat`, we ensure that they are on the application thread (which may be or may not be main thread), so we should do the same for `setMetadata` when bitmap arrives.

Also removes a comment in `DefaultMediaNotificationProvider` as bitmap request caching is already moved to CacheBitmapLoader.

PiperOrigin-RevId: 490524209
2022-11-24 14:48:50 +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