Write metadata to Mp4Muxer in the release() method

Earlier metadata was written multiple times as it came.
With new changes, all the distinct metadata entries will
get collected and will be written at once in the end.

PiperOrigin-RevId: 534088401
This commit is contained in:
sheenachhabra 2023-05-22 17:24:34 +01:00 committed by tonihei
parent a6897aedaa
commit a9e3f5def4

View File

@ -33,7 +33,9 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** {@link Muxer} implementation that uses a {@link Mp4Muxer}. */ /** {@link Muxer} implementation that uses a {@link Mp4Muxer}. */
@UnstableApi @UnstableApi
@ -85,12 +87,14 @@ public final class InAppMuxer implements Muxer {
private final long maxDelayBetweenSamplesMs; private final long maxDelayBetweenSamplesMs;
private final List<TrackToken> trackTokenList; private final List<TrackToken> trackTokenList;
private final BufferInfo bufferInfo; private final BufferInfo bufferInfo;
private final Set<Metadata.Entry> metadataEntries;
private InAppMuxer(Mp4Muxer mp4Muxer, long maxDelayBetweenSamplesMs) { private InAppMuxer(Mp4Muxer mp4Muxer, long maxDelayBetweenSamplesMs) {
this.mp4Muxer = mp4Muxer; this.mp4Muxer = mp4Muxer;
this.maxDelayBetweenSamplesMs = maxDelayBetweenSamplesMs; this.maxDelayBetweenSamplesMs = maxDelayBetweenSamplesMs;
trackTokenList = new ArrayList<>(); trackTokenList = new ArrayList<>();
bufferInfo = new BufferInfo(); bufferInfo = new BufferInfo();
metadataEntries = new LinkedHashSet<>();
} }
@Override @Override
@ -133,15 +137,16 @@ public final class InAppMuxer implements Muxer {
public void addMetadata(Metadata metadata) { public void addMetadata(Metadata metadata) {
for (int i = 0; i < metadata.length(); i++) { for (int i = 0; i < metadata.length(); i++) {
Metadata.Entry entry = metadata.get(i); Metadata.Entry entry = metadata.get(i);
// Keep only supported metadata.
if (entry instanceof Mp4LocationData) { if (entry instanceof Mp4LocationData) {
mp4Muxer.setLocation( metadataEntries.add(entry);
((Mp4LocationData) entry).latitude, ((Mp4LocationData) entry).longitude);
} }
} }
} }
@Override @Override
public void release(boolean forCancellation) throws MuxerException { public void release(boolean forCancellation) throws MuxerException {
writeMetadata();
try { try {
mp4Muxer.close(); mp4Muxer.close();
} catch (IOException e) { } catch (IOException e) {
@ -153,4 +158,13 @@ public final class InAppMuxer implements Muxer {
public long getMaxDelayBetweenSamplesMs() { public long getMaxDelayBetweenSamplesMs() {
return maxDelayBetweenSamplesMs; return maxDelayBetweenSamplesMs;
} }
private void writeMetadata() {
for (Metadata.Entry entry : metadataEntries) {
if (entry instanceof Mp4LocationData) {
mp4Muxer.setLocation(
((Mp4LocationData) entry).latitude, ((Mp4LocationData) entry).longitude);
}
}
}
} }