Java 写Avi(烽火sdk)

abdulla1992
2022-08-03 / 0 评论 / 117 阅读 / 正在检测是否收录...
public class OnCtsSdkCallBack implements CtsSdk.OnCtsSdkCallBack{
    private static Logger logger = LoggerFactory.getLogger(OnCtsSdkCallBack.class);
    private static OnCtsSdkCallBack instance = new OnCtsSdkCallBack();

    private byte[] G_buffer = new byte[0];

    private Map<Integer,byte[]> G_bufferMap = new HashMap<>();
    private static final String pcmFilePath = "/data/audios/";
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
    private int devName=0;
    private String fname = devName + "_" + dateFormat.format(new Date())+".wav";

    private String filename = pcmFilePath+fname;
    private KafkaSender kafkaSender;
    private int writeNum = 0;
    //采样率
    private static float RATE = 16000f;
    //编码格式PCM
    private static AudioFormat.Encoding ENCODING = AudioFormat.Encoding.PCM_SIGNED;
    //帧大小 16
    private static int SAMPLE_SIZE = 16;
    //是否大端
    private static boolean BIG_ENDIAN = false;//true
    //通道数
    private static int CHANNELS = 1;

    public OnCtsSdkCallBack() {

    }

    public static OnCtsSdkCallBack getInstance() {
        return instance;
    }

    public KafkaSender getKafkaSender() {
        return kafkaSender;
    }

    public void setKafkaSender(KafkaSender kafkaSender) {
        this.kafkaSender = kafkaSender;
    }

    public  static  byte[]  byteMerger(byte[] a, byte[] b){
        byte[] temp = new  byte[a.length+b.length];
        System.arraycopy(a,0,temp,0,a.length);
        System.arraycopy(b,0,temp,a.length,b.length);
        return temp;
    }
    @Override
    public int invoke(int eCbType, Pointer pParam, int dwSize, int usr_data) throws IOException, ClassNotFoundException {
        switch (eCbType) {
            case CB_Event_TermRegister:
                CtsSdk.TSdkEventTermRegister tSdkTermRegister = null;
                assert false;
                ToolKits.GetPointerDataToStruct(pParam, 0, tSdkTermRegister);
                break;
            case CB_Event_TermConnect:
                break;
            case CB_Data_TermMp3L:
                break;
            case CB_Data_TermMp3R:
                System.out.println("____________________");
                break;
            case CB_Data_TermPcmL:
                CtsSdk.TSdkDataTermPcmL tSdkDataTermPcmL = new CtsSdk.TSdkDataTermPcmL();
                ToolKits.GetPointerDataToStruct(pParam, 0, tSdkDataTermPcmL);
                Pointer pPcmData = tSdkDataTermPcmL.pPcmData;
                if (pPcmData == null){
                    return 0;
                }
                if (tSdkDataTermPcmL.dwTermID != devName){
                    devName = tSdkDataTermPcmL.dwTermID;
                    fname = devName + "_" + dateFormat.format(new Date())+".wav";
                    filename = pcmFilePath+fname;
                }

                byte[] p_recive_buf = pPcmData.getByteArray(0, tSdkDataTermPcmL.nDataSize);
                //判断缓存数组是否超过2048字节,如果超出则写入队列,然后清空数组
                byte[] G_bytes = null;
                if (null == G_bufferMap.get(devName)){
                    G_bytes = new byte[0];
                }else {
                    G_bytes = G_bufferMap.get(devName);
                }
                int p_gBufferLen = G_bytes.length;
                int p_reciveBufferLen = p_recive_buf.length;
                int p_totalLen = p_gBufferLen + p_reciveBufferLen;
                if(p_totalLen >= Constance.maxByte){
                    writeNum ++;
                    logger.info("写入次数:{},文件路径:{}",writeNum,filename);
                    byte[] temBuffer01 = new byte[Constance.maxByte-p_gBufferLen];
                    System.arraycopy(p_recive_buf,0,temBuffer01,0,Constance.maxByte-p_gBufferLen);
                    G_bytes = byteMerger(G_bytes,temBuffer01);
                    InputStream b_in = new ByteArrayInputStream(G_bytes);
                    AudioFormat format = new AudioFormat(ENCODING,RATE, SAMPLE_SIZE, CHANNELS, (SAMPLE_SIZE / 8) * CHANNELS, RATE, BIG_ENDIAN);
                    AudioInputStream stream = new AudioInputStream(b_in, format, G_bytes.length);
                    File file = new File(filename);
                    int writeResult = AudioSystem.write(stream, AudioFileFormat.Type.WAVE, file);
                    JSONObject params = new JSONObject();
                    params.put("deviceId",devName);
                    params.put("audioUrl",this.getKafkaSender().getAudioNginxFile()+fname);
                    logger.info("kafka消息内容:{}",JSONObject.toJSONString(params));
                    this.getKafkaSender().send("audio-sensor-topic",JSONObject.toJSONString(params));
                    fname = devName + "_" + dateFormat.format(new Date())+".wav";
                    filename = pcmFilePath + fname;
                    G_bytes = new byte[0];
                    byte[] temBuffer02 = new byte[p_totalLen-Constance.maxByte];
                    System.arraycopy(p_recive_buf,Constance.maxByte-p_gBufferLen,temBuffer02,0,p_totalLen-Constance.maxByte);
                    G_bytes = byteMerger(G_bytes,temBuffer02);
                    G_bufferMap.remove(devName);
                }
                else {
                    G_bytes = byteMerger(G_bytes,p_recive_buf);
                    G_bufferMap.put(devName,G_bytes);
                }
                return 0;
            case CB_Data_TermPcmR:
                break;
            case CB_Data_TermAudio://播放音频
                break;
        }
        return 0;
    }

    /**
     * byte转对象
     *
     * @param bytes
     * @return
     */
    private Object ByteToObject(byte[] bytes) {
        Object obj = null;
        try {
            // bytearray to object
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream oi = new ObjectInputStream(bi);

            obj = oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception e) {
            System.out.println("translation" + e.getMessage());
            e.printStackTrace();
        }
        return obj;
    }

    public static void main(String[] args) {
        Map<Integer,byte[]> myMap = new HashMap<Integer,byte[]>(0);
        byte[] bytes = null;
        if (null == myMap.get(123)){
            bytes = new byte[0];
        }
        System.out.println("bytes = " + bytes.length);
        myMap.put(123,new byte[1024]);
        byte[] bytes1 = myMap.get(123);
        System.out.println("bytes = " + bytes1.length);
    }
}
1

评论 (0)

取消