Search in sources :

Example 1 with Assertions.checkNotNull

use of androidx.media3.common.util.Assertions.checkNotNull in project media by androidx.

the class TrackSelectionDialog method createForTrackSelector.

/**
 * Creates a dialog for a given {@link DefaultTrackSelector}, whose parameters will be
 * automatically updated when tracks are selected.
 *
 * @param trackSelector The {@link DefaultTrackSelector}.
 * @param onDismissListener A {@link DialogInterface.OnDismissListener} to call when the dialog is
 *     dismissed.
 */
public static TrackSelectionDialog createForTrackSelector(DefaultTrackSelector trackSelector, DialogInterface.OnDismissListener onDismissListener) {
    MappedTrackInfo mappedTrackInfo = Assertions.checkNotNull(trackSelector.getCurrentMappedTrackInfo());
    TrackSelectionDialog trackSelectionDialog = new TrackSelectionDialog();
    DefaultTrackSelector.Parameters parameters = trackSelector.getParameters();
    trackSelectionDialog.init(/* titleId= */
    R.string.track_selection_title, mappedTrackInfo, /* initialParameters = */
    parameters, /* allowAdaptiveSelections= */
    true, /* allowMultipleOverrides= */
    false, /* onClickListener= */
    (dialog, which) -> {
        DefaultTrackSelector.ParametersBuilder builder = parameters.buildUpon();
        for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
            builder.clearSelectionOverrides(/* rendererIndex= */
            i).setRendererDisabled(/* rendererIndex= */
            i, trackSelectionDialog.getIsDisabled(/* rendererIndex= */
            i));
            List<SelectionOverride> overrides = trackSelectionDialog.getOverrides(/* rendererIndex= */
            i);
            if (!overrides.isEmpty()) {
                builder.setSelectionOverride(/* rendererIndex= */
                i, mappedTrackInfo.getTrackGroups(/* rendererIndex= */
                i), overrides.get(0));
            }
        }
        trackSelector.setParameters(builder);
    }, onDismissListener);
    return trackSelectionDialog;
}
Also used : SelectionOverride(androidx.media3.exoplayer.trackselection.DefaultTrackSelector.SelectionOverride) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector) MappedTrackInfo(androidx.media3.exoplayer.trackselection.MappingTrackSelector.MappedTrackInfo)

Example 2 with Assertions.checkNotNull

use of androidx.media3.common.util.Assertions.checkNotNull in project media by androidx.

