Version 0.1.8 from FTP
[asterisk/asterisk.git] / formats / format_mp3.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Everybody's favorite format: MP3 Files!  Yay!
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/channel.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/sched.h>
18 #include <asterisk/module.h>
19 #include <arpa/inet.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <pthread.h>
26 #include <sys/time.h>
27 #include "../channels/adtranvofr.h"
28
29 #define MAX_FRAME_SIZE 1441
30
31 struct ast_filestream {
32         /* First entry MUST be reserved for the channel type */
33         void *reserved[AST_RESERVED_POINTERS];
34         /* This is what a filestream means to us */
35         int fd; /* Descriptor */
36         struct ast_channel *owner;
37         struct ast_filestream *next;
38         struct ast_frame fr;    /* Frame representation of buf */
39         char offset[AST_FRIENDLY_OFFSET];
40         unsigned char buf[MAX_FRAME_SIZE * 2];
41         int lasttimeout;
42         int pos;
43         int adj;
44         struct timeval last;
45 };
46
47
48 static struct ast_filestream *glist = NULL;
49 static pthread_mutex_t mp3_lock = PTHREAD_MUTEX_INITIALIZER;
50 static int glistcnt = 0;
51
52 static char *name = "mp3";
53 static char *desc = "MPEG-1,2 Layer 3 File Format Support";
54 static char *exts = "mp3|mpeg3";
55
56 #include "../codecs/mp3anal.h"
57
58 static struct ast_filestream *mp3_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                 if (ast_pthread_mutex_lock(&mp3_lock)) {
66                         ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
67                         free(tmp);
68                         return NULL;
69                 }
70                 tmp->next = glist;
71                 glist = tmp;
72                 tmp->fd = fd;
73                 tmp->owner = NULL;
74                 tmp->lasttimeout = -1;
75                 tmp->last.tv_usec = 0;
76                 tmp->last.tv_sec = 0;
77                 tmp->adj = 0;
78                 glistcnt++;
79                 ast_pthread_mutex_unlock(&mp3_lock);
80                 ast_update_use_count();
81         }
82         return tmp;
83 }
84
85 static struct ast_filestream *mp3_rewrite(int fd, char *comment)
86 {
87         /* We don't have any header to read or anything really, but
88            if we did, it would go here.  We also might want to check
89            and be sure it's a valid file.  */
90         struct ast_filestream *tmp;
91         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
92                 if (ast_pthread_mutex_lock(&mp3_lock)) {
93                         ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
94                         free(tmp);
95                         return NULL;
96                 }
97                 tmp->next = glist;
98                 glist = tmp;
99                 tmp->fd = fd;
100                 tmp->owner = NULL;
101                 glistcnt++;
102                 ast_pthread_mutex_unlock(&mp3_lock);
103                 ast_update_use_count();
104         } else
105                 ast_log(LOG_WARNING, "Out of memory\n");
106         return tmp;
107 }
108
109 static struct ast_frame *mp3_read(struct ast_filestream *s)
110 {
111         return NULL;
112 }
113
114 static void mp3_close(struct ast_filestream *s)
115 {
116         struct ast_filestream *tmp, *tmpl = NULL;
117         if (ast_pthread_mutex_lock(&mp3_lock)) {
118                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
119                 return;
120         }
121         tmp = glist;
122         while(tmp) {
123                 if (tmp == s) {
124                         if (tmpl)
125                                 tmpl->next = tmp->next;
126                         else
127                                 glist = tmp->next;
128                         break;
129                 }
130                 tmpl = tmp;
131                 tmp = tmp->next;
132         }
133         glistcnt--;
134         if (s->owner) {
135                 s->owner->stream = NULL;
136                 if (s->owner->streamid > -1)
137                         ast_sched_del(s->owner->sched, s->owner->streamid);
138                 s->owner->streamid = -1;
139         }
140         ast_pthread_mutex_unlock(&mp3_lock);
141         ast_update_use_count();
142         if (!tmp) 
143                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
144         close(s->fd);
145         free(s);
146 }
147
148 static int ast_read_callback(void *data)
149 {
150         /* XXX Don't assume frames are this size XXX */
151         u_int32_t delay = -1;
152         int res;
153         struct ast_filestream *s = data;
154         int size;
155         int ms=0;
156         struct timeval tv;
157         if ((res = read(s->fd, s->buf , 4)) != 4) {
158                 ast_log(LOG_WARNING, "Short read (%d of 4 bytes) (%s)!\n", res, strerror(errno));
159                 s->owner->streamid = -1;
160                 return 0;
161         }
162         if (mp3_badheader(s->buf)) {
163                 ast_log(LOG_WARNING, "Bad mp3 header\n");
164                 return 0;
165         }
166         if ((size = mp3_framelen(s->buf)) < 0) {
167                 ast_log(LOG_WARNING, "Unable to calculate frame size\n");
168                 return 0;
169         }
170         if ((res = read(s->fd, s->buf + 4 , size - 4)) != size - 4) {
171                 ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size - 4, strerror(errno));
172                 s->owner->streamid = -1;
173                 return 0;
174         }
175         /* Send a frame from the file to the appropriate channel */
176         /* Read the data into the buffer */
177         s->fr.offset = AST_FRIENDLY_OFFSET;
178         s->fr.frametype = AST_FRAME_VOICE;
179         s->fr.subclass = AST_FORMAT_MP3;
180         s->fr.mallocd = 0;
181         s->fr.src = name;
182         s->fr.datalen = size;
183         s->fr.data = s->buf;
184         delay = mp3_samples(s->buf) * 1000 / mp3_samplerate(s->buf);
185         if (s->last.tv_sec || s->last.tv_usec) {
186                 /* To keep things running smoothly, we watch how close we're coming */
187                 gettimeofday(&tv, NULL);
188                 ms = ((tv.tv_usec - s->last.tv_usec) / 1000 + (tv.tv_sec - s->last.tv_sec) * 1000);
189                 /* If we're within 2 milliseconds, that's close enough */
190                 if ((ms - delay) > 0 )
191                         s->adj -= (ms - delay);
192                 s->adj -= 2;
193         }
194         s->fr.timelen = delay;
195 #if 0
196         ast_log(LOG_DEBUG, "delay is %d, adjusting by %d, as last was %d\n", delay, s->adj, ms);
197 #endif
198         delay += s->adj;
199         if (delay < 1)
200                 delay = 1;
201         /* Lastly, process the frame */
202         if (ast_write(s->owner, &s->fr)) {
203                 ast_log(LOG_WARNING, "Failed to write frame\n");
204                 s->owner->streamid = -1;
205                 return 0;
206         }
207         gettimeofday(&s->last, NULL);
208         if (s->lasttimeout != delay) {
209                 s->owner->streamid = ast_sched_add(s->owner->sched, delay, ast_read_callback, s);
210                 s->lasttimeout = delay;
211                 return 0;
212         }
213         return -1;
214 }
215
216 static int mp3_apply(struct ast_channel *c, struct ast_filestream *s)
217 {
218         /* Select our owner for this stream, and get the ball rolling. */
219         s->owner = c;
220         ast_read_callback(s);
221         return 0;
222 }
223
224 static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
225 {
226         int res;
227         if (f->frametype != AST_FRAME_VOICE) {
228                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
229                 return -1;
230         }
231         if (f->subclass != AST_FORMAT_MP3) {
232                 ast_log(LOG_WARNING, "Asked to write non-mp3 frame!\n");
233                 return -1;
234         }
235         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
236                 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
237                 return -1;
238         }       
239         return 0;
240 }
241
242 static char *mp3_getcomment(struct ast_filestream *s)
243 {
244         return NULL;
245 }
246
247 int load_module()
248 {
249         return ast_format_register(name, exts, AST_FORMAT_MP3,
250                                                                 mp3_open,
251                                                                 mp3_rewrite,
252                                                                 mp3_apply,
253                                                                 mp3_write,
254                                                                 mp3_read,
255                                                                 mp3_close,
256                                                                 mp3_getcomment);
257                                                                 
258                                                                 
259 }
260
261 int unload_module()
262 {
263         struct ast_filestream *tmp, *tmpl;
264         if (ast_pthread_mutex_lock(&mp3_lock)) {
265                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
266                 return -1;
267         }
268         tmp = glist;
269         while(tmp) {
270                 if (tmp->owner)
271                         ast_softhangup(tmp->owner);
272                 tmpl = tmp;
273                 tmp = tmp->next;
274                 free(tmpl);
275         }
276         ast_pthread_mutex_unlock(&mp3_lock);
277         return ast_format_unregister(name);
278 }       
279
280 int usecount()
281 {
282         int res;
283         if (ast_pthread_mutex_lock(&mp3_lock)) {
284                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
285                 return -1;
286         }
287         res = glistcnt;
288         ast_pthread_mutex_unlock(&mp3_lock);
289         return res;
290 }
291
292 char *description()
293 {
294         return desc;
295 }
296
297
298 char *key()
299 {
300         return ASTERISK_GPL_KEY;
301 }