Question

I'm currently playing with a little program to learn a bit more about udp datagram transfert. My script is based on an example I found here and what I'm doing is only using AudioTrack and AudioRecorder with two threads. One receives packets from the remote device and plays them with AudioTrack, the other sends the packets recorded by AudioRecorder to the remote device.

After some testing the whole hing seems to work well, the sound is transferred without problem from A to B and from B to A. Please notice that the two devices I used are two Samsung Galaxy s4 mini (actually it works with any samsung device). Then I used it between an Asus transformer tablet and an Acer Iconia 500, works well but with some little noise.

Now I tried to make it work with a Sony Xperia E and a Samsung galaxy (which works for sure), but big problem there: I'm hearing the sound when it comes from the xperia to the galaxy, but nothing ont the other side, the xperia's speaker seems not being able to play those packets (I saw in the logs that they are transferred though). I hope that it may be a setting issue but nothing sure about this, the same behavior also occurs when I communicate with a Huawei device (any kind).

Just in case if you wanna see my config and all the stuff, here is my script for the UDP streaming:

    public void startStreaming() {

    Thread streamThread = new Thread(new Runnable() {
        int bytesRcv;

        @Override
        public void run() {
            int minAudioBufSize = AudioRecord.getMinBufferSize(8000,
                    AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);

            byte[] datagramBuffer = new byte[512];

            DatagramPacket packet;

            InetAddress destination;
            try {
                destination = InetAddress.getByName(REMOTE_ADDRESS);
            } catch (UnknownHostException e1) {
                Log.e("Streaming", "Unknown host: " + e1.getLocalizedMessage());
                destination = null;
            }

            recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
                    minAudioBufSize);
            Log.d("Streaming", "Recorder initialized");

            recorder.startRecording();

            while (running == true) {
                try{
                    // reading data from MIC into buffer
                    bytesRcv = recorder.read(datagramBuffer, 0,
                            datagramBuffer.length);
                    // putting buffer in the packet

                    packet = new DatagramPacket(datagramBuffer, bytesRcv,
                            destination, SERVER_PORT);
                    Log.d("Streaming", "Packet size: " + packet.getLength());
                    Log.d("Streaming", "Packet content: " + Arrays.toString(datagramBuffer));
                    socket.send(packet);
                } catch (IOException e) {
                    Log.e("Streaming",
                            "Could not send package: "
                                    + e.getLocalizedMessage());
                    running = false;
                }catch (IllegalArgumentException iae){
                    Log.e("Streaming", "Error in audio datagram" + iae.getMessage());
                    running = false;
                }

            }

        }

    });
    streamThread.start();

And here is my script to receive the packets:

 public void startReceive() {

    Thread receiveThread = new Thread(new Runnable() {

        @Override
        public void run() {

            int audioBufSize = AudioTrack.getMinBufferSize(8000,
                    AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

            byte[] datagramBuffer = new byte[512];

            speaker = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                    8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
                    audioBufSize, AudioTrack.MODE_STREAM);

            speaker.play();

            while (running == true) {
                try {
                    DatagramPacket packet = new DatagramPacket(
                            datagramBuffer, datagramBuffer.length);
                    socket.receive(packet);
                    Log.d("Receiver", "Received " + packet.getLength() + " bytes");

                    // sending data to the Audiotrack obj i.e. speaker
                    speaker.write(datagramBuffer, 0, packet.getLength());
                    Log.d("Receiver", "Writing buffer content: " + Arrays.toString(datagramBuffer));

                } catch (IOException e) {
                    Log.e("Receiver",
                            "Could not send package: "
                                    + e.getLocalizedMessage());
                    running = false;
                }catch (IllegalArgumentException iae){
                    Log.e("Receiver", "Error in audio datagram" + iae.getMessage());
                    running = false;
                }
            }
        }
    });
    receiveThread.start();
}

Has somebody already faced this kind of issue? Any advice would be great.

Thanks

Was it helpful?

Solution

Ok solved, it's all about the sample rate and the buffer size.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top