update MANY more files with proper copyright/license info (thanks Ian!)
[asterisk/asterisk.git] / formats / format_g723.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*
20  *
21  * Old-style G.723 frame/timestamp format.
22  * 
23  */
24  
25 #include <unistd.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <sys/time.h>
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/lock.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/file.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/sched.h"
43 #include "asterisk/module.h"
44
45 #include "../channels/adtranvofr.h"
46
47
48 #define G723_MAX_SIZE 1024
49
50 struct ast_filestream {
51         /* First entry MUST be reserved for the channel type */
52         void *reserved[AST_RESERVED_POINTERS];
53         /* This is what a filestream means to us */
54         int fd; /* Descriptor */
55         struct ast_filestream *next;
56         struct ast_frame *fr;   /* Frame representation of buf */
57         struct timeval orig;    /* Original frame time */
58         char buf[G723_MAX_SIZE + AST_FRIENDLY_OFFSET];  /* Buffer for sending frames, etc */
59 };
60
61
62 AST_MUTEX_DEFINE_STATIC(g723_lock);
63 static int glistcnt = 0;
64
65 static char *name = "g723sf";
66 static char *desc = "G.723.1 Simple Timestamp File Format";
67 static char *exts = "g723|g723sf";
68
69 static struct ast_filestream *g723_open(int fd)
70 {
71         /* We don't have any header to read or anything really, but
72            if we did, it would go here.  We also might want to check
73            and be sure it's a valid file.  */
74         struct ast_filestream *tmp;
75         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
76                 memset(tmp, 0, sizeof(struct ast_filestream));
77                 if (ast_mutex_lock(&g723_lock)) {
78                         ast_log(LOG_WARNING, "Unable to lock g723 list\n");
79                         free(tmp);
80                         return NULL;
81                 }
82                 tmp->fd = fd;
83                 tmp->fr = (struct ast_frame *)tmp->buf;
84                 tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
85                 tmp->fr->frametype = AST_FRAME_VOICE;
86                 tmp->fr->subclass = AST_FORMAT_G723_1;
87                 /* datalen will vary for each frame */
88                 tmp->fr->src = name;
89                 tmp->fr->mallocd = 0;
90                 glistcnt++;
91                 ast_mutex_unlock(&g723_lock);
92                 ast_update_use_count();
93         }
94         return tmp;
95 }
96
97 static struct ast_filestream *g723_rewrite(int fd, const char *comment)
98 {
99         /* We don't have any header to read or anything really, but
100            if we did, it would go here.  We also might want to check
101            and be sure it's a valid file.  */
102         struct ast_filestream *tmp;
103         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
104                 memset(tmp, 0, sizeof(struct ast_filestream));
105                 if (ast_mutex_lock(&g723_lock)) {
106                         ast_log(LOG_WARNING, "Unable to lock g723 list\n");
107                         free(tmp);
108                         return NULL;
109                 }
110                 tmp->fd = fd;
111                 glistcnt++;
112                 ast_mutex_unlock(&g723_lock);
113                 ast_update_use_count();
114         } else
115                 ast_log(LOG_WARNING, "Out of memory\n");
116         return tmp;
117 }
118
119 static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
120 {
121         unsigned short size;
122         int res;
123         int delay;
124         /* Read the delay for the next packet, and schedule again if necessary */
125         if (read(s->fd, &delay, 4) == 4) 
126                 delay = ntohl(delay);
127         else
128                 delay = -1;
129         if (read(s->fd, &size, 2) != 2) {
130                 /* Out of data, or the file is no longer valid.  In any case
131                    go ahead and stop the stream */
132                 return NULL;
133         }
134         /* Looks like we have a frame to read from here */
135         size = ntohs(size);
136         if (size > G723_MAX_SIZE - sizeof(struct ast_frame)) {
137                 ast_log(LOG_WARNING, "Size %d is invalid\n", size);
138                 /* The file is apparently no longer any good, as we
139                    shouldn't ever get frames even close to this 
140                    size.  */
141                 return NULL;
142         }
143         /* Read the data into the buffer */
144         s->fr->offset = AST_FRIENDLY_OFFSET;
145         s->fr->datalen = size;
146         s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
147         if ((res = read(s->fd, s->fr->data , size)) != size) {
148                 ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
149                 return NULL;
150         }
151 #if 0
152                 /* Average out frames <= 50 ms */
153                 if (delay < 50)
154                         s->fr->timelen = 30;
155                 else
156                         s->fr->timelen = delay;
157 #else
158                 s->fr->samples = 240;
159 #endif
160         *whennext = s->fr->samples;
161         return s->fr;
162 }
163
164 static void g723_close(struct ast_filestream *s)
165 {
166         if (ast_mutex_lock(&g723_lock)) {
167                 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
168                 return;
169         }
170         glistcnt--;
171         ast_mutex_unlock(&g723_lock);
172         ast_update_use_count();
173         close(s->fd);
174         free(s);
175         s = NULL;
176 }
177
178
179 static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
180 {
181         u_int32_t delay;
182         u_int16_t size;
183         int res;
184         if (fs->fr) {
185                 ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
186                 return -1;
187         }
188         if (f->frametype != AST_FRAME_VOICE) {
189                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
190                 return -1;
191         }
192         if (f->subclass != AST_FORMAT_G723_1) {
193                 ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
194                 return -1;
195         }
196         delay = 0;
197         if (f->datalen <= 0) {
198                 ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
199                 return 0;
200         }
201         if ((res = write(fs->fd, &delay, 4)) != 4) {
202                 ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
203                 return -1;
204         }
205         size = htons(f->datalen);
206         if ((res =write(fs->fd, &size, 2)) != 2) {
207                 ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
208                 return -1;
209         }
210         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
211                 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
212                 return -1;
213         }       
214         return 0;
215 }
216
217 static int g723_seek(struct ast_filestream *fs, long sample_offset, int whence)
218 {
219         return -1;
220 }
221
222 static int g723_trunc(struct ast_filestream *fs)
223 {
224         /* Truncate file to current length */
225         if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
226                 return -1;
227         return 0;
228 }
229
230 static long g723_tell(struct ast_filestream *fs)
231 {
232         return -1;
233 }
234
235 static char *g723_getcomment(struct ast_filestream *s)
236 {
237         return NULL;
238 }
239
240 int load_module()
241 {
242         return ast_format_register(name, exts, AST_FORMAT_G723_1,
243                                                                 g723_open,
244                                                                 g723_rewrite,
245                                                                 g723_write,
246                                                                 g723_seek,
247                                                                 g723_trunc,
248                                                                 g723_tell,
249                                                                 g723_read,
250                                                                 g723_close,
251                                                                 g723_getcomment);
252                                                                 
253                                                                 
254 }
255
256 int unload_module()
257 {
258         return ast_format_unregister(name);
259 }       
260
261 int usecount()
262 {
263         return glistcnt;
264 }
265
266 char *description()
267 {
268         return desc;
269 }
270
271
272 char *key()
273 {
274         return ASTERISK_GPL_KEY;
275 }