mirror of
https://github.com/samsonjs/media.git
synced 2026-03-26 09:35:47 +00:00
Add an Input interface to SampleExporter.
PiperOrigin-RevId: 546822962
This commit is contained in:
parent
3d4bd7ce19
commit
ddd000128f
7 changed files with 55 additions and 31 deletions
|
|
@ -46,7 +46,7 @@ import java.util.concurrent.ConcurrentLinkedDeque;
|
|||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/** Processes raw audio samples. */
|
||||
/* package */ final class AudioGraph implements SampleConsumer, OnMediaItemChangedListener {
|
||||
/* package */ final class AudioGraph implements GraphInput {
|
||||
private static final int MAX_INPUT_BUFFER_COUNT = 10;
|
||||
private final AudioFormat outputAudioFormat;
|
||||
private final SilentAudioGenerator silentAudioGenerator;
|
||||
|
|
|
|||
|
|
@ -32,8 +32,9 @@ import androidx.media3.decoder.DecoderInputBuffer;
|
|||
import java.nio.ByteBuffer;
|
||||
import org.checkerframework.dataflow.qual.Pure;
|
||||
|
||||
/** Processes, re-encodes and muxes raw audio samples. */
|
||||
/** Processes, encodes and muxes raw audio samples. */
|
||||
/* package */ final class AudioSampleExporter extends SampleExporter {
|
||||
|
||||
private static final int DEFAULT_ENCODER_BITRATE = 128 * 1024;
|
||||
|
||||
private final Codec encoder;
|
||||
|
|
@ -96,23 +97,8 @@ import org.checkerframework.dataflow.qual.Pure;
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onMediaItemChanged(
|
||||
EditedMediaItem editedMediaItem,
|
||||
long durationUs,
|
||||
@Nullable Format trackFormat,
|
||||
boolean isLast) {
|
||||
audioGraph.onMediaItemChanged(editedMediaItem, durationUs, trackFormat, isLast);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public DecoderInputBuffer getInputBuffer() {
|
||||
return audioGraph.getInputBuffer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean queueInputBuffer() {
|
||||
return audioGraph.queueInputBuffer();
|
||||
public GraphInput getInput() {
|
||||
return audioGraph;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ import java.util.concurrent.ConcurrentLinkedDeque;
|
|||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/** Muxes encoded samples without any transcoding or transformation. */
|
||||
/* package */ final class EncodedSampleExporter extends SampleExporter {
|
||||
/* package */ final class EncodedSampleExporter extends SampleExporter implements GraphInput {
|
||||
|
||||
private static final int MAX_INPUT_BUFFER_COUNT = 10;
|
||||
|
||||
|
|
@ -88,6 +88,11 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GraphInput getInput() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Copyright 2023 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package androidx.media3.transformer;
|
||||
|
||||
/** Represents one input stream to a processing graph. */
|
||||
/* package */ interface GraphInput extends SampleConsumer, OnMediaItemChangedListener {}
|
||||
|
|
@ -38,14 +38,8 @@ import java.util.List;
|
|||
* Exporter that processes media data.
|
||||
*
|
||||
* <p>This exporter can be used to implement transformations of audio or video samples.
|
||||
*
|
||||
* <p>The {@link SampleConsumer} and {@link OnMediaItemChangedListener} methods must be called from
|
||||
* the same thread. This thread can change when the {@link
|
||||
* OnMediaItemChangedListener#onMediaItemChanged(EditedMediaItem, long, Format, boolean) MediaItem}
|
||||
* changes, and can be different from the thread used to call the other {@code SampleExporter}
|
||||
* methods.
|
||||
*/
|
||||
/* package */ abstract class SampleExporter implements SampleConsumer, OnMediaItemChangedListener {
|
||||
/* package */ abstract class SampleExporter {
|
||||
|
||||
private final MuxerWrapper muxerWrapper;
|
||||
private final @C.TrackType int outputTrackType;
|
||||
|
|
@ -59,6 +53,17 @@ import java.util.List;
|
|||
outputTrackType = getProcessedTrackType(firstInputFormat.sampleMimeType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link GraphInput} to the underlying processing graph.
|
||||
*
|
||||
* <p>The {@link GraphInput} methods must be called from the same thread. This thread can change
|
||||
* when the {@linkplain GraphInput#onMediaItemChanged MediaItem changes}, and can be different
|
||||
* from the thread used to call the other {@link GraphInput} methods. All subsequent {@link
|
||||
* GraphInput} method calls must be made from the thread on which {@link
|
||||
* GraphInput#onMediaItemChanged} is called.
|
||||
*/
|
||||
public abstract GraphInput getInput();
|
||||
|
||||
/**
|
||||
* Processes the input data and returns whether it may be possible to process more data by calling
|
||||
* this method again.
|
||||
|
|
|
|||
|
|
@ -526,18 +526,20 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
|||
@C.TrackType int trackType = getProcessedTrackType(assetLoaderOutputFormat.sampleMimeType);
|
||||
AddedTrackInfo trackInfo = checkStateNotNull(addedTrackInfoByTrackType.get(trackType));
|
||||
SampleExporter sampleExporter = getSampleExporter(assetLoaderOutputFormat, trackInfo);
|
||||
GraphInput sampleExporterInput = sampleExporter.getInput();
|
||||
|
||||
OnMediaItemChangedListener onMediaItemChangedListener =
|
||||
(editedMediaItem, durationUs, trackFormat, isLast) -> {
|
||||
onMediaItemChanged(trackType, durationUs, isLast);
|
||||
sampleExporter.onMediaItemChanged(editedMediaItem, durationUs, trackFormat, isLast);
|
||||
sampleExporterInput.onMediaItemChanged(
|
||||
editedMediaItem, durationUs, trackFormat, isLast);
|
||||
};
|
||||
sequenceAssetLoaders
|
||||
.get(sequenceIndex)
|
||||
.addOnMediaItemChangedListener(onMediaItemChangedListener, trackType);
|
||||
|
||||
internalHandler.obtainMessage(MSG_REGISTER_SAMPLE_EXPORTER, sampleExporter).sendToTarget();
|
||||
return sampleExporter;
|
||||
return sampleExporterInput;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -64,8 +64,9 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.dataflow.qual.Pure;
|
||||
|
||||
/** Processes, re-encodes and muxes raw video frames. */
|
||||
/* package */ final class VideoSampleExporter extends SampleExporter {
|
||||
// TODO: b/289986435 - Remove implementations of GraphInput after creating VideoGraph.
|
||||
/** Processes, encodes and muxes raw video frames. */
|
||||
/* package */ final class VideoSampleExporter extends SampleExporter implements GraphInput {
|
||||
|
||||
private static final String TAG = "VideoSampleExporter";
|
||||
private final AtomicLong mediaItemOffsetUs;
|
||||
|
|
@ -266,6 +267,11 @@ import org.checkerframework.dataflow.qual.Pure;
|
|||
videoFrameProcessor.signalEndOfInput();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GraphInput getInput() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
videoFrameProcessor.release();
|
||||
|
|
|
|||
Loading…
Reference in a new issue