//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.dowse.camera.client.data;

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import com.coremedia.iso.boxes.Container;
import com.dowse.base.log.DSLog;
import com.dowse.camera.client.listener.IDSMp4Listener;
import com.dowse.camera.client.listener.IDSVideoStopListener;
import com.dowse.camera.client.listener.IRecordListener;
import com.dowse.camera.client.video.DSRecordThread;
import com.googlecode.mp4parser.FileDataSourceImpl;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.tracks.h264.H264TrackImpl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class DSMP4Video {
    private DSQueue mQueue;
    private static final String TAG = "DSMP4Video";
    private IDSVideoStopListener mIDSVideoStopListener;
    private IDSMp4Listener listener;
    private Context mContext;
    private int channel;
    private int sencods;
    private String path;
    private String h264Path;
    private int videoHandle = -1;
    private DSRecordThread mDSRecordThread;
    private static final String H264_PATH = "/sdcard/dowse/mp4_h264";
    private Handler mHandler;
    private IRecordListener mIRecordListener = new IRecordListener() {
        public void onStart(long timeStamp) {
            DSLog.d("DSMP4Video", "start record mp4,start time = " + timeStamp);
        }

        public void onStop(long timeStamp) {
            DSLog.d("DSMP4Video", "stop record time mp4,end time = " + timeStamp);
            DSMP4Video.this.stopWork();
        }
    };

    public DSMP4Video(Context mContext, int channel, int seconds, String path, IDSMp4Listener listener, IDSVideoStopListener mIDSVideoStopListener) {
        this.mContext = mContext;
        this.channel = channel;
        this.sencods = seconds;
        this.path = path;
        this.listener = listener;
        this.mIDSVideoStopListener = mIDSVideoStopListener;
        this.mQueue = new DSQueue();
        HandlerThread mHandlerThread = new HandlerThread("DSMP4Video");
        mHandlerThread.start();
        this.mHandler = new Handler(mHandlerThread.getLooper());
        this.initDir();
    }

    private void initDir() {
        File file = new File("/sdcard/dowse/mp4_h264");
        if (!file.exists()) {
            file.mkdirs();
        }

    }

    public void setVideoHandle(int videoHandle) {
        this.videoHandle = videoHandle;
        this.doWork();
    }

    public void addData(DSVideoFrame videoFrame) {
        this.mQueue.add(videoFrame);
    }

    public void doWork() {
        this.h264Path = "/sdcard/dowse/mp4_h264" + File.separator + "_" + this.channel + "_" + this.sencods + "_" + System.currentTimeMillis() + "_.h264";
        if (this.mDSRecordThread == null) {
            this.mDSRecordThread = new DSRecordThread(this.mQueue, this.h264Path, this.sencods, this.mIRecordListener);
        }

        this.mDSRecordThread.start();
    }

    public void stopWork() {
        this.mDSRecordThread.stopThread();
        if (this.mIDSVideoStopListener != null) {
            this.mIDSVideoStopListener.onStopVideo(this.channel, this.videoHandle);
        }

        this.onChangeH264ToMp4();
    }

    private void onChangeH264ToMp4() {
        FileOutputStream fos = null;
        File h264File = null;
        boolean result = false;

        label109: {
            try {
                Log.d("DSMP4Video", "fileVideoPath:" + this.h264Path);
                h264File = new File(this.h264Path);
                if (h264File.exists()) {
                    H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(h264File));
                    Movie m = new Movie();
                    m.addTrack(h264Track);
                    Container out = (new DefaultMp4Builder()).build(m);
                    fos = new FileOutputStream(this.path);
                    FileChannel fc = fos.getChannel();
                    out.writeContainer(fc);
                    result = true;
                    break label109;
                }

                if (this.listener != null) {
                    this.listener.onResult(this.path, false);
                }
            } catch (Exception var17) {
                var17.printStackTrace();
                result = false;
                break label109;
            } finally {
                try {
                    if (null != fos) {
                        fos.close();
                    }
                } catch (IOException var16) {
                    var16.printStackTrace();
                }

            }

            return;
        }

        h264File.delete();
        if (this.listener != null) {
            this.listener.onResult(this.path, result);
        }

    }
}
