Search in sources :

Example 1 with DrmSessionException

use of com.google.android.exoplayer2.drm.DrmSession.DrmSessionException in project ExoPlayer by google.

the class OfflineLicenseHelper method download.

/**
   * Downloads an offline license.
   *
   * @param dataSource The {@link HttpDataSource} to be used for download.
   * @param dashManifest The {@link DashManifest} of the DASH content.
   * @return The downloaded offline license key set id.
   * @throws IOException If an error occurs reading data from the stream.
   * @throws InterruptedException If the thread has been interrupted.
   * @throws DrmSessionException Thrown when there is an error during DRM session.
   */
public byte[] download(HttpDataSource dataSource, DashManifest dashManifest) throws IOException, InterruptedException, DrmSessionException {
    // as per DASH IF Interoperability Recommendations V3.0, 7.5.3.
    if (dashManifest.getPeriodCount() < 1) {
        return null;
    }
    Period period = dashManifest.getPeriod(0);
    int adaptationSetIndex = period.getAdaptationSetIndex(C.TRACK_TYPE_VIDEO);
    if (adaptationSetIndex == C.INDEX_UNSET) {
        adaptationSetIndex = period.getAdaptationSetIndex(C.TRACK_TYPE_AUDIO);
        if (adaptationSetIndex == C.INDEX_UNSET) {
            return null;
        }
    }
    AdaptationSet adaptationSet = period.adaptationSets.get(adaptationSetIndex);
    if (adaptationSet.representations.isEmpty()) {
        return null;
    }
    Representation representation = adaptationSet.representations.get(0);
    DrmInitData drmInitData = representation.format.drmInitData;
    if (drmInitData == null) {
        Format sampleFormat = DashUtil.loadSampleFormat(dataSource, representation);
        if (sampleFormat != null) {
            drmInitData = sampleFormat.drmInitData;
        }
        if (drmInitData == null) {
            return null;
        }
    }
    blockingKeyRequest(DefaultDrmSessionManager.MODE_DOWNLOAD, null, drmInitData);
    return drmSessionManager.getOfflineLicenseKeySetId();
}
Also used : Format(com.google.android.exoplayer2.Format) Period(com.google.android.exoplayer2.source.dash.manifest.Period) AdaptationSet(com.google.android.exoplayer2.source.dash.manifest.AdaptationSet) Representation(com.google.android.exoplayer2.source.dash.manifest.Representation)

Example 2 with DrmSessionException

use of com.google.android.exoplayer2.drm.DrmSession.DrmSessionException in project ExoPlayer by google.

the class MediaMetricsListener method getErrorInfo.

