Version 0.1.1 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 (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                 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 (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                 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 (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         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) * (ms - delay) > 4)
191                         s->adj -= (ms - delay);
192         }
193         s->fr.timelen = delay;
194 #if 0
195         ast_log(LOG_DEBUG, "delay is %d, adjusting by %d, as last was %d\n", delay, s->adj, ms);
196 #endif
197         delay += s->adj;
198         if (delay < 1)
199                 delay = 1;
200         /* Lastly, process the frame */
201         if (ast_write(s->owner, &s->fr)) {
202                 ast_log(LOG_WARNING, "Failed to write frame\n");
203                 s->owner->streamid = -1;
204                 return 0;
205         }
206         gettimeofday(&s->last, NULL);
207         if (s->lasttimeout != delay) {
208                 s->owner->streamid = ast_sched_add(s->owner->sched, delay, ast_read_callback, s);
209                 s->lasttimeout = delay;
210                 return 0;
211         }
212         return -1;
213 }
214
215 static int mp3_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         ast_read_callback(s);
220         return 0;
221 }
222
223 static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
224 {
225         int res;
226         if (f->frametype != AST_FRAME_VOICE) {
227                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
228                 return -1;
229         }
230         if (f->subclass != AST_FORMAT_MP3) {
231                 ast_log(LOG_WARNING, "Asked to write non-mp3 frame!\n");
232                 return -1;
233         }
234         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
235                 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
236                 return -1;
237         }       
238         return 0;
239 }
240
241 static char *mp3_getcomment(struct ast_filestream *s)
242 {
243         return NULL;
244 }
245
246 int load_module()
247 {
248         return ast_format_register(name, exts, AST_FORMAT_MP3,
249                                                                 mp3_open,
250                                                                 mp3_rewrite,
251                                                                 mp3_apply,
252                                                                 mp3_write,
253                                                                 mp3_read,
254                                                                 mp3_close,
255                                                                 mp3_getcomment);
256                                                                 
257                                                                 
258 }
259
260 int unload_module()
261 {
262         struct ast_filestream *tmp, *tmpl;
263         if (pthread_mutex_lock(&mp3_lock)) {
264                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
265                 return -1;
266         }
267         tmp = glist;
268         while(tmp) {
269                 if (tmp->owner)
270                         ast_softhangup(tmp->owner);
271                 tmpl = tmp;
272                 tmp = tmp->next;
273                 free(tmpl);
274         }
275         pthread_mutex_unlock(&mp3_lock);
276         return ast_format_unregister(name);
277 }       
278
279 int usecount()
280 {
281         int res;
282         if (pthread_mutex_lock(&mp3_lock)) {
283                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
284                 return -1;
285         }
286         res = glistcnt;
287         pthread_mutex_unlock(&mp3_lock);
288         return res;
289 }
290
291 char *description()
292 {
293         return desc;
294 }
295