the class MainActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    playerControlView = findViewById(R.id.player_control_view);
    fullScreenView = findViewById(R.id.full_screen_view);
    fullScreenView.setOnClickListener(v -> {
        setCurrentOutputView(nonFullScreenView);
        Assertions.checkNotNull(fullScreenView).setVisibility(View.GONE);
    });
    attachSurfaceListener(fullScreenView);
    isOwner = getIntent().getBooleanExtra(OWNER_EXTRA, /* defaultValue= */
    true);
    GridLayout gridLayout = findViewById(R.id.grid_layout);
    for (int i = 0; i < 9; i++) {
        View view;
        if (i == 0) {
            Button button = new Button(/* context= */
            this);
            view = button;
            button.setText(getString(R.string.no_output_label));
            button.setOnClickListener(v -> reparent(/* surfaceView= */
            null));
        } else if (i == 1) {
            Button button = new Button(/* context= */
            this);
            view = button;
            button.setText(getString(R.string.full_screen_label));
            button.setOnClickListener(v -> {
                setCurrentOutputView(fullScreenView);
                Assertions.checkNotNull(fullScreenView).setVisibility(View.VISIBLE);
            });
        } else if (i == 2) {
            Button button = new Button(/* context= */
            this);
            view = button;
            button.setText(getString(R.string.new_activity_label));
            button.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, MainActivity.class).putExtra(OWNER_EXTRA, /* value= */
            false)));
        } else {
            SurfaceView surfaceView = new SurfaceView(this);
            view = surfaceView;
            attachSurfaceListener(surfaceView);
            surfaceView.setOnClickListener(v -> {
                setCurrentOutputView(surfaceView);
                nonFullScreenView = surfaceView;
            });
            if (nonFullScreenView == null) {
                nonFullScreenView = surfaceView;
            }
        }
        gridLayout.addView(view);
        GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
        layoutParams.width = 0;
        layoutParams.height = 0;
        layoutParams.columnSpec = GridLayout.spec(i % 3, 1f);
        layoutParams.rowSpec = GridLayout.spec(i / 3, 1f);
        layoutParams.bottomMargin = 10;
        layoutParams.leftMargin = 10;
        layoutParams.topMargin = 10;
        layoutParams.rightMargin = 10;
        view.setLayoutParams(layoutParams);
    }
}
Also used : Bundle(android.os.Bundle) SurfaceView(android.view.SurfaceView) Uri(android.net.Uri) Intent(android.content.Intent) HttpDataSource(androidx.media3.datasource.HttpDataSource) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) View(android.view.View) Button(android.widget.Button) Assertions(androidx.media3.common.util.Assertions) DefaultDataSource(androidx.media3.datasource.DefaultDataSource) MediaItem(androidx.media3.common.MediaItem) SurfaceHolder(android.view.SurfaceHolder) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) GridLayout(android.widget.GridLayout) MediaSource(androidx.media3.exoplayer.source.MediaSource) Player(androidx.media3.common.Player) FrameworkMediaDrm(androidx.media3.exoplayer.drm.FrameworkMediaDrm) Surface(android.view.Surface) ProgressiveMediaSource(androidx.media3.exoplayer.source.ProgressiveMediaSource) UUID(java.util.UUID) Util(androidx.media3.common.util.Util) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpMediaDrmCallback(androidx.media3.exoplayer.drm.HttpMediaDrmCallback) DataSource(androidx.media3.datasource.DataSource) C(androidx.media3.common.C) Nullable(androidx.annotation.Nullable) SurfaceControl(android.view.SurfaceControl) LegacyPlayerControlView(androidx.media3.ui.LegacyPlayerControlView) Activity(android.app.Activity) GridLayout(android.widget.GridLayout) Button(android.widget.Button) Intent(android.content.Intent) SurfaceView(android.view.SurfaceView) View(android.view.View) LegacyPlayerControlView(androidx.media3.ui.LegacyPlayerControlView) SurfaceView(android.view.SurfaceView)

Example 3 with Assertions.checkNotNull

use of androidx.media3.common.util.Assertions.checkNotNull in project media by androidx.

the class MainActivity method initializePlayer.