private static ErrorInfo getErrorInfo(PlaybackException error, Context context, boolean lastIoErrorForManifest) {
    if (error.errorCode == PlaybackException.ERROR_CODE_REMOTE_ERROR) {
        return new ErrorInfo(PlaybackErrorEvent.ERROR_PLAYER_REMOTE, /* subErrorCode= */
        0);
    }
    // Unpack the PlaybackException.
    // TODO(b/190203080): Use error codes instead of the Exception's cause where possible.
    boolean isRendererExoPlaybackException = false;
    int rendererFormatSupport = C.FORMAT_UNSUPPORTED_TYPE;
    if (error instanceof ExoPlaybackException) {
        ExoPlaybackException exoPlaybackException = (ExoPlaybackException) error;
        isRendererExoPlaybackException = exoPlaybackException.type == ExoPlaybackException.TYPE_RENDERER;
        rendererFormatSupport = exoPlaybackException.rendererFormatSupport;
    }
    Throwable cause = checkNotNull(error.getCause());
    if (cause instanceof IOException) {
        if (cause instanceof HttpDataSource.InvalidResponseCodeException) {
            int responseCode = ((HttpDataSource.InvalidResponseCodeException) cause).responseCode;
            return new ErrorInfo(PlaybackErrorEvent.ERROR_IO_BAD_HTTP_STATUS, /* subErrorCode= */
            responseCode);
        } else if (cause instanceof HttpDataSource.InvalidContentTypeException || cause instanceof ParserException) {
            return new ErrorInfo(lastIoErrorForManifest ? PlaybackErrorEvent.ERROR_PARSING_MANIFEST_MALFORMED : PlaybackErrorEvent.ERROR_PARSING_CONTAINER_MALFORMED, /* subErrorCode= */
            0);
        } else if (cause instanceof HttpDataSource.HttpDataSourceException || cause instanceof UdpDataSource.UdpDataSourceException) {
            if (NetworkTypeObserver.getInstance(context).getNetworkType() == C.NETWORK_TYPE_OFFLINE) {
                return new ErrorInfo(PlaybackErrorEvent.ERROR_IO_NETWORK_UNAVAILABLE, /* subErrorCode= */
                0);
            } else {
                @Nullable Throwable detailedCause = cause.getCause();
                if (detailedCause instanceof UnknownHostException) {
                    return new ErrorInfo(PlaybackErrorEvent.ERROR_IO_DNS_FAILED, /* subErrorCode= */
                    0);
                } else if (detailedCause instanceof SocketTimeoutException) {
                    return new ErrorInfo(PlaybackErrorEvent.ERROR_IO_CONNECTION_TIMEOUT, /* subErrorCode= */
                    0);
                } else if (cause instanceof HttpDataSource.HttpDataSourceException && ((HttpDataSource.HttpDataSourceException) cause).type == HttpDataSource.HttpDataSourceException.TYPE_OPEN) {
                    return new ErrorInfo(PlaybackErrorEvent.ERROR_IO_NETWORK_CONNECTION_FAILED, /* subErrorCode= */
                    0);
                } else {
                    return new ErrorInfo(PlaybackErrorEvent.ERROR_IO_CONNECTION_CLOSED, /* subErrorCode= */
                    0);
                }
            }
        } else if (error.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) {
            return new ErrorInfo(PlaybackErrorEvent.ERROR_PLAYER_BEHIND_LIVE_WINDOW, /* subErrorCode= */
            0);
        } else if (cause instanceof DrmSession.DrmSessionException) {
            // Unpack DrmSessionException.
            cause = checkNotNull(cause.getCause());
            if (Util.SDK_INT >= 21 && cause instanceof MediaDrm.MediaDrmStateException) {
                String diagnosticsInfo = ((MediaDrm.MediaDrmStateException) cause).getDiagnosticInfo();
                int subErrorCode = Util.getErrorCodeFromPlatformDiagnosticsInfo(diagnosticsInfo);
                int errorCode = getDrmErrorCode(subErrorCode);
                return new ErrorInfo(errorCode, subErrorCode);
            } else if (Util.SDK_INT >= 23 && cause instanceof MediaDrmResetException) {
                return new ErrorInfo(PlaybackErrorEvent.ERROR_DRM_SYSTEM_ERROR, /* subErrorCode= */
                0);
            } else if (Util.SDK_INT >= 18 && cause instanceof NotProvisionedException) {
                return new ErrorInfo(PlaybackErrorEvent.ERROR_DRM_PROVISIONING_FAILED, /* subErrorCode= */
                0);
            } else if (Util.SDK_INT >= 18 && cause instanceof DeniedByServerException) {
                return new ErrorInfo(PlaybackErrorEvent.ERROR_DRM_DEVICE_REVOKED, /* subErrorCode= */
                0);
            } else if (cause instanceof UnsupportedDrmException) {
                return new ErrorInfo(PlaybackErrorEvent.ERROR_DRM_SCHEME_UNSUPPORTED, /* subErrorCode= */
                0);
            } else if (cause instanceof DefaultDrmSessionManager.MissingSchemeDataException) {
                return new ErrorInfo(PlaybackErrorEvent.ERROR_DRM_CONTENT_ERROR, /* subErrorCode= */
                0);
            } else {
                return new ErrorInfo(PlaybackErrorEvent.ERROR_DRM_OTHER, /* subErrorCode= */
                0);
            }
        } else if (cause instanceof FileDataSource.FileDataSourceException && cause.getCause() instanceof FileNotFoundException) {
            @Nullable Throwable notFoundCause = checkNotNull(cause.getCause()).getCause();
            if (Util.SDK_INT >= 21 && notFoundCause instanceof ErrnoException && ((ErrnoException) notFoundCause).errno == OsConstants.EACCES) {
                return new ErrorInfo(PlaybackErrorEvent.ERROR_IO_NO_PERMISSION, /* subErrorCode= */
                0);
            } else {
                return new ErrorInfo(PlaybackErrorEvent.ERROR_IO_FILE_NOT_FOUND, /* subErrorCode= */
                0);
            }
        } else {
            return new ErrorInfo(PlaybackErrorEvent.ERROR_IO_OTHER, /* subErrorCode= */
            0);
        }
    } else if (isRendererExoPlaybackException && (rendererFormatSupport == C.FORMAT_UNSUPPORTED_TYPE || rendererFormatSupport == C.FORMAT_UNSUPPORTED_SUBTYPE)) {
        return new ErrorInfo(PlaybackErrorEvent.ERROR_DECODING_FORMAT_UNSUPPORTED, /* subErrorCode= */
        0);
    } else if (isRendererExoPlaybackException && rendererFormatSupport == C.FORMAT_EXCEEDS_CAPABILITIES) {
        return new ErrorInfo(PlaybackErrorEvent.ERROR_DECODING_FORMAT_EXCEEDS_CAPABILITIES, /* subErrorCode= */
        0);
    } else if (isRendererExoPlaybackException && rendererFormatSupport == C.FORMAT_UNSUPPORTED_DRM) {
        return new ErrorInfo(PlaybackErrorEvent.ERROR_DRM_SCHEME_UNSUPPORTED, /* subErrorCode= */
        0);
    } else if (cause instanceof MediaCodecRenderer.DecoderInitializationException) {
        @Nullable String diagnosticsInfo = ((MediaCodecRenderer.DecoderInitializationException) cause).diagnosticInfo;
        int subErrorCode = Util.getErrorCodeFromPlatformDiagnosticsInfo(diagnosticsInfo);
        return new ErrorInfo(PlaybackErrorEvent.ERROR_DECODER_INIT_FAILED, subErrorCode);
    } else if (cause instanceof MediaCodecDecoderException) {
        @Nullable String diagnosticsInfo = ((MediaCodecDecoderException) cause).diagnosticInfo;
        int subErrorCode = Util.getErrorCodeFromPlatformDiagnosticsInfo(diagnosticsInfo);
        return new ErrorInfo(PlaybackErrorEvent.ERROR_DECODING_FAILED, subErrorCode);
    } else if (cause instanceof OutOfMemoryError) {
        return new ErrorInfo(PlaybackErrorEvent.ERROR_DECODING_FAILED, /* subErrorCode= */
        0);
    } else if (cause instanceof AudioSink.InitializationException) {
        int subErrorCode = ((AudioSink.InitializationException) cause).audioTrackState;
        return new ErrorInfo(PlaybackErrorEvent.ERROR_AUDIO_TRACK_INIT_FAILED, subErrorCode);
    } else if (cause instanceof AudioSink.WriteException) {
        int subErrorCode = ((AudioSink.WriteException) cause).errorCode;
        return new ErrorInfo(PlaybackErrorEvent.ERROR_AUDIO_TRACK_WRITE_FAILED, subErrorCode);
    } else if (Util.SDK_INT >= 16 && cause instanceof MediaCodec.CryptoException) {
        int subErrorCode = ((MediaCodec.CryptoException) cause).getErrorCode();
        int errorCode = getDrmErrorCode(subErrorCode);
        return new ErrorInfo(errorCode, subErrorCode);
    } else {
        return new ErrorInfo(PlaybackErrorEvent.ERROR_PLAYER_OTHER, /* subErrorCode= */
        0);
    }
}
Also used : AudioSink(com.google.android.exoplayer2.audio.AudioSink) DefaultDrmSessionManager(com.google.android.exoplayer2.drm.DefaultDrmSessionManager) FileNotFoundException(java.io.FileNotFoundException) MediaDrm(android.media.MediaDrm) MediaCodec(android.media.MediaCodec) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) NotProvisionedException(android.media.NotProvisionedException) UnsupportedDrmException(com.google.android.exoplayer2.drm.UnsupportedDrmException) ParserException(com.google.android.exoplayer2.ParserException) UnknownHostException(java.net.UnknownHostException) MediaDrmResetException(android.media.MediaDrmResetException) ExoPlaybackException(com.google.android.exoplayer2.ExoPlaybackException) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) DeniedByServerException(android.media.DeniedByServerException) SocketTimeoutException(java.net.SocketTimeoutException) ErrnoException(android.system.ErrnoException) UdpDataSource(com.google.android.exoplayer2.upstream.UdpDataSource) MediaCodecDecoderException(com.google.android.exoplayer2.mediacodec.MediaCodecDecoderException) Nullable(androidx.annotation.Nullable)

