More include fixes
[asterisk/asterisk.git] / formats / format_pcm.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Flat, binary, ulaw PCM 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 <arpa/inet.h>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <pthread.h>
28 #ifdef __linux__
29 #include <endian.h>
30 #else
31 #include <machine/endian.h>
32 #endif
33
34 #define BUF_SIZE 160            /* 160 samples */
35
36 struct ast_filestream {
37         void *reserved[AST_RESERVED_POINTERS];
38         /* Believe it or not, we must decode/recode to account for the
39            weird MS format */
40         /* This is what a filestream means to us */
41         int fd; /* Descriptor */
42         struct ast_channel *owner;
43         struct ast_frame fr;                            /* Frame information */
44         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
45         char empty;                                                     /* Empty character */
46         unsigned char buf[BUF_SIZE];                            /* Output Buffer */
47         int lasttimeout;
48         struct timeval last;
49         int adj;
50         struct ast_filestream *next;
51 };
52
53
54 static struct ast_filestream *glist = NULL;
55 static pthread_mutex_t pcm_lock = AST_MUTEX_INITIALIZER;
56 static int glistcnt = 0;
57
58 static char *name = "pcm";
59 static char *desc = "Raw uLaw 8khz Audio support (PCM)";
60 static char *exts = "pcm|ulaw|ul|mu";
61
62 static struct ast_filestream *pcm_open(int fd)
63 {
64         /* We don't have any header to read or anything really, but
65            if we did, it would go here.  We also might want to check
66            and be sure it's a valid file.  */
67         struct ast_filestream *tmp;
68         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
69                 memset(tmp, 0, sizeof(struct ast_filestream));
70                 if (pthread_mutex_lock(&pcm_lock)) {
71                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
72                         free(tmp);
73                         return NULL;
74                 }
75                 tmp->next = glist;
76                 glist = tmp;
77                 tmp->fd = fd;
78                 tmp->owner = NULL;
79                 tmp->fr.data = tmp->buf;
80                 tmp->fr.frametype = AST_FRAME_VOICE;
81                 tmp->fr.subclass = AST_FORMAT_ULAW;
82                 /* datalen will vary for each frame */
83                 tmp->fr.src = name;
84                 tmp->fr.mallocd = 0;
85                 tmp->lasttimeout = -1;
86                 glistcnt++;
87                 pthread_mutex_unlock(&pcm_lock);
88                 ast_update_use_count();
89         }
90         return tmp;
91 }
92
93 static struct ast_filestream *pcm_rewrite(int fd, char *comment)
94 {
95         /* We don't have any header to read or anything really, but
96            if we did, it would go here.  We also might want to check
97            and be sure it's a valid file.  */
98         struct ast_filestream *tmp;
99         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
100                 memset(tmp, 0, sizeof(struct ast_filestream));
101                 if (pthread_mutex_lock(&pcm_lock)) {
102                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
103                         free(tmp);
104                         return NULL;
105                 }
106                 tmp->next = glist;
107                 glist = tmp;
108                 tmp->fd = fd;
109                 tmp->owner = NULL;
110                 tmp->lasttimeout = -1;
111                 glistcnt++;
112                 pthread_mutex_unlock(&pcm_lock);
113                 ast_update_use_count();
114         } else
115                 ast_log(LOG_WARNING, "Out of memory\n");
116         return tmp;
117 }
118
119 static struct ast_frame *pcm_read(struct ast_filestream *s)
120 {
121         return NULL;
122 }
123
124 static void pcm_close(struct ast_filestream *s)
125 {
126         struct ast_filestream *tmp, *tmpl = NULL;
127         if (pthread_mutex_lock(&pcm_lock)) {
128                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
129                 return;
130         }
131         tmp = glist;
132         while(tmp) {
133                 if (tmp == s) {
134                         if (tmpl)
135                                 tmpl->next = tmp->next;
136                         else
137                                 glist = tmp->next;
138                         break;
139                 }
140                 tmpl = tmp;
141                 tmp = tmp->next;
142         }
143         glistcnt--;
144         if (s->owner) {
145                 s->owner->stream = NULL;
146                 if (s->owner->streamid > -1)
147                         ast_sched_del(s->owner->sched, s->owner->streamid);
148                 s->owner->streamid = -1;
149         }
150         pthread_mutex_unlock(&pcm_lock);
151         ast_update_use_count();
152         if (!tmp) 
153                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
154         close(s->fd);
155         free(s);
156         s = NULL;
157 }
158
159 static int ast_read_callback(void *data)
160 {
161         int retval = 0;
162         int res;
163         int delay;
164         struct ast_filestream *s = data;
165         struct timeval tv;
166         /* Send a frame from the file to the appropriate channel */
167
168         s->fr.frametype = AST_FRAME_VOICE;
169         s->fr.subclass = AST_FORMAT_ULAW;
170         s->fr.offset = AST_FRIENDLY_OFFSET;
171         s->fr.mallocd = 0;
172         s->fr.data = s->buf;
173         if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) {
174                 if (res)
175                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
176                 s->owner->streamid = -1;
177                 return 0;
178         }
179         s->fr.samples = res;
180         s->fr.datalen = res;
181         delay = s->fr.samples/8;
182         /* Lastly, process the frame */
183         if (ast_write(s->owner, &s->fr)) {
184                 ast_log(LOG_WARNING, "Failed to write frame\n");
185                 s->owner->streamid = -1;
186                 return 0;
187         }
188         if (s->last.tv_usec || s->last.tv_usec) {
189                 int ms;
190                 gettimeofday(&tv, NULL);
191                 ms = 1000 * (tv.tv_sec - s->last.tv_sec) + 
192                         (tv.tv_usec - s->last.tv_usec) / 1000;
193                 s->last.tv_sec = tv.tv_sec;
194                 s->last.tv_usec = tv.tv_usec;
195                 if ((ms - delay) * (ms - delay) > 4) {
196                         /* Compensate if we're more than 2 ms off */
197                         s->adj -= (ms - delay);
198                 }
199 #if 0
200                 fprintf(stdout, "Delay is %d, adjustment is %d, last was %d\n", delay, s->adj, ms);
201 #endif
202                 delay += s->adj;
203                 if (delay < 1)
204                         delay = 1;
205         } else
206                 gettimeofday(&s->last, NULL);
207         if (s->lasttimeout != delay) {
208                 /* We'll install the next timeout now. */
209                 s->owner->streamid = ast_sched_add(s->owner->sched,
210                                 delay, ast_read_callback, s); 
211                 s->lasttimeout = delay;
212         } else {
213                 /* Just come back again at the same time */
214                 retval = -1;
215         }
216         return retval;
217 }
218
219 static int pcm_apply(struct ast_channel *c, struct ast_filestream *s)
220 {
221         /* Select our owner for this stream, and get the ball rolling. */
222         s->owner = c;
223         return 0;
224 }
225
226 static int pcm_play(struct ast_filestream *s)
227 {
228         ast_read_callback(s);
229         return 0;
230 }
231
232 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
233 {
234         int res;
235         if (f->frametype != AST_FRAME_VOICE) {
236                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
237                 return -1;
238         }
239         if (f->subclass != AST_FORMAT_ULAW) {
240                 ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
241                 return -1;
242         }
243         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
244                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
245                         return -1;
246         }
247         return 0;
248 }
249
250 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
251 {
252         off_t offset,min,cur,max;
253
254         min = 0;
255         cur = lseek(fs->fd, 0, SEEK_CUR);
256         max = lseek(fs->fd, 0, SEEK_END);
257         if(whence == SEEK_SET)
258                 offset = sample_offset;
259         if(whence == SEEK_CUR)
260                 offset = sample_offset + cur;
261         if(whence == SEEK_END)
262                 offset = max - sample_offset;
263         offset = (offset > max)?max:offset;
264         offset = (offset < min)?min:offset;
265         return lseek(fs->fd, offset, SEEK_SET);
266 }
267
268 static int pcm_trunc(struct ast_filestream *fs)
269 {
270         return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
271 }
272
273 static long pcm_tell(struct ast_filestream *fs)
274 {
275         off_t offset;
276         offset = lseek(fs->fd, 0, SEEK_CUR);
277         return offset;
278 }
279
280 static char *pcm_getcomment(struct ast_filestream *s)
281 {
282         return NULL;
283 }
284
285 int load_module()
286 {
287         return ast_format_register(name, exts, AST_FORMAT_ULAW,
288                                                                 pcm_open,
289                                                                 pcm_rewrite,
290                                                                 pcm_apply,
291                                                                 pcm_play,
292                                                                 pcm_write,
293                                                                 pcm_seek,
294                                                                 pcm_trunc,
295                                                                 pcm_tell,
296                                                                 pcm_read,
297                                                                 pcm_close,
298                                                                 pcm_getcomment);
299                                                                 
300                                                                 
301 }
302
303 int unload_module()
304 {
305         struct ast_filestream *tmp, *tmpl;
306         if (pthread_mutex_lock(&pcm_lock)) {
307                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
308                 return -1;
309         }
310         tmp = glist;
311         while(tmp) {
312                 if (tmp->owner)
313                         ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
314                 tmpl = tmp;
315                 tmp = tmp->next;
316                 free(tmpl);
317         }
318         pthread_mutex_unlock(&pcm_lock);
319         return ast_format_unregister(name);
320 }       
321
322 int usecount()
323 {
324         int res;
325         if (pthread_mutex_lock(&pcm_lock)) {
326                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
327                 return -1;
328         }
329         res = glistcnt;
330         pthread_mutex_unlock(&pcm_lock);
331         return res;
332 }
333
334 char *description()
335 {
336         return desc;
337 }
338
339
340 char *key()
341 {
342         return ASTERISK_GPL_KEY;
343 }