Skip to content

Commit 1a7a63c

Browse files
committed
Fix review comment
1 parent ccc0235 commit 1a7a63c

File tree

15 files changed

+109
-83
lines changed

15 files changed

+109
-83
lines changed

libraries/common/src/main/java/androidx/media3/common/VideoGraph.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package androidx.media3.common;
1818

19+
import androidx.annotation.IntRange;
1920
import androidx.annotation.Nullable;
2021
import androidx.media3.common.util.UnstableApi;
2122

@@ -73,20 +74,23 @@ interface Listener {
7374
*

A underlying processing {@link VideoFrameProcessor} is created every time this method is

7475
* called.
7576
*
77+
*

All inputs must be registered before rendering frames to the underlying

78+
* {@link #getProcessor(int) VideoFrameProcessor}.
79+
*
7680
*

If the method throws, the caller must call {@link #release}.

7781
*
78-
* @param sequenceIndex The sequence index of the input which can aid ordering of the inputs. The
79-
* index must start from 0.
82+
* @param inputIndex The index of the input which could be used to order the inputs.
83+
* The index must start from 0.
8084
*/
81-
void registerInput(int sequenceIndex) throws VideoFrameProcessingException;
85+
void registerInput(@IntRange(from = 0) int inputIndex) throws VideoFrameProcessingException;
8286

8387
/**
8488
* Returns the {@link VideoFrameProcessor} that handles the processing for an input registered via
85-
* {@link #registerInput(int)}. If the {@code sequenceIndex} is not {@linkplain
89+
* {@link #registerInput(int)}. If the {@code inputIndex} is not {@linkplain
8690
* #registerInput(int) registered} before, this method will throw an {@link
8791
* IllegalStateException}.
8892
*/
89-
VideoFrameProcessor getProcessor(int sequenceIndex);
93+
VideoFrameProcessor getProcessor(int inputIndex);
9094

9195
/**
9296
* Sets the output surface and supporting information.

libraries/effect/src/main/java/androidx/media3/effect/DefaultVideoCompositor.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static androidx.media3.common.util.Assertions.checkNotNull;
1919
import static androidx.media3.common.util.Assertions.checkState;
2020
import static androidx.media3.common.util.Assertions.checkStateNotNull;
21+
import static androidx.media3.common.util.Util.contains;
2122
import static java.lang.Math.abs;
2223
import static java.lang.Math.max;
2324

@@ -78,7 +79,8 @@ public final class DefaultVideoCompositor implements VideoCompositor {
7879

7980
private static final String THREAD_NAME = "Effect:DefaultVideoCompositor:GlThread";
8081
private static final String TAG = "DefaultVideoCompositor";
81-
private static final int PRIMARY_INPUT_ID = 0;
82+
// TODO: b/338579287: Use the first registered index instead of a constant value.
83+
private static final int PRIMARY_INPUT_INDEX = 0;
8284

8385
private final VideoCompositor.Listener listener;
8486
private final GlTextureProducer.Listener textureOutputListener;
@@ -142,32 +144,35 @@ public DefaultVideoCompositor(
142144
}
143145

144146
@Override
145-
public synchronized void registerInputSource(int sequenceIndex) {
146-
inputSources.put(sequenceIndex, new InputSource());
147+
public synchronized void registerInputSource(@IntRange(from = 0) int inputIndex) {
148+
checkState(!contains(inputSources, inputIndex));
149+
inputSources.put(inputIndex, new InputSource());
147150
}
148151

149152
@Override
150-
public synchronized void signalEndOfInputSource(int inputId) {
151-
inputSources.get(inputId).isInputEnded = true;
153+
public synchronized void signalEndOfInputSource(int inputIndex) {
154+
checkState(contains(inputSources, inputIndex));
155+
inputSources.get(inputIndex).isInputEnded = true;
152156
boolean allInputsEnded = true;
153157
for (int i = 0; i < inputSources.size(); i++) {
154-
if (!inputSources.get(inputSources.keyAt(i)).isInputEnded) {
158+
if (!inputSources.valueAt(i).isInputEnded) {
155159
allInputsEnded = false;
156160
break;
157161
}
158162
}
159163

160164
this.allInputsEnded = allInputsEnded;
161-
if (inputSources.get(PRIMARY_INPUT_ID).frameInfos.isEmpty()) {
162-
if (inputId == PRIMARY_INPUT_ID) {
165+
if (inputSources.get(PRIMARY_INPUT_INDEX).frameInfos.isEmpty()) {
166+
if (inputIndex == PRIMARY_INPUT_INDEX) {
163167
releaseExcessFramesInAllSecondaryStreams();
164168
}
165169
if (allInputsEnded) {
166170
listener.onEnded();
167171
return;
168172
}
169173
}
170-
if (inputId != PRIMARY_INPUT_ID && inputSources.get(inputId).frameInfos.size() == 1) {
174+
if (inputIndex != PRIMARY_INPUT_INDEX
175+
&& inputSources.get(inputIndex).frameInfos.size() == 1) {
171176
// When a secondary stream ends input, composite if there was only one pending frame in the
172177
// stream.
173178
videoFrameProcessingTaskExecutor.submit(this::maybeComposite);
@@ -176,12 +181,13 @@ public synchronized void signalEndOfInputSource(int inputId) {
176181

177182
@Override
178183
public synchronized void queueInputTexture(
179-
int inputId,
184+
int inputIndex,
180185
GlTextureProducer textureProducer,
181186
GlTextureInfo inputTexture,
182187
ColorInfo colorInfo,
183188
long presentationTimeUs) {
184-
InputSource inputSource = inputSources.get(inputId);
189+
checkState(contains(inputSources, inputIndex));
190+
InputSource inputSource = inputSources.get(inputIndex);
185191
checkState(!inputSource.isInputEnded);
186192
checkStateNotNull(!ColorInfo.isTransferHdr(colorInfo), "HDR input is not supported.");
187193
if (configuredColorInfo == null) {
@@ -195,10 +201,10 @@ public synchronized void queueInputTexture(
195201
textureProducer,
196202
inputTexture,
197203
presentationTimeUs,
198-
settings.getOverlaySettings(inputId, presentationTimeUs));
204+
settings.getOverlaySettings(inputIndex, presentationTimeUs));
199205
inputSource.frameInfos.add(inputFrameInfo);
200206

201-
if (inputId == PRIMARY_INPUT_ID) {
207+
if (inputIndex == PRIMARY_INPUT_INDEX) {
202208
releaseExcessFramesInAllSecondaryStreams();
203209
} else {
204210
releaseExcessFramesInSecondaryStream(inputSource);
@@ -224,11 +230,11 @@ public void releaseOutputTexture(long presentationTimeUs) {
224230
}
225231

226232
private synchronized void releaseExcessFramesInAllSecondaryStreams() {
227-
for (int i = 0; i < inputSources.size(); i++) {
228-
if (i == PRIMARY_INPUT_ID) {
233+
for (int inputIndex = 0; inputIndex < inputSources.size(); inputIndex++) {
234+
if (inputIndex == PRIMARY_INPUT_INDEX) {
229235
continue;
230236
}
231-
releaseExcessFramesInSecondaryStream(inputSources.get(inputSources.keyAt(i)));
237+
releaseExcessFramesInSecondaryStream(inputSources.valueAt(inputIndex));
232238
}
233239
}
234240

@@ -240,7 +246,7 @@ private synchronized void releaseExcessFramesInAllSecondaryStreams() {
240246
* began.
241247
*/
242248
private synchronized void releaseExcessFramesInSecondaryStream(InputSource secondaryInputSource) {
243-
InputSource primaryInputSource = inputSources.get(PRIMARY_INPUT_ID);
249+
InputSource primaryInputSource = inputSources.get(PRIMARY_INPUT_INDEX);
244250
// If the primary stream output is ended, all secondary frames can be released.
245251
if (primaryInputSource.frameInfos.isEmpty() && primaryInputSource.isInputEnded) {
246252
releaseFrames(
@@ -291,7 +297,7 @@ private synchronized void maybeComposite()
291297
return;
292298
}
293299

294-
InputFrameInfo primaryInputFrame = framesToComposite.get(PRIMARY_INPUT_ID);
300+
InputFrameInfo primaryInputFrame = framesToComposite.get(PRIMARY_INPUT_INDEX);
295301

296302
ImmutableList.Builder<Size> inputSizes = new ImmutableList.Builder<>();
297303
for (int i = 0; i < framesToComposite.size(); i++) {
@@ -312,7 +318,7 @@ private synchronized void maybeComposite()
312318
textureOutputListener.onTextureRendered(
313319
/* textureProducer= */ this, outputTexture, outputPresentationTimestampUs, syncObject);
314320

315-
InputSource primaryInputSource = inputSources.get(PRIMARY_INPUT_ID);
321+
InputSource primaryInputSource = inputSources.get(PRIMARY_INPUT_INDEX);
316322
releaseFrames(primaryInputSource, /* numberOfFramesToRelease= */ 1);
317323
releaseExcessFramesInAllSecondaryStreams();
318324

@@ -332,18 +338,18 @@ private synchronized ImmutableList getFramesToComposite() {
332338
if (outputTexturePool.freeTextureCount() == 0) {
333339
return ImmutableList.of();
334340
}
335-
for (int inputId = 0; inputId < inputSources.size(); inputId++) {
336-
if (inputSources.get(inputSources.keyAt(inputId)).frameInfos.isEmpty()) {
341+
for (int i = 0; i < inputSources.size(); i++) {
342+
if (inputSources.valueAt(i).frameInfos.isEmpty()) {
337343
return ImmutableList.of();
338344
}
339345
}
340346
ImmutableList.Builder<InputFrameInfo> framesToComposite = new ImmutableList.Builder<>();
341347
InputFrameInfo primaryFrameToComposite =
342-
inputSources.get(PRIMARY_INPUT_ID).frameInfos.element();
348+
inputSources.get(PRIMARY_INPUT_INDEX).frameInfos.element();
343349
framesToComposite.add(primaryFrameToComposite);
344350

345-
for (int inputId = 0; inputId < inputSources.size(); inputId++) {
346-
if (inputId == PRIMARY_INPUT_ID) {
351+
for (int i = 0; i < inputSources.size(); i++) {
352+
if (i == PRIMARY_INPUT_INDEX) {
347353
continue;
348354
}
349355
// Select the secondary streams' frame that would be composited next. The frame selected is
@@ -352,7 +358,7 @@ private synchronized ImmutableList getFramesToComposite() {
352358
// 2. Two or more frames, and at least one frame has timestamp greater than the target
353359
// timestamp.
354360
// The smaller timestamp is taken if two timestamps have the same distance from the primary.
355-
InputSource secondaryInputSource = inputSources.get(inputSources.keyAt(inputId));
361+
InputSource secondaryInputSource = inputSources.valueAt(i);
356362
if (secondaryInputSource.frameInfos.size() == 1 && !secondaryInputSource.isInputEnded) {
357363
return ImmutableList.of();
358364
}

libraries/effect/src/main/java/androidx/media3/effect/MultipleInputVideoGraph.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import android.opengl.EGLDisplay;
3434
import android.opengl.EGLSurface;
3535
import android.util.SparseArray;
36+
import androidx.annotation.IntRange;
3637
import androidx.annotation.Nullable;
3738
import androidx.media3.common.C;
3839
import androidx.media3.common.ColorInfo;
@@ -211,9 +212,10 @@ public void onEnded() {
211212
}
212213

213214
@Override
214-
public void registerInput(int sequenceIndex) throws VideoFrameProcessingException {
215-
checkStateNotNull(videoCompositor);
216-
videoCompositor.registerInputSource(sequenceIndex);
215+
public void registerInput(@IntRange(from = 0) int inputIndex)
216+
throws VideoFrameProcessingException {
217+
checkState(!contains(preProcessors, inputIndex));
218+
checkNotNull(videoCompositor).registerInputSource(inputIndex);
217219
// Creating a new VideoFrameProcessor for the input.
218220
VideoFrameProcessor preProcessor =
219221
videoFrameProcessorFactory
@@ -222,7 +224,7 @@ public void registerInput(int sequenceIndex) throws VideoFrameProcessingExceptio
222224
// Texture output to compositor.
223225
(textureProducer, texture, presentationTimeUs, syncObject) ->
224226
queuePreProcessingOutputToCompositor(
225-
sequenceIndex, textureProducer, texture, presentationTimeUs),
227+
inputIndex, textureProducer, texture, presentationTimeUs),
226228
PRE_COMPOSITOR_TEXTURE_OUTPUT_CAPACITY)
227229
.build()
228230
.create(
@@ -253,16 +255,16 @@ public void onError(VideoFrameProcessingException exception) {
253255

254256
@Override
255257
public void onEnded() {
256-
onPreProcessingVideoFrameProcessorEnded(sequenceIndex);
258+
onPreProcessingVideoFrameProcessorEnded(inputIndex);
257259
}
258260
});
259-
preProcessors.put(sequenceIndex, preProcessor);
261+
preProcessors.put(inputIndex, preProcessor);
260262
}
261263

262264
@Override
263-
public VideoFrameProcessor getProcessor(int sequenceIndex) {
264-
checkState(contains(preProcessors, sequenceIndex));
265-
return preProcessors.get(sequenceIndex);
265+
public VideoFrameProcessor getProcessor(int inputIndex) {
266+
checkState(contains(preProcessors, inputIndex));
267+
return preProcessors.get(inputIndex);
266268
}
267269

268270
@Override

libraries/effect/src/main/java/androidx/media3/effect/PreviewingSingleInputVideoGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@ private PreviewingSingleInputVideoGraph(
109109

110110
@Override
111111
public void renderOutputFrame(long renderTimeNs) {
112-
getProcessor(SINGLE_INPUT_INDEX).renderOutputFrame(renderTimeNs);
112+
getProcessor(getInputIndex()).renderOutputFrame(renderTimeNs);
113113
}
114114
}

libraries/effect/src/main/java/androidx/media3/effect/SingleInputVideoGraph.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616

1717
package androidx.media3.effect;
1818

19+
import static androidx.media3.common.util.Assertions.checkArgument;
1920
import static androidx.media3.common.util.Assertions.checkState;
2021
import static androidx.media3.common.util.Assertions.checkStateNotNull;
2122

2223
import android.content.Context;
24+
import androidx.annotation.IntRange;
2325
import androidx.annotation.Nullable;
26+
import androidx.media3.common.C;
2427
import androidx.media3.common.ColorInfo;
2528
import androidx.media3.common.DebugViewProvider;
2629
import androidx.media3.common.Effect;
@@ -38,9 +41,6 @@
3841
@UnstableApi
3942
public abstract class SingleInputVideoGraph implements VideoGraph {
4043

41-
/** The index of the only {@linkplain #registerInput(int) registered} input. */
42-
public static final int SINGLE_INPUT_INDEX = 0;
43-
4444
private final Context context;
4545
private final VideoFrameProcessor.Factory videoFrameProcessorFactory;
4646
private final ColorInfo outputColorInfo;
@@ -56,6 +56,7 @@ public abstract class SingleInputVideoGraph implements VideoGraph {
5656
private boolean isEnded;
5757
private boolean released;
5858
private volatile boolean hasProducedFrameWithTimestampZero;
59+
private int inputIndex;
5960

6061
/**
6162
* Creates an instance.
@@ -86,6 +87,7 @@ public SingleInputVideoGraph(
8687
this.renderFramesAutomatically = renderFramesAutomatically;
8788
this.presentation = presentation;
8889
this.initialTimestampOffsetUs = initialTimestampOffsetUs;
90+
this.inputIndex = C.INDEX_UNSET;
8991
}
9092

9193
/**
@@ -99,9 +101,11 @@ public void initialize() {
99101
}
100102

101103
@Override
102-
public void registerInput(int sequenceIndex) throws VideoFrameProcessingException {
104+
public void registerInput(int inputIndex) throws VideoFrameProcessingException {
103105
checkStateNotNull(videoFrameProcessor == null && !released);
106+
checkState(this.inputIndex == C.INDEX_UNSET);
104107

108+
this.inputIndex = inputIndex;
105109
videoFrameProcessor =
106110
videoFrameProcessorFactory.create(
107111
context,
@@ -162,7 +166,8 @@ public void onEnded() {
162166
}
163167

164168
@Override
165-
public VideoFrameProcessor getProcessor(int sequenceIndex) {
169+
public VideoFrameProcessor getProcessor(int inputIndex) {
170+
checkArgument(this.inputIndex != C.INDEX_UNSET && this.inputIndex == inputIndex);
166171
return checkStateNotNull(videoFrameProcessor);
167172
}
168173

@@ -192,6 +197,10 @@ public void release() {
192197
released = true;
193198
}
194199

200+
protected int getInputIndex() {
201+
return inputIndex;
202+
}
203+
195204
protected long getInitialTimestampOffsetUs() {
196205
return initialTimestampOffsetUs;
197206
}

libraries/effect/src/main/java/androidx/media3/effect/VideoCompositor.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package androidx.media3.effect;
1717

18+
import androidx.annotation.IntRange;
1819
import androidx.media3.common.ColorInfo;
1920
import androidx.media3.common.GlTextureInfo;
2021
import androidx.media3.common.VideoFrameProcessingException;
@@ -48,29 +49,33 @@ interface Listener {
4849
/**
4950
* Registers a new input source.
5051
*
51-
* @param sequenceIndex The sequence index of the input source which is used to determine the
52-
* order of the input sources. The same index should to be used in {@link #queueInputTexture}.
52+
* @param inputIndex The index of the input source which could be used to determine the order of
53+
* the input sources. The same index should to be used in {@link #queueInputTexture}. The
54+
* index must start from 0. All inputs must be registered before
55+
* {@linkplain #queueInputTexture(int, GlTextureProducer, GlTextureInfo, ColorInfo, long) queueing}
56+
* textures.
5357
*/
54-
void registerInputSource(int sequenceIndex);
58+
void registerInputSource(@IntRange(from = 0) int inputIndex);
5559

5660
/**
5761
* Signals that no more frames will come from the upstream {@link GlTextureProducer.Listener}.
5862
*
59-
* @param inputId The identifier for an input source, returned from {@link #registerInputSource}.
63+
* @param inputIndex The index of the input source.
6064
*/
61-
void signalEndOfInputSource(int inputId);
65+
void signalEndOfInputSource(int inputIndex);
6266

6367
/**
6468
* Queues an input texture to be composited.
6569
*
66-
* @param inputId The identifier for an input source, returned from {@link #registerInputSource}.
70+
* @param inputIndex The index of the input source, the same index used when {@linkplain
71+
* #registerInputSource(int) registering the input source}.
6772
* @param textureProducer The source from where the {@code inputTexture} is produced.
6873
* @param inputTexture The {@link GlTextureInfo} to composite.
6974
* @param colorInfo The {@link ColorInfo} of {@code inputTexture}.
7075
* @param presentationTimeUs The presentation time of {@code inputTexture}, in microseconds.
7176
*/
7277
void queueInputTexture(
73-
int inputId,
78+
int inputIndex,
7479
GlTextureProducer textureProducer,
7580
GlTextureInfo inputTexture,
7681
ColorInfo colorInfo,

0 commit comments

Comments
 (0)