Skip to content

Commit

Permalink
Add getMetrics() API to MediaExtractorCompat
Browse files Browse the repository at this point in the history
This method returns a `PersistableBundle` containing metrics data for the current media container. The bundle includes attributes and values for the media container, as described in `MediaExtractor.MetricsConstants`.

PiperOrigin-RevId: 696893276
  • Loading branch information
rohitjoins authored and copybara-github committed Nov 15, 2024
1 parent 1af0b5b commit ecc5cd8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.media.metrics.MediaMetricsManager;
import android.media.metrics.PlaybackSession;
import android.net.Uri;
import android.os.PersistableBundle;
import androidx.media3.common.C;
import androidx.media3.common.DrmInitData;
import androidx.media3.common.Format;
Expand All @@ -49,6 +50,7 @@
import androidx.media3.extractor.SeekMap.SeekPoints;
import androidx.media3.extractor.SeekPoint;
import androidx.media3.extractor.TrackOutput;
import androidx.media3.extractor.mp4.Mp4Extractor;
import androidx.media3.test.utils.TestUtil;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
Expand Down Expand Up @@ -1002,6 +1004,27 @@ public void getDrmInitData_withMultipleTracksHavingDrmInitData_returnsFirstNonNu
assertThat(mediaExtractorCompat.hasCacheReachedEndOfStream()).isTrue();
}

@Test
public void getMetrics_withMp4DataSource_returnsExpectedMetricsBundle() throws IOException {
assumeTrue(Util.SDK_INT >= 26);
// Needed to keep lint happy (it doesn't understand the assumeTrue call alone)
if (Util.SDK_INT < 26) {
return;
}
Context context = ApplicationProvider.getApplicationContext();
Uri contentUri = Uri.parse("asset:///media/mp4/sample.mp4");
MediaExtractorCompat mediaExtractorCompat = new MediaExtractorCompat(context);
mediaExtractorCompat.setDataSource(context, contentUri, /* headers= */ null);

PersistableBundle bundle = mediaExtractorCompat.getMetrics();

assertThat(bundle.getString(MediaExtractor.MetricsConstants.FORMAT))
.isEqualTo(Mp4Extractor.class.getSimpleName());
assertThat(bundle.getString(MediaExtractor.MetricsConstants.MIME_TYPE))
.isEqualTo(MimeTypes.VIDEO_MP4);
assertThat(bundle.getInt(MediaExtractor.MetricsConstants.TRACKS)).isEqualTo(2);
}

// Internal methods.

private void assertReadSample(int trackIndex, long timeUs, int size, byte... sampleData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.media.MediaFormat;
import android.media.metrics.LogSessionId;
import android.net.Uri;
import android.os.PersistableBundle;
import android.util.SparseArray;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -657,6 +658,30 @@ public boolean hasCacheReachedEndOfStream() {
return getCachedDuration() == 0;
}

/**
* Returns a {@link PersistableBundle} containing metrics data for the current media container.
*
* <p>The bundle includes attributes and values for the media container, as described in {@link
* MediaExtractor.MetricsConstants}.
*/
@RequiresApi(26)
public PersistableBundle getMetrics() {
PersistableBundle bundle = new PersistableBundle();
if (currentExtractor != null) {
bundle.putString(
MediaExtractor.MetricsConstants.FORMAT,
currentExtractor.getUnderlyingImplementation().getClass().getSimpleName());
}
if (!tracks.isEmpty()) {
Format format = tracks.get(0).getFormat(formatHolder, noDataBuffer);
if (format.containerMimeType != null) {
bundle.putString(MediaExtractor.MetricsConstants.MIME_TYPE, format.containerMimeType);
}
}
bundle.putInt(MediaExtractor.MetricsConstants.TRACKS, tracks.size());
return bundle;
}

@VisibleForTesting(otherwise = NONE)
public Allocator getAllocator() {
return allocator;
Expand Down

0 comments on commit ecc5cd8

Please sign in to comment.