Example usage for android.media MediaFormat createSubtitleFormat

List of usage examples for android.media MediaFormat createSubtitleFormat

Introduction

In this page you can find the example usage for android.media MediaFormat createSubtitleFormat.

Prototype

public static final MediaFormat createSubtitleFormat(String mime, String language) 

Source Link

Document

Creates a minimal subtitle format.

Usage

From source file:com.sonymobile.android.media.internal.VUParser.java

@Override
protected boolean parseBox(BoxHeader header) {
    if (header == null) {
        return false;
    }// w w  w.  ja  v  a  2s  . c  om
    mCurrentBoxSequence.add(header);
    long boxEndOffset = mCurrentOffset + header.boxDataSize;
    boolean parseOK = true;
    if (header.boxType == BOX_ID_FTYP) {
        int majorBrand;
        try {
            majorBrand = mDataSource.readInt();
            if (majorBrand == FTYP_BRAND_MGSV) {
                mIsMarlinProtected = true;
            }
        } catch (EOFException e) {
            if (LOGS_ENABLED)
                Log.e(TAG, "Exception parsing 'ftyp' box", e);
        } catch (IOException e) {
            if (LOGS_ENABLED)
                Log.e(TAG, "Exception parsing 'ftyp' box", e);
        }
    } else if (header.boxType == BOX_ID_UUID) {
        byte[] userType = new byte[16];
        try {
            mDataSource.read(userType);
            mCurrentOffset += 16;
            String uuidUserType = Util.bytesToHex(userType);
            if (uuidUserType.equals(UUID_PROF)) {
                parseOK = parseUuidPROF(header);
            } else if (uuidUserType.equals(UUID_MTSD)) {
                // MTSD box
                mMtsdOffset = header.startOffset;
                mNeedsMTSD = false;
            } else if (uuidUserType.equals(UUID_USMT)) {
                // USMT box
                while (mCurrentOffset < boxEndOffset && parseOK) {
                    BoxHeader nextBoxHeader = getNextBoxHeader();
                    parseOK = parseBox(nextBoxHeader);
                }
            } else {
                mCurrentOffset -= 16;
                parseOK = super.parseBox(header);
            }
        } catch (IOException e) {
            if (LOGS_ENABLED)
                Log.e(TAG, "Error parsing 'uuid' box", e);
            parseOK = false;
        }
    } else if (header.boxType == BOX_ID_MTDT) {
        parseOK = parseMtdt(header);
    } else if (header.boxType == BOX_ID_MTSM) {
        if (mMtsmList == null) {
            mMtsmList = new ArrayList<MtsmEntry>();
        }
        mCurrentMtsmEntry = new MtsmEntry();
        while (mCurrentOffset < boxEndOffset && parseOK) {
            BoxHeader nextBoxHeader = getNextBoxHeader();
            parseOK = parseBox(nextBoxHeader);
        }
        mMtsmList.add(mCurrentMtsmEntry);
    } else if (header.boxType == BOX_ID_MTHD) {
        try {
            mDataSource.skipBytes(12);
            mCurrentMtsmEntry.id = mDataSource.readInt();
            mDataSource.skipBytes(8);
        } catch (IOException e) {
            if (LOGS_ENABLED)
                Log.e(TAG, "Exception reading 'MTHD' box", e);
        }
    } else if (header.boxType == BOX_ID_MDST) {
        try {
            mDataSource.skipBytes(4);
            int metadataSampleCount = mDataSource.readInt();
            mCurrentMtsmEntry.mMdstList = new ArrayList<MdstEntry>(metadataSampleCount);
            for (int i = 0; i < metadataSampleCount; i++) {
                MdstEntry mdstEntry = new MdstEntry();
                mdstEntry.metadataSampleDescriptionIndex = mDataSource.readInt();
                mdstEntry.metadataSampleOffset = mDataSource.readInt();
                mdstEntry.metadataSampleSize = mDataSource.readInt();
                mDataSource.skipBytes(8);
                mCurrentMtsmEntry.mMdstList.add(mdstEntry);
            }
            mNeedsMTSD = true;
        } catch (EOFException e) {
            if (LOGS_ENABLED)
                Log.e(TAG, "Exception reading from 'MDST' box", e);
        } catch (IOException e) {
            if (LOGS_ENABLED)
                Log.e(TAG, "Exception reading from 'MDST' box", e);
        }
    } else if (header.boxType == BOX_ID_STGS) {
        MediaFormat mediaFormat = MediaFormat.createSubtitleFormat("subtitle/grap-text",
                mCurrentTrack.getLanguage());
        mCurrentTrack.addSampleDescriptionEntry(mediaFormat);

        mCurrentTrack.getMetaData().addValue(KEY_MIME_TYPE, mediaFormat.getString(MediaFormat.KEY_MIME));
    } else if (header.boxType == BOX_ID_AVCC) {
        byte[] data = new byte[(int) header.boxDataSize];
        try {
            if (mDataSource.readAt(mCurrentOffset, data, data.length) != data.length) {
                mCurrentBoxSequence.removeLast();
                return false;
            }
        } catch (IOException e) {
            if (LOGS_ENABLED)
                Log.e(TAG, "Error while parsing 'avcc' box", e);
            mCurrentBoxSequence.removeLast();
            return false;
        }

        if (mIsMarlinProtected) {
            ByteBuffer buffer = parseAvccForMarlin(data);
            if (buffer == null) {
                return false;
            }
            mCurrentMediaFormat.setByteBuffer("csd-0", buffer);

            parseSPS(buffer.array());
        } else {
            AvccData avccData = parseAvcc(data);
            if (avccData == null) {
                return false;
            }
            ByteBuffer csd0 = ByteBuffer.wrap(avccData.spsBuffer.array());
            ByteBuffer csd1 = ByteBuffer.wrap(avccData.ppsBuffer.array());
            mCurrentMediaFormat.setByteBuffer("csd-0", csd0);
            mCurrentMediaFormat.setByteBuffer("csd-1", csd1);

            parseSPS(avccData.spsBuffer.array());
        }
    } else if (header.boxType == BOX_ID_MDAT) {
        if (mTracks.size() > 0 && !mIsFragmented && !mNeedsMTSD) {
            mInitDone = true;
        } else if (mIsFragmented && mFirstMoofOffset != -1) {
            mInitDone = true;
        } else {
            mMdatFound = true;
        }
    } else {
        parseOK = super.parseBox(header);
    }
    mCurrentOffset = boxEndOffset;
    mCurrentBoxSequence.removeLast();
    return parseOK;
}