Index: pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec/config.h
===================================================================
--- pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec/config.h	(revision 2434)
+++ pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec/config.h	(revision 2436)
@@ -204,3 +204,23 @@
 #endif
 
+#ifndef PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
+#   define PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR	1
+#endif
+
+#ifndef PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
+#   define PJMEDIA_HAS_PASSTHROUGH_CODEC_G729	1
+#endif
+
+#ifndef PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC
+#   define PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC	1
+#endif
+
+#ifndef PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU
+#   define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU	1
+#endif
+
+#ifndef PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
+#   define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA	1
+#endif
+
 #endif	/* __PJMEDIA_CODEC_CONFIG_H__ */
Index: pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec/passthrough.h
===================================================================
--- pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec/passthrough.h	(revision 2436)
+++ pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec/passthrough.h	(revision 2436)
@@ -0,0 +1,78 @@
+/* $Id$ */
+/* 
+ * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
+ * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
+ */
+#ifndef __PJMEDIA_CODECS_PASSTHROUGH_H__
+#define __PJMEDIA_CODECS_PASSTHROUGH_H__
+
+/**
+ * @file pjmedia-codec/passthrough.h
+ * @brief Passthrough codecs.
+ */
+
+#include <pjmedia-codec/types.h>
+
+/**
+ * @defgroup PJMED_PASSTHROUGH_CODEC Passthrough Codecs
+ * @ingroup PJMEDIA_CODEC
+ * @brief Implementation of passthrough codecs
+ * @{
+ *
+ * This section describes functions to register and register passthrough 
+ * codecs factory to the codec manager. After the codec factory has been 
+ * registered, application can use @ref PJMEDIA_CODEC API to manipulate 
+ * the codec. This codec factory contains various codecs, e.g: G.729, iLBC,
+ * AMR, and G.711.
+ *
+ * Passthrough codecs are codecs wrapper that does not perform encoding 
+ * or decoding, it just pack and parse encoded audio data from/into RTP 
+ * payload. This will accomodate pjmedia ports which work with encoded
+ * audio data, e.g: encoded audio files, sound device with capability
+ * of playing/recording encoded audio data.
+ */
+
+PJ_BEGIN_DECL
+
+/**
+ * Initialize and register passthrough codecs factory to pjmedia endpoint.
+ *
+ * @param endpt	    The pjmedia endpoint.
+ *
+ * @return	    PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t) pjmedia_codec_passthrough_init( pjmedia_endpt *endpt );
+
+
+
+/**
+ * Unregister passthrough codecs factory from pjmedia endpoint.
+ *
+ * @return	    PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t) pjmedia_codec_passthrough_deinit(void);
+
+
+PJ_END_DECL
+
+
+/**
+ * @}
+ */
+
+#endif	/* __PJMEDIA_CODECS_PASSTHROUGH_H__ */
+
Index: pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/codec.h
===================================================================
--- pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/codec.h	(revision 2434)
+++ pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/codec.h	(revision 2436)
@@ -276,4 +276,7 @@
        pj_uint8_t  pcm_bits_per_sample;	/**< Bits/sample in the PCM side    */
        pj_uint8_t  pt;			/**< Payload type.		    */
+       pjmedia_fourcc format;		/**< Source format, it's format of
+					     encoder input and decoder 
+					     output.			    */
     } info;
 
Index: pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/port.h
===================================================================
--- pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/port.h	(revision 2434)
+++ pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/port.h	(revision 2436)
@@ -285,5 +285,12 @@
 
 
-/* Append one subframe to the frame_ext */
+/**
+ * Append one subframe to #pjmedia_frame_ext.
+ *
+ * @param frm		    The #pjmedia_frame_ext.
+ * @param src		    Subframe data.
+ * @param bitlen	    Lenght of subframe, in bits.
+ * @param samples_cnt	    Number of audio samples in subframe.
+ */
 PJ_INLINE(void) pjmedia_frame_ext_append_subframe(pjmedia_frame_ext *frm,
 						  const void *src,
@@ -311,25 +318,34 @@
 }
 
