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