2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
22 * \brief Old-style G.723.1 frame/timestamp format.
24 * \arg Extensions: g723, g723sf
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41 #include "asterisk/lock.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/file.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/sched.h"
46 #include "asterisk/module.h"
48 #include "../channels/adtranvofr.h"
51 #define G723_MAX_SIZE 1024
53 struct ast_filestream {
54 /* First entry MUST be reserved for the channel type */
55 void *reserved[AST_RESERVED_POINTERS];
56 /* This is what a filestream means to us */
57 FILE *f; /* Descriptor */
58 struct ast_filestream *next;
59 struct ast_frame *fr; /* Frame representation of buf */
60 struct timeval orig; /* Original frame time */
61 char buf[G723_MAX_SIZE + AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
65 AST_MUTEX_DEFINE_STATIC(g723_lock);
66 static int glistcnt = 0;
68 static char *name = "g723sf";
69 static char *desc = "G.723.1 Simple Timestamp File Format";
70 static char *exts = "g723|g723sf";
72 static struct ast_filestream *g723_open(FILE *f)
74 /* We don't have any header to read or anything really, but
75 if we did, it would go here. We also might want to check
76 and be sure it's a valid file. */
77 struct ast_filestream *tmp;
78 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
79 memset(tmp, 0, sizeof(struct ast_filestream));
80 if (ast_mutex_lock(&g723_lock)) {
81 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
86 tmp->fr = (struct ast_frame *)tmp->buf;
87 tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
88 tmp->fr->frametype = AST_FRAME_VOICE;
89 tmp->fr->subclass = AST_FORMAT_G723_1;
90 /* datalen will vary for each frame */
94 ast_mutex_unlock(&g723_lock);
95 ast_update_use_count();
100 static struct ast_filestream *g723_rewrite(FILE *f, const char *comment)
102 /* We don't have any header to read or anything really, but
103 if we did, it would go here. We also might want to check
104 and be sure it's a valid file. */
105 struct ast_filestream *tmp;
106 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
107 memset(tmp, 0, sizeof(struct ast_filestream));
108 if (ast_mutex_lock(&g723_lock)) {
109 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
115 ast_mutex_unlock(&g723_lock);
116 ast_update_use_count();
118 ast_log(LOG_WARNING, "Out of memory\n");
122 static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
127 /* Read the delay for the next packet, and schedule again if necessary */
128 if (fread(&delay, 1, 4, s->f) == 4)
129 delay = ntohl(delay);
132 if (fread(&size, 1, 2, s->f) != 2) {
133 /* Out of data, or the file is no longer valid. In any case
134 go ahead and stop the stream */
137 /* Looks like we have a frame to read from here */
139 if (size > G723_MAX_SIZE - sizeof(struct ast_frame)) {
140 ast_log(LOG_WARNING, "Size %d is invalid\n", size);
141 /* The file is apparently no longer any good, as we
142 shouldn't ever get frames even close to this
146 /* Read the data into the buffer */
147 s->fr->offset = AST_FRIENDLY_OFFSET;
148 s->fr->datalen = size;
149 s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
150 if ((res = fread(s->fr->data, 1, size, s->f)) != size) {
151 ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
155 /* Average out frames <= 50 ms */
159 s->fr->timelen = delay;
161 s->fr->samples = 240;
163 *whennext = s->fr->samples;
167 static void g723_close(struct ast_filestream *s)
169 if (ast_mutex_lock(&g723_lock)) {
170 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
174 ast_mutex_unlock(&g723_lock);
175 ast_update_use_count();
182 static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
188 ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
191 if (f->frametype != AST_FRAME_VOICE) {
192 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
195 if (f->subclass != AST_FORMAT_G723_1) {
196 ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
200 if (f->datalen <= 0) {
201 ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
204 if ((res = fwrite(&delay, 1, 4, fs->f)) != 4) {
205 ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
208 size = htons(f->datalen);
209 if ((res = fwrite(&size, 1, 2, fs->f)) != 2) {
210 ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
213 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
214 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
220 static int g723_seek(struct ast_filestream *fs, long sample_offset, int whence)
225 static int g723_trunc(struct ast_filestream *fs)
227 /* Truncate file to current length */
228 if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
233 static long g723_tell(struct ast_filestream *fs)
238 static char *g723_getcomment(struct ast_filestream *s)
245 return ast_format_register(name, exts, AST_FORMAT_G723_1,
261 return ast_format_unregister(name);
277 return ASTERISK_GPL_KEY;