Merged revisions 7799-7800 via svnmerge from
[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, long sample_offset, int whence)
178 {
179         long cur, max, offset;
180
181         cur = ftell(fs->f);
182         max = fseek(fs->f, 0, SEEK_END);
183
184         switch (whence) {
185         case SEEK_SET:
186                 offset = sample_offset;
187                 break;
188         case SEEK_END:
189                 offset = max - sample_offset;
190                 break;
191         case SEEK_CUR:
192         case SEEK_FORCECUR:
193                 offset = cur + sample_offset;
194                 break;
195         }
196
197         switch (whence) {
198         case SEEK_FORCECUR:
199                 if (offset > max) {
200                         size_t left = offset - max;
201                         size_t res;
202
203                         while (left) {
204                                 res = fwrite(ulaw_silence, (left > BUF_SIZE) ? BUF_SIZE : left,
205                                              sizeof(ulaw_silence[0]), fs->f);
206                                 if (res == -1)
207                                         return res;
208                                 left -= res;
209                         }
210                 }
211                 /* fall through */
212         default:
213                 offset = (offset > max) ? max : offset;
214                 offset = (offset < 0) ? 0 : offset;
215                 return fseek(fs->f, offset, SEEK_SET);
216         }
217 }
218
219 static int pcm_trunc(struct ast_filestream *fs)
220 {
221         return ftruncate(fileno(fs->f), ftell(fs->f));
222 }
223
224 static long pcm_tell(struct ast_filestream *fs)
225 {
226         off_t offset;
227         offset = ftell(fs->f);
228         return offset;
229 }
230
231 static char *pcm_getcomment(struct ast_filestream *s)
232 {
233         return NULL;
234 }
235
236 int load_module()
237 {
238         int index;
239
240         for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++)
241                 ulaw_silence[index] = AST_LIN2MU(0);
242
243         return ast_format_register(name, exts, AST_FORMAT_ULAW,
244                                    pcm_open,
245                                    pcm_rewrite,
246                                    pcm_write,
247                                    pcm_seek,
248                                    pcm_trunc,
249                                    pcm_tell,
250                                    pcm_read,
251                                    pcm_close,
252                                    pcm_getcomment);
253 }
254
255 int unload_module()
256 {
257         return ast_format_unregister(name);
258 }       
259
260 int usecount()
261 {
262         return glistcnt;
263 }
264
265 char *description()
266 {
267         return desc;
268 }
269
270
271 char *key()
272 {
273         return ASTERISK_GPL_KEY;
274 }