package com.dowse.camera.client.video;

import com.dowse.camera.client.data.DSQueue;
import com.dowse.camera.client.data.DSVideoFrame;
import com.dowse.camera.client.listener.IRecordListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/* loaded from: ds_camera_client-2.0.6-23030810.aar:classes.jar:com/dowse/camera/client/video/DSRecordThread.class */
public class DSRecordThread extends Thread {
    private DSQueue dsQueue;
    private boolean isFirstIn;
    private int seconds;
    private FileOutputStream fos;
    private IRecordListener mIRecordListener;
    private final String TAG = DSRecordThread.class.getCanonicalName();
    private boolean isStop = false;
    private long firstTimeStamp = 0;

    public DSRecordThread(DSQueue dsQueue, String fileName, int seconds, IRecordListener mIRecordListener) {
        this.dsQueue = null;
        this.isFirstIn = false;
        this.seconds = 0;
        this.mIRecordListener = null;
        this.dsQueue = dsQueue;
        this.mIRecordListener = mIRecordListener;
        this.seconds = seconds;
        initRecordFile(fileName);
        this.isFirstIn = true;
    }

    private void initRecordFile(String fileName) {
        try {
            this.fos = new FileOutputStream(new File(fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override // java.lang.Thread, java.lang.Runnable
    public void run() {
        Object object;
        super.run();
        FileChannel fileChannel = this.fos.getChannel();
        while (!this.isStop) {
            if (this.dsQueue != null && (object = this.dsQueue.get()) != null && fileChannel != null) {
                try {
                    DSVideoFrame frame = (DSVideoFrame) object;
                    frame.getData();
                    if (this.isFirstIn && frame.getIs_keyframe() == 1) {
                        this.isFirstIn = false;
                        fileChannel.write(ByteBuffer.wrap(frame.getData()));
                        fileChannel.force(true);
                        this.firstTimeStamp = frame.getTimeStamp();
                        if (this.mIRecordListener != null) {
                            this.mIRecordListener.onStart(frame.getTimeStamp());
                        }
                    }
                    if (!this.isFirstIn) {
                        if (frame.getTimeStamp() - this.firstTimeStamp > this.seconds - 1) {
                            this.isStop = true;
                            if (this.mIRecordListener != null) {
                                this.mIRecordListener.onStop(frame.getTimeStamp());
                            }
                            return;
                        }
                        fileChannel.write(ByteBuffer.wrap(frame.getData()));
                        fileChannel.force(true);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }

    public void stopThread() {
        try {
            this.isStop = true;
            this.dsQueue.clear();
            this.fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
