How to get WebRTC remote audio data and disable audiotrack on Android?

59 Views Asked by At

https://webrtc.googlesource.com/src/+/0f50cc284949f225f663408e7d467f39d549d3dc/sdk/android/src/java/org/webrtc/audio/WebRtcAudioTrack.java

I want to disable AudioTrack and get audio data to handle it myself. So I firstly try to dump to file, and use Audacity to listen it.

private class AudioTrackThread extends Thread {
  @Override
  public void run() {
    while (keepAlive) {
      // memcpy audio data to byteBuffer in native
      // whenever call this func, get sizeInBytes(960 Byte) audio data
      // to byteBuffer, no matter the data is wrong or correct. 
      nativeGetPlayoutData(nativeAudioTrack, sizeInBytes);

      // add my function to dump to file
      DumpToFile(byteBuffer, sizeInBytes);

      // disable it, do not use Android Audio Track.
      // int bytesWritten = audioTrack.write(byteBuffer, sizeInBytes,
      //                                AudioTrack.WRITE_BLOCKING);
    }
  }
}

Then I got a very huge file with the wrong audio data about hours even I just record seconds.

However, If I just add DumpToFile function, but do not disable audioTrack.write. I get the correct dump file, and dump file is small.

I find that it's because the while loops too much, and the nativeGetPlayoutData func gives wrong audio data. it returns correct audio data only with correct call time.

because WRITE_BLOCKING in each call of audioTrack.write, and it costs a exactly correct time, it control the loop time. And get the correct audio data to dump file.

Then I search lots of on web and calculate:

sizeInBytes = 960
samplerate = 48000 hz
chanel = 1
bitwidth = 16 bit
samplerate * chanel * bidwidth / 8 / 960 = 100 times
1000 ms / 100 = 10 ms

Then I add a sleep func in java to make sure every loop is 10ms.

It has improve, but still work so bad.


I read the AOSP native and AudioFlinger code, try to move the blocking code here, but failed.

Can anyone help me and give me some advice to dump correct audio file?

Thank you.

0

There are 0 best solutions below