Index: pjproject/branches/projects/iphone/pjmedia/src/pjmedia-codec/ilbc.c
===================================================================
--- pjproject/branches/projects/iphone/pjmedia/src/pjmedia-codec/ilbc.c	(revision 3130)
+++ pjproject/branches/projects/iphone/pjmedia/src/pjmedia-codec/ilbc.c	(revision 3137)
@@ -31,7 +31,14 @@
 #include <pj/string.h>
 #include <pj/os.h>
-#include "../../third_party/ilbc/iLBC_encode.h"
-#include "../../third_party/ilbc/iLBC_decode.h"
-
+
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    #include <AudioToolbox/AudioToolbox.h>
+    #define iLBC_Enc_Inst_t AudioConverterRef
+    #define iLBC_Dec_Inst_t AudioConverterRef
+    #define BLOCKL_MAX 		1
+#else
+    #include "../../third_party/ilbc/iLBC_encode.h"
+    #include "../../third_party/ilbc/iLBC_decode.h"
+#endif
 
 /*
@@ -132,4 +139,5 @@
     pj_timestamp	 last_tx;
 
+
     pj_bool_t		 enc_ready;
     iLBC_Enc_Inst_t	 enc;
@@ -143,4 +151,14 @@
     unsigned		 dec_samples_per_frame;
     float		 dec_block[BLOCKL_MAX];
+
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    unsigned		 enc_total_packets;
+    char		 *enc_buffer;
+    unsigned		 enc_buffer_offset;
+
+    unsigned		 dec_total_packets;
+    char		 *dec_buffer;
+    unsigned		 dec_buffer_offset;
+#endif
 };
 
@@ -353,4 +371,16 @@
 
     ilbc_codec = (struct ilbc_codec*) codec;
+
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    if (ilbc_codec->enc) {
+	AudioConverterDispose(ilbc_codec->enc);
+	ilbc_codec->enc = NULL;
+    }
+    if (ilbc_codec->dec) {
+	AudioConverterDispose(ilbc_codec->dec);
+	ilbc_codec->dec = NULL;
+    }
+#endif
+
     pj_pool_release(ilbc_codec->pool);
 
@@ -380,4 +410,26 @@
     pj_uint16_t dec_fmtp_mode = DEFAULT_MODE, 
 		enc_fmtp_mode = DEFAULT_MODE;
+
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    AudioStreamBasicDescription srcFormat, dstFormat;
+    UInt32 size;
+
+    srcFormat.mSampleRate       = attr->info.clock_rate;
+    srcFormat.mFormatID         = kAudioFormatLinearPCM;
+    srcFormat.mFormatFlags      = kLinearPCMFormatFlagIsSignedInteger
+				  | kLinearPCMFormatFlagIsPacked;
+    srcFormat.mBitsPerChannel   = attr->info.pcm_bits_per_sample;
+    srcFormat.mChannelsPerFrame = attr->info.channel_cnt;
+    srcFormat.mBytesPerFrame    = srcFormat.mChannelsPerFrame
+	                          * srcFormat.mBitsPerChannel >> 3;
+    srcFormat.mFramesPerPacket  = 1;
+    srcFormat.mBytesPerPacket   = srcFormat.mBytesPerFrame *
+				  srcFormat.mFramesPerPacket;
+
+    memset(&dstFormat, 0, sizeof(dstFormat));
+    dstFormat.mSampleRate 	= attr->info.clock_rate;
+    dstFormat.mFormatID 	= kAudioFormatiLBC;
+    dstFormat.mChannelsPerFrame = attr->info.channel_cnt;
+#endif
 
     pj_assert(ilbc_codec != NULL);
@@ -428,12 +480,32 @@
 
     /* Create encoder */
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    dstFormat.mFramesPerPacket  = CLOCK_RATE * enc_fmtp_mode / 1000;
+    dstFormat.mBytesPerPacket   = (enc_fmtp_mode == 20? 38 : 50);
+
+    /* Use AudioFormat API to fill out the rest of the description */
+    size = sizeof(dstFormat);
+    AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
+ 	                   0, NULL, &size, &dstFormat);
+
+    if (AudioConverterNew(&srcFormat, &dstFormat, &ilbc_codec->enc) != noErr)
+	return PJMEDIA_CODEC_EFAILED;
+    ilbc_codec->enc_frame_size = (enc_fmtp_mode == 20? 38 : 50);
+#else
     ilbc_codec->enc_frame_size = initEncode(&ilbc_codec->enc, enc_fmtp_mode);
