public static boolean hasCodecForTrack(MediaExtractor ex, int track) { int count = ex.getTrackCount(); if (track < 0 || track >= count) { throw new IndexOutOfBoundsException(track + " not in [0.." + (count - 1) + "]"); } return canDecode(ex.getTrackFormat(track)); }
/* */ public synchronized boolean a(String paramString) /* */ { /* 362 */ a.a("playWAV start :" + paramString); /* */ /* 364 */ if (!c(paramString)) { /* 365 */ a.b("file not exist"); /* 366 */ return false; /* */ } /* */ /* 369 */ String str1 = paramString.trim(); /* 370 */ String str2 = str1.substring(str1.lastIndexOf(".") + 1); /* 371 */ str2 = str2.toLowerCase(); /* 372 */ if (!str2.equalsIgnoreCase("wav")) { /* 373 */ a.b("file not wav file"); /* 374 */ g(); /* 375 */ return false; /* */ } /* */ /* 378 */ MediaExtractor localMediaExtractor = null; /* */ try { /* 380 */ localMediaExtractor = new MediaExtractor(); /* */ } catch (RuntimeException localRuntimeException1) { /* 382 */ localRuntimeException1.printStackTrace(); /* 383 */ a.b("MediaExtractor error"); /* */ } /* */ try /* */ { /* 387 */ localMediaExtractor.setDataSource(paramString); /* */ } catch (IOException localIOException) { /* 389 */ localIOException.printStackTrace(); /* 390 */ a.b("setDataSource error"); /* 391 */ return false; /* */ } /* */ /* 394 */ MediaFormat localMediaFormat = null; /* 395 */ int i1 = localMediaExtractor.getTrackCount(); /* 396 */ if (i1 > 0) { /* */ try { /* 398 */ localMediaFormat = localMediaExtractor.getTrackFormat(0); /* */ } catch (RuntimeException localRuntimeException2) { /* 400 */ localRuntimeException2.printStackTrace(); /* 401 */ a.b("getTrackFormat error"); /* 402 */ return false; /* */ } /* */ /* */ /* 406 */ int i2 = localMediaFormat.getInteger("sample-rate"); /* */ /* 408 */ this.j = localMediaFormat.getLong("durationUs"); /* */ /* 410 */ if (i2 < n) { /* 411 */ g(); /* 412 */ return false; /* */ } /* */ /* 415 */ return PlayWav(paramString); /* */ } /* 417 */ return false; /* */ }
/** * @param media_extractor * @param trackIndex * @return */ protected MediaCodec internal_start_video( final MediaExtractor media_extractor, final int trackIndex) { if (DEBUG) Log.v(TAG, "internal_start_video:"); MediaCodec codec = null; if (trackIndex >= 0) { final MediaFormat format = media_extractor.getTrackFormat(trackIndex); final String mime = format.getString(MediaFormat.KEY_MIME); codec = MediaCodec.createDecoderByType(mime); codec.configure(format, mOutputSurface, null, 0); codec.start(); if (DEBUG) Log.v(TAG, "internal_start_video:codec started"); } return codec; }
/** return true iff any track starting with mimePrefix is supported */ public static boolean hasCodecForMediaAndDomain(MediaExtractor ex, String mimePrefix) { mimePrefix = mimePrefix.toLowerCase(); for (int i = 0; i < ex.getTrackCount(); ++i) { MediaFormat format = ex.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); if (mime.toLowerCase().startsWith(mimePrefix)) { if (canDecode(format)) { return true; } Log.i(TAG, "no decoder for " + format); } } return false; }
/** return true iff all audio and video tracks are supported */ public static boolean hasCodecsForMedia(MediaExtractor ex) { for (int i = 0; i < ex.getTrackCount(); ++i) { MediaFormat format = ex.getTrackFormat(i); // only check for audio and video codecs String mime = format.getString(MediaFormat.KEY_MIME).toLowerCase(); if (!mime.startsWith("audio/") && !mime.startsWith("video/")) { continue; } if (!canDecode(format)) { return false; } } return true; }
public PassThroughTrackTranscoder( MediaExtractor extractor, int trackIndex, QueuedMuxer muxer, QueuedMuxer.SampleType sampleType) { mExtractor = extractor; mTrackIndex = trackIndex; mMuxer = muxer; mSampleType = sampleType; mActualOutputFormat = mExtractor.getTrackFormat(mTrackIndex); mMuxer.setOutputFormat(mSampleType, mActualOutputFormat); mBufferSize = mActualOutputFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE); mBuffer = ByteBuffer.allocateDirect(mBufferSize).order(ByteOrder.nativeOrder()); }
private void initStream() throws IOException, IllegalArgumentException { L.v(TAG, "initStream called in state=" + state); lock.lock(); try { extractor = new MediaExtractor(); if (path != null) { extractor.setDataSource(path); } else { error("initStream"); throw new IOException(); } int trackNum = 0; final MediaFormat oFormat = extractor.getTrackFormat(trackNum); if (!oFormat.containsKey(MediaFormat.KEY_SAMPLE_RATE)) { error("initStream"); throw new IOException("No KEY_SAMPLE_RATE"); } int sampleRate = oFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE); if (!oFormat.containsKey(MediaFormat.KEY_CHANNEL_COUNT)) { error("initStream"); throw new IOException("No KEY_CHANNEL_COUNT"); } int channelCount = oFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT); if (!oFormat.containsKey(MediaFormat.KEY_MIME)) { error("initStream"); throw new IOException("No KEY_MIME"); } final String mime = oFormat.getString(MediaFormat.KEY_MIME); if (!oFormat.containsKey(MediaFormat.KEY_DURATION)) { error("initStream"); throw new IOException("No KEY_DURATION"); } duration = oFormat.getLong(MediaFormat.KEY_DURATION); L.v(TAG, "Sample rate: " + sampleRate); L.v(TAG, "Mime type: " + mime); initDevice(sampleRate, channelCount); extractor.selectTrack(trackNum); codec = MediaCodec.createDecoderByType(mime); codec.configure(oFormat, null, null, 0); } finally { lock.unlock(); } }
/** * search first track index matched specific MIME * * @param extractor * @param mimeType "video/" or "audio/" * @return track index, -1 if not found */ protected static final int selectTrack(final MediaExtractor extractor, final String mimeType) { final int numTracks = extractor.getTrackCount(); MediaFormat format; String mime; for (int i = 0; i < numTracks; i++) { format = extractor.getTrackFormat(i); mime = format.getString(MediaFormat.KEY_MIME); if (mime.startsWith(mimeType)) { if (DEBUG) { Log.d(TAG_STATIC, "Extractor selected track " + i + " (" + mime + "): " + format); } return i; } } return -1; }
void setup() { int width = 0, height = 0; extractor = new MediaExtractor(); try { extractor.setDataSource(mVideoFile.getPath()); } catch (IOException e) { return; } for (int i = 0; i < extractor.getTrackCount(); i++) { MediaFormat format = extractor.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); width = format.getInteger(MediaFormat.KEY_WIDTH); height = format.getInteger(MediaFormat.KEY_HEIGHT); if (mime.startsWith("video/")) { extractor.selectTrack(i); try { decoder = MediaCodec.createDecoderByType(mime); } catch (IOException e) { continue; } // Decode to surface // decoder.configure(format, surface, null, 0); // Decode to offscreen surface surface = new CtsMediaOutputSurface(width, height); mMatBuffer = new MatBuffer(width, height); decoder.configure(format, surface.getSurface(), null, 0); break; } } if (decoder == null) { Log.e("VideoDecoderForOpenCV", "Can't find video info!"); return; } valid = true; }
/** * @param src_file * @return first video track index, -1 if not found */ protected int internal_prepare_video(final String source_file) { int trackindex = -1; mVideoMediaExtractor = new MediaExtractor(); try { mVideoMediaExtractor.setDataSource(source_file); trackindex = selectTrack(mVideoMediaExtractor, "video/"); if (trackindex >= 0) { mVideoMediaExtractor.selectTrack(trackindex); final MediaFormat format = mVideoMediaExtractor.getTrackFormat(trackindex); mVideoWidth = format.getInteger(MediaFormat.KEY_WIDTH); mVideoHeight = format.getInteger(MediaFormat.KEY_HEIGHT); mDuration = format.getLong(MediaFormat.KEY_DURATION); if (DEBUG) Log.v( TAG, String.format( "format:size(%d,%d),duration=%d,bps=%d,framerate=%f,rotation=%d", mVideoWidth, mVideoHeight, mDuration, mBitrate, mFrameRate, mRotation)); } } catch (final IOException e) { } return trackindex; }
@CalledByNative private static boolean decodeAudioFile( Context ctx, int nativeMediaCodecBridge, int inputFD, long dataSize) { if (dataSize < 0 || dataSize > 0x7fffffff) return false; MediaExtractor extractor = new MediaExtractor(); ParcelFileDescriptor encodedFD; encodedFD = ParcelFileDescriptor.adoptFd(inputFD); try { extractor.setDataSource(encodedFD.getFileDescriptor(), 0, dataSize); } catch (Exception e) { e.printStackTrace(); encodedFD.detachFd(); return false; } if (extractor.getTrackCount() <= 0) { encodedFD.detachFd(); return false; } MediaFormat format = extractor.getTrackFormat(0); // Number of channels specified in the file int inputChannelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT); // Number of channels the decoder will provide. (Not // necessarily the same as inputChannelCount. See // crbug.com/266006.) int outputChannelCount = inputChannelCount; int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE); String mime = format.getString(MediaFormat.KEY_MIME); long durationMicroseconds = 0; if (format.containsKey(MediaFormat.KEY_DURATION)) { try { durationMicroseconds = format.getLong(MediaFormat.KEY_DURATION); } catch (Exception e) { Log.d(LOG_TAG, "Cannot get duration"); } } if (DEBUG) { Log.d( LOG_TAG, "Tracks: " + extractor.getTrackCount() + " Rate: " + sampleRate + " Channels: " + inputChannelCount + " Mime: " + mime + " Duration: " + durationMicroseconds + " microsec"); } nativeInitializeDestination( nativeMediaCodecBridge, inputChannelCount, sampleRate, durationMicroseconds); // Create decoder MediaCodec codec = MediaCodec.createDecoderByType(mime); codec.configure(format, null /* surface */, null /* crypto */, 0 /* flags */); codec.start(); ByteBuffer[] codecInputBuffers = codec.getInputBuffers(); ByteBuffer[] codecOutputBuffers = codec.getOutputBuffers(); // A track must be selected and will be used to read samples. extractor.selectTrack(0); boolean sawInputEOS = false; boolean sawOutputEOS = false; // Keep processing until the output is done. while (!sawOutputEOS) { if (!sawInputEOS) { // Input side int inputBufIndex = codec.dequeueInputBuffer(TIMEOUT_MICROSECONDS); if (inputBufIndex >= 0) { ByteBuffer dstBuf = codecInputBuffers[inputBufIndex]; int sampleSize = extractor.readSampleData(dstBuf, 0); long presentationTimeMicroSec = 0; if (sampleSize < 0) { sawInputEOS = true; sampleSize = 0; } else { presentationTimeMicroSec = extractor.getSampleTime(); } codec.queueInputBuffer( inputBufIndex, 0, /* offset */ sampleSize, presentationTimeMicroSec, sawInputEOS ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0); if (!sawInputEOS) { extractor.advance(); } } } // Output side MediaCodec.BufferInfo info = new BufferInfo(); final int outputBufIndex = codec.dequeueOutputBuffer(info, TIMEOUT_MICROSECONDS); if (outputBufIndex >= 0) { ByteBuffer buf = codecOutputBuffers[outputBufIndex]; if (info.size > 0) { nativeOnChunkDecoded( nativeMediaCodecBridge, buf, info.size, inputChannelCount, outputChannelCount); } buf.clear(); codec.releaseOutputBuffer(outputBufIndex, false /* render */); if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { sawOutputEOS = true; } } else if (outputBufIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) { codecOutputBuffers = codec.getOutputBuffers(); } else if (outputBufIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { MediaFormat newFormat = codec.getOutputFormat(); outputChannelCount = newFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT); Log.d(LOG_TAG, "output format changed to " + newFormat); } } encodedFD.detachFd(); codec.stop(); codec.release(); codec = null; return true; }