5c730f098cd74fc11d80ed23cbc41939e3420eda
[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.timelen = delay;
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         ast_read_callback(s);
222         return 0;
223 }
224
225 static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
226 {
227         int res;
228         if (f->frametype != AST_FRAME_VOICE) {
229                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
230                 return -1;
231         }
232         if (f->subclass != AST_FORMAT_MP3) {
233                 ast_log(LOG_WARNING, "Asked to write non-mp3 frame!\n");
234                 return -1;
235         }
236         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
237                 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
238                 return -1;
239         }       
240         return 0;
241 }
242
243 static char *mp3_getcomment(struct ast_filestream *s)
244 {
245         return NULL;
246 }
247
248 int load_module()
249 {
250         return ast_format_register(name, exts, AST_FORMAT_MP3,
251                                                                 mp3_open,
252                                                                 mp3_rewrite,
253                                                                 mp3_apply,
254                                                                 mp3_write,
255                                                                 mp3_read,
256                                                                 mp3_close,
257                                                                 mp3_getcomment);
258                                                                 
259                                                                 
260 }
261
262 int unload_module()
263 {
264         struct ast_filestream *tmp, *tmpl;
265         if (ast_pthread_mutex_lock(&mp3_lock)) {
266                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
267                 return -1;
268         }
269         tmp = glist;
270         while(tmp) {
271                 if (tmp->owner)
272                         ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
273                 tmpl = tmp;
274                 tmp = tmp->next;
275                 free(tmpl);
276         }
277         ast_pthread_mutex_unlock(&mp3_lock);
278         return ast_format_unregister(name);
279 }       
280
281 int usecount()
282 {
283         int res;
284         if (ast_pthread_mutex_lock(&mp3_lock)) {
285                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
286                 return -1;
287         }
288         res = glistcnt;
289         ast_pthread_mutex_unlock(&mp3_lock);
290         return res;
291 }
292
293 char *description()
294 {
295         return desc;
296 }
297
298
299 char *key()
300 {
301         return ASTERISK_GPL_KEY;
302 }