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