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