0

I`am developping and android VoIP apps using Audiotracks and Audiorecord. But there is a lot of echo, I tried to use Speex echo canceller with JNI but I get error :

07-21 16:58:39.029: A/libc(7570): Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1)

the important of my code is

   public void run(){

      short[] audioShorts, recvShorts, recordedShorts, filteredShorts;
      byte[] audioBytes, recvBytes;
      int shortsRead;
      DatagramPacket packet;

      int samplingRate = 8000;
      int managerBufferSize = 2000;
      AudioTrack player = new AudioTrack(AudioManager.STREAM_MUSIC, samplingRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, managerBufferSize, AudioTrack.MODE_STREAM);
      recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, managerBufferSize);
      recorder.startRecording();
      player.play();

      audioShorts = new short[1000];
      shortsRead = recorder.read(audioShorts, 0, audioShorts.length);

      audioBytes = new byte[shortsRead*2];
      ByteBuffer.wrap(audioBytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(audioShorts);

      packet = new DatagramPacket(audioBytes, audioBytes.length);
      socket.send(packet);

      while (!this.isInterrupted()){

        recvBytes = new byte[2000];
        packet = new DatagramPacket(recvBytes, recvBytes.length);
        socket.receive(packet);
        recvShorts = new short[packet.getLength()/2];
        ByteBuffer.wrap(packet.getData(), 0, packet.getLength()).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(recvShorts);

        player.write(recvShorts, 0, recvShorts.length);

        recordedShorts = new short[1000];
        shortsRead = recorder.read(recordedShorts, 0, recordedShorts.length);

        filteredShorts = speexEchoCancel(recordedShorts, recvShorts);

        audioBytes = new byte[shortsRead*2];
        ByteBuffer.wrap(audioBytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(filteredShorts);

        packet = new DatagramPacket(audioBytes, audioBytes.length);
        socket.send(packet);                

      }

and the C code with JNI is

void ava_com_example_sendaudio2_MainActivity_initEchoState_initEchoState(JNIEnv *env, jobject jobj, jint frameSize, jint filterLength){
  echo_state = speex_echo_state_init(frameSize, filterLength);
}

ava_com_example_sendaudio2_MainActivity_initEchoState_speexEchoCancel(JNIEnv *env, jobject jObj, jshortArray input_frame, jshortArray echo_frame){

  //create native shorts from java shorts
  jshort *native_input_frame = (*env)->GetShortArrayElements(env, input_frame, NULL);
  jshort *native_echo_frame = (*env)->GetShortArrayElements(env, echo_frame, NULL);

  //allocate memory for output data
  jint length = (*env)->GetArrayLength(env, input_frame);
  jshortArray temp = (*env)->NewShortArray(env, length);
  jshort *native_output_frame = (*env)->GetShortArrayElements(env, temp, 0);

  //call echo cancellation
  speex_echo_cancellation(echo_state, native_input_frame, native_echo_frame, native_output_frame);

  //convert native output to java layer output
  jshortArray output_shorts = (*env)->NewShortArray(env, length);
  (*env)->SetShortArrayRegion(env, output_shorts, 0, length, native_output_frame);

  //cleanup and return
  (*env)->ReleaseShortArrayElements(env, input_frame, native_input_frame, 0);
  (*env)->ReleaseShortArrayElements(env, echo_frame, native_echo_frame, 0);
  (*env)->ReleaseShortArrayElements(env, temp, native_output_frame, 0);
  return output_shorts;
}

this error is getting when the code use the function filteredShorts = speexEchoCancel(recordedShorts, recvShorts);

How can I resolve this ? Thanks in advance!

  • Either the library has a bug, or you passed it a null pointer and it trusted it. Can't tell more, you didn't give the full stack dump. I'd log every call to speex and check for unexpected nulls. – Gabe Sechan Jul 21 '14 at 21:36
  • I entred in debug mode, this is the only error that i get, and it's when i come to echo cancel function, So this is the full dump! – user3684082 Jul 21 '14 at 22:19
  • In your logcat you'll find a full dump. At worst it will say what file your full dump was written to – Gabe Sechan Jul 21 '14 at 22:25
  • Hello, that what i get in logcat 07-22 14:38:55.349: D/VR(8866): Writing buffer content to speaker 07-22 14:38:55.349: D/VR(8866): Socket Created7 07-22 14:38:55.349: A/libc(8866): Fatal signal 11 (SIGSEGV) at 0x1f000005 (code=1) 07-22 14:38:56.109: W/AudioTrack(8866): obtainBuffer() track 0x1bce4b0 disabled, restarting 07-22 14:38:56.789: W/AudioTrack(8866): obtainBuffer() track 0x1bce4b0 disabled, restarting 07-22 14:38:57.569: W/AudioTrack(8866): obtainBuffer() track 0x1bce4b0 disabled, restarting And then the apps craches :/ is ther any solution? thanks – user3684082 Jul 22 '14 at 18:41

0 Answers0