public final class

DumpableMp4Box

extends java.lang.Object

implements Dumper.Dumpable

 java.lang.Object

↳androidx.media3.test.utils.DumpableMp4Box

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-test-utils', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-test-utils
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-test-utils:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Wraps an Mp4 box to allow dumping it.

Summary

Constructors
publicDumpableMp4Box(java.nio.ByteBuffer box)

Creates an instance.

Methods
public voiddump(Dumper dumper)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public DumpableMp4Box(java.nio.ByteBuffer box)

Creates an instance.

Parameters:

box: The Mp4 box to wrap.

Methods

public void dump(Dumper dumper)

Source

/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.test.utils;

import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.common.util.UnstableApi;
import com.google.common.collect.ImmutableSet;
import java.nio.ByteBuffer;

/** Wraps an Mp4 box to allow dumping it. */
@UnstableApi
public final class DumpableMp4Box implements Dumper.Dumpable {
  private static final ImmutableSet<String> CONTAINER_BOXES =
      ImmutableSet.of(
          "moov", "trak", "mdia", "minf", "stbl", "edts", "meta", "mvex", "moof", "traf", "edvd");
  private final ParsableByteArray box;

  /***
   * Creates an instance.
   *
   * @param box The Mp4 box to wrap.
   */
  public DumpableMp4Box(ByteBuffer box) {
    byte[] boxArray = new byte[box.remaining()];
    box.get(boxArray);
    this.box = new ParsableByteArray(boxArray);
  }

  @Override
  public void dump(Dumper dumper) {
    dumpBoxRecursively(box.limit(), dumper);
  }

  private void dumpBoxRecursively(long endPosition, Dumper dumper) {
    while (box.getPosition() < endPosition) {
      long size = box.readInt();
      String name = box.readString(/* length= */ 4);
      long payloadSize = size - 8;
      // When the box needs 64-bit box size (typically used for mdat box), then the original 32-bit
      // box size is set to 1.
      if (size == 1) {
        size = box.readUnsignedLongToLong();
        // Parsing is not supported for box having size > Integer.MAX_VALUE.
        Assertions.checkState(size <= Integer.MAX_VALUE);
        // Subtract 4 bytes (32-bit box size) + 4 bytes (box name) + 8 bytes (64-bit box size).
        payloadSize = size - 16;
      }
      dumper.startBlock(name + " (" + size + " bytes)");
      if (CONTAINER_BOXES.contains(name)) {
        dumpBoxRecursively(box.getPosition() + payloadSize, dumper);
      } else {
        byte[] data = new byte[(int) payloadSize];
        box.readBytes(data, /* offset= */ 0, (int) payloadSize);
        dumper.add("Data", data);
      }
      dumper.endBlock();
    }
  }
}