Search in sources :

Example 11 with FlacStreamMetadataHolder

use of com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder in project ExoPlayer by google.

the class FlacFrameReaderTest method checkFrameHeaderFromPeek_validData_doesNotUpdatePositions.

@Test
public void checkFrameHeaderFromPeek_validData_doesNotUpdatePositions() throws Exception {
    String file = "media/flac/bear_one_metadata_block.flac";
    FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder(/* flacStreamMetadata= */
    null);
    ExtractorInput input = buildExtractorInputReadingFromFirstFrame(file, streamMetadataHolder);
    int frameStartMarker = FlacMetadataReader.getFrameStartMarker(input);
    long peekPosition = input.getPosition();
    // Set read position to 0.
    input = buildExtractorInput(file);
    input.advancePeekPosition((int) peekPosition);
    FlacFrameReader.checkFrameHeaderFromPeek(input, streamMetadataHolder.flacStreamMetadata, frameStartMarker, new SampleNumberHolder());
    assertThat(input.getPosition()).isEqualTo(0);
    assertThat(input.getPeekPosition()).isEqualTo(peekPosition);
}
Also used : FlacStreamMetadataHolder(com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder) SampleNumberHolder(com.google.android.exoplayer2.extractor.FlacFrameReader.SampleNumberHolder) FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput) Test(org.junit.Test)

Example 12 with FlacStreamMetadataHolder

use of com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder in project ExoPlayer by google.

the class FlacMetadataReader method readMetadataBlock.

/**
 * Reads one FLAC metadata block.
 *
 * <p>If no exception is thrown, the peek position of {@code input} is aligned with the read
 * position.
 *
 * @param input Input stream to read the metadata block from (header included).
 * @param metadataHolder A holder for the metadata read. If the stream info block (which must be
 *     the first metadata block) is read, the holder contains a new instance representing the
 *     stream info data. If the block read is a Vorbis comment block or a picture block, the
 *     holder contains a copy of the existing stream metadata with the corresponding metadata
 *     added. Otherwise, the metadata in the holder is unchanged.
 * @return Whether the block read is the last metadata block.
 * @throws IllegalArgumentException If the block read is not a stream info block and the metadata
 *     in {@code metadataHolder} is {@code null}. In this case, the read position will be at the
 *     start of a metadata block and there is no guarantee on the peek position.
 * @throws IOException If reading from the input fails. In this case, the read position will be at
 *     the start of a metadata block and there is no guarantee on the peek position.
 */
public static boolean readMetadataBlock(ExtractorInput input, FlacStreamMetadataHolder metadataHolder) throws IOException {
    input.resetPeekPosition();
    ParsableBitArray scratch = new ParsableBitArray(new byte[4]);
    input.peekFully(scratch.data, 0, FlacConstants.METADATA_BLOCK_HEADER_SIZE);
    boolean isLastMetadataBlock = scratch.readBit();
    int type = scratch.readBits(7);
    int length = FlacConstants.METADATA_BLOCK_HEADER_SIZE + scratch.readBits(24);
    if (type == FlacConstants.METADATA_TYPE_STREAM_INFO) {
        metadataHolder.flacStreamMetadata = readStreamInfoBlock(input);
    } else {
        @Nullable FlacStreamMetadata flacStreamMetadata = metadataHolder.flacStreamMetadata;
        if (flacStreamMetadata == null) {
            throw new IllegalArgumentException();
        }
        if (type == FlacConstants.METADATA_TYPE_SEEK_TABLE) {
            FlacStreamMetadata.SeekTable seekTable = readSeekTableMetadataBlock(input, length);
            metadataHolder.flacStreamMetadata = flacStreamMetadata.copyWithSeekTable(seekTable);
        } else if (type == FlacConstants.METADATA_TYPE_VORBIS_COMMENT) {
            List<String> vorbisComments = readVorbisCommentMetadataBlock(input, length);
            metadataHolder.flacStreamMetadata = flacStreamMetadata.copyWithVorbisComments(vorbisComments);
        } else if (type == FlacConstants.METADATA_TYPE_PICTURE) {
            ParsableByteArray pictureBlock = new ParsableByteArray(length);
            input.readFully(pictureBlock.getData(), 0, length);
            pictureBlock.skipBytes(FlacConstants.METADATA_BLOCK_HEADER_SIZE);
            PictureFrame pictureFrame = PictureFrame.fromPictureBlock(pictureBlock);
            metadataHolder.flacStreamMetadata = flacStreamMetadata.copyWithPictureFrames(ImmutableList.of(pictureFrame));
        } else {
            input.skipFully(length);
        }
    }
    return isLastMetadataBlock;
}
Also used : ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PictureFrame(com.google.android.exoplayer2.metadata.flac.PictureFrame) Nullable(androidx.annotation.Nullable) ParsableBitArray(com.google.android.exoplayer2.util.ParsableBitArray)

Example 13 with FlacStreamMetadataHolder

use of com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder in project ExoPlayer by google.

the class FlacMetadataReaderTest method readMetadataBlock_vorbisCommentBlock_updatesStreamMetadata.

