package com.mbridge.msdk.playercommon.exoplayer2; import android.os.Handler; import android.os.SystemClock; import androidx.annotation.Nullable; import com.mbridge.msdk.playercommon.exoplayer2.util.Assertions; import java.util.concurrent.TimeoutException; /* loaded from: classes4.dex */ public final class PlayerMessage { private Handler handler; private boolean isCanceled; private boolean isDelivered; private boolean isProcessed; private boolean isSent; private Object payload; private final Sender sender; private final Target target; private final Timeline timeline; private int type; private int windowIndex; private long positionMs = C.TIME_UNSET; private boolean deleteAfterDelivery = true; public interface Sender { void sendMessage(PlayerMessage playerMessage); } public interface Target { void handleMessage(int i, Object obj) throws ExoPlaybackException; } public final boolean getDeleteAfterDelivery() { return this.deleteAfterDelivery; } public final Handler getHandler() { return this.handler; } public final Object getPayload() { return this.payload; } public final long getPositionMs() { return this.positionMs; } public final Target getTarget() { return this.target; } public final Timeline getTimeline() { return this.timeline; } public final int getType() { return this.type; } public final int getWindowIndex() { return this.windowIndex; } public PlayerMessage(Sender sender, Target target, Timeline timeline, int i, Handler handler) { this.sender = sender; this.target = target; this.timeline = timeline; this.handler = handler; this.windowIndex = i; } public final PlayerMessage setType(int i) { Assertions.checkState(!this.isSent); this.type = i; return this; } public final PlayerMessage setPayload(@Nullable Object obj) { Assertions.checkState(!this.isSent); this.payload = obj; return this; } public final PlayerMessage setHandler(Handler handler) { Assertions.checkState(!this.isSent); this.handler = handler; return this; } public final PlayerMessage setPosition(long j) { Assertions.checkState(!this.isSent); this.positionMs = j; return this; } public final PlayerMessage setPosition(int i, long j) { Assertions.checkState(!this.isSent); Assertions.checkArgument(j != C.TIME_UNSET); if (i < 0 || (!this.timeline.isEmpty() && i >= this.timeline.getWindowCount())) { throw new IllegalSeekPositionException(this.timeline, i, j); } this.windowIndex = i; this.positionMs = j; return this; } public final PlayerMessage setDeleteAfterDelivery(boolean z) { Assertions.checkState(!this.isSent); this.deleteAfterDelivery = z; return this; } public final PlayerMessage send() { Assertions.checkState(!this.isSent); if (this.positionMs == C.TIME_UNSET) { Assertions.checkArgument(this.deleteAfterDelivery); } this.isSent = true; this.sender.sendMessage(this); return this; } public final synchronized PlayerMessage cancel() { Assertions.checkState(this.isSent); this.isCanceled = true; markAsProcessed(false); return this; } public final synchronized boolean isCanceled() { return this.isCanceled; } public final synchronized boolean blockUntilDelivered() throws InterruptedException, TimeoutException { boolean z; try { Assertions.checkState(this.isSent); Assertions.checkState(this.handler.getLooper().getThread() != Thread.currentThread()); long j = 500; long elapsedRealtime = SystemClock.elapsedRealtime() + 500; while (true) { z = this.isProcessed; if (z || j <= 0) { break; } wait(j); j = elapsedRealtime - SystemClock.elapsedRealtime(); } if (!z) { throw new TimeoutException("Message delivery time out"); } } catch (Throwable th) { throw th; } return this.isDelivered; } public final synchronized void markAsProcessed(boolean z) { this.isDelivered = z | this.isDelivered; this.isProcessed = true; notifyAll(); } }