8cc1431786efac40031ce9c7a7b3ba4689fa732f
[asterisk/asterisk.git] / formats / format_vox.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Flat, binary, ADPCM vox file format.
22  * \arg File name extensions: vox
23  * 
24  * \ingroup formats
25  */
26  
27 #include <unistd.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <string.h>
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/module.h"
46 #include "asterisk/endian.h"
47
48 #define BUF_SIZE 80             /* 160 samples */
49
50 struct ast_filestream {
51         void *reserved[AST_RESERVED_POINTERS];
52         /* This is what a filestream means to us */
53         FILE *f; /* Descriptor */
54         struct ast_frame fr;                            /* Frame information */
55         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
56         char empty;                                                     /* Empty character */
57         unsigned char buf[BUF_SIZE];    /* Output Buffer */
58         int lasttimeout;
59         struct timeval last;
60         short signal;                                           /* Signal level (file side) */
61         short ssindex;                                          /* Signal ssindex (file side) */
62         unsigned char zero_count;                               /* counter of consecutive zero samples */
63         unsigned char next_flag;
64 };
65
66
67 AST_MUTEX_DEFINE_STATIC(vox_lock);
68 static int glistcnt = 0;
69
70 static char *name = "vox";
71 static char *desc = "Dialogic VOX (ADPCM) File Format";
72 static char *exts = "vox";
73
74 static struct ast_filestream *vox_open(FILE *f)
75 {
76         /* We don't have any header to read or anything really, but
77            if we did, it would go here.  We also might want to check
78            and be sure it's a valid file.  */
79         struct ast_filestream *tmp;
80         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
81                 memset(tmp, 0, sizeof(struct ast_filestream));
82                 if (ast_mutex_lock(&vox_lock)) {
83                         ast_log(LOG_WARNING, "Unable to lock vox list\n");
84                         free(tmp);
85                         return NULL;
86                 }
87                 tmp->f = f;
88                 tmp->fr.data = tmp->buf;
89                 tmp->fr.frametype = AST_FRAME_VOICE;
90                 tmp->fr.subclass = AST_FORMAT_ADPCM;
91                 /* datalen will vary for each frame */
92                 tmp->fr.src = name;
93                 tmp->fr.mallocd = 0;
94                 tmp->lasttimeout = -1;
95                 glistcnt++;
96                 ast_mutex_unlock(&vox_lock);
97                 ast_update_use_count();
98         }
99         return tmp;
100 }
101
102 static struct ast_filestream *vox_rewrite(FILE *f, const char *comment)
103 {
104         /* We don't have any header to read or anything really, but
105            if we did, it would go here.  We also might want to check
106            and be sure it's a valid file.  */
107         struct ast_filestream *tmp;
108         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
109                 memset(tmp, 0, sizeof(struct ast_filestream));
110                 if (ast_mutex_lock(&vox_lock)) {
111                         ast_log(LOG_WARNING, "Unable to lock vox list\n");
112                         free(tmp);
113                         return NULL;
114                 }
115                 tmp->f = f;
116                 glistcnt++;
117                 ast_mutex_unlock(&vox_lock);
118                 ast_update_use_count();
119         } else
120                 ast_log(LOG_WARNING, "Out of memory\n");
121         return tmp;
122 }
123
124 static void vox_close(struct ast_filestream *s)
125 {
126         if (ast_mutex_lock(&vox_lock)) {
127                 ast_log(LOG_WARNING, "Unable to lock vox list\n");
128                 return;
129         }
130         glistcnt--;
131         ast_mutex_unlock(&vox_lock);
132         ast_update_use_count();
133         fclose(s->f);
134         free(s);
135         s = NULL;
136 }
137
138 static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
139 {
140         int res;
141         /* Send a frame from the file to the appropriate channel */
142         s->fr.frametype = AST_FRAME_VOICE;
143         s->fr.subclass = AST_FORMAT_ADPCM;
144         s->fr.offset = AST_FRIENDLY_OFFSET;
145         s->fr.mallocd = 0;
146         s->fr.data = s->buf;
147         if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
148                 if (res)
149                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
150                 return NULL;
151         }
152         s->fr.samples = res * 2;
153         s->fr.datalen = res;
154         *whennext = s->fr.samples;
155         return &s->fr;
156 }
157
158 static int vox_write(struct ast_filestream *fs, struct ast_frame *f)
159 {
160         int res;
161         if (f->frametype != AST_FRAME_VOICE) {
162                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
163                 return -1;
164         }
165         if (f->subclass != AST_FORMAT_ADPCM) {
166                 ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
167                 return -1;
168         }
169         if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
170                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
171                         return -1;
172         }
173         return 0;
174 }
175
176 static char *vox_getcomment(struct ast_filestream *s)
177 {
178         return NULL;
179 }
180
181 static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
182 {
183      off_t offset=0,min,cur,max,distance;
184         
185      min = 0;
186      cur = ftello(fs->f);
187      fseeko(fs->f, 0, SEEK_END);
188          max = ftello(fs->f);
189          
190      /* have to fudge to frame here, so not fully to sample */
191      distance = sample_offset/2;
192      if(whence == SEEK_SET)
193           offset = distance;
194      else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
195           offset = distance + cur;
196      else if(whence == SEEK_END)
197           offset = max - distance;
198      if (whence != SEEK_FORCECUR) {
199           offset = (offset > max)?max:offset;
200           offset = (offset < min)?min:offset;
201      }
202      fseeko(fs->f, offset, SEEK_SET);
203          return ftello(fs->f);
204 }
205
206 static int vox_trunc(struct ast_filestream *fs)
207 {
208      return ftruncate(fileno(fs->f), ftello(fs->f));
209 }
210
211 static off_t vox_tell(struct ast_filestream *fs)
212 {
213      off_t offset;
214      offset = ftello(fs->f) << 1;
215      return offset; 
216 }
217
218 int load_module()
219 {
220         return ast_format_register(name, exts, AST_FORMAT_ADPCM,
221                                                                 vox_open,
222                                                                 vox_rewrite,
223                                                                 vox_write,
224                                                                 vox_seek,
225                                                                 vox_trunc,
226                                                                 vox_tell,
227                                                                 vox_read,
228                                                                 vox_close,
229                                                                 vox_getcomment);
230                                                                 
231                                                                 
232 }
233
234 int unload_module()
235 {
236         return ast_format_unregister(name);
237 }       
238
239 int usecount()
240 {
241         return glistcnt;
242 }
243
244 char *description()
245 {
246         return desc;
247 }
248
249
250 char *key()
251 {
252         return ASTERISK_GPL_KEY;
253 }