Example usage for android.media MediaCrypto MediaCrypto

List of usage examples for android.media MediaCrypto MediaCrypto

Introduction

In this page you can find the example usage for android.media MediaCrypto MediaCrypto.

Prototype

public MediaCrypto(@NonNull UUID uuid, @NonNull byte[] initData) throws MediaCryptoException 

Source Link

Document

Instantiate a MediaCrypto object using opaque, crypto scheme specific data.

Usage

From source file:org.chromium.media.MediaDrmBridge.java

/**
 * Create a MediaCrypto object.//  w w  w.ja  va 2 s .c  om
 *
 * @return if a MediaCrypto object is successfully created.
 */
private boolean createMediaCrypto() {
    assert (mSessionId != null);
    assert (mMediaCrypto == null);
    try {
        final byte[] session = mSessionId.getBytes("UTF-8");
        if (MediaCrypto.isCryptoSchemeSupported(mSchemeUUID)) {
            mMediaCrypto = new MediaCrypto(mSchemeUUID, session);
        }
    } catch (android.media.MediaCryptoException e) {
        Log.e(TAG, "Cannot create MediaCrypto " + e.toString());
        return false;
    } catch (java.io.UnsupportedEncodingException e) {
        Log.e(TAG, "Cannot create MediaCrypto " + e.toString());
        return false;
    }

    assert (mMediaCrypto != null);
    nativeOnMediaCryptoReady(mNativeMediaDrmBridge);
    return true;
}

From source file:com.sonymobile.android.media.internal.Util.java

public static MediaCrypto createMediaCrypto(MediaFormat format) throws MediaCryptoException {
    if (format.containsKey(MetaData.KEY_PLAYREADY_SESSIONID)) {
        ByteBuffer buffer = format.getByteBuffer(MetaData.KEY_PLAYREADY_SESSIONID);
        if (buffer != null) {
            return new MediaCrypto(DrmUUID.PLAY_READY, buffer.array());
        }/*from   w  w  w  .  ja  va 2s  . c  o m*/
    } else if (format.containsKey(MetaData.KEY_MARLIN_JSON)) {
        byte[] marlinJson;
        try {
            marlinJson = format.getString(MetaData.KEY_MARLIN_JSON).getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            if (LOGS_ENABLED)
                Log.e(TAG, "Unsupported encoding", e);
            return null;
        }

        return new MediaCrypto(DrmUUID.MARLIN, marlinJson);
    }

    return null;
}

From source file:com.nagravision.mediaplayer.FullscreenActivity.java

/**
 *
 *//*from  w  w w. j ava 2  s. c  o  m*/
