bb25f190f25588184e4579cd098ed1aa0586cc6e
[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 <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <stdlib.h>
17 #include <sys/time.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include <string.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 #include "asterisk/endian.h"
34
35 /* Some Ideas for this code came from makeg729e.c by Jeffrey 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 AST_MUTEX_DEFINE_STATIC(g729_lock);
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, 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(&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 && (res != 10))
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 % 10) {
154                 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\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/10): %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         }
187         /* protect against seeking beyond begining. */
188         offset = (offset < min)?min:offset;
189         if (lseek(fs->fd, offset, SEEK_SET) < 0)
190                 return -1;
191         return 0;
192 }
193
194 static int g729_trunc(struct ast_filestream *fs)
195 {
196         /* Truncate file to current length */
197         if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
198                 return -1;
199         return 0;
200 }
201
202 static long g729_tell(struct ast_filestream *fs)
203 {
204         off_t offset;
205         offset = lseek(fs->fd, 0, SEEK_CUR);
206         return (offset/20)*160;
207 }
208
209 int load_module()
210 {
211         return ast_format_register(name, exts, AST_FORMAT_G729A,
212                                                                 g729_open,
213                                                                 g729_rewrite,
214                                                                 g729_write,
215                                                                 g729_seek,
216                                                                 g729_trunc,
217                                                                 g729_tell,
218                                                                 g729_read,
219                                                                 g729_close,
220                                                                 g729_getcomment);
221                                                                 
222                                                                 
223 }
224
225 int unload_module()
226 {
227         return ast_format_unregister(name);
228 }       
229
230 int usecount()
231 {
232         int res;
233         if (ast_mutex_lock(&g729_lock)) {
234                 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
235                 return -1;
236         }
237         res = glistcnt;
238         ast_mutex_unlock(&g729_lock);
239         return res;
240 }
241
242 char *description()
243 {
244         return desc;
245 }
246
247
248 char *key()
249 {
250         return ASTERISK_GPL_KEY;
251 }