18
18
import static androidx .media3 .common .util .Assertions .checkNotNull ;
19
19
import static androidx .media3 .common .util .Assertions .checkState ;
20
20
import static androidx .media3 .common .util .Assertions .checkStateNotNull ;
21
+ import static androidx .media3 .common .util .Util .contains ;
21
22
import static java .lang .Math .abs ;
22
23
import static java .lang .Math .max ;
23
24
@@ -78,7 +79,8 @@ public final class DefaultVideoCompositor implements VideoCompositor {
78
79
79
80
private static final String THREAD_NAME = "Effect:DefaultVideoCompositor:GlThread" ;
80
81
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 ;
82
84
83
85
private final VideoCompositor .Listener listener ;
84
86
private final GlTextureProducer .Listener textureOutputListener ;
@@ -142,32 +144,35 @@ public DefaultVideoCompositor(
142
144
}
143
145
144
146
@ 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 ());
147
150
}
148
151
149
152
@ 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 ;
152
156
boolean allInputsEnded = true ;
153
157
for (int i = 0 ; i < inputSources .size (); i ++) {
154
- if (!inputSources .get ( inputSources . keyAt ( i ) ).isInputEnded ) {
158
+ if (!inputSources .valueAt ( i ).isInputEnded ) {
155
159
allInputsEnded = false ;
156
160
break ;
157
161
}
158
162
}
159
163
160
164
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 ) {
163
167
releaseExcessFramesInAllSecondaryStreams ();
164
168
}
165
169
if (allInputsEnded ) {
166
170
listener .onEnded ();
167
171
return ;
168
172
}
169
173
}
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 ) {
171
176
// When a secondary stream ends input, composite if there was only one pending frame in the
172
177
// stream.
173
178
videoFrameProcessingTaskExecutor .submit (this ::maybeComposite );
@@ -176,12 +181,13 @@ public synchronized void signalEndOfInputSource(int inputId) {
176
181
177
182
@ Override
178
183
public synchronized void queueInputTexture (
179
- int inputId ,
184
+ int inputIndex ,
180
185
GlTextureProducer textureProducer ,
181
186
GlTextureInfo inputTexture ,
182
187
ColorInfo colorInfo ,
183
188
long presentationTimeUs ) {
184
- InputSource inputSource = inputSources .get (inputId );
189
+ checkState (contains (inputSources , inputIndex ));
190
+ InputSource inputSource = inputSources .get (inputIndex );
185
191
checkState (!inputSource .isInputEnded );
186
192
checkStateNotNull (!ColorInfo .isTransferHdr (colorInfo ), "HDR input is not supported." );
187
193
if (configuredColorInfo == null ) {
@@ -195,10 +201,10 @@ public synchronized void queueInputTexture(
195
201
textureProducer ,
196
202
inputTexture ,
197
203
presentationTimeUs ,
198
- settings .getOverlaySettings (inputId , presentationTimeUs ));
204
+ settings .getOverlaySettings (inputIndex , presentationTimeUs ));
199
205
inputSource .frameInfos .add (inputFrameInfo );
200
206
201
- if (inputId == PRIMARY_INPUT_ID ) {
207
+ if (inputIndex == PRIMARY_INPUT_INDEX ) {
202
208
releaseExcessFramesInAllSecondaryStreams ();
203
209
} else {
204
210
releaseExcessFramesInSecondaryStream (inputSource );
@@ -224,11 +230,11 @@ public void releaseOutputTexture(long presentationTimeUs) {
224
230
}
225
231
226
232
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 ) {
229
235
continue ;
230
236
}
231
- releaseExcessFramesInSecondaryStream (inputSources .get ( inputSources . keyAt ( i ) ));
237
+ releaseExcessFramesInSecondaryStream (inputSources .valueAt ( inputIndex ));
232
238
}
233
239
}
234
240
@@ -240,7 +246,7 @@ private synchronized void releaseExcessFramesInAllSecondaryStreams() {
240
246
* began.
241
247
*/
242
248
private synchronized void releaseExcessFramesInSecondaryStream (InputSource secondaryInputSource ) {
243
- InputSource primaryInputSource = inputSources .get (PRIMARY_INPUT_ID );
249
+ InputSource primaryInputSource = inputSources .get (PRIMARY_INPUT_INDEX );
244
250
// If the primary stream output is ended, all secondary frames can be released.
245
251
if (primaryInputSource .frameInfos .isEmpty () && primaryInputSource .isInputEnded ) {
246
252
releaseFrames (
@@ -291,7 +297,7 @@ private synchronized void maybeComposite()
291
297
return ;
292
298
}
293
299
294
- InputFrameInfo primaryInputFrame = framesToComposite .get (PRIMARY_INPUT_ID );
300
+ InputFrameInfo primaryInputFrame = framesToComposite .get (PRIMARY_INPUT_INDEX );
295
301
296
302
ImmutableList .Builder <Size > inputSizes = new ImmutableList .Builder <>();
297
303
for (int i = 0 ; i < framesToComposite .size (); i ++) {
@@ -312,7 +318,7 @@ private synchronized void maybeComposite()
312
318
textureOutputListener .onTextureRendered (
313
319
/* textureProducer= */ this , outputTexture , outputPresentationTimestampUs , syncObject );
314
320
315
- InputSource primaryInputSource = inputSources .get (PRIMARY_INPUT_ID );
321
+ InputSource primaryInputSource = inputSources .get (PRIMARY_INPUT_INDEX );
316
322
releaseFrames (primaryInputSource , /* numberOfFramesToRelease= */ 1 );
317
323
releaseExcessFramesInAllSecondaryStreams ();
318
324
@@ -332,18 +338,18 @@ private synchronized ImmutableList getFramesToComposite() {
332
338
if (outputTexturePool .freeTextureCount () == 0 ) {
333
339
return ImmutableList .of ();
334
340
}
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 ()) {
337
343
return ImmutableList .of ();
338
344
}
339
345
}
340
346
ImmutableList .Builder <InputFrameInfo > framesToComposite = new ImmutableList .Builder <>();
341
347
InputFrameInfo primaryFrameToComposite =
342
- inputSources .get (PRIMARY_INPUT_ID ).frameInfos .element ();
348
+ inputSources .get (PRIMARY_INPUT_INDEX ).frameInfos .element ();
343
349
framesToComposite .add (primaryFrameToComposite );
344
350
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 ) {
347
353
continue ;
348
354
}
349
355
// Select the secondary streams' frame that would be composited next. The frame selected is
@@ -352,7 +358,7 @@ private synchronized ImmutableList getFramesToComposite() {
352
358
// 2. Two or more frames, and at least one frame has timestamp greater than the target
353
359
// timestamp.
354
360
// 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 );
356
362
if (secondaryInputSource .frameInfos .size () == 1 && !secondaryInputSource .isInputEnded ) {
357
363
return ImmutableList .of ();
358
364
}
0 commit comments