-/* Get the pointer and length of the n-th subframe */
+/**
+ * Get a subframe from #pjmedia_frame_ext.
+ *
+ * @param frm		    The #pjmedia_frame_ext.
+ * @param n		    Subframe index, zero based.
+ *
+ * @return		    The n-th subframe, or NULL if n is out-of-range.
+ */
 PJ_INLINE(pjmedia_frame_ext_subframe*) 
 	  pjmedia_frame_ext_get_subframe(const pjmedia_frame_ext *frm,
 					 unsigned n)
 {
-    pj_uint8_t *p;
-    unsigned i;
-    pjmedia_frame_ext_subframe *tmp;
-
-    pj_assert(n < frm->subframe_cnt);
-
-    p = (pj_uint8_t*)frm + sizeof(pjmedia_frame_ext);
-    for (i = 0; i < n; ++i) {	
-	tmp = (pjmedia_frame_ext_subframe*) p;
-	p += tmp->bitlen / 8;
-	if (tmp->bitlen % 8)
-	    ++p;
+    pjmedia_frame_ext_subframe *sf = NULL;
+
+    if (n < frm->subframe_cnt) {
+	pj_uint8_t *p;
+	unsigned i;
+
+	p = (pj_uint8_t*)frm + sizeof(pjmedia_frame_ext);
+	for (i = 0; i < n; ++i) {	
+	    sf = (pjmedia_frame_ext_subframe*) p;
+	    p += sf->bitlen / 8;
+	    if (sf->bitlen % 8)
+		++p;
+	}
+        
+	sf = (pjmedia_frame_ext_subframe*) p;
     }
-    
-    tmp = (pjmedia_frame_ext_subframe*) p;
-    return tmp;
+
+    return sf;
 }
 	
Index: pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia-codec/passthrough.c
===================================================================
--- pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia-codec/passthrough.c	(revision 2436)
+++ pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia-codec/passthrough.c	(revision 2436)
@@ -0,0 +1,849 @@
+/* $Id$ */
+/* 
+ * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
+ * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
+ */
+#include <pjmedia-codec/passthrough.h>
+#include <pjmedia/codec.h>
+#include <pjmedia/errno.h>
+#include <pjmedia/endpoint.h>
+#include <pjmedia/port.h>
+#include <pj/assert.h>
+#include <pj/log.h>
+#include <pj/pool.h>
+#include <pj/string.h>
+#include <pj/os.h>
+
+/*
+ * Only build this file if PJMEDIA_HAS_PASSTHROUGH_CODECS != 0
+ */
+#if defined(PJMEDIA_HAS_PASSTHROUGH_CODECS) && PJMEDIA_HAS_PASSTHROUGH_CODECS!=0
+
+#define THIS_FILE   "passthrough.c"
+
+
+/* Prototypes for passthrough codecs factory */
+static pj_status_t test_alloc( pjmedia_codec_factory *factory, 
+			       const pjmedia_codec_info *id );
+static pj_status_t default_attr( pjmedia_codec_factory *factory, 
+				 const pjmedia_codec_info *id, 
+				 pjmedia_codec_param *attr );
+static pj_status_t enum_codecs( pjmedia_codec_factory *factory, 
+				unsigned *count, 
+				pjmedia_codec_info codecs[]);
+static pj_status_t alloc_codec( pjmedia_codec_factory *factory, 
+				const pjmedia_codec_info *id, 
+				pjmedia_codec **p_codec);
+static pj_status_t dealloc_codec( pjmedia_codec_factory *factory, 
+				  pjmedia_codec *codec );
+
+/* Prototypes for passthrough codecs implementation. */
+static pj_status_t codec_init( pjmedia_codec *codec, 
+			       pj_pool_t *pool );
+static pj_status_t codec_open( pjmedia_codec *codec, 
+			       pjmedia_codec_param *attr );
+static pj_status_t codec_close( pjmedia_codec *codec );
+static pj_status_t codec_modify(pjmedia_codec *codec, 
+			        const pjmedia_codec_param *attr );
+static pj_status_t codec_parse( pjmedia_codec *codec,
+			        void *pkt,
+				pj_size_t pkt_size,
+				const pj_timestamp *ts,
+				unsigned *frame_cnt,
+				pjmedia_frame frames[]);
+static pj_status_t codec_encode( pjmedia_codec *codec, 
+				 const struct pjmedia_frame *input,
+				 unsigned output_buf_len,
+				 struct pjmedia_frame *output);
+static pj_status_t codec_decode( pjmedia_codec *codec,
+				 const struct pjmedia_frame *input,
+				 unsigned output_buf_len, 
+				 struct pjmedia_frame *output);
+static pj_status_t codec_recover( pjmedia_codec *codec, 
+				  unsigned output_buf_len, 
+				  struct pjmedia_frame *output);
+
+/* Definition for passthrough codecs operations. */
+static pjmedia_codec_op codec_op = 
+{
+    &codec_init,
+    &codec_open,
+    &codec_close,
+    &codec_modify,
+    &codec_parse,
+    &codec_encode,
+    &codec_decode,
+    &codec_recover
+};
+
+/* Definition for passthrough codecs factory operations. */
+static pjmedia_codec_factory_op codec_factory_op =
+{
+    &test_alloc,
+    &default_attr,
+    &enum_codecs,
+    &alloc_codec,
+    &dealloc_codec
+};
+
+/* Passthrough codecs factory */
+static struct codec_factory {
+    pjmedia_codec_factory    base;
+    pjmedia_endpt	    *endpt;
+    pj_pool_t		    *pool;
+    pj_mutex_t		    *mutex;
+} codec_factory;
+
+/* Passthrough codecs private data. */
+typedef struct codec_private {
+    pj_pool_t		*pool;		    /**< Pool for each instance.    */
+    int			 codec_idx;	    /**< Codec index.		    */
+    void		*codec_setting;	    /**< Specific codec setting.    */
+    pj_uint16_t		 avg_frame_size;    /**< Average of frame size.	    */
+} codec_private_t;
+
+
+
+/* CUSTOM CALLBACKS */
+
+/* Parse frames from a packet. Default behaviour of frame parsing is 
+ * just separating frames based on calculating frame length derived 
+ * from bitrate. Implement this callback when the default behaviour is 
+ * unapplicable.
+ */
+typedef pj_status_t (*parse_cb)(codec_private_t *codec_data, void *pkt, 
+				pj_size_t pkt_size, const pj_timestamp *ts,
+				unsigned *frame_cnt, pjmedia_frame frames[]);
+
+/* Pack frames into a packet. Default behaviour of packing frames is 
+ * just stacking the frames with octet aligned without adding any 
+ * payload header. Implement this callback when the default behaviour is
+ * unapplicable.
+ */
+typedef pj_status_t (*pack_cb)(codec_private_t *codec_data, 
+			       const struct pjmedia_frame_ext *input,
+			       unsigned output_buf_len, 
+			       struct pjmedia_frame *output);
+
+
+/* Custom callback implementations. */
+static pj_status_t parse_amr( codec_private_t *codec_data, void *pkt, 
+			      pj_size_t pkt_size, const pj_timestamp *ts,
+			      unsigned *frame_cnt, pjmedia_frame frames[]);
+static pj_status_t pack_amr ( codec_private_t *codec_data,
+			      const struct pjmedia_frame_ext *input,
+			      unsigned output_buf_len, 
+			      struct pjmedia_frame *output);
+
+
+/* Passthrough codec implementation descriptions. */
+static struct codec_desc {
+    int		     enabled;		/* Is this codec enabled?	    */
+    const char	    *name;		/* Codec name.			    */
+    pj_uint8_t	     pt;		/* Payload type.		    */
+    pjmedia_fourcc   format;		/* Source format.		    */
+    unsigned	     clock_rate;	/* Codec's clock rate.		    */
+    unsigned	     channel_count;	/* Codec's channel count.	    */
+    unsigned	     samples_per_frame;	/* Codec's samples count.	    */
+    unsigned	     def_bitrate;	/* Default bitrate of this codec.   */
+    unsigned	     max_bitrate;	/* Maximum bitrate of this codec.   */
+    pj_uint8_t	     frm_per_pkt;	/* Default num of frames per packet.*/
+    parse_cb	     parse;		/* Callback to parse bitstream.	    */
+    pack_cb	     pack;		/* Callback to pack bitstream.	    */
+    pjmedia_codec_fmtp dec_fmtp;	/* Decoder's fmtp params.	    */
+}
+
+codec_desc[] = 
+{
+#   if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
+    {1, "AMR",	    PJMEDIA_RTP_PT_AMR,       PJMEDIA_FOURCC_AMR,
+		    8000, 1, 160, 
+		    5900, 12200, 4,
+		    &parse_amr, &pack_amr
+		    /*, {1, {{{"octet-align", 11}, {"1", 1}}} } */
+    },
+#   endif
+
+#   if PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
+    {1, "G729",	    PJMEDIA_RTP_PT_G729,      PJMEDIA_FOURCC_G729,
+		    8000, 1,  80,
+		    8000, 11800, 2
+    },
+#   endif
+
+#   if PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC
+    {1, "iLBC",	    PJMEDIA_RTP_PT_ILBC,      PJMEDIA_FOURCC_ILBC,
+		    8000, 1,  80,
+		    8000, 11800, 2,
+		    NULL, NULL,
+		    {1, {{{"mode", 4}, {"30", 2}}} }
+    },
+#   endif
+
+#   if PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU
+    {1, "PCMU",	    PJMEDIA_RTP_PT_PCMU,      PJMEDIA_FOURCC_G711U,
+		    8000, 1,  80,
+		    64000, 64000, 2
+    },
+#   endif
+
+#   if PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA
+    {1, "PCMA",	    PJMEDIA_RTP_PT_PCMA,      PJMEDIA_FOURCC_G711A,
+		    8000, 1,  80,
+		    64000, 64000, 2
+    },
+#   endif
+};
+
+#if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
+
+#include <pjmedia-codec/amr_helper.h>
+
+typedef struct amr_settings_t {
+    pjmedia_codec_amr_pack_setting enc_setting;
+    pjmedia_codec_amr_pack_setting dec_setting;
+    pj_int8_t enc_mode;
+} amr_settings_t;
+
+
+/* Pack AMR payload */
+static pj_status_t pack_amr ( codec_private_t *codec_data,
+			      const struct pjmedia_frame_ext *input,
+			      unsigned output_buf_len, 
+			      struct pjmedia_frame *output)
+{
+    enum {MAX_FRAMES_PER_PACKET = 16};
+
+    pjmedia_frame frames[MAX_FRAMES_PER_PACKET];
+    unsigned nframes = 0;
+    pjmedia_codec_amr_bit_info *info;
+
+    PJ_TODO(DEFINE_AMR_FRAME_INFO_FOR_PJMEDIA_FRAME_EXT);
+
+#if 0
+
+    pj_uint8_t *r; /* Read cursor */
+    pj_uint8_t SID_FT;
+    pjmedia_codec_amr_pack_setting *setting;
+
+    setting = &((amr_settings_t*)codec_data->codec_setting)->enc_setting;
+
+    SID_FT = (pj_uint8_t)(setting->amr_nb? 8 : 9);
+
+    /* Align pkt buf right */
+    r = (pj_uint8_t*)pkt + max_pkt_size - *pkt_size;
+    pj_memmove(r, pkt, *pkt_size);
+
+    /* Get frames */
+    for (;;) {
+	pj_bool_t eof;
+	pj_uint16_t info_;
+
+	info_ = *((pj_uint16_t*)r);
+	eof = ((info_ & 0x40) != 0);
+
+	info = (pjmedia_codec_amr_bit_info*) &frames[nframes].bit_info;
+	pj_bzero(info, sizeof(*info));
+	info->frame_type = (pj_uint8_t)(info_ & 0x0F);
+	info->good_quality = 1;
+	info->mode = (pj_int8_t) ((info_ >> 8) & 0x0F);
+
+	frames[nframes].buf = r + 2;
+	frames[nframes].size = info->frame_type <= SID_FT ?
+			       pjmedia_codec_amrnb_framelen[info->frame_type] :
+			       0;
+
+	r += frames[nframes].size + 2;
+
+	/* Last frame */
+	if (++nframes >= MAX_FRAMES_PER_PACKET || eof)
+	    break;
+    }
+
+    /* Pack */
+    *pkt_size = max_pkt_size;
+
+    return pjmedia_codec_amr_pack(frames, nframes, setting, pkt, pkt_size);
+#endif
+
+    return PJ_ENOTSUP;
+}
+
+
+/* Parse AMR payload into frames. */
+static pj_status_t parse_amr(codec_private_t *codec_data, void *pkt, 
+			     pj_size_t pkt_size, const pj_timestamp *ts,
+			     unsigned *frame_cnt, pjmedia_frame frames[])
+{
+    amr_settings_t* s = (amr_settings_t*)codec_data->codec_setting;
+    pjmedia_codec_amr_pack_setting *setting;
+    pj_status_t status;
+    pj_uint8_t cmr;
+
+    setting = &s->dec_setting;
+
+    status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, setting, frames, 
+				     frame_cnt, &cmr);
+    if (status != PJ_SUCCESS)
+	return status;
+
+    /* Check Change Mode Request. */
+    if ((setting->amr_nb && cmr <= 7) || (!setting->amr_nb && cmr <= 8)) {
+	s->enc_mode = cmr;
+    }
+
+    return PJ_SUCCESS;
+}
+
+#endif /* PJMEDIA_HAS_PASSTROUGH_CODEC_AMR */
+
+
+/*
+ * Initialize and register passthrough codec factory to pjmedia endpoint.
+ */
+PJ_DEF(pj_status_t) pjmedia_codec_passthrough_init( pjmedia_endpt *endpt )
+{
+    pjmedia_codec_mgr *codec_mgr;
+    pj_status_t status;
+
+    if (codec_factory.pool != NULL) {
+	/* Already initialized. */
+	return PJ_SUCCESS;
+    }
+
+    /* Create passthrough codec factory. */
+    codec_factory.base.op = &codec_factory_op;
+    codec_factory.base.factory_data = NULL;
+    codec_factory.endpt = endpt;
+
+    codec_factory.pool = pjmedia_endpt_create_pool(endpt, "Passthrough codecs",
+						   4000, 4000);
+    if (!codec_factory.pool)
+	return PJ_ENOMEM;
+
+    /* Create mutex. */
+    status = pj_mutex_create_simple(codec_factory.pool, "Passthrough codecs",
+				    &codec_factory.mutex);
+    if (status != PJ_SUCCESS)
+	goto on_error;
+
+    /* Get the codec manager. */
+    codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
+    if (!codec_mgr) {
+	status = PJ_EINVALIDOP;
+	goto on_error;
+    }
+
+    /* Register codec factory to endpoint. */
+    status = pjmedia_codec_mgr_register_factory(codec_mgr, 
+						&codec_factory.base);
+    if (status != PJ_SUCCESS)
+	goto on_error;
+
+    /* Done. */
+    return PJ_SUCCESS;
+
+on_error:
+    pj_pool_release(codec_factory.pool);
+    codec_factory.pool = NULL;
+    return status;
+}
+
+/*
+ * Unregister passthrough codecs factory from pjmedia endpoint.
+ */
+PJ_DEF(pj_status_t) pjmedia_codec_passthrough_deinit(void)
+{
+    pjmedia_codec_mgr *codec_mgr;
+    pj_status_t status;
+
+    if (codec_factory.pool == NULL) {
+	/* Already deinitialized */
+	return PJ_SUCCESS;
+    }
+
+    pj_mutex_lock(codec_factory.mutex);
+
+    /* Get the codec manager. */
+    codec_mgr = pjmedia_endpt_get_codec_mgr(codec_factory.endpt);
+    if (!codec_mgr) {
+	pj_pool_release(codec_factory.pool);
+	codec_factory.pool = NULL;
+	return PJ_EINVALIDOP;
+    }
+
+    /* Unregister passthrough codecs factory. */
+    status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
+						  &codec_factory.base);
+    
+    /* Destroy mutex. */
+    pj_mutex_destroy(codec_factory.mutex);
+
+    /* Destroy pool. */
+    pj_pool_release(codec_factory.pool);
+    codec_factory.pool = NULL;
+
+    return status;
+}
+
+/* 
+ * Check if factory can allocate the specified codec. 
+ */
+static pj_status_t test_alloc( pjmedia_codec_factory *factory, 
+			       const pjmedia_codec_info *info )
+{
+    unsigned i;
+
+    PJ_UNUSED_ARG(factory);
+
+    /* Type MUST be audio. */
+    if (info->type != PJMEDIA_TYPE_AUDIO)
+	return PJMEDIA_CODEC_EUNSUP;
+
+    for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
+	pj_str_t name = pj_str((char*)codec_desc[i].name);
+	if ((pj_stricmp(&info->encoding_name, &name) == 0) &&
+	    (info->clock_rate == (unsigned)codec_desc[i].clock_rate) &&
+	    (info->channel_cnt == (unsigned)codec_desc[i].channel_count) &&
+	    (codec_desc[i].enabled))
+	{
+	    return PJ_SUCCESS;
+	}
+    }
+    
+    /* Unsupported, or mode is disabled. */
+    return PJMEDIA_CODEC_EUNSUP;
+}
+
+/*
+ * Generate default attribute.
+ */
+static pj_status_t default_attr ( pjmedia_codec_factory *factory, 
+				  const pjmedia_codec_info *id, 
+				  pjmedia_codec_param *attr )
+{
+    unsigned i;
+
+    PJ_ASSERT_RETURN(factory==&codec_factory.base, PJ_EINVAL);
+
+    pj_bzero(attr, sizeof(pjmedia_codec_param));
+
+    for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
+	pj_str_t name = pj_str((char*)codec_desc[i].name);
+	if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
+	    (id->clock_rate == (unsigned)codec_desc[i].clock_rate) &&
+	    (id->channel_cnt == (unsigned)codec_desc[i].channel_count) &&
+	    (id->pt == (unsigned)codec_desc[i].pt))
+	{
+	    attr->info.pt = (pj_uint8_t)id->pt;
+	    attr->info.channel_cnt = codec_desc[i].channel_count;
+	    attr->info.clock_rate = codec_desc[i].clock_rate;
+	    attr->info.avg_bps = codec_desc[i].def_bitrate;
+	    attr->info.max_bps = codec_desc[i].max_bitrate;
+	    attr->info.pcm_bits_per_sample = 16;
+	    attr->info.frm_ptime =  (pj_uint16_t)
+				    (codec_desc[i].samples_per_frame * 1000 / 
+				    codec_desc[i].channel_count / 
+				    codec_desc[i].clock_rate);
+	    attr->info.format.u32 = codec_desc[i].format.u32;
+
+	    /* Default flags. */
+	    attr->setting.frm_per_pkt = codec_desc[i].frm_per_pkt;
+	    attr->setting.plc = 1;
+	    attr->setting.penh= 0;
+	    attr->setting.vad = 1;
+	    attr->setting.cng = attr->setting.vad;
+	    attr->setting.dec_fmtp = codec_desc[i].dec_fmtp;
+
+	    if (attr->setting.vad == 0) {
+#if PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
+		if (id->pt == PJMEDIA_RTP_PT_G729) {
+		    /* Signal G729 Annex B is being disabled */
+		    attr->setting.dec_fmtp.cnt = 1;
+		    pj_strset2(&attr->setting.dec_fmtp.param[0].name, "annexb");
+		    pj_strset2(&attr->setting.dec_fmtp.param[0].val, "no");
+		}
+#endif
+	    }
+
+	    return PJ_SUCCESS;
+	}
+    }
+
+    return PJMEDIA_CODEC_EUNSUP;
+}
+
+/*
+ * Enum codecs supported by this factory.
+ */
+static pj_status_t enum_codecs( pjmedia_codec_factory *factory, 
+				unsigned *count, 
+				pjmedia_codec_info codecs[])
+{
+    unsigned max;
+    unsigned i;
+
+    PJ_UNUSED_ARG(factory);
+    PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
+
+    max = *count;
+    
+    for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(codec_desc) && *count < max; ++i) 
+    {
+	if (!codec_desc[i].enabled)
+	    continue;
+
+	pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
+	codecs[*count].encoding_name = pj_str((char*)codec_desc[i].name);
+	codecs[*count].pt = codec_desc[i].pt;
+	codecs[*count].type = PJMEDIA_TYPE_AUDIO;
+	codecs[*count].clock_rate = codec_desc[i].clock_rate;
+	codecs[*count].channel_cnt = codec_desc[i].channel_count;
+
+	++*count;
+    }
+
+    return PJ_SUCCESS;
+}
+
+/*
+ * Allocate a new codec instance.
+ */
+static pj_status_t alloc_codec( pjmedia_codec_factory *factory, 
+				const pjmedia_codec_info *id,
+				pjmedia_codec **p_codec)
+{
+    codec_private_t *codec_data;
+    pjmedia_codec *codec;
+    int idx;
+    pj_pool_t *pool;
+    unsigned i;
+
+    PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
+    PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL);
+
+    pj_mutex_lock(codec_factory.mutex);
+
+    /* Find codec's index */
+    idx = -1;
+    for (i = 0; i < PJ_ARRAY_SIZE(codec_desc); ++i) {
+	pj_str_t name = pj_str((char*)codec_desc[i].name);
+	if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
+	    (id->clock_rate == (unsigned)codec_desc[i].clock_rate) &&
+	    (id->channel_cnt == (unsigned)codec_desc[i].channel_count) &&
+	    (codec_desc[i].enabled))
+	{
+	    idx = i;
+	    break;
+	}
+    }
+    if (idx == -1) {
+	*p_codec = NULL;
+	return PJMEDIA_CODEC_EUNSUP;
+    }
+
+    /* Create pool for codec instance */
+    pool = pjmedia_endpt_create_pool(codec_factory.endpt, "passthroughcodec",
+				     512, 512);
+    codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
+    codec->op = &codec_op;
+    codec->factory = factory;
+    codec->codec_data = PJ_POOL_ZALLOC_T(pool, codec_private_t);
+    codec_data = (codec_private_t*) codec->codec_data;
+    codec_data->pool = pool;
+    codec_data->codec_idx = idx;
+
+    pj_mutex_unlock(codec_factory.mutex);
+
+    *p_codec = codec;
+    return PJ_SUCCESS;
+}
+
+/*
+ * Free codec.
+ */
+static pj_status_t dealloc_codec( pjmedia_codec_factory *factory, 
+				  pjmedia_codec *codec )
+{
+    codec_private_t *codec_data;
+
+    PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
+    PJ_ASSERT_RETURN(factory == &codec_factory.base, PJ_EINVAL);
+
+    /* Close codec, if it's not closed. */
+    codec_data = (codec_private_t*) codec->codec_data;
+    codec_close(codec);
+
+    pj_pool_release(codec_data->pool);
+
+    return PJ_SUCCESS;
+}
+
+/*
+ * Init codec.
+ */
+static pj_status_t codec_init( pjmedia_codec *codec, 
+			       pj_pool_t *pool )
+{
+    PJ_UNUSED_ARG(codec);
+    PJ_UNUSED_ARG(pool);
+    return PJ_SUCCESS;
+}
+
+/*
+ * Open codec.
+ */
+static pj_status_t codec_open( pjmedia_codec *codec, 
+			       pjmedia_codec_param *attr )
+{
+    codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
+    struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
+    pj_pool_t *pool;
+    int i, j;
+
+    pool = codec_data->pool;
+
+    /* Get bitstream size */
+    i = attr->info.avg_bps * desc->samples_per_frame;
+    j = desc->clock_rate << 3;
+    codec_data->avg_frame_size = (pj_uint16_t)(i / j);
+    if (i % j) ++codec_data->avg_frame_size;
+
+#if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
+    /* Init AMR settings */
+    if (desc->pt == PJMEDIA_RTP_PT_AMR || desc->pt == PJMEDIA_RTP_PT_AMRWB) {
+	amr_settings_t *s;
+	pj_uint8_t octet_align = 0;
+	const pj_str_t STR_FMTP_OCTET_ALIGN = {"octet-align", 11};
+
+	/* Fetch octet-align setting. It should be fine to fetch only 
+	 * the decoder, since encoder & decoder must use the same setting 
+	 * (RFC 4867 section 8.3.1).
+	 */
+	for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) {
+	    if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name, 
+			   &STR_FMTP_OCTET_ALIGN) == 0)
+	    {
+		octet_align=(pj_uint8_t)
+			    (pj_strtoul(&attr->setting.dec_fmtp.param[i].val));
+		break;
+	    }
+	}
+
+	s = PJ_POOL_ZALLOC_T(pool, amr_settings_t);
+	codec_data->codec_setting = s;
+
+	s->enc_mode = pjmedia_codec_amr_get_mode(desc->def_bitrate);
+	if (s->enc_mode < 0)
+	    return PJMEDIA_CODEC_EINMODE;
+
+	s->enc_setting.amr_nb = desc->pt == PJMEDIA_RTP_PT_AMR;
+	s->enc_setting.octet_aligned = octet_align;
+	s->enc_setting.reorder = PJ_FALSE; /* Note this! passthrough codec
+					      doesn't do sensitivity bits 
+					      reordering */
+	s->enc_setting.cmr = 15;
+	
+	s->dec_setting.amr_nb = desc->pt == PJMEDIA_RTP_PT_AMR;
+	s->dec_setting.octet_aligned = octet_align;
+	s->dec_setting.reorder = PJ_FALSE; /* Note this! passthrough codec
+					      doesn't do sensitivity bits 
+					      reordering */
+    }
+#endif
+
+    return PJ_SUCCESS;
+}
+
+/*
+ * Close codec.
+ */
+static pj_status_t codec_close( pjmedia_codec *codec )
+{
+    PJ_UNUSED_ARG(codec);
+
+    return PJ_SUCCESS;
+}
+
+
+/*
+ * Modify codec settings.
+ */
+static pj_status_t codec_modify( pjmedia_codec *codec, 
+				 const pjmedia_codec_param *attr )
+{
+    /* Not supported yet. */
+    PJ_UNUSED_ARG(codec);
+    PJ_UNUSED_ARG(attr);
+
+    return PJ_ENOTSUP;
+}
+
+/*
+ * Get frames in the packet.
+ */
+static pj_status_t codec_parse( pjmedia_codec *codec,
+				void *pkt,
+				pj_size_t pkt_size,
+				const pj_timestamp *ts,
+				unsigned *frame_cnt,
+				pjmedia_frame frames[])
+{
+    codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
+    struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
+    unsigned count = 0;
+
+    PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
+
+    if (desc->parse != NULL) {
+	return desc->parse(codec_data, pkt,  pkt_size, ts, frame_cnt, frames);
+    }
+
+    while (pkt_size >= codec_data->avg_frame_size && count < *frame_cnt) {
+	frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
+	frames[count].buf = pkt;
+	frames[count].size = codec_data->avg_frame_size;
+	frames[count].timestamp.u64 = ts->u64 + count*desc->samples_per_frame;
+
+	pkt = ((char*)pkt) + codec_data->avg_frame_size;
+	pkt_size -= codec_data->avg_frame_size;
+
+	++count;
+    }
+
+    if (pkt_size && count < *frame_cnt) {
+	frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
+	frames[count].buf = pkt;
+	frames[count].size = pkt_size;
+	frames[count].timestamp.u64 = ts->u64 + count*desc->samples_per_frame;
+	++count;
+    }
+
+    *frame_cnt = count;
+    return PJ_SUCCESS;
+}
+
+/*
+ * Encode frames.
+ */
+static pj_status_t codec_encode( pjmedia_codec *codec, 
+				 const struct pjmedia_frame *input,
+				 unsigned output_buf_len, 
+				 struct pjmedia_frame *output)
+{
+    codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
+    struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
+    const pjmedia_frame_ext *input_ = (const pjmedia_frame_ext*) input;
+
+    pj_assert(input && input->type == PJMEDIA_FRAME_TYPE_EXTENDED);
+
+    if (desc->pack != NULL) {
+	desc->pack(codec_data, input_, output_buf_len, output);
+    } else {
+	if (input_->subframe_cnt == 0) {
+	    /* DTX */
+	    output->buf = NULL;
+	    output->size = 0;
+	    output->type = PJMEDIA_FRAME_TYPE_NONE;
+	} else {
+	    unsigned i;
+	    pj_uint8_t *p = output->buf;
+
+	    output->type = PJMEDIA_FRAME_TYPE_AUDIO;
+	    output->size = 0;
+	    
+	    for (i = 0; i < input_->subframe_cnt; ++i) {
+		pjmedia_frame_ext_subframe *sf;
+		unsigned sf_len;
+
+		sf = pjmedia_frame_ext_get_subframe(input_, i);
+		pj_assert(sf);
+		
+		sf_len = sf->bitlen >> 3;
+		if (sf->bitlen & 0x07)
+		    ++sf_len;
+
+		pj_memcpy(p, sf->data, sf_len);
+		p += sf_len;
+		output->size += sf_len;
+	    }
+	}
+    }
+
+    return PJ_SUCCESS;
+}
+
+/*
+ * Decode frame.
+ */
+static pj_status_t codec_decode( pjmedia_codec *codec, 
+				 const struct pjmedia_frame *input,
+				 unsigned output_buf_len, 
+				 struct pjmedia_frame *output)
+{
+    codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
+    struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
+    pjmedia_frame_ext *output_ = (pjmedia_frame_ext*) output;
+
+    /* Check if input is formatted in pjmedia_frame */
+    pj_assert(input && input->type == PJMEDIA_FRAME_TYPE_AUDIO);
+
+#if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
+    /* Need to rearrange the AMR bitstream, since the bitstream may not be 
+     * started from bit 0 or may need to be reordered from sensitivity order 
+     * into encoder bits order.
+     */
+    if (desc->pt == PJMEDIA_RTP_PT_AMR) {
+	pjmedia_frame frame;
+	pjmedia_codec_amr_pack_setting *setting;
+
+	setting = &((amr_settings_t*)codec_data->codec_setting)->dec_setting;
+
+	frame = *input;
+	pjmedia_codec_amr_predecode(input, setting, &frame);
+    }
+#endif
+
+    pj_bzero(output_, sizeof(pjmedia_frame_ext));
+    output_->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
+    
+    if (input && input->size > 0) {
+	PJ_ASSERT_RETURN(output_buf_len >= sizeof(pjmedia_frame_ext) +
+					   sizeof(pjmedia_frame_ext_subframe) +
+					   input->size,
+			 PJMEDIA_CODEC_EFRMTOOSHORT);
+
+	pjmedia_frame_ext_append_subframe(output_, input->buf, 
+					  (pj_uint16_t)(input->size << 3),
+					  (pj_uint16_t)desc->samples_per_frame);
+    }
+
+    return PJ_SUCCESS;
+}
+
+/* 
+ * Recover lost frame.
+ */
+static pj_status_t codec_recover( pjmedia_codec *codec, 
+				  unsigned output_buf_len, 
+				  struct pjmedia_frame *output)
+{
+    return codec_decode(codec, NULL, output_buf_len, output);
+}
+
+#endif	/* PJMEDIA_HAS_PASSTHROUGH_CODECS */
+
