Format cleanups
[asterisk/asterisk.git] / formats / format_pcm_alaw.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Flat, binary, alaw PCM file format.
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 <arpa/inet.h>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <sys/times.h>
24 #include <sys/types.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <pthread.h>
30 #ifdef __linux__
31 #include <endian.h>
32 #else
33 #include <machine/endian.h>
34 #endif
35
36 #define BUF_SIZE 160            /* 160 samples */
37
38 // #define REALTIME_WRITE
39
40 struct ast_filestream {
41         void *reserved[AST_RESERVED_POINTERS];
42         /* Believe it or not, we must decode/recode to account for the
43            weird MS format */
44         /* This is what a filestream means to us */
45         int fd; /* Descriptor */
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 buf[BUF_SIZE];                            /* Output Buffer */
50 #ifdef REALTIME_WRITE
51         unsigned long start_time;
52 #endif
53 };
54
55
56 static ast_mutex_t pcm_lock = AST_MUTEX_INITIALIZER;
57 static int glistcnt = 0;
58
59 static char *name = "alaw";
60 static char *desc = "Raw aLaw 8khz PCM Audio support";
61 static char *exts = "alaw|al";
62
63
64 /* Returns time in msec since system boot. */
65 static unsigned long get_time(void)
66 {
67         struct tms buf;
68         clock_t cur;
69
70         cur = times( &buf );
71         if( cur < 0 )
72         {
73                 ast_log( LOG_WARNING, "Cannot get current time\n" );
74                 return 0;
75         }
76         return cur * 1000 / sysconf( _SC_CLK_TCK );
77 }
78
79 static struct ast_filestream *pcm_open(int fd)
80 {
81         /* We don't have any header to read or anything really, but
82            if we did, it would go here.  We also might want to check
83            and be sure it's a valid file.  */
84         struct ast_filestream *tmp;
85         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
86                 memset(tmp, 0, sizeof(struct ast_filestream));
87                 if (ast_mutex_lock(&pcm_lock)) {
88                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
89                         free(tmp);
90                         return NULL;
91                 }
92                 tmp->fd = fd;
93                 tmp->fr.data = tmp->buf;
94                 tmp->fr.frametype = AST_FRAME_VOICE;
95                 tmp->fr.subclass = AST_FORMAT_ALAW;
96                 /* datalen will vary for each frame */
97                 tmp->fr.src = name;
98                 tmp->fr.mallocd = 0;
99 #ifdef REALTIME_WRITE
100                 tmp->start_time = get_time();
101 #endif
102                 glistcnt++;
103                 ast_mutex_unlock(&pcm_lock);
104                 ast_update_use_count();
105         }
106         return tmp;
107 }
108
109 static struct ast_filestream *pcm_rewrite(int fd, char *comment)
110 {
111         /* We don't have any header to read or anything really, but
112            if we did, it would go here.  We also might want to check
113            and be sure it's a valid file.  */
114         struct ast_filestream *tmp;
115         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
116                 memset(tmp, 0, sizeof(struct ast_filestream));
117                 if (ast_mutex_lock(&pcm_lock)) {
118                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
119                         free(tmp);
120                         return NULL;
121                 }
122                 tmp->fd = fd;
123 #ifdef REALTIME_WRITE
124                 tmp->start_time = get_time();
125 #endif
126                 glistcnt++;
127                 ast_mutex_unlock(&pcm_lock);
128                 ast_update_use_count();
129         } else
130                 ast_log(LOG_WARNING, "Out of memory\n");
131         return tmp;
132 }
133
134 static void pcm_close(struct ast_filestream *s)
135 {
136         if (ast_mutex_lock(&pcm_lock)) {
137                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
138                 return;
139         }
140         glistcnt--;
141         ast_mutex_unlock(&pcm_lock);
142         ast_update_use_count();
143         close(s->fd);
144         free(s);
145         s = NULL;
146 }
147
148 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
149 {
150         int res;
151         /* Send a frame from the file to the appropriate channel */
152
153         s->fr.frametype = AST_FRAME_VOICE;
154         s->fr.subclass = AST_FORMAT_ALAW;
155         s->fr.offset = AST_FRIENDLY_OFFSET;
156         s->fr.mallocd = 0;
157         s->fr.data = s->buf;
158         if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) {
159                 if (res)
160                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
161                 return NULL;
162         }
163         s->fr.samples = res;
164         s->fr.datalen = res;
165         *whennext = s->fr.samples;
166         return &s->fr;
167 }
168
169 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
170 {
171         int res;
172 #ifdef REALTIME_WRITE
173         unsigned long cur_time;
174         unsigned long fpos;
175         struct stat stat_buf;
176 #endif
177
178         if (f->frametype != AST_FRAME_VOICE) {
179                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
180                 return -1;
181         }
182         if (f->subclass != AST_FORMAT_ALAW) {
183                 ast_log(LOG_WARNING, "Asked to write non-alaw frame (%d)!\n", f->subclass);
184                 return -1;
185         }
186
187 #ifdef REALTIME_WRITE
188         cur_time = get_time();
189         fpos = ( cur_time - fs->start_time ) * 8;       // 8 bytes per msec
190         // Check if we have written to this position yet. If we have, then increment pos by one frame
191         // for some degree of protection against receiving packets in the same clock tick.
192         fstat( fs->fd, &stat_buf );
193         if( stat_buf.st_size > fpos )
194         {
195                 fpos += f->datalen;     // Incrementing with the size of this current frame
196         }
197
198         if( stat_buf.st_size < fpos )
199         {
200                 // fill the gap with 0x55 rather than 0.
201                 char buf[ 512 ];
202                 unsigned long cur, to_write;
203
204                 cur = stat_buf.st_size;
205                 if( lseek( fs->fd, cur, SEEK_SET ) < 0 )
206                 {
207                         ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
208                         return -1;
209                 }
210                 memset( buf, 0x55, 512 );
211                 while( cur < fpos )
212                 {
213                         to_write = fpos - cur;
214                         if( to_write > 512 )
215                         {
216                                 to_write = 512;
217                         }
218                         write( fs->fd, buf, to_write );
219                         cur += to_write;
220                 }
221         }
222
223
224         if( lseek( fs->fd, fpos, SEEK_SET ) < 0 )
225         {
226                 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
227                 return -1;
228         }
229 #endif  // REALTIME_WRITE
230         
231         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
232                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
233                         return -1;
234         }
235         return 0;
236 }
237
238 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
239 {
240         off_t offset=0,min,cur,max;
241
242         min = 0;
243         cur = lseek(fs->fd, 0, SEEK_CUR);
244         max = lseek(fs->fd, 0, SEEK_END);
245         if(whence == SEEK_SET)
246                 offset = sample_offset;
247         if(whence == SEEK_CUR)
248                 offset = sample_offset + cur;
249         if(whence == SEEK_END)
250                 offset = max - sample_offset;
251         offset = (offset > max)?max:offset;
252         offset = (offset < min)?min:offset;
253         return lseek(fs->fd, offset, SEEK_SET);
254 }
255
256 static int pcm_trunc(struct ast_filestream *fs)
257 {
258         return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
259 }
260
261 static long pcm_tell(struct ast_filestream *fs)
262 {
263         off_t offset;
264         offset = lseek(fs->fd, 0, SEEK_CUR);
265         return offset;
266 }
267
268
269 static char *pcm_getcomment(struct ast_filestream *s)
270 {
271         return NULL;
272 }
273
274 int load_module()
275 {
276         return ast_format_register(name, exts, AST_FORMAT_ALAW,
277                                                                 pcm_open,
278                                                                 pcm_rewrite,
279                                                                 pcm_write,
280                                                                 pcm_seek,
281                                                                 pcm_trunc,
282                                                                 pcm_tell,
283                                                                 pcm_read,
284                                                                 pcm_close,
285                                                                 pcm_getcomment);
286 }
287
288 int unload_module()
289 {
290         return ast_format_unregister(name);
291 }       
292
293 int usecount()
294 {
295         int res;
296         if (ast_mutex_lock(&pcm_lock)) {
297                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
298                 return -1;
299         }
300         res = glistcnt;
301         ast_mutex_unlock(&pcm_lock);
302         return res;
303 }
304
305 char *description()
306 {
307         return desc;
308 }
309
310
311 char *key()
312 {
313         return ASTERISK_GPL_KEY;
314 }