Change Mp4Writer to an interface
The Mp4Writer does not have any default implementation, hence it can be an interface rather than an abstract class. PiperOrigin-RevId: 611110415
This commit is contained in:
parent
08993b6fb1
commit
6809c0a642
@ -30,16 +30,24 @@ import com.google.common.collect.Range;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default implementation of {@link Mp4Writer} which writes all the samples in a single mdat
|
* The default implementation of {@link Mp4Writer} which writes all the samples in a single mdat
|
||||||
* box.
|
* box.
|
||||||
*/
|
*/
|
||||||
/* package */ final class DefaultMp4Writer extends Mp4Writer {
|
/* package */ final class DefaultMp4Writer implements Mp4Writer {
|
||||||
private static final long INTERLEAVE_DURATION_US = 1_000_000L;
|
private static final long INTERLEAVE_DURATION_US = 1_000_000L;
|
||||||
|
|
||||||
|
private final FileOutputStream outputStream;
|
||||||
|
private final FileChannel output;
|
||||||
|
private final Mp4MoovStructure moovGenerator;
|
||||||
|
private final AnnexBToAvccConverter annexBToAvccConverter;
|
||||||
|
private final List<Track> tracks;
|
||||||
private final AtomicBoolean hasWrittenSamples;
|
private final AtomicBoolean hasWrittenSamples;
|
||||||
|
|
||||||
private long mdatStart;
|
private long mdatStart;
|
||||||
@ -62,7 +70,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
FileOutputStream outputStream,
|
FileOutputStream outputStream,
|
||||||
Mp4MoovStructure moovGenerator,
|
Mp4MoovStructure moovGenerator,
|
||||||
AnnexBToAvccConverter annexBToAvccConverter) {
|
AnnexBToAvccConverter annexBToAvccConverter) {
|
||||||
super(outputStream, moovGenerator, annexBToAvccConverter);
|
this.outputStream = outputStream;
|
||||||
|
this.output = outputStream.getChannel();
|
||||||
|
this.moovGenerator = moovGenerator;
|
||||||
|
this.annexBToAvccConverter = annexBToAvccConverter;
|
||||||
|
tracks = new ArrayList<>();
|
||||||
hasWrittenSamples = new AtomicBoolean(false);
|
hasWrittenSamples = new AtomicBoolean(false);
|
||||||
lastMoovWritten = Range.closed(0L, 0L);
|
lastMoovWritten = Range.closed(0L, 0L);
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ import com.google.common.collect.ImmutableList;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||||
@ -43,7 +44,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
|||||||
* An {@link Mp4Writer} implementation which writes samples into multiple fragments as per the
|
* An {@link Mp4Writer} implementation which writes samples into multiple fragments as per the
|
||||||
* fragmented MP4 (ISO/IEC 14496-12) standard.
|
* fragmented MP4 (ISO/IEC 14496-12) standard.
|
||||||
*/
|
*/
|
||||||
/* package */ final class FragmentedMp4Writer extends Mp4Writer {
|
/* package */ final class FragmentedMp4Writer implements Mp4Writer {
|
||||||
/** Provides a limited set of sample metadata. */
|
/** Provides a limited set of sample metadata. */
|
||||||
public static class SampleMetadata {
|
public static class SampleMetadata {
|
||||||
public final long durationVu;
|
public final long durationVu;
|
||||||
@ -57,6 +58,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final FileOutputStream outputStream;
|
||||||
|
private final FileChannel output;
|
||||||
|
private final Mp4MoovStructure moovGenerator;
|
||||||
|
private final AnnexBToAvccConverter annexBToAvccConverter;
|
||||||
|
private final List<Track> tracks;
|
||||||
private final int fragmentDurationUs;
|
private final int fragmentDurationUs;
|
||||||
|
|
||||||
private @MonotonicNonNull Track videoTrack;
|
private @MonotonicNonNull Track videoTrack;
|
||||||
@ -70,7 +76,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
|||||||
Mp4MoovStructure moovGenerator,
|
Mp4MoovStructure moovGenerator,
|
||||||
AnnexBToAvccConverter annexBToAvccConverter,
|
AnnexBToAvccConverter annexBToAvccConverter,
|
||||||
int fragmentDurationUs) {
|
int fragmentDurationUs) {
|
||||||
super(outputStream, moovGenerator, annexBToAvccConverter);
|
this.outputStream = outputStream;
|
||||||
|
this.output = outputStream.getChannel();
|
||||||
|
this.moovGenerator = moovGenerator;
|
||||||
|
this.annexBToAvccConverter = annexBToAvccConverter;
|
||||||
|
tracks = new ArrayList<>();
|
||||||
this.fragmentDurationUs = fragmentDurationUs;
|
this.fragmentDurationUs = fragmentDurationUs;
|
||||||
minInputPresentationTimeUs = Long.MAX_VALUE;
|
minInputPresentationTimeUs = Long.MAX_VALUE;
|
||||||
currentFragmentSequenceNumber = 1;
|
currentFragmentSequenceNumber = 1;
|
||||||
|
@ -24,52 +24,24 @@ import androidx.media3.common.Format;
|
|||||||
import androidx.media3.common.MimeTypes;
|
import androidx.media3.common.MimeTypes;
|
||||||
import androidx.media3.muxer.Mp4Muxer.TrackToken;
|
import androidx.media3.muxer.Mp4Muxer.TrackToken;
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.FileChannel;
|
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** Writes MP4 data to the disk. */
|
/** Writes MP4 data to the disk. */
|
||||||
/* package */ abstract class Mp4Writer {
|
/* package */ interface Mp4Writer {
|
||||||
protected final FileOutputStream outputStream;
|
|
||||||
protected final FileChannel output;
|
|
||||||
protected final Mp4MoovStructure moovGenerator;
|
|
||||||
protected final AnnexBToAvccConverter annexBToAvccConverter;
|
|
||||||
protected final List<Track> tracks;
|
|
||||||
|
|
||||||
/**
|
TrackToken addTrack(int sortKey, Format format);
|
||||||
* Creates an instance.
|
|
||||||
*
|
|
||||||
* @param outputStream The {@link FileOutputStream} to write the data to.
|
|
||||||
* @param moovGenerator An {@link Mp4MoovStructure} instance to generate the moov box.
|
|
||||||
* @param annexBToAvccConverter The {@link AnnexBToAvccConverter} to be used to convert H.264 and
|
|
||||||
* H.265 NAL units from the Annex-B format (using start codes to delineate NAL units) to the
|
|
||||||
* AVCC format (which uses length prefixes).
|
|
||||||
*/
|
|
||||||
public Mp4Writer(
|
|
||||||
FileOutputStream outputStream,
|
|
||||||
Mp4MoovStructure moovGenerator,
|
|
||||||
AnnexBToAvccConverter annexBToAvccConverter) {
|
|
||||||
this.outputStream = outputStream;
|
|
||||||
this.output = outputStream.getChannel();
|
|
||||||
this.moovGenerator = moovGenerator;
|
|
||||||
this.annexBToAvccConverter = annexBToAvccConverter;
|
|
||||||
tracks = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract TrackToken addTrack(int sortKey, Format format);
|
void writeSampleData(Mp4Muxer.TrackToken token, ByteBuffer byteBuffer, BufferInfo bufferInfo)
|
||||||
|
throws IOException;
|
||||||
|
|
||||||
public abstract void writeSampleData(
|
void close() throws IOException;
|
||||||
Mp4Muxer.TrackToken token, ByteBuffer byteBuffer, BufferInfo bufferInfo) throws IOException;
|
|
||||||
|
|
||||||
public abstract void close() throws IOException;
|
class Track implements Mp4Muxer.TrackToken, Mp4MoovStructure.TrackMetadataProvider {
|
||||||
|
|
||||||
protected static class Track
|
|
||||||
implements Mp4Muxer.TrackToken, Mp4MoovStructure.TrackMetadataProvider {
|
|
||||||
public final Format format;
|
public final Format format;
|
||||||
public final int sortKey;
|
public final int sortKey;
|
||||||
public final List<BufferInfo> writtenSamples;
|
public final List<BufferInfo> writtenSamples;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user