811acbe149a98d9af6c6778f395114803673ef20
[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, Adtran Inc. and Linux Support Services, LLC
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
30 #define MP3_MAX_SIZE 1400
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 buf[sizeof(struct ast_frame) + MP3_MAX_SIZE + AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
41         int pos;
42 };
43
44
45 static struct ast_filestream *glist = NULL;
46 static pthread_mutex_t mp3_lock = PTHREAD_MUTEX_INITIALIZER;
47 static int glistcnt = 0;
48
49 static char *name = "mp3";
50 static char *desc = "MPEG-2 Layer 3 File Format Support";
51 static char *exts = "mp3|mpeg3";
52
53 #if 0
54 #define MP3_FRAMELEN 417
55 #else
56 #define MP3_FRAMELEN 400
57 #endif
58 #define MP3_OUTPUTLEN 2304      /* Bytes */
59 #define MP3_TIMELEN ((MP3_OUTPUTLEN * 1000 / 16000) )
60
61 static struct ast_filestream *mp3_open(int fd)
62 {
63         /* We don't have any header to read or anything really, but
64            if we did, it would go here.  We also might want to check
65            and be sure it's a valid file.  */
66         struct ast_filestream *tmp;
67         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
68                 if (pthread_mutex_lock(&mp3_lock)) {
69                         ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
70                         free(tmp);
71                         return NULL;
72                 }
73                 tmp->next = glist;
74                 glist = tmp;
75                 tmp->fd = fd;
76                 tmp->owner = NULL;
77                 tmp->fr = (struct ast_frame *)tmp->buf;
78                 tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
79                 tmp->fr->frametype = AST_FRAME_VOICE;
80                 tmp->fr->subclass = AST_FORMAT_MP3;
81                 /* datalen will vary for each frame */
82                 tmp->fr->src = name;
83                 tmp->fr->mallocd = 0;
84                 glistcnt++;
85                 pthread_mutex_unlock(&mp3_lock);
86                 ast_update_use_count();
87         }
88         return tmp;
89 }
90
91 static struct ast_filestream *mp3_rewrite(int fd, char *comment)
92 {
93         /* We don't have any header to read or anything really, but
94            if we did, it would go here.  We also might want to check
95            and be sure it's a valid file.  */
96         struct ast_filestream *tmp;
97         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
98                 if (pthread_mutex_lock(&mp3_lock)) {
99                         ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
100                         free(tmp);
101                         return NULL;
102                 }
103                 tmp->next = glist;
104                 glist = tmp;
105                 tmp->fd = fd;
106                 tmp->owner = NULL;
107                 tmp->fr = NULL;
108                 glistcnt++;
109                 pthread_mutex_unlock(&mp3_lock);
110                 ast_update_use_count();
111         } else
112                 ast_log(LOG_WARNING, "Out of memory\n");
113         return tmp;
114 }
115
116 static struct ast_frame *mp3_read(struct ast_filestream *s)
117 {
118         return NULL;
119 }
120
121 static void mp3_close(struct ast_filestream *s)
122 {
123         struct ast_filestream *tmp, *tmpl = NULL;
124         if (pthread_mutex_lock(&mp3_lock)) {
125                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
126                 return;
127         }
128         tmp = glist;
129         while(tmp) {
130                 if (tmp == s) {
131                         if (tmpl)
132                                 tmpl->next = tmp->next;
133                         else
134                                 glist = tmp->next;
135                         break;
136                 }
137                 tmpl = tmp;
138                 tmp = tmp->next;
139         }
140         glistcnt--;
141         if (s->owner) {
142                 s->owner->stream = NULL;
143                 if (s->owner->streamid > -1)
144                         ast_sched_del(s->owner->sched, s->owner->streamid);
145                 s->owner->streamid = -1;
146         }
147         pthread_mutex_unlock(&mp3_lock);
148         ast_update_use_count();
149         if (!tmp) 
150                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
151         close(s->fd);
152         free(s);
153 }
154
155 static int ast_read_callback(void *data)
156 {
157         /* XXX Don't assume frames are this size XXX */
158         u_int16_t size=MP3_FRAMELEN;
159         u_int32_t delay = -1;
160         int res;
161         struct ast_filestream *s = data;
162         /* Send a frame from the file to the appropriate channel */
163         /* Read the data into the buffer */
164         s->fr->offset = AST_FRIENDLY_OFFSET;
165         s->fr->datalen = size;
166         s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
167         if ((res = read(s->fd, s->fr->data , size)) != size) {
168                 ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
169                 s->owner->streamid = -1;
170                 return 0;
171         }
172         delay = MP3_TIMELEN;
173         s->fr->timelen = delay;
174         /* Lastly, process the frame */
175         if (ast_write(s->owner, s->fr)) {
176                 ast_log(LOG_WARNING, "Failed to write frame\n");
177                 s->owner->streamid = -1;
178                 return 0;
179         }
180         return -1;
181 }
182
183 static int mp3_apply(struct ast_channel *c, struct ast_filestream *s)
184 {
185         /* Select our owner for this stream, and get the ball rolling. */
186         s->owner = c;
187         s->owner->streamid = ast_sched_add(s->owner->sched, MP3_TIMELEN, ast_read_callback, s);
188         ast_read_callback(s);
189         return 0;
190 }
191
192 static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
193 {
194         int res;
195         if (fs->fr) {
196                 ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
197                 return -1;
198         }
199         if (f->frametype != AST_FRAME_VOICE) {
200                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
201                 return -1;
202         }
203         if (f->subclass != AST_FORMAT_MP3) {
204                 ast_log(LOG_WARNING, "Asked to write non-mp3 frame!\n");
205                 return -1;
206         }
207         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
208                 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
209                 return -1;
210         }       
211         return 0;
212 }
213
214 char *mp3_getcomment(struct ast_filestream *s)
215 {
216         return NULL;
217 }
218
219 int load_module()
220 {
221         return ast_format_register(name, exts, AST_FORMAT_MP3,
222                                                                 mp3_open,
223                                                                 mp3_rewrite,
224                                                                 mp3_apply,
225                                                                 mp3_write,
226                                                                 mp3_read,
227                                                                 mp3_close,
228                                                                 mp3_getcomment);
229                                                                 
230                                                                 
231 }
232
233 int unload_module()
234 {
235         struct ast_filestream *tmp, *tmpl;
236         if (pthread_mutex_lock(&mp3_lock)) {
237                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
238                 return -1;
239         }
240         tmp = glist;
241         while(tmp) {
242                 if (tmp->owner)
243                         ast_softhangup(tmp->owner);
244                 tmpl = tmp;
245                 tmp = tmp->next;
246                 free(tmpl);
247         }
248         pthread_mutex_unlock(&mp3_lock);
249         return ast_format_unregister(name);
250 }       
251
252 int usecount()
253 {
254         int res;
255         if (pthread_mutex_lock(&mp3_lock)) {
256                 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
257                 return -1;
258         }
259         res = glistcnt;
260         pthread_mutex_unlock(&mp3_lock);
261         return res;
262 }
263
264 char *description()
265 {
266         return desc;
267 }
268