package com.dowse.camera.client;

import android.content.Context;
import android.text.TextUtils;
// import com.dowse.base.audio.DSAudioFrame;
import com.dowse.base.data.DS4GInfo;
import com.dowse.base.data.DSDeviceCustomInfo;
import com.dowse.base.data.DSLocation;
import com.dowse.base.data.DSTFInfo;
import com.dowse.base.data.DSWIFIAPInfo;
import com.dowse.base.data.DSWIFIInfo;
import com.dowse.base.data.StaticIPInfo;
import com.dowse.base.log.DSLog;
import com.dowse.base.param.NetType;
import com.dowse.base.param.maintenance.MaintenanceParam;
import com.dowse.base.param.pic.ChannelPicParam;
import com.dowse.base.param.video.ChannelOSD;
import com.dowse.base.param.video.ChannelVideoParam;
import com.dowse.base.util.ContextUtil;
import com.dowse.base.util.MD5Util;
import com.dowse.camera.client.data.DSCamera;
import com.dowse.camera.client.data.DSDeviceInfo;
import com.dowse.camera.client.data.DSMP4Video;
import com.dowse.camera.client.data.DSSupportPhotoSize;
import com.dowse.camera.client.data.DSVideoFrame;
import com.dowse.camera.client.jni.DSCameraClientJNI;
import com.dowse.camera.client.listener.DSCameraListener;
import com.dowse.camera.client.listener.IAudioDataListener;
import com.dowse.camera.client.listener.IDSMp4Listener;
import com.dowse.camera.client.listener.IDSVideoStopListener;
import com.dowse.camera.client.listener.IVideoDataListener;
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;
import java.util.HashMap;

/* loaded from: ds_camera_client-2.0.6-23030810.aar:classes.jar:com/dowse/camera/client/DSCameraManager.class */
public class DSCameraManager implements IDSVideoStopListener {
    private static final String TAG = "DSCameraManager";
    private Context mContext;
    private DSCameraClientJNI cameraJNI;
    private static String mServerIP;
    private boolean isInit = false;
    private int mHandle = -1;
    private int mVideoHandle = -1;
    private int mAudioHandle = -1;
    private HashMap<Integer, DSMP4Video> mDSMp4Maps = null;
    private IVideoDataListener mVideoListener = null;
    private IAudioDataListener mAudioListener = null;
    private DSCameraListener listener = new DSCameraListener() { // from class: com.dowse.camera.client.DSCameraManager.1
        @Override // com.dowse.camera.client.listener.DSCameraListener
        public void onDisconnect(int handle) {
            // DSLog.e(DSCameraManager.TAG, "DSCameraManager is disconnect!!!");
            DSCameraManager.this.mHandle = -1;
            DSCameraManager.this.mVideoHandle = -1;
            DSCameraManager.this.mAudioHandle = -1;
            if (DSCameraManager.this.mVideoListener != null) {
                DSCameraManager.this.mVideoListener.onVideoStop();
            }
            if (DSCameraManager.this.mAudioListener != null) {
                DSCameraManager.this.mAudioListener.onAudioStop();
            }
        }

        @Override // com.dowse.camera.client.listener.DSCameraListener
        public void onData(int handle, byte channel, byte stream_no, byte type, byte code, byte is_keyframe, long timeStamp, short width, short heigth, byte[] data) {
            if (type == 0) {
                DSVideoFrame videoFrame = new DSVideoFrame(channel, stream_no, width, heigth, code, is_keyframe, timeStamp, data);
                DSCameraManager.this.onMp4VideoProcess(channel, videoFrame);
                DSCameraManager.this.onVideoProcess(handle, videoFrame);
            }
            if (type == 1) {
                // DSAudioFrame audioFrame = new DSAudioFrame(data);
                // DSCameraManager.this.onAudioProcess(handle, audioFrame);
            }
        }
    };