@Test
public void readMetadataBlock_vorbisCommentBlock_updatesStreamMetadata() throws Exception {
    ExtractorInput input = buildExtractorInput("media/flac/bear_with_vorbis_comments.flac");
    // Skip to Vorbis comment block.
    input.skipFully(640);
    FlacStreamMetadataHolder metadataHolder = new FlacStreamMetadataHolder(buildStreamMetadata());
    long originalSampleRate = metadataHolder.flacStreamMetadata.sampleRate;
    FlacMetadataReader.readMetadataBlock(input, metadataHolder);
    assertThat(metadataHolder.flacStreamMetadata).isNotNull();
    // Check that metadata passed has not been erased.
    assertThat(metadataHolder.flacStreamMetadata.sampleRate).isEqualTo(originalSampleRate);
    Metadata metadata = metadataHolder.flacStreamMetadata.getMetadataCopyWithAppendedEntriesFrom(null);
    assertThat(metadata).isNotNull();
    VorbisComment vorbisComment = (VorbisComment) metadata.get(0);
    assertThat(vorbisComment.key).isEqualTo("TITLE");
    assertThat(vorbisComment.value).isEqualTo("test title");
}
Also used : FlacStreamMetadataHolder(com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder) FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput) Metadata(com.google.android.exoplayer2.metadata.Metadata) VorbisComment(com.google.android.exoplayer2.metadata.vorbis.VorbisComment) Test(org.junit.Test)

Example 14 with FlacStreamMetadataHolder

use of com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder in project ExoPlayer by google.

the class FlacMetadataReaderTest method readMetadataBlock_notLastMetadataBlock_isFalse.

@Test
public void readMetadataBlock_notLastMetadataBlock_isFalse() throws Exception {
    ExtractorInput input = buildExtractorInput("media/flac/bear.flac");
    input.skipFully(FlacConstants.STREAM_MARKER_SIZE);
    boolean result = FlacMetadataReader.readMetadataBlock(input, new FlacStreamMetadataHolder(/* flacStreamMetadata= */
    null));
    assertThat(result).isFalse();
}
Also used : FlacStreamMetadataHolder(com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder) FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput) Test(org.junit.Test)

Example 15 with FlacStreamMetadataHolder

use of com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder in project ExoPlayer by google.

the class FlacMetadataReaderTest method readMetadataBlock_pictureBlock_updatesStreamMetadata.

@Test
public void readMetadataBlock_pictureBlock_updatesStreamMetadata() throws Exception {
    ExtractorInput input = buildExtractorInput("media/flac/bear_with_picture.flac");
    // Skip to picture block.
    input.skipFully(640);
    FlacStreamMetadataHolder metadataHolder = new FlacStreamMetadataHolder(buildStreamMetadata());
    long originalSampleRate = metadataHolder.flacStreamMetadata.sampleRate;
    FlacMetadataReader.readMetadataBlock(input, metadataHolder);
    assertThat(metadataHolder.flacStreamMetadata).isNotNull();
    // Check that metadata passed has not been erased.
    assertThat(metadataHolder.flacStreamMetadata.sampleRate).isEqualTo(originalSampleRate);
    Metadata metadata = metadataHolder.flacStreamMetadata.getMetadataCopyWithAppendedEntriesFrom(null);
    assertThat(metadata).isNotNull();
    PictureFrame pictureFrame = (PictureFrame) metadata.get(0);
    assertThat(pictureFrame.pictureType).isEqualTo(3);
    assertThat(pictureFrame.mimeType).isEqualTo("image/png");
    assertThat(pictureFrame.description).isEqualTo("");
    assertThat(pictureFrame.width).isEqualTo(371);
    assertThat(pictureFrame.height).isEqualTo(320);
    assertThat(pictureFrame.depth).isEqualTo(24);
    assertThat(pictureFrame.colors).isEqualTo(0);
    assertThat(pictureFrame.pictureData).hasLength(30943);
}
Also used : FlacStreamMetadataHolder(com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder) FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput) Metadata(com.google.android.exoplayer2.metadata.Metadata) PictureFrame(com.google.android.exoplayer2.metadata.flac.PictureFrame) Test(org.junit.Test)

Aggregations

FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)21 FlacStreamMetadataHolder (com.google.android.exoplayer2.extractor.FlacMetadataReader.FlacStreamMetadataHolder)20 Test (org.junit.Test)20 SampleNumberHolder (com.google.android.exoplayer2.extractor.FlacFrameReader.SampleNumberHolder)9 ParsableByteArray (com.google.android.exoplayer2.util.ParsableByteArray)5 Metadata (com.google.android.exoplayer2.metadata.Metadata)2 PictureFrame (com.google.android.exoplayer2.metadata.flac.PictureFrame)2 Nullable (androidx.annotation.Nullable)1 VorbisComment (com.google.android.exoplayer2.metadata.vorbis.VorbisComment)1 ParsableBitArray (com.google.android.exoplayer2.util.ParsableBitArray)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1