diff options
author | Matthew Jordan <mjordan@digium.com> | 2014-07-20 22:06:33 +0000 |
---|---|---|
committer | Matthew Jordan <mjordan@digium.com> | 2014-07-20 22:06:33 +0000 |
commit | a2c912e9972c91973ea66902d217746133f96026 (patch) | |
tree | 50e01d14ba62950e3f78766d5ba435ba51ca327d /formats | |
parent | b299052e203807c9a2111eb2cd919246d7589cb3 (diff) |
media formats: re-architect handling of media for performance improvements
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
Diffstat (limited to 'formats')
-rw-r--r-- | formats/format_g719.c | 15 | ||||
-rw-r--r-- | formats/format_g723.c | 14 | ||||
-rw-r--r-- | formats/format_g726.c | 15 | ||||
-rw-r--r-- | formats/format_g729.c | 15 | ||||
-rw-r--r-- | formats/format_gsm.c | 14 | ||||
-rw-r--r-- | formats/format_h263.c | 26 | ||||
-rw-r--r-- | formats/format_h264.c | 26 | ||||
-rw-r--r-- | formats/format_ilbc.c | 14 | ||||
-rw-r--r-- | formats/format_jpeg.c | 13 | ||||
-rw-r--r-- | formats/format_ogg_vorbis.c | 16 | ||||
-rw-r--r-- | formats/format_pcm.c | 31 | ||||
-rw-r--r-- | formats/format_siren14.c | 14 | ||||
-rw-r--r-- | formats/format_siren7.c | 14 | ||||
-rw-r--r-- | formats/format_sln.c | 78 | ||||
-rw-r--r-- | formats/format_vox.c | 14 | ||||
-rw-r--r-- | formats/format_wav.c | 26 | ||||
-rw-r--r-- | formats/format_wav_gsm.c | 15 |
17 files changed, 88 insertions, 272 deletions
diff --git a/formats/format_g719.c b/formats/format_g719.c index ee9221230..8d508f0e3 100644 --- a/formats/format_g719.c +++ b/formats/format_g719.c @@ -34,6 +34,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" #define BUF_SIZE 160 /* 20 milliseconds == 160 bytes, 960 samples */ #define SAMPLES_TO_BYTES(x) ((typeof(x)) x / ((float) 960 / 160)) @@ -44,9 +45,6 @@ static struct ast_frame *g719read(struct ast_filestream *s, int *whennext) int res; /* Send a frame from the file to the appropriate channel */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_G719, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { if (res) @@ -61,14 +59,6 @@ static int g719write(struct ast_filestream *fs, struct ast_frame *f) { int res; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_G719) { - ast_log(LOG_WARNING, "Asked to write non-G.719 frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) { ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); return -1; @@ -147,7 +137,8 @@ static struct ast_format_def g719_f = { static int load_module(void) { - ast_format_set(&g719_f.format, AST_FORMAT_G719, 0); + g719_f.format = ast_format_g719; + if (ast_format_def_register(&g719_f)) return AST_MODULE_LOAD_DECLINE; return AST_MODULE_LOAD_SUCCESS; diff --git a/formats/format_g723.c b/formats/format_g723.c index c3c194d15..51fe6b049 100644 --- a/formats/format_g723.c +++ b/formats/format_g723.c @@ -35,6 +35,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" +#include "asterisk/format_cache.h" #define G723_MAX_SIZE 1024 @@ -64,9 +65,6 @@ static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext) return NULL; } /* Read the data into the buffer */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_G723_1, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != size) { ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno)); @@ -82,14 +80,6 @@ static int g723_write(struct ast_filestream *s, struct ast_frame *f) uint16_t size; int res; /* XXX there used to be a check s->fr means a read stream */ - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_G723_1) { - ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n"); - return -1; - } delay = 0; if (f->datalen <= 0) { ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen); @@ -151,7 +141,7 @@ static struct ast_format_def g723_1_f = { static int load_module(void) { - ast_format_set(&g723_1_f.format, AST_FORMAT_G723_1, 0); + g723_1_f.format = ast_format_g723; if (ast_format_def_register(&g723_1_f)) return AST_MODULE_LOAD_FAILURE; diff --git a/formats/format_g726.c b/formats/format_g726.c index 636aff091..bde3e2d06 100644 --- a/formats/format_g726.c +++ b/formats/format_g726.c @@ -39,6 +39,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" #define RATE_40 0 #define RATE_32 1 @@ -122,9 +123,6 @@ static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext) struct g726_desc *fs = (struct g726_desc *)s->_private; /* Send a frame from the file to the appropriate channel */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_G726, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]); s->fr.samples = 8 * FRAME_TIME; if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { @@ -141,15 +139,6 @@ static int g726_write(struct ast_filestream *s, struct ast_frame *f) int res; struct g726_desc *fs = (struct g726_desc *)s->_private; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_G726) { - ast_log(LOG_WARNING, "Asked to write non-G726 frame (%s)!\n", - ast_getformatname(&f->subclass.format)); - return -1; - } if (f->datalen % frame_size[fs->rate]) { ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n", f->datalen, frame_size[fs->rate]); @@ -239,7 +228,7 @@ static int load_module(void) int i; for (i = 0; f[i].desc_size ; i++) { - ast_format_set(&f[i].format, AST_FORMAT_G726, 0); + f[i].format = ast_format_g726; if (ast_format_def_register(&f[i])) { /* errors are fatal */ ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name); return AST_MODULE_LOAD_FAILURE; diff --git a/formats/format_g729.c b/formats/format_g729.c index f11fa97d9..7d562ede7 100644 --- a/formats/format_g729.c +++ b/formats/format_g729.c @@ -37,6 +37,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */ @@ -49,9 +50,6 @@ static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext) { int res; /* Send a frame from the file to the appropriate channel */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_G729A, 0); - s->fr.mallocd = 0; s->fr.samples = G729A_SAMPLES; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { @@ -66,14 +64,7 @@ static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext) static int g729_write(struct ast_filestream *fs, struct ast_frame *f) { int res; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_G729A) { - ast_log(LOG_WARNING, "Asked to write non-G729 frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } + if (f->datalen % 10) { ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen); return -1; @@ -147,7 +138,7 @@ static struct ast_format_def g729_f = { static int load_module(void) { - ast_format_set(&g729_f.format, AST_FORMAT_G729A, 0); + g729_f.format = ast_format_g729; if (ast_format_def_register(&g729_f)) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; diff --git a/formats/format_gsm.c b/formats/format_gsm.c index 47ed41c3a..fe1e55c7a 100644 --- a/formats/format_gsm.c +++ b/formats/format_gsm.c @@ -34,6 +34,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" #include "msgsm.h" @@ -56,10 +57,7 @@ static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext) { int res; - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_GSM, 0); AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE) - s->fr.mallocd = 0; if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) { if (res) ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); @@ -74,14 +72,6 @@ static int gsm_write(struct ast_filestream *fs, struct ast_frame *f) int res; unsigned char gsm[2*GSM_FRAME_SIZE]; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_GSM) { - ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } if (!(f->datalen % 65)) { /* This is in MSGSM format, need to be converted */ int len=0; @@ -193,7 +183,7 @@ static struct ast_format_def gsm_f = { static int load_module(void) { - ast_format_set(&gsm_f.format, AST_FORMAT_GSM, 0); + gsm_f.format = ast_format_gsm; if (ast_format_def_register(&gsm_f)) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; diff --git a/formats/format_h263.c b/formats/format_h263.c index 56e9b3aa0..6511cef04 100644 --- a/formats/format_h263.c +++ b/formats/format_h263.c @@ -35,6 +35,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */ @@ -48,6 +49,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") * ridiculously large). */ #define BUF_SIZE 32768 /* Four real h.263 Frames */ +#define FRAME_ENDED 0x8000 + struct h263_desc { unsigned int lastts; }; @@ -76,15 +79,12 @@ static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext) if ((res = fread(&len, 1, sizeof(len), s->f)) < 1) return NULL; len = ntohs(len); - mark = (len & 0x8000) ? 1 : 0; + mark = (len & FRAME_ENDED) ? 1 : 0; len &= 0x7fff; if (len > BUF_SIZE) { ast_log(LOG_WARNING, "Length %d is too long\n", len); return NULL; } - s->fr.frametype = AST_FRAME_VIDEO; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_H263, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { if (res) @@ -93,11 +93,7 @@ static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext) } s->fr.samples = fs->lastts; /* XXX what ? */ s->fr.datalen = len; - if (mark) { - ast_format_set_video_mark(&s->fr.subclass.format); - } - s->fr.delivery.tv_sec = 0; - s->fr.delivery.tv_usec = 0; + s->fr.subclass.frame_ending = mark; if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) { fs->lastts = ntohl(ts); *whennext = fs->lastts * 4/45; @@ -112,15 +108,7 @@ static int h263_write(struct ast_filestream *fs, struct ast_frame *f) unsigned int ts; unsigned short len; uint32_t mark = 0; - if (f->frametype != AST_FRAME_VIDEO) { - ast_log(LOG_WARNING, "Asked to write non-video frame!\n"); - return -1; - } - mark = ast_format_get_video_mark(&f->subclass.format) ? 0x8000 : 0; - if (f->subclass.format.id != AST_FORMAT_H263) { - ast_log(LOG_WARNING, "Asked to write non-h263 frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } + mark = f->subclass.frame_ending ? FRAME_ENDED : 0; ts = htonl(f->samples); if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) { ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); @@ -182,7 +170,7 @@ static struct ast_format_def h263_f = { static int load_module(void) { - ast_format_set(&h263_f.format, AST_FORMAT_H263, 0); + h263_f.format = ast_format_h263; if (ast_format_def_register(&h263_f)) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; diff --git a/formats/format_h264.c b/formats/format_h264.c index e090c2bac..56ec007c0 100644 --- a/formats/format_h264.c +++ b/formats/format_h264.c @@ -35,12 +35,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" /* Some Ideas for this code came from makeh264e.c by Jeffrey Chilton */ /* Portions of the conversion code are by guido@sienanet.it */ /*! \todo Check this buf size estimate, it may be totally wrong for large frame video */ +#define FRAME_ENDED 0x8000 + #define BUF_SIZE 4096 /* Two Real h264 Frames */ struct h264_desc { unsigned int lastts; @@ -68,15 +71,12 @@ static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext) if ((res = fread(&len, 1, sizeof(len), s->f)) < 1) return NULL; len = ntohs(len); - mark = (len & 0x8000) ? 1 : 0; + mark = (len & FRAME_ENDED) ? 1 : 0; len &= 0x7fff; if (len > BUF_SIZE) { ast_log(LOG_WARNING, "Length %d is too long\n", len); len = BUF_SIZE; /* XXX truncate */ } - s->fr.frametype = AST_FRAME_VIDEO; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_H264, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { if (res) @@ -85,11 +85,7 @@ static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext) } s->fr.samples = fs->lastts; s->fr.datalen = len; - if (mark) { - ast_format_set_video_mark(&s->fr.subclass.format); - } - s->fr.delivery.tv_sec = 0; - s->fr.delivery.tv_usec = 0; + s->fr.subclass.frame_ending = mark; if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) { fs->lastts = ntohl(ts); *whennext = fs->lastts * 4/45; @@ -105,15 +101,7 @@ static int h264_write(struct ast_filestream *s, struct ast_frame *f) unsigned short len; int mark; - if (f->frametype != AST_FRAME_VIDEO) { - ast_log(LOG_WARNING, "Asked to write non-video frame!\n"); - return -1; - } - mark = ast_format_get_video_mark(&f->subclass.format) ? 0x8000 : 0; - if (f->subclass.format.id != AST_FORMAT_H264) { - ast_log(LOG_WARNING, "Asked to write non-h264 frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } + mark = f->subclass.frame_ending ? FRAME_ENDED : 0; ts = htonl(f->samples); if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) { ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); @@ -175,7 +163,7 @@ static struct ast_format_def h264_f = { static int load_module(void) { - ast_format_set(&h264_f.format, AST_FORMAT_H264, 0); + h264_f.format = ast_format_h264; if (ast_format_def_register(&h264_f)) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; diff --git a/formats/format_ilbc.c b/formats/format_ilbc.c index 07155b717..ce5135f7b 100644 --- a/formats/format_ilbc.c +++ b/formats/format_ilbc.c @@ -36,6 +36,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */ @@ -48,9 +49,6 @@ static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext) { int res; /* Send a frame from the file to the appropriate channel */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_ILBC, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { if (res) @@ -64,14 +62,6 @@ static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext) static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f) { int res; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_ILBC) { - ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } if (f->datalen % 50) { ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen); return -1; @@ -145,7 +135,7 @@ static struct ast_format_def ilbc_f = { static int load_module(void) { - ast_format_set(&ilbc_f.format, AST_FORMAT_ILBC, 0); + ilbc_f.format = ast_format_ilbc; if (ast_format_def_register(&ilbc_f)) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; diff --git a/formats/format_jpeg.c b/formats/format_jpeg.c index d6733020e..1e91482f7 100644 --- a/formats/format_jpeg.c +++ b/formats/format_jpeg.c @@ -36,6 +36,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/module.h" #include "asterisk/image.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" static struct ast_frame *jpeg_read_image(int fd, int len) { @@ -52,7 +53,7 @@ static struct ast_frame *jpeg_read_image(int fd, int len) } memset(&fr, 0, sizeof(fr)); fr.frametype = AST_FRAME_IMAGE; - ast_format_set(&fr.subclass.format, AST_FORMAT_JPEG, 0); + fr.subclass.format = ast_format_jpeg; fr.data.ptr = buf; fr.src = "JPEG Read"; fr.datalen = len; @@ -74,14 +75,6 @@ static int jpeg_identify(int fd) static int jpeg_write_image(int fd, struct ast_frame *fr) { int res=0; - if (fr->frametype != AST_FRAME_IMAGE) { - ast_log(LOG_WARNING, "Not an image\n"); - return -1; - } - if (fr->subclass.format.id != AST_FORMAT_JPEG) { - ast_log(LOG_WARNING, "Not a jpeg image\n"); - return -1; - } if (fr->datalen) { res = write(fd, fr->data.ptr, fr->datalen); if (res != fr->datalen) { @@ -103,7 +96,7 @@ static struct ast_imager jpeg_format = { static int load_module(void) { - ast_format_set(&jpeg_format.format, AST_FORMAT_JPEG, 0); + jpeg_format.format = ast_format_jpeg; if (ast_image_register(&jpeg_format)) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; diff --git a/formats/format_ogg_vorbis.c b/formats/format_ogg_vorbis.c index 9eb390066..6171491a8 100644 --- a/formats/format_ogg_vorbis.c +++ b/formats/format_ogg_vorbis.c @@ -45,6 +45,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" +#include "asterisk/format_cache.h" /* * this is the number of samples we deal with. Samples are converted @@ -242,16 +243,6 @@ static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f) ast_log(LOG_ERROR, "This stream is not set up for writing!\n"); return -1; } - - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_SLINEAR) { - ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%s)!\n", - ast_getformatname(&f->subclass.format)); - return -1; - } if (!f->datalen) return -1; @@ -310,9 +301,6 @@ static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs, } /* initialize frame */ - fs->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&fs->fr.subclass.format, AST_FORMAT_SLINEAR, 0); - fs->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); out_buf = (char *) (fs->fr.data.ptr); /* SLIN data buffer */ @@ -435,7 +423,7 @@ static struct ast_format_def vorbis_f = { static int load_module(void) { - ast_format_set(&vorbis_f.format, AST_FORMAT_SLINEAR, 0); + vorbis_f.format = ast_format_slin; if (ast_format_def_register(&vorbis_f)) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; diff --git a/formats/format_pcm.c b/formats/format_pcm.c index 1ee333fba..2d864078e 100644 --- a/formats/format_pcm.c +++ b/formats/format_pcm.c @@ -37,6 +37,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/endian.h" #include "asterisk/ulaw.h" #include "asterisk/alaw.h" +#include "asterisk/format_cache.h" #define BUF_SIZE 160 /* 160 bytes, and same number of samples */ @@ -66,7 +67,7 @@ static unsigned long get_time(void) static int pcma_open(struct ast_filestream *s) { - if (s->fmt->format == AST_FORMAT_ALAW) + if (ast_format_cmp(s->fmt->format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) pd->starttime = get_time(); return 0; } @@ -83,9 +84,6 @@ static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext) /* Send a frame from the file to the appropriate channel */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_copy(&s->fr.subclass.format, &s->fmt->format); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) { if (res) @@ -93,7 +91,7 @@ static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext) return NULL; } s->fr.datalen = res; - if (s->fmt->format.id == AST_FORMAT_G722) + if (ast_format_cmp(s->fmt->format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) *whennext = s->fr.samples = res * 2; else *whennext = s->fr.samples = res; @@ -141,7 +139,7 @@ static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence) } if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */ size_t left = offset - max; - const char *src = (fs->fmt->format.id == AST_FORMAT_ALAW) ? alaw_silence : ulaw_silence; + const char *src = (ast_format_cmp(fs->fmt->format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) ? alaw_silence : ulaw_silence; while (left) { size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f); @@ -185,17 +183,8 @@ static int pcm_write(struct ast_filestream *fs, struct ast_frame *f) { int res; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (ast_format_cmp(&f->subclass.format, &fs->fmt->format) == AST_FORMAT_CMP_NOT_EQUAL) { - ast_log(LOG_WARNING, "Asked to write incompatible format frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } - #ifdef REALTIME_WRITE - if (s->fmt->format == AST_FORMAT_ALAW) { + if (ast_format_cmp(s->fmt->format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) { struct pcm_desc *pd = (struct pcm_desc *)fs->_private; struct stat stat_buf; unsigned long cur_time = get_time(); @@ -399,7 +388,7 @@ static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence) off_t min = AU_HEADER_SIZE, max, cur; long offset = 0, bytes; - if (fs->fmt->format.id == AST_FORMAT_G722) + if (ast_format_cmp(fs->fmt->format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) bytes = sample_offset / 2; else bytes = sample_offset; @@ -523,10 +512,10 @@ static int load_module(void) for (i = 0; i < ARRAY_LEN(alaw_silence); i++) alaw_silence[i] = AST_LIN2A(0); - ast_format_set(&pcm_f.format, AST_FORMAT_ULAW, 0); - ast_format_set(&alaw_f.format, AST_FORMAT_ALAW, 0); - ast_format_set(&au_f.format, AST_FORMAT_ULAW, 0); - ast_format_set(&g722_f.format, AST_FORMAT_G722, 0); + pcm_f.format = ast_format_ulaw; + alaw_f.format = ast_format_alaw; + au_f.format = ast_format_ulaw; + g722_f.format = ast_format_g722; if ( ast_format_def_register(&pcm_f) || ast_format_def_register(&alaw_f) || ast_format_def_register(&au_f) diff --git a/formats/format_siren14.c b/formats/format_siren14.c index 77a9fe3ef..b7e2de1ec 100644 --- a/formats/format_siren14.c +++ b/formats/format_siren14.c @@ -34,6 +34,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" #define BUF_SIZE 120 /* 20 milliseconds == 120 bytes, 640 samples */ #define SAMPLES_TO_BYTES(x) ((typeof(x)) x / ((float) 640 / 120)) @@ -44,9 +45,6 @@ static struct ast_frame *siren14read(struct ast_filestream *s, int *whennext) int res; /* Send a frame from the file to the appropriate channel */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_SIREN14, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { if (res) @@ -61,14 +59,6 @@ static int siren14write(struct ast_filestream *fs, struct ast_frame *f) { int res; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_SIREN14) { - ast_log(LOG_WARNING, "Asked to write non-Siren14 frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) { ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); return -1; @@ -148,7 +138,7 @@ static struct ast_format_def siren14_f = { static int load_module(void) { - ast_format_set(&siren14_f.format, AST_FORMAT_SIREN14, 0); + siren14_f.format = ast_format_siren14; if (ast_format_def_register(&siren14_f)) return AST_MODULE_LOAD_DECLINE; diff --git a/formats/format_siren7.c b/formats/format_siren7.c index 35e2a3460..0b3c75d9c 100644 --- a/formats/format_siren7.c +++ b/formats/format_siren7.c @@ -34,6 +34,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" #define BUF_SIZE 80 /* 20 milliseconds == 80 bytes, 320 samples */ #define SAMPLES_TO_BYTES(x) x / (320 / 80) @@ -44,9 +45,6 @@ static struct ast_frame *siren7read(struct ast_filestream *s, int *whennext) int res; /* Send a frame from the file to the appropriate channel */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_SIREN7, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { if (res) @@ -61,14 +59,6 @@ static int siren7write(struct ast_filestream *fs, struct ast_frame *f) { int res; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_SIREN7) { - ast_log(LOG_WARNING, "Asked to write non-Siren7 frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) { ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); return -1; @@ -148,7 +138,7 @@ static struct ast_format_def siren7_f = { static int load_module(void) { - ast_format_set(&siren7_f.format, AST_FORMAT_SIREN7, 0); + siren7_f.format = ast_format_siren7; if (ast_format_def_register(&siren7_f)) return AST_MODULE_LOAD_DECLINE; diff --git a/formats/format_sln.c b/formats/format_sln.c index 30a1ebe31..a3230637c 100644 --- a/formats/format_sln.c +++ b/formats/format_sln.c @@ -32,15 +32,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" -static struct ast_frame *generic_read(struct ast_filestream *s, int *whennext, unsigned int buf_size, enum ast_format_id id) +static struct ast_frame *generic_read(struct ast_filestream *s, int *whennext, unsigned int buf_size) { int res; /* Send a frame from the file to the appropriate channel */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, id, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, buf_size); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) { if (res) @@ -52,17 +50,9 @@ static struct ast_frame *generic_read(struct ast_filestream *s, int *whennext, u return &s->fr; } -static int generic_write(struct ast_filestream *fs, struct ast_frame *f, enum ast_format_id id) +static int slinear_write(struct ast_filestream *fs, struct ast_frame *f) { int res; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != id) { - ast_log(LOG_WARNING, "Asked to write non-slinear frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) { ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); return -1; @@ -127,8 +117,7 @@ static off_t slinear_tell(struct ast_filestream *fs) return ftello(fs->f) / 2; } -static int slinear_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR);} -static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 320, AST_FORMAT_SLINEAR);} +static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 320);} static struct ast_format_def slin_f = { .name = "sln", .exts = "sln|raw", @@ -140,12 +129,11 @@ static struct ast_format_def slin_f = { .buf_size = 320 + AST_FRIENDLY_OFFSET, }; -static int slinear12_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR12);} -static struct ast_frame *slinear12_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 480, AST_FORMAT_SLINEAR12);} +static struct ast_frame *slinear12_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 480);} static struct ast_format_def slin12_f = { .name = "sln12", .exts = "sln12", - .write = slinear12_write, + .write = slinear_write, .seek = slinear_seek, .trunc = slinear_trunc, .tell = slinear_tell, @@ -153,12 +141,11 @@ static struct ast_format_def slin12_f = { .buf_size = 480 + AST_FRIENDLY_OFFSET, }; -static int slinear16_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR16);} -static struct ast_frame *slinear16_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 640, AST_FORMAT_SLINEAR16);} +static struct ast_frame *slinear16_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 640);} static struct ast_format_def slin16_f = { .name = "sln16", .exts = "sln16", - .write = slinear16_write, + .write = slinear_write, .seek = slinear_seek, .trunc = slinear_trunc, .tell = slinear_tell, @@ -166,12 +153,11 @@ static struct ast_format_def slin16_f = { .buf_size = 640 + AST_FRIENDLY_OFFSET, }; -static int slinear24_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR24);} -static struct ast_frame *slinear24_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 960, AST_FORMAT_SLINEAR24);} +static struct ast_frame *slinear24_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 960);} static struct ast_format_def slin24_f = { .name = "sln24", .exts = "sln24", - .write = slinear24_write, + .write = slinear_write, .seek = slinear_seek, .trunc = slinear_trunc, .tell = slinear_tell, @@ -179,12 +165,11 @@ static struct ast_format_def slin24_f = { .buf_size = 960 + AST_FRIENDLY_OFFSET, }; -static int slinear32_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR32);} -static struct ast_frame *slinear32_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1280, AST_FORMAT_SLINEAR32);} +static struct ast_frame *slinear32_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1280);} static struct ast_format_def slin32_f = { .name = "sln32", .exts = "sln32", - .write = slinear32_write, + .write = slinear_write, .seek = slinear_seek, .trunc = slinear_trunc, .tell = slinear_tell, @@ -192,12 +177,11 @@ static struct ast_format_def slin32_f = { .buf_size = 1280 + AST_FRIENDLY_OFFSET, }; -static int slinear44_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR44);} -static struct ast_frame *slinear44_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1764, AST_FORMAT_SLINEAR44);} +static struct ast_frame *slinear44_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1764);} static struct ast_format_def slin44_f = { .name = "sln44", .exts = "sln44", - .write = slinear44_write, + .write = slinear_write, .seek = slinear_seek, .trunc = slinear_trunc, .tell = slinear_tell, @@ -205,12 +189,11 @@ static struct ast_format_def slin44_f = { .buf_size = 1764 + AST_FRIENDLY_OFFSET, }; -static int slinear48_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR48);} -static struct ast_frame *slinear48_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1920, AST_FORMAT_SLINEAR48);} +static struct ast_frame *slinear48_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1920);} static struct ast_format_def slin48_f = { .name = "sln48", .exts = "sln48", - .write = slinear48_write, + .write = slinear_write, .seek = slinear_seek, .trunc = slinear_trunc, .tell = slinear_tell, @@ -218,12 +201,11 @@ static struct ast_format_def slin48_f = { .buf_size = 1920 + AST_FRIENDLY_OFFSET, }; -static int slinear96_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR96);} -static struct ast_frame *slinear96_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 3840, AST_FORMAT_SLINEAR96);} +static struct ast_frame *slinear96_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 3840);} static struct ast_format_def slin96_f = { .name = "sln96", .exts = "sln96", - .write = slinear96_write, + .write = slinear_write, .seek = slinear_seek, .trunc = slinear_trunc, .tell = slinear_tell, @@ -231,12 +213,11 @@ static struct ast_format_def slin96_f = { .buf_size = 3840 + AST_FRIENDLY_OFFSET, }; -static int slinear192_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR192);} -static struct ast_frame *slinear192_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 7680, AST_FORMAT_SLINEAR192);} +static struct ast_frame *slinear192_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 7680);} static struct ast_format_def slin192_f = { .name = "sln192", .exts = "sln192", - .write = slinear192_write, + .write = slinear_write, .seek = slinear_seek, .trunc = slinear_trunc, .tell = slinear_tell, @@ -259,15 +240,16 @@ static struct ast_format_def *slin_list[] = { static int load_module(void) { int i; - ast_format_set(&slin_f.format, AST_FORMAT_SLINEAR, 0); - ast_format_set(&slin12_f.format, AST_FORMAT_SLINEAR12, 0); - ast_format_set(&slin16_f.format, AST_FORMAT_SLINEAR16, 0); - ast_format_set(&slin24_f.format, AST_FORMAT_SLINEAR24, 0); - ast_format_set(&slin32_f.format, AST_FORMAT_SLINEAR32, 0); - ast_format_set(&slin44_f.format, AST_FORMAT_SLINEAR44, 0); - ast_format_set(&slin48_f.format, AST_FORMAT_SLINEAR48, 0); - ast_format_set(&slin96_f.format, AST_FORMAT_SLINEAR96, 0); - ast_format_set(&slin192_f.format, AST_FORMAT_SLINEAR192, 0); + + slin_f.format = ast_format_slin; + slin12_f.format = ast_format_slin12; + slin16_f.format = ast_format_slin16; + slin24_f.format = ast_format_slin24; + slin32_f.format = ast_format_slin32; + slin44_f.format = ast_format_slin44; + slin48_f.format = ast_format_slin48; + slin96_f.format = ast_format_slin96; + slin192_f.format = ast_format_slin192; for (i = 0; i < ARRAY_LEN(slin_list); i++) { if (ast_format_def_register(slin_list[i])) { diff --git a/formats/format_vox.c b/formats/format_vox.c index f640021d8..7beec5e6a 100644 --- a/formats/format_vox.c +++ b/formats/format_vox.c @@ -35,6 +35,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" #define BUF_SIZE 80 /* 80 bytes, 160 samples */ #define VOX_SAMPLES 160 @@ -44,9 +45,6 @@ static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext) int res; /* Send a frame from the file to the appropriate channel */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_ADPCM, 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) { if (res) @@ -61,14 +59,6 @@ static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext) static int vox_write(struct ast_filestream *s, struct ast_frame *f) { int res; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_ADPCM) { - ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) { ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); return -1; @@ -147,7 +137,7 @@ static struct ast_format_def vox_f = { static int load_module(void) { - ast_format_set(&vox_f.format, AST_FORMAT_ADPCM, 0); + vox_f.format = ast_format_adpcm; if (ast_format_def_register(&vox_f)) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; diff --git a/formats/format_wav.c b/formats/format_wav.c index 06d6ae12d..fb810625f 100644 --- a/formats/format_wav.c +++ b/formats/format_wav.c @@ -35,6 +35,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" +#include "asterisk/format.h" +#include "asterisk/codec.h" /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */ @@ -319,7 +322,7 @@ static int wav_open(struct ast_filestream *s) if we did, it would go here. We also might want to check and be sure it's a valid file. */ struct wav_desc *tmp = (struct wav_desc *)s->_private; - if ((tmp->maxlen = check_header(s->f, (s->fmt->format.id == AST_FORMAT_SLINEAR16 ? 16000 : 8000))) < 0) + if ((tmp->maxlen = check_header(s->f, ast_format_get_sample_rate(s->fmt->format))) < 0) return -1; return 0; } @@ -331,7 +334,7 @@ static int wav_rewrite(struct ast_filestream *s, const char *comment) and be sure it's a valid file. */ struct wav_desc *tmp = (struct wav_desc *)s->_private; - tmp->hz = (s->fmt->format.id == AST_FORMAT_SLINEAR16 ? 16000 : 8000); + tmp->hz = ast_format_get_sample_rate(s->fmt->format); if (write_header(s->f,tmp->hz)) return -1; return 0; @@ -379,9 +382,6 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext) if (bytes < 0) bytes = 0; /* ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */ - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, (fs->hz == 16000 ? AST_FORMAT_SLINEAR16 : AST_FORMAT_SLINEAR), 0); - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes); if ( (res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) <= 0 ) { @@ -412,18 +412,6 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f) struct wav_desc *s = (struct wav_desc *)fs->_private; int res; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if ((f->subclass.format.id != AST_FORMAT_SLINEAR) && (f->subclass.format.id != AST_FORMAT_SLINEAR16)) { - ast_log(LOG_WARNING, "Asked to write non-SLINEAR%s frame (%s)!\n", s->hz == 16000 ? "16" : "", ast_getformatname(&f->subclass.format)); - return -1; - } - if (ast_format_cmp(&f->subclass.format, &fs->fmt->format) == AST_FORMAT_CMP_NOT_EQUAL) { - ast_log(LOG_WARNING, "Can't change SLINEAR frequency during write\n"); - return -1; - } if (!f->datalen) return -1; @@ -547,8 +535,8 @@ static struct ast_format_def wav_f = { static int load_module(void) { - ast_format_set(&wav_f.format, AST_FORMAT_SLINEAR, 0); - ast_format_set(&wav16_f.format, AST_FORMAT_SLINEAR16, 0); + wav_f.format = ast_format_slin; + wav16_f.format = ast_format_slin16; if (ast_format_def_register(&wav_f) || ast_format_def_register(&wav16_f)) return AST_MODULE_LOAD_FAILURE; diff --git a/formats/format_wav_gsm.c b/formats/format_wav_gsm.c index c8d83ff4c..bbb1977a9 100644 --- a/formats/format_wav_gsm.c +++ b/formats/format_wav_gsm.c @@ -38,6 +38,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/mod_format.h" #include "asterisk/module.h" #include "asterisk/endian.h" +#include "asterisk/format_cache.h" #include "msgsm.h" @@ -409,11 +410,7 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext) /* Send a frame from the file to the appropriate channel */ struct wavg_desc *fs = (struct wavg_desc *)s->_private; - s->fr.frametype = AST_FRAME_VOICE; - ast_format_set(&s->fr.subclass.format, AST_FORMAT_GSM, 0); - s->fr.offset = AST_FRIENDLY_OFFSET; s->fr.samples = GSM_SAMPLES; - s->fr.mallocd = 0; AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE); if (fs->secondhalf) { /* Just return a frame based on the second GSM frame */ @@ -443,14 +440,6 @@ static int wav_write(struct ast_filestream *s, struct ast_frame *f) int size; struct wavg_desc *fs = (struct wavg_desc *)s->_private; - if (f->frametype != AST_FRAME_VOICE) { - ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); - return -1; - } - if (f->subclass.format.id != AST_FORMAT_GSM) { - ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(&f->subclass.format)); - return -1; - } /* XXX this might fail... if the input is a multiple of MSGSM_FRAME_SIZE * we assume it is already in the correct format. */ @@ -577,7 +566,7 @@ static struct ast_format_def wav49_f = { static int load_module(void) { - ast_format_set(&wav49_f.format, AST_FORMAT_GSM, 0); + wav49_f.format = ast_format_gsm; if (ast_format_def_register(&wav49_f)) return AST_MODULE_LOAD_FAILURE; return AST_MODULE_LOAD_SUCCESS; |