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