Search in sources :

Example 26 with MediaExtractor

use of android.media.MediaExtractor in project Signal-Android by WhisperSystems.

the class AudioTrackConverter method create.

@Nullable
static AudioTrackConverter create(@NonNull final MediaInput input, final long timeFrom, final long timeTo, final int audioBitrate) throws IOException {
    final MediaExtractor audioExtractor = input.createExtractor();
    final int audioInputTrack = getAndSelectAudioTrackIndex(audioExtractor);
    if (audioInputTrack == -1) {
        audioExtractor.release();
        return null;
    }
    return new AudioTrackConverter(audioExtractor, audioInputTrack, timeFrom, timeTo, audioBitrate);
}
Also used : MediaExtractor(android.media.MediaExtractor) Nullable(androidx.annotation.Nullable)

Example 27 with MediaExtractor

use of android.media.MediaExtractor in project Signal-Android by WhisperSystems.

the class VideoThumbnailsExtractor method extractThumbnails.

static void extractThumbnails(@NonNull final MediaInput input, final int thumbnailCount, final int thumbnailResolution, @NonNull final Callback callback) {
    MediaExtractor extractor = null;
    MediaCodec decoder = null;
    OutputSurface outputSurface = null;
    try {
        extractor = input.createExtractor();
        MediaFormat mediaFormat = null;
        for (int index = 0; index < extractor.getTrackCount(); ++index) {
            if (extractor.getTrackFormat(index).getString(MediaFormat.KEY_MIME).startsWith("video/")) {
                extractor.selectTrack(index);
                mediaFormat = extractor.getTrackFormat(index);
                break;
            }
        }
        if (mediaFormat != null) {
            final String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
            final int rotation = mediaFormat.containsKey(MediaFormat.KEY_ROTATION) ? mediaFormat.getInteger(MediaFormat.KEY_ROTATION) : 0;
            final int width = mediaFormat.getInteger(MediaFormat.KEY_WIDTH);
            final int height = mediaFormat.getInteger(MediaFormat.KEY_HEIGHT);
            final int outputWidth;
            final int outputHeight;
            if (width < height) {
                outputWidth = thumbnailResolution;
                outputHeight = height * outputWidth / width;
            } else {
                outputHeight = thumbnailResolution;
                outputWidth = width * outputHeight / height;
            }
            final int outputWidthRotated;
            final int outputHeightRotated;
            if ((rotation % 180 == 90)) {
                // noinspection SuspiciousNameCombination
                outputWidthRotated = outputHeight;
                // noinspection SuspiciousNameCombination
                outputHeightRotated = outputWidth;
            } else {
                outputWidthRotated = outputWidth;
                outputHeightRotated = outputHeight;
            }
            Log.i(TAG, "video: " + width + "x" + height + " " + rotation);
            Log.i(TAG, "output: " + outputWidthRotated + "x" + outputHeightRotated);
            outputSurface = new OutputSurface(outputWidthRotated, outputHeightRotated, true);
            decoder = MediaCodec.createDecoderByType(mime);
            decoder.configure(mediaFormat, outputSurface.getSurface(), null, 0);
            decoder.start();
            long duration = 0;
            if (mediaFormat.containsKey(MediaFormat.KEY_DURATION)) {
                duration = mediaFormat.getLong(MediaFormat.KEY_DURATION);
            } else {
                Log.w(TAG, "Video is missing duration!");
            }
            callback.durationKnown(duration);
            doExtract(extractor, decoder, outputSurface, outputWidthRotated, outputHeightRotated, duration, thumbnailCount, callback);
        }
    } catch (IOException | TranscodingException | MediaCodec.CodecException e) {
        Log.w(TAG, e);
        callback.failed();
    } finally {
        if (outputSurface != null) {
            outputSurface.release();
        }
        if (decoder != null) {
            try {
                decoder.stop();
            } catch (MediaCodec.CodecException codecException) {
                Log.w(TAG, "Decoder stop failed: " + codecException.getDiagnosticInfo(), codecException);
            } catch (IllegalStateException ise) {
                Log.w(TAG, "Decoder stop failed", ise);
            }
            decoder.release();
        }
        if (extractor != null) {
            extractor.release();
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) IOException(java.io.IOException) MediaExtractor(android.media.MediaExtractor) MediaCodec(android.media.MediaCodec)

Example 28 with MediaExtractor

use of android.media.MediaExtractor in project robolectric by robolectric.

the class ShadowMediaExtractorTest method selectTrack_onlyOneAtATime.

@Test
public void selectTrack_onlyOneAtATime() throws IOException {
    ShadowMediaExtractor.addTrack(dataSource, audioMediaFormat, new byte[0]);
    ShadowMediaExtractor.addTrack(dataSource, videoMediaFormat, new byte[0]);
    MediaExtractor mediaExtractor = new MediaExtractor();
    mediaExtractor.setDataSource(path);
    mediaExtractor.selectTrack(0);
    assertThrows(IllegalStateException.class, () -> mediaExtractor.selectTrack(1));
}
Also used : MediaExtractor(android.media.MediaExtractor) Test(org.junit.Test)

Example 29 with MediaExtractor

use of android.media.MediaExtractor in project robolectric by robolectric.

the class ShadowMediaExtractorTest method readSampleData_returnsSampleData.

@Test
public void readSampleData_returnsSampleData() throws IOException {
    byte[] sampleData = generateSampleData(4096);
    ShadowMediaExtractor.addTrack(dataSource, audioMediaFormat, sampleData);
    int byteBufferSize = 1024;
    ByteBuffer byteBuffer = ByteBuffer.allocate(byteBufferSize);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    MediaExtractor mediaExtractor = new MediaExtractor();
    mediaExtractor.setDataSource(path);
    mediaExtractor.selectTrack(0);
    while (true) {
        int length = mediaExtractor.readSampleData(byteBuffer, 0);
        if (length == -1) {
            break;
        }
        outputStream.write(byteBuffer.array());
        byteBuffer.rewind();
        mediaExtractor.advance();
        assertThat(length).isEqualTo(byteBufferSize);
    }
    // Check that the read data matches the injected data.
    assertThat(outputStream.toByteArray()).isEqualTo(sampleData);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) MediaExtractor(android.media.MediaExtractor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 30 with MediaExtractor

use of android.media.MediaExtractor in project robolectric by robolectric.

the class ShadowMediaExtractorTest method getSampleTrackIndex_returnsSelectedTrack.

@Test
public void getSampleTrackIndex_returnsSelectedTrack() throws IOException {
    ShadowMediaExtractor.addTrack(dataSource, audioMediaFormat, new byte[0]);
    ShadowMediaExtractor.addTrack(dataSource, videoMediaFormat, new byte[0]);
    MediaExtractor mediaExtractor = new MediaExtractor();
    mediaExtractor.setDataSource(path);
    mediaExtractor.selectTrack(0);
    assertThat(mediaExtractor.getSampleTrackIndex()).isEqualTo(0);
}
Also used : MediaExtractor(android.media.MediaExtractor) Test(org.junit.Test)

Aggregations

MediaExtractor (android.media.MediaExtractor)35 MediaFormat (android.media.MediaFormat)20 IOException (java.io.IOException)9 MediaCodec (android.media.MediaCodec)8 ByteBuffer (java.nio.ByteBuffer)8 Test (org.junit.Test)8 File (java.io.File)7 Size (android.util.Size)5 Nullable (androidx.annotation.Nullable)5 RequiresApi (androidx.annotation.RequiresApi)4 BufferInfo (android.media.MediaCodec.BufferInfo)2 MediaMuxer (android.media.MediaMuxer)2 NonNull (androidx.annotation.NonNull)2 WorkerThread (androidx.annotation.WorkerThread)2 ByteString (com.google.protobuf.ByteString)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DecryptableUriMediaInput (org.thoughtcrime.securesms.media.DecryptableUriMediaInput)2 MediaInput (org.thoughtcrime.securesms.media.MediaInput)2 AssetFileDescriptor (android.content.res.AssetFileDescriptor)1 MediaMetadataRetriever (android.media.MediaMetadataRetriever)1