package R0;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.Choreographer;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class x implements Choreographer.FrameCallback, Handler.Callback {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final x f5998u = new x();

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public volatile long f5999q = -9223372036854775807L;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final Handler f6000r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public Choreographer f6001s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public int f6002t;

    public x() {
        HandlerThread handlerThread = new HandlerThread("ExoPlayer:FrameReleaseChoreographer");
        handlerThread.start();
        Looper looper = handlerThread.getLooper();
        int i = x0.v.f18553a;
        Handler handler = new Handler(looper, this);
        this.f6000r = handler;
        handler.sendEmptyMessage(1);
    }

    @Override // android.view.Choreographer.FrameCallback
    public final void doFrame(long j8) {
        this.f5999q = j8;
        Choreographer choreographer = this.f6001s;
        choreographer.getClass();
        choreographer.postFrameCallbackDelayed(this, 500L);
    }

    @Override // android.os.Handler.Callback
    public final boolean handleMessage(Message message) {
        int i = message.what;
        if (i == 1) {
            try {
                this.f6001s = Choreographer.getInstance();
            } catch (RuntimeException e8) {
                AbstractC1709a.C("VideoFrameReleaseHelper", "Vsync sampling disabled due to platform error", e8);
            }
            return true;
        }
        if (i == 2) {
            Choreographer choreographer = this.f6001s;
            if (choreographer != null) {
                int i5 = this.f6002t + 1;
                this.f6002t = i5;
                if (i5 == 1) {
                    choreographer.postFrameCallback(this);
                }
            }
            return true;
        }
        if (i != 3) {
            return false;
        }
        Choreographer choreographer2 = this.f6001s;
        if (choreographer2 != null) {
            int i8 = this.f6002t - 1;
            this.f6002t = i8;
            if (i8 == 0) {
                choreographer2.removeFrameCallback(this);
                this.f5999q = -9223372036854775807L;
            }
        }
        return true;
    }
}
