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