Search in sources :

Example 1 with Renderer

use of androidx.media3.exoplayer.Renderer in project media by androidx.

the class DefaultRenderersFactory method buildAudioRenderers.

/**
 * Builds audio renderers for use by the player.
 *
 * @param context The {@link Context} associated with the player.
 * @param extensionRendererMode The extension renderer mode.
 * @param mediaCodecSelector A decoder selector.
 * @param enableDecoderFallback Whether to enable fallback to lower-priority decoders if decoder
 *     initialization fails. This may result in using a decoder that is slower/less efficient than
 *     the primary decoder.
 * @param audioSink A sink to which the renderers will output.
 * @param eventHandler A handler to use when invoking event listeners and outputs.
 * @param eventListener An event listener.
 * @param out An array to which the built renderers should be appended.
 */
protected void buildAudioRenderers(Context context, @ExtensionRendererMode int extensionRendererMode, MediaCodecSelector mediaCodecSelector, boolean enableDecoderFallback, AudioSink audioSink, Handler eventHandler, AudioRendererEventListener eventListener, ArrayList<Renderer> out) {
    MediaCodecAudioRenderer audioRenderer = new MediaCodecAudioRenderer(context, getCodecAdapterFactory(), mediaCodecSelector, enableDecoderFallback, eventHandler, eventListener, audioSink);
    out.add(audioRenderer);
    if (extensionRendererMode == EXTENSION_RENDERER_MODE_OFF) {
        return;
    }
    int extensionRendererIndex = out.size();
    if (extensionRendererMode == EXTENSION_RENDERER_MODE_PREFER) {
        extensionRendererIndex--;
    }
    try {
        // Full class names used for constructor args so the LINT rule triggers if any of them move.
        Class<?> clazz = Class.forName("androidx.media3.decoder.opus.LibopusAudioRenderer");
        Constructor<?> constructor = clazz.getConstructor(android.os.Handler.class, androidx.media3.exoplayer.audio.AudioRendererEventListener.class, androidx.media3.exoplayer.audio.AudioSink.class);
        Renderer renderer = (Renderer) constructor.newInstance(eventHandler, eventListener, audioSink);
        out.add(extensionRendererIndex++, renderer);
        Log.i(TAG, "Loaded LibopusAudioRenderer.");
    } catch (ClassNotFoundException e) {
    // Expected if the app was built without the extension.
    } catch (Exception e) {
        // The extension is present, but instantiation failed.
        throw new RuntimeException("Error instantiating Opus extension", e);
    }
    try {
        // Full class names used for constructor args so the LINT rule triggers if any of them move.
        Class<?> clazz = Class.forName("androidx.media3.decoder.flac.LibflacAudioRenderer");
        Constructor<?> constructor = clazz.getConstructor(android.os.Handler.class, androidx.media3.exoplayer.audio.AudioRendererEventListener.class, androidx.media3.exoplayer.audio.AudioSink.class);
        Renderer renderer = (Renderer) constructor.newInstance(eventHandler, eventListener, audioSink);
        out.add(extensionRendererIndex++, renderer);
        Log.i(TAG, "Loaded LibflacAudioRenderer.");
    } catch (ClassNotFoundException e) {
    // Expected if the app was built without the extension.
    } catch (Exception e) {
        // The extension is present, but instantiation failed.
        throw new RuntimeException("Error instantiating FLAC extension", e);
    }
    try {
        // Full class names used for constructor args so the LINT rule triggers if any of them move.
        Class<?> clazz = Class.forName("androidx.media3.decoder.ffmpeg.FfmpegAudioRenderer");
        Constructor<?> constructor = clazz.getConstructor(android.os.Handler.class, androidx.media3.exoplayer.audio.AudioRendererEventListener.class, androidx.media3.exoplayer.audio.AudioSink.class);
        Renderer renderer = (Renderer) constructor.newInstance(eventHandler, eventListener, audioSink);
        out.add(extensionRendererIndex++, renderer);
        Log.i(TAG, "Loaded FfmpegAudioRenderer.");
    } catch (ClassNotFoundException e) {
    // Expected if the app was built without the extension.
    } catch (Exception e) {
        // The extension is present, but instantiation failed.
        throw new RuntimeException("Error instantiating FFmpeg extension", e);
    }
}
Also used : MediaCodecAudioRenderer(androidx.media3.exoplayer.audio.MediaCodecAudioRenderer) MediaCodecAudioRenderer(androidx.media3.exoplayer.audio.MediaCodecAudioRenderer) TextRenderer(androidx.media3.exoplayer.text.TextRenderer) MediaCodecVideoRenderer(androidx.media3.exoplayer.video.MediaCodecVideoRenderer) CameraMotionRenderer(androidx.media3.exoplayer.video.spherical.CameraMotionRenderer) MetadataRenderer(androidx.media3.exoplayer.metadata.MetadataRenderer)

