Remove pthread.h from source. We should be using asterisk/lock.h everywhere instead...
[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 #ifdef __linux__
29 #include <endian.h>
30 #else
31 #include <machine/endian.h>
32 #endif
33
34 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
35
36 /* Portions of the conversion code are by guido@sienanet.it */
37
38 struct ast_filestream {
39         void *reserved[AST_RESERVED_POINTERS];
40         /* Believe it or not, we must decode/recode to account for the
41            weird MS format */
42         /* This is what a filestream means to us */
43         int fd; /* Descriptor */
44         struct ast_frame fr;                            /* Frame information */
45         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
46         char empty;                                                     /* Empty character */
47         unsigned char g729[20];                         /* Two Real G729 Frames */
48 };
49
50
51 AST_MUTEX_DEFINE_STATIC(g729_lock);
52 static int glistcnt = 0;
53
54 static char *name = "g729";
55 static char *desc = "Raw G729 data";
56 static char *exts = "g729";
57
58 static struct ast_filestream *g729_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(&g729_lock)) {
67                         ast_log(LOG_WARNING, "Unable to lock g729 list\n");
68                         free(tmp);
69                         return NULL;
70                 }
71                 tmp->fd = fd;
72                 tmp->fr.data = tmp->g729;
73                 tmp->fr.frametype = AST_FRAME_VOICE;
74                 tmp->fr.subclass = AST_FORMAT_G729A;
75                 /* datalen will vary for each frame */
76                 tmp->fr.src = name;
77                 tmp->fr.mallocd = 0;
78                 glistcnt++;
79                 ast_mutex_unlock(&g729_lock);
80                 ast_update_use_count();
81         }
82         return tmp;
83 }
84
85 static struct ast_filestream *g729_rewrite(int fd, 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(&g729_lock)) {
94                         ast_log(LOG_WARNING, "Unable to lock g729 list\n");
95                         free(tmp);
96                         return NULL;
97                 }
98                 tmp->fd = fd;
99                 glistcnt++;
100                 ast_mutex_unlock(&g729_lock);
101                 ast_update_use_count();
102         } else
103                 ast_log(LOG_WARNING, "Out of memory\n");
104         return tmp;
105 }
106
107 static void g729_close(struct ast_filestream *s)
108 {
109         if (ast_mutex_lock(&g729_lock)) {
110                 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
111                 return;
112         }
113         glistcnt--;
114         ast_mutex_unlock(&g729_lock);
115         ast_update_use_count();
116         close(s->fd);
117         free(s);
118         s = NULL;
119 }
120
121 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
122 {
123         int res;
124         /* Send a frame from the file to the appropriate channel */
125         s->fr.frametype = AST_FRAME_VOICE;
126         s->fr.subclass = AST_FORMAT_G729A;
127         s->fr.offset = AST_FRIENDLY_OFFSET;
128         s->fr.samples = 160;
129         s->fr.datalen = 20;
130         s->fr.mallocd = 0;
131         s->fr.data = s->g729;
132         if ((res = read(s->fd, s->g729, 20)) != 20) {
133                 if (res)
134                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
135                 return NULL;
136         }
137         *whennext = s->fr.samples;
138         return &s->fr;
139 }
140
141 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
142 {
143         int res;
144         if (f->frametype != AST_FRAME_VOICE) {
145                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
146                 return -1;
147         }
148         if (f->subclass != AST_FORMAT_G729A) {
149                 ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
150                 return -1;
151         }
152         if (f->datalen % 10) {
153                 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
154                 return -1;
155         }
156         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
157                         ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
158                         return -1;
159         }
160         return 0;
161 }
162
163 static char *g729_getcomment(struct ast_filestream *s)
164 {
165         return NULL;
166 }
167
168 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
169 {
170         long bytes;
171         off_t min,cur,max,offset=0;
172         min = 0;
173         cur = lseek(fs->fd, 0, SEEK_CUR);
174         max = lseek(fs->fd, 0, SEEK_END);
175         
176         bytes = 20 * (sample_offset / 160);
177         if (whence == SEEK_SET)
178                 offset = bytes;
179         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
180                 offset = cur + bytes;
181         else if (whence == SEEK_END)
182                 offset = max - bytes;
183         if (whence != SEEK_FORCECUR) {
184                 offset = (offset > max)?max:offset;
185         }
186         // protect against seeking beyond begining.
187         offset = (offset < min)?min:offset;
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 }