@SuppressWarnings("unused")
private void ClickItem(int position, int msec) {
    Log.v(LOG_TAG, "FullscreenActivity::ClickItem - Enter\n");

    /* Close the drawer */
    mDrawer.closeDrawers();

    /* Build the notification we are going to display */
    mNotifBuilder.setContentText(getResources().getString(R.string.playing_notification));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    (new PrintWriter(baos)).format(getResources().getString(R.string.long_playing_notification),
            MOVIES_ARR[position]);
    mNotifBuilder.setContentInfo(baos.toString());
    mNotifBuilder.setContentTitle(getResources().getString(R.string.app_name));
    mNotifBuilder.setSubText(getResources().getString(R.string.app_copyright));
    mNotifBuilder.setSmallIcon(R.drawable.ic_action_play);
    mExplicitIntent.setData(Uri.parse(MOVIES_URLS[position]));
    mNotifBuilder.setContentIntent(mDefaultIntent);
    mNotifBuilder.addAction(R.drawable.ic_action_about,
            getResources().getString(R.string.infos_action_description), mInfosIntent);

    /* If a notif was already sent, cancel it */
    if (mLastNotif != null) {
        if (mLastPosition != -1) {
            mNotifMgr.cancel(getResources().getString(R.string.playing_notification), mLastPosition);
            mLastPosition = -1;
        } else
            mNotifMgr.cancelAll();
        mLastNotif = null;
    }
    mLastNotif = mNotifBuilder.build();
    mLastPosition = position;

    if (false && MOVIES_MDTA[position].contains("drm")) {
        Log.d(LOG_TAG, "Starting extraction of Media Metadata\n");
        MediaExtractor extractor = new MediaExtractor();
        try {
            Log.d(LOG_TAG, "URL: " + MOVIES_URLS[position]);
            extractor.setDataSource(MOVIES_URLS[position]);
            int numTracks = extractor.getTrackCount();
            Log.v(LOG_TAG, "Number of tracks: " + numTracks);
            for (int i = 0; i < numTracks; ++i) {
                MediaFormat format = extractor.getTrackFormat(i);
                String mime = format.getString(MediaFormat.KEY_MIME);
                Log.v(LOG_TAG, "Track[" + i + "].mime = " + mime);
                if (mime.equals("video/avc")) {
                    extractor.selectTrack(i);
                    //                        byte[] key = new byte[16];
                    //                        key[0] = (byte) 0x7f; key[1] = (byte) 0x99; key[2] = (byte) 0x06; key[3] = (byte) 0x95;
                    //                        key[4] = (byte) 0x78; key[5] = (byte) 0xab; key[6] = (byte) 0x4d; key[7] = (byte) 0xae;
                    //                        key[8] = (byte) 0x8d; key[9] = (byte) 0x6c; key[10] = (byte) 0xe2; key[11] = (byte) 0x4c;
                    //                        key[12] = (byte) 0xc3; key[13] = (byte) 0x21; key[14] = (byte) 0x02; key[15] = (byte) 0x32;
                    MediaCrypto mediaCrypto;
                    MediaCodec codec = MediaCodec.createDecoderByType("video/avc");
                    Map<UUID, byte[]> psshInfo = extractor.getPsshInfo();
                    for (Iterator<UUID> it = psshInfo.keySet().iterator(); it.hasNext();) {
                        UUID uuid = it.next();
                        String psshData = new String(psshInfo.get(uuid));
                        Log.v(LOG_TAG, "PSSH for UUID: " + uuid.toString() + " has data :" + psshData);
                        try {
                            mediaCrypto = new MediaCrypto(uuid, psshInfo.get(uuid));
                            codec.configure(format, mVideoHolder.getHolder().getSurface(), mediaCrypto, 0);
                            codec.start();
                            @SuppressWarnings("unused")
                            ByteBuffer[] inputBuffers = codec.getInputBuffers();
                            @SuppressWarnings("unused")
                            ByteBuffer[] outputBuffers = codec.getOutputBuffers();
                            for (;;) {
                                int inputBufferIndex = codec.dequeueInputBuffer(200000);
                                if (inputBufferIndex >= 0) {
                                    //extractor.readSampleData();
                                    Log.v(LOG_TAG,
                                            "Read data from extractor (and crypto) and provide to decoder\n");
                                }
                            }
                        } catch (MediaCryptoException mce) {
                            Log.e(LOG_TAG, "Could not instanciate MediaCrypto ! " + mce);
                        }
                    }
                }
            }
        } catch (IOException ioe) {
            Log.e(LOG_TAG, "Cannot access URL: " + ioe);
        }
    }

    /* Setup media player for playing the movie */
    mVideoHolder.setVideoURI(Uri.parse(MOVIES_URLS[position]));
    mVideoHolder.requestFocus();
    mVideoHolder.start();

    if (msec > 0) {
        if (mVideoHolder.canPause())
            mVideoHolder.pause();
        mVideoHolder.seekTo(msec);
        if (mVideoHolder.canPause())
            mVideoHolder.resume();
    }

    /* Player has started ... send notification */
    mNotifMgr.notify(getResources().getString(R.string.playing_notification), mLastPosition, mLastNotif);
}