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