+#endif
     ilbc_codec->enc_samples_per_frame = CLOCK_RATE * enc_fmtp_mode / 1000;
     ilbc_codec->enc_ready = PJ_TRUE;
 
     /* Create decoder */
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    if (AudioConverterNew(&dstFormat, &srcFormat, &ilbc_codec->dec) != noErr)
+	return PJMEDIA_CODEC_EFAILED;
+    ilbc_codec->dec_samples_per_frame = CLOCK_RATE * dec_fmtp_mode / 1000;
+#else
     ilbc_codec->dec_samples_per_frame = initDecode(&ilbc_codec->dec,
 						   dec_fmtp_mode,
 						   attr->setting.penh);
+#endif
     ilbc_codec->dec_frame_size = (dec_fmtp_mode == 20? 38 : 50);
     ilbc_codec->dec_ready = PJ_TRUE;
@@ -511,5 +583,5 @@
 	frames[count].size = ilbc_codec->dec_frame_size;
 	frames[count].timestamp.u64 = ts->u64 + count * 
-					  ilbc_codec->dec_samples_per_frame;
+				      ilbc_codec->dec_samples_per_frame;
 
 	pkt = ((char*)pkt) + ilbc_codec->dec_frame_size;
@@ -522,4 +594,67 @@
     return PJ_SUCCESS;
 }
+
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+static OSStatus encodeDataProc (
+    AudioConverterRef             inAudioConverter,
+    UInt32                        *ioNumberDataPackets,
+    AudioBufferList               *ioData,
+    AudioStreamPacketDescription  **outDataPacketDescription,
+    void                          *inUserData
+)
+{
+    struct ilbc_codec *ilbc_codec = (struct ilbc_codec*)inUserData;
+
+    /* Initialize in case of failure */
+    ioData->mBuffers[0].mData = NULL;
+    ioData->mBuffers[0].mDataByteSize = 0;
+
+    if (ilbc_codec->enc_total_packets < *ioNumberDataPackets) {
+	*ioNumberDataPackets = ilbc_codec->enc_total_packets;
+    }
+
+    if (*ioNumberDataPackets) {
+	ioData->mBuffers[0].mData = ilbc_codec->enc_buffer +
+				    ilbc_codec->enc_buffer_offset;
+	ioData->mBuffers[0].mDataByteSize = *ioNumberDataPackets *
+					    ilbc_codec->enc_samples_per_frame
+					    << 1;
+	ilbc_codec->enc_buffer_offset += ioData->mBuffers[0].mDataByteSize;
+    }
+
+    ilbc_codec->enc_total_packets -= *ioNumberDataPackets;
+    return noErr;
+}
+
+static OSStatus decodeDataProc (
+    AudioConverterRef             inAudioConverter,
+    UInt32                        *ioNumberDataPackets,
+    AudioBufferList               *ioData,
+    AudioStreamPacketDescription  **outDataPacketDescription,
+    void                          *inUserData
+)
+{
+    struct ilbc_codec *ilbc_codec = (struct ilbc_codec*)inUserData;
+
+    /* Initialize in case of failure */
+    ioData->mBuffers[0].mData = NULL;
+    ioData->mBuffers[0].mDataByteSize = 0;
+
+    if (ilbc_codec->dec_total_packets < *ioNumberDataPackets) {
+	*ioNumberDataPackets = ilbc_codec->dec_total_packets;
+    }
+
+    if (*ioNumberDataPackets) {
+	ioData->mBuffers[0].mData = ilbc_codec->dec_buffer +
+				    ilbc_codec->dec_buffer_offset;
+	ioData->mBuffers[0].mDataByteSize = *ioNumberDataPackets *
+					    ilbc_codec->dec_frame_size;
+	ilbc_codec->dec_buffer_offset += ioData->mBuffers[0].mDataByteSize;
+    }
+
+    ilbc_codec->dec_total_packets -= *ioNumberDataPackets;
+    return noErr;
+}
+#endif
 
 /*
@@ -534,4 +669,9 @@
     pj_int16_t *pcm_in;
     unsigned nsamples;
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    UInt32 npackets;
+    OSStatus err;
+    AudioBufferList theABL;
+#endif
 
     pj_assert(ilbc_codec && input && output);
@@ -574,4 +714,23 @@
     /* Encode */
     output->size = 0;
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    npackets = nsamples / ilbc_codec->enc_samples_per_frame;
+
+    theABL.mNumberBuffers = 1;
+    theABL.mBuffers[0].mNumberChannels = 1;
+    theABL.mBuffers[0].mDataByteSize = output_buf_len;
+    theABL.mBuffers[0].mData = output->buf;
+
+    ilbc_codec->enc_total_packets = npackets;
+    ilbc_codec->enc_buffer = (char *)input->buf;
+    ilbc_codec->enc_buffer_offset = 0;
+
+    err = AudioConverterFillComplexBuffer(ilbc_codec->enc, encodeDataProc,
+					  ilbc_codec, &npackets,
+					  &theABL, NULL);
+    if (err == noErr) {
+	output->size = npackets * ilbc_codec->enc_frame_size;
+    }
+#else
     while (nsamples >= ilbc_codec->enc_samples_per_frame) {
 	unsigned i;
@@ -589,4 +748,5 @@
 	nsamples -= ilbc_codec->enc_samples_per_frame;
     }
