Search in sources :

Example 1 with AudioRendererEventListener

use of androidx.media3.exoplayer.audio.AudioRendererEventListener 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 AudioRendererEventListener

use of androidx.media3.exoplayer.audio.AudioRendererEventListener 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 AudioRendererEventListener

use of androidx.media3.exoplayer.audio.AudioRendererEventListener in project media by androidx.

the class TestExoPlayerBuilder method build.

/**
 * Builds an {@link ExoPlayer} using the provided values or their defaults.
 */
public ExoPlayer build() {
    Assertions.checkNotNull(looper, "TestExoPlayer builder run on a thread without Looper and no Looper specified.");
    // Do not update renderersFactory and renderers here, otherwise their getters may
    // return different values before and after build() is called, making them confusing.
    RenderersFactory playerRenderersFactory = renderersFactory;
    if (playerRenderersFactory == null) {
        playerRenderersFactory = (eventHandler, videoRendererEventListener, audioRendererEventListener, textRendererOutput, metadataRendererOutput) -> renderers != null ? renderers : new Renderer[] { new FakeVideoRenderer(eventHandler, videoRendererEventListener), new FakeAudioRenderer(eventHandler, audioRendererEventListener) };
    }
    ExoPlayer.Builder builder = new ExoPlayer.Builder(context, playerRenderersFactory).setTrackSelector(trackSelector).setLoadControl(loadControl).setBandwidthMeter(bandwidthMeter).setAnalyticsCollector(new DefaultAnalyticsCollector(clock)).setClock(clock).setUseLazyPreparation(useLazyPreparation).setLooper(looper).setSeekBackIncrementMs(seekBackIncrementMs).setSeekForwardIncrementMs(seekForwardIncrementMs);
    if (mediaSourceFactory != null) {
        builder.setMediaSourceFactory(mediaSourceFactory);
    }
    return builder.build();
}
Also used : Renderer(androidx.media3.exoplayer.Renderer) RenderersFactory(androidx.media3.exoplayer.RenderersFactory) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) DefaultAnalyticsCollector(androidx.media3.exoplayer.analytics.DefaultAnalyticsCollector)

Example 4 with AudioRendererEventListener

use of androidx.media3.exoplayer.audio.AudioRendererEventListener in project media by androidx.

the class MediaCodecAudioRendererTest method supportsFormat_withEac3JocMediaAndEac3Decoder_returnsTrue.

@Test
public void supportsFormat_withEac3JocMediaAndEac3Decoder_returnsTrue() throws Exception {
    Format mediaFormat = new Format.Builder().setSampleMimeType(MimeTypes.AUDIO_E_AC3_JOC).setCodecs(MimeTypes.CODEC_E_AC3_JOC).build();
    MediaCodecSelector mediaCodecSelector = (mimeType, requiresSecureDecoder, requiresTunnelingDecoder) -> !mimeType.equals(MimeTypes.AUDIO_E_AC3) ? ImmutableList.of() : ImmutableList.of(MediaCodecInfo.newInstance(/* name= */
    "eac3-codec", /* mimeType= */
    mimeType, /* codecMimeType= */
    mimeType, /* capabilities= */
    null, /* hardwareAccelerated= */
    false, /* softwareOnly= */
    true, /* vendor= */
    false, /* forceDisableAdaptive= */
    false, /* forceSecure= */
    false));
    MediaCodecAudioRenderer renderer = new MediaCodecAudioRenderer(ApplicationProvider.getApplicationContext(), mediaCodecSelector, /* enableDecoderFallback= */
    false, /* eventHandler= */
    new Handler(Looper.getMainLooper()), audioRendererEventListener, audioSink);
    renderer.init(/* index= */
    0, PlayerId.UNSET);
    @Capabilities int capabilities = renderer.supportsFormat(mediaFormat);
    assertThat(RendererCapabilities.getFormatSupport(capabilities)).isEqualTo(C.FORMAT_HANDLED);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ExoPlaybackException(androidx.media3.exoplayer.ExoPlaybackException) MediaFormat(android.media.MediaFormat) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Shadows.shadowOf(org.robolectric.Shadows.shadowOf) RendererCapabilities(androidx.media3.exoplayer.RendererCapabilities) Capabilities(androidx.media3.exoplayer.RendererCapabilities.Capabilities) ApplicationProvider(androidx.test.core.app.ApplicationProvider) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) MediaCodecInfo(androidx.media3.exoplayer.mediacodec.MediaCodecInfo) END_OF_STREAM_ITEM(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Handler(android.os.Handler) Looper(android.os.Looper) MockitoJUnit(org.mockito.junit.MockitoJUnit) RendererConfiguration(androidx.media3.exoplayer.RendererConfiguration) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Before(org.junit.Before) MimeTypes(androidx.media3.common.MimeTypes) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) PlaybackException(androidx.media3.common.PlaybackException) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) Format(androidx.media3.common.Format) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) C(androidx.media3.common.C) MediaCodecSelector(androidx.media3.exoplayer.mediacodec.MediaCodecSelector) Nullable(androidx.annotation.Nullable) Rule(org.junit.Rule) FakeSampleStreamItem.oneByteSample(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.oneByteSample) PlayerId(androidx.media3.exoplayer.analytics.PlayerId) FakeSampleStreamItem.format(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.format) MockitoRule(org.mockito.junit.MockitoRule) Collections(java.util.Collections) MediaCodecSelector(androidx.media3.exoplayer.mediacodec.MediaCodecSelector) MediaFormat(android.media.MediaFormat) Format(androidx.media3.common.Format) RendererCapabilities(androidx.media3.exoplayer.RendererCapabilities) Capabilities(androidx.media3.exoplayer.RendererCapabilities.Capabilities) Handler(android.os.Handler) Test(org.junit.Test)

