Changeset 3406


Ignore:
Timestamp:
Jan 20, 2011 3:32:14 AM (13 years ago)
Author:
ming
Message:

Re #3398: Fixes assertion in the resample procedure and callback.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c

    r3404 r3406  
    121121    AudioConverterRef            resample; 
    122122    pj_int16_t                  *resample_buf; 
     123    void                        *resample_buf_ptr; 
    123124    unsigned                     resample_buf_count; 
    124125    unsigned                     resample_buf_size; 
     
    656657    struct coreaudio_stream *strm = (struct coreaudio_stream*)inUserData; 
    657658 
    658     pj_assert(*ioNumberDataPackets == strm->resample_buf_count); 
     659    if (*ioNumberDataPackets > strm->resample_buf_size) 
     660        *ioNumberDataPackets = strm->resample_buf_size; 
    659661 
    660662    ioData->mNumberBuffers = 1; 
    661663    ioData->mBuffers[0].mNumberChannels = 1; 
    662     ioData->mBuffers[0].mData = strm->resample_buf; 
    663     ioData->mBuffers[0].mDataByteSize = strm->resample_buf_count * 
     664    ioData->mBuffers[0].mData = strm->resample_buf_ptr; 
     665    ioData->mBuffers[0].mDataByteSize = *ioNumberDataPackets * 
    664666                                        strm->param.bits_per_sample >> 3; 
    665     return 0; 
     667 
     668    return noErr; 
    666669} 
    667670 
     
    680683    pj_int16_t *input; 
    681684    UInt32 resampleSize; 
    682     UInt32 size; 
    683685 
    684686    pj_assert(!strm->quit_flag); 
     
    718720    input = (pj_int16_t *)buf->mBuffers[0].mData; 
    719721 
    720     /* Calculate how many frames we need to fill an entire packet */ 
    721     resampleSize = strm->param.samples_per_frame * 
    722                    strm->param.bits_per_sample >> 3; 
    723     size = sizeof(resampleSize); 
    724     ostatus = AudioConverterGetProperty( 
    725                   strm->resample, 
    726                   kAudioConverterPropertyCalculateInputBufferSize, 
    727                   &size, 
    728                   &resampleSize); 
    729     if (ostatus != noErr) { 
    730         PJ_LOG(5, (THIS_FILE, "Core audio converter measure error %i", 
    731                    ostatus)); 
    732         goto on_break; 
    733     } 
    734     resampleSize /= strm->param.bits_per_sample >> 3; 
    735  
     722    resampleSize = strm->resample_buf_size; 
    736723    nsamples = inNumberFrames * strm->param.channel_count + 
    737724               strm->resample_buf_count; 
    738     pj_assert(nsamples < strm->resample_buf_size); 
    739725 
    740726    if (nsamples >= resampleSize) { 
     727        pjmedia_frame frame; 
    741728        UInt32 resampleOutput = strm->param.samples_per_frame; 
    742         unsigned chunk_count = 0; 
    743         pjmedia_frame frame; 
    744729        AudioBufferList ab; 
    745730 
    746731        frame.type = PJMEDIA_FRAME_TYPE_AUDIO; 
     732        frame.buf = (void*) strm->rec_buf; 
    747733        frame.size = strm->param.samples_per_frame * 
    748734                     strm->param.bits_per_sample >> 3; 
    749735        frame.bit_info = 0; 
     736         
     737        ab.mNumberBuffers = 1; 
     738        ab.mBuffers[0].mNumberChannels = 1; 
     739        ab.mBuffers[0].mData = strm->rec_buf; 
     740        ab.mBuffers[0].mDataByteSize = frame.size; 
    750741 
    751742        /* If buffer is not empty, combine the buffer with the just incoming 
    752743         * samples, then call put_frame. 
    753744         */ 
    754  
    755         chunk_count = resampleSize - strm->resample_buf_count; 
    756         pjmedia_copy_samples(strm->resample_buf + strm->resample_buf_count, 
    757                              input, chunk_count); 
    758         strm->resample_buf_count += chunk_count; 
    759  
    760         frame.buf = (void*) strm->rec_buf; 
    761         frame.timestamp.u64 = strm->rec_timestamp.u64; 
    762  
    763         ab.mNumberBuffers = 1; 
    764         ab.mBuffers[0].mNumberChannels = 1; 
    765         ab.mBuffers[0].mDataByteSize = frame.size; 
    766         ab.mBuffers[0].mData = frame.buf; 
    767  
    768         /* Do the resample */ 
    769         ostatus = AudioConverterFillComplexBuffer(strm->resample, 
    770                                                   resampleProc, 
    771                                                   strm, 
    772                                                   &resampleOutput, 
    773                                                   &ab, 
    774                                                   NULL); 
    775         if (ostatus != noErr) { 
    776             goto on_break; 
    777         } 
    778  
    779         status = (*strm->rec_cb)(strm->user_data, &frame); 
    780  
    781         input = input + chunk_count; 
    782         nsamples -= resampleSize; 
    783         strm->resample_buf_count = 0; 
    784         strm->rec_timestamp.u64 += strm->param.samples_per_frame / 
    785                                    strm->param.channel_count; 
    786  
    787         pj_assert(nsamples < resampleSize); 
     745        if (strm->resample_buf_count) { 
     746            unsigned chunk_count = resampleSize - strm->resample_buf_count; 
     747            pjmedia_copy_samples(strm->resample_buf + strm->resample_buf_count, 
     748                                 input, chunk_count); 
     749 
     750            /* Do the resample */ 
     751 
     752            strm->resample_buf_ptr = strm->resample_buf; 
     753            ostatus = AudioConverterFillComplexBuffer(strm->resample, 
     754                                                      resampleProc, 
     755                                                      strm, 
     756                                                      &resampleOutput, 
     757                                                      &ab, 
     758                                                      NULL); 
     759            if (ostatus != noErr) { 
     760                goto on_break; 
     761            } 
     762            frame.timestamp.u64 = strm->rec_timestamp.u64; 
     763 
     764            status = (*strm->rec_cb)(strm->user_data, &frame); 
     765 
     766            input = input + chunk_count; 
     767            nsamples -= resampleSize; 
     768            strm->resample_buf_count = 0; 
     769            strm->rec_timestamp.u64 += strm->param.samples_per_frame / 
     770                                       strm->param.channel_count; 
     771        } 
     772         
     773         
     774        /* Give all frames we have */ 
     775        while (nsamples >= resampleSize && status == 0) { 
     776            frame.timestamp.u64 = strm->rec_timestamp.u64; 
     777             
     778            /* Do the resample */ 
     779            strm->resample_buf_ptr = input; 
     780            ab.mBuffers[0].mDataByteSize = frame.size; 
     781            resampleOutput = strm->param.samples_per_frame; 
     782            ostatus = AudioConverterFillComplexBuffer(strm->resample, 
     783                                                      resampleProc, 
     784                                                      strm, 
     785                                                      &resampleOutput, 
     786                                                      &ab, 
     787                                                      NULL); 
     788            if (ostatus != noErr) { 
     789                goto on_break; 
     790            }        
     791             
     792            status = (*strm->rec_cb)(strm->user_data, &frame); 
     793             
     794            input = (pj_int16_t*) input + resampleSize; 
     795            nsamples -= resampleSize; 
     796            strm->rec_timestamp.u64 += strm->param.samples_per_frame / 
     797                                       strm->param.channel_count; 
     798        } 
    788799 
    789800        /* Store the remaining samples into the buffer */ 
     
    862873    nsamples = inNumberFrames * strm->param.channel_count + 
    863874               strm->rec_buf_count; 
    864     if (nsamples >= strm->param.samples_per_frame) 
    865      { 
     875    if (nsamples >= strm->param.samples_per_frame) { 
    866876        pjmedia_frame frame; 
    867877 
     
    11781188        return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); 
    11791189    } 
    1180  
     1190     
    11811191    /* 
    1182      * Allocate the buffer required to hold the enough input data 
    1183      * for two complete frames of audio. 
     1192     * Allocate the buffer required to hold enough input data 
    11841193     */ 
    1185     strm->resample_buf_size = (unsigned)(desc->mSampleRate * 
    1186                               strm->param.samples_per_frame / 
    1187                               strm->param.clock_rate * 2); 
     1194    strm->resample_buf_size =  (unsigned)(desc->mSampleRate * 
     1195                                          strm->param.samples_per_frame / 
     1196                                          strm->param.clock_rate); 
    11881197    strm->resample_buf = (pj_int16_t*) 
    11891198                         pj_pool_alloc(strm->pool, 
Note: See TracChangeset for help on using the changeset viewer.