2
* Asterisk -- A telephony toolkit for Linux.
4
* Old-style G.723 frame/timestamp format.
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"
30
#define G723_MAX_SIZE 1024
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
u_int32_t lasttimeout; /* Last amount of timeout */
38
struct ast_channel *owner;
39
struct ast_filestream *next;
40
struct ast_frame *fr; /* Frame representation of buf */
41
struct timeval orig; /* Original frame time */
42
char buf[G723_MAX_SIZE + AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
46
static struct ast_filestream *glist = NULL;
47
static pthread_mutex_t g723_lock = PTHREAD_MUTEX_INITIALIZER;
48
static int glistcnt = 0;
50
static char *name = "g723sf";
51
static char *desc = "G.723.1 Simple Timestamp File Format";
52
static char *exts = "g723";
54
static struct ast_filestream *g723_open(int fd)
56
/* We don't have any header to read or anything really, but
57
if we did, it would go here. We also might want to check
58
and be sure it's a valid file. */
59
struct ast_filestream *tmp;
60
if ((tmp = malloc(sizeof(struct ast_filestream)))) {
61
if (ast_pthread_mutex_lock(&g723_lock)) {
62
ast_log(LOG_WARNING, "Unable to lock g723 list\n");
70
tmp->fr = (struct ast_frame *)tmp->buf;
71
tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
72
tmp->fr->frametype = AST_FRAME_VOICE;
73
tmp->fr->subclass = AST_FORMAT_G723_1;
74
/* datalen will vary for each frame */
77
tmp->lasttimeout = -1;
78
tmp->orig.tv_usec = 0;
81
ast_pthread_mutex_unlock(&g723_lock);
82
ast_update_use_count();
87
static struct ast_filestream *g723_rewrite(int fd, char *comment)
89
/* We don't have any header to read or anything really, but
90
if we did, it would go here. We also might want to check
91
and be sure it's a valid file. */
92
struct ast_filestream *tmp;
93
if ((tmp = malloc(sizeof(struct ast_filestream)))) {
94
if (ast_pthread_mutex_lock(&g723_lock)) {
95
ast_log(LOG_WARNING, "Unable to lock g723 list\n");
104
tmp->lasttimeout = -1;
105
tmp->orig.tv_usec = 0;
106
tmp->orig.tv_sec = 0;
108
ast_pthread_mutex_unlock(&g723_lock);
109
ast_update_use_count();
111
ast_log(LOG_WARNING, "Out of memory\n");
115
static struct ast_frame *g723_read(struct ast_filestream *s)
120
static void g723_close(struct ast_filestream *s)
122
struct ast_filestream *tmp, *tmpl = NULL;
123
if (ast_pthread_mutex_lock(&g723_lock)) {
124
ast_log(LOG_WARNING, "Unable to lock g723 list\n");
131
tmpl->next = tmp->next;
141
s->owner->stream = NULL;
142
if (s->owner->streamid > -1)
143
ast_sched_del(s->owner->sched, s->owner->streamid);
144
s->owner->streamid = -1;
146
ast_pthread_mutex_unlock(&g723_lock);
147
ast_update_use_count();
149
ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
154
static int ast_read_callback(void *data)
157
u_int32_t delay = -1;
161
struct ast_filestream *s = data;
162
/* Send a frame from the file to the appropriate channel */
164
if (read(s->fd, &size, 2) != 2) {
165
/* Out of data, or the file is no longer valid. In any case
166
go ahead and stop the stream */
167
s->owner->streamid = -1;
170
/* Looks like we have a frame to read from here */
172
if (size > G723_MAX_SIZE - sizeof(struct ast_frame)) {
173
ast_log(LOG_WARNING, "Size %d is invalid\n", size);
174
/* The file is apparently no longer any good, as we
175
shouldn't ever get frames even close to this
177
s->owner->streamid = -1;
180
/* Read the data into the buffer */
181
s->fr->offset = AST_FRIENDLY_OFFSET;
182
s->fr->datalen = size;
183
s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
184
if ((res = read(s->fd, s->fr->data , size)) != size) {
185
ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
186
s->owner->streamid = -1;
189
/* Read the delay for the next packet, and schedule again if necessary */
190
if (read(s->fd, &delay, 4) == 4)
191
delay = ntohl(delay);
195
/* Average out frames <= 50 ms */
199
s->fr->timelen = delay;
203
/* Unless there is no delay, we're going to exit out as soon as we
204
have processed the current frame. */
205
if (delay > VOFR_FUDGE) {
207
/* If there is a delay, lets schedule the next event */
208
if (delay != s->lasttimeout) {
209
/* We'll install the next timeout now. */
210
s->owner->streamid = ast_sched_add(s->owner->sched,
212
ast_read_callback, s);
214
s->lasttimeout = delay;
216
/* Just come back again at the same time */
219
/* Lastly, process the frame */
220
if (ast_write(s->owner, s->fr)) {
221
ast_log(LOG_WARNING, "Failed to write frame\n");
222
s->owner->streamid = -1;
229
static int g723_apply(struct ast_channel *c, struct ast_filestream *s)
232
/* Select our owner for this stream, and get the ball rolling. */
234
/* Read and ignore the first delay */
235
if (read(s->fd, &delay, 4) != 4) {
239
ast_read_callback(s);
243
static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
250
ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
253
if (f->frametype != AST_FRAME_VOICE) {
254
ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
257
if (f->subclass != AST_FORMAT_G723_1) {
258
ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
261
if (!(fs->orig.tv_usec || fs->orig.tv_sec)) {
262
/* First frame should have zeros for delay */
264
if (gettimeofday(&fs->orig, NULL)) {
265
ast_log(LOG_WARNING, "gettimeofday() failed?? What is this? Y2k?\n");
269
if (gettimeofday(&now, NULL)) {
270
ast_log(LOG_WARNING, "gettimeofday() failed?? What is this? Y2k?\n");
273
delay = (now.tv_sec - fs->orig.tv_sec) * 1000 + (now.tv_usec - fs->orig.tv_usec) / 1000;
274
delay = htonl(delay);
275
fs->orig.tv_sec = now.tv_sec;
276
fs->orig.tv_usec = now.tv_usec;
278
if (f->datalen <= 0) {
279
ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
282
if ((res = write(fs->fd, &delay, 4)) != 4) {
283
ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
286
size = htons(f->datalen);
287
if ((res =write(fs->fd, &size, 2)) != 2) {
288
ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
291
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
292
ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
298
static char *g723_getcomment(struct ast_filestream *s)
305
return ast_format_register(name, exts, AST_FORMAT_G723_1,
319
struct ast_filestream *tmp, *tmpl;
320
if (ast_pthread_mutex_lock(&g723_lock)) {
321
ast_log(LOG_WARNING, "Unable to lock g723 list\n");
327
ast_softhangup(tmp->owner);
332
ast_pthread_mutex_unlock(&g723_lock);
333
return ast_format_unregister(name);
339
if (ast_pthread_mutex_lock(&g723_lock)) {
340
ast_log(LOG_WARNING, "Unable to lock g723 list\n");
344
ast_pthread_mutex_unlock(&g723_lock);
356
return ASTERISK_GPL_KEY;