JnetPcap: конвертировать трафик Rtp из байта [] в аудио (.wav или.au)

В течение нескольких дней я искал способы декодирования rtp-трафика с использованием jnetpcap-1.4.r1425-1, я обнаруживаю rtp-трафик и сохраняю полезную нагрузку как байт []. Я хочу конвертировать byte[] в аудиофайл (.au или.wav ..) . Я попробовал это решение, это реализованный код:

byte[] byteArray = rtp.getPayload();

try {
    try {
        ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);
        AudioSystem.getAudioInputStream(byteStream);
    } catch (UnsupportedAudioFileException e) {
        byteArray = addWavHeader(byteArray);
        ByteArrayInputStream byteStream01 = new ByteArrayInputStream(byteArray);
        AudioSystem.getAudioInputStream(byteStream01);
        AudioFileFormat.Type fileType = AudioFileFormat.Type.AU;
        AudioSystem.write(AudioSystem.getAudioInputStream(byteStream01), fileType, fos);
    }
} catch (IOException | UnsupportedAudioFileException e) {
    throw new RuntimeException("cannot convert bytes to audio stream: " + e);
}

private static byte[] addWavHeader(byte[] bytes) throws IOException {
    ByteBuffer bufferWithHeader = ByteBuffer.allocate(bytes.length + 44);
    bufferWithHeader.order(ByteOrder.LITTLE_ENDIAN);
    bufferWithHeader.put("RIFF".getBytes());
    bufferWithHeader.putInt(bytes.length + 36);
    bufferWithHeader.put("WAVE".getBytes());
    bufferWithHeader.put("fmt ".getBytes());
    bufferWithHeader.putInt(16);
    bufferWithHeader.putShort((short) 1);
    bufferWithHeader.putShort((short) 1);
    bufferWithHeader.putInt(16000);
    bufferWithHeader.putInt(32000);
    bufferWithHeader.putShort((short) 2);
    bufferWithHeader.putShort((short) 16);
    bufferWithHeader.put("data".getBytes());
    bufferWithHeader.putInt(bytes.length);
    bufferWithHeader.put(bytes);
    return bufferWithHeader.array();
}

У меня есть эта ошибка:

Exception in thread "main"
java.lang.RuntimeException: cannot convert bytes to audio stream:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
    at com.journaldev.spring.cisco.axl.api.Demo.JnetPcapCapture_SIP_RTP$1.nextPacket(JnetPcapCapture_SIP_RTP.java:164)

0 ответов

Другие вопросы по тегам