media formats: re-architect handling of media for performance improvements
authorMatthew Jordan <mjordan@digium.com>
Sun, 20 Jul 2014 22:06:33 +0000 (22:06 +0000)
committerMatthew Jordan <mjordan@digium.com>
Sun, 20 Jul 2014 22:06:33 +0000 (22:06 +0000)
In the old times media formats were represented using a bit field. This was
fast but had a few limitations.
 1. Asterisk was limited in how many formats it could handle.
 2. Formats, being a bit field, could not include any attribute information.
    A format was strictly its type, e.g., "this is ulaw".
This was changed in Asterisk 10 (see
https://wiki.asterisk.org/wiki/display/AST/Media+Architecture+Proposal for
notes on that work) which led to the creation of the ast_format structure.
This structure allowed Asterisk to handle attributes and bundle information
with a format.

Additionally, ast_format_cap was created to act as a container for multiple
formats that, together, formed the capability of some entity. Another
mechanism was added to allow logic to be registered which performed format
attribute negotiation. Everywhere throughout the codebase Asterisk was
changed to use this strategy.

Unfortunately, in software, there is no free lunch. These new capabilities
came at a cost.

Performance analysis and profiling showed that we spend an inordinate
amount of time comparing, copying, and generally manipulating formats and
their related structures. Basic prototyping has shown that a reasonably
large performance improvement could be made in this area. This patch is the
result of that project, which overhauled the media format architecture
and its usage in Asterisk to improve performance.