private void initializePlayer() {
    Intent intent = getIntent();
    String action = intent.getAction();
    Uri uri = ACTION_VIEW.equals(action) ? Assertions.checkNotNull(intent.getData()) : Uri.parse(DEFAULT_MEDIA_URI);
    DrmSessionManager drmSessionManager;
    if (intent.hasExtra(DRM_SCHEME_EXTRA)) {
        String drmScheme = Assertions.checkNotNull(intent.getStringExtra(DRM_SCHEME_EXTRA));
        String drmLicenseUrl = Assertions.checkNotNull(intent.getStringExtra(DRM_LICENSE_URL_EXTRA));
        UUID drmSchemeUuid = Assertions.checkNotNull(Util.getDrmUuid(drmScheme));
        HttpDataSource.Factory licenseDataSourceFactory = new DefaultHttpDataSource.Factory();
        HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(drmLicenseUrl, licenseDataSourceFactory);
        drmSessionManager = new DefaultDrmSessionManager.Builder().setUuidAndExoMediaDrmProvider(drmSchemeUuid, FrameworkMediaDrm.DEFAULT_PROVIDER).build(drmCallback);
    } else {
        drmSessionManager = DrmSessionManager.DRM_UNSUPPORTED;
    }
    DataSource.Factory dataSourceFactory = new DefaultDataSource.Factory(this);
    MediaSource mediaSource;
    @C.ContentType int type = Util.inferContentType(uri, intent.getStringExtra(EXTENSION_EXTRA));
    if (type == C.TYPE_DASH) {
        mediaSource = new DashMediaSource.Factory(dataSourceFactory).setDrmSessionManagerProvider(unusedMediaItem -> drmSessionManager).createMediaSource(MediaItem.fromUri(uri));
    } else if (type == C.TYPE_OTHER) {
        mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).setDrmSessionManagerProvider(unusedMediaItem -> drmSessionManager).createMediaSource(MediaItem.fromUri(uri));
    } else {
        throw new IllegalStateException();
    }
    ExoPlayer player = new ExoPlayer.Builder(getApplicationContext()).build();
    player.setMediaSource(mediaSource);
    player.prepare();
    player.play();
    player.setRepeatMode(Player.REPEAT_MODE_ALL);
    surfaceControl = new SurfaceControl.Builder().setName(SURFACE_CONTROL_NAME).setBufferSize(/* width= */
    0, /* height= */
    0).build();
    videoSurface = new Surface(surfaceControl);
    player.setVideoSurface(videoSurface);
    MainActivity.player = player;
}
Also used : Bundle(android.os.Bundle) SurfaceView(android.view.SurfaceView) Uri(android.net.Uri) Intent(android.content.Intent) HttpDataSource(androidx.media3.datasource.HttpDataSource) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) View(android.view.View) Button(android.widget.Button) Assertions(androidx.media3.common.util.Assertions) DefaultDataSource(androidx.media3.datasource.DefaultDataSource) MediaItem(androidx.media3.common.MediaItem) SurfaceHolder(android.view.SurfaceHolder) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) GridLayout(android.widget.GridLayout) MediaSource(androidx.media3.exoplayer.source.MediaSource) Player(androidx.media3.common.Player) FrameworkMediaDrm(androidx.media3.exoplayer.drm.FrameworkMediaDrm) Surface(android.view.Surface) ProgressiveMediaSource(androidx.media3.exoplayer.source.ProgressiveMediaSource) UUID(java.util.UUID) Util(androidx.media3.common.util.Util) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpMediaDrmCallback(androidx.media3.exoplayer.drm.HttpMediaDrmCallback) DataSource(androidx.media3.datasource.DataSource) C(androidx.media3.common.C) Nullable(androidx.annotation.Nullable) SurfaceControl(android.view.SurfaceControl) LegacyPlayerControlView(androidx.media3.ui.LegacyPlayerControlView) Activity(android.app.Activity) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) Intent(android.content.Intent) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) Uri(android.net.Uri) HttpDataSource(androidx.media3.datasource.HttpDataSource) DefaultDataSource(androidx.media3.datasource.DefaultDataSource) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) DataSource(androidx.media3.datasource.DataSource) Surface(android.view.Surface) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) ProgressiveMediaSource(androidx.media3.exoplayer.source.ProgressiveMediaSource) HttpDataSource(androidx.media3.datasource.HttpDataSource) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpMediaDrmCallback(androidx.media3.exoplayer.drm.HttpMediaDrmCallback) UUID(java.util.UUID)

Example 4 with Assertions.checkNotNull

use of androidx.media3.common.util.Assertions.checkNotNull in project media by androidx.

the class MainActivity method initializePlayer.