Example 3 with DrmSessionException

use of com.google.android.exoplayer2.drm.DrmSession.DrmSessionException in project ExoPlayer by google.

the class DecoderAudioRenderer method maybeInitDecoder.

private void maybeInitDecoder() throws ExoPlaybackException {
    if (decoder != null) {
        return;
    }
    setDecoderDrmSession(sourceDrmSession);
    CryptoConfig cryptoConfig = null;
    if (decoderDrmSession != null) {
        cryptoConfig = decoderDrmSession.getCryptoConfig();
        if (cryptoConfig == null) {
            DrmSessionException drmError = decoderDrmSession.getError();
            if (drmError != null) {
            // Continue for now. We may be able to avoid failure if a new input format causes the
            // session to be replaced without it having been used.
            } else {
                // The drm session isn't open yet.
                return;
            }
        }
    }
    try {
        long codecInitializingTimestamp = SystemClock.elapsedRealtime();
        TraceUtil.beginSection("createAudioDecoder");
        decoder = createDecoder(inputFormat, cryptoConfig);
        TraceUtil.endSection();
        long codecInitializedTimestamp = SystemClock.elapsedRealtime();
        eventDispatcher.decoderInitialized(decoder.getName(), codecInitializedTimestamp, codecInitializedTimestamp - codecInitializingTimestamp);
        decoderCounters.decoderInitCount++;
    } catch (DecoderException e) {
        Log.e(TAG, "Audio codec error", e);
        eventDispatcher.audioCodecError(e);
        throw createRendererException(e, inputFormat, PlaybackException.ERROR_CODE_DECODER_INIT_FAILED);
    } catch (OutOfMemoryError e) {
        throw createRendererException(e, inputFormat, PlaybackException.ERROR_CODE_DECODER_INIT_FAILED);
    }
}
Also used : DecoderException(com.google.android.exoplayer2.decoder.DecoderException) DrmSessionException(com.google.android.exoplayer2.drm.DrmSession.DrmSessionException) CryptoConfig(com.google.android.exoplayer2.decoder.CryptoConfig)

