Search in sources :

Example 1 with FramePredicate

use of com.google.android.exoplayer2.metadata.id3.Id3Decoder.FramePredicate in project ExoPlayer by google.

the class Id3Decoder method decode.

/**
 * Decodes ID3 tags.
 *
 * @param data The bytes to decode ID3 tags from.
 * @param size Amount of bytes in {@code data} to read.
 * @return A {@link Metadata} object containing the decoded ID3 tags, or null if the data could
 *     not be decoded.
 */
@Nullable
public Metadata decode(byte[] data, int size) {
    List<Id3Frame> id3Frames = new ArrayList<>();
    ParsableByteArray id3Data = new ParsableByteArray(data, size);
    @Nullable Id3Header id3Header = decodeHeader(id3Data);
    if (id3Header == null) {
        return null;
    }
    int startPosition = id3Data.getPosition();
    int frameHeaderSize = id3Header.majorVersion == 2 ? 6 : 10;
    int framesSize = id3Header.framesSize;
    if (id3Header.isUnsynchronized) {
        framesSize = removeUnsynchronization(id3Data, id3Header.framesSize);
    }
    id3Data.setLimit(startPosition + framesSize);
    boolean unsignedIntFrameSizeHack = false;
    if (!validateFrames(id3Data, id3Header.majorVersion, frameHeaderSize, false)) {
        if (id3Header.majorVersion == 4 && validateFrames(id3Data, 4, frameHeaderSize, true)) {
            unsignedIntFrameSizeHack = true;
        } else {
            Log.w(TAG, "Failed to validate ID3 tag with majorVersion=" + id3Header.majorVersion);
            return null;
        }
    }
    while (id3Data.bytesLeft() >= frameHeaderSize) {
        @Nullable Id3Frame frame = decodeFrame(id3Header.majorVersion, id3Data, unsignedIntFrameSizeHack, frameHeaderSize, framePredicate);
        if (frame != null) {
            id3Frames.add(frame);
        }
    }
    return new Metadata(id3Frames);
}
Also used : ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray) ArrayList(java.util.ArrayList) Metadata(com.google.android.exoplayer2.metadata.Metadata) Nullable(androidx.annotation.Nullable) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)1 Metadata (com.google.android.exoplayer2.metadata.Metadata)1 ParsableByteArray (com.google.android.exoplayer2.util.ParsableByteArray)1 ArrayList (java.util.ArrayList)1