Search in sources :

Example 1 with EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener 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 EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener in project media by androidx.

the class AdTagLoader method addListenerWithAdView.

/**
 * Starts passing events from this instance (including any pending ad playback state) and
 * registers obstructions.
 */
public void addListenerWithAdView(EventListener eventListener, AdViewProvider adViewProvider) {
    boolean isStarted = !eventListeners.isEmpty();
    eventListeners.add(eventListener);
    if (isStarted) {
        if (!AdPlaybackState.NONE.equals(adPlaybackState)) {
            // Pass the existing ad playback state to the new listener.
            eventListener.onAdPlaybackState(adPlaybackState);
        }
        return;
    }
    lastVolumePercent = 0;
    lastAdProgress = VideoProgressUpdate.VIDEO_TIME_NOT_READY;
    lastContentProgress = VideoProgressUpdate.VIDEO_TIME_NOT_READY;
    maybeNotifyPendingAdLoadError();
    if (!AdPlaybackState.NONE.equals(adPlaybackState)) {
        // Pass the ad playback state to the player, and resume ads if necessary.
        eventListener.onAdPlaybackState(adPlaybackState);
    } else if (adsManager != null) {
        adPlaybackState = new AdPlaybackState(adsId, getAdGroupTimesUsForCuePoints(adsManager.getAdCuePoints()));
        updateAdPlaybackState();
    }
    for (AdOverlayInfo overlayInfo : adViewProvider.getAdOverlayInfos()) {
        adDisplayContainer.registerFriendlyObstruction(imaFactory.createFriendlyObstruction(overlayInfo.view, ImaUtil.getFriendlyObstructionPurpose(overlayInfo.purpose), overlayInfo.reasonDetail));
    }
}
Also used : AdOverlayInfo(androidx.media3.common.AdOverlayInfo) AdPlaybackState(androidx.media3.common.AdPlaybackState)

Example 3 with EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener in project media by androidx.

the class CacheDataSourceTest2 method buildCacheDataSource.

private static CacheDataSource buildCacheDataSource(Context context, DataSource upstreamSource, boolean useAesEncryption) throws CacheException {
    File cacheDir = context.getExternalCacheDir();
    Cache cache = new SimpleCache(new File(cacheDir, EXO_CACHE_DIR), new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
    emptyCache(cache);
    // Source and cipher
    final String secretKey = "testKey:12345678";
    DataSource file = new FileDataSource();
    DataSource cacheReadDataSource = useAesEncryption ? new AesCipherDataSource(Util.getUtf8Bytes(secretKey), file) : file;
    // Sink and cipher
    CacheDataSink cacheSink = new CacheDataSink(cache, EXO_CACHE_MAX_FILESIZE);
    byte[] scratch = new byte[3897];
    DataSink cacheWriteDataSink = useAesEncryption ? new AesCipherDataSink(Util.getUtf8Bytes(secretKey), cacheSink, scratch) : cacheSink;
    return new CacheDataSource(cache, upstreamSource, cacheReadDataSource, cacheWriteDataSink, CacheDataSource.FLAG_BLOCK_ON_CACHE, // eventListener
    null);
}
Also used : DataSink(androidx.media3.datasource.DataSink) AesCipherDataSink(androidx.media3.datasource.AesCipherDataSink) FileDataSource(androidx.media3.datasource.FileDataSource) DataSource(androidx.media3.datasource.DataSource) FakeDataSource(androidx.media3.test.utils.FakeDataSource) AesCipherDataSource(androidx.media3.datasource.AesCipherDataSource) AesCipherDataSink(androidx.media3.datasource.AesCipherDataSink) FileDataSource(androidx.media3.datasource.FileDataSource) AesCipherDataSource(androidx.media3.datasource.AesCipherDataSource) File(java.io.File)

Example 4 with EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener in project media by androidx.

the class MediaSourceList method prepareChildSource.

private void prepareChildSource(MediaSourceHolder holder) {
    MediaSource mediaSource = holder.mediaSource;
    MediaSource.MediaSourceCaller caller = (source, timeline) -> mediaSourceListInfoListener.onPlaylistUpdateRequested();
    ForwardingEventListener eventListener = new ForwardingEventListener(holder);
    childSources.put(holder, new MediaSourceAndListener(mediaSource, caller, eventListener));
    mediaSource.addEventListener(Util.createHandlerForCurrentOrMainLooper(), eventListener);
    mediaSource.addDrmEventListener(Util.createHandlerForCurrentOrMainLooper(), eventListener);
    mediaSource.prepareSource(caller, mediaTransferListener, playerId);
}
Also used : LoadEventInfo(androidx.media3.exoplayer.source.LoadEventInfo) Allocator(androidx.media3.exoplayer.upstream.Allocator) HashMap(java.util.HashMap) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Handler(android.os.Handler) Map(java.util.Map) Assertions(androidx.media3.common.util.Assertions) MaskingMediaPeriod(androidx.media3.exoplayer.source.MaskingMediaPeriod) AnalyticsCollector(androidx.media3.exoplayer.analytics.AnalyticsCollector) TransferListener(androidx.media3.datasource.TransferListener) MediaSource(androidx.media3.exoplayer.source.MediaSource) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) IdentityHashMap(java.util.IdentityHashMap) Iterator(java.util.Iterator) DrmSession(androidx.media3.exoplayer.drm.DrmSession) MediaLoadData(androidx.media3.exoplayer.source.MediaLoadData) Set(java.util.Set) IOException(java.io.IOException) Math.min(java.lang.Math.min) ShuffleOrder(androidx.media3.exoplayer.source.ShuffleOrder) DefaultShuffleOrder(androidx.media3.exoplayer.source.ShuffleOrder.DefaultShuffleOrder) Util(androidx.media3.common.util.Util) List(java.util.List) Nullable(androidx.annotation.Nullable) Timeline(androidx.media3.common.Timeline) PlayerId(androidx.media3.exoplayer.analytics.PlayerId) Log(androidx.media3.common.util.Log) Math.max(java.lang.Math.max) MediaPeriod(androidx.media3.exoplayer.source.MediaPeriod) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource)

