Avoid cppcheck warnings; removing unused vars and a bit of cleanup.
[asterisk/asterisk.git] / formats / format_wav_gsm.c
index 627e841..35d4339 100644 (file)
  * e-mail attachments mainly.
  * \ingroup formats
  */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
  
 #include "asterisk.h"
 
@@ -48,7 +52,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #define        MSGSM_SAMPLES           (2*GSM_SAMPLES) /* samples in an MSGSM block */
 
 /* begin binary data: */
-char msgsm_silence[] = /* 65 */
+static char msgsm_silence[] = /* 65 */
 {0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49
 ,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92
 ,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00
@@ -150,7 +154,7 @@ static int check_header(FILE *f)
                return -1;
        }
        if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
-               ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
+               ast_log(LOG_WARNING, "Unexpected frequency %d\n", ltohl(freq));
                return -1;
        }
        /* Ignore the byte frequency */
@@ -214,8 +218,8 @@ static int update_header(FILE *f)
        /* in a gsm WAV, data starts 60 bytes in */
        bytes = end - MSGSM_DATA_OFFSET;
        samples = htoll(bytes / MSGSM_FRAME_SIZE * MSGSM_SAMPLES);
-       datalen = htoll((bytes + 1) & ~0x1);
-       filelen = htoll(MSGSM_DATA_OFFSET - 8 + ((bytes + 1) & ~0x1));
+       datalen = htoll(bytes);
+       filelen = htoll(MSGSM_DATA_OFFSET - 8 + bytes);
        if (cur < 0) {
                ast_log(LOG_WARNING, "Unable to find our position\n");
                return -1;
@@ -370,7 +374,7 @@ static int wav_open(struct ast_filestream *s)
        /* We don't have any header to read or anything really, but
           if we did, it would go here.  We also might want to check
           and be sure it's a valid file.  */
-       struct wavg_desc *fs = (struct wavg_desc *)s->private;
+       struct wavg_desc *fs = (struct wavg_desc *)s->_private;
 
        if (check_header(s->f))
                return -1;
@@ -389,29 +393,20 @@ static int wav_rewrite(struct ast_filestream *s, const char *comment)
        return 0;
 }
 
-static void wav_close(struct ast_filestream *s)
-{
-       char zero = 0;
-       /* Pad to even length */
-       fseek(s->f, 0, SEEK_END);
-       if (ftello(s->f) & 0x1)
-               fwrite(&zero, 1, 1, s->f);
-}
-
 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;
+       struct wavg_desc *fs = (struct wavg_desc *)s->_private;
 
        s->fr.frametype = AST_FRAME_VOICE;
-       s->fr.subclass = AST_FORMAT_GSM;
+       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 */
-               s->fr.data = (char *)s->fr.data + GSM_FRAME_SIZE;
+               s->fr.data.ptr = (char *)s->fr.data.ptr + GSM_FRAME_SIZE;
                s->fr.offset += GSM_FRAME_SIZE;
        } else {
                /* read and convert */
@@ -424,7 +419,7 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
                        return NULL;
                }
                /* Convert from MS format to two real GSM frames */
-               conv65(msdata, s->fr.data);
+               conv65(msdata, s->fr.data.ptr);
        }
        fs->secondhalf = !fs->secondhalf;
        *whennext = GSM_SAMPLES;
@@ -435,14 +430,14 @@ static int wav_write(struct ast_filestream *s, struct ast_frame *f)
 {
        int len;
        int size;
-       struct wavg_desc *fs = (struct wavg_desc *)s->private;
+       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 != AST_FORMAT_GSM) {
-               ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
+       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
@@ -458,16 +453,16 @@ static int wav_write(struct ast_filestream *s, struct ast_frame *f)
                int res;
                unsigned char *src, msdata[MSGSM_FRAME_SIZE];
                if (fs->secondhalf) {   /* second half of raw gsm to be converted */
-                       memcpy(s->buf + GSM_FRAME_SIZE, f->data + len, GSM_FRAME_SIZE);
+                       memcpy(s->buf + GSM_FRAME_SIZE, f->data.ptr + len, GSM_FRAME_SIZE);
                        conv66((unsigned char *) s->buf, msdata);
                        src = msdata;
                        fs->secondhalf = 0;
                } else if (size == GSM_FRAME_SIZE) {    /* first half of raw gsm */
-                       memcpy(s->buf, f->data + len, GSM_FRAME_SIZE);
+                       memcpy(s->buf, f->data.ptr + len, GSM_FRAME_SIZE);
                        src = NULL;     /* nothing to write */
                        fs->secondhalf = 1;
                } else {        /* raw msgsm data */
-                       src = f->data + len;
+                       src = f->data.ptr + len;
                }
                if (src && (res = fwrite(src, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
                        ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
@@ -481,7 +476,7 @@ static int wav_write(struct ast_filestream *s, struct ast_frame *f)
 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
 {
        off_t offset=0, distance, max;
-       struct wavg_desc *s = (struct wavg_desc *)fs->private;
+       struct wavg_desc *s = (struct wavg_desc *)fs->_private;
 
        off_t min = MSGSM_DATA_OFFSET;
        off_t cur = ftello(fs->f);
@@ -505,7 +500,9 @@ static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
                int i;
                fseek(fs->f, 0, SEEK_END);
                for (i=0; i< (offset - max) / MSGSM_FRAME_SIZE; i++) {
-                       fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f);
+                       if (!fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f)) {
+                               ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
+                       }
                }
        }
        s->secondhalf = 0;
@@ -528,10 +525,9 @@ static off_t wav_tell(struct ast_filestream *fs)
        return (offset - MSGSM_DATA_OFFSET)/MSGSM_FRAME_SIZE*MSGSM_SAMPLES;
 }
 
-static const struct ast_format wav49_f = {
+static struct ast_format_def wav49_f = {
        .name = "wav49",
        .exts = "WAV|wav49",
-       .format = AST_FORMAT_GSM,
        .open = wav_open,
        .rewrite = wav_rewrite,
        .write = wav_write,
@@ -539,21 +535,25 @@ static const struct ast_format wav49_f = {
        .trunc = wav_trunc,
        .tell = wav_tell,
        .read = wav_read,
-       .close = wav_close,
        .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET,
        .desc_size = sizeof(struct wavg_desc),
 };
 
 static int load_module(void)
 {
-       if (ast_format_register(&wav49_f))
+       ast_format_set(&wav49_f.format, AST_FORMAT_GSM, 0);
+       if (ast_format_def_register(&wav49_f))
                return AST_MODULE_LOAD_FAILURE;
        return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)
 {
-       return ast_format_unregister(wav49_f.name);
-}      
+       return ast_format_def_unregister(wav49_f.name);
+}
 
-AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Microsoft WAV format (Proprietary GSM)");
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV format (Proprietary GSM)",
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_APP_DEPEND
+);