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