2

I'm doing a VoIP application for my thesis. I would like to know if someone can help me with this scenario: I have two threads, AudioThread and AudioSendThread. The first one its the listener that receive the audio-packet throught a DatagramSocket and play it in the phone. The second one its a recorder that grab 20 millisecond of sound and send it to the other device. I have implemented it in java but its really slow, so i decided to try OpenSL, but i dont find any documentation for something like this.

This is the AudioSendThread

public class AudioSendThread implements Runnable {
private final static String TAG = "AudioSndThread";
private boolean createdAudioP = false;
private DatagramSocket audioSndSocket;
private String ipAddr;
private byte[] buffer;

public AudioSendThread(Object o){
    this.ipAddr = //getting IpAddress
    audioSndSocket = (DatagramSocket)o;
}

@Override
public void run() {
    if(!createdAudioP)
        createdAudioP = createAudioRecorder();
    if(createdAudioP)
        startRecording();
    DatagramPacket packet = null;
    while(true){
            byte[] buffer = readAudio(20); //read 20 milliseconds of audio, this is the one i would like to implement in OpenSL
        try {
            packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(this.ipAddr), PORT.AUDIO);
            audioSndSocket.send(packet);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            return;
        }

    }
}

public static native void startRecording();
public static native boolean createAudioRecorder();
public static native byte[] readAudio(int millis);

static {
    System.loadLibrary("SoundUtils");
}}

And thisone the AudioThread

public class AudioThread implements Runnable{
private DatagramSocket audioServSock;

@Override
public void run() {
            createBufferQueueAudioPlayer();
    DatagramPacket packet = null;
    Thread audioSndThread = null;
    try {
        this.audioServSock = new DatagramSocket(PORT.AUDIO);
    } catch (SocketException e1) {
        e1.printStackTrace();
    }
    if(true){
        audioSndThread = new Thread(new AudioSendThread(this.audioServSock));
        audioSndThread.start();
    }
            byte[] buffer = new buffer[1500]; //random size
    packet = new DatagramPacket(buffer, 1500);
    while(true){
        try {
            audioServSock.receive(packet);
            playAudio(buffer, packet.getLength()); //other method i would like to implement in OpenSL
        } catch (IOException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            return;
        }           
    }
    at.stop();
    at.release();
}

public static native void createBufferQueueAudioPlayer();
public static native void playAudio(byte[] buffer, int length);

/** Load jni .so on initialization */
static {
     System.loadLibrary("native-audio-jni");
}

}

The other native methods are taken by the NativeAudio sample of the NDK

Thanks all for any suggestion!

Oxenarf
  • 200
  • 6
  • 23
  • The Android NDK includes a sample application called `native-audio` which shows how to do recording and playback using OpenSL ES. – Michael Oct 07 '13 at 17:25
  • i know it, and i tried to use this sample, but the playback isn't working.. only the mp3 reproducing it it working. – Oxenarf Oct 08 '13 at 16:43
  • Here is the ultimate article on low latency streaming on Android: http://createdigitalmusic.com/2013/05/why-mobile-low-latency-is-hard-explained-by-google-galaxy-nexus-still-android-of-choice/ – Alex Cohn Mar 12 '14 at 21:42
  • Did you ever manage to get OpenSL to record calls? – Recycled Steel Mar 02 '16 at 16:40

1 Answers1

3

You tried the native-audio sample code supplied with Android-NDK which means you're familiar with JNI calls. Here's a nice blog of Victor Lazzarini which describes his approach to the audio streaming for voice communication using OpenSL ES.

Android audio streaming with OpenSL ES and the NDK.

You can download the source code from here. Follow the instructions and run it in your device.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98