package com.eitchsyh.instrument;

import android.view.Surface;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import io.flutter.Log;

public class Video
{
    static
    {
        System.loadLibrary("iMVR");
    }

    private native int InitDecoder(int w, int h);

    private native int UninitDecoder();

    private native int DecoderNal(byte[] in, int insize, byte[] out);

    private native int SleepMS(int ms);

    private native int GetJPGFileData(byte[] in, byte[] data, byte[] name);

    private native int DeleteAllJPG();

    private native int GetVersion(byte[] out);

    private native int JPGStartDecoder(Surface surface);

    //返回0 关闭成功，其他失败
    private native int JPGStopDecoder();

    private native int JPGKeyPress(String jpgpath, int keypress);

    private native int JPGGetJPGVideo(byte[] data);

    private native int JPGStartRecord(String avifilename);

    //返回0   停止成功
    private native int JPGStopRecord();

    private final static String TAG = "VideoNative";

    /**
     * 解码线程
     */
    private Thread decoderThread;
    /**
     * 是否正在解码视频
     */
    private AtomicBoolean isDecodingVideo = new AtomicBoolean(false);
    /**
     * 等待开始视频的surface
     */
    private AtomicReference<Surface> pendingStartVideoSurface = new AtomicReference<>(null);

    /**
     * 获取连接状态
     *
     * @return
     */
    public int getVersion()
    {
        return GetVersion(new byte[24]);
    }

    /**
     * 开始视频
     *
     * @param surface
     */
    public void startVideo(Surface surface)
    {
        //是否正在解码视频,如果正在解码,记录要开始视频的surface
        //这种情况通常出现在stopVideo后又startVideo的情况
        //stopVideo是异步的,不能保证stopVideo后底层就会stop掉
        //这里做判断用于防止重复startVideo导致崩溃的问题
        if (isDecodingVideo.get())
        {
            pendingStartVideoSurface.set(surface);
            Log.d(TAG, "开始视频,但是正在解码中,记录surface");
            return;
        }
        Log.d(TAG, "开始视频");
        decoderThread = new Thread(() -> {
            isDecodingVideo.set(true);
            Log.d(TAG, "开始视频解码");
            //会阻塞线程
            JPGStartDecoder(surface);
            Log.d(TAG, "结束视频解码");
            isDecodingVideo.set(false);
            decoderThread = null;
            Surface pendingSurface = pendingStartVideoSurface.get();
            pendingStartVideoSurface.set(null);
            if (pendingSurface != null)
            {
                Log.d(TAG, "视频解码结束,有等待开始视频的surface,重新开始视频");
                startVideo(pendingSurface);
            }
        }, "视频渲染线程");
        decoderThread.start();
    }

    /**
     * 停止视频
     */
    public void stopVideo()
    {
        //清空要开始视频的surface
        pendingStartVideoSurface.set(null);
        if (!isDecodingVideo.get())
        {
            return;
        }
        Log.d(TAG, "停止视频");
        JPGStopDecoder();
    }

    /**
     * 开始录像
     *
     * @param avifilename 视频地址,.avi后缀
     * @return
     */
    public int startRecord(String avifilename)
    {
        return JPGStartRecord(avifilename);
    }

    /**
     * 停止录像
     *
     * @return
     */
    public int stopRecord()
    {
        return JPGStopRecord();
    }
}
