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

Question: How to override the video track selection for multiple videos #5727

Closed
DionataFerraz opened this issue Apr 4, 2019 · 2 comments
Closed
Assignees
Labels

Comments

@DionataFerraz
Copy link

DionataFerraz commented Apr 4, 2019

Can you change the resolution of all the videos that are playing in the player without having to create a new instance of the player?

Exoplayer version 2.9.4

To make the exchange of quality I am using this method

private fun applySelection() {
        val trackInfo = trackSelector.currentMappedTrackInfo ?: return
        val trackGroups = trackInfo.getTrackGroups(videoRendererIndex)

        val parametersBuilder = trackSelector.buildUponParameters()
        parametersBuilder.setRendererDisabled(videoRendererIndex, false)

        if (overridePlayer != null) {
            parametersBuilder.setSelectionOverride(videoRendererIndex, trackGroups, overridePlayer)
        } else {
            parametersBuilder.clearSelectionOverrides(videoRendererIndex)
        }

        trackSelector.setParameters(parametersBuilder)
}
@ojw28
Copy link
Contributor

ojw28 commented Apr 7, 2019

Selection overrides only apply to content that has the same set of tracks that you've set the override for. Or more precisely, a video track override will only apply to content where the trackGroups assigned to the video renderer are the same as those that you've set the override for. If you start playing content that has a different set of tracks, you'll need to set a new override for the new trackGroups (note: there is no requirement that you create a new instance of the player to do this).

If possible, try using constraint based track selection rather than specific track overrides when playing multiple videos. For example if you're trying to impose a maximum quality, use parametersBuilder.setMaxVideoSize or parametersBuilder.setMaxVideoBitrate. Constraints are a lot more flexible than specific overrides, and apply naturally over multiple videos. Setting minimum qualities via constraints is currently not possible; adding this functionality is tracked by #4511.

@ojw28 ojw28 removed the needs triage label Apr 7, 2019
@ojw28 ojw28 changed the title Player does not change the quality of all HLS videos Question: How to override the video track selection for multiple videos Apr 7, 2019
@DionataFerraz
Copy link
Author

DionataFerraz commented Apr 11, 2019

Solved with that
OBS: it was not me that did

import com.google.android.exoplayer2.source.TrackGroup
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection
import com.google.android.exoplayer2.trackselection.TrackSelection
import com.google.android.exoplayer2.upstream.BandwidthMeter
import com.google.android.exoplayer2.util.Clock

typealias SelectedIndex = () -> Int?

class FixedAdaptiveTrackSelection(
    group: TrackGroup,
    tracks: IntArray,
    bandwidth: BandwidthMeter,
    minDurationForQualityIncreaseMs: Long,
    maxDurationForQualityDecreaseMs: Long,
    minDurationToRetainAfterDiscardMs: Long,
    bandwidthFraction: Float,
    bufferedFractionToLiveEdgeForQualityIncrease: Float,
    minTimeBetweenBufferReevaluationMs: Long,
    clock: Clock,
    private var selectedIndexFunction: SelectedIndex?
) : AdaptiveTrackSelection(
    group,
    tracks,
    bandwidth,
    minDurationForQualityIncreaseMs,
    maxDurationForQualityDecreaseMs,
    minDurationToRetainAfterDiscardMs,
    bandwidthFraction,
    bufferedFractionToLiveEdgeForQualityIncrease,
    minTimeBetweenBufferReevaluationMs,
    clock
) {

    class Factory : TrackSelection.Factory {

        private var bandwidthMeter: BandwidthMeter? = null
        private var minDurationForQualityIncreaseMs: Int? = null
        private var maxDurationForQualityDecreaseMs: Int? = null
        private var minDurationToRetainAfterDiscardMs: Int? = null
        private var bandwidthFraction: Float? = null
        private var bufferedFractionToLiveEdgeForQualityIncrease: Float? = null
        private var minTimeBetweenBufferReevaluationMs: Long? = null
        private var clock: Clock
        private var selectedIndex: SelectedIndex? = null

        constructor(selectedIndex: SelectedIndex?) : this(
            AdaptiveTrackSelection.DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS,
            DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS,
            DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS,
            DEFAULT_BANDWIDTH_FRACTION,
            DEFAULT_BUFFERED_FRACTION_TO_LIVE_EDGE_FOR_QUALITY_INCREASE,
            DEFAULT_MIN_TIME_BETWEEN_BUFFER_REEVALUTATION_MS,
            Clock.DEFAULT,
            selectedIndex
        )

        constructor(
            minDurationForQualityIncreaseMs: Int,
            maxDurationForQualityDecreaseMs: Int,
            minDurationToRetainAfterDiscardMs: Int,
            bandwidthFraction: Float,
            bufferedFractionToLiveEdgeForQualityIncrease: Float,
            minTimeBetweenBufferReevaluationMs: Long,
            clock: Clock,
            selectedIndex: SelectedIndex?
        ) {
            this.minDurationForQualityIncreaseMs = minDurationForQualityIncreaseMs
            this.maxDurationForQualityDecreaseMs = maxDurationForQualityDecreaseMs
            this.minDurationToRetainAfterDiscardMs = minDurationToRetainAfterDiscardMs
            this.bandwidthFraction = bandwidthFraction
            this.bufferedFractionToLiveEdgeForQualityIncrease = bufferedFractionToLiveEdgeForQualityIncrease
            this.minTimeBetweenBufferReevaluationMs = minTimeBetweenBufferReevaluationMs
            this.clock = clock
            this.selectedIndex = selectedIndex
        }

        override fun createTrackSelection(group: TrackGroup, bandwidth: BandwidthMeter, vararg tracks: Int): FixedAdaptiveTrackSelection {
            var meter = bandwidth
            if (bandwidthMeter != null) {
                meter = bandwidthMeter as BandwidthMeter
            }
            return FixedAdaptiveTrackSelection(
                group,
                tracks,
                meter,
                minDurationForQualityIncreaseMs?.toLong() ?: 0,
                maxDurationForQualityDecreaseMs?.toLong() ?: 0,
                minDurationToRetainAfterDiscardMs?.toLong() ?: 0,
                bandwidthFraction ?: 0F,
                bufferedFractionToLiveEdgeForQualityIncrease ?: 0F,
                minTimeBetweenBufferReevaluationMs ?: 0,
                clock,
                selectedIndex)
        }

    }

    override fun getSelectedIndex(): Int {
        return selectedIndexFunction?.invoke() ?: super.getSelectedIndex()
    }

}
val trackSelector: DefaultTrackSelector by lazy {
        DefaultTrackSelector(FixedAdaptiveTrackSelection.Factory(::getSelectedQualityIndex)).apply {
            parameters = trackSelectorParameters
        }
 }

@google google locked and limited conversation to collaborators Aug 29, 2019
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

2 participants