package com.tencent.mm.plugin.voip.model;

import android.os.Process;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.plugin.voip.a.a;
import com.tencent.mm.sdk.platformtools.ab;
import org.xwalk.core.XWalkFeature;

class c$1 implements Runnable {
    final /* synthetic */ c sNS;

    c$1(c cVar) {
        this.sNS = cVar;
    }

    public final void run() {
        AppMethodBeat.i(4321);
        Process.setThreadPriority(-19);
        ab.d("MicroMsg.Voip.AudioPlayer", "AudioPlayer enter  to start....");
        int i = 0;
        while (this.sNS.sNi && this.sNS.aQf != null) {
            int playbackHeadPosition;
            int m;
            try {
                playbackHeadPosition = this.sNS.aQf.getPlaybackHeadPosition();
            } catch (Exception e) {
                ab.w("MicroMsg.Voip.AudioPlayer", "getPlaybackHeadPosition: ", new Object[]{e});
                c.i(this.sNS);
                playbackHeadPosition = 0;
            }
            long currentTimeMillis = System.currentTimeMillis();
            c.d(this.sNS, c.j(this.sNS) - playbackHeadPosition);
            if (c.k(this.sNS) == 1) {
                c.e(this.sNS, playbackHeadPosition);
                c.a(this.sNS, currentTimeMillis);
                c.b(this.sNS, currentTimeMillis);
                c.l(this.sNS);
                c.c(this.sNS, currentTimeMillis);
            } else {
                m = playbackHeadPosition - c.m(this.sNS);
                if (m > c.n(this.sNS)) {
                    c.f(this.sNS, m);
                } else {
                    c.f(this.sNS, (int) (((((float) c.n(this.sNS)) * 49999.0f) / 50000.0f) + (((float) m) / 50000.0f)));
                }
                c.c(this.sNS, currentTimeMillis);
                if (c.o(this.sNS) == 0) {
                    c.g(this.sNS, c.p(this.sNS) + c.q(this.sNS));
                } else if (currentTimeMillis > c.r(this.sNS) + 5000) {
                    c.b(this.sNS, currentTimeMillis);
                    if (c.n(this.sNS) < (c.p(this.sNS) >> 1)) {
                        c.g(this.sNS, c.p(this.sNS) - (c.q(this.sNS) >> 2));
                    }
                    if (c.n(this.sNS) > c.p(this.sNS)) {
                        c.g(this.sNS, c.n(this.sNS));
                    }
                }
                if (c.p(this.sNS) < c.s(this.sNS)) {
                    c.g(this.sNS, c.s(this.sNS));
                }
                if (c.p(this.sNS) < c.q(this.sNS)) {
                    c.g(this.sNS, c.q(this.sNS));
                }
                if (m > 0) {
                    c.e(this.sNS, playbackHeadPosition);
                }
            }
            if (playbackHeadPosition != 0 && this.sNS.sNk) {
                c.t(this.sNS);
            }
            if (this.sNS.sNt != null) {
                c.h(this.sNS, c.u(this.sNS) + this.sNS.fZP);
                if (c.a(this.sNS) == 0) {
                    playbackHeadPosition = this.sNS.sNt.N(c.v(this.sNS), this.sNS.fZP);
                } else {
                    System.currentTimeMillis();
                    if (c.b(this.sNS) >= this.sNS.fZP) {
                        synchronized (c.f(this.sNS)) {
                            try {
                                System.arraycopy(c.g(this.sNS), 0, c.v(this.sNS), 0, this.sNS.fZP);
                                m = c.b(this.sNS) - this.sNS.fZP;
                                System.arraycopy(c.g(this.sNS), this.sNS.fZP, c.w(this.sNS), 0, m);
                                System.arraycopy(c.w(this.sNS), 0, c.g(this.sNS), 0, m);
                                c.b(this.sNS, c.b(this.sNS) - this.sNS.fZP);
                            } catch (Throwable th) {
                                AppMethodBeat.o(4321);
                            }
                        }
                        playbackHeadPosition = 0;
                    } else if (c.o(this.sNS) >= (c.x(this.sNS) * 5) / XWalkFeature.INTERFACE_GEO_LOCATION_PERMISSION || c.y(this.sNS) != 0) {
                        a.GG(2);
                    } else {
                        c.i(this.sNS, 1);
                        playbackHeadPosition = this.sNS.sNt.N(c.v(this.sNS), this.sNS.fZP);
                        c.i(this.sNS, 0);
                    }
                }
                if (playbackHeadPosition < 0) {
                    a.GG(5);
                    ab.d("MicroMsg.Voip.AudioPlayer", "AudioPlayer::  pDevCallBack.PlayDevDataCallBack ret :".concat(String.valueOf(playbackHeadPosition)));
                } else if (c.z(this.sNS)) {
                    ab.d("MicroMsg.Voip.AudioPlayer", "isSwitching " + c.z(this.sNS));
                    a.GG(10);
                } else {
                    c.a(this.sNS, currentTimeMillis);
                    if (c.A(this.sNS) < this.sNS.fZP) {
                        System.arraycopy(c.v(this.sNS), 0, c.B(this.sNS), i, c.A(this.sNS) - i);
                        playbackHeadPosition = this.sNS.aQf.write(c.B(this.sNS), 0, c.B(this.sNS).length);
                        if (playbackHeadPosition < 0) {
                            c.j(this.sNS, playbackHeadPosition);
                            ab.e("MicroMsg.Voip.AudioPlayer", "AudioPlayer::  audioTrack.write ret :".concat(String.valueOf(playbackHeadPosition)));
                        }
                        c.k(this.sNS, c.j(this.sNS) + (c.B(this.sNS).length >> 1));
                        i = c.A(this.sNS) - i;
                        playbackHeadPosition = this.sNS.fZP - i;
                        while (playbackHeadPosition >= c.A(this.sNS)) {
                            this.sNS.aQf.write(c.v(this.sNS), i, c.A(this.sNS));
                            i += c.A(this.sNS);
                            playbackHeadPosition -= c.A(this.sNS);
                            c.k(this.sNS, c.j(this.sNS) + (c.A(this.sNS) >> 1));
                        }
                        System.arraycopy(c.v(this.sNS), i, c.B(this.sNS), 0, playbackHeadPosition);
                        i = playbackHeadPosition;
                    } else if (!c.z(this.sNS)) {
                        playbackHeadPosition = this.sNS.aQf.write(c.v(this.sNS), 0, this.sNS.fZP);
                        if (playbackHeadPosition < 0) {
                            c.j(this.sNS, playbackHeadPosition);
                            ab.e("MicroMsg.Voip.AudioPlayer", "AudioPlayer::  audioTrack.write ret :".concat(String.valueOf(playbackHeadPosition)));
                        }
                        c.k(this.sNS, c.j(this.sNS) + (this.sNS.fZP >> 1));
                    }
                }
            } else {
                a.GG(10);
            }
        }
        AppMethodBeat.o(4321);
    }
}