private void initializePlayer() {
    Intent intent = getIntent();
    String action = intent.getAction();
    Uri uri = ACTION_VIEW.equals(action) ? Assertions.checkNotNull(intent.getData()) : Uri.parse(DEFAULT_MEDIA_URI);
    DrmSessionManager drmSessionManager;
    if (Util.SDK_INT >= 18 && intent.hasExtra(DRM_SCHEME_EXTRA)) {
        String drmScheme = Assertions.checkNotNull(intent.getStringExtra(DRM_SCHEME_EXTRA));
        String drmLicenseUrl = Assertions.checkNotNull(intent.getStringExtra(DRM_LICENSE_URL_EXTRA));
        UUID drmSchemeUuid = Assertions.checkNotNull(Util.getDrmUuid(drmScheme));
        HttpDataSource.Factory licenseDataSourceFactory = new DefaultHttpDataSource.Factory();
        HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(drmLicenseUrl, licenseDataSourceFactory);
        drmSessionManager = new DefaultDrmSessionManager.Builder().setUuidAndExoMediaDrmProvider(drmSchemeUuid, FrameworkMediaDrm.DEFAULT_PROVIDER).build(drmCallback);
    } else {
        drmSessionManager = DrmSessionManager.DRM_UNSUPPORTED;
    }
    DataSource.Factory dataSourceFactory = new DefaultDataSource.Factory(this);
    MediaSource mediaSource;
    @C.ContentType int type = Util.inferContentType(uri, intent.getStringExtra(EXTENSION_EXTRA));
    if (type == C.TYPE_DASH) {
        mediaSource = new DashMediaSource.Factory(dataSourceFactory).setDrmSessionManagerProvider(unusedMediaItem -> drmSessionManager).createMediaSource(MediaItem.fromUri(uri));
    } else if (type == C.TYPE_OTHER) {
        mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).setDrmSessionManagerProvider(unusedMediaItem -> drmSessionManager).createMediaSource(MediaItem.fromUri(uri));
    } else {
        throw new IllegalStateException();
    }
    ExoPlayer player = new ExoPlayer.Builder(getApplicationContext()).build();
    player.setRepeatMode(Player.REPEAT_MODE_ALL);
    player.setMediaSource(mediaSource);
    player.prepare();
    player.play();
    VideoProcessingGLSurfaceView videoProcessingGLSurfaceView = Assertions.checkNotNull(this.videoProcessingGLSurfaceView);
    videoProcessingGLSurfaceView.setPlayer(player);
    Assertions.checkNotNull(playerView).setPlayer(player);
    player.addAnalyticsListener(new EventLogger(/* trackSelector= */
    null));
    this.player = player;
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) Uri(android.net.Uri) FrameLayout(android.widget.FrameLayout) Intent(android.content.Intent) GlUtil(androidx.media3.common.util.GlUtil) HttpDataSource(androidx.media3.datasource.HttpDataSource) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) Toast(android.widget.Toast) Assertions(androidx.media3.common.util.Assertions) DefaultDataSource(androidx.media3.datasource.DefaultDataSource) MediaItem(androidx.media3.common.MediaItem) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) MediaSource(androidx.media3.exoplayer.source.MediaSource) Player(androidx.media3.common.Player) FrameworkMediaDrm(androidx.media3.exoplayer.drm.FrameworkMediaDrm) ProgressiveMediaSource(androidx.media3.exoplayer.source.ProgressiveMediaSource) UUID(java.util.UUID) Util(androidx.media3.common.util.Util) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpMediaDrmCallback(androidx.media3.exoplayer.drm.HttpMediaDrmCallback) DataSource(androidx.media3.datasource.DataSource) EventLogger(androidx.media3.exoplayer.util.EventLogger) C(androidx.media3.common.C) Nullable(androidx.annotation.Nullable) PlayerView(androidx.media3.ui.PlayerView) Activity(android.app.Activity) EventLogger(androidx.media3.exoplayer.util.EventLogger) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) Intent(android.content.Intent) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) Uri(android.net.Uri) HttpDataSource(androidx.media3.datasource.HttpDataSource) DefaultDataSource(androidx.media3.datasource.DefaultDataSource) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) DataSource(androidx.media3.datasource.DataSource) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) ProgressiveMediaSource(androidx.media3.exoplayer.source.ProgressiveMediaSource) HttpDataSource(androidx.media3.datasource.HttpDataSource) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpMediaDrmCallback(androidx.media3.exoplayer.drm.HttpMediaDrmCallback) UUID(java.util.UUID)

Example 5 with Assertions.checkNotNull

use of androidx.media3.common.util.Assertions.checkNotNull in project media by androidx.

