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);
}
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();
}
}
}
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));
}
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);
}
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);
}
Aggregations