Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get selected Format with TracksInfo? #9674

Closed
Goshin opened this issue Nov 9, 2021 · 4 comments
Closed

How to get selected Format with TracksInfo? #9674

Goshin opened this issue Nov 9, 2021 · 4 comments
Assignees
Labels

Comments

@Goshin
Copy link

Goshin commented Nov 9, 2021

I used Player.getCurrentTrackSelections() and ExoTrackSelection.getSelectedFormat() to get some info of a playing HLS video:

TrackSelectionArray trackSelectionArray = player.getCurrentTrackSelections();
for (int i = 0; i < trackSelectionArray.length; i++) {
    TrackSelection trackSelection = trackSelectionArray.get(i);
    if (trackSelection instanceof ExoTrackSelection) {
        ExoTrackSelection exoTrackSelection = (ExoTrackSelection) trackSelection;
        Format format = exoTrackSelection.getSelectedFormat();
        // Obtain format.codecs, format.bitrate, format.height ... here
    }
}

Since Player.getCurrentTrackSelections() is now marked deprecated, I try using Player.getCurrentTracksInfo() instead, which is suggested in the documentation.

However, a TracksInfo does not provide TrackSelection or the selected Format as getCurrentTrackSelections() does. I can see there is a similar method named TrackGroupInfo.isTrackSelected(int trackIndex) but it is not equivalent as documented and tested by myself.

    /**
     * Returns if a track in a {@link TrackGroup} is selected for playback.
     *
     * <p>Multiple tracks of a track group may be selected. This is common in adaptive streaming,
     * where multiple tracks of different quality are selected and the player switches between them
     * depending on the network and the {@link TrackSelectionParameters}.
     *
     * <p>While this class doesn't provide which selected track is currently playing, some player
     * implementations have ways of getting such information. For example ExoPlayer provides this
     * information in {@code ExoTrackSelection.getSelectedFormat}.
     *
     * @param trackIndex The index of the track in the {@link TrackGroup}.
     * @return true if the track is selected, false otherwise.
     */
    public boolean isTrackSelected(int trackIndex) {
      return trackSelected[trackIndex];
    }

I was wondering what the proper way is to obtain ExoTrackSelection for now?

@moneytoo
Copy link
Contributor

You can get Format from Player.getCurrentTracksInfo(), check the code sample here: #9665 (comment)

@Goshin
Copy link
Author

Goshin commented Nov 10, 2021

@moneytoo

You can get Format from Player.getCurrentTracksInfo(), check the code sample here: #9665 (comment)

I see, to my understanding, that sample only shows how to get a Format by its index in a TrackGroupInfo, but I want to obtain the selected format like that provided by ExoTrackSelection.getSelectedFormat().

@tonihei
Copy link
Collaborator

tonihei commented Nov 10, 2021

but I want to obtain the selected format like that provided by ExoTrackSelection.getSelectedFormat().

(Exo)TrackSelection.getSelectedFormat() was never meant to be called by app code as it's not thread safe and also not aligned with the rest of the player state.

The simplest way to listen to adaptive video format changes is to listen to Player.Listener.onVideoSizeChanged, or alternatively to AnalyticsListener.onDownstreamFormatChanged (which contains more details like the actual Format and the reason for the change). You can also obtain the current video size by calling Player.getVideoSize().

@Goshin
Copy link
Author

Goshin commented Nov 10, 2021

@tonihei Many thanks. AnalyticsListener.onDownstreamFormatChangedworks, but seems like it does not work for HLS streams with only one track.
Then I try AnalyticsListener.onVideoInputFormatChanged instead and it works like a charm!

@google google locked and limited conversation to collaborators Jan 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants