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