Version 0.2.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 }
153
154 static int ast_read_callback(void *data)
155 {
156         int retval = 0;
157         int res;
158         int delay;
159         struct ast_filestream *s = data;
160         struct timeval tv;
161         /* Send a frame from the file to the appropriate channel */
162
163         s->fr.frametype = AST_FRAME_VOICE;
164         s->fr.subclass = AST_FORMAT_ULAW;
165         s->fr.offset = AST_FRIENDLY_OFFSET;
166         s->fr.mallocd = 0;
167         s->fr.data = s->buf;
168         if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) {
169                 if (res)
170                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
171                 s->owner->streamid = -1;
172                 return 0;
173         }
174         s->fr.timelen = res / 8;
175         s->fr.datalen = res;
176         delay = s->fr.timelen;
177         /* Lastly, process the frame */
178         if (ast_write(s->owner, &s->fr)) {
179                 ast_log(LOG_WARNING, "Failed to write frame\n");
180                 s->owner->streamid = -1;
181                 return 0;
182         }
183         if (s->last.tv_usec || s->last.tv_usec) {
184                 int ms;
185                 gettimeofday(&tv, NULL);
186                 ms = 1000 * (tv.tv_sec - s->last.tv_sec) + 
187                         (tv.tv_usec - s->last.tv_usec) / 1000;
188                 s->last.tv_sec = tv.tv_sec;
189                 s->last.tv_usec = tv.tv_usec;
190                 if ((ms - delay) * (ms - delay) > 4) {
191                         /* Compensate if we're more than 2 ms off */
192                         s->adj -= (ms - delay);
193                 }
194 #if 0
195                 fprintf(stdout, "Delay is %d, adjustment is %d, last was %d\n", delay, s->adj, ms);
196 #endif
197                 delay += s->adj;
198                 if (delay < 1)
199                         delay = 1;
200         } else
201                 gettimeofday(&s->last, NULL);
202         if (s->lasttimeout != delay) {
203                 /* We'll install the next timeout now. */
204                 s->owner->streamid = ast_sched_add(s->owner->sched,
205                                 delay, ast_read_callback, s); 
206                 s->lasttimeout = delay;
207         } else {
208                 /* Just come back again at the same time */
209                 retval = -1;
210         }
211         return retval;
212 }
213
214 static int pcm_apply(struct ast_channel *c, struct ast_filestream *s)
215 {
216         /* Select our owner for this stream, and get the ball rolling. */
217         s->owner = c;
218         ast_read_callback(s);
219         return 0;
220 }
221
222 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
223 {
224         int res;
225         if (f->frametype != AST_FRAME_VOICE) {
226                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
227                 return -1;
228         }
229         if (f->subclass != AST_FORMAT_ULAW) {
230                 ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
231                 return -1;
232         }
233         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
234                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
235                         return -1;
236         }
237         return 0;
238 }
239
240 static char *pcm_getcomment(struct ast_filestream *s)
241 {
242         return NULL;
243 }
244
245 int load_module()
246 {
247         return ast_format_register(name, exts, AST_FORMAT_ULAW,
248                                                                 pcm_open,
249                                                                 pcm_rewrite,
250                                                                 pcm_apply,
251                                                                 pcm_write,
252                                                                 pcm_read,
253                                                                 pcm_close,
254                                                                 pcm_getcomment);
255                                                                 
256                                                                 
257 }
258
259 int unload_module()
260 {
261         struct ast_filestream *tmp, *tmpl;
262         if (pthread_mutex_lock(&pcm_lock)) {
263                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
264                 return -1;
265         }
266         tmp = glist;
267         while(tmp) {
268                 if (tmp->owner)
269                         ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
270                 tmpl = tmp;
271                 tmp = tmp->next;
272                 free(tmpl);
273         }
274         pthread_mutex_unlock(&pcm_lock);
275         return ast_format_unregister(name);
276 }       
277
278 int usecount()
279 {
280         int res;
281         if (pthread_mutex_lock(&pcm_lock)) {
282                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
283                 return -1;
284         }
285         res = glistcnt;
286         pthread_mutex_unlock(&pcm_lock);
287         return res;
288 }
289
290 char *description()
291 {
292         return desc;
293 }
294
295
296 char *key()
297 {
298         return ASTERISK_GPL_KEY;
299 }