From e41b23d2f4b576a64ce9c7e750858c46511d78af Mon Sep 17 00:00:00 2001 From: samrobinson Date: Tue, 16 Jan 2024 08:40:37 -0800 Subject: [PATCH] Add thread-safe information & annotations to necessary fields. This change is a small incremental step towards better thread-safety. PiperOrigin-RevId: 598852158 --- .../transformer/SequenceAssetLoader.java | 4 +++- .../transformer/TransformerInternal.java | 24 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/libraries/transformer/src/main/java/androidx/media3/transformer/SequenceAssetLoader.java b/libraries/transformer/src/main/java/androidx/media3/transformer/SequenceAssetLoader.java index 239540828d..1ca602f4c1 100644 --- a/libraries/transformer/src/main/java/androidx/media3/transformer/SequenceAssetLoader.java +++ b/libraries/transformer/src/main/java/androidx/media3/transformer/SequenceAssetLoader.java @@ -91,7 +91,9 @@ import java.util.concurrent.atomic.AtomicInteger; private boolean decodeVideo; private int sequenceLoopCount; private int processedInputsSize; - private boolean released; + + // Accessed when switching asset loader. + private volatile boolean released; private volatile long currentAssetDurationUs; private volatile long maxSequenceDurationUs; diff --git a/libraries/transformer/src/main/java/androidx/media3/transformer/TransformerInternal.java b/libraries/transformer/src/main/java/androidx/media3/transformer/TransformerInternal.java index a1b9598a7c..69f566a56f 100644 --- a/libraries/transformer/src/main/java/androidx/media3/transformer/TransformerInternal.java +++ b/libraries/transformer/src/main/java/androidx/media3/transformer/TransformerInternal.java @@ -128,16 +128,34 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; private final AssetLoaderInputTracker assetLoaderInputTracker; private final List sampleExporters; - private final Object setMaxSequenceDurationUsLock; private final MuxerWrapper muxerWrapper; private final ConditionVariable transformerConditionVariable; + private final Object setMaxSequenceDurationUsLock; private boolean isDrainingExporters; + + @GuardedBy("setMaxSequenceDurationUsLock") private long currentMaxSequenceDurationUs; + + @GuardedBy("setMaxSequenceDurationUsLock") private int nonLoopingSequencesWithNonFinalDuration; - private @Transformer.ProgressState int progressState; + private @MonotonicNonNull RuntimeException cancelException; + /** + * The current {@link Transformer.ProgressState}. + * + *

Modified on the internal thread. Accessed on the application thread (in {@link + * #getProgress}). + */ + private volatile @Transformer.ProgressState int progressState; + + /** + * The boolean tracking if this component has been released. + * + *

Modified on the internal thread. Accessed on the application thread (in {@link #getProgress} + * and {@link #cancel()}). + */ private volatile boolean released; public TransformerInternal( @@ -196,8 +214,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; } compositionHasLoopingSequence = nonLoopingSequencesWithNonFinalDuration != composition.sequences.size(); - sampleExporters = new ArrayList<>(); setMaxSequenceDurationUsLock = new Object(); + sampleExporters = new ArrayList<>(); transformerConditionVariable = new ConditionVariable(); // It's safe to use "this" because we don't send a message before exiting the constructor. @SuppressWarnings("nullness:methodref.receiver.bound")