mirror of
https://github.com/samsonjs/media.git
synced 2026-03-25 09:25:53 +00:00
Add getMetrics() API to MediaExtractorCompat
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
This commit is contained in:
parent
1af0b5b432
commit
ecc5cd889f
2 changed files with 48 additions and 0 deletions
|
|
@ -31,6 +31,7 @@ import android.media.metrics.LogSessionId;
|
|||
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;
|
||||
|
|
@ -49,6 +50,7 @@ import androidx.media3.extractor.SeekMap;
|
|||
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;
|
||||
|
|
@ -1002,6 +1004,27 @@ public class MediaExtractorCompatTest {
|
|||
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) {
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import android.media.MediaExtractor;
|
|||
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;
|
||||
|
|
@ -657,6 +658,30 @@ public final class MediaExtractorCompat {
|
|||
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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue