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