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