Question

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;

sock.setReceiveBufferSize(bSockBuffer.length);

// 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);

track.play();

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

    // Paket empfangen:
    sock.receive(pack);

    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;

    EnterCriticalSection(&decodecrit);

    if (!decodesound)
        return (FMOD_ERR_FILE_EOF);

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

    if (result == FMOD_ERR_FILE_EOF)
    {
        // Handle looping:
        decodesound->seekData(0);

        datalen -= read;

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

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

    do
    {
        uSizeTmp = (read - uSentTmp);

        if (uSizeTmp > 1024)
            uSizeTmp = 1024;

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

    } while (uSentTmp < read);

    LeaveCriticalSection(&decodecrit);

    return (FMOD_OK);
}
Was it helpful?

Solution

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;
    FMOD_SOUND_FORMAT fFormat;
    FMOD_SOUND_TYPE fType;

    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);

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

        if ((result != FMOD_OK) && (result != FMOD_ERR_FILE_EOF))
            ASSERT(FALSE);

        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:
                Sleep(iSleep);
                dSleepTotal += iSleep;
            }

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

                // Sleep after sending every second playtime:
                Sleep(dSleepTotal);
            }
        }

    } 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.

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