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