Merge FreeBSD locking fixes (bug #1411)
[asterisk/asterisk.git] / formats / format_h263.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Save to raw, headerless h263 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 makeh263e.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         unsigned int lastts;
46         struct ast_frame fr;                            /* Frame information */
47         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
48         char empty;                                                     /* Empty character */
49         unsigned char h263[4096];                               /* Two Real h263 Frames */
50 };
51
52
53 AST_MUTEX_DEFINE_STATIC(h263_lock);
54 static int glistcnt = 0;
55
56 static char *name = "h263";
57 static char *desc = "Raw h263 data";
58 static char *exts = "h263";
59
60 static struct ast_filestream *h263_open(int fd)
61 {
62         /* We don't have any header to read or anything really, but
63            if we did, it would go here.  We also might want to check
64            and be sure it's a valid file.  */
65         struct ast_filestream *tmp;
66         unsigned int ts;
67         int res;
68         if ((res = read(fd, &ts, sizeof(ts))) < sizeof(ts)) {
69                 ast_log(LOG_WARNING, "Empty file!\n");
70                 return NULL;
71         }
72                 
73         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
74                 memset(tmp, 0, sizeof(struct ast_filestream));
75                 if (ast_mutex_lock(&h263_lock)) {
76                         ast_log(LOG_WARNING, "Unable to lock h263 list\n");
77                         free(tmp);
78                         return NULL;
79                 }
80                 tmp->fd = fd;
81                 tmp->fr.data = tmp->h263;
82                 tmp->fr.frametype = AST_FRAME_VIDEO;
83                 tmp->fr.subclass = AST_FORMAT_H263;
84                 /* datalen will vary for each frame */
85                 tmp->fr.src = name;
86                 tmp->fr.mallocd = 0;
87                 glistcnt++;
88                 ast_mutex_unlock(&h263_lock);
89                 ast_update_use_count();
90         }
91         return tmp;
92 }
93
94 static struct ast_filestream *h263_rewrite(int fd, char *comment)
95 {
96         /* We don't have any header to read or anything really, but
97            if we did, it would go here.  We also might want to check
98            and be sure it's a valid file.  */
99         struct ast_filestream *tmp;
100         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
101                 memset(tmp, 0, sizeof(struct ast_filestream));
102                 if (ast_mutex_lock(&h263_lock)) {
103                         ast_log(LOG_WARNING, "Unable to lock h263 list\n");
104                         free(tmp);
105                         return NULL;
106                 }
107                 tmp->fd = fd;
108                 glistcnt++;
109                 ast_mutex_unlock(&h263_lock);
110                 ast_update_use_count();
111         } else
112                 ast_log(LOG_WARNING, "Out of memory\n");
113         return tmp;
114 }
115
116 static void h263_close(struct ast_filestream *s)
117 {
118         if (ast_mutex_lock(&h263_lock)) {
119                 ast_log(LOG_WARNING, "Unable to lock h263 list\n");
120                 return;
121         }
122         glistcnt--;
123         ast_mutex_unlock(&h263_lock);
124         ast_update_use_count();
125         close(s->fd);
126         free(s);
127         s = NULL;
128 }
129
130 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
131 {
132         int res;
133         int mark=0;
134         unsigned short len;
135         unsigned int ts;
136         /* Send a frame from the file to the appropriate channel */
137         s->fr.frametype = AST_FRAME_VIDEO;
138         s->fr.subclass = AST_FORMAT_H263;
139         s->fr.offset = AST_FRIENDLY_OFFSET;
140         s->fr.mallocd = 0;
141         s->fr.data = s->h263;
142         if ((res = read(s->fd, &len, sizeof(len))) < 1) {
143                 return NULL;
144         }
145         len = ntohs(len);
146         if (len & 0x8000) {
147                 mark = 1;
148         }
149         len &= 0x7fff;
150         if (len > sizeof(s->h263)) {
151                 ast_log(LOG_WARNING, "Length %d is too long\n", len);
152         }
153         if ((res = read(s->fd, s->h263, len)) != len) {
154                 if (res)
155                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
156                 return NULL;
157         }
158         s->fr.samples = s->lastts;
159         s->fr.datalen = len;
160         s->fr.subclass |= mark;
161         if ((res = read(s->fd, &ts, sizeof(ts))) == sizeof(ts)) {
162                 s->lastts = *whennext = ntohl(ts) * 4/45;
163         } else
164                 *whennext = 0;
165         return &s->fr;
166 }
167
168 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
169 {
170         int res;
171         unsigned int ts;
172         unsigned short len;
173         int subclass;
174         int mark=0;
175         if (f->frametype != AST_FRAME_VIDEO) {
176                 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
177                 return -1;
178         }
179         subclass = f->subclass;
180         if (subclass & 0x1)
181                 mark=0x8000;
182         subclass &= ~0x1;
183         if (subclass != AST_FORMAT_H263) {
184                 ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
185                 return -1;
186         }
187         ts = htonl(f->samples);
188         if ((res = write(fs->fd, &ts, sizeof(ts))) != sizeof(ts)) {
189                         ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
190                         return -1;
191         }
192         len = htons(f->datalen | mark);
193         if ((res = write(fs->fd, &len, sizeof(len))) != sizeof(len)) {
194                         ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
195                         return -1;
196         }
197         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
198                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
199                         return -1;
200         }
201         return 0;
202 }
203
204 static char *h263_getcomment(struct ast_filestream *s)
205 {
206         return NULL;
207 }
208
209 static int h263_seek(struct ast_filestream *fs, long sample_offset, int whence)
210 {
211         /* No way Jose */
212         return -1;
213 }
214
215 static int h263_trunc(struct ast_filestream *fs)
216 {
217         /* Truncate file to current length */
218         if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
219                 return -1;
220         return 0;
221 }
222
223 static long h263_tell(struct ast_filestream *fs)
224 {
225         off_t offset;
226         offset = lseek(fs->fd, 0, SEEK_CUR);
227         return (offset/20)*160;
228 }
229
230 int load_module()
231 {
232         return ast_format_register(name, exts, AST_FORMAT_H263,
233                                                                 h263_open,
234                                                                 h263_rewrite,
235                                                                 h263_write,
236                                                                 h263_seek,
237                                                                 h263_trunc,
238                                                                 h263_tell,
239                                                                 h263_read,
240                                                                 h263_close,
241                                                                 h263_getcomment);
242                                                                 
243                                                                 
244 }
245
246 int unload_module()
247 {
248         return ast_format_unregister(name);
249 }       
250
251 int usecount()
252 {
253         int res;
254         if (ast_mutex_lock(&h263_lock)) {
255                 ast_log(LOG_WARNING, "Unable to lock h263 list\n");
256                 return -1;
257         }
258         res = glistcnt;
259         ast_mutex_unlock(&h263_lock);
260         return res;
261 }
262
263 char *description()
264 {
265         return desc;
266 }
267
268
269 char *key()
270 {
271         return ASTERISK_GPL_KEY;
272 }