the class OkHttpDataSource method open.

@Override
public long open(DataSpec dataSpec) throws HttpDataSourceException {
    this.dataSpec = dataSpec;
    bytesRead = 0;
    bytesToRead = 0;
    transferInitializing(dataSpec);
    Request request = makeRequest(dataSpec);
    Response response;
    ResponseBody responseBody;
    try {
        this.response = callFactory.newCall(request).execute();
        response = this.response;
        responseBody = Assertions.checkNotNull(response.body());
        responseByteStream = responseBody.byteStream();
    } catch (IOException e) {
        throw HttpDataSourceException.createForIOException(e, dataSpec, HttpDataSourceException.TYPE_OPEN);
    }
    int responseCode = response.code();
    // Check for a valid response code.
    if (!response.isSuccessful()) {
        if (responseCode == 416) {
            long documentSize = HttpUtil.getDocumentSize(response.headers().get(HttpHeaders.CONTENT_RANGE));
            if (dataSpec.position == documentSize) {
                opened = true;
                transferStarted(dataSpec);
                return dataSpec.length != C.LENGTH_UNSET ? dataSpec.length : 0;
            }
        }
        byte[] errorResponseBody;
        try {
            errorResponseBody = Util.toByteArray(Assertions.checkNotNull(responseByteStream));
        } catch (IOException e) {
            errorResponseBody = Util.EMPTY_BYTE_ARRAY;
        }
        Map<String, List<String>> headers = response.headers().toMultimap();
        closeConnectionQuietly();
        @Nullable IOException cause = responseCode == 416 ? new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE) : null;
        throw new InvalidResponseCodeException(responseCode, response.message(), cause, headers, dataSpec, errorResponseBody);
    }
    // Check for a valid content type.
    @Nullable MediaType mediaType = responseBody.contentType();
    String contentType = mediaType != null ? mediaType.toString() : "";
    if (contentTypePredicate != null && !contentTypePredicate.apply(contentType)) {
        closeConnectionQuietly();
        throw new InvalidContentTypeException(contentType, dataSpec);
    }
    // If we requested a range starting from a non-zero position and received a 200 rather than a
    // 206, then the server does not support partial requests. We'll need to manually skip to the
    // requested position.
    long bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0;
    // Determine the length of the data to be read, after skipping.
    if (dataSpec.length != C.LENGTH_UNSET) {
        bytesToRead = dataSpec.length;
    } else {
        long contentLength = responseBody.contentLength();
        bytesToRead = contentLength != -1 ? (contentLength - bytesToSkip) : C.LENGTH_UNSET;
    }
    opened = true;
    transferStarted(dataSpec);
    try {
        skipFully(bytesToSkip, dataSpec);
    } catch (HttpDataSourceException e) {
        closeConnectionQuietly();
        throw e;
    }
    return bytesToRead;
}
Also used : Request(okhttp3.Request) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) DataSourceException(androidx.media3.datasource.DataSourceException) MediaType(okhttp3.MediaType) List(java.util.List) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)28 Format (androidx.media3.common.Format)10 ArrayList (java.util.ArrayList)7 Uri (android.net.Uri)5 TrackGroup (androidx.media3.common.TrackGroup)5 Matcher (java.util.regex.Matcher)5 SuppressLint (android.annotation.SuppressLint)4 Context (android.content.Context)4 MediaItem (androidx.media3.common.MediaItem)4 Cue (androidx.media3.common.text.Cue)4 DataSource (androidx.media3.datasource.DataSource)4 StatsDataSource (androidx.media3.datasource.StatsDataSource)4 ExoPlayer (androidx.media3.exoplayer.ExoPlayer)4 MediaSource (androidx.media3.exoplayer.source.MediaSource)4 Activity (android.app.Activity)3 Intent (android.content.Intent)3 Bundle (android.os.Bundle)3 C (androidx.media3.common.C)3 Player (androidx.media3.common.Player)3 TrackGroupArray (androidx.media3.common.TrackGroupArray)3