Take the 2-minute tour ×

i want to send an audio stream from PC (C++ application, using FMOD-API to decode audio data and send via UDP Socket) to an android device. The communication already works and i can hear "sound" (100ms sound, followed by 900ms silence, alternating) on the android.

I don't know why the sound is stuttering - on the PC the same audio stream is played fine in nice quality. I think the problem is on the android..

Here is the code:

DatagramSocket  sock = new DatagramSocket(12345);
byte            []bSockBuffer = new byte[1024];
byte            []bRecvBufTmp;
int             iAudioBufSize, iCurAudioBufPos = 0;


// Audio Stream initialisieren:
iAudioBufSize       = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);

AudioTrack track    = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_STEREO,
                                    AudioFormat.ENCODING_PCM_16BIT, iAudioBufSize, AudioTrack.MODE_STREAM);


while (true)
    DatagramPacket pack = new DatagramPacket(bSockBuffer, bSockBuffer.length);

    // Paket empfangen:

    track.write(pack.getData(), 0, pack.getLength());

I'm sure to set up 'AudioTrack' object correctly, settings compare to my settings in the c++ application.

An other step was pre-buffering the received socket-data in a temporary 'byte[]' variable and writing it to the AudioTrack-object when the size of the buffer 'iAudioBufSize' was reached. This did not helped.

Any idears? Thanks

[EDIT] Code of C++ Application, used sample "manualdecode" of FMOD API examples:

FMOD_RESULT F_CALLBACK pcmreadcallback(FMOD_SOUND *sound, void *data, unsigned int datalen)
    CCtrlSocket     *cClientTmp = /* Obtaining target client sock here */;
    FMOD_RESULT     result;
    unsigned int    read, uSentTmp, uSizeTmp;


    if (!decodesound)
        return (FMOD_ERR_FILE_EOF);

    result = decodesound->readData(data, datalen, &read);

    if (result == FMOD_ERR_FILE_EOF)
        // Handle looping:

        datalen -= read;

        result = decodesound->readData((char*) data + read, datalen, &read);

    // Split package in multiple parts:
    uSentTmp = 0;

        uSizeTmp = (read - uSentTmp);

        if (uSizeTmp > 1024)
            uSizeTmp = 1024;

        uSentTmp += cClientTmp->SendAudioData((char*) data + uSentTmp, uSizeTmp);

    } while (uSentTmp < read);


    return (FMOD_OK);
share|improve this question
You know that UDP datagrams could be bigger then 1024 bytes, right? What are actual packet sizes? –  Nikolai N Fetissov May 24 '13 at 15:28
I've cut every sent package in my c++ application into 1024 byte pieces. I also have compared the size I#ve sent and received - it fits. –  Meisterzunge May 25 '13 at 8:17
You should try changing buffer sizes. Both socket and AudioTrack player multiply current values be 2 or 3 and check if something changed –  MP23 May 25 '13 at 17:24
I've experimented with the sizes.. –  Meisterzunge May 26 '13 at 9:46
Changing the size of the AudioTrack-buffer results in changing the duration of silence. I also tried a Thread.sleep([various timings]); after track.write(); - changed duration of silence... –  Meisterzunge May 26 '13 at 9:56

1 Answer 1

up vote 0 down vote accepted

I've done this problem. The mess was an entry in a logfile that has cost lots of time creating a lag :(

Now i can hear the streamed music on my android client. But there are still some lags. I've experimented a LOT of values for socket and AudioTrack buffers. I have compared the amount of sent and received bytes: In 20 secs sending 9170000 bytes of data results in receiving 8120000 bytes on android device. At first the stream is played fast for 3 secs (that means buffer's full?). After 30 secs the stream lags (which means buffer's empty?). In general the music quality is very good, but there is a sizzling noise all the time (which indicates lost socket packages?).

My 'PlaybackStart()' function has changed - i'm not using a PCM read callback anymore:

FMOD_RESULT CAudioStream::PlaybackStart()
    CCtrlSocket     *cClientTmp;
    unsigned int    read, uSentTmp, uSizeTmp;
    FMOD_RESULT result;

    result = system->createStream("C:\\test.mp3", FMOD_OPENONLY | FMOD_ACCURATETIME, 0, &sound);
    if(result != FMOD_OK)
        return (result);

    int iChannels, iBits;

    result = sound->getFormat(&fType, &fFormat, &iChannels, &iBits);
    if(result != FMOD_OK)
        return (result);

    void *data;
    unsigned int length = 0;
    int iSampleSec          = 1;                // Playtime
    int iSampleSize         = (44100 * 2 * sizeof(signed short) * iSampleSec);
    int iSleep              = 6;                // Sleep after sending a package
    DWORD   dSleepTotal;

    result = sound->getLength(&length, FMOD_TIMEUNIT_PCMBYTES);
    if(result != FMOD_OK)
        return (result);

    data = malloc(iSampleSize);
    if (!data)
        return (FMOD_RESULT_FORCEINT);

    cClientTmp = (CCtrlSocket*) CCtrlSocket::cServerSock.GetClientSock(CCtrlSocket::cServerSock.GetClientSockCount() - 1);

        result = sound->readData((char*) data, iSampleSize, &read);

        if ((result != FMOD_OK) && (result != FMOD_ERR_FILE_EOF))

        else if (read > 0)
            dSleepTotal = 0;

            for (int i = 0; i < read; i += NET_SVR_AUDIO_BUFFER)
                // MIN_VAL_LIMITED      ((MIN_VAL(VAL1, VAL2) <= LIMIT) ? LIMIT : MIN_VAL(VAL1, VAL2))
                cClientTmp->SendAudioData((char*) data + i, MIN_VAL_LIMITED(NET_SVR_AUDIO_BUFFER, (read - i), 0));

                // Sleep after sending every package:
                dSleepTotal += iSleep;

            if (dSleepTotal < (iSampleSec * 1000))
                dSleepTotal = (iSampleSec * 1000) - dSleepTotal;

                // Sleep after sending every second playtime:

    } while (read > 0);

    result = sound->release();
    if(result != FMOD_OK)
        return (result);

    result = system->close();
    if(result != FMOD_OK)
        return (result);

    result = system->release();
    if(result != FMOD_OK)
        return (result);

    return (result);

I have experimented with different sleep-timings, too.

share|improve this answer
Solved the problem! UDP loses some packages (that was the sizzling noise) and the stuttering came from unsorted incoming packages. Everything works fine on TCP/IP! –  Meisterzunge Jun 14 '13 at 14:20

Your Answer