Example 5 with EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener in project media by androidx.

the class DecoderVideoRendererTest method enable_withMayRenderStartOfStream_rendersFirstFrameBeforeStart.

@Test
public void enable_withMayRenderStartOfStream_rendersFirstFrameBeforeStart() throws Exception {
    FakeSampleStream fakeSampleStream = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    H264_FORMAT, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME)));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    renderer.enable(RendererConfiguration.DEFAULT, new Format[] { H264_FORMAT }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0L, /* offsetUs */
    0);
    for (int i = 0; i < 10; i++) {
        renderer.render(/* positionUs= */
        0, SystemClock.elapsedRealtime() * 1000);
        // Ensure pending messages are delivered.
        ShadowLooper.idleMainLooper();
    }
    verify(eventListener).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
Also used : FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Test(org.junit.Test)

Aggregations

DrmSessionEventListener (androidx.media3.exoplayer.drm.DrmSessionEventListener)18 Test (org.junit.Test)18 FakeSampleStream (androidx.media3.test.utils.FakeSampleStream)17 DefaultAllocator (androidx.media3.exoplayer.upstream.DefaultAllocator)16 Nullable (androidx.annotation.Nullable)8 Format (androidx.media3.common.Format)6 Before (org.junit.Before)6 ShadowLooper (org.robolectric.shadows.ShadowLooper)6 SurfaceTexture (android.graphics.SurfaceTexture)5 Handler (android.os.Handler)5 Surface (android.view.Surface)5 MediaFormat (android.media.MediaFormat)4 Looper (android.os.Looper)4 C (androidx.media3.common.C)4 MimeTypes (androidx.media3.common.MimeTypes)4 VideoSize (androidx.media3.common.VideoSize)4 RendererCapabilities (androidx.media3.exoplayer.RendererCapabilities)4 PlayerId (androidx.media3.exoplayer.analytics.PlayerId)4 CodecCapabilities (android.media.MediaCodecInfo.CodecCapabilities)3 CodecProfileLevel (android.media.MediaCodecInfo.CodecProfileLevel)3