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