+#endif
 
     output->type = PJMEDIA_FRAME_TYPE_AUDIO;
@@ -605,5 +765,11 @@
 {
     struct ilbc_codec *ilbc_codec = (struct ilbc_codec*)codec;
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    UInt32 npackets;
+    OSStatus err;
+    AudioBufferList theABL;
+#else
     unsigned i;
+#endif
 
     pj_assert(ilbc_codec != NULL);
@@ -617,4 +783,24 @@
 
     /* Decode to temporary buffer */
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    npackets = input->size / ilbc_codec->dec_frame_size *
+	       ilbc_codec->dec_samples_per_frame;
+
+    theABL.mNumberBuffers = 1;
+    theABL.mBuffers[0].mNumberChannels = 1;
+    theABL.mBuffers[0].mDataByteSize = output_buf_len;
+    theABL.mBuffers[0].mData = output->buf;
+
+    ilbc_codec->dec_total_packets = npackets;
+    ilbc_codec->dec_buffer = (char *)input->buf;
+    ilbc_codec->dec_buffer_offset = 0;
+
+    err = AudioConverterFillComplexBuffer(ilbc_codec->dec, decodeDataProc,
+					  ilbc_codec, &npackets,
+					  &theABL, NULL);
+    if (err == noErr) {
+	output->size = npackets * (ilbc_codec->dec_samples_per_frame << 1);
+    }
+#else
     iLBC_decode(ilbc_codec->dec_block, (unsigned char*) input->buf,
 		&ilbc_codec->dec, 1);
@@ -625,4 +811,6 @@
     }
     output->size = (ilbc_codec->dec_samples_per_frame << 1);
+#endif
+
     output->type = PJMEDIA_FRAME_TYPE_AUDIO;
     output->timestamp = input->timestamp;
@@ -640,5 +828,11 @@
 {
     struct ilbc_codec *ilbc_codec = (struct ilbc_codec*)codec;
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    UInt32 npackets;
+    OSStatus err;
+    AudioBufferList theABL;
+#else
     unsigned i;
+#endif
 
     pj_assert(ilbc_codec != NULL);
@@ -649,4 +843,27 @@
 
     /* Decode to temporary buffer */
+#ifdef PJMEDIA_ILBC_CODEC_USE_COREAUDIO
+    npackets = 1;
+
+    theABL.mNumberBuffers = 1;
+    theABL.mBuffers[0].mNumberChannels = 1;
+    theABL.mBuffers[0].mDataByteSize = output_buf_len;
+    theABL.mBuffers[0].mData = output->buf;
+
+    ilbc_codec->dec_total_packets = npackets;
+    ilbc_codec->dec_buffer_offset = 0;
+    if (ilbc_codec->dec_buffer) {
+	err = AudioConverterFillComplexBuffer(ilbc_codec->dec, decodeDataProc,
+					      ilbc_codec, &npackets,
+					      &theABL, NULL);
+	if (err == noErr) {
+	    output->size = npackets *
+		           (ilbc_codec->dec_samples_per_frame << 1);
+	}
+    } else {
+	output->size = npackets * (ilbc_codec->dec_samples_per_frame << 1);
+	pj_bzero(output->buf, output->size);
+    }
+#else
     iLBC_decode(ilbc_codec->dec_block, NULL, &ilbc_codec->dec, 0);
 
@@ -656,4 +873,5 @@
     }
     output->size = (ilbc_codec->dec_samples_per_frame << 1);
+#endif
     output->type = PJMEDIA_FRAME_TYPE_AUDIO;
 
@@ -663,3 +881,2 @@
 
 #endif	/* PJMEDIA_HAS_ILBC_CODEC */
-
