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