Example 2 with Renderer

use of androidx.media3.exoplayer.Renderer in project media by androidx.

the class DownloadHelper method getRendererCapabilities.

/**
 * Extracts renderer capabilities for the renderers created by the provided renderers factory.
 *
 * @param renderersFactory A {@link RenderersFactory}.
 * @return The {@link RendererCapabilities} for each renderer created by the {@code
 *     renderersFactory}.
 */
public static RendererCapabilities[] getRendererCapabilities(RenderersFactory renderersFactory) {
    Renderer[] renderers = renderersFactory.createRenderers(Util.createHandlerForCurrentOrMainLooper(), new VideoRendererEventListener() {
    }, new AudioRendererEventListener() {
    }, (cues) -> {
    }, (metadata) -> {
    });
    RendererCapabilities[] capabilities = new RendererCapabilities[renderers.length];
    for (int i = 0; i < renderers.length; i++) {
        capabilities[i] = renderers[i].getCapabilities();
    }
    return capabilities;
}
Also used : Renderer(androidx.media3.exoplayer.Renderer) VideoRendererEventListener(androidx.media3.exoplayer.video.VideoRendererEventListener) AudioRendererEventListener(androidx.media3.exoplayer.audio.AudioRendererEventListener) RendererCapabilities(androidx.media3.exoplayer.RendererCapabilities)

Example 3 with Renderer

use of androidx.media3.exoplayer.Renderer in project media by androidx.

the class DownloadHelper method addTrackSelectionForSingleRenderer.

/**
 * Convenience method to add a selection of tracks to be downloaded for a single renderer. Must
 * not be called until after preparation completes.
 *
 * @param periodIndex The period index the track selection is added for.
 * @param rendererIndex The renderer index the track selection is added for.
 * @param trackSelectorParameters The {@link DefaultTrackSelector.Parameters} to obtain the new
 *     selection of tracks.
 * @param overrides A list of {@link SelectionOverride SelectionOverrides} to apply to the {@code
 *     trackSelectorParameters}. If empty, {@code trackSelectorParameters} are used as they are.
 */
public void addTrackSelectionForSingleRenderer(int periodIndex, int rendererIndex, DefaultTrackSelector.Parameters trackSelectorParameters, List<SelectionOverride> overrides) {
    assertPreparedWithMedia();
    DefaultTrackSelector.ParametersBuilder builder = trackSelectorParameters.buildUpon();
    for (int i = 0; i < mappedTrackInfos[periodIndex].getRendererCount(); i++) {
        builder.setRendererDisabled(/* rendererIndex= */
        i, /* disabled= */
        i != rendererIndex);
    }
    if (overrides.isEmpty()) {
        addTrackSelection(periodIndex, builder.build());
    } else {
        TrackGroupArray trackGroupArray = mappedTrackInfos[periodIndex].getTrackGroups(rendererIndex);
        for (int i = 0; i < overrides.size(); i++) {
            builder.setSelectionOverride(rendererIndex, trackGroupArray, overrides.get(i));
            addTrackSelection(periodIndex, builder.build());
        }
    }
}
Also used : TrackGroupArray(androidx.media3.common.TrackGroupArray) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector)

Example 4 with Renderer

use of androidx.media3.exoplayer.Renderer in project media by androidx.

the class DefaultTrackSelector method maybeConfigureRenderersForTunneling.

// Utility methods.
/**
 * Determines whether tunneling can be enabled, replacing {@link RendererConfiguration}s in {@code
 * rendererConfigurations} with configurations that enable tunneling on the appropriate renderers
 * if so.
 *
 * @param mappedTrackInfo Mapped track information.
 * @param renderererFormatSupports The {@link Capabilities} for each mapped track, indexed by
 *     renderer, track group and track (in that order).
 * @param rendererConfigurations The renderer configurations. Configurations may be replaced with
 *     ones that enable tunneling as a result of this call.
 * @param trackSelections The renderer track selections.
 */
