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