Search in sources :

Example 11 with MediaExtractor

use of android.media.MediaExtractor in project android_frameworks_base by ResurrectionRemix.

the class Camera2RecordingTest method validateRecording.

private void validateRecording(Size sz, int expectedDurationMs) throws Exception {
    File outFile = new File(mOutMediaFileName);
    assertTrue("No video is recorded", outFile.exists());
    MediaExtractor extractor = new MediaExtractor();
    try {
        extractor.setDataSource(mOutMediaFileName);
        long durationUs = 0;
        int width = -1, height = -1;
        int numTracks = extractor.getTrackCount();
        final String VIDEO_MIME_TYPE = "video";
        for (int i = 0; i < numTracks; i++) {
            MediaFormat format = extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.contains(VIDEO_MIME_TYPE)) {
                Log.i(TAG, "video format is: " + format.toString());
                durationUs = format.getLong(MediaFormat.KEY_DURATION);
                width = format.getInteger(MediaFormat.KEY_WIDTH);
                height = format.getInteger(MediaFormat.KEY_HEIGHT);
                break;
            }
        }
        Size videoSz = new Size(width, height);
        assertTrue("Video size doesn't match, expected " + sz.toString() + " got " + videoSz.toString(), videoSz.equals(sz));
        int duration = (int) (durationUs / 1000);
        if (VERBOSE) {
            Log.v(TAG, String.format("Video duration: recorded %dms, expected %dms", duration, expectedDurationMs));
        }
        // TODO: Don't skip this for video snapshot
        if (!mStaticInfo.isHardwareLevelLegacy()) {
            assertTrue(String.format("Camera %s: Video duration doesn't match: recorded %dms, expected %dms.", mCamera.getId(), duration, expectedDurationMs), Math.abs(duration - expectedDurationMs) < DURATION_MARGIN * expectedDurationMs);
        }
    } finally {
        extractor.release();
        if (!DEBUG_DUMP) {
            outFile.delete();
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) Size(android.util.Size) MediaExtractor(android.media.MediaExtractor) File(java.io.File)

Example 12 with MediaExtractor

use of android.media.MediaExtractor in project android_frameworks_base by crdroidandroid.

the class Camera2RecordingTest method validateRecording.

private void validateRecording(Size sz, int expectedDurationMs) throws Exception {
    File outFile = new File(mOutMediaFileName);
    assertTrue("No video is recorded", outFile.exists());
    MediaExtractor extractor = new MediaExtractor();
    try {
        extractor.setDataSource(mOutMediaFileName);
        long durationUs = 0;
        int width = -1, height = -1;
        int numTracks = extractor.getTrackCount();
        final String VIDEO_MIME_TYPE = "video";
        for (int i = 0; i < numTracks; i++) {
            MediaFormat format = extractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.contains(VIDEO_MIME_TYPE)) {
                Log.i(TAG, "video format is: " + format.toString());
                durationUs = format.getLong(MediaFormat.KEY_DURATION);
                width = format.getInteger(MediaFormat.KEY_WIDTH);
                height = format.getInteger(MediaFormat.KEY_HEIGHT);
                break;
            }
        }
        Size videoSz = new Size(width, height);
        assertTrue("Video size doesn't match, expected " + sz.toString() + " got " + videoSz.toString(), videoSz.equals(sz));
        int duration = (int) (durationUs / 1000);
        if (VERBOSE) {
            Log.v(TAG, String.format("Video duration: recorded %dms, expected %dms", duration, expectedDurationMs));
        }
        // TODO: Don't skip this for video snapshot
        if (!mStaticInfo.isHardwareLevelLegacy()) {
            assertTrue(String.format("Camera %s: Video duration doesn't match: recorded %dms, expected %dms.", mCamera.getId(), duration, expectedDurationMs), Math.abs(duration - expectedDurationMs) < DURATION_MARGIN * expectedDurationMs);
        }
    } finally {
        extractor.release();
        if (!DEBUG_DUMP) {
            outFile.delete();
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) Size(android.util.Size) MediaExtractor(android.media.MediaExtractor) File(java.io.File)

Example 13 with MediaExtractor

use of android.media.MediaExtractor in project android_frameworks_base by crdroidandroid.

the class MediaDecoder method onStart.

private void onStart() throws Exception {
    if (mOpenGLEnabled) {
        getRenderTarget().focus();
    }
    mMediaExtractor = new MediaExtractor();
    mMediaExtractor.setDataSource(mContext, mUri, null);
    mVideoTrackIndex = -1;
    mAudioTrackIndex = -1;
    for (int i = 0; i < mMediaExtractor.getTrackCount(); i++) {
        MediaFormat format = mMediaExtractor.getTrackFormat(i);
        if (DEBUG) {
            Log.i(LOG_TAG, "Uri " + mUri + ", track " + i + ": " + format);
        }
        if (DecoderUtil.isVideoFormat(format) && mVideoTrackIndex == -1) {
            mVideoTrackIndex = i;
        } else if (DecoderUtil.isAudioFormat(format) && mAudioTrackIndex == -1) {
            mAudioTrackIndex = i;
        }
    }
    if (mVideoTrackIndex == -1 && mAudioTrackIndex == -1) {
        throw new IllegalArgumentException("Couldn't find a video or audio track in the provided file");
    }
    if (mVideoTrackIndex != -1) {
        MediaFormat videoFormat = mMediaExtractor.getTrackFormat(mVideoTrackIndex);
        mVideoTrackDecoder = mOpenGLEnabled ? new GpuVideoTrackDecoder(mVideoTrackIndex, videoFormat, this) : new CpuVideoTrackDecoder(mVideoTrackIndex, videoFormat, this);
        mVideoTrackDecoder.init();
        mMediaExtractor.selectTrack(mVideoTrackIndex);
        if (Build.VERSION.SDK_INT >= 17) {
            retrieveDefaultRotation();
        }
    }
    if (mAudioTrackIndex != -1) {
        MediaFormat audioFormat = mMediaExtractor.getTrackFormat(mAudioTrackIndex);
        mAudioTrackDecoder = new AudioTrackDecoder(mAudioTrackIndex, audioFormat, this);
        mAudioTrackDecoder.init();
        mMediaExtractor.selectTrack(mAudioTrackIndex);
    }
    if (mStartMicros > 0) {
        mMediaExtractor.seekTo(mStartMicros, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
    }
    mStarted = true;
    mListener.onDecodingStarted();
}
Also used : MediaFormat(android.media.MediaFormat) MediaExtractor(android.media.MediaExtractor)

Aggregations

MediaExtractor (android.media.MediaExtractor)13 MediaFormat (android.media.MediaFormat)13 Size (android.util.Size)5 File (java.io.File)5 MediaCodec (android.media.MediaCodec)2 IOException (java.io.IOException)2 BufferInfo (android.media.MediaCodec.BufferInfo)1 MediaPlayer (android.media.MediaPlayer)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 FileNotFoundException (java.io.FileNotFoundException)1 ByteBuffer (java.nio.ByteBuffer)1 CalledByNative (org.chromium.base.CalledByNative)1