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