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