Changeset 3406 for pjproject/trunk
- Timestamp:
- Jan 20, 2011 3:32:14 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c
r3404 r3406 121 121 AudioConverterRef resample; 122 122 pj_int16_t *resample_buf; 123 void *resample_buf_ptr; 123 124 unsigned resample_buf_count; 124 125 unsigned resample_buf_size; … … 656 657 struct coreaudio_stream *strm = (struct coreaudio_stream*)inUserData; 657 658 658 pj_assert(*ioNumberDataPackets == strm->resample_buf_count); 659 if (*ioNumberDataPackets > strm->resample_buf_size) 660 *ioNumberDataPackets = strm->resample_buf_size; 659 661 660 662 ioData->mNumberBuffers = 1; 661 663 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 * 664 666 strm->param.bits_per_sample >> 3; 665 return 0; 667 668 return noErr; 666 669 } 667 670 … … 680 683 pj_int16_t *input; 681 684 UInt32 resampleSize; 682 UInt32 size;683 685 684 686 pj_assert(!strm->quit_flag); … … 718 720 input = (pj_int16_t *)buf->mBuffers[0].mData; 719 721 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; 736 723 nsamples = inNumberFrames * strm->param.channel_count + 737 724 strm->resample_buf_count; 738 pj_assert(nsamples < strm->resample_buf_size);739 725 740 726 if (nsamples >= resampleSize) { 727 pjmedia_frame frame; 741 728 UInt32 resampleOutput = strm->param.samples_per_frame; 742 unsigned chunk_count = 0;743 pjmedia_frame frame;744 729 AudioBufferList ab; 745 730 746 731 frame.type = PJMEDIA_FRAME_TYPE_AUDIO; 732 frame.buf = (void*) strm->rec_buf; 747 733 frame.size = strm->param.samples_per_frame * 748 734 strm->param.bits_per_sample >> 3; 749 735 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; 750 741 751 742 /* If buffer is not empty, combine the buffer with the just incoming 752 743 * samples, then call put_frame. 753 744 */ 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 } 788 799 789 800 /* Store the remaining samples into the buffer */ … … 862 873 nsamples = inNumberFrames * strm->param.channel_count + 863 874 strm->rec_buf_count; 864 if (nsamples >= strm->param.samples_per_frame) 865 { 875 if (nsamples >= strm->param.samples_per_frame) { 866 876 pjmedia_frame frame; 867 877 … … 1178 1188 return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); 1179 1189 } 1180 1190 1181 1191 /* 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 1184 1193 */ 1185 strm->resample_buf_size = (unsigned)(desc->mSampleRate *1186 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); 1188 1197 strm->resample_buf = (pj_int16_t*) 1189 1198 pj_pool_alloc(strm->pool,
Note: See TracChangeset
for help on using the changeset viewer.