Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6823453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:47:04+00:00 2026-05-26T21:47:04+00:00

I’m working on an rtsp streaming(AAC format) client for iOS using ffmpeg. Right now

  • 0

I’m working on an rtsp streaming(AAC format) client for iOS using ffmpeg. Right now I can only say my app is workable, but the streaming sound is very noisy and even a little distorted, far worse than when it’s played by vlc or mplayer.

The stream is read by av_read_frame(), decoded by avcodec_decode_audio3(). Then I just send the decoded raw audio to Audio Queue.

When decoding a local aac file with my app, the sound seemed not so noisy at all. I know initial encoding would dramatically affect the result. However at least I should try to have it sounded like other streaming clients…

Many parts in my implementation/modification actually came from try and error. I believe I’m doing something wrong in setting up Audio Queue, and the callback function for filling Audio Buffer.

Any hints, suggestions or help are greatly appreciated.

// –info of test materials dumped by av_dump_format() —

Metadata:
    title           : /demo/test.3gp
  Duration: 00:00:30.11, start: 0.000000, bitrate: N/A
    Stream #0:0: Audio: aac, 32000 Hz, stereo, s16
aac  Advanced Audio Coding 

// — the Audio Queue setup procedure —

- (void) startPlayback
{
    OSStatus err = 0;
    if(playState.playing) return;

    playState.started = false;

    if(!playState.queue) 
    {

        UInt32 bufferSize;


        playState.format.mSampleRate = _av->audio.sample_rate;
        playState.format.mFormatID = kAudioFormatLinearPCM;
        playState.format.mFormatFlags = kAudioFormatFlagsCanonical;
        playState.format.mChannelsPerFrame = _av->audio.channels_per_frame;
        playState.format.mBytesPerPacket = sizeof(AudioSampleType) *_av->audio.channels_per_frame;
        playState.format.mBytesPerFrame = sizeof(AudioSampleType) *_av->audio.channels_per_frame;
        playState.format.mBitsPerChannel = 8 * sizeof(AudioSampleType);

        playState.format.mFramesPerPacket = 1;        
        playState.format.mReserved = 0;


        pauseStart = 0;
        DeriveBufferSize(playState.format,playState.format.mBytesPerPacket,BUFFER_DURATION,&bufferSize,&numPacketsToRead);
        err= AudioQueueNewOutput(&playState.format, aqCallback, &playState, NULL, kCFRunLoopCommonModes, 0, &playState.queue);

        if(err != 0)
        {
            printf("AQHandler.m startPlayback: Error creating new AudioQueue: %d \n", (int)err);
        }

        for(int i = 0 ; i < NUM_BUFFERS ; i ++)
        {
            err = AudioQueueAllocateBufferWithPacketDescriptions(playState.queue, bufferSize, numPacketsToRead , &playState.buffers[i]);

            if(err != 0)
                printf("AQHandler.m startPlayback: Error allocating buffer %d", i);
            fillAudioBuffer(&playState,playState.queue, playState.buffers[i]);
        }

    }

    startTime = mu_currentTimeInMicros();

    err=AudioQueueStart(playState.queue, NULL);

    if(err)
    {

        char sErr[4];
        printf("AQHandler.m startPlayback: Could not start queue %ld %s.", err, FormatError(sErr,err));

        playState.playing = NO;
    } 
    else
    {
        AudioSessionSetActive(true);
        playState.playing = YES;
    }           
}

// — callback for filling audio buffer —

