BSD portability enhancements (bug #234)
[asterisk/asterisk.git] / formats / format_g729.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Save to raw, headerless G729 data.
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 <asterisk/lock.h>
15 #include <asterisk/channel.h>
16 #include <asterisk/file.h>
17 #include <asterisk/logger.h>
18 #include <asterisk/sched.h>
19 #include <asterisk/module.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <pthread.h>
29 #ifdef __linux__
30 #include <endian.h>
31 #else
32 #include <machine/endian.h>
33 #endif
34
35 /* Some Ideas for this code came from makeg729e.c by Jeffery Chilton */
36
37 /* Portions of the conversion code are by guido@sienanet.it */
38
39 struct ast_filestream {
40         void *reserved[AST_RESERVED_POINTERS];
41         /* Believe it or not, we must decode/recode to account for the
42            weird MS format */
43         /* This is what a filestream means to us */
44         int fd; /* Descriptor */
45         struct ast_frame fr;                            /* Frame information */
46         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
47         char empty;                                                     /* Empty character */
48         unsigned char g729[20];                         /* Two Real G729 Frames */
49 };
50
51
52 static ast_mutex_t g729_lock = AST_MUTEX_INITIALIZER;
53 static int glistcnt = 0;
54
55 static char *name = "g729";
56 static char *desc = "Raw G729 data";
57 static char *exts = "g729";
58
59 static struct ast_filestream *g729_open(int fd)
60 {
61         /* We don't have any header to read or anything really, but
62            if we did, it would go here.  We also might want to check
63            and be sure it's a valid file.  */
64         struct ast_filestream *tmp;
65         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
66                 memset(tmp, 0, sizeof(struct ast_filestream));
67                 if (ast_mutex_lock(&g729_lock)) {
68                         ast_log(LOG_WARNING, "Unable to lock g729 list\n");
69                         free(tmp);
70                         return NULL;
71                 }
72                 tmp->fd = fd;
73                 tmp->fr.data = tmp->g729;
74                 tmp->fr.frametype = AST_FRAME_VOICE;
75                 tmp->fr.subclass = AST_FORMAT_G729A;
76                 /* datalen will vary for each frame */
77                 tmp->fr.src = name;
78                 tmp->fr.mallocd = 0;
79                 glistcnt++;
80                 ast_mutex_unlock(&g729_lock);
81                 ast_update_use_count();
82         }
83         return tmp;
84 }
85
86 static struct ast_filestream *g729_rewrite(int fd, 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(&g729_lock)) {
95                         ast_log(LOG_WARNING, "Unable to lock g729 list\n");
96                         free(tmp);
97                         return NULL;
98                 }
99                 tmp->fd = fd;
100                 glistcnt++;
101                 ast_mutex_unlock(&g729_lock);
102                 ast_update_use_count();
103         } else
104                 ast_log(LOG_WARNING, "Out of memory\n");
105         return tmp;
106 }
107
108 static void g729_close(struct ast_filestream *s)
109 {
110         if (ast_mutex_lock(&g729_lock)) {
111                 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
112                 return;
113         }
114         glistcnt--;
115         ast_mutex_unlock(&g729_lock);
116         ast_update_use_count();
117         close(s->fd);
118         free(s);
119         s = NULL;
120 }
121
122 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
123 {
124         int res;
125         /* Send a frame from the file to the appropriate channel */
126         s->fr.frametype = AST_FRAME_VOICE;
127         s->fr.subclass = AST_FORMAT_G729A;
128         s->fr.offset = AST_FRIENDLY_OFFSET;
129         s->fr.samples = 160;
130         s->fr.datalen = 20;
131         s->fr.mallocd = 0;
132         s->fr.data = s->g729;
133         if ((res = read(s->fd, s->g729, 20)) != 20) {
134                 if (res)
135                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
136                 return NULL;
137         }
138         *whennext = s->fr.samples;
139         return &s->fr;
140 }
141
142 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
143 {
144         int res;
145         if (f->frametype != AST_FRAME_VOICE) {
146                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
147                 return -1;
148         }
149         if (f->subclass != AST_FORMAT_G729A) {
150                 ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
151                 return -1;
152         }
153         if (f->datalen % 20) {
154                 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 20\n", f->datalen);
155                 return -1;
156         }
157         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
158                         ast_log(LOG_WARNING, "Bad write (%d/20): %s\n", res, strerror(errno));
159                         return -1;
160         }
161         return 0;
162 }
163
164 static char *g729_getcomment(struct ast_filestream *s)
165 {
166         return NULL;
167 }
168
169 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
170 {
171         long bytes;
172         off_t min,cur,max,offset=0;
173         min = 0;
174         cur = lseek(fs->fd, 0, SEEK_CUR);
175         max = lseek(fs->fd, 0, SEEK_END);
176         
177         bytes = 20 * (sample_offset / 160);
178         if (whence == SEEK_SET)
179                 offset = bytes;
180         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
181                 offset = cur + bytes;
182         else if (whence == SEEK_END)
183                 offset = max - bytes;
184         if (whence != SEEK_FORCECUR) {
185                 offset = (offset > max)?max:offset;
186                 offset = (offset < min)?min:offset;
187         }
188         if (lseek(fs->fd, offset, SEEK_SET) < 0)
189                 return -1;
190         return 0;
191 }
192
193 static int g729_trunc(struct ast_filestream *fs)
194 {
195         /* Truncate file to current length */
196         if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
197                 return -1;
198         return 0;
199 }
200
201 static long g729_tell(struct ast_filestream *fs)
202 {
203         off_t offset;
204         offset = lseek(fs->fd, 0, SEEK_CUR);
205         return (offset/20)*160;
206 }
207
208 int load_module()
209 {
210         return ast_format_register(name, exts, AST_FORMAT_G729A,
211                                                                 g729_open,
212                                                                 g729_rewrite,
213                                                                 g729_write,
214                                                                 g729_seek,
215                                                                 g729_trunc,
216                                                                 g729_tell,
217                                                                 g729_read,
218                                                                 g729_close,
219                                                                 g729_getcomment);
220                                                                 
221                                                                 
222 }
223
224 int unload_module()
225 {
226         return ast_format_unregister(name);
227 }       
228
229 int usecount()
230 {
231         int res;
232         if (ast_mutex_lock(&g729_lock)) {
233                 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
234                 return -1;
235         }
236         res = glistcnt;
237         ast_mutex_unlock(&g729_lock);
238         return res;
239 }
240
241 char *description()
242 {
243         return desc;
244 }
245
246
247 char *key()
248 {
249         return ASTERISK_GPL_KEY;
250 }