    static {
        System.loadLibrary("camera_client");
        mServerIP = "127.0.0.1";
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void onMp4VideoProcess(int channel, DSVideoFrame videoFrame) {
        DSMP4Video mp4Video;
        if (this.mDSMp4Maps != null && (mp4Video = this.mDSMp4Maps.get(Integer.valueOf(channel))) != null) {
            mp4Video.addData(videoFrame);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void onVideoProcess(int handle, DSVideoFrame videoFrame) {
        if (this.mVideoHandle >= 0 && this.mHandle == handle && this.mVideoListener != null) {
            this.mVideoListener.onVideoData(videoFrame);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /*
    public void onAudioProcess(int handle, DSAudioFrame audioFrame) {
        if (this.mAudioHandle >= 0 && this.mHandle == handle && this.mAudioListener != null) {
            this.mAudioListener.onAudioData(audioFrame);
        }
    }

     */

    public DSCameraManager() {
        this.mContext = null;
        this.mContext = ContextUtil.getApplication();
        if (this.cameraJNI == null) {
            this.cameraJNI = new DSCameraClientJNI();
        }
    }

    public static DSCameraManager getInstace() {
        return DSCameraManagerHolder.instance;
    }

    /* loaded from: ds_camera_client-2.0.6-23030810.aar:classes.jar:com/dowse/camera/client/DSCameraManager$DSCameraManagerHolder.class */
    private static class DSCameraManagerHolder {
        public static final DSCameraManager instance = new DSCameraManager();

        private DSCameraManagerHolder() {
        }
    }

    /* JADX WARN: Type inference failed for: r0v0, types: [com.dowse.camera.client.DSCameraManager$2] */
    private void getConnectHandle() {
        new Thread() { // from class: com.dowse.camera.client.DSCameraManager.2
            @Override // java.lang.Thread, java.lang.Runnable
            public void run() {
                if (DSCameraManager.this.mHandle <= 0) {
                    DSCameraManager.this.mHandle = DSCameraManager.this.cameraJNI.connect(DSCameraManager.mServerIP, DSCameraManager.this.listener);
                }
                DSLog.e(DSCameraManager.TAG, "DSCameraManager getConnectHandle mHandle=" + DSCameraManager.this.mHandle);
            }
        }.start();
    }

    private boolean client_check() {
        if (!this.isInit) {
            DSLog.e(TAG, "DSCameraManager is not call init!!!");
            return false;
        } else if (this.mHandle > 0) {
            return true;
        } else {
            getConnectHandle();
            return false;
        }
    }

    private boolean check() {
        if (!this.isInit) {
            DSLog.e(TAG, "DSCameraManager is not call init!!!");
            return false;
        }
        if (this.mHandle < 0) {
            DSLog.d(TAG, "DSCameraManager not connect, reconnect");
            this.mHandle = this.cameraJNI.connect(mServerIP, this.listener);
        }
        if (this.mHandle > 0) {
            return true;
        }
        return false;
    }

    public boolean isCameraServiceOK() {
        boolean result = client_check();
        DSLog.d(TAG, "DSCameraManager isCameraServiceOK = " + result);
        return result;
    }

    private boolean writeFile(String path, byte[] bytes) {
        try {
            FileOutputStream out = new FileOutputStream(path);
            FileChannel fileChannel = out.getChannel();
            fileChannel.write(ByteBuffer.wrap(bytes));
            fileChannel.force(true);
            fileChannel.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean setServerIP(String serverIP) {
        mServerIP = serverIP;
        return check();
    }

    public boolean init() {
        if (!this.isInit) {
            String logDir = "/sdcard/apps/log/" + MD5Util.md5(this.mContext.getPackageName()) + File.separator;
            File file = new File(logDir);
            if (!file.isDirectory() || !file.exists()) {
                file.mkdirs();
            }
            String debugName = MD5Util.md5(this.mContext.getPackageName()) + "_native.log";
            String errorName = MD5Util.md5(this.mContext.getPackageName()) + "_native_fatal.log";
            DSLog.i(TAG, "Native Log Path=" + logDir);
            DSLog.i(TAG, "Native log file path = " + logDir + debugName);
            DSLog.i(TAG, "Native fatal log file path = " + logDir + errorName);
            this.cameraJNI.init(logDir, debugName, errorName);
            this.isInit = true;
            return true;
        }
        return true;
    }

    public boolean takePhoto(String path, int channel, String osd_time) {
        byte[] image;
        if (check() && (image = this.cameraJNI.takePhotoWithOSD(this.mHandle, channel, 0, 0, osd_time + " ")) != null && image.length > 0) {
            return writeFile(path, image);
        }
        return false;
    }

    public boolean takePhoto(String path, int channel) {
        byte[] image;
        if (check() && (image = this.cameraJNI.takePhoto(this.mHandle, channel, 0, 0)) != null && image.length > 0) {
            return writeFile(path, image);
        }
        return false;
    }

    public boolean takePhoto(String path, int channel, int width, int height) {
        byte[] image;
        if (check() && (image = this.cameraJNI.takePhoto(this.mHandle, channel, width, height)) != null && image.length > 0) {
            return writeFile(path, image);
        }
        return false;
    }

    public boolean takePhoto(String path, int channel, int width, int height, String osd_time) {
        byte[] image;
        if (check() && (image = this.cameraJNI.takePhotoWithOSD(this.mHandle, channel, width, height, osd_time + " ")) != null && image.length > 0) {
            return writeFile(path, image);
        }
        return false;
    }

    public boolean recordMp4(Context mContext, int channel, int stream_no, String path, int seconds, IDSMp4Listener listener) {
        if (check()) {
            if (this.mDSMp4Maps == null) {
                this.mDSMp4Maps = new HashMap<>();
            }
            if (this.mDSMp4Maps.get(Integer.valueOf(channel)) != null) {
                DSLog.d(TAG, "channel : " + channel + " is recording mp4");
                return false;
            }
            DSMP4Video dsmp4Video = new DSMP4Video(mContext, channel, seconds, path, listener, this);
            this.mDSMp4Maps.put(Integer.valueOf(channel), dsmp4Video);
            int videoHandle = this.cameraJNI.startVideoStreaming(this.mHandle, channel, stream_no);
            if (videoHandle < 0) {
                this.mDSMp4Maps.remove(Integer.valueOf(channel));
                return false;
            }
            dsmp4Video.setVideoHandle(videoHandle);
            return true;
        }
        return false;
    }

    public boolean startVideo(int channel, int stream_no, IVideoDataListener mVideoListener) {
        if (check()) {
            if (this.mVideoHandle < 0) {
                this.mVideoHandle = this.cameraJNI.startVideoStreaming(this.mHandle, channel, stream_no);
                if (this.mVideoHandle < 0) {
                    DSLog.d(TAG, "video start failed!");
                    return false;
                }
                this.mVideoListener = mVideoListener;
                return true;
            }
            DSLog.d(TAG, "video is streaming ...,please stop first!");
            return false;
        }
        return false;
    }

    public void stopVideo() {
        if (check() && this.mVideoHandle >= 0) {
            this.cameraJNI.stopVideoStreaming(this.mHandle, this.mVideoHandle);
        }
        this.mVideoHandle = -1;
        this.mVideoListener = null;
    }

    public boolean unInit() {
        if (this.isInit) {
            if (this.mHandle > 0) {
                this.cameraJNI.disconnect(this.mHandle);
            }
            this.cameraJNI.uninit();
            this.isInit = false;
            return true;
        }
        return true;
    }

    @Override // com.dowse.camera.client.listener.IDSVideoStopListener
    public void onStopVideo(int channel, int videoHandle) {
        if (check()) {
            this.cameraJNI.stopVideoStreaming(this.mHandle, videoHandle);
        }
        this.mDSMp4Maps.remove(Integer.valueOf(channel));
    }

    public DSCamera getDSCamera() {
        if (check()) {
            return this.cameraJNI.getDSCamera(this.mHandle);
        }
        return null;
    }

    public DSSupportPhotoSize getSupportPhotoSize(int channel) {
        if (check()) {
            return this.cameraJNI.getSupportPhotoSize(this.mHandle, channel);
        }
        return null;
    }

    public DSDeviceInfo getDeviceInfo() {
        if (check()) {
            return this.cameraJNI.getDeviceInfo(this.mHandle);
        }
        return null;
    }

    public StaticIPInfo getStaticIP() {
        if (check()) {
            return this.cameraJNI.getStaticIP(this.mHandle);
        }
        return null;
    }

    public boolean setStaticIP(String ip, String mask, String gw, String dns1, String dns2) {
        if (check()) {
            StaticIPInfo staticIPInfo = new StaticIPInfo();
            staticIPInfo.setIp(ip);
            staticIPInfo.setMask(mask);
            staticIPInfo.setGw(gw);
            staticIPInfo.setDns1(dns1);
            staticIPInfo.setDns2(dns2);
            return this.cameraJNI.setStaticIP(this.mHandle, staticIPInfo);
        }
        return false;
    }

    public DS4GInfo getDS4GInfo(NetType netType) {
        if (check()) {
            return this.cameraJNI.get4GInfo(this.mHandle, netType.getNet_type());
        }
        return null;
    }

    public String getMCUVersion() {
        if (check()) {
            return this.cameraJNI.getMCUVer(this.mHandle);
        }
        return null;
    }

    public int getSolarVolt() {
        if (check()) {
            return this.cameraJNI.getSolarVolt(this.mHandle);
        }
        return -1;
    }

    public int getBatVolt() {
        if (check()) {
            return this.cameraJNI.getBatVolt(this.mHandle);
        }
        return -1;
    }

    public int getBatSoc() {
        if (check()) {
            return this.cameraJNI.getBatSoc(this.mHandle);
        }
        return -1;
    }

    public int getBatTotal() {
        if (check()) {
            return this.cameraJNI.getBatTotal(this.mHandle);
        }
        return -1;
    }

    public int getWorkCurrent() {
        if (check()) {
            return this.cameraJNI.getWorkCurrent(this.mHandle);
        }
        return -1;
    }

    public int getChargeStatus() {
        if (check()) {
            return this.cameraJNI.getChargeStatus(this.mHandle);
        }
        return -1;
    }

    public String getNRSECVersion() {
        if (check()) {
            return this.cameraJNI.getNrsecVersion(this.mHandle);
        }
        return null;
    }

    public DSLocation getGPSLocation() {
        if (check()) {
            return this.cameraJNI.getGPSLocation(this.mHandle);
        }
        return null;
    }

    public boolean setGPSEnable(boolean enable) {
        if (check()) {
            return this.cameraJNI.setGPSStatus(this.mHandle, enable ? 1 : 0);
        }
        return false;
    }

    public DSTFInfo getTFInfo() {
        if (check()) {
            return this.cameraJNI.getDSTFInfo(this.mHandle);
        }
        return null;
    }

    public DSWIFIInfo getWiFiInfo() {
        if (check()) {
            return this.cameraJNI.getWiFiInfo(this.mHandle);
        }
        return null;
    }

    public boolean openWiFi(String ssid, String password) {
        if (check()) {
            if (TextUtils.isEmpty(ssid) || TextUtils.isEmpty(password)) {
                DSLog.e(TAG, "ssid is null or password is null!!!");
                return false;
            } else if (password.length() < 8) {
                DSLog.e(TAG, "password len is less than 8!!!");
                return false;
            } else {
                DSWIFIInfo wifiInfo = new DSWIFIInfo();
                wifiInfo.setSsid(ssid);
                wifiInfo.setPassword(password);
                wifiInfo.setEnable(1);
                return this.cameraJNI.setWiFiInfo(this.mHandle, wifiInfo);
            }
        }
        return false;
    }

    public boolean closeWiFi() {
        DSWIFIInfo wifiInfo;
        if (check() && (wifiInfo = getWiFiInfo()) != null) {
            wifiInfo.setEnable(0);
            return this.cameraJNI.setWiFiInfo(this.mHandle, wifiInfo);
        }
        return false;
    }

    public DSWIFIAPInfo getWiFiAPInfo() {
        if (check()) {
            return this.cameraJNI.getWiFiAPInfo(this.mHandle);
        }
        return null;
    }

    public boolean openWiFiAP(String ssid, String password) {
        if (check()) {
            if (TextUtils.isEmpty(ssid) || TextUtils.isEmpty(password)) {
                DSLog.e(TAG, "ssid is null or password is null!!!");
                return false;
            } else if (password.length() < 8) {
                DSLog.e(TAG, "password len is less than 8!!!");
                return false;
            } else {
                DSWIFIAPInfo dswifiapInfo = new DSWIFIAPInfo();
                dswifiapInfo.setSsid(ssid);
                dswifiapInfo.setPassword(password);
                dswifiapInfo.setEnable(1);
                return this.cameraJNI.setWiFiAPInfo(this.mHandle, dswifiapInfo);
            }
        }
        return false;
    }

    public boolean closeWiFiAP() {
        DSWIFIAPInfo wiFiAPInfo;
        if (check() && (wiFiAPInfo = getWiFiAPInfo()) != null) {
            wiFiAPInfo.setEnable(0);
            return this.cameraJNI.setWiFiAPInfo(this.mHandle, wiFiAPInfo);
        }
        return false;
    }

    public boolean startCapture() {
        boolean result = false;
        if (check()) {
            result = this.cameraJNI.startAudioCapture(this.mHandle);
        }
        return result;
    }

    public boolean stopCapture() {
        boolean result = false;
        if (check()) {
            result = this.cameraJNI.stopAudioCapture(this.mHandle);
        }
        return result;
    }

    public boolean startAudio(IAudioDataListener listener) {
        if (this.mAudioHandle < 0) {
            this.mAudioHandle = this.cameraJNI.startAudio(this.mHandle, 0);
            if (this.mAudioHandle < 0) {
                DSLog.d(TAG, "audio start failed!");
                return false;
            }
            this.mAudioListener = listener;
            return true;
        }
        DSLog.d(TAG, "audio is streaming ...,please stop first!");
        return false;
    }

    public void stopAudio() {
        if (check() && this.mAudioHandle >= 0) {
            this.cameraJNI.stopAudio(this.mHandle, this.mAudioHandle);
        }
        this.mAudioHandle = -1;
        this.mAudioListener = null;
    }

    public boolean startAudioPlayer() {
        boolean result = false;
        if (check()) {
            result = this.cameraJNI.startAudioPlayer(this.mHandle);
        }
        return result;
    }

    public boolean stopAudioPlayer() {
        boolean result = false;
        if (check()) {
            result = this.cameraJNI.stopAudioPlayer(this.mHandle);
        }
        return result;
    }

    public boolean playAudio(byte[] g711) {
        boolean result = false;
        if (check()) {
            result = this.cameraJNI.playAudio(this.mHandle, g711);
        }
        return result;
    }

    public int getChargeCurrent() {
        if (check()) {
            return this.cameraJNI.getChargeCurrent(this.mHandle);
        }
        return -1;
    }

    public int getHiVolt() {
        if (check()) {
            return this.cameraJNI.getHiVolt(this.mHandle);
        }
        return -1;
    }

    public int getLowVolt() {
        if (check()) {
            return this.cameraJNI.getLowVolt(this.mHandle);
        }
        return -1;
    }

    public int getBoardTemp() {
        if (check()) {
            return this.cameraJNI.getBoardTemp(this.mHandle);
        }
        return -1;
    }

    public int getBatTemp() {
        if (check()) {
            return this.cameraJNI.getBatTemp(this.mHandle);
        }
        return -1;
    }

    public MaintenanceParam getMaintenanceParam() {
        if (check()) {
            return this.cameraJNI.getConnectPCParam(this.mHandle);
        }
        return null;
    }

    public boolean setConnectPCParam(int enable, NetType netType, String ip, int port) {
        if (check()) {
            int sim = NetType.getSim(netType);
            if (this.mContext != null) {
                String ethName = NetType.getBindEthName(this.mContext, sim);
                return this.cameraJNI.setConnectPCParam(this.mHandle, enable, sim, ethName, ip, port);
            }
            DSLog.e(TAG, "setConnectPCParam failed,mContext is null!!!");
            return false;
        }
        return false;
    }

    public DSDeviceCustomInfo getDeviceCustomInfo() {
        if (check()) {
            return this.cameraJNI.getDeviceCustomInfo(this.mHandle);
        }
        return null;
    }

    public boolean setDeviceCustomInfo(DSDeviceCustomInfo info) {
        if (check()) {
            return this.cameraJNI.setDeviceCustomInfo(this.mHandle, info);
        }
        return false;
    }

    public ChannelPicParam getPicParam(int channel) {
        if (check()) {
            return this.cameraJNI.getPicParam(this.mHandle, channel);
        }
        return null;
    }

    public boolean setPicParam(int channel, ChannelPicParam param) {
        if (check()) {
            return this.cameraJNI.setPicParam(this.mHandle, channel, param);
        }
        return false;
    }

    public ChannelVideoParam getVideoParam(int channel) {
        if (check()) {
            return this.cameraJNI.getVideoParam(this.mHandle, channel);
        }
        return null;
    }

    public boolean setVideoParam(int channel, ChannelVideoParam param) {
        if (check()) {
            return this.cameraJNI.setVideoParam(this.mHandle, channel, param);
        }
        return false;
    }

    public ChannelOSD getOSDParam(int channel) {
        if (check()) {
            return this.cameraJNI.getOSDParam(this.mHandle, channel);
        }
        return null;
    }

    public boolean setOSDParam(int channel, ChannelOSD param) {
        if (check()) {
            return this.cameraJNI.setOSDParam(this.mHandle, channel, param);
        }
        return false;
    }

    public String getI1ParamJson(String i1_key) {
        if (check()) {
            return this.cameraJNI.getI1ParamJson(this.mHandle, i1_key);
        }
        return "";
    }

    public boolean setI1ParamJson(String i1_key, String i1_param_json) {
        if (check()) {
            return this.cameraJNI.setI1ParamJson(this.mHandle, 0, i1_key, i1_param_json);
        }
        return false;
    }

    public String getAIRegCode() {
        if (check()) {
            return this.cameraJNI.getAIRegCode(this.mHandle);
        }
        return "";
    }

    public boolean setAICDKey(String key) {
        if (check()) {
            return this.cameraJNI.setAICDKey(this.mHandle, key);
        }
        return false;
    }

    public boolean callI1Fun(int funID, int channel, int media_type) {
        if (check()) {
            return this.cameraJNI.runI1Fun(this.mHandle, funID, channel, media_type);
        }
        return false;
    }

    public String callRemote(String key, String param, String callFrom) {
        if (check()) {
            return this.cameraJNI.callFromRemote(this.mHandle, key, param, callFrom);
        }
        return null;
    }
}