Example 4 with DrmSessionException

use of com.google.android.exoplayer2.drm.DrmSession.DrmSessionException in project ExoPlayer by google.

the class DefaultDrmSessionManager method acquireSession.

// Must be called on the playback thread.
@Nullable
private DrmSession acquireSession(Looper playbackLooper, @Nullable DrmSessionEventListener.EventDispatcher eventDispatcher, Format format, boolean shouldReleasePreacquiredSessionsBeforeRetrying) {
    maybeCreateMediaDrmHandler(playbackLooper);
    if (format.drmInitData == null) {
        // Content is not encrypted.
        return maybeAcquirePlaceholderSession(MimeTypes.getTrackType(format.sampleMimeType), shouldReleasePreacquiredSessionsBeforeRetrying);
    }
    @Nullable List<SchemeData> schemeDatas = null;
    if (offlineLicenseKeySetId == null) {
        schemeDatas = getSchemeDatas(checkNotNull(format.drmInitData), uuid, false);
        if (schemeDatas.isEmpty()) {
            final MissingSchemeDataException error = new MissingSchemeDataException(uuid);
            Log.e(TAG, "DRM error", error);
            if (eventDispatcher != null) {
                eventDispatcher.drmSessionManagerError(error);
            }
            return new ErrorStateDrmSession(new DrmSessionException(error, PlaybackException.ERROR_CODE_DRM_CONTENT_ERROR));
        }
    }
    @Nullable DefaultDrmSession session;
    if (!multiSession) {
        session = noMultiSessionDrmSession;
    } else {
        // Only use an existing session if it has matching init data.
        session = null;
        for (DefaultDrmSession existingSession : sessions) {
            if (Util.areEqual(existingSession.schemeDatas, schemeDatas)) {
                session = existingSession;
                break;
            }
        }
    }
    if (session == null) {
        // Create a new session.
        session = createAndAcquireSessionWithRetry(schemeDatas, /* isPlaceholderSession= */
        false, eventDispatcher, shouldReleasePreacquiredSessionsBeforeRetrying);
        if (!multiSession) {
            noMultiSessionDrmSession = session;
        }
        sessions.add(session);
    } else {
        session.acquire(eventDispatcher);
    }
    return session;
}
Also used : DrmSessionException(com.google.android.exoplayer2.drm.DrmSession.DrmSessionException) SchemeData(com.google.android.exoplayer2.drm.DrmInitData.SchemeData) Nullable(androidx.annotation.Nullable) Nullable(androidx.annotation.Nullable)

