Elminate unused code/warnings (bug #934)
[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 #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, 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         fstat( fs->fd, &stat_buf );
196         if( stat_buf.st_size > fpos )
197         {
198                 fpos += f->datalen;     // Incrementing with the size of this current frame
199         }
200
201         if( stat_buf.st_size < fpos )
202         {
203                 // fill the gap with 0x55 rather than 0.
204                 char buf[ 512 ];
205                 unsigned long cur, to_write;
206
207                 cur = stat_buf.st_size;
208                 if( lseek( fs->fd, cur, SEEK_SET ) < 0 )
209                 {
210                         ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
211                         return -1;
212                 }
213                 memset( buf, 0x55, 512 );
214                 while( cur < fpos )
215                 {
216                         to_write = fpos - cur;
217                         if( to_write > 512 )
218                         {
219                                 to_write = 512;
220                         }
221                         write( fs->fd, buf, to_write );
222                         cur += to_write;
223                 }
224         }
225
226
227         if( lseek( fs->fd, fpos, SEEK_SET ) < 0 )
228         {
229                 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
230                 return -1;
231         }
232 #endif  // REALTIME_WRITE
233         
234         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
235                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
236                         return -1;
237         }
238         return 0;
239 }
240
241 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
242 {
243         off_t offset=0,min,cur,max;
244
245         min = 0;
246         cur = lseek(fs->fd, 0, SEEK_CUR);
247         max = lseek(fs->fd, 0, SEEK_END);
248         if (whence == SEEK_SET)
249                 offset = sample_offset;
250         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
251                 offset = sample_offset + cur;
252         else if (whence == SEEK_END)
253                 offset = max - sample_offset;
254         if (whence != SEEK_FORCECUR) {
255                 offset = (offset > max)?max:offset;
256                 offset = (offset < min)?min:offset;
257         }
258         return lseek(fs->fd, offset, SEEK_SET);
259 }
260
261 static int pcm_trunc(struct ast_filestream *fs)
262 {
263         return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
264 }
265
266 static long pcm_tell(struct ast_filestream *fs)
267 {
268         off_t offset;
269         offset = lseek(fs->fd, 0, SEEK_CUR);
270         return offset;
271 }
272
273
274 static char *pcm_getcomment(struct ast_filestream *s)
275 {
276         return NULL;
277 }
278
279 int load_module()
280 {
281         return ast_format_register(name, exts, AST_FORMAT_ALAW,
282                                                                 pcm_open,
283                                                                 pcm_rewrite,
284                                                                 pcm_write,
285                                                                 pcm_seek,
286                                                                 pcm_trunc,
287                                                                 pcm_tell,
288                                                                 pcm_read,
289                                                                 pcm_close,
290                                                                 pcm_getcomment);
291 }
292
293 int unload_module()
294 {
295         return ast_format_unregister(name);
296 }       
297
298 int usecount()
299 {
300         int res;
301         if (ast_mutex_lock(&pcm_lock)) {
302                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
303                 return -1;
304         }
305         res = glistcnt;
306         ast_mutex_unlock(&pcm_lock);
307         return res;
308 }
309
310 char *description()
311 {
312         return desc;
313 }
314
315
316 char *key()
317 {
318         return ASTERISK_GPL_KEY;
319 }