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/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>
27 #include "../channels/adtranvofr.h"
29 #define MAX_FRAME_SIZE 1441
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];
48 static struct ast_filestream *glist = NULL;
49 static pthread_mutex_t mp3_lock = PTHREAD_MUTEX_INITIALIZER;
50 static int glistcnt = 0;
52 static char *name = "mp3";
53 static char *desc = "MPEG-1,2 Layer 3 File Format Support";
54 static char *exts = "mp3|mpeg3";
56 #include "../codecs/mp3anal.h"
58 static struct ast_filestream *mp3_open(int fd)
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");
74 tmp->lasttimeout = -1;
75 tmp->last.tv_usec = 0;
79 pthread_mutex_unlock(&mp3_lock);
80 ast_update_use_count();
85 static struct ast_filestream *mp3_rewrite(int fd, char *comment)
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");
102 pthread_mutex_unlock(&mp3_lock);
103 ast_update_use_count();
105 ast_log(LOG_WARNING, "Out of memory\n");
109 static struct ast_frame *mp3_read(struct ast_filestream *s)
114 static void mp3_close(struct ast_filestream *s)
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");
125 tmpl->next = tmp->next;
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;
140 pthread_mutex_unlock(&mp3_lock);
141 ast_update_use_count();
143 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
148 static int ast_read_callback(void *data)
150 /* XXX Don't assume frames are this size XXX */
151 u_int32_t delay = -1;
153 struct ast_filestream *s = data;
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;
162 if (mp3_badheader(s->buf)) {
163 ast_log(LOG_WARNING, "Bad mp3 header\n");
166 if ((size = mp3_framelen(s->buf)) < 0) {
167 ast_log(LOG_WARNING, "Unable to calculate frame size\n");
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;
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;
182 s->fr.datalen = size;
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);
193 s->fr.timelen = delay;
195 ast_log(LOG_DEBUG, "delay is %d, adjusting by %d, as last was %d\n", delay, s->adj, ms);
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;
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;
215 static int mp3_apply(struct ast_channel *c, struct ast_filestream *s)
217 /* Select our owner for this stream, and get the ball rolling. */
219 ast_read_callback(s);
223 static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
226 if (f->frametype != AST_FRAME_VOICE) {
227 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
230 if (f->subclass != AST_FORMAT_MP3) {
231 ast_log(LOG_WARNING, "Asked to write non-mp3 frame!\n");
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));
241 static char *mp3_getcomment(struct ast_filestream *s)
248 return ast_format_register(name, exts, AST_FORMAT_MP3,
262 struct ast_filestream *tmp, *tmpl;
263 if (pthread_mutex_lock(&mp3_lock)) {
264 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
270 ast_softhangup(tmp->owner);
275 pthread_mutex_unlock(&mp3_lock);
276 return ast_format_unregister(name);
282 if (pthread_mutex_lock(&mp3_lock)) {
283 ast_log(LOG_WARNING, "Unable to lock mp3 list\n");
287 pthread_mutex_unlock(&mp3_lock);