use of android.media.MediaExtractor in project android_frameworks_base by ResurrectionRemix.
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();
}
use of android.media.MediaExtractor in project android_frameworks_base by DirtyUnicorns.
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();
}
use of android.media.MediaExtractor in project android_frameworks_base by DirtyUnicorns.
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();
}
}
}
use of android.media.MediaExtractor in project android_frameworks_base by AOSPA.
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();
}
}
}
use of android.media.MediaExtractor in project android_packages_apps_Gallery2 by LineageOS.
the class VideoUtils method genVideoUsingMuxer.
/**
* @param srcPath the path of source video file.
* @param dstPath the path of destination video file.
* @param startMs starting time in milliseconds for trimming. Set to
* negative if starting from beginning.
* @param endMs end time for trimming in milliseconds. Set to negative if
* no trimming at the end.
* @param useAudio true if keep the audio track from the source.
* @param useVideo true if keep the video track from the source.
* @throws IOException
*/
private static void genVideoUsingMuxer(String srcPath, String dstPath, int startMs, int endMs, boolean useAudio, boolean useVideo) throws IOException {
// Set up MediaExtractor to read from the source.
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(srcPath);
int trackCount = extractor.getTrackCount();
// Set up MediaMuxer for the destination.
MediaMuxer muxer;
muxer = new MediaMuxer(dstPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
// Set up the tracks and retrieve the max buffer size for selected
// tracks.
HashMap<Integer, Integer> indexMap = new HashMap<Integer, Integer>(trackCount);
int bufferSize = -1;
for (int i = 0; i < trackCount; i++) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
boolean selectCurrentTrack = false;
if (mime.startsWith("audio/") && useAudio) {
selectCurrentTrack = true;
} else if (mime.startsWith("video/") && useVideo) {
selectCurrentTrack = true;
}
if (selectCurrentTrack) {
extractor.selectTrack(i);
try {
int dstIndex = muxer.addTrack(format);
indexMap.put(i, dstIndex);
if (format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
int newSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
bufferSize = newSize > bufferSize ? newSize : bufferSize;
}
} catch (IllegalArgumentException e) {
Log.e(LOGTAG, "Unsupported format '" + mime + "'");
throw new IOException("Muxer does not support " + mime);
}
}
}
if (bufferSize < 0) {
bufferSize = DEFAULT_BUFFER_SIZE;
}
// Set up the orientation and starting time for extractor.
MediaMetadataRetriever retrieverSrc = new MediaMetadataRetriever();
retrieverSrc.setDataSource(srcPath);
String degreesString = retrieverSrc.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
if (degreesString != null) {
int degrees = Integer.parseInt(degreesString);
if (degrees >= 0) {
muxer.setOrientationHint(degrees);
}
}
if (startMs > 0) {
extractor.seekTo(startMs * 1000, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
}
// Copy the samples from MediaExtractor to MediaMuxer. We will loop
// for copying each sample and stop when we get to the end of the source
// file or exceed the end time of the trimming.
int offset = 0;
int trackIndex = -1;
ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
BufferInfo bufferInfo = new BufferInfo();
try {
muxer.start();
while (true) {
bufferInfo.offset = offset;
bufferInfo.size = extractor.readSampleData(dstBuf, offset);
if (bufferInfo.size < 0) {
Log.d(LOGTAG, "Saw input EOS.");
bufferInfo.size = 0;
break;
} else {
bufferInfo.presentationTimeUs = extractor.getSampleTime();
if (endMs > 0 && bufferInfo.presentationTimeUs > (endMs * 1000)) {
Log.d(LOGTAG, "The current sample is over the trim end time.");
break;
} else {
bufferInfo.flags = extractor.getSampleFlags();
trackIndex = extractor.getSampleTrackIndex();
muxer.writeSampleData(indexMap.get(trackIndex), dstBuf, bufferInfo);
extractor.advance();
}
}
}
muxer.stop();
} catch (IllegalStateException e) {
// Swallow the exception due to malformed source.
Log.w(LOGTAG, "The source video file is malformed");
File f = new File(dstPath);
if (f.exists()) {
f.delete();
}
throw e;
} finally {
muxer.release();
}
return;
}
Aggregations