Example 5 with AudioRendererEventListener

use of androidx.media3.exoplayer.audio.AudioRendererEventListener in project media by androidx.

the class DefaultRenderersFactory method createRenderers.

@Override
public Renderer[] createRenderers(Handler eventHandler, VideoRendererEventListener videoRendererEventListener, AudioRendererEventListener audioRendererEventListener, TextOutput textRendererOutput, MetadataOutput metadataRendererOutput) {
    ArrayList<Renderer> renderersList = new ArrayList<>();
    buildVideoRenderers(context, extensionRendererMode, mediaCodecSelector, enableDecoderFallback, eventHandler, videoRendererEventListener, allowedVideoJoiningTimeMs, renderersList);
    @Nullable AudioSink audioSink = buildAudioSink(context, enableFloatOutput, enableAudioTrackPlaybackParams, enableOffload);
    if (audioSink != null) {
        buildAudioRenderers(context, extensionRendererMode, mediaCodecSelector, enableDecoderFallback, audioSink, eventHandler, audioRendererEventListener, renderersList);
    }
    buildTextRenderers(context, textRendererOutput, eventHandler.getLooper(), extensionRendererMode, renderersList);
    buildMetadataRenderers(context, metadataRendererOutput, eventHandler.getLooper(), extensionRendererMode, renderersList);
    buildCameraMotionRenderers(context, extensionRendererMode, renderersList);
    buildMiscellaneousRenderers(context, eventHandler, extensionRendererMode, renderersList);
    return renderersList.toArray(new Renderer[0]);
}
Also used : AudioSink(androidx.media3.exoplayer.audio.AudioSink) DefaultAudioSink(androidx.media3.exoplayer.audio.DefaultAudioSink) 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) ArrayList(java.util.ArrayList) Nullable(androidx.annotation.Nullable)

Aggregations

MediaFormat (android.media.MediaFormat)2 Handler (android.os.Handler)2 Nullable (androidx.annotation.Nullable)2 Format (androidx.media3.common.Format)2 PlaybackException (androidx.media3.common.PlaybackException)2 ExoPlaybackException (androidx.media3.exoplayer.ExoPlaybackException)2 Renderer (androidx.media3.exoplayer.Renderer)2 RendererCapabilities (androidx.media3.exoplayer.RendererCapabilities)2 MediaCodecAudioRenderer (androidx.media3.exoplayer.audio.MediaCodecAudioRenderer)2 DrmSessionEventListener (androidx.media3.exoplayer.drm.DrmSessionEventListener)2 MetadataRenderer (androidx.media3.exoplayer.metadata.MetadataRenderer)2 TextRenderer (androidx.media3.exoplayer.text.TextRenderer)2 MediaCodecVideoRenderer (androidx.media3.exoplayer.video.MediaCodecVideoRenderer)2 CameraMotionRenderer (androidx.media3.exoplayer.video.spherical.CameraMotionRenderer)2 Looper (android.os.Looper)1 SystemClock (android.os.SystemClock)1 C (androidx.media3.common.C)1 MimeTypes (androidx.media3.common.MimeTypes)1 ExoPlayer (androidx.media3.exoplayer.ExoPlayer)1 Capabilities (androidx.media3.exoplayer.RendererCapabilities.Capabilities)1