2 * Asterisk -- A telephony toolkit for Linux.
4 * Everybody's favorite format: MP3 Files! Yay!
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
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>
28 #include "../channels/adtranvofr.h"
30 #define MAX_FRAME_SIZE 1441
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];
49 static struct ast_filestream *glist = NULL;
50 static pthread_mutex_t mp3_lock = AST_MUTEX_INITIALIZER;
51 static int glistcnt = 0;
53 static char *name = "mp3";
54 static char *desc = "MPEG-1,2 Layer 3 File Format Support";
55 static char *exts = "mp3|mpeg3";
57 #include "../codecs/mp3anal.h"
59 static struct ast_filestream *mp3_open(int fd)
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");
75 tmp->lasttimeout = -1;
76 tmp->last.tv_usec = 0;
80 ast_pthread_mutex_unlock(&mp3_lock);
81 ast_update_use_count();
86 static struct ast_filestream *mp3_rewrite(int fd, char *comment)
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");
103 ast_pthread_mutex_unlock(&mp3_lock);
104 ast_update_use_count();
106 ast_log(LOG_WARNING, "Out of memory\n");
110 static struct ast_frame *mp3_read(struct ast_filestream *s)
115 static void mp3_close(struct ast_filestream *s)
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");
126 tmpl->next = tmp->next;
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;
141 ast_pthread_mutex_unlock(&mp3_lock);
142 ast_update_use_count();
144 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
149 static int ast_read_callback(void *data)
151 /* XXX Don't assume frames are this size XXX */
152 u_int32_t delay = -1;
154 struct ast_filestream *s = data;
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;
163 if (mp3_badheader(s->buf)) {
164 ast_log(LOG_WARNING, "Bad mp3 header\n");
167 if ((size = mp3_framelen(s->buf)) < 0) {
168 ast_log(LOG_WARNING, "Unable to calculate frame size\n");
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;
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;
183 s->fr.datalen = size;
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);
195 s->fr.samples = delay * 8;
197 ast_log(LOG_DEBUG, "delay is %d, adjusting by %d, as last was %d\n", delay, s->adj, ms);
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;
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;
217 static int mp3_apply(struct ast_channel *c, struct ast_filestream *s)
219 /* Select our owner for this stream, and get the ball rolling. */
224 static int mp3_play(struct ast_filestream *s)
226 ast_read_callback(s);
230 static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
233 if (f->frametype != AST_FRAME_VOICE) {
234 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
237 if (f->subclass != AST_FORMAT_MP3) {
238 ast_log(LOG_WARNING, "Asked to write non-mp3 frame!\n");
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));
248 static int mp3_seek(struct ast_filestream *fs, long sample_offset, int whence)
253 static int mp3_trunc(struct ast_filestream *fs)
258 static long mp3_tell(struct ast_filestream *fs)
263 static char *mp3_getcomment(struct ast_filestream *s)
270 return ast_format_register(name, exts, AST_FORMAT_MP3,
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");
296 ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
301 ast_pthread_mutex_unlock(&mp3_lock);
302 return ast_format_unregister(name);
308 if (ast_pthread_mutex_lock(&mp3_lock)) {
309 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
313 ast_pthread_mutex_unlock(&mp3_lock);
325 return ASTERISK_GPL_KEY;