Make sure RTP timestamps are accurate on playback
[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 <unistd.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <stdlib.h>
18 #include <sys/time.h>
19 #include <stdio.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 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, const 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         s->fr.delivery.tv_sec = 0;
162         s->fr.delivery.tv_usec = 0;
163         if ((res = read(s->fd, &ts, sizeof(ts))) == sizeof(ts)) {
164                 s->lastts = ntohl(ts);
165                 *whennext = s->lastts * 4/45;
166         } else
167                 *whennext = 0;
168         return &s->fr;
169 }
170
171 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
172 {
173         int res;
174         unsigned int ts;
175         unsigned short len;
176         int subclass;
177         int mark=0;
178         if (f->frametype != AST_FRAME_VIDEO) {
179                 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
180                 return -1;
181         }
182         subclass = f->subclass;
183         if (subclass & 0x1)
184                 mark=0x8000;
185         subclass &= ~0x1;
186         if (subclass != AST_FORMAT_H263) {
187                 ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
188                 return -1;
189         }
190         ts = htonl(f->samples);
191         if ((res = write(fs->fd, &ts, sizeof(ts))) != sizeof(ts)) {
192                         ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
193                         return -1;
194         }
195         len = htons(f->datalen | mark);
196         if ((res = write(fs->fd, &len, sizeof(len))) != sizeof(len)) {
197                         ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
198                         return -1;
199         }
200         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
201                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
202                         return -1;
203         }
204         return 0;
205 }
206
207 static char *h263_getcomment(struct ast_filestream *s)
208 {
209         return NULL;
210 }
211
212 static int h263_seek(struct ast_filestream *fs, long sample_offset, int whence)
213 {
214         /* No way Jose */
215         return -1;
216 }
217
218 static int h263_trunc(struct ast_filestream *fs)
219 {
220         /* Truncate file to current length */
221         if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
222                 return -1;
223         return 0;
224 }
225
226 static long h263_tell(struct ast_filestream *fs)
227 {
228         off_t offset;
229         offset = lseek(fs->fd, 0, SEEK_CUR);
230         return (offset/20)*160;
231 }
232
233 int load_module()
234 {
235         return ast_format_register(name, exts, AST_FORMAT_H263,
236                                                                 h263_open,
237                                                                 h263_rewrite,
238                                                                 h263_write,
239                                                                 h263_seek,
240                                                                 h263_trunc,
241                                                                 h263_tell,
242                                                                 h263_read,
243                                                                 h263_close,
244                                                                 h263_getcomment);
245                                                                 
246                                                                 
247 }
248
249 int unload_module()
250 {
251         return ast_format_unregister(name);
252 }       
253
254 int usecount()
255 {
256         return glistcnt;
257 }
258
259 char *description()
260 {
261         return desc;
262 }
263
264
265 char *key()
266 {
267         return ASTERISK_GPL_KEY;
268 }