Make ADPCM frames standard
[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 static ast_mutex_t vox_lock = AST_MUTEX_INITIALIZER;
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         return -1;
171 }
172
173 static int vox_trunc(struct ast_filestream *fs)
174 {
175         return -1;
176 }
177
178 static long vox_tell(struct ast_filestream *fs)
179 {
180         return -1;
181 }
182
183 int load_module()
184 {
185         return ast_format_register(name, exts, AST_FORMAT_ADPCM,
186                                                                 vox_open,
187                                                                 vox_rewrite,
188                                                                 vox_write,
189                                                                 vox_seek,
190                                                                 vox_trunc,
191                                                                 vox_tell,
192                                                                 vox_read,
193                                                                 vox_close,
194                                                                 vox_getcomment);
195                                                                 
196                                                                 
197 }
198
199 int unload_module()
200 {
201         return ast_format_unregister(name);
202 }       
203
204 int usecount()
205 {
206         int res;
207         if (ast_mutex_lock(&vox_lock)) {
208                 ast_log(LOG_WARNING, "Unable to lock vox list\n");
209                 return -1;
210         }
211         res = glistcnt;
212         ast_mutex_unlock(&vox_lock);
213         return res;
214 }
215
216 char *description()
217 {
218         return desc;
219 }
220
221
222 char *key()
223 {
224         return ASTERISK_GPL_KEY;
225 }