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