Generally, the new philosophy for handling formats is as follows:
 * The ast_format structure is reference counted. This removed a large amount
   of the memory allocations and copying that was done in prior versions.
 * In order to prevent race conditions while keeping things performant, the
   ast_format structure is immutable by convention and lock-free. Violate this
   tenet at your peril!
 * Because formats are reference counted, codecs are also reference counted.
   The Asterisk core generally provides built-in codecs and caches the
   ast_format structures created to represent them. Generally, to prevent
   inordinate amounts of module reference bumping, codecs and formats can be
   added at run-time but cannot be removed.
 * All compatibility with the bit field representation of codecs/formats has
   been moved to a compatibility API. The primary user of this representation
   is chan_iax2, which must continue to maintain its bit-field usage of formats
   for interoperability concerns.
 * When a format is negotiated with attributes, or when a format cannot be
   represented by one of the cached formats, a new format object is created or
   cloned from an existing format. That format may have the same codec
   underlying it, but is a different format than a version of the format with
   different attributes or without attributes.
 * While formats are reference counted objects, the reference count maintained
   on the format should be manipulated with care. Formats are generally cached
   and will persist for the lifetime of Asterisk and do not explicitly need
   to have their lifetime modified. An exception to this is when the user of a
   format does not know where the format came from *and* the user may outlive
   the provider of the format. This occurs, for example, when a format is read
   from a channel: the channel may have a format with attributes (hence,
   non-cached) and the user of the format may last longer than the channel (if
   the reference to the channel is released prior to the format's reference).

For more information on this work, see the API design notes:
  https://wiki.asterisk.org/wiki/display/AST/Media+Format+Rewrite

Finally, this work was the culmination of a large number of developer's
efforts. Extra thanks goes to Corey Farrell, who took on a large amount of the
work in the Asterisk core, chan_sip, and was an invaluable resource in peer
reviews throughout this project.

There were a substantial number of patches contributed during this work; the
following issues/patch names simply reflect some of the work (and will cause
the release scripts to give attribution to the individuals who work on them).

Reviews:
 https://reviewboard.asterisk.org/r/3814
 https://reviewboard.asterisk.org/r/3808
 https://reviewboard.asterisk.org/r/3805
 https://reviewboard.asterisk.org/r/3803
 https://reviewboard.asterisk.org/r/3801
 https://reviewboard.asterisk.org/r/3798
 https://reviewboard.asterisk.org/r/3800
 https://reviewboard.asterisk.org/r/3794
 https://reviewboard.asterisk.org/r/3793
 https://reviewboard.asterisk.org/r/3792
 https://reviewboard.asterisk.org/r/3791
 https://reviewboard.asterisk.org/r/3790
 https://reviewboard.asterisk.org/r/3789
 https://reviewboard.asterisk.org/r/3788
 https://reviewboard.asterisk.org/r/3787
 https://reviewboard.asterisk.org/r/3786
 https://reviewboard.asterisk.org/r/3784
 https://reviewboard.asterisk.org/r/3783
 https://reviewboard.asterisk.org/r/3778
 https://reviewboard.asterisk.org/r/3774
 https://reviewboard.asterisk.org/r/3775
 https://reviewboard.asterisk.org/r/3772
 https://reviewboard.asterisk.org/r/3761
 https://reviewboard.asterisk.org/r/3754
 https://reviewboard.asterisk.org/r/3753
 https://reviewboard.asterisk.org/r/3751
 https://reviewboard.asterisk.org/r/3750
 https://reviewboard.asterisk.org/r/3748
 https://reviewboard.asterisk.org/r/3747
 https://reviewboard.asterisk.org/r/3746
 https://reviewboard.asterisk.org/r/3742
 https://reviewboard.asterisk.org/r/3740
 https://reviewboard.asterisk.org/r/3739
 https://reviewboard.asterisk.org/r/3738
 https://reviewboard.asterisk.org/r/3737
 https://reviewboard.asterisk.org/r/3736
 https://reviewboard.asterisk.org/r/3734
 https://reviewboard.asterisk.org/r/3722
 https://reviewboard.asterisk.org/r/3713
 https://reviewboard.asterisk.org/r/3703
 https://reviewboard.asterisk.org/r/3689
 https://reviewboard.asterisk.org/r/3687
 https://reviewboard.asterisk.org/r/3674
 https://reviewboard.asterisk.org/r/3671
 https://reviewboard.asterisk.org/r/3667
 https://reviewboard.asterisk.org/r/3665
 https://reviewboard.asterisk.org/r/3625
 https://reviewboard.asterisk.org/r/3602
 https://reviewboard.asterisk.org/r/3519
 https://reviewboard.asterisk.org/r/3518
 https://reviewboard.asterisk.org/r/3516
 https://reviewboard.asterisk.org/r/3515
 https://reviewboard.asterisk.org/r/3512
 https://reviewboard.asterisk.org/r/3506
 https://reviewboard.asterisk.org/r/3413
 https://reviewboard.asterisk.org/r/3410
 https://reviewboard.asterisk.org/r/3387
 https://reviewboard.asterisk.org/r/3388
 https://reviewboard.asterisk.org/r/3389
 https://reviewboard.asterisk.org/r/3390
 https://reviewboard.asterisk.org/r/3321
 https://reviewboard.asterisk.org/r/3320
 https://reviewboard.asterisk.org/r/3319
 https://reviewboard.asterisk.org/r/3318
 https://reviewboard.asterisk.org/r/3266
 https://reviewboard.asterisk.org/r/3265
 https://reviewboard.asterisk.org/r/3234
 https://reviewboard.asterisk.org/r/3178

ASTERISK-23114 #close
Reported by: mjordan
  media_formats_translation_core.diff uploaded by kharwell (License 6464)
  rb3506.diff uploaded by mjordan (License 6283)
  media_format_app_file.diff uploaded by kharwell (License 6464)
  misc-2.diff uploaded by file (License 5000)
  chan_mild-3.diff uploaded by file (License 5000)
  chan_obscure.diff uploaded by file (License 5000)
  jingle.diff uploaded by file (License 5000)
  funcs.diff uploaded by file (License 5000)
  formats.diff uploaded by file (License 5000)
  core.diff uploaded by file (License 5000)
  bridges.diff uploaded by file (License 5000)
  mf-codecs-2.diff uploaded by file (License 5000)
  mf-app_fax.diff uploaded by file (License 5000)
  mf-apps-3.diff uploaded by file (License 5000)
  media-formats-3.diff uploaded by file (License 5000)

ASTERISK-23715
  rb3713.patch uploaded by coreyfarrell (License 5909)
  rb3689.patch uploaded by mjordan (License 6283)

ASTERISK-23957
  rb3722.patch uploaded by mjordan (License 6283)
  mf-attributes-3.diff uploaded by file (License 5000)

ASTERISK-23958
Tested by: jrose
  rb3822.patch uploaded by coreyfarrell (License 5909)
  rb3800.patch uploaded by jrose (License 6182)
  chan_sip.diff uploaded by mjordan (License 6283)
  rb3747.patch uploaded by jrose (License 6182)

ASTERISK-23959 #close
Tested by: sgriepentrog, mjordan, coreyfarrell
  sip_cleanup.diff uploaded by opticron (License 6273)
  chan_sip_caps.diff uploaded by mjordan (License 6283)
  rb3751.patch uploaded by coreyfarrell (License 5909)
  chan_sip-3.diff uploaded by file (License 5000)

ASTERISK-23960 #close
Tested by: opticron
  direct_media.diff uploaded by opticron (License 6273)
  pjsip-direct-media.diff uploaded by file (License 5000)
  format_cap_remove.diff uploaded by opticron (License 6273)
  media_format_fixes.diff uploaded by opticron (License 6273)
  chan_pjsip-2.diff uploaded by file (License 5000)

ASTERISK-23966 #close
Tested by: rmudgett
  rb3803.patch uploaded by rmudgetti (License 5621)
  chan_dahdi.diff uploaded by file (License 5000)

ASTERISK-24064 #close
Tested by: coreyfarrell, mjordan, opticron, file, rmudgett, sgriepentrog, jrose
  rb3814.patch uploaded by rmudgett (License 5621)
  moh_cleanup.diff uploaded by opticron (License 6273)
  bridge_leak.diff uploaded by opticron (License 6273)
  translate.diff uploaded by file (License 5000)
  rb3795.patch uploaded by rmudgett (License 5621)
  tls_fix.diff uploaded by mjordan (License 6283)
  fax-mf-fix-2.diff uploaded by file (License 5000)
  rtp_transfer_stuff uploaded by mjordan (License 6283)
  rb3787.patch uploaded by rmudgett (License 5621)
  media-formats-explicit-translate-format-3.diff uploaded by file (License 5000)
  format_cache_case_fix.diff uploaded by opticron (License 6273)
  rb3774.patch uploaded by rmudgett (License 5621)
  rb3775.patch uploaded by rmudgett (License 5621)
  rtp_engine_fix.diff uploaded by opticron (License 6273)
  rtp_crash_fix.diff uploaded by opticron (License 6273)
  rb3753.patch uploaded by mjordan (License 6283)
  rb3750.patch uploaded by mjordan (License 6283)
  rb3748.patch uploaded by rmudgett (License 5621)
  media_format_fixes.diff uploaded by opticron (License 6273)
  rb3740.patch uploaded by mjordan (License 6283)
  rb3739.patch uploaded by mjordan (License 6283)
  rb3734.patch uploaded by mjordan (License 6283)
  rb3689.patch uploaded by mjordan (License 6283)
  rb3674.patch uploaded by coreyfarrell (License 5909)
  rb3671.patch uploaded by coreyfarrell (License 5909)
  rb3667.patch uploaded by coreyfarrell (License 5909)
  rb3665.patch uploaded by mjordan (License 6283)
  rb3625.patch uploaded by coreyfarrell (License 5909)
  rb3602.patch uploaded by coreyfarrell (License 5909)
  format_compatibility-2.diff uploaded by file (License 5000)
  core.diff uploaded by file (License 5000)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419044 65c4cc65-6c06-0410-ace0-fbb531ad65f3

205 files changed:
UPGRADE.txt
addons/chan_mobile.c
addons/chan_ooh323.c
addons/chan_ooh323.h
addons/format_mp3.c
addons/ooh323cDriver.c
addons/ooh323cDriver.h
apps/app_agent_pool.c
apps/app_alarmreceiver.c
apps/app_amd.c
apps/app_chanspy.c
apps/app_confbridge.c
apps/app_dictate.c
apps/app_dumpchan.c
apps/app_echo.c
apps/app_fax.c
apps/app_festival.c
apps/app_ices.c
apps/app_jack.c
apps/app_meetme.c
apps/app_milliwatt.c
apps/app_mixmonitor.c
apps/app_mp3.c
apps/app_nbscat.c
apps/app_originate.c
apps/app_record.c
apps/app_sms.c
apps/app_speech_utils.c
apps/app_talkdetect.c
apps/app_test.c
apps/app_voicemail.c
apps/app_waitforsilence.c
apps/confbridge/conf_chan_record.c
bridges/bridge_holding.c
bridges/bridge_native_rtp.c
bridges/bridge_simple.c
bridges/bridge_softmix.c
channels/chan_alsa.c
channels/chan_bridge_media.c
channels/chan_console.c
channels/chan_dahdi.c
channels/chan_iax2.c
channels/chan_mgcp.c
channels/chan_misdn.c
channels/chan_motif.c
channels/chan_multicast_rtp.c
channels/chan_nbs.c
channels/chan_oss.c
channels/chan_phone.c
channels/chan_pjsip.c
channels/chan_sip.c
channels/chan_skinny.c
channels/chan_unistim.c
channels/chan_vpb.cc
channels/dahdi/bridge_native_dahdi.c
channels/iax2/codec_pref.c [new file with mode: 0644]
channels/iax2/format_compatibility.c [new file with mode: 0644]
channels/iax2/include/codec_pref.h [new file with mode: 0644]
channels/iax2/include/format_compatibility.h [new file with mode: 0644]
channels/iax2/parser.c
channels/iax2/provision.c
channels/pjsip/dialplan_functions.c
channels/sip/include/sip.h
codecs/codec_a_mu.c
codecs/codec_adpcm.c
codecs/codec_alaw.c
codecs/codec_dahdi.c
codecs/codec_g722.c
codecs/codec_g726.c
codecs/codec_gsm.c
codecs/codec_ilbc.c
codecs/codec_lpc10.c
codecs/codec_resample.c
codecs/codec_speex.c
codecs/codec_ulaw.c
codecs/ex_adpcm.h
codecs/ex_alaw.h
codecs/ex_g722.h
codecs/ex_g726.h
codecs/ex_gsm.h
codecs/ex_ilbc.h
codecs/ex_lpc10.h
codecs/ex_speex.h
codecs/ex_ulaw.h
formats/format_g719.c
formats/format_g723.c
formats/format_g726.c
formats/format_g729.c
formats/format_gsm.c
formats/format_h263.c
formats/format_h264.c
formats/format_ilbc.c
formats/format_jpeg.c
formats/format_ogg_vorbis.c
formats/format_pcm.c
formats/format_siren14.c
formats/format_siren7.c
formats/format_sln.c
formats/format_vox.c
formats/format_wav.c
formats/format_wav_gsm.c
funcs/func_channel.c
funcs/func_frame_trace.c
funcs/func_pitchshift.c
funcs/func_speex.c
funcs/func_talkdetect.c
include/asterisk/_private.h
include/asterisk/abstract_jb.h
include/asterisk/audiohook.h
include/asterisk/bridge_channel.h
include/asterisk/callerid.h
include/asterisk/channel.h
include/asterisk/codec.h [new file with mode: 0644]
include/asterisk/config_options.h
include/asterisk/data.h
include/asterisk/file.h
include/asterisk/format.h
include/asterisk/format_cache.h [new file with mode: 0644]
include/asterisk/format_cap.h
include/asterisk/format_compatibility.h [new file with mode: 0644]
include/asterisk/format_pref.h [deleted file]
include/asterisk/frame.h
include/asterisk/image.h
include/asterisk/mod_format.h
include/asterisk/res_pjsip.h
include/asterisk/res_pjsip_session.h
include/asterisk/rtp_engine.h
include/asterisk/slin.h
include/asterisk/slinfactory.h
include/asterisk/smoother.h [new file with mode: 0644]
include/asterisk/speech.h
include/asterisk/translate.h
include/asterisk/vector.h
main/abstract_jb.c
main/app.c
main/asterisk.c
main/audiohook.c
main/bridge.c
main/bridge_basic.c
main/bridge_channel.c
main/callerid.c
main/ccss.c
main/channel.c
main/channel_internal_api.c
main/cli.c
main/codec.c [new file with mode: 0644]
main/codec_builtin.c [new file with mode: 0644]
main/config_options.c
main/core_local.c
main/core_unreal.c
main/data.c
main/dial.c
main/dsp.c
main/file.c
main/format.c
main/format_cache.c [new file with mode: 0644]
main/format_cap.c
main/format_compatibility.c [new file with mode: 0644]
main/format_pref.c [deleted file]
main/frame.c
main/image.c
main/indications.c
main/manager.c
main/media_index.c
main/rtp_engine.c
main/slinfactory.c
main/smoother.c [new file with mode: 0644]
main/sorcery.c
main/sounds_index.c
main/translate.c
main/utils.c
pbx/pbx_spool.c
res/ari/resource_bridges.c
res/ari/resource_channels.c
res/ari/resource_sounds.c
res/parking/parking_applications.c
res/res_adsi.c
res/res_agi.c
res/res_calendar.c
res/res_clioriginate.c
res/res_fax.c
res/res_fax_spandsp.c
res/res_format_attr_celt.c
res/res_format_attr_h263.c
res/res_format_attr_h264.c
res/res_format_attr_opus.c
res/res_format_attr_silk.c
res/res_musiconhold.c
res/res_pjsip/pjsip_configuration.c
res/res_pjsip_sdp_rtp.c
res/res_pjsip_session.c
res/res_rtp_asterisk.c
res/res_rtp_multicast.c
res/res_speech.c
res/res_stasis.c
res/res_stasis_snoop.c
tests/test_abstract_jb.c
tests/test_cel.c
tests/test_config.c
tests/test_core_codec.c [new file with mode: 0644]
tests/test_core_format.c [new file with mode: 0644]
tests/test_format_api.c [deleted file]
tests/test_format_cache.c [new file with mode: 0644]
tests/test_format_cap.c [new file with mode: 0644]
tests/test_voicemail_api.c

index 84b3e8d..535782a 100644 (file)
@@ -208,6 +208,32 @@ chan_sip:
    progress indications into a 180 Ringing (if a 180 has not yet been
    transmitted) if 'progressinband=never'.
 
+  - The codec preference order in an SDP during an offer is slightly different
+    than previous releases. Prior to Asterisk 13, the preference order of
+    codecs used to be:
+    (1) Our preferred codec
+    (2) Our configured codecs
+    (3) Any non-audio joint codecs
+
+    One of the ways the new media format architecture in Asterisk 13 improves
+    performance is by reference counting formats, such that they can be reused
+    in many places without additional allocation. To not require a large
+    amount of locking, an instance of a format is immutable by convention.
+    This works well except for formats with attributes. Since a media format
+    with an attribute is a different object than the same format without an
+    attribute, we have to carry over the formats with attributes from an
+    inbound offer so that the correct attributes are offered in an outgoing
+    INVITE request. This requires some subtle tweaks to the preference order
+    to ensure that the media format with attributes is offered to a remote
+    peer, as opposed to the same media format (but without attributes) that
+    may be stored in the peer object.
+
+    All of this means that our offer offer list will now be:
+    (1) Our preferred codec
+    (2) Any joint codecs offered by the inbound offer
+    (3) All other codecs that are not the preferred codec and not a joint
+        codec offered by the inbound offer
+
 CLI commands:
  - "core show settings" now lists the current console verbosity in addition
    to the root console verbosity.
index a7cbc00..75db0fa 100644 (file)
@@ -73,16 +73,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/app.h"
 #include "asterisk/manager.h"
 #include "asterisk/io.h"
+#include "asterisk/smoother.h"
+#include "asterisk/format_cache.h"
 
 #define MBL_CONFIG "chan_mobile.conf"
 #define MBL_CONFIG_OLD "mobile.conf"
 
 #define DEVICE_FRAME_SIZE 48
-#define DEVICE_FRAME_FORMAT AST_FORMAT_SLINEAR
+#define DEVICE_FRAME_FORMAT ast_format_slin
 #define CHANNEL_FRAME_SIZE 320
 
-static struct ast_format prefformat;
-
 static int discovery_interval = 60;                    /* The device discovery interval, default 60 seconds. */
 static pthread_t discovery_thread = AST_PTHREADT_NULL; /* The discovery thread */
 static sdp_session_t *sdp_session;
@@ -840,7 +840,6 @@ e_return:
 static struct ast_channel *mbl_new(int state, struct mbl_pvt *pvt, char *cid_num,
                const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
 {
-
        struct ast_channel *chn;
 
        pvt->answered = 0;
@@ -862,11 +861,11 @@ static struct ast_channel *mbl_new(int state, struct mbl_pvt *pvt, char *cid_num
        }
 
        ast_channel_tech_set(chn, &mbl_tech);
-       ast_format_cap_add(ast_channel_nativeformats(chn), &prefformat);
-       ast_format_copy(ast_channel_rawreadformat(chn), &prefformat);
-       ast_format_copy(ast_channel_rawwriteformat(chn), &prefformat);
-       ast_format_copy(ast_channel_writeformat(chn), &prefformat);
-       ast_format_copy(ast_channel_readformat(chn), &prefformat);
+       ast_channel_nativeformats_set(chn, mbl_tech.capabilities);
+       ast_channel_set_rawreadformat(chn, DEVICE_FRAME_FORMAT);
+       ast_channel_set_rawwriteformat(chn, DEVICE_FRAME_FORMAT);
+       ast_channel_set_writeformat(chn, DEVICE_FRAME_FORMAT);
+       ast_channel_set_readformat(chn, DEVICE_FRAME_FORMAT);
        ast_channel_tech_pvt_set(chn, pvt);
 
        if (state == AST_STATE_RING)
@@ -902,9 +901,9 @@ static struct ast_channel *mbl_request(const char *type, struct ast_format_cap *
                return NULL;
        }
 
-       if (!(ast_format_cap_iscompatible(cap, &prefformat))) {
-               char tmp[256];
-               ast_log(LOG_WARNING, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname_multiple(tmp, sizeof(tmp), cap));
+       if (ast_format_cap_iscompatible_format(cap, DEVICE_FRAME_FORMAT) == AST_FORMAT_CMP_NOT_EQUAL) {
+               struct ast_str *codec_buf = ast_str_alloca(64);
+               ast_log(LOG_WARNING, "Asked to get a channel of unsupported format '%s'\n", ast_format_cap_get_names(cap, &codec_buf));
                *cause = AST_CAUSE_FACILITY_NOT_IMPLEMENTED;
                return NULL;
        }
@@ -1116,7 +1115,7 @@ static struct ast_frame *mbl_read(struct ast_channel *ast)
 
        memset(&pvt->fr, 0x00, sizeof(struct ast_frame));
        pvt->fr.frametype = AST_FRAME_VOICE;
-       ast_format_set(&pvt->fr.subclass.format, DEVICE_FRAME_FORMAT, 0);
+       pvt->fr.subclass.format = DEVICE_FRAME_FORMAT;
        pvt->fr.src = "Mobile";
        pvt->fr.offset = AST_FRIENDLY_OFFSET;
        pvt->fr.mallocd = 0;
@@ -4697,7 +4696,8 @@ static int unload_module(void)
        if (sdp_session)
                sdp_close(sdp_session);
 
-       mbl_tech.capabilities = ast_format_cap_destroy(mbl_tech.capabilities);
+       ao2_ref(mbl_tech.capabilities, -1);
+       mbl_tech.capabilities = NULL;
        return 0;
 }
 
@@ -4706,11 +4706,11 @@ static int load_module(void)
 
        int dev_id, s;
 
-       if (!(mbl_tech.capabilities = ast_format_cap_alloc(0))) {
+       if (!(mbl_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return AST_MODULE_LOAD_DECLINE;
        }
-       ast_format_set(&prefformat, DEVICE_FRAME_FORMAT, 0);
-       ast_format_cap_add(mbl_tech.capabilities, &prefformat);
+
+       ast_format_cap_append(mbl_tech.capabilities, DEVICE_FRAME_FORMAT, 0);
        /* Check if we have Bluetooth, no point loading otherwise... */
        dev_id = hci_get_route(NULL);
        s = hci_open_dev(dev_id);
index 8f1ae76..f02002f 100644 (file)
@@ -92,8 +92,6 @@ static int ooh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance
 static void ooh323_get_codec(struct ast_channel *chan, struct ast_format_cap *result);
 void setup_rtp_remote(ooCallData *call, const char *remoteIp, int remotePort);
 
-static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
-
 struct ooh323_peer *find_friend(const char *name, int port);
 
 
@@ -174,10 +172,9 @@ static struct ooh323_pvt {
        char callee_url[AST_MAX_EXTENSION];
  
        int port;
-       struct ast_format readformat;   /* negotiated read format */
-       struct ast_format writeformat;  /* negotiated write format */
+       struct ast_format *readformat;   /* negotiated read format */
+       struct ast_format *writeformat;  /* negotiated write format */
        struct ast_format_cap *cap;
-       struct ast_codec_pref prefs;
        int dtmfmode;
        int dtmfcodec;
        char exten[AST_MAX_EXTENSION];  /* Requested extension */
@@ -212,7 +209,6 @@ struct ooh323_user{
        char            accountcode[20];
        int             amaflags;
        struct ast_format_cap *cap;
-       struct ast_codec_pref prefs;
        int             dtmfmode;
        int             dtmfcodec;
        int             faxdetect;
@@ -239,7 +235,6 @@ struct ooh323_peer{
        unsigned    outgoinglimit;
        unsigned    outUse;
        struct ast_format_cap *cap;
-       struct ast_codec_pref prefs;
        char        accountcode[20];
        int         amaflags;
        int         dtmfmode;
@@ -307,7 +302,6 @@ int v6mode = 0;
 static char gCallerID[AST_MAX_EXTENSION] = "";
 static struct ooAliases *gAliasList;
 static struct ast_format_cap *gCap;
-static struct ast_codec_pref gPrefs;
 static int  gDTMFMode = H323_DTMF_RFC2833;
 static int  gDTMFCodec = 101;
 static int  gFAXdetect = FAXDETECT_CNG;
@@ -366,15 +360,17 @@ static struct ast_channel *ooh323_new(struct ooh323_pvt *i, int state,
                                              const char *host, struct ast_format_cap *cap, 
                                                                                         const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
 {
+       struct ast_format_cap *caps = NULL;
        struct ast_channel *ch = NULL;
-       struct ast_format tmpfmt;
+       struct ast_format *tmpfmt = NULL;
        int features = 0;
 
        if (gH323Debug) {
                ast_verb(0, "---   ooh323_new - %s\n", host);
        }
 
-       ast_format_clear(&tmpfmt);
+       caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+
        /* Don't hold a h323 pvt lock while we allocate a channel */
        ast_mutex_unlock(&i->lock);
        ast_mutex_lock(&ooh323c_cn_lock);
@@ -386,17 +382,25 @@ static struct ast_channel *ooh323_new(struct ooh323_pvt *i, int state,
    
        ast_mutex_lock(&i->lock);
 
-       if (ch) {
+       if (ch && caps) {
                ast_channel_tech_set(ch, &ooh323_tech);
 
-               if (cap)
-                       ast_best_codec(cap, &tmpfmt);
-               if (!tmpfmt.id)
-                       ast_codec_pref_index(&i->prefs, 0, &tmpfmt);
+               if (cap) {
+                       tmpfmt = ast_format_cap_get_format(cap, 0);
+               }
+               if (!tmpfmt) {
+                       tmpfmt = ast_format_cap_get_format(i->cap, 0);
+               }
+
+               ast_format_cap_append(caps, tmpfmt, 0);
+               ast_channel_nativeformats_set(ch, caps);
+               ao2_ref(caps, -1);
 
-               ast_format_cap_add(ast_channel_nativeformats(ch), &tmpfmt);
-               ast_format_copy(ast_channel_rawwriteformat(ch), &tmpfmt);
-               ast_format_copy(ast_channel_rawreadformat(ch), &tmpfmt);
+               ast_channel_set_rawwriteformat(ch, tmpfmt);
+               ast_channel_set_rawreadformat(ch, tmpfmt);
+               ast_set_write_format(ch, tmpfmt);
+               ast_set_read_format(ch, tmpfmt);
+               ao2_ref(tmpfmt, -1);
 
                ast_jb_configure(ch, &global_jbconf);
 
@@ -404,8 +408,6 @@ static struct ast_channel *ooh323_new(struct ooh323_pvt *i, int state,
                        ast_channel_rings_set(ch, 1);
 
                ast_channel_adsicpe_set(ch, AST_ADSI_UNAVAILABLE);
-               ast_set_write_format(ch, &tmpfmt);
-               ast_set_read_format(ch, &tmpfmt);
                ast_channel_tech_pvt_set(ch, i);
                i->owner = ch;
                ast_module_ref(myself);
@@ -485,8 +487,10 @@ static struct ast_channel *ooh323_new(struct ooh323_pvt *i, int state,
                        ast_publish_channel_state(ch);
 
                }
-       } else
+       } else {
+               ao2_cleanup(caps);
                ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
+       }
 
 
        if(ch)   ast_channel_unlock(ch);
@@ -512,7 +516,7 @@ static struct ooh323_pvt *ooh323_alloc(int callref, char *callToken)
                ast_log(LOG_ERROR, "Couldn't allocate private ooh323 structure\n");
                return NULL;
        }
-       if (!(pvt->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
+       if (!(pvt->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                ast_free(pvt);
                ast_log(LOG_ERROR, "Couldn't allocate private ooh323 structure\n");
                return NULL;
@@ -546,8 +550,7 @@ static struct ooh323_pvt *ooh323_alloc(int callref, char *callToken)
        ast_copy_string(pvt->accountcode, gAccountcode, sizeof(pvt->accountcode));
 
        pvt->amaflags = gAMAFLAGS;
-       ast_format_cap_copy(pvt->cap, gCap);
-       memcpy(&pvt->prefs, &gPrefs, sizeof(pvt->prefs));
+       ast_format_cap_append_from_cap(pvt->cap, gCap, AST_MEDIA_TYPE_UNKNOWN);
 
        pvt->aniasdni = gANIasDNI;
 
@@ -573,21 +576,21 @@ static struct ast_channel *ooh323_request(const char *type, struct ast_format_ca
                const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
 
 {
+       struct ast_str *codec_buf = ast_str_alloca(64);
        struct ast_channel *chan = NULL;
        struct ooh323_pvt *p = NULL;
        struct ooh323_peer *peer = NULL;
        char *dest = NULL; 
        char *ext = NULL;
        char tmp[256];
-       char formats[FORMAT_STRING_SIZE];
        int port = 0;
 
-       if (gH323Debug)
-               ast_verb(0, "---   ooh323_request - data %s format %s\n", data,
-                                                                               ast_getformatname_multiple(formats,FORMAT_STRING_SIZE,cap));
+       if (gH323Debug) {
+               ast_verb(0, "---   ooh323_request - data %s format %s\n", data, ast_format_cap_get_names(cap, &codec_buf));
+       }
 
-       if (!(ast_format_cap_has_type(cap, AST_FORMAT_TYPE_AUDIO))) {
-               ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname_multiple(formats,FORMAT_STRING_SIZE,cap));
+       if (!(ast_format_cap_has_type(cap, AST_MEDIA_TYPE_AUDIO))) {
+               ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_format_cap_get_names(cap, &codec_buf));
                return NULL;
        }
 
@@ -651,8 +654,7 @@ static struct ast_channel *ooh323_request(const char *type, struct ast_format_ca
                if (ext)
                        ast_copy_string(p->exten, ext, sizeof(p->exten));
 
-               ast_format_cap_copy(p->cap, peer->cap);
-               memcpy(&p->prefs, &peer->prefs, sizeof(struct ast_codec_pref));
+               ast_format_cap_append_from_cap(p->cap, peer->cap, AST_MEDIA_TYPE_UNKNOWN);
                p->g729onlyA = peer->g729onlyA;
                p->dtmfmode |= peer->dtmfmode;
                p->dtmfcodec  = peer->dtmfcodec;
@@ -697,7 +699,7 @@ static struct ast_channel *ooh323_request(const char *type, struct ast_format_ca
                p->t38support = gT38Support;
                p->rtptimeout = gRTPTimeout;
                p->nat = gNat;
-               ast_format_cap_copy(p->cap, gCap);
+               ast_format_cap_append_from_cap(p->cap, gCap, AST_MEDIA_TYPE_UNKNOWN);
                p->rtdrinterval = gRTDRInterval;
                p->rtdrcount = gRTDRCount;
                p->faststart = gFastStart;
@@ -705,7 +707,6 @@ static struct ast_channel *ooh323_request(const char *type, struct ast_format_ca
                p->directrtp = gDirectRTP;
                p->earlydirect = gEarlyDirect;
 
-               memcpy(&p->prefs, &gPrefs, sizeof(struct ast_codec_pref));
                p->username = strdup(dest);
 
                p->host = strdup(dest);
@@ -1164,7 +1165,6 @@ static int ooh323_write(struct ast_channel *ast, struct ast_frame *f)
 {
        struct ooh323_pvt *p = ast_channel_tech_pvt(ast);
        int res = 0;
-       char buf[256];
 
        if (p) {
                ast_mutex_lock(&p->lock);
@@ -1190,16 +1190,17 @@ static int ooh323_write(struct ast_channel *ast, struct ast_frame *f)
                        }
 
 
-                       if (!(ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format))) {
-                               if (!(ast_format_cap_is_empty(ast_channel_nativeformats(ast)))) {
+                       if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
+                               if (ast_format_cap_count(ast_channel_nativeformats(ast))) {
+                                       struct ast_str *codec_buf = ast_str_alloca(64);
                                        ast_log(LOG_WARNING,
                                                        "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
-                                                       ast_getformatname(&f->subclass.format),
-                                                       ast_getformatname_multiple(buf, sizeof(buf), ast_channel_nativeformats(ast)),
-                                                       ast_getformatname(ast_channel_readformat(ast)),
-                                                       ast_getformatname(ast_channel_writeformat(ast)));
+                                                       ast_format_get_name(f->subclass.format),
+                                                       ast_format_cap_get_names(ast_channel_nativeformats(ast), &codec_buf),
+                                                       ast_format_get_name(ast_channel_readformat(ast)),
+                                                       ast_format_get_name(ast_channel_writeformat(ast)));
 
-                                       ast_set_write_format(ast, &f->subclass.format);
+                                       ast_set_write_format(ast, f->subclass.format);
                                } else {
                                        /* ast_set_write_format(ast, f->subclass);
                                        ast->nativeformats = f->subclass; */
@@ -1508,11 +1509,10 @@ static int ooh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan
 void ooh323_set_write_format(ooCallData *call, struct ast_format *fmt, int txframes)
 {
        struct ooh323_pvt *p = NULL;
-       char formats[FORMAT_STRING_SIZE];
 
        if (gH323Debug)
                ast_verb(0, "---   ooh323_update_writeformat %s/%d\n", 
-                               ast_getformatname(fmt), txframes);
+                               ast_format_get_name(fmt), txframes);
        
        p = find_call(call);
        if (!p) {
@@ -1522,9 +1522,17 @@ void ooh323_set_write_format(ooCallData *call, struct ast_format *fmt, int txfra
 
        ast_mutex_lock(&p->lock);
 
-       ast_format_copy(&(p->writeformat), fmt);
+       ao2_replace(p->writeformat, fmt);
 
        if (p->owner) {
+               struct ast_format_cap *caps;
+
+               caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+               if (!caps) {
+                       ast_log(LOG_ERROR, "Could not allocate capabilities structure\n");
+                       return;
+               }
+
                while (p->owner && ast_channel_trylock(p->owner)) {
                        ast_debug(1,"Failed to grab lock, trying again\n");
                        DEADLOCK_AVOIDANCE(&p->lock);
@@ -1532,15 +1540,16 @@ void ooh323_set_write_format(ooCallData *call, struct ast_format *fmt, int txfra
                if (!p->owner) {
                        ast_mutex_unlock(&p->lock);
                        ast_log(LOG_ERROR, "Channel has no owner\n");
+                       ao2_ref(caps, -1);
                        return;
                }
-               if (gH323Debug)
-                       ast_verb(0, "Writeformat before update %s/%s\n", 
-                         ast_getformatname(ast_channel_writeformat(p->owner)),
-                         ast_getformatname_multiple(formats, sizeof(formats), ast_channel_nativeformats(p->owner)));
-               if (txframes)
-                       ast_codec_pref_setsize(&p->prefs, fmt, txframes);
-               ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &p->prefs);
+               if (gH323Debug) {
+                       struct ast_str *codec_buf = ast_str_alloca(64);
+                       ast_verb(0, "Writeformat before update %s/%s\n", 
+                         ast_format_get_name(ast_channel_writeformat(p->owner)),
+                         ast_format_cap_get_names(ast_channel_nativeformats(p->owner), &codec_buf));
+               }
+
                if (p->dtmfmode & H323_DTMF_RFC2833 && p->dtmfcodec) {
                        ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
                                 p->rtp, p->dtmfcodec, "audio", "telephone-event", 0);
@@ -1550,7 +1559,12 @@ void ooh323_set_write_format(ooCallData *call, struct ast_format *fmt, int txfra
                                 p->rtp, p->dtmfcodec, "audio", "cisco-telephone-event", 0);
                }
 
-               ast_format_cap_set(ast_channel_nativeformats(p->owner), fmt);
+               if (txframes) {
+                       ast_format_cap_set_framing(caps, txframes);
+               }
+               ast_format_cap_append(caps, fmt, 0);
+               ast_channel_nativeformats_set(p->owner, caps);
+               ao2_ref(caps, -1);
                ast_set_write_format(p->owner, ast_channel_writeformat(p->owner));
                ast_set_read_format(p->owner, ast_channel_readformat(p->owner));
                ast_channel_unlock(p->owner);
@@ -1570,7 +1584,7 @@ void ooh323_set_read_format(ooCallData *call, struct ast_format *fmt)
 
        if (gH323Debug)
                ast_verb(0, "---   ooh323_update_readformat %s\n", 
-                               ast_getformatname(fmt));
+                               ast_format_get_name(fmt));
        
        p = find_call(call);
        if (!p) {
@@ -1580,9 +1594,17 @@ void ooh323_set_read_format(ooCallData *call, struct ast_format *fmt)
 
        ast_mutex_lock(&p->lock);
 
-       ast_format_copy(&(p->readformat), fmt);
+       ao2_replace(p->readformat, fmt);
 
        if (p->owner) {
+               struct ast_format_cap *caps;
+
+               caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+               if (!caps) {
+                       ast_log(LOG_ERROR, "Could not allocate capabilities structure\n");
+                       return;
+               }
+
                while (p->owner && ast_channel_trylock(p->owner)) {
                        ast_debug(1,"Failed to grab lock, trying again\n");
                        DEADLOCK_AVOIDANCE(&p->lock);
@@ -1590,14 +1612,18 @@ void ooh323_set_read_format(ooCallData *call, struct ast_format *fmt)
                if (!p->owner) {
                        ast_mutex_unlock(&p->lock);
                        ast_log(LOG_ERROR, "Channel has no owner\n");
+                       ao2_ref(caps, -1);
                        return;
                }
 
-               if (gH323Debug)
-                       ast_verb(0, "Readformat before update %s\n", 
-                                 ast_getformatname(ast_channel_readformat(p->owner)));
-               ast_format_cap_set(ast_channel_nativeformats(p->owner), fmt);
-               ast_set_read_format(p->owner, ast_channel_readformat(p->owner));
+               if (gH323Debug) {
+                       ast_verb(0, "Readformat before update %s\n", 
+                         ast_format_get_name(ast_channel_readformat(p->owner)));
+               }
+               ast_format_cap_append(caps, fmt, 0);
+               ast_channel_nativeformats_set(p->owner, caps);
+               ao2_ref(caps, -1);
+               ast_set_read_format(p->owner, ast_channel_readformat(p->owner));
                ast_channel_unlock(p->owner);
        } else
                ast_log(LOG_ERROR, "No owner found\n");
@@ -1854,9 +1880,8 @@ int ooh323_onReceivedSetup(ooCallData *call, Q931Message *pmsg)
                ast_copy_string(p->context, user->context, sizeof(p->context));
                ast_copy_string(p->accountcode, user->accountcode, sizeof(p->accountcode));
                p->amaflags = user->amaflags;
-               ast_format_cap_copy(p->cap, user->cap);
+               ast_format_cap_append_from_cap(p->cap, user->cap, AST_MEDIA_TYPE_UNKNOWN);
                p->g729onlyA = user->g729onlyA;
-               memcpy(&p->prefs, &user->prefs, sizeof(struct ast_codec_pref));
                p->dtmfmode |= user->dtmfmode;
                p->dtmfcodec = user->dtmfcodec;
                p->faxdetect = user->faxdetect;
@@ -1914,7 +1939,7 @@ int ooh323_onReceivedSetup(ooCallData *call, Q931Message *pmsg)
         }
        }
 
-       ooh323c_set_capability_for_call(call, &p->prefs, p->cap, p->dtmfmode, p->dtmfcodec,
+       ooh323c_set_capability_for_call(call, p->cap, p->dtmfmode, p->dtmfcodec,
                                         p->t38support, p->g729onlyA);
 /* Incoming call */
        c = ooh323_new(p, AST_STATE_RING, p->username, 0, NULL, NULL);
@@ -2088,13 +2113,14 @@ int onNewCallCreated(ooCallData *call)
                }
 
                if (gH323Debug) {
-                       char prefsBuf[256];
-                       ast_codec_pref_string(&p->prefs, prefsBuf, sizeof(prefsBuf));
+                       struct ast_str *codec_buf = ast_str_alloca(64);
+
                        ast_verb(0, " Outgoing call %s(%s) - Codec prefs - %s\n", 
-                               p->username?p->username:"NULL", call->callToken, prefsBuf);
+                               p->username?p->username:"NULL", call->callToken,
+                               ast_format_cap_get_names(p->cap, &codec_buf));
                }
 
-               ooh323c_set_capability_for_call(call, &p->prefs, p->cap,
+               ooh323c_set_capability_for_call(call, p->cap,
                                      p->dtmfmode, p->dtmfcodec, p->t38support, p->g729onlyA);
 
                configure_local_rtp(p, call);
@@ -2284,7 +2310,7 @@ void ooh323_delete_peer(struct ooh323_peer *peer)
       if(peer->url)      free(peer->url);
       if(peer->e164)     free(peer->e164);
 
-               peer->cap = ast_format_cap_destroy(peer->cap);
+      ao2_cleanup(peer->cap);
                free(peer);
        }  
 
@@ -2311,8 +2337,7 @@ static struct ooh323_user *build_user(const char *name, struct ast_variable *v)
                }
                ast_mutex_init(&user->lock);
                ast_copy_string(user->name, name, sizeof(user->name));
-               ast_format_cap_copy(user->cap, gCap);
-               memcpy(&user->prefs, &gPrefs, sizeof(user->prefs));
+               ast_format_cap_append_from_cap(user->cap, gCap, AST_MEDIA_TYPE_UNKNOWN);
                user->rtptimeout = gRTPTimeout;
                user->nat = gNat;
                user->dtmfmode = gDTMFMode;
@@ -2367,15 +2392,13 @@ static struct ooh323_user *build_user(const char *name, struct ast_variable *v)
                                                                sizeof(user->rtpmaskstr));
                                } else user->rtpmask = NULL;
                        } else if (!strcasecmp(v->name, "disallow")) {
-                               ast_parse_allow_disallow(&user->prefs, 
-                                       user->cap,  v->value, 0);
+                               ast_format_cap_update_by_allow_disallow(user->cap,  v->value, 0);
                        } else if (!strcasecmp(v->name, "allow")) {
                                const char* tcodecs = v->value;
                                if (!strcasecmp(v->value, "all")) {
                                        tcodecs = "ulaw,alaw,g729,g723,gsm";
                                }
-                               ast_parse_allow_disallow(&user->prefs,
-                                        user->cap,  tcodecs, 1);
+                               ast_format_cap_update_by_allow_disallow(user->cap,  tcodecs, 1);
                        } else if (!strcasecmp(v->name, "amaflags")) {
                                user->amaflags = ast_channel_string2amaflag(v->value);
                        } else if (!strcasecmp(v->name, "ip") || !strcasecmp(v->name, "host")) {
@@ -2461,8 +2484,7 @@ static struct ooh323_peer *build_peer(const char *name, struct ast_variable *v,
                }
                ast_mutex_init(&peer->lock);
                ast_copy_string(peer->name, name, sizeof(peer->name));
-               ast_format_cap_copy(peer->cap, gCap);
-               memcpy(&peer->prefs, &gPrefs, sizeof(peer->prefs));
+               ast_format_cap_append_from_cap(peer->cap, gCap, AST_MEDIA_TYPE_UNKNOWN);
                peer->rtptimeout = gRTPTimeout;
                peer->nat = gNat;
                ast_copy_string(peer->accountcode, gAccountcode, sizeof(peer->accountcode));
@@ -2565,15 +2587,13 @@ static struct ooh323_peer *build_peer(const char *name, struct ast_variable *v,
                                                                sizeof(peer->rtpmaskstr));
                                } else peer->rtpmask = NULL;
                        } else if (!strcasecmp(v->name, "disallow")) {
-                               ast_parse_allow_disallow(&peer->prefs, peer->cap, 
-                                                                                                v->value, 0); 
+                               ast_format_cap_update_by_allow_disallow(peer->cap, v->value, 0);
                        } else if (!strcasecmp(v->name, "allow")) {
                                const char* tcodecs = v->value;
                                if (!strcasecmp(v->value, "all")) {
                                        tcodecs = "ulaw,alaw,g729,g723,gsm";
                                }
-                               ast_parse_allow_disallow(&peer->prefs, peer->cap, 
-                                                                                                tcodecs, 1);                            
+                               ast_format_cap_update_by_allow_disallow(peer->cap, tcodecs, 1);
                        } else if (!strcasecmp(v->name,  "amaflags")) {
                                peer->amaflags = ast_channel_string2amaflag(v->value);
                        } else if (!strcasecmp(v->name, "roundtrip")) {
@@ -2754,7 +2774,6 @@ int reload_config(int reload)
        struct ooh323_peer *peer = NULL;
        char *cat;
        const char *utype;
-       struct ast_format tmpfmt;
 
        if (gH323Debug)
                ast_verb(0, "---   reload_config\n");
@@ -2790,8 +2809,8 @@ int reload_config(int reload)
        gPort = 1720;
        gIP[0] = '\0';
        strcpy(gCallerID, DEFAULT_H323ID);
-       ast_format_cap_set(gCap, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0));
-       memset(&gPrefs, 0, sizeof(struct ast_codec_pref));
+       ast_format_cap_remove_by_type(gCap, AST_MEDIA_TYPE_UNKNOWN);
+       ast_format_cap_append(gCap, ast_format_ulaw, 0);
        gDTMFMode = H323_DTMF_RFC2833;
        gDTMFCodec = 101;
        gFAXdetect = FAXDETECT_CNG;
@@ -2992,13 +3011,13 @@ int reload_config(int reload)
                } else if (!strcasecmp(v->name, "accountcode")) {
          ast_copy_string(gAccountcode, v->value, sizeof(gAccountcode));
                } else if (!strcasecmp(v->name, "disallow")) {
-                       ast_parse_allow_disallow(&gPrefs, gCap, v->value, 0);
+                       ast_format_cap_update_by_allow_disallow(gCap, v->value, 0);
                } else if (!strcasecmp(v->name, "allow")) {
                        const char* tcodecs = v->value;
                        if (!strcasecmp(v->value, "all")) {
                                tcodecs = "ulaw,alaw,g729,g723,gsm";
                        }
-                       ast_parse_allow_disallow(&gPrefs, gCap, tcodecs, 1);
+                       ast_format_cap_update_by_allow_disallow(gCap, tcodecs, 1);
                } else if (!strcasecmp(v->name, "dtmfmode")) {
                        if (!strcasecmp(v->value, "inband"))
                                gDTMFMode = H323_DTMF_INBAND;
@@ -3150,9 +3169,6 @@ static char *handle_cli_ooh323_show_peer(struct ast_cli_entry *e, int cmd, struc
                                        peer->h245tunneling?"yes":"no");
                ast_cli(a->fd, "%-15s%s\n", "DirectRTP", peer->directrtp ? "yes" : "no");
                ast_cli(a->fd, "%-15s%s\n", "EarlyDirectRTP", peer->earlydirect ? "yes" : "no");
-               ast_cli(a->fd, "%-15.15s%s", "Format Prefs: ", "(");
-               print_codec_to_cli(a->fd, &peer->prefs);
-               ast_cli(a->fd, ")\n");
                ast_cli(a->fd, "%-15.15s", "DTMF Mode: ");
                if (peer->dtmfmode & H323_DTMF_CISCO) {
                        ast_cli(a->fd, "%s\n", "cisco");
@@ -3214,7 +3230,7 @@ static char *handle_cli_ooh323_show_peer(struct ast_cli_entry *e, int cmd, struc
 static char *handle_cli_ooh323_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct ooh323_peer *prev = NULL, *peer = NULL;
-   char formats[FORMAT_STRING_SIZE];
+       struct ast_str *codec_buf = ast_str_alloca(64);
    char ip_port[30];
 #define FORMAT  "%-15.15s  %-15.15s  %-23.23s  %-s\n"
 
@@ -3239,10 +3255,10 @@ static char *handle_cli_ooh323_show_peers(struct ast_cli_entry *e, int cmd, stru
        while (peer) {
                ast_mutex_lock(&peer->lock);
                snprintf(ip_port, sizeof(ip_port), "%s:%d", peer->ip, peer->port);
-     ast_cli(a->fd, FORMAT, peer->name, 
+               ast_cli(a->fd, FORMAT, peer->name, 
                                        peer->accountcode,
                                        ip_port,
-                 ast_getformatname_multiple(formats,FORMAT_STRING_SIZE,peer->cap));
+                                       ast_format_cap_get_names(peer->cap, &codec_buf));
                prev = peer;
                peer = peer->next;
                ast_mutex_unlock(&prev->lock);
@@ -3253,24 +3269,6 @@ static char *handle_cli_ooh323_show_peers(struct ast_cli_entry *e, int cmd, stru
        return CLI_SUCCESS;
 }
 
-/*! \brief Print codec list from preference to CLI/manager */
-static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
-{
-       int x;
-       struct ast_format tmpfmt;
-       for (x = 0; x < 32; x++) {
-               ast_codec_pref_index(pref, x, &tmpfmt);
-               if (!tmpfmt.id)
-                       break;
-               ast_cli(fd, "%s", ast_getformatname(&tmpfmt));
-               ast_cli(fd, ":%d", pref->framing[x]);
-               if (x < 31 && ast_codec_pref_index(pref, x + 1, &tmpfmt))
-                       ast_cli(fd, ",");
-       }
-       if (!x)
-               ast_cli(fd, "none");
-}
-
 static char *handle_cli_ooh323_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct ooh323_user *prev = NULL, *user = NULL;
@@ -3309,9 +3307,6 @@ static char *handle_cli_ooh323_show_user(struct ast_cli_entry *e, int cmd, struc
                                        user->h245tunneling?"yes":"no");
                ast_cli(a->fd, "%-15s%s\n", "DirectRTP", user->directrtp ? "yes" : "no");
                ast_cli(a->fd, "%-15s%s\n", "EarlyDirectRTP", user->earlydirect ? "yes" : "no");
-               ast_cli(a->fd, "%-15.15s%s", "Format Prefs: ", "(");
-               print_codec_to_cli(a->fd, &user->prefs);
-               ast_cli(a->fd, ")\n");
                ast_cli(a->fd, "%-15.15s", "DTMF Mode: ");
                if (user->dtmfmode & H323_DTMF_CISCO) {
                        ast_cli(a->fd, "%s\n", "cisco");
@@ -3374,7 +3369,7 @@ static char *handle_cli_ooh323_show_user(struct ast_cli_entry *e, int cmd, struc
 static char *handle_cli_ooh323_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct ooh323_user *prev = NULL, *user = NULL;
-   char formats[FORMAT_STRING_SIZE];
+       struct ast_str *codec_buf = ast_str_alloca(64);
 #define FORMAT1  "%-15.15s  %-15.15s  %-15.15s  %-s\n"
 
        switch (cmd) {
@@ -3401,7 +3396,7 @@ static char *handle_cli_ooh323_show_users(struct ast_cli_entry *e, int cmd, stru
                ast_mutex_lock(&user->lock);
                ast_cli(a->fd, FORMAT1, user->name, 
                                        user->accountcode, user->context,
-                                       ast_getformatname_multiple(formats, FORMAT_STRING_SIZE, user->cap));
+                                       ast_format_cap_get_names(user->cap, &codec_buf));
                prev = user;
                user = user->next;
                ast_mutex_unlock(&prev->lock);
@@ -3508,6 +3503,7 @@ static char *handle_cli_ooh323_show_gk(struct ast_cli_entry *e, int cmd, struct
 static char *handle_cli_ooh323_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        char value[FORMAT_STRING_SIZE];
+       struct ast_str *codec_buf = ast_str_alloca(64);
        ooAliases *pAlias = NULL, *pAliasNext = NULL;;
 
        switch (cmd) {
@@ -3556,7 +3552,7 @@ static char *handle_cli_ooh323_show_config(struct ast_cli_entry *e, int cmd, str
        ast_cli(a->fd,  "%-20s%s\n", "H.323 LogFile:", gLogFile);
        ast_cli(a->fd,  "%-20s%s\n", "Context:", gContext);
        ast_cli(a->fd,  "%-20s%s\n", "Capability:",
-               ast_getformatname_multiple(value,FORMAT_STRING_SIZE,gCap));
+               ast_format_cap_get_names(gCap, &codec_buf));
        ast_cli(a->fd, "%-20s", "DTMF Mode: ");
        if (gDTMFMode & H323_DTMF_CISCO) {
                ast_cli(a->fd, "%s\n", "cisco");
@@ -3737,7 +3733,6 @@ static int load_module(void)
 {
        struct ooAliases * pNewAlias = NULL;
        struct ooh323_peer *peer = NULL;
-       struct ast_format tmpfmt;
        OOH225MsgCallbacks h225Callbacks = {0, 0, 0, 0};
 
        OOH323CALLBACKS h323Callbacks = {
@@ -3753,14 +3748,16 @@ static int load_module(void)
                .onModeChanged = onModeChanged,
                .onMediaChanged = (cb_OnMediaChanged) setup_rtp_remote,
        };
-       if (!(gCap = ast_format_cap_alloc(0))) {
+       if (!(gCap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return AST_MODULE_LOAD_FAILURE; 
        }
-       if (!(ooh323_tech.capabilities = ast_format_cap_alloc(0))) {
+       if (!(ooh323_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
+               ao2_ref(gCap, -1);
+               gCap = NULL;
                return AST_MODULE_LOAD_FAILURE;
        }
-       ast_format_cap_add(gCap, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0));
-       ast_format_cap_add_all(ooh323_tech.capabilities);
+       ast_format_cap_append(gCap, ast_format_ulaw, 0);
+       ast_format_cap_append_by_type(ooh323_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
 
        myself = ast_module_info->self;
 
@@ -3789,12 +3786,20 @@ static int load_module(void)
                if (OO_OK != ooH323EpInitialize(OO_CALLMODE_AUDIOCALL, gLogFile)) {
                        ast_log(LOG_ERROR, "Failed to initialize OOH323 endpoint-"
                             "OOH323 Disabled\n");
+                       ao2_ref(gCap, -1);
+                       gCap = NULL;
+                       ao2_ref(ooh323_tech.capabilities, -1);
+                       ooh323_tech.capabilities = NULL;
                        return AST_MODULE_LOAD_FAILURE;
                }
 
                /* Make sure we can register our OOH323 channel type */
                if (ast_channel_register(&ooh323_tech)) {
                        ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
+                       ao2_ref(gCap, -1);
+                       gCap = NULL;
+                       ao2_ref(ooh323_tech.capabilities, -1);
+                       ooh323_tech.capabilities = NULL;
                        return AST_MODULE_LOAD_FAILURE;
                }
                ast_rtp_glue_register(&ooh323_rtp);
@@ -3876,8 +3881,12 @@ static int load_module(void)
                ooH323EpSetH323Callbacks(h323Callbacks);
 
                /* Add endpoint capabilities */
-               if (ooh323c_set_capability(&gPrefs, gCap, gDTMFMode, gDTMFCodec) < 0) {
+               if (ooh323c_set_capability(gCap, gDTMFMode, gDTMFCodec) < 0) {
                        ast_log(LOG_ERROR, "Capabilities failure for OOH323. OOH323 Disabled.\n");
+                       ao2_ref(gCap, -1);
+                       gCap = NULL;
+                       ao2_ref(ooh323_tech.capabilities, -1);
+                       ooh323_tech.capabilities = NULL;
                        return 1;
                }
   
@@ -3887,6 +3896,10 @@ static int load_module(void)
                             "OOH323 DISABLED\n");
                
                        ooH323EpDestroy();
+                       ao2_ref(gCap, -1);
+                       gCap = NULL;
+                       ao2_ref(ooh323_tech.capabilities, -1);
+                       ooh323_tech.capabilities = NULL;
                        return 1;
                }
 
@@ -3894,6 +3907,10 @@ static int load_module(void)
                        ast_log(LOG_ERROR, "Failed to start OOH323 stack thread. "
                             "OOH323 DISABLED\n");
                        ooH323EpDestroy();
+                       ao2_ref(gCap, -1);
+                       gCap = NULL;
+                       ao2_ref(ooh323_tech.capabilities, -1);
+                       ooh323_tech.capabilities = NULL;
                        return 1;
                }
                /* And start the monitor for the first time */
@@ -4142,7 +4159,9 @@ int ooh323_destroy(struct ooh323_pvt *p)
 
                ast_mutex_unlock(&cur->lock);
                ast_mutex_destroy(&cur->lock);
-               cur->cap = ast_format_cap_destroy(cur->cap);
+               ao2_cleanup(cur->writeformat);
+               ao2_cleanup(cur->readformat);
+               ao2_cleanup(cur->cap);
                ast_free(cur);
        }
 
@@ -4207,7 +4226,7 @@ int delete_users()
                                free(prev->rtpmask);
                        }
                }
-               prev->cap = ast_format_cap_destroy(prev->cap);
+       ao2_cleanup(prev->cap);
                free(prev);
                if (cur == userl.users) {
                        break;
@@ -4335,8 +4354,10 @@ static int unload_module(void)
                ast_verb(0, "+++ ooh323  unload_module \n");
        }
 
-       gCap = ast_format_cap_destroy(gCap);
-       ooh323_tech.capabilities = ast_format_cap_destroy(ooh323_tech.capabilities);
+       ao2_ref(gCap, -1);
+       gCap = NULL;
+       ao2_ref(ooh323_tech.capabilities, -1);
+       ooh323_tech.capabilities = NULL;
        return 0;
 }
 
@@ -4348,8 +4369,11 @@ static void ooh323_get_codec(struct ast_channel *chan, struct ast_format_cap *re
        }
 
        if (p) {
-               ast_format_cap_append(result,  ast_format_cap_is_empty(ast_channel_nativeformats(chan)) ?
-                               (ast_format_cap_is_empty(p->cap) ? NULL : p->cap) : ast_channel_nativeformats(chan));
+               if (ast_format_cap_count(ast_channel_nativeformats(chan))) {
+                       ast_format_cap_append_from_cap(result, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_UNKNOWN);
+               } else if (ast_format_cap_count(p->cap)) {
+                       ast_format_cap_append_from_cap(result, p->cap, AST_MEDIA_TYPE_UNKNOWN);
+               }
        }
 
        if (gH323Debug) {
@@ -4423,56 +4447,28 @@ static enum ast_rtp_glue_result ooh323_get_vrtp_peer(struct ast_channel *chan, s
        return res;
 }
 
-
-int ooh323_update_capPrefsOrderForCall
-       (ooCallData *call, struct ast_codec_pref *prefs)
-{
-       int i = 0;
-       struct ast_format tmpfmt;
-
-       ast_codec_pref_index(prefs, i, &tmpfmt);
-
-       ooResetCapPrefs(call);
-       while (tmpfmt.id) {
-               ooAppendCapToCapPrefs(call, ooh323_convertAsteriskCapToH323Cap(&tmpfmt));
-               ast_codec_pref_index(prefs, ++i, &tmpfmt);
-       }
-
-       return 0;
-}
-
-
 int ooh323_convertAsteriskCapToH323Cap(struct ast_format *format)
 {
-       switch (format->id) {
-       case AST_FORMAT_ULAW:
+       if (ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
                return OO_G711ULAW64K;
-       case AST_FORMAT_ALAW:
+       } else if (ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
                return OO_G711ALAW64K;
-       case AST_FORMAT_GSM:
+       } else if (ast_format_cmp(format, ast_format_gsm) == AST_FORMAT_CMP_EQUAL) {
                return OO_GSMFULLRATE;
-
-#ifdef AST_FORMAT_AMRNB
-       case AST_FORMAT_AMRNB:
-               return OO_AMRNB;
-#endif
-#ifdef AST_FORMAT_SPEEX
-       case AST_FORMAT_SPEEX:
+       } else if (ast_format_cmp(format, ast_format_speex) == AST_FORMAT_CMP_EQUAL) {
                return OO_SPEEX;
-#endif
-
-       case AST_FORMAT_G729A:
+       } else if (ast_format_cmp(format, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
                return OO_G729A;
-       case AST_FORMAT_G726:
+       } else if (ast_format_cmp(format, ast_format_g726) == AST_FORMAT_CMP_EQUAL) {
                return OO_G726;
-       case AST_FORMAT_G726_AAL2:
+       } else if (ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL) {
                return OO_G726AAL2;
-       case AST_FORMAT_G723_1:
+       } else if (ast_format_cmp(format, ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
                return OO_G7231;
-       case AST_FORMAT_H263:
+       } else if (ast_format_cmp(format, ast_format_h263) == AST_FORMAT_CMP_EQUAL) {
                return OO_H263VIDEO;
-       default:
-               ast_log(LOG_NOTICE, "Don't know how to deal with mode %s\n", ast_getformatname(format));
+       } else {
+               ast_log(LOG_NOTICE, "Don't know how to deal with mode %s\n", ast_format_get_name(format));
                return -1;
        }
 }
@@ -4551,9 +4547,6 @@ int configure_local_rtp(struct ooh323_pvt *p, ooCallData *call)
        struct ast_sockaddr tmp;
        ooMediaInfo mediaInfo;
        int x;
-       struct ast_format tmpfmt;
-
-       ast_format_clear(&tmpfmt);
 
        if (gH323Debug)
                ast_verb(0, "---   configure_local_rtp\n");
@@ -4599,7 +4592,10 @@ int configure_local_rtp(struct ooh323_pvt *p, ooCallData *call)
        ast_channel_unlock(p->owner);
 
        if (p->rtp) {
-               ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &p->prefs);
+               if (p->cap) {
+                       ast_rtp_codecs_set_framing(ast_rtp_instance_get_codecs(p->rtp),
+                               ast_format_cap_get_framing(p->cap));
+               }
                if (p->nat) {
                        ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_NAT, 1);
                }
@@ -4636,9 +4632,11 @@ int configure_local_rtp(struct ooh323_pvt *p, ooCallData *call)
        ast_copy_string(mediaInfo.lMediaIP, lhost, sizeof(mediaInfo.lMediaIP));
        mediaInfo.lMediaPort = lport;
        mediaInfo.lMediaCntrlPort = mediaInfo.lMediaPort + 1;
-       for (x = 0; ast_codec_pref_index(&p->prefs, x, &tmpfmt); x++) {
+       for (x = 0; x < ast_format_cap_count(p->cap); x++) {
+               struct ast_format *format = ast_format_cap_get_format(p->cap, x);
+
                strcpy(mediaInfo.dir, "transmit");
-               mediaInfo.cap = ooh323_convertAsteriskCapToH323Cap(&tmpfmt);
+               mediaInfo.cap = ooh323_convertAsteriskCapToH323Cap(format);
                ooAddMediaInfo(call, mediaInfo);
                strcpy(mediaInfo.dir, "receive");
                ooAddMediaInfo(call, mediaInfo);
@@ -4655,6 +4653,8 @@ int configure_local_rtp(struct ooh323_pvt *p, ooCallData *call)
                        strcpy(mediaInfo.dir, "receive");
                        ooAddMediaInfo(call, mediaInfo);
                }
+
+               ao2_ref(format, -1);
        }
 
        if (p->udptl) {
@@ -4738,7 +4738,7 @@ void setup_rtp_connection(ooCallData *call, const char *remoteIp, int remotePort
        ast_sockaddr_set_port(&tmp, remotePort);
        ast_rtp_instance_set_remote_address(p->rtp, &tmp);
 
-       if (p->writeformat.id == AST_FORMAT_G726_AAL2) {
+       if (ast_format_cmp(p->writeformat, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL) {
                 ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp), p->rtp, 2,
                                                        "audio", "G726-32", AST_RTP_OPT_G726_NONSTANDARD);
        }
@@ -4955,15 +4955,24 @@ struct ast_frame *ooh323_rtp_read(struct ast_channel *ast, struct ooh323_pvt *p)
 
        if (f && p->owner && !p->faxmode && (f->frametype == AST_FRAME_VOICE)) {
                /* We already hold the channel lock */
-               if (!(ast_format_cap_iscompatible(ast_channel_nativeformats(p->owner), &f->subclass.format))) {
-                       ast_debug(1, "Oooh, voice format changed to %s\n", ast_getformatname(&f->subclass.format));
-                       ast_format_cap_set(ast_channel_nativeformats(p->owner), &f->subclass.format);
+               if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(p->owner), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
+                       struct ast_format_cap *caps;
+
+                       ast_debug(1, "Oooh, voice format changed to %s\n", ast_format_get_name(f->subclass.format));
+
+                       caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+                       if (caps) {
+                               ast_format_cap_append(caps, f->subclass.format, 0);
+                               ast_channel_nativeformats_set(p->owner, caps);
+                               ao2_ref(caps, -1);
+                       }
                        ast_set_read_format(p->owner, ast_channel_readformat(p->owner));
                        ast_set_write_format(p->owner, ast_channel_writeformat(p->owner));
                }
                if (((p->dtmfmode & H323_DTMF_INBAND) || (p->faxdetect & FAXDETECT_CNG)) && p->vad &&
-                   (f->subclass.format.id == AST_FORMAT_SLINEAR || f->subclass.format.id == AST_FORMAT_ALAW ||
-                    f->subclass.format.id == AST_FORMAT_ULAW)) {
+                       ((ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) ||
+                       (ast_format_cmp(f->subclass.format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) ||
+                       (ast_format_cmp(f->subclass.format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL))) {
                        dfr = ast_frdup(f);
                        dfr = ast_dsp_process(p->owner, p->vad, dfr);
                }
index 689adce..89caaff 100644 (file)
@@ -65,6 +65,7 @@
 #include "asterisk/format_cap.h"
 #include "asterisk/udptl.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/format_cache.h"
 
 #include "ootypes.h"
 #include "ooUtils.h"
@@ -104,9 +105,6 @@ struct ast_frame *ooh323_rtp_read
 void ooh323_set_write_format(ooCallData *call, struct ast_format *fmt, int txframes);
 void ooh323_set_read_format(ooCallData *call, struct ast_format *fmt);
 
-int ooh323_update_capPrefsOrderForCall
-   (ooCallData *call, struct ast_codec_pref *prefs);
-
 int ooh323_convertAsteriskCapToH323Cap(struct ast_format *format);
 
 int ooh323_convert_hangupcause_asteriskToH323(int cause);
index 78fcc28..4c98789 100644 (file)
@@ -42,6 +42,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/mod_format.h"
 #include "asterisk/logger.h"
+#include "asterisk/format_cache.h"
 
 #define MP3_BUFLEN 320
 #define MP3_SCACHE 16384
@@ -229,10 +230,7 @@ static struct ast_frame *mp3_read(struct ast_filestream *s, int *whennext)
 
        p->offset += p->buflen;
        delay = p->buflen / 2;
-       s->fr.frametype = AST_FRAME_VOICE;
-       ast_format_set(&s->fr.subclass.format, AST_FORMAT_SLINEAR, 0);
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, p->buflen);
-       s->fr.mallocd = 0;
        s->fr.samples = delay;
        *whennext = delay;
        return &s->fr;
@@ -318,7 +316,7 @@ static struct ast_format_def mp3_f = {
 
 static int load_module(void)
 {
-       ast_format_set(&mp3_f.format, AST_FORMAT_SLINEAR, 0);
+       mp3_f.format = ast_format_slin;
        InitMP3Constants();
        return ast_format_def_register(&mp3_f);
 }
index dd127a4..bceb077 100644 (file)
@@ -225,17 +225,17 @@ int ooh323c_stop_stack_thread(void)
 }
 
 int ooh323c_set_capability
-   (struct ast_codec_pref *prefs, struct ast_format_cap *cap, int dtmf, int dtmfcodec)
+   (struct ast_format_cap *cap, int dtmf, int dtmfcodec)
 {
    int ret = 0, x;
-   struct ast_format tmpfmt;
    if (gH323Debug) {
      ast_verb(0, "\tAdding capabilities to H323 endpoint\n");
    }
 
-   for(x=0; ast_codec_pref_index(prefs, x, &tmpfmt); x++)
+   for(x=0; x<ast_format_cap_count(cap); x++)
    {
-      if(tmpfmt.id == AST_FORMAT_ULAW)
+    struct ast_format *format = ast_format_cap_get_format(cap, x);
+      if(ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g711 ulaw capability to H323 endpoint\n");
@@ -246,7 +246,7 @@ int ooh323c_set_capability
                                      &ooh323c_stop_receive_channel, 
                                      &ooh323c_stop_transmit_channel);
       }
-      if(tmpfmt.id == AST_FORMAT_ALAW)
+      if(ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g711 alaw capability to H323 endpoint\n");
@@ -258,7 +258,7 @@ int ooh323c_set_capability
                                      &ooh323c_stop_transmit_channel);
       }
 
-      if(tmpfmt.id == AST_FORMAT_G729A)
+      if(ast_format_cmp(format, ast_format_g729) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
            ast_verb(0, "\tAdding g729A capability to H323 endpoint\n");
@@ -287,7 +287,7 @@ int ooh323c_set_capability
                                      &ooh323c_stop_transmit_channel);
       }
 
-      if(tmpfmt.id == AST_FORMAT_G723_1)
+      if(ast_format_cmp(format, ast_format_g723) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g7231 capability to H323 endpoint\n");
@@ -300,7 +300,7 @@ int ooh323c_set_capability
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G726)
+      if(ast_format_cmp(format, ast_format_g726) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g726 capability to H323 endpoint\n");
@@ -313,7 +313,7 @@ int ooh323c_set_capability
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G726_AAL2)
+      if(ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g726aal2 capability to H323 endpoint\n");
@@ -326,7 +326,7 @@ int ooh323c_set_capability
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_H263)
+      if(ast_format_cmp(format, ast_format_h263) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding h263 capability to H323 endpoint\n");
@@ -339,7 +339,7 @@ int ooh323c_set_capability
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_GSM)
+      if(ast_format_cmp(format, ast_format_gsm) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding gsm capability to H323 endpoint\n");
@@ -351,24 +351,8 @@ int ooh323c_set_capability
                                      &ooh323c_stop_transmit_channel);
 
       }
-      
-#ifdef AST_FORMAT_AMRNB
-      if(tmpfmt.id == AST_FORMAT_AMRNB)
-      {
-         if (gH323Debug) {
-            ast_verb(0, "\tAdding amr nb capability to H323 endpoint\n");
-        }
-         ret = ooH323EpAddAMRNBCapability(OO_AMRNB, 4, 4, FALSE, 
-                                     OORXANDTX, &ooh323c_start_receive_channel,
-                                     &ooh323c_start_transmit_channel,
-                                     &ooh323c_stop_receive_channel, 
-                                     &ooh323c_stop_transmit_channel);
-
-      }
-#endif
 
-#ifdef AST_FORMAT_SPEEX
-      if(tmpfmt.id == AST_FORMAT_SPEEX)
+      if(ast_format_cmp(format, ast_format_speex) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding speex capability to H323 endpoint\n");
@@ -380,8 +364,8 @@ int ooh323c_set_capability
                                      &ooh323c_stop_transmit_channel);
 
       }
-#endif
-      
+
+    ao2_ref(format, -1);      
    }
    
    if(dtmf & H323_DTMF_CISCO)
@@ -397,11 +381,10 @@ int ooh323c_set_capability
 }
 
 int ooh323c_set_capability_for_call
-   (ooCallData *call, struct ast_codec_pref *prefs, struct ast_format_cap *cap, int dtmf, int dtmfcodec,
+   (ooCallData *call, struct ast_format_cap *cap, int dtmf, int dtmfcodec,
                 int t38support, int g729onlyA)
 {
    int ret = 0, x, txframes;
-   struct ast_format tmpfmt;
    if (gH323Debug) {
      ast_verb(0, "\tAdding capabilities to call(%s, %s)\n", call->callType, 
                                                             call->callToken);
@@ -423,15 +406,16 @@ int ooh323c_set_capability_for_call
                                        &ooh323c_stop_transmit_datachannel,
                                        0);
 
-   for(x=0; ast_codec_pref_index(prefs, x, &tmpfmt); x++)
+   for(x=0; x<ast_format_cap_count(cap); x++)
    {
-      if(tmpfmt.id == AST_FORMAT_ULAW)
+    struct ast_format *format = ast_format_cap_get_format(cap, x);
+      if(ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g711 ulaw capability to call(%s, %s)\n", 
                                               call->callType, call->callToken);
         }
-        txframes = prefs->framing[x];
+        txframes = ast_format_cap_get_format_framing(cap, format);
          ret= ooCallAddG711Capability(call, OO_G711ULAW64K, txframes, 
                                       txframes, OORXANDTX, 
                                       &ooh323c_start_receive_channel,
@@ -439,13 +423,13 @@ int ooh323c_set_capability_for_call
                                       &ooh323c_stop_receive_channel, 
                                       &ooh323c_stop_transmit_channel);
       }
-      if(tmpfmt.id == AST_FORMAT_ALAW)
+      if(ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g711 alaw capability to call(%s, %s)\n",
                                             call->callType, call->callToken);
         }
-         txframes = prefs->framing[x];
+         txframes = ast_format_cap_get_format_framing(cap, format);
          ret= ooCallAddG711Capability(call, OO_G711ALAW64K, txframes, 
                                      txframes, OORXANDTX, 
                                      &ooh323c_start_receive_channel,
@@ -454,13 +438,13 @@ int ooh323c_set_capability_for_call
                                      &ooh323c_stop_transmit_channel);
       }
 
-      if(tmpfmt.id == AST_FORMAT_G726)
+      if(ast_format_cmp(format, ast_format_g726) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g726 capability to call (%s, %s)\n",
                                            call->callType, call->callToken);
         }
-        txframes = prefs->framing[x];
+        txframes = ast_format_cap_get_format_framing(cap, format);
          ret = ooCallAddG726Capability(call, OO_G726, txframes, grxframes, FALSE,
                                      OORXANDTX, &ooh323c_start_receive_channel,
                                      &ooh323c_start_transmit_channel,
@@ -469,13 +453,13 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G726_AAL2)
+      if(ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g726aal2 capability to call (%s, %s)\n",
                                            call->callType, call->callToken);
         }
-        txframes = prefs->framing[x];
+        txframes = ast_format_cap_get_format_framing(cap, format);
          ret = ooCallAddG726Capability(call, OO_G726AAL2, txframes, grxframes, FALSE,
                                      OORXANDTX, &ooh323c_start_receive_channel,
                                      &ooh323c_start_transmit_channel,
@@ -484,10 +468,10 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G729A)
+      if(ast_format_cmp(format, ast_format_g729) == AST_FORMAT_CMP_EQUAL)
       {
       
-         txframes = (prefs->framing[x])/10;
+         txframes = (ast_format_cap_get_format_framing(cap, format))/10;
          if (gH323Debug) {
             ast_verb(0, "\tAdding g729A capability to call(%s, %s)\n",
                                             call->callType, call->callToken);
@@ -520,7 +504,7 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G723_1)
+      if(ast_format_cmp(format, ast_format_g723) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g7231 capability to call (%s, %s)\n",
@@ -534,7 +518,7 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_H263)
+      if(ast_format_cmp(format, ast_format_h263) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding h263 capability to call (%s, %s)\n",
@@ -548,7 +532,7 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_GSM)
+      if(ast_format_cmp(format, ast_format_gsm) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding gsm capability to call(%s, %s)\n", 
@@ -561,22 +545,7 @@ int ooh323c_set_capability_for_call
                                      &ooh323c_stop_transmit_channel);
       }
 
-#ifdef AST_FORMAT_AMRNB
-      if(tmpfmt.id == AST_FORMAT_AMRNB)
-      {
-         if (gH323Debug) {
-            ast_verb(0, "\tAdding AMR capability to call(%s, %s)\n", 
-                                             call->callType, call->callToken);
-        }
-         ret = ooCallAddAMRNBCapability(call, OO_AMRNB, 4, 4, FALSE, 
-                                     OORXANDTX, &ooh323c_start_receive_channel,
-                                     &ooh323c_start_transmit_channel,
-                                     &ooh323c_stop_receive_channel, 
-                                     &ooh323c_stop_transmit_channel);
-      }
-#endif
-#ifdef AST_FORMAT_SPEEX
-      if(tmpfmt.id == AST_FORMAT_SPEEX)
+      if(ast_format_cmp(format, ast_format_speex) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding Speex capability to call(%s, %s)\n", 
@@ -588,7 +557,8 @@ int ooh323c_set_capability_for_call
                                      &ooh323c_stop_receive_channel, 
                                      &ooh323c_stop_transmit_channel);
       }
-#endif
+
+      ao2_ref(format, -1);
    }
    return ret;
 }
@@ -622,9 +592,9 @@ int ooh323c_set_aliases(ooAliases * aliases)
    
 int ooh323c_start_receive_channel(ooCallData *call, ooLogicalChannel *pChannel)
 {
-   struct ast_format tmpfmt;
-   convertH323CapToAsteriskCap(pChannel->chanCap->cap, &tmpfmt);
-   if(tmpfmt.id) {
+   struct ast_format *tmpfmt = NULL;
+   tmpfmt = convertH323CapToAsteriskCap(pChannel->chanCap->cap);
+   if(tmpfmt) {
       /* ooh323_set_read_format(call, fmt); */
    }else{
      ast_log(LOG_ERROR, "Invalid capability type for receive channel %s\n",
@@ -636,19 +606,17 @@ int ooh323c_start_receive_channel(ooCallData *call, ooLogicalChannel *pChannel)
 
 int ooh323c_start_transmit_channel(ooCallData *call, ooLogicalChannel *pChannel)
 {
-   struct ast_format tmpfmt;
-   convertH323CapToAsteriskCap(pChannel->chanCap->cap, &tmpfmt);
-   if(tmpfmt.id) {
-      switch (tmpfmt.id) {
-      case AST_FORMAT_ALAW:
-      case AST_FORMAT_ULAW:
-       ooh323_set_write_format(call, &tmpfmt, ((OOCapParams *)(pChannel->chanCap->params))->txframes);
-       break;
-      case AST_FORMAT_G729A:
-       ooh323_set_write_format(call, &tmpfmt, ((OOCapParams *)(pChannel->chanCap->params))->txframes*10);
-       break;
-      default:
-       ooh323_set_write_format(call, &tmpfmt, 0);
+   struct ast_format *tmpfmt = NULL;
+   tmpfmt = convertH323CapToAsteriskCap(pChannel->chanCap->cap);
+
+   if (tmpfmt) {
+    if ((ast_format_cmp(tmpfmt, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) ||
+      (ast_format_cmp(tmpfmt, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL)) {
+       ooh323_set_write_format(call, tmpfmt, ((OOCapParams *)(pChannel->chanCap->params))->txframes);
+    } else if (ast_format_cmp(tmpfmt, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
+        ooh323_set_write_format(call, tmpfmt, ((OOCapParams *)(pChannel->chanCap->params))->txframes*10);
+  } else {
+       ooh323_set_write_format(call, tmpfmt, 0);
       }
    }else{
       ast_log(LOG_ERROR, "Invalid capability type for receive channel %s\n",
@@ -693,41 +661,32 @@ int ooh323c_stop_transmit_datachannel(ooCallData *call, ooLogicalChannel *pChann
    return 1;
 }
 
-struct ast_format *convertH323CapToAsteriskCap(int cap, struct ast_format *result)
+struct ast_format *convertH323CapToAsteriskCap(int cap)
 {
-   ast_format_clear(result);
    switch(cap)
    {
       case OO_G711ULAW64K:
-         return ast_format_set(result, AST_FORMAT_ULAW, 0);
+         return ast_format_ulaw;
       case OO_G711ALAW64K:
-         return ast_format_set(result, AST_FORMAT_ALAW, 0);
+         return ast_format_alaw;
       case OO_GSMFULLRATE:
-         return ast_format_set(result, AST_FORMAT_GSM, 0);
-
-#ifdef AST_FORMAT_AMRNB
-      case OO_AMRNB:
-         return ast_format_set(result, AST_FORMAT_AMRNB, 0);
-#endif
-#ifdef AST_FORMAT_SPEEX
+         return ast_format_gsm;
       case OO_SPEEX:
-         return ast_format_set(result, AST_FORMAT_SPEEX, 0);
-#endif
-
+         return ast_format_speex;
       case OO_G729:
-         return ast_format_set(result, AST_FORMAT_G729A, 0);
+         return ast_format_g729;
       case OO_G729A:
-         return ast_format_set(result, AST_FORMAT_G729A, 0);
+         return ast_format_g729;
       case OO_G729B:
-         return ast_format_set(result, AST_FORMAT_G729A, 0);
+         return ast_format_g729;
       case OO_G7231:
-         return ast_format_set(result, AST_FORMAT_G723_1, 0);
+         return ast_format_g723;
       case OO_G726:
-         return ast_format_set(result, AST_FORMAT_G726, 0);
+         return ast_format_g726;
       case OO_G726AAL2:
-         return ast_format_set(result, AST_FORMAT_G726_AAL2, 0);
+         return ast_format_g726_aal2;
       case OO_H263VIDEO:
-         return ast_format_set(result, AST_FORMAT_H263, 0);
+         return ast_format_h263;
       default:
          ast_debug(1, "Cap %d is not supported by driver yet\n", cap);
          return NULL;
index af03eea..9819801 100644 (file)
@@ -37,9 +37,9 @@ int ooh323c_stop_stack_thread(void);
 int ooh323c_start_call_thread(ooCallData *call);
 int ooh323c_stop_call_thread(ooCallData *call);
 int ooh323c_set_capability
-   (struct ast_codec_pref *prefs, struct ast_format_cap *cap, int dtmf, int dtmfcodec);
-struct ast_format *convertH323CapToAsteriskCap(int cap, struct ast_format *format);
+   (struct ast_format_cap *cap, int dtmf, int dtmfcodec);
+struct ast_format *convertH323CapToAsteriskCap(int cap);
 int ooh323c_set_capability_for_call
-   (ooCallData *call, struct ast_codec_pref *prefs, struct ast_format_cap *cap, int dtmf, int dtmfcodec,
+   (ooCallData *call, struct ast_format_cap *cap, int dtmf, int dtmfcodec,
        int t38support, int g729onlyA);
 #endif
index 5095001..f531c16 100644 (file)
@@ -2136,8 +2136,8 @@ static int agent_login_exec(struct ast_channel *chan, const char *data)
        }
 
        ast_verb(2, "Agent '%s' logged in (format %s/%s)\n", agent->username,
-               ast_getformatname(ast_channel_readformat(chan)),
-               ast_getformatname(ast_channel_writeformat(chan)));
+               ast_format_get_name(ast_channel_readformat(chan)),
+               ast_format_get_name(ast_channel_writeformat(chan)));
        ast_channel_lock(chan);
        send_agent_login(chan, agent->username);
        ast_channel_unlock(chan);
index 9063e1a..4f9de36 100644 (file)
@@ -65,6 +65,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/astdb.h"
 #include "asterisk/utils.h"
 #include "asterisk/indications.h"
+#include "asterisk/format_cache.h"
 
 #define ALMRCV_CONFIG "alarmreceiver.conf"
 #define UNKNOWN_FORMAT "UNKNOWN_FORMAT"
@@ -798,19 +799,19 @@ static int alarmreceiver_exec(struct ast_channel *chan, const char *data)
        char signalling_type[64] = "";
        event_node_t *event_head = NULL;
 
-       if (ast_channel_writeformat(chan)->id != AST_FORMAT_ALAW
-               && ast_channel_writeformat(chan)->id != AST_FORMAT_ULAW) {
+       if ((ast_format_cmp(ast_channel_writeformat(chan), ast_format_ulaw) == AST_FORMAT_CMP_NOT_EQUAL) &&
+               (ast_format_cmp(ast_channel_writeformat(chan), ast_format_alaw) == AST_FORMAT_CMP_NOT_EQUAL)) {
                ast_verb(4, "AlarmReceiver: Setting write format to Mu-law\n");
-               if (ast_set_write_format_by_id(chan,AST_FORMAT_ULAW)) {
+               if (ast_set_write_format(chan, ast_format_ulaw)) {
                        ast_log(LOG_WARNING, "AlarmReceiver: Unable to set write format to Mu-law on %s\n",ast_channel_name(chan));
                        return -1;
                }
        }
 
-       if (ast_channel_readformat(chan)->id != AST_FORMAT_ALAW
-               && ast_channel_readformat(chan)->id != AST_FORMAT_ULAW) {
+       if ((ast_format_cmp(ast_channel_readformat(chan), ast_format_ulaw) == AST_FORMAT_CMP_NOT_EQUAL) &&
+               (ast_format_cmp(ast_channel_readformat(chan), ast_format_alaw) == AST_FORMAT_CMP_NOT_EQUAL)) {
                ast_verb(4, "AlarmReceiver: Setting read format to Mu-law\n");
-               if (ast_set_read_format_by_id(chan,AST_FORMAT_ULAW)) {
+               if (ast_set_read_format(chan, ast_format_ulaw)) {
                        ast_log(LOG_WARNING, "AlarmReceiver: Unable to set read format to Mu-law on %s\n",ast_channel_name(chan));
                        return -1;
                }
index d8f26b4..3dd8cc6 100644 (file)
@@ -52,6 +52,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/pbx.h"
 #include "asterisk/config.h"
 #include "asterisk/app.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="AMD" language="en_US">
@@ -163,7 +164,7 @@ static void isAnsweringMachine(struct ast_channel *chan, const char *data)
        struct ast_frame *f = NULL;
        struct ast_dsp *silenceDetector = NULL;
        int dspsilence = 0, framelength = 0;
-       struct ast_format readFormat;
+       RAII_VAR(struct ast_format *, readFormat, NULL, ao2_cleanup);
        int inInitialSilence = 1;
        int inGreeting = 0;
        int voiceDuration = 0;
@@ -202,11 +203,10 @@ static void isAnsweringMachine(struct ast_channel *chan, const char *data)
                AST_APP_ARG(argMaximumWordLength);
        );
 
-       ast_format_clear(&readFormat);
        ast_verb(3, "AMD: %s %s %s (Fmt: %s)\n", ast_channel_name(chan),
                S_COR(ast_channel_caller(chan)->ani.number.valid, ast_channel_caller(chan)->ani.number.str, "(N/A)"),
                S_COR(ast_channel_redirecting(chan)->from.number.valid, ast_channel_redirecting(chan)->from.number.str, "(N/A)"),
-               ast_getformatname(ast_channel_readformat(chan)));
+               ast_format_get_name(ast_channel_readformat(chan)));
 
        /* Lets parse the arguments. */
        if (!ast_strlen_zero(parse)) {
@@ -255,8 +255,8 @@ static void isAnsweringMachine(struct ast_channel *chan, const char *data)
                                minimumWordLength, betweenWordsSilence, maximumNumberOfWords, silenceThreshold, maximumWordLength);
 
        /* Set read format to signed linear so we get signed linear frames in */
-       ast_format_copy(&readFormat, ast_channel_readformat(chan));
-       if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR) < 0 ) {
+       readFormat = ao2_bump(ast_channel_readformat(chan));
+       if (ast_set_read_format(chan, ast_format_slin) < 0 ) {
                ast_log(LOG_WARNING, "AMD: Channel [%s]. Unable to set to linear mode, giving up\n", ast_channel_name(chan));
                pbx_builtin_setvar_helper(chan , "AMDSTATUS", "");
                pbx_builtin_setvar_helper(chan , "AMDCAUSE", "");
@@ -289,7 +289,7 @@ static void isAnsweringMachine(struct ast_channel *chan, const char *data)
                if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_NULL || f->frametype == AST_FRAME_CNG) {
                        /* If the total time exceeds the analysis time then give up as we are not too sure */
                        if (f->frametype == AST_FRAME_VOICE) {
-                               framelength = (ast_codec_get_samples(f) / DEFAULT_SAMPLES_PER_MS);
+                               framelength = (ast_codec_samples_count(f) / DEFAULT_SAMPLES_PER_MS);
                        } else {
                                framelength = 2 * maxWaitTimeForFrame;
                        }
@@ -412,7 +412,7 @@ static void isAnsweringMachine(struct ast_channel *chan, const char *data)
        pbx_builtin_setvar_helper(chan , "AMDCAUSE" , amdCause);
 
        /* Restore channel read format */
-       if (readFormat.id && ast_set_read_format(chan, &readFormat))
+       if (readFormat && ast_set_read_format(chan, readFormat))
                ast_log(LOG_WARNING, "AMD: Unable to restore read format on '%s'\n", ast_channel_name(chan));
 
        /* Free the DSP used to detect silence */
@@ -510,10 +510,10 @@ static int unload_module(void)
  */
 static int load_module(void)
 {
-       if (load_config(0))
+       if (load_config(0) || ast_register_application_xml(app, amd_exec)) {
                return AST_MODULE_LOAD_DECLINE;
-       if (ast_register_application_xml(app, amd_exec))
-               return AST_MODULE_LOAD_FAILURE;
+       }
+
        return AST_MODULE_LOAD_SUCCESS;
 }
 
index 47f755e..af69be3 100644 (file)
@@ -57,6 +57,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/autochan.h"
 #include "asterisk/stasis_channels.h"
 #include "asterisk/json.h"
+#include "asterisk/format_cache.h"
 
 #define AST_NAME_STRLEN 256
 #define NUM_SPYGROUPS 128
@@ -451,9 +452,6 @@ static int spy_generate(struct ast_channel *chan, void *data, int len, int sampl
 {
        struct chanspy_translation_helper *csth = data;
        struct ast_frame *f, *cur;
-       struct ast_format format_slin;
-
-       ast_format_set(&format_slin, AST_FORMAT_SLINEAR, 0);
 
        ast_audiohook_lock(&csth->spy_audiohook);
        if (csth->spy_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) {
@@ -464,9 +462,9 @@ static int spy_generate(struct ast_channel *chan, void *data, int len, int sampl
 
        if (ast_test_flag(&csth->flags, OPTION_READONLY)) {
                /* Option 'o' was set, so don't mix channel audio */
-               f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_READ, &format_slin);
+               f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_READ, ast_format_slin);
        } else {
-               f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, &format_slin);
+               f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, ast_format_slin);
        }
 
        ast_audiohook_unlock(&csth->spy_audiohook);
@@ -1181,7 +1179,7 @@ static int chanspy_exec(struct ast_channel *chan, const char *data)
                .volume = '#',
                .exit = '\0',
        };
-       struct ast_format oldwf;
+       RAII_VAR(struct ast_format *, oldwf, NULL, ao2_cleanup);
        int volfactor = 0;
        int res;
        char *mailbox = NULL;
@@ -1194,7 +1192,6 @@ static int chanspy_exec(struct ast_channel *chan, const char *data)
        char *parse = ast_strdupa(data);
 
        AST_STANDARD_APP_ARGS(args, parse);
-       ast_format_clear(&oldwf);
 
        if (args.spec && !strcmp(args.spec, "all"))
                args.spec = NULL;
@@ -1258,8 +1255,8 @@ static int chanspy_exec(struct ast_channel *chan, const char *data)
                ast_clear_flag(&flags, AST_FLAGS_ALL);
        }
 
-       ast_format_copy(&oldwf, ast_channel_writeformat(chan));
-       if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
+       oldwf = ao2_bump(ast_channel_writeformat(chan));
+       if (ast_set_write_format(chan, ast_format_slin) < 0) {
                ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
                return -1;
        }
@@ -1279,7 +1276,7 @@ static int chanspy_exec(struct ast_channel *chan, const char *data)
        if (fd)
                close(fd);
 
-       if (oldwf.id && ast_set_write_format(chan, &oldwf) < 0)
+       if (oldwf && ast_set_write_format(chan, oldwf) < 0)
                ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
 
        if (ast_test_flag(&flags, OPTION_EXITONHANGUP)) {
@@ -1301,7 +1298,7 @@ static int extenspy_exec(struct ast_channel *chan, const char *data)
                .volume = '#',
                .exit = '\0',
        };
-       struct ast_format oldwf;
+       RAII_VAR(struct ast_format *, oldwf, NULL, ao2_cleanup);
        int volfactor = 0;
        int res;
        char *mailbox = NULL;
@@ -1313,7 +1310,6 @@ static int extenspy_exec(struct ast_channel *chan, const char *data)
        char *parse = ast_strdupa(data);
 
        AST_STANDARD_APP_ARGS(args, parse);
-       ast_format_clear(&oldwf);
 
        if (!ast_strlen_zero(args.context) && (ptr = strchr(args.context, '@'))) {
                exten = args.context;
@@ -1383,8 +1379,8 @@ static int extenspy_exec(struct ast_channel *chan, const char *data)
                ast_clear_flag(&flags, AST_FLAGS_ALL);
        }
 
-       ast_format_copy(&oldwf, ast_channel_writeformat(chan));
-       if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
+       oldwf = ao2_bump(ast_channel_writeformat(chan));
+       if (ast_set_write_format(chan, ast_format_slin) < 0) {
                ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
                return -1;
        }
@@ -1405,7 +1401,7 @@ static int extenspy_exec(struct ast_channel *chan, const char *data)
        if (fd)
                close(fd);
 
-       if (oldwf.id && ast_set_write_format(chan, &oldwf) < 0)
+       if (oldwf && ast_set_write_format(chan, oldwf) < 0)
                ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
 
        return res;
@@ -1420,13 +1416,13 @@ static int dahdiscan_exec(struct ast_channel *chan, const char *data)
                .volume = '\0',
                .exit = '*',
        };
-       struct ast_format oldwf;
+       struct ast_format *oldwf;
        int res;
        char *mygroup = NULL;
 
        /* Coverity - This uninit_use should be ignored since this macro initializes the flags */
        ast_clear_flag(&flags, AST_FLAGS_ALL);
-       ast_format_clear(&oldwf);
+
        if (!ast_strlen_zero(data)) {
                mygroup = ast_strdupa(data);
        }
@@ -1434,16 +1430,18 @@ static int dahdiscan_exec(struct ast_channel *chan, const char *data)
        ast_set_flag(&flags, OPTION_DTMF_CYCLE);
        ast_set_flag(&flags, OPTION_DAHDI_SCAN);
 
-       ast_format_copy(&oldwf, ast_channel_writeformat(chan));
-       if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
+       oldwf = ao2_bump(ast_channel_writeformat(chan));
+       if (ast_set_write_format(chan, ast_format_slin) < 0) {
                ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
+               ao2_cleanup(oldwf);
                return -1;
        }
 
        res = common_exec(chan, &flags, 0, 0, &user_options, mygroup, NULL, spec, NULL, NULL, NULL, NULL);
 
-       if (oldwf.id && ast_set_write_format(chan, &oldwf) < 0)
+       if (oldwf && ast_set_write_format(chan, oldwf) < 0)
                ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
+       ao2_cleanup(oldwf);
 
        return res;
 }
index 9a704ac..445b196 100644 (file)
@@ -70,6 +70,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/stasis.h"
 #include "asterisk/stasis_bridges.h"
 #include "asterisk/json.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="ConfBridge" language="en_US">
@@ -676,7 +677,6 @@ static int conf_stop_record_thread(struct confbridge_conference *conference)
 static int conf_start_record(struct confbridge_conference *conference)
 {
        struct ast_format_cap *cap;
-       struct ast_format format;
 
        if (conference->record_state != CONF_RECORD_STOP) {
                return -1;
@@ -687,16 +687,16 @@ static int conf_start_record(struct confbridge_conference *conference)
                return -1;
        }
 
-       cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+       cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
        if (!cap) {
                return -1;
        }
 
-       ast_format_cap_add(cap, ast_format_set(&format, AST_FORMAT_SLINEAR, 0));
+       ast_format_cap_append(cap, ast_format_slin, 0);
 
        conference->record_chan = ast_request("CBRec", cap, NULL, NULL,
                conference->name, NULL);
-       cap = ast_format_cap_destroy(cap);
+       ao2_ref(cap, -1);
        if (!conference->record_chan) {
                return -1;
        }
@@ -1374,16 +1374,15 @@ static void leave_conference(struct confbridge_user *user)
 static int alloc_playback_chan(struct confbridge_conference *conference)
 {
        struct ast_format_cap *cap;
-       struct ast_format format;
 
-       cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+       cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
        if (!cap) {
                return -1;
        }
-       ast_format_cap_add(cap, ast_format_set(&format, AST_FORMAT_SLINEAR, 0));
+       ast_format_cap_append(cap, ast_format_slin, 0);
        conference->playback_chan = ast_request("CBAnn", cap, NULL, NULL,
                conference->name, NULL);
-       cap = ast_format_cap_destroy(cap);
+       ao2_ref(cap, -1);
        if (!conference->playback_chan) {
                return -1;
        }
@@ -3238,7 +3237,7 @@ void conf_remove_user_waiting(struct confbridge_conference *conference, struct c
 static void unregister_channel_tech(struct ast_channel_tech *tech)
 {
        ast_channel_unregister(tech);
-       tech->capabilities = ast_format_cap_destroy(tech->capabilities);
+       ao2_cleanup(tech->capabilities);
 }
 
 /*!
@@ -3253,11 +3252,11 @@ static void unregister_channel_tech(struct ast_channel_tech *tech)
  */
 static int register_channel_tech(struct ast_channel_tech *tech)
 {
-       tech->capabilities = ast_format_cap_alloc(0);
+       tech->capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
        if (!tech->capabilities) {
                return -1;
        }
-       ast_format_cap_add_all(tech->capabilities);
+       ast_format_cap_append_by_type(tech->capabilities, AST_MEDIA_TYPE_UNKNOWN);
        if (ast_channel_register(tech)) {
                ast_log(LOG_ERROR, "Unable to register channel technology %s(%s).\n",
                        tech->type, tech->description);
index 1820a1f..8f229f2 100644 (file)
@@ -43,6 +43,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/say.h"
 #include "asterisk/app.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="Dictate" language="en_US">
@@ -108,8 +109,7 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                len = 0,
                maxlen = 0,
                mode = 0;
-       struct ast_format oldr;
-       ast_format_clear(&oldr);
+       struct ast_format *oldr;
 
        snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
        if (!ast_strlen_zero(data)) {
@@ -126,9 +126,10 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
        if (args.argc > 1 && args.filename) {
                filename = args.filename;
        }
-       ast_format_copy(&oldr, ast_channel_readformat(chan));
-       if ((res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR)) < 0) {
+       oldr = ao2_bump(ast_channel_readformat(chan));
+       if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
                ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
+               ao2_cleanup(oldr);
                return -1;
        }
 
@@ -335,8 +336,9 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                        ast_frfree(f);
                }
        }
-       if (oldr.id) {
-               ast_set_read_format(chan, &oldr);
+       if (oldr) {
+               ast_set_read_format(chan, oldr);
+               ao2_ref(oldr, -1);
        }
        return 0;
 }
index 5c5be18..1794c25 100644 (file)
@@ -72,7 +72,7 @@ static int serialize_showchan(struct ast_channel *c, char *buf, size_t size)
 {
        long elapsed_seconds = 0;
        int hour = 0, min = 0, sec = 0;
-       char nf[256];
+       struct ast_str *format_buf = ast_str_alloca(64);
        char cgrp[256];
        char pgrp[256];
        struct ast_str *write_transpath = ast_str_alloca(256);
@@ -143,11 +143,11 @@ static int serialize_showchan(struct ast_channel *c, char *buf, size_t size)
                ast_state2str(ast_channel_state(c)),
                ast_channel_state(c),
                ast_channel_rings(c),
-               ast_getformatname_multiple(nf, sizeof(nf), ast_channel_nativeformats(c)),
-               ast_getformatname(ast_channel_writeformat(c)),
-               ast_getformatname(ast_channel_readformat(c)),
-               ast_getformatname(ast_channel_rawwriteformat(c)),
-               ast_getformatname(ast_channel_rawreadformat(c)),
+               ast_format_cap_get_names(ast_channel_nativeformats(c), &format_buf),
+               ast_format_get_name(ast_channel_writeformat(c)),
+               ast_format_get_name(ast_channel_readformat(c)),
+               ast_format_get_name(ast_channel_rawwriteformat(c)),
+               ast_format_get_name(ast_channel_rawreadformat(c)),
                ast_channel_writetrans(c) ? "Yes" : "No",
                ast_translate_path_to_str(ast_channel_writetrans(c), &write_transpath),
                ast_channel_readtrans(c) ? "Yes" : "No",
index df5a914..d8b2079 100644 (file)
@@ -58,11 +58,6 @@ static const char app[] = "Echo";
 static int echo_exec(struct ast_channel *chan, const char *data)
 {
        int res = -1;
-       struct ast_format format;
-
-       ast_best_codec(ast_channel_nativeformats(chan), &format);
-       ast_set_write_format(chan, &format);
-       ast_set_read_format(chan, &format);
 
        while (ast_waitfor(chan, -1) > -1) {
                struct ast_frame *f = ast_read(chan);
index e23ac43..ceff38f 100644 (file)
@@ -45,6 +45,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/stasis.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="SendFAX" language="en_US" module="app_fax">
@@ -332,9 +333,9 @@ static int fax_generator_generate(struct ast_channel *chan, void *data, int len,
     
        struct ast_frame outf = {
                .frametype = AST_FRAME_VOICE,
+               .subclass.format = ast_format_slin,
                .src = __FUNCTION__,
        };
-       ast_format_set(&outf.subclass.format, AST_FORMAT_SLINEAR, 0);
 
        if (samples > MAX_SAMPLES) {
                ast_log(LOG_WARNING, "Only generating %d samples, where %d requested\n", MAX_SAMPLES, samples);
@@ -365,8 +366,8 @@ static struct ast_generator generator = {
 static int transmit_audio(fax_session *s)
 {
        int res = -1;
-       struct ast_format original_read_fmt;
-       struct ast_format original_write_fmt;
+       struct ast_format *original_read_fmt;
+       struct ast_format *original_write_fmt = NULL;
        fax_state_t fax;
        t30_state_t *t30state;
        struct ast_frame *inf = NULL;
@@ -386,9 +387,6 @@ static int transmit_audio(fax_session *s)
 */
        };
 
-       ast_format_clear(&original_read_fmt);
-       ast_format_clear(&original_write_fmt);
-
        /* if in called party mode, try to use T.38 */
        if (s->caller_mode == FALSE) {
                /* check if we are already in T.38 mode (unlikely), or if we can request
@@ -461,22 +459,18 @@ static int transmit_audio(fax_session *s)
         t30state = &fax.t30_state;
 #endif
 
-       ast_format_copy(&original_read_fmt, ast_channel_readformat(s->chan));
-       if (original_read_fmt.id != AST_FORMAT_SLINEAR) {
-               res = ast_set_read_format_by_id(s->chan, AST_FORMAT_SLINEAR);
-               if (res < 0) {
-                       ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n");
-                       goto done;
-               }
+    original_read_fmt = ao2_bump(ast_channel_readformat(s->chan));
+       res = ast_set_read_format(s->chan, ast_format_slin);
+       if (res < 0) {
+               ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n");
+               goto done;
        }
 
-       ast_format_copy(&original_write_fmt, ast_channel_writeformat(s->chan));
-       if (original_write_fmt.id != AST_FORMAT_SLINEAR) {
-               res = ast_set_write_format_by_id(s->chan, AST_FORMAT_SLINEAR);
-               if (res < 0) {
-                       ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n");
-                       goto done;
-               }
+       original_write_fmt = ao2_bump(ast_channel_writeformat(s->chan));
+       res = ast_set_write_format(s->chan, ast_format_slin);
+       if (res < 0) {
+               ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n");
+               goto done;
        }
 
        /* Initialize T30 terminal */
@@ -529,12 +523,13 @@ static int transmit_audio(fax_session *s)
                        break;
                }
 
-               ast_debug(10, "frame %d/%u, len=%d\n", inf->frametype, (unsigned int) inf->subclass.format.id, inf->datalen);
+               ast_debug(10, "frame %d/%s, len=%d\n", inf->frametype, ast_format_get_name(inf->subclass.format), inf->datalen);
 
                /* Check the frame type. Format also must be checked because there is a chance
                   that a frame in old format was already queued before we set channel format
                   to slinear so it will still be received by ast_read */
-               if (inf->frametype == AST_FRAME_VOICE && inf->subclass.format.id == AST_FORMAT_SLINEAR) {
+               if (inf->frametype == AST_FRAME_VOICE &&
+                       (ast_format_cmp(inf->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)) {
                        if (fax_rx(&fax, inf->data.ptr, inf->samples) < 0) {
                                /* I know fax_rx never returns errors. The check here is for good style only */
                                ast_log(LOG_WARNING, "fax_rx returned error\n");
@@ -588,14 +583,16 @@ static int transmit_audio(fax_session *s)
        fax_release(&fax);
 
 done:
-       if (original_write_fmt.id != AST_FORMAT_SLINEAR) {
-               if (ast_set_write_format(s->chan, &original_write_fmt) < 0)
+       if (original_write_fmt) {
+               if (ast_set_write_format(s->chan, original_write_fmt) < 0)
                        ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", ast_channel_name(s->chan));
+               ao2_ref(original_write_fmt, -1);
        }
 
-       if (original_read_fmt.id != AST_FORMAT_SLINEAR) {
-               if (ast_set_read_format(s->chan, &original_read_fmt) < 0)
+       if (original_read_fmt) {
+               if (ast_set_read_format(s->chan, original_read_fmt) < 0)
                        ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", ast_channel_name(s->chan));
+               ao2_ref(original_read_fmt, -1);
        }
 
        return res;
index 3ccacb1..3626563 100644 (file)
@@ -63,6 +63,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/lock.h"
 #include "asterisk/app.h"
 #include "asterisk/endian.h"
+#include "asterisk/format_cache.h"
 
 #define FESTIVAL_CONFIG "festival.conf"
 #define MAXLEN 180
@@ -177,7 +178,7 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
        int res = 0;
        int fds[2];
        int needed = 0;
-       struct ast_format owriteformat;
+       struct ast_format *owriteformat;
        struct ast_frame *f;
        struct myframe {
                struct ast_frame f;
@@ -187,7 +188,6 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
                .f = { 0, },
        };
 
-       ast_format_clear(&owriteformat);
        if (pipe(fds)) {
                ast_log(LOG_WARNING, "Unable to create pipe\n");
                return -1;
@@ -199,12 +199,19 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
        ast_stopstream(chan);
        ast_indicate(chan, -1);
        
-       ast_format_copy(&owriteformat, ast_channel_writeformat(chan));
-       res = ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR);
+       owriteformat = ao2_bump(ast_channel_writeformat(chan));
+       res = ast_set_write_format(chan, ast_format_slin);
        if (res < 0) {
                ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
+               ao2_cleanup(owriteformat);
                return -1;
        }
+
+       myf.f.frametype = AST_FRAME_VOICE;
+       myf.f.subclass.format = ast_format_slin;
+       myf.f.offset = AST_FRIENDLY_OFFSET;
+       myf.f.src = __PRETTY_FUNCTION__;
+       myf.f.data.ptr = myf.frdata;
        
        res = send_waveform_to_fd(waveform, length, fds[1]);
        if (res >= 0) {
@@ -240,13 +247,8 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
                                }
                                res = read(fds[0], myf.frdata, needed);
                                if (res > 0) {
-                                       myf.f.frametype = AST_FRAME_VOICE;
-                                       ast_format_set(&myf.f.subclass.format, AST_FORMAT_SLINEAR, 0);
                                        myf.f.datalen = res;
                                        myf.f.samples = res / 2;
-                                       myf.f.offset = AST_FRIENDLY_OFFSET;
-                                       myf.f.src = __PRETTY_FUNCTION__;
-                                       myf.f.data.ptr = myf.frdata;
                                        if (ast_write(chan, &myf.f) < 0) {
                                                res = -1;
                                                ast_frfree(f);
@@ -269,8 +271,10 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
        close(fds[0]);
        close(fds[1]);
 
-       if (!res && owriteformat.id)
-               ast_set_write_format(chan, &owriteformat);
+       if (!res && owriteformat)
+               ast_set_write_format(chan, owriteformat);
+       ao2_cleanup(owriteformat);
+
        return res;
 }
 
index bdccff4..3122273 100644 (file)
@@ -48,6 +48,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/translate.h"
 #include "asterisk/app.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="ICES" language="en_US">
@@ -115,12 +116,11 @@ static int ices_exec(struct ast_channel *chan, const char *data)
        int ms = -1;
        int pid = -1;
        int flags;
-       struct ast_format oreadformat;
+       struct ast_format *oreadformat;
        struct ast_frame *f;
        char filename[256]="";
        char *c;
 
-       ast_format_clear(&oreadformat);
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
                return -1;
@@ -145,12 +145,13 @@ static int ices_exec(struct ast_channel *chan, const char *data)
                return -1;
        }
 
-       ast_format_copy(&oreadformat, ast_channel_readformat(chan));
-       res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
+       oreadformat = ao2_bump(ast_channel_readformat(chan));
+       res = ast_set_read_format(chan, ast_format_slin);
        if (res < 0) {
                close(fds[0]);
                close(fds[1]);
                ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
+               ao2_cleanup(oreadformat);
                return -1;
        }
        if (((char *)data)[0] == '/')
@@ -197,8 +198,9 @@ static int ices_exec(struct ast_channel *chan, const char *data)
 
        if (pid > -1)
                kill(pid, SIGKILL);
-       if (!res && oreadformat.id)
-               ast_set_read_format(chan, &oreadformat);
+       if (!res && oreadformat)
+               ast_set_read_format(chan, oreadformat);
+       ao2_cleanup(oreadformat);
 
        return res;
 }
index 9c59cea..8adfbc4 100644 (file)
@@ -58,6 +58,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/app.h"
 #include "asterisk/pbx.h"
 #include "asterisk/audiohook.h"
+#include "asterisk/format_cache.h"
 
 #define RESAMPLE_QUALITY 1
 
@@ -129,7 +130,7 @@ struct jack_data {
        jack_port_t *output_port;
        jack_ringbuffer_t *input_rb;
        jack_ringbuffer_t *output_rb;
-       enum ast_format_id audiohook_format_id;
+       struct ast_format *audiohook_format;
        unsigned int audiohook_rate;
        unsigned int frame_datalen;
        void *output_resampler;
@@ -394,7 +395,6 @@ static int init_jack_data(struct ast_channel *chan, struct jack_data *jack_data)
        jack_status_t status = 0;
        jack_options_t jack_options = JackNullOption;
 
-       struct ast_format format_slin;
        unsigned int channel_rate;
 
        unsigned int ringbuffer_size;
@@ -402,19 +402,17 @@ static int init_jack_data(struct ast_channel *chan, struct jack_data *jack_data)
        /* Deducing audiohook sample rate from channel format
           ATTENTION: Might be problematic, if channel has different sampling than used by audiohook!
        */
-       channel_rate = ast_format_rate(ast_channel_readformat(chan));
-       jack_data->audiohook_format_id = ast_format_slin_by_rate(channel_rate);
-
-       ast_format_set(&format_slin, jack_data->audiohook_format_id, 0);
-       jack_data->audiohook_rate = ast_format_rate(&format_slin);
+       channel_rate = ast_format_get_sample_rate(ast_channel_readformat(chan));
+       jack_data->audiohook_format = ast_format_cache_get_slin_by_rate(channel_rate);
+       jack_data->audiohook_rate = ast_format_get_sample_rate(jack_data->audiohook_format);
 
        /* Guessing frame->datalen assuming a ptime of 20ms */
        jack_data->frame_datalen = jack_data->audiohook_rate / 50;
 
        ringbuffer_size = jack_data->frame_datalen * RINGBUFFER_FRAME_CAPACITY;
 
-       ast_debug(1, "Audiohook parameters: slin-format:%d, rate:%d, frame-len:%d, ringbuffer_size: %d\n",
-           jack_data->audiohook_format_id, jack_data->audiohook_rate, jack_data->frame_datalen, ringbuffer_size);
+       ast_debug(1, "Audiohook parameters: slin-format:%s, rate:%d, frame-len:%d, ringbuffer_size: %d\n",
+           ast_format_get_name(jack_data->audiohook_format), jack_data->audiohook_rate, jack_data->frame_datalen, ringbuffer_size);
 
        if (!ast_strlen_zero(jack_data->client_name)) {
                client_name = jack_data->client_name;
@@ -628,12 +626,12 @@ static void handle_jack_audio(struct ast_channel *chan, struct jack_data *jack_d
        short buf[jack_data->frame_datalen];
        struct ast_frame f = {
                .frametype = AST_FRAME_VOICE,
+               .subclass.format = jack_data->audiohook_format,
                .src = "JACK",
                .data.ptr = buf,
                .datalen = sizeof(buf),
                .samples = ARRAY_LEN(buf),
        };
-       ast_format_set(&f.subclass.format, jack_data->audiohook_format_id, 0);
 
        for (;;) {
                size_t res, read_len;
@@ -778,12 +776,12 @@ static int jack_exec(struct ast_channel *chan, const char *data)
                return -1;
        }
 
-       if (ast_set_read_format_by_id(chan, jack_data->audiohook_format_id)) {
+       if (ast_set_read_format(chan, jack_data->audiohook_format)) {
                destroy_jack_data(jack_data);
                return -1;
        }
 
-       if (ast_set_write_format_by_id(chan, jack_data->audiohook_format_id)) {
+       if (ast_set_write_format(chan, jack_data->audiohook_format)) {
                destroy_jack_data(jack_data);
                return -1;
        }
@@ -859,9 +857,10 @@ static int jack_hook_callback(struct ast_audiohook *audiohook, struct ast_channe
 
        jack_data = datastore->data;
 
-       if (frame->subclass.format.id != jack_data->audiohook_format_id) {
-               ast_log(LOG_WARNING, "Expected frame in SLINEAR with id %d for the audiohook, but got format %s\n",
-                   jack_data->audiohook_format_id, ast_getformatname(&frame->subclass.format));
+       if (ast_format_cmp(frame->subclass.format, jack_data->audiohook_format) == AST_FORMAT_CMP_NOT_EQUAL) {
+               ast_log(LOG_WARNING, "Expected frame in %s for the audiohook, but got format %s\n",
+                       ast_format_get_name(jack_data->audiohook_format),
+                       ast_format_get_name(frame->subclass.format));
                ast_channel_unlock(chan);
                return 0;
        }
index 1a780e6..6d0b5ac 100644 (file)
@@ -77,6 +77,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/stasis_channels.h"
 #include "asterisk/stasis_message_router.h"
 #include "asterisk/json.h"
+#include "asterisk/format_compatibility.h"
 
 #include "enter.h"
 #include "leave.h"
@@ -1606,8 +1607,7 @@ static struct ast_conference *build_conf(const char *confno, const char *pin,
        struct ast_conference *cnf;
        struct dahdi_confinfo dahdic = { 0, };
        int confno_int = 0;
-       struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-       struct ast_format tmp_fmt;
+       struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 
        AST_LIST_LOCK(&confs);
 
@@ -1619,7 +1619,7 @@ static struct ast_conference *build_conf(const char *confno, const char *pin,
        if (cnf || (!make && !dynamic) || !cap_slin)
                goto cnfout;
 
-       ast_format_cap_add(cap_slin, ast_format_set(&tmp_fmt, AST_FORMAT_SLINEAR, 0));
+       ast_format_cap_append(cap_slin, ast_format_slin, 0);
        /* Make a new one */
        if (!(cnf = ast_calloc(1, sizeof(*cnf))) ||
                !(cnf->usercontainer = ao2_container_alloc(1, NULL, user_no_cmp))) {
@@ -1667,8 +1667,8 @@ static struct ast_conference *build_conf(const char *confno, const char *pin,
        /* Setup a new channel for playback of audio files */
        cnf->chan = ast_request("DAHDI", cap_slin, NULL, chan, "pseudo", NULL);
        if (cnf->chan) {
-               ast_set_read_format_by_id(cnf->chan, AST_FORMAT_SLINEAR);
-               ast_set_write_format_by_id(cnf->chan, AST_FORMAT_SLINEAR);
+               ast_set_read_format(cnf->chan, ast_format_slin);
+               ast_set_write_format(cnf->chan, ast_format_slin);
                dahdic.chan = 0;
                dahdic.confno = cnf->dahdiconf;
                dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
@@ -1704,7 +1704,7 @@ static struct ast_conference *build_conf(const char *confno, const char *pin,
                conf_map[confno_int] = 1;
        
 cnfout:
-       cap_slin = ast_format_cap_destroy(cap_slin);
+       ao2_cleanup(cap_slin);
        if (cnf)
                ast_atomic_fetchadd_int(&cnf->refcount, refcount);
 
@@ -3089,8 +3089,8 @@ static void meetme_menu_admin_extended(enum menu_modes *menu_mode, int *dtmf,
                        if ((conf->recordthread == AST_PTHREADT_NULL) && ast_test_flag64(confflags, CONFFLAG_RECORDCONF) && ((conf->lchan = ast_request("DAHDI", cap_slin, NULL, chan, "pseudo", NULL)))) {
                                struct dahdi_confinfo dahdic;
 
-                               ast_set_read_format_by_id(conf->lchan, AST_FORMAT_SLINEAR);
-                               ast_set_write_format_by_id(conf->lchan, AST_FORMAT_SLINEAR);
+                               ast_set_read_format(conf->lchan, ast_format_slin);
+                               ast_set_write_format(conf->lchan, ast_format_slin);
                                dahdic.chan = 0;
                                dahdic.confno = conf->dahdiconf;
                                dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
@@ -3217,13 +3217,12 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, struc
        int setusercount = 0;
        int confsilence = 0, totalsilence = 0;
        char *mailbox, *context;
-       struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-       struct ast_format tmpfmt;
+       struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 
        if (!cap_slin) {
                goto conf_run_cleanup;
        }
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
+       ast_format_cap_append(cap_slin, ast_format_slin, 0);
 
        if (!(user = ao2_alloc(sizeof(*user), NULL))) {
                goto conf_run_cleanup;
@@ -3345,8 +3344,8 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, struc
        ast_mutex_lock(&conf->recordthreadlock);
        if ((conf->recordthread == AST_PTHREADT_NULL) && ast_test_flag64(confflags, CONFFLAG_RECORDCONF) &&
                ((conf->lchan = ast_request("DAHDI", cap_slin, NULL, chan, "pseudo", NULL)))) {
-               ast_set_read_format_by_id(conf->lchan, AST_FORMAT_SLINEAR);
-               ast_set_write_format_by_id(conf->lchan, AST_FORMAT_SLINEAR);
+               ast_set_read_format(conf->lchan, ast_format_slin);
+               ast_set_write_format(conf->lchan, ast_format_slin);
                dahdic.chan = 0;
                dahdic.confno = conf->dahdiconf;
                dahdic.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
@@ -3572,12 +3571,12 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, struc
                ast_indicate(chan, -1);
        }
 
-       if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
+       if (ast_set_write_format(chan, ast_format_slin) < 0) {
                ast_log(LOG_WARNING, "Unable to set '%s' to write linear mode\n", ast_channel_name(chan));
                goto outrun;
        }
 
-       if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
+       if (ast_set_read_format(chan, ast_format_slin) < 0) {
                ast_log(LOG_WARNING, "Unable to set '%s' to read linear mode\n", ast_channel_name(chan));
                goto outrun;
        }
@@ -4131,7 +4130,7 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, struc
                                        dtmfstr[1] = '\0';
                                }
 
-                               if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id == AST_FORMAT_SLINEAR)) {
+                               if ((f->frametype == AST_FRAME_VOICE) && (ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)) {
                                        if (user->talk.actual) {
                                                ast_frame_adjust_volume(f, user->talk.actual);
                                        }
@@ -4289,7 +4288,7 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, struc
                                if (res > 0) {
                                        memset(&fr, 0, sizeof(fr));
                                        fr.frametype = AST_FRAME_VOICE;
-                                       ast_format_set(&fr.subclass.format, AST_FORMAT_SLINEAR, 0);
+                                       fr.subclass.format = ast_format_slin;
                                        fr.datalen = res;
                                        fr.samples = res / 2;
                                        fr.data.ptr = buf;
@@ -4301,7 +4300,7 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, struc
                                                 )) {
                                                int idx;
                                                for (idx = 0; idx < AST_FRAME_BITS; idx++) {
-                                                       if (ast_format_to_old_bitfield(ast_channel_rawwriteformat(chan)) & (1 << idx)) {
+                                                       if (ast_format_compatibility_format2bitfield(ast_channel_rawwriteformat(chan)) & (1 << idx)) {
                                                                break;
                                                        }
                                                }
@@ -4319,11 +4318,7 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, struc
                                                                        mohtempstopped = 1;
                                                                }
                                                                if (!conf->transpath[idx]) {
-                                                                       struct ast_format src;
-                                                                       struct ast_format dst;
-                                                                       ast_format_set(&src, AST_FORMAT_SLINEAR, 0);
-                                                                       ast_format_from_old_bitfield(&dst, (1 << idx));
-                                                                       conf->transpath[idx] = ast_translator_build_path(&dst, &src);
+                                                                       conf->transpath[idx] = ast_translator_build_path(ast_channel_rawwriteformat(chan), ast_format_slin);
                                                                }
                                                                if (conf->transpath[idx]) {
                                                                        conf->transframe[idx] = ast_translate(conf->transpath[idx], conf->origframe, 0);
@@ -4485,7 +4480,7 @@ bailoutandtrynormal:
 
 
 conf_run_cleanup:
-       cap_slin = ast_format_cap_destroy(cap_slin);
+       ao2_cleanup(cap_slin);
 
        return ret;
 }
index a4adc08..a350958 100644 (file)
@@ -37,6 +37,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/channel.h"
 #include "asterisk/pbx.h"
 #include "asterisk/indications.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="Milliwatt" language="en_US">
@@ -79,13 +80,14 @@ static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int
 {
        unsigned char buf[AST_FRIENDLY_OFFSET + 640];
        const int maxsamples = ARRAY_LEN(buf) - (AST_FRIENDLY_OFFSET / sizeof(buf[0]));
-       int i, *indexp = (int *) data;
+       int i, *indexp = (int *) data, res;
        struct ast_frame wf = {
                .frametype = AST_FRAME_VOICE,
                .offset = AST_FRIENDLY_OFFSET,
                .src = __FUNCTION__,
        };
-       ast_format_set(&wf.subclass.format, AST_FORMAT_ULAW, 0);
+
+       wf.subclass.format = ast_format_ulaw;
        wf.data.ptr = buf + AST_FRIENDLY_OFFSET;
 
        /* Instead of len, use samples, because channel.c generator_force
@@ -108,7 +110,10 @@ static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int
                *indexp &= 7;
        }
 
-       if (ast_write(chan,&wf) < 0) {
+       res = ast_write(chan, &wf);
+       ast_frfree(&wf);
+
+       if (res < 0) {
                ast_log(LOG_WARNING,"Failed to write frame to '%s': %s\n",ast_channel_name(chan),strerror(errno));
                return -1;
        }
@@ -124,8 +129,8 @@ static struct ast_generator milliwattgen = {
 
 static int old_milliwatt_exec(struct ast_channel *chan)
 {
-       ast_set_write_format_by_id(chan, AST_FORMAT_ULAW);
-       ast_set_read_format_by_id(chan, AST_FORMAT_ULAW);
+       ast_set_write_format(chan, ast_format_ulaw);
+       ast_set_read_format(chan, ast_format_ulaw);
 
        if (ast_channel_state(chan) != AST_STATE_UP) {
                ast_answer(chan);
index ab1d0ba..1e7833c 100644 (file)
@@ -57,6 +57,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/linkedlists.h"
 #include "asterisk/test.h"
 #include "asterisk/mixmonitor.h"
+#include "asterisk/format_cache.h"
 #include "asterisk/beep.h"
 
 /*** DOCUMENTATION
@@ -616,7 +617,7 @@ static void mixmonitor_save_prep(struct mixmonitor *mixmonitor, char *filename,
                                *errflag = 1;
                        } else {
                                struct ast_filestream *tmp = *fs;
-                               mixmonitor->mixmonitor_ds->samp_rate = MAX(mixmonitor->mixmonitor_ds->samp_rate, ast_format_rate(&tmp->fmt->format));
+                               mixmonitor->mixmonitor_ds->samp_rate = MAX(mixmonitor->mixmonitor_ds->samp_rate, ast_format_get_sample_rate(tmp->fmt->format));
                        }
                }
        }
@@ -635,7 +636,7 @@ static void *mixmonitor_thread(void *obj)
 
        unsigned int oflags;
        int errflag = 0;
-       struct ast_format format_slin;
+       struct ast_format *format_slin;
 
        /* Keep callid association before any log messages */
        if (mixmonitor->callid) {
@@ -653,11 +654,10 @@ static void *mixmonitor_thread(void *obj)
        mixmonitor_save_prep(mixmonitor, mixmonitor->filename_read, fs_read, &oflags, &errflag, &fs_read_ext);
        mixmonitor_save_prep(mixmonitor, mixmonitor->filename_write, fs_write, &oflags, &errflag, &fs_write_ext);
 
-       ast_format_set(&format_slin, ast_format_slin_by_rate(mixmonitor->mixmonitor_ds->samp_rate), 0);
+       format_slin = ast_format_cache_get_slin_by_rate(mixmonitor->mixmonitor_ds->samp_rate);
 
        ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock);
 
-
        /* The audiohook must enter and exit the loop locked */
        ast_audiohook_lock(&mixmonitor->audiohook);
        while (mixmonitor->audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING && !mixmonitor->mixmonitor_ds->fs_quit) {
@@ -665,7 +665,7 @@ static void *mixmonitor_thread(void *obj)
                struct ast_frame *fr_read = NULL;
                struct ast_frame *fr_write = NULL;
 
-               if (!(fr = ast_audiohook_read_frame_all(&mixmonitor->audiohook, SAMPLES_PER_FRAME, &format_slin,
+               if (!(fr = ast_audiohook_read_frame_all(&mixmonitor->audiohook, SAMPLES_PER_FRAME, format_slin,
                                                &fr_read, &fr_write))) {
                        ast_audiohook_trigger_wait(&mixmonitor->audiohook);
 
index 463a919..7e4e8e4 100644 (file)
@@ -47,6 +47,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/translate.h"
 #include "asterisk/app.h"
+#include "asterisk/format_cache.h"
 
 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
 #define MPG_123 "/usr/bin/mpg123"
@@ -143,7 +144,7 @@ static int mp3_exec(struct ast_channel *chan, const char *data)
        int fds[2];
        int ms = -1;
        int pid = -1;
-       struct ast_format owriteformat;
+       RAII_VAR(struct ast_format *, owriteformat, NULL, ao2_cleanup);
        int timeout = 2000;
        struct timeval next;
        struct ast_frame *f;
@@ -155,7 +156,6 @@ static int mp3_exec(struct ast_channel *chan, const char *data)
                .f = { 0, },
        };
 
-       ast_format_clear(&owriteformat);
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
                return -1;
@@ -168,12 +168,21 @@ static int mp3_exec(struct ast_channel *chan, const char *data)
        
        ast_stopstream(chan);
 
-       ast_format_copy(&owriteformat, ast_channel_writeformat(chan));
-       res = ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR);
+       owriteformat = ao2_bump(ast_channel_writeformat(chan));
+       res = ast_set_write_format(chan, ast_format_slin);
        if (res < 0) {
                ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
                return -1;
        }
+
+       myf.f.frametype = AST_FRAME_VOICE;
+       myf.f.subclass.format = ast_format_slin;
+       myf.f.mallocd = 0;
+       myf.f.offset = AST_FRIENDLY_OFFSET;
+       myf.f.src = __PRETTY_FUNCTION__;
+       myf.f.delivery.tv_sec = 0;
+       myf.f.delivery.tv_usec = 0;
+       myf.f.data.ptr = myf.frdata;
        
        res = mp3play(data, fds[1]);
        if (!strncasecmp(data, "http://", 7)) {
@@ -191,16 +200,8 @@ static int mp3_exec(struct ast_channel *chan, const char *data)
                        if (ms <= 0) {
                                res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
                                if (res > 0) {
-                                       myf.f.frametype = AST_FRAME_VOICE;
-                                       ast_format_set(&myf.f.subclass.format, AST_FORMAT_SLINEAR, 0);
                                        myf.f.datalen = res;
                                        myf.f.samples = res / 2;
-                                       myf.f.mallocd = 0;
-                                       myf.f.offset = AST_FRIENDLY_OFFSET;
-                                       myf.f.src = __PRETTY_FUNCTION__;
-                                       myf.f.delivery.tv_sec = 0;
-                                       myf.f.delivery.tv_usec = 0;
-                                       myf.f.data.ptr = myf.frdata;
                                        if (ast_write(chan, &myf.f) < 0) {
                                                res = -1;
                                                break;
@@ -241,8 +242,10 @@ static int mp3_exec(struct ast_channel *chan, const char *data)
        
        if (pid > -1)
                kill(pid, SIGKILL);
-       if (!res && owriteformat.id)
-               ast_set_write_format(chan, &owriteformat);
+       if (!res && owriteformat)
+               ast_set_write_format(chan, owriteformat);
+
+       ast_frfree(&myf.f);
        
        return res;
 }
index 8cf02b3..435af8a 100644 (file)
@@ -46,6 +46,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/translate.h"
 #include "asterisk/app.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="NBScat" language="en_US">
@@ -115,7 +116,7 @@ static int NBScat_exec(struct ast_channel *chan, const char *data)
        int fds[2];
        int ms = -1;
        int pid = -1;
-       struct ast_format owriteformat;
+       struct ast_format *owriteformat;
        struct timeval next;
        struct ast_frame *f;
        struct myframe {
@@ -124,7 +125,6 @@ static int NBScat_exec(struct ast_channel *chan, const char *data)
                short frdata[160];
        } myf;
 
-       ast_format_clear(&owriteformat);
        if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
                ast_log(LOG_WARNING, "Unable to create socketpair\n");
                return -1;
@@ -132,12 +132,22 @@ static int NBScat_exec(struct ast_channel *chan, const char *data)
        
        ast_stopstream(chan);
 
-       ast_format_copy(&owriteformat, ast_channel_writeformat(chan));
-       res = ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR);
+       owriteformat = ao2_bump(ast_channel_writeformat(chan));
+       res = ast_set_write_format(chan, ast_format_slin);
        if (res < 0) {
                ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
+               ao2_cleanup(owriteformat);
                return -1;
        }
+
+       myf.f.frametype = AST_FRAME_VOICE;
+       myf.f.subclass.format = ast_format_slin;
+       myf.f.mallocd = 0;
+       myf.f.offset = AST_FRIENDLY_OFFSET;
+       myf.f.src = __PRETTY_FUNCTION__;
+       myf.f.delivery.tv_sec = 0;
+       myf.f.delivery.tv_usec = 0;
+       myf.f.data.ptr = myf.frdata;
        
        res = NBScatplay(fds[1]);
        /* Wait 1000 ms first */
@@ -152,16 +162,8 @@ static int NBScat_exec(struct ast_channel *chan, const char *data)
                        if (ms <= 0) {
                                res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
                                if (res > 0) {
-                                       myf.f.frametype = AST_FRAME_VOICE;
-                                       ast_format_set(&myf.f.subclass.format, AST_FORMAT_SLINEAR, 0);
                                        myf.f.datalen = res;
                                        myf.f.samples = res / 2;
-                                       myf.f.mallocd = 0;
-                                       myf.f.offset = AST_FRIENDLY_OFFSET;
-                                       myf.f.src = __PRETTY_FUNCTION__;
-                                       myf.f.delivery.tv_sec = 0;
-                                       myf.f.delivery.tv_usec = 0;
-                                       myf.f.data.ptr = myf.frdata;
                                        if (ast_write(chan, &myf.f) < 0) {
                                                res = -1;
                                                break;
@@ -199,11 +201,13 @@ static int NBScat_exec(struct ast_channel *chan, const char *data)
        }
        close(fds[0]);
        close(fds[1]);
+       ast_frfree(&myf.f);
        
        if (pid > -1)
                kill(pid, SIGKILL);
-       if (!res && owriteformat.id)
-               ast_set_write_format(chan, &owriteformat);
+       if (!res && owriteformat)
+               ast_set_write_format(chan, owriteformat);
+       ao2_cleanup(owriteformat);
 
        return res;
 }
index 9fceb08..15898b2 100644 (file)
@@ -45,6 +45,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/pbx.h"
 #include "asterisk/module.h"
 #include "asterisk/app.h"
+#include "asterisk/format_cache.h"
 
 static const char app_originate[] = "Originate";
 
@@ -112,22 +113,22 @@ static int originate_exec(struct ast_channel *chan, const char *data)
        int outgoing_status = 0;
        unsigned int timeout = 30;
        static const char default_exten[] = "s";
-       struct ast_format tmpfmt;
-       struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+       struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 
        ast_autoservice_start(chan);
        if (!cap_slin) {
                goto return_cleanup;
        }
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR12, 0));
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0));
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR24, 0));
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR32, 0));
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR44, 0));
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR48, 0));
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR96, 0));
-       ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR192, 0));
+
+       ast_format_cap_append(cap_slin, ast_format_slin, 0);
+       ast_format_cap_append(cap_slin, ast_format_slin12, 0);
+       ast_format_cap_append(cap_slin, ast_format_slin16, 0);
+       ast_format_cap_append(cap_slin, ast_format_slin24, 0);
+       ast_format_cap_append(cap_slin, ast_format_slin32, 0);
+       ast_format_cap_append(cap_slin, ast_format_slin44, 0);
+       ast_format_cap_append(cap_slin, ast_format_slin48, 0);
+       ast_format_cap_append(cap_slin, ast_format_slin96, 0);
+       ast_format_cap_append(cap_slin, ast_format_slin192, 0);
 
        if (ast_strlen_zero(data)) {
                ast_log(LOG_ERROR, "Originate() requires arguments\n");
@@ -222,7 +223,7 @@ return_cleanup:
                        break;
                }
        }
-       cap_slin = ast_format_cap_destroy(cap_slin);
+       ao2_cleanup(cap_slin);
        ast_autoservice_stop(chan);
 
        return res;
index 45f1d86..4008fc0 100644 (file)
@@ -39,6 +39,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/app.h"
 #include "asterisk/channel.h"
 #include "asterisk/dsp.h"      /* use dsp routines for silence detection */
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="Record" language="en_US">
@@ -196,7 +197,7 @@ static int record_exec(struct ast_channel *chan, const char *data)
        int maxduration = 0;            /* max duration of recording in milliseconds */
        int gottimeout = 0;             /* did we timeout for maxduration exceeded? */
        int terminator = '#';
-       struct ast_format rfmt;
+       RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
        int ioflags;
        struct ast_silence_generator *silgen = NULL;
        struct ast_flags flags = { 0, };
@@ -209,8 +210,6 @@ static int record_exec(struct ast_channel *chan, const char *data)
        int ms;
        struct timeval start;
 
-       ast_format_clear(&rfmt);
-
        /* The next few lines of code parse out the filename and header from the input string */
        if (ast_strlen_zero(data)) { /* no data implies no filename or anything is present */
                ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
@@ -331,8 +330,8 @@ static int record_exec(struct ast_channel *chan, const char *data)
        /* The end of beep code.  Now the recording starts */
 
        if (silence > 0) {
-               ast_format_copy(&rfmt, ast_channel_readformat(chan));
-               res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
+               rfmt = ao2_bump(ast_channel_readformat(chan));
+               res = ast_set_read_format(chan, ast_format_slin);
                if (res < 0) {
                        ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
                        pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
@@ -459,8 +458,8 @@ static int record_exec(struct ast_channel *chan, const char *data)
                ast_channel_stop_silence_generator(chan, silgen);
 
 out:
-       if ((silence > 0) && rfmt.id) {
-               res = ast_set_read_format(chan, &rfmt);
+       if ((silence > 0) && rfmt) {
+               res = ast_set_read_format(chan, rfmt);
                if (res) {
                        ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", ast_channel_name(chan));
                }
index b7d2478..266f8ab 100644 (file)
@@ -56,6 +56,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/callerid.h"
 #include "asterisk/utils.h"
 #include "asterisk/app.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="SMS" language="en_US">
@@ -140,11 +141,11 @@ static const signed short wave[] = {
 static unsigned char wavea[80];
 typedef unsigned char output_t;
 static const output_t *wave_out = wavea;    /* outgoing samples */
-#define __OUT_FMT AST_FORMAT_ALAW;
+#define __OUT_FMT ast_format_alaw
 #else
 typedef signed short output_t;
 static const output_t *wave_out = wave;     /* outgoing samples */
-#define __OUT_FMT AST_FORMAT_SLINEAR
+#define __OUT_FMT ast_format_slin
 #endif
 
 #define OSYNC_BITS     80                      /* initial sync bits */
@@ -1599,7 +1600,7 @@ static int sms_generate(struct ast_channel *chan, void *data, int len, int sampl
 #define MAXSAMPLES (800)
        output_t *buf;
        sms_t *h = data;
-       int i;
+       int i, res;
 
        if (samples > MAXSAMPLES) {
                ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n",
@@ -1610,7 +1611,7 @@ static int sms_generate(struct ast_channel *chan, void *data, int len, int sampl
        buf = ast_alloca(len);
 
        f.frametype = AST_FRAME_VOICE;
-       ast_format_set(&f.subclass.format, __OUT_FMT, 0);
+       f.subclass.format = __OUT_FMT;
        f.datalen = samples * sizeof(*buf);
        f.offset = AST_FRIENDLY_OFFSET;
        f.mallocd = 0;
@@ -1660,7 +1661,9 @@ static int sms_generate(struct ast_channel *chan, void *data, int len, int sampl
                        }
                }
        }
-       if (ast_write(chan, &f) < 0) {
+       res = ast_write(chan, &f);
+       ast_frfree(&f);
+       if (res < 0) {
                ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", ast_channel_name(chan), strerror(errno));
                return -1;
        }
@@ -2012,9 +2015,9 @@ static int sms_exec(struct ast_channel *chan, const char *data)
                sms_messagetx(&h);
        }
 
-       res = ast_set_write_format_by_id(chan, __OUT_FMT);
+       res = ast_set_write_format(chan, __OUT_FMT);
        if (res >= 0) {
-               res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
+               res = ast_set_read_format(chan, ast_format_slin);
        }
        if (res < 0) {
                ast_log(LOG_ERROR, "Unable to set to linear mode, giving up\n");
index e9ca63e..e52425d 100644 (file)
@@ -672,7 +672,7 @@ static int speech_background(struct ast_channel *chan, const char *data)
        int res = 0, done = 0, started = 0, quieted = 0, max_dtmf_len = 0;
        struct ast_speech *speech = find_speech(chan);
        struct ast_frame *f = NULL;
-       struct ast_format oldreadformat;
+       RAII_VAR(struct ast_format *, oldreadformat, NULL, ao2_cleanup);
        char dtmf[AST_MAX_EXTENSION] = "";
        struct timeval start = { 0, 0 }, current;
        struct ast_datastore *datastore = NULL;
@@ -688,7 +688,6 @@ static int speech_background(struct ast_channel *chan, const char *data)
        parse = ast_strdupa(data);
        AST_STANDARD_APP_ARGS(args, parse);
 
-       ast_format_clear(&oldreadformat);
        if (speech == NULL)
                return -1;
 
@@ -704,10 +703,10 @@ static int speech_background(struct ast_channel *chan, const char *data)
        }
 
        /* Record old read format */
-       ast_format_copy(&oldreadformat, ast_channel_readformat(chan));
+       oldreadformat = ao2_bump(ast_channel_readformat(chan));
 
        /* Change read format to be signed linear */
-       if (ast_set_read_format(chan, &speech->format))
+       if (ast_set_read_format(chan, speech->format))
                return -1;
 
        if (!ast_strlen_zero(args.soundfile)) {
@@ -912,7 +911,7 @@ static int speech_background(struct ast_channel *chan, const char *data)
                        ast_channel_datastore_remove(chan, datastore);
        } else {
                /* Channel is okay so restore read format */
-               ast_set_read_format(chan, &oldreadformat);
+               ast_set_read_format(chan, oldreadformat);
        }
 
        return 0;
index 5ef80c3..5ccf260 100644 (file)
@@ -42,6 +42,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/utils.h"
 #include "asterisk/dsp.h"
 #include "asterisk/app.h"
+#include "asterisk/format.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="BackgroundDetect" language="en_US">
@@ -91,7 +93,7 @@ static int background_detect_exec(struct ast_channel *chan, const char *data)
        int analysistime = -1;
        int continue_analysis = 1;
        int x;
-       struct ast_format origrformat;
+       RAII_VAR(struct ast_format *, origrformat, NULL, ao2_cleanup);
        struct ast_dsp *dsp = NULL;
        AST_DECLARE_APP_ARGS(args,
                AST_APP_ARG(filename);
@@ -101,7 +103,6 @@ static int background_detect_exec(struct ast_channel *chan, const char *data)
                AST_APP_ARG(analysistime);
        );
 
-       ast_format_clear(&origrformat);
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
                return -1;
@@ -131,8 +132,8 @@ static int background_detect_exec(struct ast_channel *chan, const char *data)
                        }
                }
 
-               ast_format_copy(&origrformat, ast_channel_readformat(chan));
-               if ((ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR))) {
+               origrformat = ao2_bump(ast_channel_readformat(chan));
+               if ((ast_set_read_format(chan, ast_format_slin))) {
                        ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
                        res = -1;
                        break;
@@ -187,7 +188,8 @@ static int background_detect_exec(struct ast_channel *chan, const char *data)
                                                ast_frfree(fr);
                                                break;
                                        }
-                               } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass.format.id == AST_FORMAT_SLINEAR) && continue_analysis) {
+                               } else if ((fr->frametype == AST_FRAME_VOICE) &&
+                               (ast_format_cmp(fr->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) && continue_analysis) {
                                        int totalsilence;
                                        int ms;
                                        res = ast_dsp_silence(dsp, fr, &totalsilence);
@@ -233,9 +235,9 @@ static int background_detect_exec(struct ast_channel *chan, const char *data)
        } while (0);
 
        if (res > -1) {
-               if (origrformat.id && ast_set_read_format(chan, &origrformat)) {
+               if (origrformat && ast_set_read_format(chan, origrformat)) {
                        ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n", 
-                               ast_channel_name(chan), ast_getformatname(&origrformat));
+                               ast_channel_name(chan), ast_format_get_name(origrformat));
                }
        }
        if (dsp) {
index 5b7b979..54139ba 100644 (file)
@@ -44,6 +44,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/app.h"
 #include "asterisk/pbx.h"
 #include "asterisk/utils.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="TestServer" language="en_US">
@@ -91,11 +92,12 @@ static int measurenoise(struct ast_channel *chan, int ms, char *who)
        short *foo;
        struct timeval start;
        struct ast_frame *f;
-       struct ast_format rformat;
+       struct ast_format *rformat;
 
-       ast_format_copy(&rformat, ast_channel_readformat(chan));
-       if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR)) {
+       rformat = ao2_bump(ast_channel_readformat(chan));
+       if (ast_set_read_format(chan, ast_format_slin)) {
                ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
+               ao2_cleanup(rformat);
                return -1;
        }
        start = ast_tvnow();
@@ -111,7 +113,8 @@ static int measurenoise(struct ast_channel *chan, int ms, char *who)
                        res = -1;
                        break;
                }
-               if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id == AST_FORMAT_SLINEAR)) {
+               if ((f->frametype == AST_FRAME_VOICE) &&
+                       (ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)) {
                        foo = (short *)f->data.ptr;
                        for (x=0;x<f->samples;x++) {
                                noise += abs(foo[x]);
@@ -121,11 +124,13 @@ static int measurenoise(struct ast_channel *chan, int ms, char *who)
                ast_frfree(f);
        }
 
-       if (rformat.id) {
-               if (ast_set_read_format(chan, &rformat)) {
+       if (rformat) {
+               if (ast_set_read_format(chan, rformat)) {
                        ast_log(LOG_NOTICE, "Unable to restore original format!\n");
+                       ao2_ref(rformat, -1);
                        return -1;
                }
+               ao2_ref(rformat, -1);
        }
        if (res < 0)
                return res;
index 9c8a4b8..8a9c0f3 100644 (file)
@@ -135,6 +135,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/astobj2.h"
 #include "asterisk/taskprocessor.h"
 #include "asterisk/test.h"
+#include "asterisk/format_cache.h"
 
 #ifdef ODBC_STORAGE
 #include "asterisk/res_odbc.h"
@@ -6112,13 +6113,16 @@ static int msg_create_from_file(struct ast_vm_recording_data *recdata)
        if ((recording_fs = ast_readfile(recdata->recording_file, recdata->recording_ext, NULL, 0, 0, VOICEMAIL_DIR_MODE))) {
                if (!ast_seekstream(recording_fs, 0, SEEK_END)) {
                        long framelength = ast_tellstream(recording_fs);
-                       struct ast_format result = {0,};
+                       struct ast_format *result;
                        /* XXX This use of ast_getformatbyname seems incorrect here. The file extension does not necessarily correspond
                         * to the name of the format. For instance, if "raw" were passed in, I don't think ast_getformatbyname would
                         * find the slinear format
                         */
-                       ast_getformatbyname(recdata->recording_ext, &result);
-                       duration = (int) (framelength / ast_format_rate(&result));
+                       result = ast_format_cache_get(recdata->recording_ext);
+                       if (result) {
+                               duration = (int) (framelength / ast_format_get_sample_rate(result));
+                               ao2_ref(result, -1);
+                       }
                }
        }
 
@@ -14026,7 +14030,7 @@ AST_TEST_DEFINE(test_voicemail_vmsayname)
 
        struct ast_channel *test_channel1 = NULL;
        int res = -1;
-       struct ast_format_cap *nativeformats;
+       struct ast_format_cap *capabilities;
 
        static const struct ast_channel_tech fake_tech = {
                .write = fake_write,
@@ -14051,12 +14055,17 @@ AST_TEST_DEFINE(test_voicemail_vmsayname)
        }
 
        /* normally this is done in the channel driver */
-       ast_format_set(ast_channel_writeformat(test_channel1), AST_FORMAT_GSM, 0);
-       nativeformats = ast_channel_nativeformats(test_channel1);
-       ast_format_cap_add(nativeformats, ast_channel_writeformat(test_channel1));
-       ast_format_set(ast_channel_rawwriteformat(test_channel1), AST_FORMAT_GSM, 0);
-       ast_format_set(ast_channel_readformat(test_channel1), AST_FORMAT_GSM, 0);
-       ast_format_set(ast_channel_rawreadformat(test_channel1), AST_FORMAT_GSM, 0);
+       capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+       if (!capabilities) {
+               goto exit_vmsayname_test;
+       }
+       ast_format_cap_append(capabilities, ast_format_gsm, 0);
+       ast_channel_nativeformats_set(test_channel1, capabilities);
+       ao2_ref(capabilities, -1);
+       ast_channel_set_writeformat(test_channel1, ast_format_gsm);
+       ast_channel_set_rawwriteformat(test_channel1, ast_format_gsm);
+       ast_channel_set_readformat(test_channel1, ast_format_gsm);
+       ast_channel_set_rawreadformat(test_channel1, ast_format_gsm);
        ast_channel_tech_set(test_channel1, &fake_tech);
 
        ast_channel_unlock(test_channel1);
index 53870df..f2111d7 100644 (file)
@@ -51,6 +51,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/pbx.h"
 #include "asterisk/dsp.h"
 #include "asterisk/module.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="WaitForSilence" language="en_US">
@@ -129,7 +130,7 @@ static char *app_noise = "WaitForNoise";
 static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, int wait_for_silence) {
        struct ast_frame *f = NULL;
        int dsptime = 0;
-       struct ast_format rfmt;
+       RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
        int res = 0;
        struct ast_dsp *sildet;  /* silence detector dsp */
        time_t now;
@@ -138,8 +139,8 @@ static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart,
        int (*ast_dsp_func)(struct ast_dsp*, struct ast_frame*, int*) =
                                wait_for_silence ? ast_dsp_silence : ast_dsp_noise;
 
-       ast_format_copy(&rfmt, ast_channel_readformat(chan)); /* Set to linear mode */
-       if ((res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR)) < 0) {
+       rfmt = ao2_bump(ast_channel_readformat(chan));
+       if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
                ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
                return -1;
        }
@@ -199,8 +200,8 @@ static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart,
        }
 
 
-       if (rfmt.id && ast_set_read_format(chan, &rfmt)) {
-               ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(&rfmt), ast_channel_name(chan));
+       if (rfmt && ast_set_read_format(chan, rfmt)) {
+               ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_format_get_name(rfmt), ast_channel_name(chan));
        }
        ast_dsp_free(sildet);
        return res;
index 34de5af..0e298e7 100644 (file)
@@ -33,6 +33,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/channel.h"
 #include "asterisk/bridge.h"
+#include "asterisk/format_cache.h"
 #include "include/confbridge.h"
 
 /* ------------------------------------------------------------------- */
@@ -56,8 +57,14 @@ static int rec_write(struct ast_channel *ast, struct ast_frame *f)
 static struct ast_channel *rec_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
 {
        struct ast_channel *chan;
-       struct ast_format format;
        const char *conf_name = data;
+       RAII_VAR(struct ast_format_cap *, capabilities, NULL, ao2_cleanup);
+
+       capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+       if (!capabilities) {
+               return NULL;
+       }
+       ast_format_cap_append_by_type(capabilities, AST_MEDIA_TYPE_UNKNOWN);
 
        chan = ast_channel_alloc(1, AST_STATE_UP, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0,
                "CBRec/conf-%s-uid-%d",
@@ -70,13 +77,13 @@ static struct ast_channel *rec_request(const char *type, struct ast_format_cap *
                ast_channel_release(chan);
                return NULL;
        }
-       ast_format_set(&format, AST_FORMAT_SLINEAR, 0);
+
        ast_channel_tech_set(chan, conf_record_get_tech());
-       ast_format_cap_add_all(ast_channel_nativeformats(chan));
-       ast_format_copy(ast_channel_writeformat(chan), &format);
-       ast_format_copy(ast_channel_rawwriteformat(chan), &format);
-       ast_format_copy(ast_channel_readformat(chan), &format);
-       ast_format_copy(ast_channel_rawreadformat(chan), &format);
+       ast_channel_nativeformats_set(chan, capabilities);
+       ast_channel_set_writeformat(chan, ast_format_slin);
+       ast_channel_set_rawwriteformat(chan, ast_format_slin);
+       ast_channel_set_readformat(chan, ast_format_slin);
+       ast_channel_set_rawreadformat(chan, ast_format_slin);
        ast_channel_unlock(chan);
        return chan;
 }
index 5eeb470..c343cc6 100644 (file)
@@ -48,6 +48,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/bridge_technology.h"
 #include "asterisk/frame.h"
 #include "asterisk/musiconhold.h"
+#include "asterisk/format_cache.h"
 
 enum holding_roles {
        HOLDING_ROLE_PARTICIPANT,
@@ -180,7 +181,7 @@ static void participant_reaction_announcer_join(struct ast_bridge_channel *bridg
 
        chan = bridge_channel->chan;
        participant_entertainment_stop(bridge_channel);
-       if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR)) {
+       if (ast_set_write_format(chan, ast_format_slin)) {
                ast_log(LOG_WARNING, "Could not make participant %s compatible.\n", ast_channel_name(chan));
        }
 }
@@ -233,7 +234,7 @@ static void handle_participant_join(struct ast_bridge_channel *bridge_channel, s
        }
 
        /* We need to get compatible with the announcer. */
-       if (ast_set_write_format_by_id(us, AST_FORMAT_SLINEAR)) {
+       if (ast_set_write_format(us, ast_format_slin)) {
                ast_log(LOG_WARNING, "Could not make participant %s compatible.\n", ast_channel_name(us));
        }
 }
@@ -270,7 +271,7 @@ static int holding_bridge_join(struct ast_bridge *bridge, struct ast_bridge_chan
                hc->role = HOLDING_ROLE_ANNOUNCER;
 
                /* The announcer should always be made compatible with signed linear */
-               if (ast_set_read_format_by_id(us, AST_FORMAT_SLINEAR)) {
+               if (ast_set_read_format(us, ast_format_slin)) {
                        ast_log(LOG_ERROR, "Could not make announcer %s compatible.\n", ast_channel_name(us));
                }
 
@@ -427,18 +428,19 @@ static void deferred_action(struct ast_bridge_channel *bridge_channel, const voi
 
 static int unload_module(void)
 {
-       ast_format_cap_destroy(holding_bridge.format_capabilities);
+       ao2_cleanup(holding_bridge.format_capabilities);
+       holding_bridge.format_capabilities = NULL;
        return ast_bridge_technology_unregister(&holding_bridge);
 }
 
 static int load_module(void)
 {
-       if (!(holding_bridge.format_capabilities = ast_format_cap_alloc(0))) {
+       if (!(holding_bridge.format_capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return AST_MODULE_LOAD_DECLINE;
        }
-       ast_format_cap_add_all_by_type(holding_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
-       ast_format_cap_add_all_by_type(holding_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
-       ast_format_cap_add_all_by_type(holding_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
+       ast_format_cap_append_by_type(holding_bridge.format_capabilities, AST_MEDIA_TYPE_AUDIO);
+       ast_format_cap_append_by_type(holding_bridge.format_capabilities, AST_MEDIA_TYPE_VIDEO);
+       ast_format_cap_append_by_type(holding_bridge.format_capabilities, AST_MEDIA_TYPE_TEXT);
 
        return ast_bridge_technology_register(&holding_bridge);
 }
index 2362ad2..4655efe 100644 (file)
@@ -137,8 +137,8 @@ static void native_rtp_bridge_start(struct ast_bridge *bridge, struct ast_channe
        RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
        RAII_VAR(struct ast_rtp_instance *, tinstance0, NULL, ao2_cleanup);
        RAII_VAR(struct ast_rtp_instance *, tinstance1, NULL, ao2_cleanup);
-       RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
-       RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
+       RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
+       RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
 
        if (c0 == c1) {
                return;
@@ -316,8 +316,8 @@ static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
        RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ao2_cleanup);
        RAII_VAR(struct ast_rtp_instance *, vinstance0, NULL, ao2_cleanup);
        RAII_VAR(struct ast_rtp_instance *, vinstance1, NULL, ao2_cleanup);
-       RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
-       RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
+       RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
+       RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
        int read_ptime0, read_ptime1, write_ptime0, write_ptime1;
 
        /* We require two channels before even considering native bridging */
@@ -374,19 +374,18 @@ static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
        if (glue1->get_codec) {
                glue1->get_codec(c1->chan, cap1);
        }
-       if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
-               char tmp0[256] = { 0, }, tmp1[256] = { 0, };
-
+       if (ast_format_cap_count(cap0) != 0 && ast_format_cap_count(cap1) != 0 && !ast_format_cap_iscompatible(cap0, cap1)) {
+               struct ast_str *codec_buf0 = ast_str_alloca(64);
+               struct ast_str *codec_buf1 = ast_str_alloca(64);
                ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
-                       ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
-                       ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
+                       ast_format_cap_get_names(cap0, &codec_buf0), ast_format_cap_get_names(cap1, &codec_buf1));
                return 0;
        }
 
-       read_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawreadformat(c0->chan))).cur_ms;
-       read_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawreadformat(c1->chan))).cur_ms;
-       write_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawwriteformat(c0->chan))).cur_ms;
-       write_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawwriteformat(c1->chan))).cur_ms;
+       read_ptime0 = ast_format_cap_get_format_framing(cap0, ast_channel_rawreadformat(c0->chan));
+       read_ptime1 = ast_format_cap_get_format_framing(cap1, ast_channel_rawreadformat(c1->chan));
+       write_ptime0 = ast_format_cap_get_format_framing(cap0, ast_channel_rawwriteformat(c0->chan));
+       write_ptime1 = ast_format_cap_get_format_framing(cap1, ast_channel_rawwriteformat(c1->chan));
 
        if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
                ast_debug(1, "Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
@@ -518,7 +517,7 @@ static struct ast_bridge_technology native_rtp_bridge = {
 
 static int unload_module(void)
 {
-       ast_format_cap_destroy(native_rtp_bridge.format_capabilities);
+       ao2_t_ref(native_rtp_bridge.format_capabilities, -1, "Dispose of capabilities in module unload");
        return ast_bridge_technology_unregister(&native_rtp_bridge);
 }
 
@@ -527,9 +526,9 @@ static int load_module(void)
        if (!(native_rtp_bridge.format_capabilities = ast_format_cap_alloc(0))) {
                return AST_MODULE_LOAD_DECLINE;
        }
-       ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
-       ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
-       ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
+       ast_format_cap_append_by_type(native_rtp_bridge.format_capabilities, AST_MEDIA_TYPE_AUDIO);
+       ast_format_cap_append_by_type(native_rtp_bridge.format_capabilities, AST_MEDIA_TYPE_VIDEO);
+       ast_format_cap_append_by_type(native_rtp_bridge.format_capabilities, AST_MEDIA_TYPE_TEXT);
 
        return ast_bridge_technology_register(&native_rtp_bridge);
 }
index 4a03dbe..1626a39 100644 (file)
@@ -76,18 +76,19 @@ static struct ast_bridge_technology simple_bridge = {
 
 static int unload_module(void)
 {
-       ast_format_cap_destroy(simple_bridge.format_capabilities);
+       ao2_cleanup(simple_bridge.format_capabilities);
+       simple_bridge.format_capabilities = NULL;
        return ast_bridge_technology_unregister(&simple_bridge);
 }
 
 static int load_module(void)
 {
-       if (!(simple_bridge.format_capabilities = ast_format_cap_alloc(0))) {
+       if (!(simple_bridge.format_capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return AST_MODULE_LOAD_DECLINE;
        }
-       ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
-       ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
-       ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
+       ast_format_cap_append_by_type(simple_bridge.format_capabilities, AST_MEDIA_TYPE_AUDIO);
+       ast_format_cap_append_by_type(simple_bridge.format_capabilities, AST_MEDIA_TYPE_VIDEO);
+       ast_format_cap_append_by_type(simple_bridge.format_capabilities, AST_MEDIA_TYPE_TEXT);
 
        return ast_bridge_technology_register(&simple_bridge);
 }
index 3f74355..f5f4c2b 100644 (file)
@@ -163,14 +163,14 @@ struct softmix_mixing_array {
 struct softmix_translate_helper_entry {
        int num_times_requested; /*!< Once this entry is no longer requested, free the trans_pvt
                                      and re-init if it was usable. */
-       struct ast_format dst_format; /*!< The destination format for this helper */
+       struct ast_format *dst_format; /*!< The destination format for this helper */
        struct ast_trans_pvt *trans_pvt; /*!< the translator for this slot. */
        struct ast_frame *out_frame; /*!< The output frame from the last translation */
        AST_LIST_ENTRY(softmix_translate_helper_entry) entry;
 };
 
 struct softmix_translate_helper {
-       struct ast_format slin_src; /*!< the source format expected for all the translators */
+       struct ast_format *slin_src; /*!< the source format expected for all the translators */
        AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries;
 };
 
@@ -180,12 +180,14 @@ static struct softmix_translate_helper_entry *softmix_translate_helper_entry_all
        if (!(entry = ast_calloc(1, sizeof(*entry)))) {
                return NULL;
        }
-       ast_format_copy(&entry->dst_format, dst);
+       entry->dst_format = ao2_bump(dst);
        return entry;
 }
 
 static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry)
 {
+       ao2_cleanup(entry->dst_format);
+
        if (entry->trans_pvt) {
                ast_translator_free_path(entry->trans_pvt);
        }
@@ -199,7 +201,7 @@ static void *softmix_translate_helper_free_entry(struct softmix_translate_helper
 static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
 {
        memset(trans_helper, 0, sizeof(*trans_helper));
-       ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
+       trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
 }
 
 static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper)
@@ -215,11 +217,11 @@ static void softmix_translate_helper_change_rate(struct softmix_translate_helper
 {
        struct softmix_translate_helper_entry *entry;
 
-       ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
+       trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
        AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
                if (entry->trans_pvt) {
                        ast_translator_free_path(entry->trans_pvt);
-                       if (!(entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src))) {
+                       if (!(entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src))) {
                                AST_LIST_REMOVE_CURRENT(entry);
                                entry = softmix_translate_helper_free_entry(entry);
                        }
@@ -274,19 +276,19 @@ static void softmix_process_write_audio(struct softmix_translate_helper *trans_h
        }
 
        AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
-               if (ast_format_cmp(&entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
+               if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
                        entry->num_times_requested++;
                } else {
                        continue;
                }
                if (!entry->trans_pvt && (entry->num_times_requested > 1)) {
-                       entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src);
+                       entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src);
                }
                if (entry->trans_pvt && !entry->out_frame) {
                        entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0);
                }
                if (entry->out_frame && (entry->out_frame->datalen < MAX_DATALEN)) {
-                       ast_format_copy(&sc->write_frame.subclass.format, &entry->out_frame->subclass.format);
+                       ao2_replace(sc->write_frame.subclass.format, entry->out_frame->subclass.format);
                        memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen);
                        sc->write_frame.datalen = entry->out_frame->datalen;
                        sc->write_frame.samples = entry->out_frame->samples;
@@ -316,32 +318,45 @@ static void softmix_translate_helper_cleanup(struct softmix_translate_helper *tr
 static void set_softmix_bridge_data(int rate, int interval, struct ast_bridge_channel *bridge_channel, int reset)
 {
        struct softmix_channel *sc = bridge_channel->tech_pvt;
-       unsigned int channel_read_rate = ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan));
+       unsigned int channel_read_rate = ast_format_get_sample_rate(ast_channel_rawreadformat(bridge_channel->chan));
 
        ast_mutex_lock(&sc->lock);
        if (reset) {
                ast_slinfactory_destroy(&sc->factory);
                ast_dsp_free(sc->dsp);
        }
-       /* Setup read/write frame parameters */
+
+       /* Setup write frame parameters */
        sc->write_frame.frametype = AST_FRAME_VOICE;
-       ast_format_set(&sc->write_frame.subclass.format, ast_format_slin_by_rate(rate), 0);
+       ao2_cleanup(sc->write_frame.subclass.format);
+       /*
+        * NOTE: The format is bumped here because translation could
+        * be needed and the format changed to the translated format
+        * for the channel.  The translated format may not be a
+        * static cached format.
+        */
+       sc->write_frame.subclass.format = ao2_bump(ast_format_cache_get_slin_by_rate(rate));
        sc->write_frame.data.ptr = sc->final_buf;
        sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval);
        sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval);
 
+       /* Setup read frame parameters */
        sc->read_frame.frametype = AST_FRAME_VOICE;
-       ast_format_set(&sc->read_frame.subclass.format, ast_format_slin_by_rate(channel_read_rate), 0);
+       /*
+        * NOTE: The format is not bumbed here because it will always
+        * be a signed linear format.
+        */
+       sc->read_frame.subclass.format = ast_format_cache_get_slin_by_rate(channel_read_rate);
        sc->read_frame.data.ptr = sc->our_buf;
        sc->read_frame.datalen = SOFTMIX_DATALEN(channel_read_rate, interval);
        sc->read_frame.samples = SOFTMIX_SAMPLES(channel_read_rate, interval);
 
        /* Setup smoother */
-       ast_slinfactory_init_with_format(&sc->factory, &sc->write_frame.subclass.format);
+       ast_slinfactory_init_with_format(&sc->factory, sc->write_frame.subclass.format);
 
        /* set new read and write formats on channel. */
-       ast_set_read_format(bridge_channel->chan, &sc->read_frame.subclass.format);
-       ast_set_write_format(bridge_channel->chan, &sc->write_frame.subclass.format);
+       ast_set_read_format(bridge_channel->chan, sc->read_frame.subclass.format);
+       ast_set_write_format(bridge_channel->chan, sc->write_frame.subclass.format);
 
        /* set up new DSP.  This is on the read side only right before the read frame enters the smoother.  */
        sc->dsp = ast_dsp_new_with_rate(channel_read_rate);
@@ -446,6 +461,9 @@ static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_ch
        /* Drop the factory */
        ast_slinfactory_destroy(&sc->factory);
 
+       /* Drop any formats on the frames */
+       ao2_cleanup(sc->write_frame.subclass.format);
+
        /* Drop the DSP */
        ast_dsp_free(sc->dsp);
 
@@ -500,7 +518,7 @@ static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bri
                ast_mutex_lock(&sc->lock);
                ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan,
                        sc->video_talker.energy_average,
-                       ast_format_get_video_mark(&frame->subclass.format));
+                       frame->subclass.frame_ending);
                ast_mutex_unlock(&sc->lock);
                video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
                if (video_src_priority == 1) {
@@ -575,8 +593,7 @@ static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bri
 
        /* If a frame was provided add it to the smoother, unless drop silence is enabled and this frame
         * is not determined to be talking. */
-       if (!(bridge_channel->tech_args.drop_silence && !sc->talking) &&
-               (frame->frametype == AST_FRAME_VOICE && ast_format_is_slinear(&frame->subclass.format))) {
+       if (!(bridge_channel->tech_args.drop_silence && !sc->talking)) {
                ast_slinfactory_feed(&sc->factory, frame);
        }
 
@@ -680,8 +697,8 @@ static void gather_softmix_stats(struct softmix_stats *stats,
        int channel_native_rate;
        int i;
        /* Gather stats about channel sample rates. */
-       channel_native_rate = MAX(ast_format_rate(ast_channel_rawwriteformat(bridge_channel->chan)),
-               ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan)));
+       channel_native_rate = MAX(ast_format_get_sample_rate(ast_channel_rawwriteformat(bridge_channel->chan)),
+               ast_format_get_sample_rate(ast_channel_rawreadformat(bridge_channel->chan)));
 
        if (channel_native_rate > stats->highest_supported_rate) {
                stats->highest_supported_rate = channel_native_rate;
@@ -845,7 +862,7 @@ static int softmix_mixing_loop(struct ast_bridge *bridge)
        while (!softmix_data->stop && bridge->num_active) {
                struct ast_bridge_channel *bridge_channel;
                int timeout = -1;
-               enum ast_format_id cur_slin_id = ast_format_slin_by_rate(softmix_data->internal_rate);
+               struct ast_format *cur_slin = ast_format_cache_get_slin_by_rate(softmix_data->internal_rate);
                unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
                unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
 
@@ -927,9 +944,8 @@ static int softmix_mixing_loop(struct ast_bridge *bridge)
                        ast_mutex_lock(&sc->lock);
 
                        /* Make SLINEAR write frame from local buffer */
-                       if (sc->write_frame.subclass.format.id != cur_slin_id) {
-                               ast_format_set(&sc->write_frame.subclass.format, cur_slin_id, 0);
-                       }
+                       ao2_t_replace(sc->write_frame.subclass.format, cur_slin,
+                               "Replace softmix channel slin format");
                        sc->write_frame.datalen = softmix_datalen;
                        sc->write_frame.samples = softmix_samples;
                        memcpy(sc->final_buf, buf, softmix_datalen);
@@ -1144,17 +1160,17 @@ static struct ast_bridge_technology softmix_bridge = {
 
 static int unload_module(void)
 {
-       ast_format_cap_destroy(softmix_bridge.format_capabilities);
+       ao2_cleanup(softmix_bridge.format_capabilities);
+       softmix_bridge.format_capabilities = NULL;
        return ast_bridge_technology_unregister(&softmix_bridge);
 }
 
 static int load_module(void)
 {
-       struct ast_format tmp;
-       if (!(softmix_bridge.format_capabilities = ast_format_cap_alloc(0))) {
+       if (!(softmix_bridge.format_capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return AST_MODULE_LOAD_DECLINE;
        }
-       ast_format_cap_add(softmix_bridge.format_capabilities, ast_format_set(&tmp, AST_FORMAT_SLINEAR, 0));
+       ast_format_cap_append(softmix_bridge.format_capabilities, ast_format_slin, 0);
        return ast_bridge_technology_register(&softmix_bridge);
 }
 
index 9d43ba1..f74e78f 100644 (file)
@@ -63,6 +63,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/musiconhold.h"
 #include "asterisk/poll-compat.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/format_cache.h"
 
 /*! Global jitterbuffer configuration - by default, jb is disabled
  *  \note Values shown here match the defaults shown in alsa.conf.sample */
@@ -511,7 +512,7 @@ static struct ast_frame *alsa_read(struct ast_channel *chan)
                }
 
                f.frametype = AST_FRAME_VOICE;
-               ast_format_set(&f.subclass.format, AST_FORMAT_SLINEAR, 0);
+               f.subclass.format = ast_format_slin;
                f.samples = FRAME_SIZE;
                f.datalen = FRAME_SIZE * 2;
                f.data.ptr = buf;
@@ -585,9 +586,9 @@ static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state, const st
 
        ast_channel_tech_set(tmp, &alsa_tech);
        ast_channel_set_fd(tmp, 0, readdev);
-       ast_format_set(ast_channel_readformat(tmp), AST_FORMAT_SLINEAR, 0);
-       ast_format_set(ast_channel_writeformat(tmp), AST_FORMAT_SLINEAR, 0);
-       ast_format_cap_add(ast_channel_nativeformats(tmp), ast_channel_writeformat(tmp));
+       ast_channel_set_readformat(tmp, ast_format_slin);
+       ast_channel_set_writeformat(tmp, ast_format_slin);
+       ast_channel_nativeformats_set(tmp, alsa_tech.capabilities);
 
        ast_channel_tech_pvt_set(tmp, p);
        if (!ast_strlen_zero(p->context))
@@ -616,14 +617,11 @@ static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state, const st
 
 static struct ast_channel *alsa_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
 {
-       struct ast_format tmpfmt;
-       char buf[256];
        struct ast_channel *tmp = NULL;
 
-       ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0);
-
-       if (!(ast_format_cap_iscompatible(cap, &tmpfmt))) {
-               ast_log(LOG_NOTICE, "Asked to get a channel of format '%s'\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
+       if (ast_format_cap_iscompatible_format(cap, ast_format_slin) == AST_FORMAT_CMP_NOT_EQUAL) {
+               struct ast_str *codec_buf = ast_str_alloca(64);
+               ast_log(LOG_NOTICE, "Asked to get a channel of format '%s'\n", ast_format_cap_get_names(cap, &codec_buf));
                return NULL;
        }
 
@@ -959,12 +957,11 @@ static int load_module(void)
        struct ast_config *cfg;
        struct ast_variable *v;
        struct ast_flags config_flags = { 0 };
-       struct ast_format tmpfmt;
 
-       if (!(alsa_tech.capabilities = ast_format_cap_alloc(0))) {
+       if (!(alsa_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return AST_MODULE_LOAD_DECLINE;
        }
-       ast_format_cap_add(alsa_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
+       ast_format_cap_append(alsa_tech.capabilities, ast_format_slin, 0);
 
        /* Copy the default jb config over global_jbconf */
        memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
@@ -1042,7 +1039,9 @@ static int unload_module(void)
        if (alsa.owner)
                return -1;
 
-       alsa_tech.capabilities = ast_format_cap_destroy(alsa_tech.capabilities);
+       ao2_cleanup(alsa_tech.capabilities);
+       alsa_tech.capabilities = NULL;
+
        return 0;
 }
 
index ac15e7d..6502b5c 100644 (file)
@@ -164,11 +164,13 @@ static struct ast_channel *record_request(const char *type, struct ast_format_ca
 static void cleanup_capabilities(void)
 {
        if (announce_tech.capabilities) {
-               announce_tech.capabilities = ast_format_cap_destroy(announce_tech.capabilities);
+               ao2_ref(announce_tech.capabilities, -1);
+               announce_tech.capabilities = NULL;
        }
 
        if (record_tech.capabilities) {
-               record_tech.capabilities = ast_format_cap_destroy(record_tech.capabilities);
+               ao2_ref(record_tech.capabilities, -1);
+               record_tech.capabilities = NULL;
        }
 }
 
@@ -182,18 +184,18 @@ static int unload_module(void)
 
 static int load_module(void)
 {
-       announce_tech.capabilities = ast_format_cap_alloc(0);
+       announce_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
        if (!announce_tech.capabilities) {
                return AST_MODULE_LOAD_DECLINE;
        }
 
-       record_tech.capabilities = ast_format_cap_alloc(0);
+       record_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
        if (!record_tech.capabilities) {
                return AST_MODULE_LOAD_DECLINE;
        }
 
-       ast_format_cap_add_all(announce_tech.capabilities);
-       ast_format_cap_add_all(record_tech.capabilities);
+       ast_format_cap_append_by_type(announce_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
+       ast_format_cap_append_by_type(record_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
 
        if (ast_channel_register(&announce_tech)) {
                ast_log(LOG_ERROR, "Unable to register channel technology %s(%s).\n",
index b1f5cdf..3aeda93 100644 (file)
@@ -77,6 +77,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/callerid.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/format_cache.h"
 
 /*! 
  * \brief The sample rate to request from PortAudio 
@@ -270,12 +271,12 @@ static void *stream_monitor(void *data)
        PaError res;
        struct ast_frame f = {
                .frametype = AST_FRAME_VOICE,
+               .subclass.format = ast_format_slin16,
                .src = "console_stream_monitor",
                .data.ptr = buf,
                .datalen = sizeof(buf),
                .samples = sizeof(buf) / sizeof(int16_t),
        };
-       ast_format_set(&f.subclass.format, AST_FORMAT_SLINEAR16, 0);
 
        for (;;) {
                pthread_testcancel();
@@ -421,19 +422,28 @@ static int stop_stream(struct console_pvt *pvt)
  */
 static struct ast_channel *console_new(struct console_pvt *pvt, const char *ext, const char *ctx, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
 {
+       struct ast_format_cap *caps;
        struct ast_channel *chan;
 
+       caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+       if (!caps) {
+               return NULL;
+       }
+
        if (!(chan = ast_channel_alloc(1, state, pvt->cid_num, pvt->cid_name, NULL, 
                ext, ctx, assignedids, requestor, 0, "Console/%s", pvt->name))) {
+               ao2_ref(caps, -1);
                return NULL;
        }
 
        ast_channel_stage_snapshot(chan);
 
        ast_channel_tech_set(chan, &console_tech);
-       ast_format_set(ast_channel_readformat(chan), AST_FORMAT_SLINEAR16, 0);
-       ast_format_set(ast_channel_writeformat(chan), AST_FORMAT_SLINEAR16, 0);
-       ast_format_cap_add(ast_channel_nativeformats(chan), ast_channel_readformat(chan));
+       ast_channel_set_readformat(chan, ast_format_slin16);
+       ast_channel_set_writeformat(chan, ast_format_slin16);
+       ast_format_cap_append(caps, ast_format_slin16, 0);
+       ast_channel_nativeformats_set(chan, caps);
+       ao2_ref(caps, -1);
        ast_channel_tech_pvt_set(chan, ref_pvt(pvt));
 
        pvt->owner = chan;
@@ -462,15 +472,16 @@ static struct ast_channel *console_request(const char *type, struct ast_format_c
 {
        struct ast_channel *chan = NULL;
        struct console_pvt *pvt;
-       char buf[512];
 
        if (!(pvt = find_pvt(data))) {
                ast_log(LOG_ERROR, "Console device '%s' not found\n", data);
                return NULL;
        }
 
-       if (!(ast_format_cap_has_joint(cap, console_tech.capabilities))) {
-               ast_log(LOG_NOTICE, "Channel requested with unsupported format(s): '%s'\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
+       if (!(ast_format_cap_iscompatible(cap, console_tech.capabilities))) {
+               struct ast_str *cap_buf = ast_str_alloca(64);
+               ast_log(LOG_NOTICE, "Channel requested with unsupported format(s): '%s'\n",
+                       ast_format_cap_get_names(cap, &cap_buf));
                goto return_unref;
        }
 
@@ -1467,7 +1478,8 @@ static void stop_streams(void)
 
 static int unload_module(void)
 {
-       console_tech.capabilities = ast_format_cap_destroy(console_tech.capabilities);
+       ao2_ref(console_tech.capabilities, -1);
+       console_tech.capabilities = NULL;
        ast_channel_unregister(&console_tech);
        ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
 
@@ -1495,13 +1507,12 @@ static int unload_module(void)
  */
 static int load_module(void)
 {
-       struct ast_format tmpfmt;
        PaError res;
 
-       if (!(console_tech.capabilities = ast_format_cap_alloc(0))) {
+       if (!(console_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return AST_MODULE_LOAD_DECLINE;
        }
-       ast_format_cap_add(console_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0));
+       ast_format_cap_append(console_tech.capabilities, ast_format_slin16, 0);
 
        init_pvt(&globals, NULL);
 
@@ -1538,6 +1549,8 @@ return_error:
        if (pvts)
                ao2_ref(pvts, -1);
        pvts = NULL;
+       ao2_ref(console_tech.capabilities, -1);
+       console_tech.capabilities = NULL;
        pvt_destructor(&globals);
 
        return AST_MODULE_LOAD_DECLINE;
index 8124253..c22833d 100644 (file)
@@ -129,6 +129,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/bridge.h"
 #include "asterisk/stasis_channels.h"
 #include "asterisk/parking.h"
+#include "asterisk/format_cache.h"
 #include "chan_dahdi.h"
 #include "dahdi/bridge_native_dahdi.h"
 
@@ -461,7 +462,7 @@ static struct ast_jb_conf global_jbconf;
 /*! \brief Typically, how many rings before we should send Caller*ID */
 #define DEFAULT_CIDRINGS 1
 
-#define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
+#define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? ast_format_alaw : ast_format_ulaw)
 
 
 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
@@ -1321,7 +1322,6 @@ static int my_get_callerid(void *pvt, char *namebuf, char *numbuf, enum analog_e
        int res;
        unsigned char buf[256];
        int flags;
-       struct ast_format tmpfmt;
 
        poller.fd = p->subs[SUB_REAL].dfd;
        poller.events = POLLPRI | POLLIN;
@@ -1354,9 +1354,9 @@ static int my_get_callerid(void *pvt, char *namebuf, char *numbuf, enum analog_e
                }
 
                if (p->cid_signalling == CID_SIG_V23_JP) {
-                       res = callerid_feed_jp(p->cs, buf, res, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+                       res = callerid_feed_jp(p->cs, buf, res, AST_LAW(p));
                } else {
-                       res = callerid_feed(p->cs, buf, res, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+                       res = callerid_feed(p->cs, buf, res, AST_LAW(p));
                }
                if (res < 0) {
                        /*
@@ -1525,7 +1525,7 @@ static int restore_conference(struct dahdi_pvt *p);
 static int my_callwait(void *pvt)
 {
        struct dahdi_pvt *p = pvt;
-       struct ast_format tmpfmt;
+
        p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
        if (p->cidspill) {
                ast_log(LOG_WARNING, "Spill already exists?!?\n");
@@ -1542,11 +1542,11 @@ static int my_callwait(void *pvt)
        /* Silence */
        memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
        if (!p->callwaitrings && p->callwaitingcallerid) {
-               ast_gen_cas(p->cidspill, 1, 2400 + 680, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+               ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
                p->callwaitcas = 1;
                p->cidlen = 2400 + 680 + READ_SIZE * 4;
        } else {
-               ast_gen_cas(p->cidspill, 1, 2400, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+               ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
                p->callwaitcas = 0;
                p->cidlen = 2400 + READ_SIZE * 4;
        }
@@ -1559,7 +1559,6 @@ static int my_callwait(void *pvt)
 static int my_send_callerid(void *pvt, int cwcid, struct ast_party_caller *caller)
 {
        struct dahdi_pvt *p = pvt;
-       struct ast_format tmpfmt;
 
        ast_debug(2, "Starting cid spill\n");
 
@@ -1573,7 +1572,7 @@ static int my_send_callerid(void *pvt, int cwcid, struct ast_party_caller *calle
                        p->cidlen = ast_callerid_generate(p->cidspill,
                                caller->id.name.str,
                                caller->id.number.str,
-                               ast_format_set(&tmpfmt, AST_LAW(p), 0));
+                               AST_LAW(p));
                } else {
                        ast_verb(3, "CPE supports Call Waiting Caller*ID.  Sending '%s/%s'\n",
                                caller->id.name.str, caller->id.number.str);
@@ -1582,7 +1581,7 @@ static int my_send_callerid(void *pvt, int cwcid, struct ast_party_caller *calle
                        p->cidlen = ast_callerid_callwaiting_generate(p->cidspill,
                                caller->id.name.str,
                                caller->id.number.str,
-                               ast_format_set(&tmpfmt, AST_LAW(p), 0));
+                               AST_LAW(p));
                        p->cidlen += READ_SIZE * 4;
                }
                p->cidpos = 0;
@@ -4965,14 +4964,12 @@ static int restore_conference(struct dahdi_pvt *p)
 
 static int send_cwcidspill(struct dahdi_pvt *p)
 {
-       struct ast_format tmpfmt;
-
        p->callwaitcas = 0;
        p->cidcwexpire = 0;
        p->cid_suppress_expire = 0;
        if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
                return -1;
-       p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+       p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
        /* Make sure we account for the end */
        p->cidlen += READ_SIZE * 4;
        p->cidpos = 0;
@@ -5038,7 +5035,7 @@ static int send_callerid(struct dahdi_pvt *p)
 static int dahdi_callwait(struct ast_channel *ast)
 {
        struct dahdi_pvt *p = ast_channel_tech_pvt(ast);
-       struct ast_format tmpfmt;
+
        p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
        if (p->cidspill) {
                ast_log(LOG_WARNING, "Spill already exists?!?\n");
@@ -5055,11 +5052,11 @@ static int dahdi_callwait(struct ast_channel *ast)
        /* Silence */
        memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
        if (!p->callwaitrings && p->callwaitingcallerid) {
-               ast_gen_cas(p->cidspill, 1, 2400 + 680, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+               ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
                p->callwaitcas = 1;
                p->cidlen = 2400 + 680 + READ_SIZE * 4;
        } else {
-               ast_gen_cas(p->cidspill, 1, 2400, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+               ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
                p->callwaitcas = 0;
                p->cidlen = 2400 + READ_SIZE * 4;
        }
@@ -8504,25 +8501,20 @@ static struct ast_frame *dahdi_read(struct ast_channel *ast)
                return f;
        }
 
-       if (ast_channel_rawreadformat(ast)->id == AST_FORMAT_SLINEAR) {
+       if (ast_format_cmp(ast_channel_rawreadformat(ast), ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
                if (!p->subs[idx].linear) {
                        p->subs[idx].linear = 1;
                        res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
                        if (res)
                                ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, idx);
                }
-       } else if ((ast_channel_rawreadformat(ast)->id == AST_FORMAT_ULAW) ||
-               (ast_channel_rawreadformat(ast)->id == AST_FORMAT_ALAW)) {
+       } else {
                if (p->subs[idx].linear) {
                        p->subs[idx].linear = 0;
                        res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
                        if (res)
                                ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to companded mode.\n", p->channel, idx);
                }
-       } else {
-               ast_log(LOG_WARNING, "Don't know how to read frames in format %s\n", ast_getformatname(ast_channel_rawreadformat(ast)));
-               ast_mutex_unlock(&p->lock);
-               return NULL;
        }
        readbuf = ((unsigned char *)p->subs[idx].buffer) + AST_FRIENDLY_OFFSET;
        CHECK_BLOCKING(ast);
@@ -8612,7 +8604,7 @@ static struct ast_frame *dahdi_read(struct ast_channel *ast)
        }
 
        p->subs[idx].f.frametype = AST_FRAME_VOICE;
-       ast_format_copy(&p->subs[idx].f.subclass.format, ast_channel_rawreadformat(ast));
+       p->subs[idx].f.subclass.format = ast_channel_rawreadformat(ast);
        p->subs[idx].f.samples = READ_SIZE;
        p->subs[idx].f.mallocd = 0;
        p->subs[idx].f.offset = AST_FRIENDLY_OFFSET;
@@ -8795,12 +8787,6 @@ static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame)
                        ast_log(LOG_WARNING, "Don't know what to do with frame type '%u'\n", frame->frametype);
                return 0;
        }
-       if ((frame->subclass.format.id != AST_FORMAT_SLINEAR) &&
-               (frame->subclass.format.id != AST_FORMAT_ULAW) &&
-               (frame->subclass.format.id != AST_FORMAT_ALAW)) {
-               ast_log(LOG_WARNING, "Cannot handle frames in %s format\n", ast_getformatname(&frame->subclass.format));
-               return -1;
-       }
        if (p->dialing) {
                ast_debug(5, "Dropping frame since I'm still dialing on %s...\n",ast_channel_name(ast));
                return 0;
@@ -8818,7 +8804,7 @@ static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame)
        if (!frame->data.ptr || !frame->datalen)
                return 0;
 
-       if (frame->subclass.format.id == AST_FORMAT_SLINEAR) {
+       if (ast_format_cmp(frame->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
                if (!p->subs[idx].linear) {
                        p->subs[idx].linear = 1;
                        res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
@@ -8826,7 +8812,8 @@ static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame)
                                ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
                }
                res = my_dahdi_write(p, (unsigned char *)frame->data.ptr, frame->datalen, idx, 1);
-       } else {
+       } else if (ast_format_cmp(frame->subclass.format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL
+               || ast_format_cmp(frame->subclass.format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
                /* x-law already */
                if (p->subs[idx].linear) {
                        p->subs[idx].linear = 0;
@@ -8835,6 +8822,10 @@ static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame)
                                ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
                }
                res = my_dahdi_write(p, (unsigned char *)frame->data.ptr, frame->datalen, idx, 0);
+       } else {
+               ast_log(LOG_WARNING, "Cannot handle frames in %s format\n",
+                       ast_format_get_name(frame->subclass.format));
+               return -1;
        }
        if (res < 0) {
                ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
@@ -9024,7 +9015,8 @@ static struct ast_channel *dahdi_new_callid_clean(struct dahdi_pvt *i, int state
 static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpbx, int idx, int law, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, struct ast_callid *callid)
 {
        struct ast_channel *tmp;
-       struct ast_format deflaw;
+       struct ast_format_cap *caps;
+       struct ast_format *deflaw;
        int x;
        int features;
        struct ast_str *chan_name;
@@ -9037,7 +9029,6 @@ static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpb
                return NULL;
        }
 
-       ast_format_clear(&deflaw);
 #if defined(HAVE_PRI)
        /*
         * The dnid has been stuffed with the called-number[:subaddress]
@@ -9051,9 +9042,16 @@ static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpb
                return NULL;
        }
 
+       caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+       if (!caps) {
+               ast_free(chan_name);
+               return NULL;
+       }
+
        tmp = ast_channel_alloc(0, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, assignedids, requestor, i->amaflags, "DAHDI/%s", ast_str_buffer(chan_name));
        ast_free(chan_name);
        if (!tmp) {
+               ao2_ref(caps, -1);
                return NULL;
        }
 
@@ -9073,9 +9071,9 @@ static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpb
        if (law) {
                i->law = law;
                if (law == DAHDI_LAW_ALAW) {
-                       ast_format_set(&deflaw, AST_FORMAT_ALAW, 0);
+                       deflaw = ast_format_alaw;
                } else {
-                       ast_format_set(&deflaw, AST_FORMAT_ULAW, 0);
+                       deflaw = ast_format_ulaw;
                }
        } else {
                switch (i->sig) {
@@ -9089,18 +9087,20 @@ static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpb
                        break;
                }
                if (i->law_default == DAHDI_LAW_ALAW) {
-                       ast_format_set(&deflaw, AST_FORMAT_ALAW, 0);
+                       deflaw = ast_format_alaw;
                } else {
-                       ast_format_set(&deflaw, AST_FORMAT_ULAW, 0);
+                       deflaw = ast_format_ulaw;
                }
        }
        ast_channel_set_fd(tmp, 0, i->subs[idx].dfd);
-       ast_format_cap_add(ast_channel_nativeformats(tmp), &deflaw);
+       ast_format_cap_append(caps, deflaw, 0);
+       ast_channel_nativeformats_set(tmp, caps);
+       ao2_ref(caps, -1);
        /* Start out assuming ulaw since it's smaller :) */
-       ast_format_copy(ast_channel_rawreadformat(tmp), &deflaw);
-       ast_format_copy(ast_channel_readformat(tmp), &deflaw);
-       ast_format_copy(ast_channel_rawwriteformat(tmp), &deflaw);
-       ast_format_copy(ast_channel_writeformat(tmp), &deflaw);
+       ast_channel_set_rawreadformat(tmp, deflaw);
+       ast_channel_set_readformat(tmp, deflaw);
+       ast_channel_set_rawwriteformat(tmp, deflaw);
+       ast_channel_set_writeformat(tmp, deflaw);
        i->subs[idx].linear = 0;
        dahdi_setlinear(i->subs[idx].dfd, i->subs[idx].linear);
        features = 0;
@@ -9397,7 +9397,6 @@ static void *analog_ss_thread(void *data)
        int len = 0;
        int res;
        int idx;
-       struct ast_format tmpfmt;
        RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
        const char *pickupexten;
 
@@ -10114,9 +10113,9 @@ static void *analog_ss_thread(void *data)
                                                        samples += res;
 
                                                        if (p->cid_signalling == CID_SIG_V23_JP) {
-                                                               res = callerid_feed_jp(cs, buf, res, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+                                                               res = callerid_feed_jp(cs, buf, res, AST_LAW(p));
                                                        } else {
-                                                               res = callerid_feed(cs, buf, res, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+                                                               res = callerid_feed(cs, buf, res, AST_LAW(p));
                                                        }
                                                        if (res < 0) {
                                                                /*
@@ -10399,7 +10398,7 @@ static void *analog_ss_thread(void *data)
                                                                }
                                                        }
                                                        samples += res;
-                                                       res = callerid_feed(cs, buf, res, ast_format_set(&tmpfmt, AST_LAW(p), 0));
+                                                       res = callerid_feed(cs, buf, res, AST_LAW(p));
                                                        if (res < 0) {
                                                                /*
                                                                 * The previous diagnostic message output likely
@@ -10569,7 +10568,7 @@ struct mwi_thread_data {
        size_t len;
 };
 
-static int calc_energy(const unsigned char *buf, int len, enum ast_format_id law)
+static int calc_energy(const unsigned char *buf, int len, struct ast_format *law)
 {
        int x;
        int sum = 0;
@@ -10578,7 +10577,7 @@ static int calc_energy(const unsigned char *buf, int len, enum ast_format_id law
                return 0;
 
        for (x = 0; x < len; x++)
-               sum += abs(law == AST_FORMAT_ULAW ? AST_MULAW(buf[x]) : AST_ALAW(buf[x]));
+               sum += abs(law == ast_format_ulaw ? AST_MULAW(buf[x]) : AST_ALAW(buf[x]));
 
        return sum / len;
 }
@@ -10594,13 +10593,12 @@ static void *mwi_thread(void *data)
        int i, res;
        unsigned int spill_done = 0;
        int spill_result = -1;
-       struct ast_format tmpfmt;
 
        if (!(cs = callerid_new(mtd->pvt->cid_signalling))) {
                goto quit_no_clean;
        }
 
-       callerid_feed(cs, mtd->buf, mtd->len, ast_format_set(&tmpfmt, AST_LAW(mtd->pvt), 0));
+       callerid_feed(cs, mtd->buf, mtd->len, AST_LAW(mtd->pvt));
 
        bump_gains(mtd->pvt);
 
@@ -10686,7 +10684,7 @@ static void *mwi_thread(void *data)
                        }
                        samples += res;
                        if (!spill_done) {
-                               if ((spill_result = callerid_feed(cs, mtd->buf, res, ast_format_set(&tmpfmt, AST_LAW(mtd->pvt), 0))) < 0) {
+                               if ((spill_result = callerid_feed(cs, mtd->buf, res, AST_LAW(mtd->pvt))) < 0) {
                                        /*
                                         * The previous diagnostic message output likely
                                         * explains why it failed.
@@ -10744,7 +10742,6 @@ quit_no_clean:
 static int mwi_send_init(struct dahdi_pvt * pvt)
 {
        int x;
-       struct ast_format tmpfmt;
 
 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
        /* Determine how this spill is to be sent */
@@ -10786,8 +10783,8 @@ static int mwi_send_init(struct dahdi_pvt * pvt)
 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
        if (pvt->mwisend_fsk) {
 #endif
-               pvt->cidlen = ast_callerid_vmwi_generate(pvt->cidspill, has_voicemail(pvt), CID_MWI_TYPE_MDMF_FULL,
-                                                        ast_format_set(&tmpfmt, AST_LAW(pvt), 0), pvt->cid_name, pvt->cid_num, 0);
+               pvt->cidlen = ast_callerid_vmwi_generate(pvt->cidspill, has_voicemail(pvt),
+                       CID_MWI_TYPE_MDMF_FULL, AST_LAW(pvt), pvt->cid_name, pvt->cid_num, 0);
                pvt->cidpos = 0;
 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
        }
@@ -17314,7 +17311,8 @@ static int __unload_module(void)
 
        dahdi_native_unload();
 
-       dahdi_tech.capabilities = ast_format_cap_destroy(dahdi_tech.capabilities);
+       ao2_cleanup(dahdi_tech.capabilities);
+       dahdi_tech.capabilities = NULL;
        STASIS_MESSAGE_TYPE_CLEANUP(dahdichannel_type);
        return 0;
 }
@@ -19496,7 +19494,6 @@ static const struct ast_data_entry dahdi_data_providers[] = {
 static int load_module(void)
 {
        int res;
-       struct ast_format tmpfmt;
 #if defined(HAVE_PRI) || defined(HAVE_SS7)
        int y;
 #endif /* defined(HAVE_PRI) || defined(HAVE_SS7) */
@@ -19505,14 +19502,15 @@ static int load_module(void)
                return AST_MODULE_LOAD_FAILURE;
        }
 
-       if (!(dahdi_tech.capabilities = ast_format_cap_alloc(0))) {
+       if (!(dahdi_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return AST_MODULE_LOAD_FAILURE;
        }
-       ast_format_cap_add(dahdi_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
-       ast_format_cap_add(dahdi_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0));
-       ast_format_cap_add(dahdi_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0));
+       ast_format_cap_append(dahdi_tech.capabilities, ast_format_slin, 0);
+       ast_format_cap_append(dahdi_tech.capabilities, ast_format_ulaw, 0);
+       ast_format_cap_append(dahdi_tech.capabilities, ast_format_alaw, 0);
 
        if (dahdi_native_load(ast_module_info->self, &dahdi_tech)) {
+               ao2_ref(dahdi_tech.capabilities, -1);
                return AST_MODULE_LOAD_FAILURE;
        }
 
@@ -19558,8 +19556,10 @@ static int load_module(void)
 #endif /* defined(HAVE_SS7) */
        res = setup_dahdi(0);
        /* Make sure we can register our DAHDI channel type */
-       if (res)
+       if (res) {
+               __unload_module();
                return AST_MODULE_LOAD_DECLINE;
+       }
        if (ast_channel_register(&dahdi_tech)) {
                ast_log(LOG_ERROR, "Unable to register channel class 'DAHDI'\n");
                __unload_module();
@@ -19652,10 +19652,9 @@ static int dahdi_sendtext(struct ast_channel *c, const char *text)
                return -1;
        mybuf = buf;
        if (p->mate) {
-               struct ast_format tmp;
                /* PUT_CLI_MARKMS is a macro and requires a format ptr called codec to be present */
-               struct ast_format *codec = &tmp;
-               ast_format_set(codec, AST_LAW(p), 0);
+               struct ast_format *codec = AST_LAW(p);
+
                for (x = 0; x < HEADER_MS; x++) {       /* 50 ms of Mark */
                        PUT_CLID_MARKMS;
                }
index 4beacfb..be5b4c7 100644 (file)
@@ -116,11 +116,17 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/stasis.h"
 #include "asterisk/stasis_system.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/format_cache.h"
+#include "asterisk/format_compatibility.h"
+#include "asterisk/format_cap.h"
 
 #include "iax2/include/iax2.h"
 #include "iax2/include/firmware.h"
 #include "iax2/include/parser.h"
 #include "iax2/include/provision.h"
+#include "iax2/include/codec_pref.h"
+#include "iax2/include/format_compatibility.h"
+
 #include "jitterbuf.h"
 
 /*** DOCUMENTATION
@@ -279,7 +285,7 @@ static int nochecksums = 0;
 /* Sample over last 100 units to determine historic jitter */
 #define GAMMA (0.01)
 
-static struct ast_codec_pref prefs;
+static struct iax2_codec_pref prefs;
 
 static const char tdesc[] = "Inter Asterisk eXchange Driver (Ver 2)";
 
@@ -347,22 +353,22 @@ static int (*iax2_regfunk)(const char *username, int onoff) = NULL;
 #define IAX_CAPABILITY_FULLBANDWIDTH   0xFFFF
 /* T1, maybe ISDN */
 #define IAX_CAPABILITY_MEDBANDWIDTH (IAX_CAPABILITY_FULLBANDWIDTH & \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_SLINEAR) &      \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_SLINEAR16) &    \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_SIREN7) &       \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_SIREN14) &      \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_G719) &         \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_ULAW) &         \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_ALAW) &         \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_G722))
+                     ~ast_format_compatibility_format2bitfield(ast_format_slin) &      \
+                     ~ast_format_compatibility_format2bitfield(ast_format_slin16) &    \
+                     ~ast_format_compatibility_format2bitfield(ast_format_siren7) &       \
+                     ~ast_format_compatibility_format2bitfield(ast_format_siren14) &      \
+                     ~ast_format_compatibility_format2bitfield(ast_format_g719) &         \
+                     ~ast_format_compatibility_format2bitfield(ast_format_ulaw) &         \
+                     ~ast_format_compatibility_format2bitfield(ast_format_alaw) &         \
+                     ~ast_format_compatibility_format2bitfield(ast_format_g722))
 /* A modem */
 #define IAX_CAPABILITY_LOWBANDWIDTH (IAX_CAPABILITY_MEDBANDWIDTH & \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_G726) &         \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_G726_AAL2) &    \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_ADPCM))
+                     ~ast_format_compatibility_format2bitfield(ast_format_g726) &         \
+                     ~ast_format_compatibility_format2bitfield(ast_format_g726_aal2) &    \
+                     ~ast_format_compatibility_format2bitfield(ast_format_adpcm))
 
 #define IAX_CAPABILITY_LOWFREE      (IAX_CAPABILITY_LOWBANDWIDTH & \
-                     ~ast_format_id_to_old_bitfield(AST_FORMAT_G723_1))
+                     ~ast_format_compatibility_format2bitfield(ast_format_g723))
 
 
 #define DEFAULT_MAXMS          2000            /* Must be faster than 2 seconds by default */
@@ -503,7 +509,7 @@ struct iax2_user {
        iax2_format capability;
        int maxauthreq; /*!< Maximum allowed outstanding AUTHREQs */
        int curauthreq; /*!< Current number of outstanding AUTHREQs */
-       struct ast_codec_pref prefs;
+       struct iax2_codec_pref prefs;
        struct ast_acl_list *acl;
        struct iax2_context *contexts;
        struct ast_variable *vars;
@@ -532,7 +538,7 @@ struct iax2_peer {
                AST_STRING_FIELD(zonetag);              /*!< Time Zone */
                AST_STRING_FIELD(parkinglot);   /*!< Default parkinglot for device */
        );
-       struct ast_codec_pref prefs;
+       struct iax2_codec_pref prefs;
        struct ast_dnsmgr_entry *dnsmgr;                /*!< DNS refresh manager */
        struct ast_sockaddr addr;
        int formats;
@@ -708,9 +714,9 @@ struct chan_iax2_pvt {
        /*! Peer Address */
        struct ast_sockaddr addr;
        /*! Actual used codec preferences */
-       struct ast_codec_pref prefs;
+       struct iax2_codec_pref prefs;
        /*! Requested codec preferences */
-       struct ast_codec_pref rprefs;
+       struct iax2_codec_pref rprefs;
        /*! Our call number */
        unsigned short callno;
        /*! Our callno_entry entry */
@@ -1809,17 +1815,18 @@ static iax2_format uncompress_subclass(unsigned char csub)
                return csub;
 }
 
-static iax2_format iax2_codec_choose(struct ast_codec_pref *pref, iax2_format formats, int find_best)
+static iax2_format iax2_codec_choose(struct iax2_codec_pref *pref, iax2_format formats, int find_best)
 {
        struct ast_format_cap *cap;
-       struct ast_format tmpfmt;
+       struct ast_format *tmpfmt;
        iax2_format format = 0;
-       if ((cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
-               ast_format_clear(&tmpfmt);
-               ast_format_cap_from_old_bitfield(cap, formats);
-               ast_codec_choose(pref, cap, find_best, &tmpfmt);
-               format = ast_format_to_old_bitfield(&tmpfmt);
-               cap = ast_format_cap_destroy(cap);
+
+       if ((cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
+               iax2_format_compatibility_bitfield2cap(formats, cap);
+               tmpfmt = ast_format_cap_get_format(cap, 0);
+               format = ast_format_compatibility_format2bitfield(tmpfmt);
+               ao2_ref(tmpfmt, -1);
+               ao2_ref(cap, -1);
        }
 
        return format;
@@ -1827,55 +1834,84 @@ static iax2_format iax2_codec_choose(struct ast_codec_pref *pref, iax2_format fo
 
 static iax2_format iax2_best_codec(iax2_format formats)
 {
-       struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-       struct ast_format tmpfmt;
+       struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+       struct ast_format *tmpfmt;
+       iax2_format format;
+
        if (!cap) {
                return 0;
        }
 
-       ast_format_clear(&tmpfmt);
-       ast_format_cap_from_old_bitfield(cap, formats);
-       ast_best_codec(cap, &tmpfmt);
-       cap = ast_format_cap_destroy(cap);
-       return ast_format_to_old_bitfield(&tmpfmt);
+       iax2_format_compatibility_bitfield2cap(formats, cap);
+       tmpfmt = ast_format_cap_get_format(cap, 0);
+       format = ast_format_compatibility_format2bitfield(tmpfmt);
+       ao2_ref(tmpfmt, -1);
+       ao2_ref(cap, -1);
+
+       return format;
 }
 
 const char *iax2_getformatname(iax2_format format)
 {
-       struct ast_format tmpfmt;
-       if (!(ast_format_from_old_bitfield(&tmpfmt, format))) {
+       struct ast_format *tmpfmt;
+
+       tmpfmt = ast_format_compatibility_bitfield2format(format);
+       if (!tmpfmt) {
                return "Unknown";
        }
 
-       return ast_getformatname(&tmpfmt);
+       return ast_format_get_name(tmpfmt);
 }
 
-static char *iax2_getformatname_multiple(char *codec_buf, size_t len, iax2_format format)
+static const char *iax2_getformatname_multiple(iax2_format format, struct ast_str **codec_buf)
 {
-       struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+       struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 
        if (!cap) {
                return "(Nothing)";
        }
-       ast_format_cap_from_old_bitfield(cap, format);
-       ast_getformatname_multiple(codec_buf, len, cap);
-       cap = ast_format_cap_destroy(cap);
+       iax2_format_compatibility_bitfield2cap(format, cap);
+       ast_format_cap_get_names(cap, codec_buf);
+       ao2_ref(cap, -1);
 
-       return codec_buf;
+       return ast_str_buffer(*codec_buf);
 }
 
-static int iax2_parse_allow_disallow(struct ast_codec_pref *pref, iax2_format *formats, const char *list, int allowing)
+static int iax2_parse_allow_disallow(struct iax2_codec_pref *pref, iax2_format *formats, const char *list, int allowing)
 {
-       int res;
-       struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+       int res, i;
+       struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+
        if (!cap) {
                return 1;
        }
 
-       ast_format_cap_from_old_bitfield(cap, *formats);
-       res = ast_parse_allow_disallow(pref, cap, list, allowing);
-       *formats = ast_format_cap_to_old_bitfield(cap);
-       cap = ast_format_cap_destroy(cap);
+       /* We want to add the formats to the cap in the preferred order */
+       for (i = 0; i < IAX2_CODEC_PREF_SIZE; i++) {
+               uint64_t pref_as_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[i]);
+
+               if (!pref_as_bitfield) {
+                       break;
+               }
+
+               if (iax2_format_compatibility_bitfield2cap(pref_as_bitfield, cap)) {
+                       ao2_ref(cap, -1);
+                       return 1;
+               }
+       }
+
+       res = ast_format_cap_update_by_allow_disallow(cap, list, allowing);
+       *formats = iax2_format_compatibility_cap2bitfield(cap);
+
+       iax2_codec_pref_remove_missing(pref, *formats);
+
+       for (i = 0; i < ast_format_cap_count(cap); i++) {
+               struct ast_format *fmt = ast_format_cap_get_format(cap, i);
+               iax2_codec_pref_append(pref, fmt, ast_format_cap_get_format_framing(cap, fmt));
+               ao2_ref(fmt, -1);
+       }
+
+       ao2_ref(cap, -1);
 
        return res;
 }
@@ -1883,13 +1919,13 @@ static int iax2_parse_allow_disallow(struct ast_codec_pref *pref, iax2_format *f
 static int iax2_data_add_codecs(struct ast_data *root, const char *node_name, iax2_format formats)
 {
        int res;
-       struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+       struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
        if (!cap) {
                return -1;
        }
-       ast_format_cap_from_old_bitfield(cap, formats);
+       iax2_format_compatibility_bitfield2cap(formats, cap);
        res = ast_data_add_codecs(root, node_name, cap);
-       cap = ast_format_cap_destroy(cap);
+       ao2_ref(cap, -1);
        return res;
 }
 
@@ -3737,9 +3773,9 @@ static char *handle_cli_iax2_show_peer(struct ast_cli_entry *e, int cmd, struct
        char status[30];
        char cbuf[256];
        struct iax2_peer *peer;
-       char codec_buf[512];
+       struct ast_str *codec_buf = ast_str_alloca(64);
        struct ast_str *encmethods = ast_str_alloca(256);
-       int x = 0, load_realtime = 0;
+       int load_realtime = 0;
 
        switch (cmd) {
        case CLI_INIT:
@@ -3789,23 +3825,13 @@ static char *handle_cli_iax2_show_peer(struct ast_cli_entry *e, int cmd, struct
                ast_cli(a->fd, "  Defaddr->IP  : %s Port %s\n", str_defaddr, str_defport);
                ast_cli(a->fd, "  Username     : %s\n", peer->username);
                ast_cli(a->fd, "  Codecs       : ");
-               iax2_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
-               ast_cli(a->fd, "%s\n", codec_buf);
+               ast_cli(a->fd, "%s\n", iax2_getformatname_multiple(peer->capability, &codec_buf));
 
-               ast_cli(a->fd, "  Codec Order  : (");
-               for(x = 0; x < AST_CODEC_PREF_SIZE; x++) {
-                       struct ast_format tmpfmt;
-                       if(!(ast_codec_pref_index(&peer->prefs, x, &tmpfmt)))
-                               break;
-                       ast_cli(a->fd, "%s", ast_getformatname(&tmpfmt));
-                       if(x < 31 && ast_codec_pref_index(&peer->prefs, x+1, &tmpfmt))
-                               ast_cli(a->fd, "|");
+               ast_cli(a->fd, "  Codec Order  : ");
+               if (iax2_codec_pref_string(&peer->prefs, cbuf, sizeof(cbuf)) != -1) {
+                       ast_cli(a->fd, "%s\n", cbuf);
                }
 
-               if (!x)
-                       ast_cli(a->fd, "none");
-               ast_cli(a->fd, ")\n");
-
                ast_cli(a->fd, "  Status       : ");
                peer_status(peer, status, sizeof(status));
                ast_cli(a->fd, "%s\n",status);
@@ -4084,9 +4110,9 @@ static void __get_from_jb(const void *p)
        ms = ast_tvdiff_ms(now, pvt->rxcore);
 
        if(ms >= (next = jb_next(pvt->jb))) {
-               struct ast_format voicefmt;
-               ast_format_from_old_bitfield(&voicefmt, pvt->voiceformat);
-               ret = jb_get(pvt->jb, &frame, ms, ast_codec_interp_len(&voicefmt));
+               struct ast_format *voicefmt;
+               voicefmt = ast_format_compatibility_bitfield2format(pvt->voiceformat);
+               ret = jb_get(pvt->jb, &frame, ms, ast_format_get_default_ms(voicefmt));
                switch(ret) {
                case JB_OK:
                        fr = frame.data;
@@ -4100,8 +4126,8 @@ static void __get_from_jb(const void *p)
 
                        /* create an interpolation frame */
                        af.frametype = AST_FRAME_VOICE;
-                       ast_format_copy(&af.subclass.format, &voicefmt);
-                       af.samples  = frame.ms * (ast_format_rate(&voicefmt) / 1000);
+                       af.subclass.format = voicefmt;
+                       af.samples  = frame.ms * (ast_format_get_sample_rate(voicefmt) / 1000);
                        af.src  = "IAX2 JB interpolation";
                        af.delivery = ast_tvadd(pvt->rxcore, ast_samp2tv(next, 1000));
                        af.offset = AST_FRIENDLY_OFFSET;
@@ -4182,7 +4208,7 @@ static int schedule_delivery(struct iax_frame *fr, int updatehistory, int fromtr
 
        if(fr->af.frametype == AST_FRAME_VOICE) {
                type = JB_TYPE_VOICE;
-               len = ast_codec_get_samples(&fr->af) / (ast_format_rate(&fr->af.subclass.format) / 1000);
+               len = ast_codec_samples_count(&fr->af) / (ast_format_get_sample_rate(fr->af.subclass.format) / 1000);
        } else if(fr->af.frametype == AST_FRAME_CNG) {
                type = JB_TYPE_SILENCE;
        }
@@ -4579,7 +4605,7 @@ static int create_addr(const char *peername, struct ast_channel *c, struct ast_s
 {
        struct iax2_peer *peer;
        int res = -1;
-       struct ast_codec_pref ourprefs;
+       struct iax2_codec_pref ourprefs;
 
        ast_clear_flag64(cai, IAX_SENDANI | IAX_TRUNK);
        cai->sockfd = defaultsockfd;
@@ -4604,14 +4630,16 @@ static int create_addr(const char *peername, struct ast_channel *c, struct ast_s
                /* But move the calling channel's native codec to the top of the preference list */
                memcpy(&ourprefs, &prefs, sizeof(ourprefs));
                if (c) {
-                       struct ast_format tmpfmt;
-                       ast_format_cap_iter_start(ast_channel_nativeformats(c));
-                       while (!(ast_format_cap_iter_next(ast_channel_nativeformats(c), &tmpfmt))) {
-                               ast_codec_pref_prepend(&ourprefs, &tmpfmt, 1);
+                       int i;
+
+                       for (i = 0; i < ast_format_cap_count(ast_channel_nativeformats(c)); i++) {
+                               struct ast_format *format = ast_format_cap_get_format(
+                                       ast_channel_nativeformats(c), i);
+                               iax2_codec_pref_prepend(&ourprefs, format, ast_format_cap_get_format_framing(ast_channel_nativeformats(c), format), 1);
+                               ao2_ref(format, -1);
                        }
-                       ast_format_cap_iter_end(ast_channel_nativeformats(c));
                }
-               ast_codec_pref_convert(&ourprefs, cai->prefs, sizeof(cai->prefs), 1);
+               iax2_codec_pref_convert(&ourprefs, cai->prefs, sizeof(cai->prefs), 1);
                return 0;
        }
 
@@ -4635,15 +4663,16 @@ static int create_addr(const char *peername, struct ast_channel *c, struct ast_s
        memcpy(&ourprefs, &peer->prefs, sizeof(ourprefs));
        /* Move the calling channel's native codec to the top of the preference list */
        if (c) {
-               struct ast_format tmpfmt;
-               ast_format_cap_iter_start(ast_channel_nativeformats(c));
-               while (!(ast_format_cap_iter_next(ast_channel_nativeformats(c), &tmpfmt))) {
-                       ast_debug(1, "prepending %s to prefs\n", ast_getformatname(&tmpfmt));
-                       ast_codec_pref_prepend(&ourprefs, &tmpfmt, 1);
+               int i;
+
+               for (i = 0; i < ast_format_cap_count(ast_channel_nativeformats(c)); i++) {
+                       struct ast_format *tmpfmt = ast_format_cap_get_format(
+                               ast_channel_nativeformats(c), i);
+                       iax2_codec_pref_prepend(&ourprefs, tmpfmt, ast_format_cap_get_format_framing(ast_channel_nativeformats(c), tmpfmt), 1);
+                       ao2_ref(tmpfmt, -1);
                }
-               ast_format_cap_iter_end(ast_channel_nativeformats(c));
        }
-       ast_codec_pref_convert(&ourprefs, cai->prefs, sizeof(cai->prefs), 1);
+       iax2_codec_pref_convert(&ourprefs, cai->prefs, sizeof(cai->prefs), 1);
        ast_copy_string(cai->context, peer->context, sizeof(cai->context));
        ast_copy_string(cai->peercontext, peer->peercontext, sizeof(cai->peercontext));
        ast_copy_string(cai->username, peer->username, sizeof(cai->username));
@@ -5181,7 +5210,7 @@ static int iax2_call(struct ast_channel *c, const char *dest, int timeout)
        if (pds.password)
                ast_string_field_set(iaxs[callno], secret, pds.password);
 
-       iax2_tmpfmt = ast_format_cap_to_old_bitfield(ast_channel_nativeformats(c));
+       iax2_tmpfmt = iax2_format_compatibility_cap2bitfield(ast_channel_nativeformats(c));
        iax_ie_append_int(&ied, IAX_IE_FORMAT, (int) iax2_tmpfmt);
        iax_ie_append_versioned_uint64(&ied, IAX_IE_FORMAT2, 0, iax2_tmpfmt);
 
@@ -5551,11 +5580,13 @@ static enum ast_bridge_result iax2_bridge(struct ast_channel *c0, struct ast_cha
                        return AST_BRIDGE_FAILED_NOWARN;
                }
                if (!(ast_format_cap_identical(ast_channel_nativeformats(c0), ast_channel_nativeformats(c1)))) {
-                       char buf0[256];
-                       char buf1[256];
-                       ast_getformatname_multiple(buf0, sizeof(buf0), ast_channel_nativeformats(c0));
-                       ast_getformatname_multiple(buf1, sizeof(buf1), ast_channel_nativeformats(c1));
-                       ast_verb(3, "Operating with different codecs [%s] [%s] , can't native bridge...\n", buf0, buf1);
+                       struct ast_str *c0_buf = ast_str_alloca(64);
+                       struct ast_str *c1_buf = ast_str_alloca(64);
+
+                       ast_verb(3, "Operating with different codecs [%s] [%s] , can't native bridge...\n",
+                               ast_format_cap_get_names(ast_channel_nativeformats(c0), &c0_buf),
+                               ast_format_cap_get_names(ast_channel_nativeformats(c1), &c1_buf));
+
                        /* Remove from native mode */
                        lock_both(callno0, callno1);
                        if (iaxs[callno0])
@@ -5780,7 +5811,8 @@ static struct ast_channel *ast_iax2_new(int callno, int state, iax2_format capab
        struct ast_channel *tmp;
        struct chan_iax2_pvt *i;
        struct ast_variable *v = NULL;
-       struct ast_format tmpfmt;
+       struct ast_format_cap *native;
+       struct ast_format *tmpfmt;
        struct ast_callid *callid;
 
        if (!(i = iaxs[callno])) {
@@ -5788,6 +5820,11 @@ static struct ast_channel *ast_iax2_new(int callno, int state, iax2_format capab
                return NULL;
        }
 
+       native = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+       if (!native) {
+               return NULL;
+       }
+
        /* Don't hold call lock */
        ast_mutex_unlock(&iaxsl[callno]);
        tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, assignedids, requestor, i->amaflags, "IAX2/%s-%d", i->host, i->callno);
@@ -5800,9 +5837,11 @@ static struct ast_channel *ast_iax2_new(int callno, int state, iax2_format capab
                        tmp = ast_channel_release(tmp);
                        ast_mutex_lock(&iaxsl[callno]);
                }
+               ao2_ref(native, -1);
                return NULL;
        }
        if (!tmp) {
+               ao2_ref(native, -1);
                return NULL;
        }
 
@@ -5813,14 +5852,19 @@ static struct ast_channel *ast_iax2_new(int callno, int state, iax2_format capab
        }
 
        ast_channel_tech_set(tmp, &iax2_tech);
+
        /* We can support any format by default, until we get restricted */
-       ast_format_cap_from_old_bitfield(ast_channel_nativeformats(tmp), capability);
-       ast_best_codec(ast_channel_nativeformats(tmp), &tmpfmt);
+       iax2_format_compatibility_bitfield2cap(capability, native);
+       ast_channel_nativeformats_set(tmp, native);
+       tmpfmt = ast_format_cap_get_format(ast_channel_nativeformats(tmp), 0);
 
-       ast_format_copy(ast_channel_readformat(tmp), &tmpfmt);
-       ast_format_copy(ast_channel_rawreadformat(tmp), &tmpfmt);
-       ast_format_copy(ast_channel_writeformat(tmp), &tmpfmt);
-       ast_format_copy(ast_channel_rawwriteformat(tmp), &tmpfmt);
+       ast_channel_set_readformat(tmp, tmpfmt);
+       ast_channel_set_rawreadformat(tmp, tmpfmt);
+       ast_channel_set_writeformat(tmp, tmpfmt);
+       ast_channel_set_rawwriteformat(tmp, tmpfmt);
+
+       ao2_ref(tmpfmt, -1);
+       ao2_ref(native, -1);
 
        ast_channel_tech_pvt_set(tmp, CALLNO_TO_PTR(i->callno));
 
@@ -5971,7 +6015,7 @@ static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, str
        int voice = 0;
        int genuine = 0;
        int adjust;
-       int rate = ast_format_rate(&f->subclass.format) / 1000;
+       int rate = 0;
        struct timeval *delivery = NULL;
 
 
@@ -5983,6 +6027,7 @@ static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, str
        */
        if (f->frametype == AST_FRAME_VOICE) {
                voice = 1;
+               rate = ast_format_get_sample_rate(f->subclass.format) / 1000;
                delivery = &f->delivery;
        } else if (f->frametype == AST_FRAME_IAX) {
                genuine = 1;
@@ -6092,8 +6137,9 @@ static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, str
                }
        }
        p->lastsent = ms;
-       if (voice)
+       if (voice) {
                p->nextpred = p->nextpred + f->samples / rate;
+       }
        return ms;
 }
 
@@ -6344,9 +6390,9 @@ static int decode_frame(ast_aes_decrypt_key *dcx, struct ast_iax2_full_hdr *fh,
                memcpy(efh->encdata, workspace + padding, *datalen - sizeof(struct ast_iax2_full_enc_hdr));
                f->frametype = fh->type;
                if (f->frametype == AST_FRAME_VIDEO) {
-                       ast_format_from_old_bitfield(&f->subclass.format, (uncompress_subclass(fh->csub & ~0x40) | ((fh->csub >> 6) & 0x1)));
+                       f->subclass.format = ast_format_compatibility_bitfield2format(uncompress_subclass(fh->csub & ~0x40) | ((fh->csub >> 6) & 0x1));
                } else if (f->frametype == AST_FRAME_VOICE) {
-                       ast_format_from_old_bitfield(&f->subclass.format, uncompress_subclass(fh->csub));
+                       f->subclass.format = ast_format_compatibility_bitfield2format(uncompress_subclass(fh->csub));
                } else {
                        f->subclass.integer = uncompress_subclass(fh->csub);
                }
@@ -6487,7 +6533,7 @@ static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned in
                /* High two bytes are the same on timestamp, or sending on a trunk */ &&
            (f->frametype == AST_FRAME_VOICE)
                /* is a voice frame */ &&
-               (f->subclass.format.id == ast_format_id_from_old_bitfield(pvt->svoiceformat))
+               (ast_format_cmp(f->subclass.format, ast_format_compatibility_bitfield2format(pvt->svoiceformat)))
                /* is the same type */ ) {
                        /* Force immediate rather than delayed transmission */
                        now = 1;
@@ -6501,7 +6547,7 @@ static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned in
                 * Otherwise send a mini video frame
                 */
                if (((fts & 0xFFFF8000L) == (pvt->lastvsent & 0xFFFF8000L)) &&
-                   ((f->subclass.format.id) == ast_format_id_from_old_bitfield(pvt->svideoformat))
+               (ast_format_cmp(f->subclass.format, ast_format_compatibility_bitfield2format(pvt->svideoformat)))
                   ) {
                        now = 1;
                        sendmini = 1;
@@ -6556,11 +6602,11 @@ static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned in
                fh->type = fr->af.frametype & 0xFF;
 
                if (fr->af.frametype == AST_FRAME_VIDEO) {
-                       iax2_format tmpfmt = ast_format_to_old_bitfield(&fr->af.subclass.format);
-                       tmpfmt |= ast_format_get_video_mark(&fr->af.subclass.format) ? 0x1LL : 0;
+                       iax2_format tmpfmt = ast_format_compatibility_format2bitfield(fr->af.subclass.format);
+                       tmpfmt |= fr->af.subclass.frame_ending ? 0x1LL : 0;
                        fh->csub = compress_subclass(tmpfmt | ((tmpfmt & 0x1LL) << 6));
                } else if (fr->af.frametype == AST_FRAME_VOICE) {
-                       fh->csub = compress_subclass(ast_format_to_old_bitfield(&fr->af.subclass.format));
+                       fh->csub = compress_subclass(ast_format_compatibility_format2bitfield(fr->af.subclass.format));
                } else {
                        fh->csub = compress_subclass(fr->af.subclass.integer);
                }
@@ -6583,9 +6629,9 @@ static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned in
                if ((f->frametype == AST_FRAME_IAX) && (f->subclass.integer == IAX_COMMAND_ACK))
                        fr->retries = -1;
                else if (f->frametype == AST_FRAME_VOICE)
-                       pvt->svoiceformat = ast_format_to_old_bitfield(&f->subclass.format);
+                       pvt->svoiceformat = ast_format_compatibility_format2bitfield(f->subclass.format);
                else if (f->frametype == AST_FRAME_VIDEO)
-                       pvt->svideoformat = ast_format_to_old_bitfield(&f->subclass.format);
+                       pvt->svideoformat = ast_format_compatibility_format2bitfield(f->subclass.format);
                if (ast_test_flag64(pvt, IAX_ENCRYPTED)) {
                        if (ast_test_flag64(pvt, IAX_KEYPOPULATED)) {
                                if (fr->transfer)
@@ -6616,7 +6662,7 @@ static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned in
                        vh = (struct ast_iax2_video_hdr *)(fr->af.data.ptr - sizeof(struct ast_iax2_video_hdr));
                        vh->zeros = 0;
                        vh->callno = htons(0x8000 | fr->callno);
-                       vh->ts = htons((fr->ts & 0x7FFF) | (ast_format_get_video_mark(&fr->af.subclass.format) ? 0x8000 : 0));
+                       vh->ts = htons((fr->ts & 0x7FFF) | (fr->af.subclass.frame_ending ? 0x8000 : 0));
                        fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_video_hdr);
                        fr->data = vh;
                        fr->retries = -1;
@@ -7767,8 +7813,8 @@ static int check_access(int callno, struct ast_sockaddr *addr, struct iax_ies *i
 
        /* Use provided preferences until told otherwise for actual preferences */
        if (ies->codec_prefs) {
-               ast_codec_pref_convert(&iaxs[callno]->rprefs, ies->codec_prefs, 32, 0);
-               ast_codec_pref_convert(&iaxs[callno]->prefs, ies->codec_prefs, 32, 0);
+               iax2_codec_pref_convert(&iaxs[callno]->rprefs, ies->codec_prefs, 32, 0);
+               iax2_codec_pref_convert(&iaxs[callno]->prefs, ies->codec_prefs, 32, 0);
        }
 
        if (!gotcapability) {
@@ -9827,7 +9873,7 @@ static int socket_process_meta(int packet_len, struct ast_iax2_meta_hdr *meta, s
                        ast_log(LOG_WARNING, "Received trunked frame before first full voice frame\n");
                        iax2_vnak(fr->callno);
                } else {
-                       ast_format_from_old_bitfield(&f.subclass.format, iaxs[fr->callno]->voiceformat);
+                       f.subclass.format = ast_format_compatibility_bitfield2format(iaxs[fr->callno]->voiceformat);
                        f.datalen = len;
                        if (f.datalen >= 0) {
                                if (f.datalen)
@@ -9847,7 +9893,7 @@ static int socket_process_meta(int packet_len, struct ast_iax2_meta_hdr *meta, s
                                        f.mallocd = 0;
                                        f.offset = 0;
                                        if (f.datalen && (f.frametype == AST_FRAME_VOICE))
-                                               f.samples = ast_codec_get_samples(&f);
+                                               f.samples = ast_codec_samples_count(&f);
                                        else
                                                f.samples = 0;
                                        fr->outoforder = 0;
@@ -10004,7 +10050,7 @@ static int socket_process_helper(struct iax2_thread *thread)
        struct iax_frame *duped_fr;
        char host_pref_buf[128];
        char caller_pref_buf[128];
-       struct ast_codec_pref pref;
+       struct iax2_codec_pref pref;
        char *using_prefs = "mine";
 
        /* allocate an iax_frame with 4096 bytes of data buffer */
@@ -10069,12 +10115,12 @@ static int socket_process_helper(struct iax2_thread *thread)
                /* Retrieve the type and subclass */
                f.frametype = fh->type;
                if (f.frametype == AST_FRAME_VIDEO) {
-                       ast_format_from_old_bitfield(&f.subclass.format, (uncompress_subclass(fh->csub & ~0x40)));
+                       f.subclass.format = ast_format_compatibility_bitfield2format(uncompress_subclass(fh->csub & ~0x40));
                        if ((fh->csub >> 6) & 0x1) {
-                               ast_format_set_video_mark(&f.subclass.format);
+                               f.subclass.frame_ending = 1;
                        }
                } else if (f.frametype == AST_FRAME_VOICE) {
-                       ast_format_from_old_bitfield(&f.subclass.format, uncompress_subclass(fh->csub));
+                       f.subclass.format = ast_format_compatibility_bitfield2format(uncompress_subclass(fh->csub));
                } else {
                        f.subclass.integer = uncompress_subclass(fh->csub);
                }
@@ -10498,22 +10544,21 @@ static int socket_process_helper(struct iax2_thread *thread)
                }
 
                if (f.frametype == AST_FRAME_VOICE) {
-                       if (ast_format_to_old_bitfield(&f.subclass.format) != iaxs[fr->callno]->voiceformat) {
-                                       iaxs[fr->callno]->voiceformat = ast_format_to_old_bitfield(&f.subclass.format);
-                                       ast_debug(1, "Ooh, voice format changed to '%s'\n", ast_getformatname(&f.subclass.format));
+                       if (ast_format_compatibility_format2bitfield(f.subclass.format) != iaxs[fr->callno]->voiceformat) {
+                                       iaxs[fr->callno]->voiceformat = ast_format_compatibility_format2bitfield(f.subclass.format);
+                                       ast_debug(1, "Ooh, voice format changed to '%s'\n", ast_format_get_name(f.subclass.format));
                                        if (iaxs[fr->callno]->owner) {
                                                iax2_lock_owner(fr->callno);
                                                if (iaxs[fr->callno]) {
                                                        if (iaxs[fr->callno]->owner) {
-                                                               struct ast_format_cap *orignative = ast_format_cap_dup(ast_channel_nativeformats(iaxs[fr->callno]->owner));
-                                                               struct ast_format_cap *native = ast_channel_nativeformats(iaxs[fr->callno]->owner);
-                                                               if (orignative) {
-                                                                       ast_format_cap_set(native, &f.subclass.format);
-                                                                       if (ast_channel_readformat(iaxs[fr->callno]->owner)->id) {
+                                                               struct ast_format_cap *native = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+                                                               if (native) {
+                                                                       ast_format_cap_append(native, f.subclass.format, 0);
+                                                                       ast_channel_nativeformats_set(iaxs[fr->callno]->owner, native);
+                                                                       if (ast_channel_readformat(iaxs[fr->callno]->owner)) {
                                                                                ast_set_read_format(iaxs[fr->callno]->owner, ast_channel_readformat(iaxs[fr->callno]->owner));
                                                                        }
-                                                                       ast_format_cap_copy(native, orignative);
-                                                                       orignative = ast_format_cap_destroy(orignative);
+                                                                       ao2_ref(native, -1);
                                                                }
                                                                ast_channel_unlock(iaxs[fr->callno]->owner);
                                                        }
@@ -10532,9 +10577,9 @@ static int socket_process_helper(struct iax2_thread *thread)
                        }
                }
                if (f.frametype == AST_FRAME_VIDEO) {
-                       if (f.subclass.format.id != ast_format_id_from_old_bitfield(iaxs[fr->callno]->videoformat)) {
-                               ast_debug(1, "Ooh, video format changed to %s\n", ast_getformatname(&f.subclass.format));
-                               iaxs[fr->callno]->videoformat = ast_format_to_old_bitfield(&f.subclass.format);
+                       if (ast_format_compatibility_format2bitfield(f.subclass.format) != iaxs[fr->callno]->videoformat) {
+                               ast_debug(1, "Ooh, video format changed to %s\n", ast_format_get_name(f.subclass.format));
+                               iaxs[fr->callno]->videoformat = ast_format_compatibility_format2bitfield(f.subclass.format);
                        }
                }
                if (f.frametype == AST_FRAME_IAX) {
@@ -10697,12 +10742,12 @@ static int socket_process_helper(struct iax2_thread *thread)
                                                        strcpy(caller_pref_buf, "disabled");
                                                        strcpy(host_pref_buf, "disabled");
                                                } else {
-                                                       struct ast_format tmpfmt;
+                                                       struct ast_format *tmpfmt;
                                                        using_prefs = "mine";
                                                        /* If the information elements are in here... use them */
                                                        if (ies.codec_prefs)
-                                                               ast_codec_pref_convert(&iaxs[fr->callno]->rprefs, ies.codec_prefs, 32, 0);
-                                                       if (ast_codec_pref_index(&iaxs[fr->callno]->rprefs, 0, &tmpfmt)) {
+                                                               iax2_codec_pref_convert(&iaxs[fr->callno]->rprefs, ies.codec_prefs, 32, 0);
+                                                       if (iax2_codec_pref_index(&iaxs[fr->callno]->rprefs, 0, &tmpfmt)) {
                                                                /* If we are codec_first_choice we let the caller have the 1st shot at picking the codec.*/
                                                                if (ast_test_flag64(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
                                                                        pref = iaxs[fr->callno]->rprefs;
@@ -10714,8 +10759,8 @@ static int socket_process_helper(struct iax2_thread *thread)
                                                            &