FileDocCategorySizeDatePackage
MediaExtractor.javaAPI DocAndroid 5.1 API13299Thu Mar 12 22:22:30 GMT 2015android.media

MediaExtractor

public final class MediaExtractor extends Object
MediaExtractor facilitates extraction of demuxed, typically encoded, media data from a data source.

It is generally used like this:

MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(...);
int numTracks = extractor.getTrackCount();
for (int i = 0; i < numTracks; ++i) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (weAreInterestedInThisTrack) {
extractor.selectTrack(i);
}
}
ByteBuffer inputBuffer = ByteBuffer.allocate(...)
while (extractor.readSampleData(inputBuffer, ...) >= 0) {
int trackIndex = extractor.getSampleTrackIndex();
long presentationTimeUs = extractor.getSampleTime();
...
extractor.advance();
}

extractor.release();
extractor = null;

Fields Summary
public static final int
SEEK_TO_PREVIOUS_SYNC
If possible, seek to a sync sample at or before the specified time
public static final int
SEEK_TO_NEXT_SYNC
If possible, seek to a sync sample at or after the specified time
public static final int
SEEK_TO_CLOSEST_SYNC
If possible, seek to the sync sample closest to the specified time
public static final int
SAMPLE_FLAG_SYNC
The sample is a sync sample (or in {@link MediaCodec}'s terminology it is a key frame.)
public static final int
SAMPLE_FLAG_ENCRYPTED
The sample is (at least partially) encrypted, see also the documentation for {@link android.media.MediaCodec#queueSecureInputBuffer}
private long
mNativeContext
Constructors Summary
public MediaExtractor()

        native_setup();
    
Methods Summary
public native booleanadvance()
Advance to the next sample. Returns false if no more sample data is available (end of stream).

protected voidfinalize()

        native_finalize();
    
public native longgetCachedDuration()
Returns an estimate of how much data is presently cached in memory expressed in microseconds. Returns -1 if that information is unavailable or not applicable (no cache).

private native java.util.MapgetFileFormatNative()

public java.util.MapgetPsshInfo()
Get the PSSH info if present.

return
a map of uuid-to-bytes, with the uuid specifying the crypto scheme, and the bytes being the data specific to that scheme.

        Map<UUID, byte[]> psshMap = null;
        Map<String, Object> formatMap = getFileFormatNative();
        if (formatMap != null && formatMap.containsKey("pssh")) {
            ByteBuffer rawpssh = (ByteBuffer) formatMap.get("pssh");
            rawpssh.order(ByteOrder.nativeOrder());
            rawpssh.rewind();
            formatMap.remove("pssh");
            // parse the flat pssh bytebuffer into something more manageable
            psshMap = new HashMap<UUID, byte[]>();
            while (rawpssh.remaining() > 0) {
                rawpssh.order(ByteOrder.BIG_ENDIAN);
                long msb = rawpssh.getLong();
                long lsb = rawpssh.getLong();
                UUID uuid = new UUID(msb, lsb);
                rawpssh.order(ByteOrder.nativeOrder());
                int datalen = rawpssh.getInt();
                byte [] psshdata = new byte[datalen];
                rawpssh.get(psshdata);
                psshMap.put(uuid, psshdata);
            }
        }
        return psshMap;
    
public native booleangetSampleCryptoInfo(MediaCodec.CryptoInfo info)
If the sample flags indicate that the current sample is at least partially encrypted, this call returns relevant information about the structure of the sample data required for decryption.

param
info The android.media.MediaCodec.CryptoInfo structure to be filled in.
return
true iff the sample flags contain {@link #SAMPLE_FLAG_ENCRYPTED}

public native intgetSampleFlags()
Returns the current sample's flags.

public native longgetSampleTime()
Returns the current sample's presentation time in microseconds. or -1 if no more samples are available.

public native intgetSampleTrackIndex()
Returns the track index the current sample originates from (or -1 if no more samples are available)

public final native intgetTrackCount()
Count the number of tracks found in the data source.

public android.media.MediaFormatgetTrackFormat(int index)
Get the track format at the specified index. More detail on the representation can be found at {@link android.media.MediaCodec}

        return new MediaFormat(getTrackFormatNative(index));
    
private native java.util.MapgetTrackFormatNative(int index)

public native booleanhasCacheReachedEndOfStream()
Returns true iff we are caching data and the cache has reached the end of the data stream (for now, a future seek may of course restart the fetching of data). This API only returns a meaningful result if {@link #getCachedDuration} indicates the presence of a cache, i.e. does NOT return -1.

private final native voidnativeSetDataSource(android.os.IBinder httpServiceBinder, java.lang.String path, java.lang.String[] keys, java.lang.String[] values)

private final native voidnative_finalize()

private static final native voidnative_init()

private final native voidnative_setup()

public native intreadSampleData(java.nio.ByteBuffer byteBuf, int offset)
Retrieve the current encoded sample and store it in the byte buffer starting at the given offset.

Note:As of API 21, on success the position and limit of {@code byteBuf} is updated to point to the data just read.

param
byteBuf the destination byte buffer
return
the sample size (or -1 if no more samples are available).

public final native voidrelease()
Make sure you call this when you're done to free up any resources instead of relying on the garbage collector to do this for you at some point in the future.

public native voidseekTo(long timeUs, int mode)
All selected tracks seek near the requested time according to the specified mode.

public native voidselectTrack(int index)
Subsequent calls to {@link #readSampleData}, {@link #getSampleTrackIndex} and {@link #getSampleTime} only retrieve information for the subset of tracks selected. Selecting the same track multiple times has no effect, the track is only selected once.

public final native voidsetDataSource(DataSource source)
Sets the DataSource object to be used as the data source for this extractor {@hide}

public final voidsetDataSource(android.content.Context context, android.net.Uri uri, java.util.Map headers)
Sets the data source as a content Uri.

param
context the Context to use when resolving the Uri
param
uri the Content URI of the data you want to extract from.
param
headers the headers to be sent together with the request for the data

        String scheme = uri.getScheme();
        if(scheme == null || scheme.equals("file")) {
            setDataSource(uri.getPath());
            return;
        }

        AssetFileDescriptor fd = null;
        try {
            ContentResolver resolver = context.getContentResolver();
            fd = resolver.openAssetFileDescriptor(uri, "r");
            if (fd == null) {
                return;
            }
            // Note: using getDeclaredLength so that our behavior is the same
            // as previous versions when the content provider is returning
            // a full file.
            if (fd.getDeclaredLength() < 0) {
                setDataSource(fd.getFileDescriptor());
            } else {
                setDataSource(
                        fd.getFileDescriptor(),
                        fd.getStartOffset(),
                        fd.getDeclaredLength());
            }
            return;
        } catch (SecurityException ex) {
        } catch (IOException ex) {
        } finally {
            if (fd != null) {
                fd.close();
            }
        }

        setDataSource(uri.toString(), headers);
    
public final voidsetDataSource(java.lang.String path, java.util.Map headers)
Sets the data source (file-path or http URL) to use.

param
path the path of the file, or the http URL
param
headers the headers associated with the http request for the stream you want to play

        String[] keys = null;
        String[] values = null;

        if (headers != null) {
            keys = new String[headers.size()];
            values = new String[headers.size()];

            int i = 0;
            for (Map.Entry<String, String> entry: headers.entrySet()) {
                keys[i] = entry.getKey();
                values[i] = entry.getValue();
                ++i;
            }
        }

        nativeSetDataSource(
                MediaHTTPService.createHttpServiceBinderIfNecessary(path),
                path,
                keys,
                values);
    
public final voidsetDataSource(java.lang.String path)
Sets the data source (file-path or http URL) to use.

param
path the path of the file, or the http URL of the stream

When path refers to a local file, the file may actually be opened by a process other than the calling application. This implies that the pathname should be an absolute path (as any other process runs with unspecified current working directory), and that the pathname should reference a world-readable file. As an alternative, the application could first open the file for reading, and then use the file descriptor form {@link #setDataSource(FileDescriptor)}.

        nativeSetDataSource(
                MediaHTTPService.createHttpServiceBinderIfNecessary(path),
                path,
                null,
                null);
    
public final voidsetDataSource(java.io.FileDescriptor fd)
Sets the data source (FileDescriptor) to use. It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.

param
fd the FileDescriptor for the file you want to extract from.

        setDataSource(fd, 0, 0x7ffffffffffffffL);
    
public final native voidsetDataSource(java.io.FileDescriptor fd, long offset, long length)
Sets the data source (FileDescriptor) to use. The FileDescriptor must be seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.

param
fd the FileDescriptor for the file you want to extract from.
param
offset the offset into the file where the data to be extracted starts, in bytes
param
length the length in bytes of the data to be extracted

public native voidunselectTrack(int index)
Subsequent calls to {@link #readSampleData}, {@link #getSampleTrackIndex} and {@link #getSampleTime} only retrieve information for the subset of tracks selected.