private static void maybeConfigureRenderersForTunneling(MappedTrackInfo mappedTrackInfo, @Capabilities int[][][] renderererFormatSupports, @NullableType RendererConfiguration[] rendererConfigurations, @NullableType ExoTrackSelection[] trackSelections) {
    // Check whether we can enable tunneling. To enable tunneling we require exactly one audio and
    // one video renderer to support tunneling and have a selection.
    int tunnelingAudioRendererIndex = -1;
    int tunnelingVideoRendererIndex = -1;
    boolean enableTunneling = true;
    for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
        int rendererType = mappedTrackInfo.getRendererType(i);
        ExoTrackSelection trackSelection = trackSelections[i];
        if ((rendererType == C.TRACK_TYPE_AUDIO || rendererType == C.TRACK_TYPE_VIDEO) && trackSelection != null) {
            if (rendererSupportsTunneling(renderererFormatSupports[i], mappedTrackInfo.getTrackGroups(i), trackSelection)) {
                if (rendererType == C.TRACK_TYPE_AUDIO) {
                    if (tunnelingAudioRendererIndex != -1) {
                        enableTunneling = false;
                        break;
                    } else {
                        tunnelingAudioRendererIndex = i;
                    }
                } else {
                    if (tunnelingVideoRendererIndex != -1) {
                        enableTunneling = false;
                        break;
                    } else {
                        tunnelingVideoRendererIndex = i;
                    }
                }
            }
        }
    }
    enableTunneling &= tunnelingAudioRendererIndex != -1 && tunnelingVideoRendererIndex != -1;
    if (enableTunneling) {
        RendererConfiguration tunnelingRendererConfiguration = new RendererConfiguration(/* tunneling= */
        true);
        rendererConfigurations[tunnelingAudioRendererIndex] = tunnelingRendererConfiguration;
        rendererConfigurations[tunnelingVideoRendererIndex] = tunnelingRendererConfiguration;
    }
}
Also used : RendererConfiguration(androidx.media3.exoplayer.RendererConfiguration) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point)

Example 5 with Renderer

use of androidx.media3.exoplayer.Renderer in project media by androidx.

the class DefaultTrackSelector method selectOtherTrack.

// Generic track selection methods.
/**
 * Called by {@link #selectAllTracks(MappedTrackInfo, int[][][], int[], Parameters)} to create a
 * {@link ExoTrackSelection} for a renderer whose type is neither video, audio or text.
 *
 * @param trackType The type of the renderer.
 * @param groups The {@link TrackGroupArray} mapped to the renderer.
 * @param formatSupport The {@link Capabilities} for each mapped track, indexed by track group and
 *     track (in that order).
 * @param params The selector's current constraint parameters.
 * @return The {@link ExoTrackSelection} for the renderer, or null if no selection was made.
 * @throws ExoPlaybackException If an error occurs while selecting the tracks.
 */
@Nullable
protected ExoTrackSelection.Definition selectOtherTrack(int trackType, TrackGroupArray groups, @Capabilities int[][] formatSupport, Parameters params) throws ExoPlaybackException {
    @Nullable TrackGroup selectedGroup = null;
    int selectedTrackIndex = 0;
    @Nullable OtherTrackScore selectedTrackScore = null;
    for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
        TrackGroup trackGroup = groups.get(groupIndex);
        @Capabilities int[] trackFormatSupport = formatSupport[groupIndex];
        for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
            if (isSupported(trackFormatSupport[trackIndex], params.exceedRendererCapabilitiesIfNecessary)) {
                Format format = trackGroup.getFormat(trackIndex);
                OtherTrackScore trackScore = new OtherTrackScore(format, trackFormatSupport[trackIndex]);
                if (selectedTrackScore == null || trackScore.compareTo(selectedTrackScore) > 0) {
                    selectedGroup = trackGroup;
                    selectedTrackIndex = trackIndex;
                    selectedTrackScore = trackScore;
                }
            }
        }
    }
    return selectedGroup == null ? null : new ExoTrackSelection.Definition(selectedGroup, selectedTrackIndex);
}
Also used : Format(androidx.media3.common.Format) TrackGroup(androidx.media3.common.TrackGroup) RendererCapabilities(androidx.media3.exoplayer.RendererCapabilities) Capabilities(androidx.media3.exoplayer.RendererCapabilities.Capabilities) Nullable(androidx.annotation.Nullable) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) Nullable(androidx.annotation.Nullable)

Aggregations

Test (org.junit.Test)36 FakeRenderer (androidx.media3.test.utils.FakeRenderer)27 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)25 FakeTimeline (androidx.media3.test.utils.FakeTimeline)25 Nullable (androidx.annotation.Nullable)24 Format (androidx.media3.common.Format)24 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)24 Timeline (androidx.media3.common.Timeline)22 Player (androidx.media3.common.Player)19 ArrayList (java.util.ArrayList)18 SinglePeriodTimeline (androidx.media3.exoplayer.source.SinglePeriodTimeline)17 NoUidTimeline (androidx.media3.test.utils.NoUidTimeline)17 ActionSchedule (androidx.media3.test.utils.ActionSchedule)16 Listener (androidx.media3.common.Player.Listener)15 ExoPlayerTestRunner (androidx.media3.test.utils.ExoPlayerTestRunner)15 TrackGroupArray (androidx.media3.common.TrackGroupArray)14 TimelineWindowDefinition (androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition)14 C (androidx.media3.common.C)13 MimeTypes (androidx.media3.common.MimeTypes)12 PlaybackParameters (androidx.media3.common.PlaybackParameters)12