Example 5 with DrmSessionException

use of com.google.android.exoplayer2.drm.DrmSession.DrmSessionException in project ExoPlayer by google.

the class OfflineLicenseHelper method getLicenseDurationRemainingSec.

/**
 * Returns the remaining license and playback durations in seconds, for an offline license.
 *
 * @param offlineLicenseKeySetId The key set id of the license.
 * @return The remaining license and playback durations, in seconds.
 * @throws DrmSessionException Thrown when a DRM session error occurs.
 */
public synchronized Pair<Long, Long> getLicenseDurationRemainingSec(byte[] offlineLicenseKeySetId) throws DrmSessionException {
    Assertions.checkNotNull(offlineLicenseKeySetId);
    drmSessionManager.setPlayer(handlerThread.getLooper(), PlayerId.UNSET);
    drmSessionManager.prepare();
    DrmSession drmSession = openBlockingKeyRequest(DefaultDrmSessionManager.MODE_QUERY, offlineLicenseKeySetId, FORMAT_WITH_EMPTY_DRM_INIT_DATA);
    DrmSessionException error = drmSession.getError();
    Pair<Long, Long> licenseDurationRemainingSec = WidevineUtil.getLicenseDurationRemainingSec(drmSession);
    drmSession.release(eventDispatcher);
    drmSessionManager.release();
    if (error != null) {
        if (error.getCause() instanceof KeysExpiredException) {
            return Pair.create(0L, 0L);
        }
        throw error;
    }
    return Assertions.checkNotNull(licenseDurationRemainingSec);
}
Also used : DrmSessionException(com.google.android.exoplayer2.drm.DrmSession.DrmSessionException)

Aggregations

DrmSessionException (com.google.android.exoplayer2.drm.DrmSession.DrmSessionException)7 Nullable (androidx.annotation.Nullable)3 CryptoConfig (com.google.android.exoplayer2.decoder.CryptoConfig)2 DecoderException (com.google.android.exoplayer2.decoder.DecoderException)2 SuppressLint (android.annotation.SuppressLint)1 DeniedByServerException (android.media.DeniedByServerException)1 MediaCodec (android.media.MediaCodec)1 MediaCrypto (android.media.MediaCrypto)1 MediaCryptoException (android.media.MediaCryptoException)1 MediaDrm (android.media.MediaDrm)1 MediaDrmResetException (android.media.MediaDrmResetException)1 NotProvisionedException (android.media.NotProvisionedException)1 ErrnoException (android.system.ErrnoException)1 ExoPlaybackException (com.google.android.exoplayer2.ExoPlaybackException)1 Format (com.google.android.exoplayer2.Format)1 ParserException (com.google.android.exoplayer2.ParserException)1 AudioSink (com.google.android.exoplayer2.audio.AudioSink)1 DefaultDrmSessionManager (com.google.android.exoplayer2.drm.DefaultDrmSessionManager)1 SchemeData (com.google.android.exoplayer2.drm.DrmInitData.SchemeData)1 FrameworkCryptoConfig (com.google.android.exoplayer2.drm.FrameworkCryptoConfig)1