Add support for writing MP4 location data using media muxer

PiperOrigin-RevId: 515297752
This commit is contained in:
sheenachhabra 2023-03-09 12:22:57 +00:00 committed by tonihei
parent 9be419e679
commit f69160079c
5 changed files with 48 additions and 0 deletions

View File

@ -76,6 +76,11 @@ public final class DefaultMuxer implements Muxer {
this.muxer = muxer;
}
@Override
public void setLocation(float latitude, float longitude) {
this.muxer.setLocation(latitude, longitude);
}
@Override
public int addTrack(Format format) throws MuxerException {
return muxer.addTrack(format);

View File

@ -104,6 +104,11 @@ import java.nio.ByteBuffer;
videoTrackIndex = C.INDEX_UNSET;
}
@Override
public void setLocation(float latitude, float longitude) {
mediaMuxer.setLocation(latitude, longitude);
}
@Override
public int addTrack(Format format) throws MuxerException {
String sampleMimeType = checkNotNull(format.sampleMimeType);

View File

@ -63,6 +63,14 @@ public interface Muxer {
ImmutableList<String> getSupportedSampleMimeTypes(@C.TrackType int trackType);
}
/**
* Sets the location.
*
* @param latitude The latitude, in degrees. Its value must be in the range [-90, 90].
* @param longitude The longitude, in degrees. Its value must be in the range [-180, 180].
*/
void setLocation(float latitude, float longitude);
/**
* Adds a track with the specified format.
*

View File

@ -27,6 +27,8 @@ import androidx.annotation.IntRange;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.mp4.Mp4LocationData;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
@ -129,6 +131,9 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
* and all the formats must be added before any samples can be {@linkplain #writeSample(int,
* ByteBuffer, boolean, long) written}.
*
* <p>If the {@link Format#metadata} contains {@link Mp4LocationData}, then it will be added to
* the muxer.
*
* @param format The {@link Format} to be added.
* @throws IllegalArgumentException If the format is unsupported.
* @throws IllegalStateException If the number of formats added exceeds the {@linkplain
@ -159,6 +164,11 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
isReady = true;
resetAbortTimer();
}
@Nullable Mp4LocationData mp4LocationData = extractMp4LocationData(format);
if (mp4LocationData != null) {
muxer.setLocation(mp4LocationData.latitude, mp4LocationData.longitude);
}
}
/**
@ -328,6 +338,21 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
return trackInfoWithMinTimeUs;
}
@Nullable
private static Mp4LocationData extractMp4LocationData(Format format) {
if (format.metadata == null) {
return null;
}
for (int i = 0; i < format.metadata.length(); i++) {
Metadata.Entry entry = format.metadata.get(i);
if (entry instanceof Mp4LocationData) {
return (Mp4LocationData) entry;
}
}
return null;
}
private static final class TrackInfo {
public final Format format;
public final int index;

View File

@ -42,6 +42,11 @@ public final class TestMuxer implements Muxer, Dumper.Dumpable {
// Muxer implementation.
@Override
public void setLocation(float latitude, float longitude) {
muxer.setLocation(latitude, longitude);
}
@Override
public int addTrack(Format format) throws MuxerException {
int trackIndex = muxer.addTrack(format);