static int ct = 0;
static void fillAudioBuffer(void *info,AudioQueueRef queue, AudioQueueBufferRef buffer)
{

    int lengthCopied = INT32_MAX;
    int dts= 0;
    int isDone = 0;

    buffer->mAudioDataByteSize = 0;
    buffer->mPacketDescriptionCount = 0;

    OSStatus err = 0;
    AudioTimeStamp bufferStartTime;

    AudioQueueGetCurrentTime(queue, NULL, &bufferStartTime, NULL);


    PlayState *ps = (PlayState *)info;

    if (!ps->started)
        ps->started = true;

    while(buffer->mPacketDescriptionCount < numPacketsToRead && lengthCopied > 0)
    {
        lengthCopied = getNextAudio(_av,
                        buffer->mAudioDataBytesCapacity-buffer->mAudioDataByteSize,
                        (uint8_t*)buffer->mAudioData+buffer->mAudioDataByteSize,
                        &dts,&isDone);

        ct+= lengthCopied;

        if(lengthCopied < 0 || isDone) 
        {
            printf("nothing to read....\n\n");
            PlayState *ps = (PlayState *)info;
            ps->finished = true;
            ps->started = false;
            break;
        }

        if(aqStartDts < 0) aqStartDts = dts;

        if(buffer->mPacketDescriptionCount ==0)
        {
            bufferStartTime.mFlags = kAudioTimeStampSampleTimeValid;
            bufferStartTime.mSampleTime = (Float64)(dts-aqStartDts);//* _av->audio.frame_size;

            if (bufferStartTime.mSampleTime <0 ) 
                bufferStartTime.mSampleTime = 0;

            printf("AQHandler.m fillAudioBuffer: DTS for %x: %lf time base: %lf StartDTS: %d\n", 
                    (unsigned int)buffer, 
                    bufferStartTime.mSampleTime, 
                    _av->audio.time_base, 
                    aqStartDts);

        }

        buffer->mPacketDescriptions[buffer->mPacketDescriptionCount].mStartOffset = buffer->mAudioDataByteSize;
        buffer->mPacketDescriptions[buffer->mPacketDescriptionCount].mDataByteSize = lengthCopied;



        buffer->mPacketDescriptions[buffer->mPacketDescriptionCount].mVariableFramesInPacket = 0;

        buffer->mPacketDescriptionCount++;

        buffer->mAudioDataByteSize += lengthCopied;
    }

    int audioBufferCount, audioBufferTotal,  videoBufferCount, videoBufferTotal;
    bufferCheck(_av,&videoBufferCount, &videoBufferTotal, &audioBufferCount, &audioBufferTotal);

    if(buffer->mAudioDataByteSize)
    {

        err = AudioQueueEnqueueBufferWithParameters(queue, buffer, 0, NULL, 0, 0, 0, NULL, &bufferStartTime, NULL);

        if(err)
        {
            char sErr[10];
            printf("AQHandler.m fillAudioBuffer: Could not enqueue buffer 0x%x: %d %s.", buffer, err, FormatError(sErr, err));

        }

    }

}




int getNextAudio(video_data_t* vInst, int maxlength, uint8_t* buf, int* pts, int* isDone) 
{

    struct video_context_t  *ctx = vInst->context;
    int    datalength            = 0;

    while(ctx->audio_ring.lock || (ctx->audio_ring.count <= 0 && ((ctx->play_state & STATE_DIE) != STATE_DIE)))
    {

        if (ctx->play_state & STATE_EOF) return -1;        
        usleep(100);
    }

    *pts = 0;
    ctx->audio_ring.lock = kLocked;

    if(ctx->audio_ring.count>0 && maxlength > ctx->audio_buffer[ctx->audio_ring.read].size)
    {    
        memcpy(buf, ctx->audio_buffer[ctx->audio_ring.read].data,ctx->audio_buffer[ctx->audio_ring.read].size);

        *pts = ctx->audio_buffer[ctx->audio_ring.read].pts;

        datalength = ctx->audio_buffer[ctx->audio_ring.read].size;

        ctx->audio_ring.read++;        
        ctx->audio_ring.read %= ABUF_SIZE;        
        ctx->audio_ring.count--;

    }
    ctx->audio_ring.lock = kUnlocked;

    if((ctx->play_state & STATE_EOF) == STATE_EOF && ctx->audio_ring.count == 0) *isDone = 1;

    return datalength;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:47:05+00:00Added an answer on May 26, 2026 at 9:47 pm

    The most likely reason for the distorted sound is simple packet loss, which RTSP can be susceptible to, especially over wireless connections.

    I suggest you look into configuring ffmpeg to use TCP based connections when possible instead of the default RTP/UDP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.