silence some more compiler warnings
[asterisk/asterisk.git] / formats / format_pcm.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, 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, ulaw PCM file format.
22  * \arg File name extension: pcm, ulaw, ul, mu
23  * 
24  * \ingroup formats
25  */
26  
27 #include <unistd.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <stdlib.h>
31 #include <sys/time.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 #include "asterisk/ulaw.h"
48
49 #define BUF_SIZE 160            /* 160 samples */
50
51 struct ast_filestream {
52         void *reserved[AST_RESERVED_POINTERS];
53         /* This is what a filestream means to us */
54         FILE *f; /* Descriptor */
55         struct ast_channel *owner;
56         struct ast_frame fr;                            /* Frame information */
57         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
58         char empty;                                                     /* Empty character */
59         unsigned char buf[BUF_SIZE];                            /* Output Buffer */
60         struct timeval last;
61 };
62
63
64 AST_MUTEX_DEFINE_STATIC(pcm_lock);
65 static int glistcnt = 0;
66
67 static char *name = "pcm";
68 static char *desc = "Raw uLaw 8khz Audio support (PCM)";
69 static char *exts = "pcm|ulaw|ul|mu";
70
71 static char ulaw_silence[BUF_SIZE];
72
73 static struct ast_filestream *pcm_open(FILE *f)
74 {
75         /* We don't have any header to read or anything really, but
76            if we did, it would go here.  We also might want to check
77            and be sure it's a valid file.  */
78         struct ast_filestream *tmp;
79         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
80                 memset(tmp, 0, sizeof(struct ast_filestream));
81                 if (ast_mutex_lock(&pcm_lock)) {
82                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
83                         free(tmp);
84                         return NULL;
85                 }
86                 tmp->f = f;
87                 tmp->fr.data = tmp->buf;
88                 tmp->fr.frametype = AST_FRAME_VOICE;
89                 tmp->fr.subclass = AST_FORMAT_ULAW;
90                 /* datalen will vary for each frame */
91                 tmp->fr.src = name;
92                 tmp->fr.mallocd = 0;
93                 glistcnt++;
94                 ast_mutex_unlock(&pcm_lock);
95                 ast_update_use_count();
96         }
97         return tmp;
98 }
99
100 static struct ast_filestream *pcm_rewrite(FILE *f, const char *comment)
101 {
102         /* We don't have any header to read or anything really, but
103            if we did, it would go here.  We also might want to check
104            and be sure it's a valid file.  */
105         struct ast_filestream *tmp;
106         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
107                 memset(tmp, 0, sizeof(struct ast_filestream));
108                 if (ast_mutex_lock(&pcm_lock)) {
109                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
110                         free(tmp);
111                         return NULL;
112                 }
113                 tmp->f = f;
114                 glistcnt++;
115                 ast_mutex_unlock(&pcm_lock);
116                 ast_update_use_count();
117         } else
118                 ast_log(LOG_WARNING, "Out of memory\n");
119         return tmp;
120 }
121
122 static void pcm_close(struct ast_filestream *s)
123 {
124         if (ast_mutex_lock(&pcm_lock)) {
125                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
126                 return;
127         }
128         glistcnt--;
129         ast_mutex_unlock(&pcm_lock);
130         ast_update_use_count();
131         fclose(s->f);
132         free(s);
133         s = NULL;
134 }
135
136 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
137 {
138         int res;
139         int delay;
140         /* Send a frame from the file to the appropriate channel */
141
142         s->fr.frametype = AST_FRAME_VOICE;
143         s->fr.subclass = AST_FORMAT_ULAW;
144         s->fr.offset = AST_FRIENDLY_OFFSET;
145         s->fr.mallocd = 0;
146         s->fr.data = s->buf;
147         if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
148                 if (res)
149                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
150                 return NULL;
151         }
152         s->fr.samples = res;
153         s->fr.datalen = res;
154         delay = s->fr.samples;
155         *whennext = delay;
156         return &s->fr;
157 }
158
159 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
160 {
161         int res;
162         if (f->frametype != AST_FRAME_VOICE) {
163                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
164                 return -1;
165         }
166         if (f->subclass != AST_FORMAT_ULAW) {
167                 ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
168                 return -1;
169         }
170         if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
171                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
172                         return -1;
173         }
174         return 0;
175 }
176
177 static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
178 {
179         off_t cur, max, offset = 0;
180         int ret = -1;   /* assume error */
181
182         cur = ftello(fs->f);
183         fseeko(fs->f, 0, SEEK_END);
184         max = ftello(fs->f);
185
186         switch (whence) {
187         case SEEK_SET:
188                 offset = sample_offset;
189                 break;
190         case SEEK_END:
191                 offset = max - sample_offset;
192                 break;
193         case SEEK_CUR:
194         case SEEK_FORCECUR:
195                 offset = cur + sample_offset;
196                 break;
197         default:
198                 ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
199                 offset = sample_offset;
200         }
201         if (offset < 0) {
202                 ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset);
203                 offset = 0;
204         }
205         if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */
206                 size_t left = offset - max;
207
208                 while (left) {
209                         size_t written = fwrite(ulaw_silence, sizeof(ulaw_silence[0]),
210                                      (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
211                         if (written == -1)
212                                 break;  /* error */
213                         left -= written * sizeof(ulaw_silence[0]);
214                 }
215                 ret = 0; /* successful */
216         } else {
217                 if (offset > max) {
218                         ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
219                         offset = max;
220                 }
221                 ret = fseeko(fs->f, offset, SEEK_SET);
222         }
223         return ret;
224 }
225
226 static int pcm_trunc(struct ast_filestream *fs)
227 {
228         return ftruncate(fileno(fs->f), ftello(fs->f));
229 }
230
231 static off_t pcm_tell(struct ast_filestream *fs)
232 {
233         off_t offset;
234         offset = ftello(fs->f);
235         return offset;
236 }
237
238 static char *pcm_getcomment(struct ast_filestream *s)
239 {
240         return NULL;
241 }
242
243 int load_module()
244 {
245         int index;
246
247         for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++)
248                 ulaw_silence[index] = AST_LIN2MU(0);
249
250         return ast_format_register(name, exts, AST_FORMAT_ULAW,
251                                    pcm_open,
252                                    pcm_rewrite,
253                                    pcm_write,
254                                    pcm_seek,
255                                    pcm_trunc,
256                                    pcm_tell,
257                                    pcm_read,
258                                    pcm_close,
259                                    pcm_getcomment);
260 }
261
262 int unload_module()
263 {
264         return ast_format_unregister(name);
265 }       
266
267 int usecount()
268 {
269         return glistcnt;
270 }
271
272 char *description()
273 {
274         return desc;
275 }
276
277
278 char *key()
279 {
280         return ASTERISK_GPL_KEY;
281 }