remove extraneous svn:executable properties
[asterisk/asterisk.git] / formats / format_pcm.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, 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
48 #define BUF_SIZE 160            /* 160 samples */
49
50 struct ast_filestream {
51         void *reserved[AST_RESERVED_POINTERS];
52         /* This is what a filestream means to us */
53         FILE *f; /* Descriptor */
54         struct ast_channel *owner;
55         struct ast_frame fr;                            /* Frame information */
56         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
57         char empty;                                                     /* Empty character */
58         unsigned char buf[BUF_SIZE];                            /* Output Buffer */
59         struct timeval last;
60 };
61
62
63 AST_MUTEX_DEFINE_STATIC(pcm_lock);
64 static int glistcnt = 0;
65
66 static char *name = "pcm";
67 static char *desc = "Raw uLaw 8khz Audio support (PCM)";
68 static char *exts = "pcm|ulaw|ul|mu";
69
70 static struct ast_filestream *pcm_open(FILE *f)
71 {
72         /* We don't have any header to read or anything really, but
73            if we did, it would go here.  We also might want to check
74            and be sure it's a valid file.  */
75         struct ast_filestream *tmp;
76         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
77                 memset(tmp, 0, sizeof(struct ast_filestream));
78                 if (ast_mutex_lock(&pcm_lock)) {
79                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
80                         free(tmp);
81                         return NULL;
82                 }
83                 tmp->f = f;
84                 tmp->fr.data = tmp->buf;
85                 tmp->fr.frametype = AST_FRAME_VOICE;
86                 tmp->fr.subclass = AST_FORMAT_ULAW;
87                 /* datalen will vary for each frame */
88                 tmp->fr.src = name;
89                 tmp->fr.mallocd = 0;
90                 glistcnt++;
91                 ast_mutex_unlock(&pcm_lock);
92                 ast_update_use_count();
93         }
94         return tmp;
95 }
96
97 static struct ast_filestream *pcm_rewrite(FILE *f, const char *comment)
98 {
99         /* We don't have any header to read or anything really, but
100            if we did, it would go here.  We also might want to check
101            and be sure it's a valid file.  */
102         struct ast_filestream *tmp;
103         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
104                 memset(tmp, 0, sizeof(struct ast_filestream));
105                 if (ast_mutex_lock(&pcm_lock)) {
106                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
107                         free(tmp);
108                         return NULL;
109                 }
110                 tmp->f = f;
111                 glistcnt++;
112                 ast_mutex_unlock(&pcm_lock);
113                 ast_update_use_count();
114         } else
115                 ast_log(LOG_WARNING, "Out of memory\n");
116         return tmp;
117 }
118
119 static void pcm_close(struct ast_filestream *s)
120 {
121         if (ast_mutex_lock(&pcm_lock)) {
122                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
123                 return;
124         }
125         glistcnt--;
126         ast_mutex_unlock(&pcm_lock);
127         ast_update_use_count();
128         fclose(s->f);
129         free(s);
130         s = NULL;
131 }
132
133 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
134 {
135         int res;
136         int delay;
137         /* Send a frame from the file to the appropriate channel */
138
139         s->fr.frametype = AST_FRAME_VOICE;
140         s->fr.subclass = AST_FORMAT_ULAW;
141         s->fr.offset = AST_FRIENDLY_OFFSET;
142         s->fr.mallocd = 0;
143         s->fr.data = s->buf;
144         if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
145                 if (res)
146                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
147                 return NULL;
148         }
149         s->fr.samples = res;
150         s->fr.datalen = res;
151         delay = s->fr.samples;
152         *whennext = delay;
153         return &s->fr;
154 }
155
156 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
157 {
158         int res;
159         if (f->frametype != AST_FRAME_VOICE) {
160                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
161                 return -1;
162         }
163         if (f->subclass != AST_FORMAT_ULAW) {
164                 ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
165                 return -1;
166         }
167         if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
168                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
169                         return -1;
170         }
171         return 0;
172 }
173
174 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
175 {
176         off_t offset=0,min,cur,max;
177
178         min = 0;
179         cur = ftell(fs->f);
180         fseek(fs->f, 0, SEEK_END);
181         max = ftell(fs->f);
182         if (whence == SEEK_SET)
183                 offset = sample_offset;
184         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
185                 offset = sample_offset + cur;
186         else if (whence == SEEK_END)
187                 offset = max - sample_offset;
188         if (whence != SEEK_FORCECUR) {
189                 offset = (offset > max)?max:offset;
190         }
191         /* always protect against seeking past begining. */
192         offset = (offset < min)?min:offset;
193         return fseek(fs->f, offset, SEEK_SET);
194 }
195
196 static int pcm_trunc(struct ast_filestream *fs)
197 {
198         return ftruncate(fileno(fs->f), ftell(fs->f));
199 }
200
201 static long pcm_tell(struct ast_filestream *fs)
202 {
203         off_t offset;
204         offset = ftell(fs->f);
205         return offset;
206 }
207
208 static char *pcm_getcomment(struct ast_filestream *s)
209 {
210         return NULL;
211 }
212
213 int load_module()
214 {
215         return ast_format_register(name, exts, AST_FORMAT_ULAW,
216                                                                 pcm_open,
217                                                                 pcm_rewrite,
218                                                                 pcm_write,
219                                                                 pcm_seek,
220                                                                 pcm_trunc,
221                                                                 pcm_tell,
222                                                                 pcm_read,
223                                                                 pcm_close,
224                                                                 pcm_getcomment);
225                                                                 
226                                                                 
227 }
228
229 int unload_module()
230 {
231         return ast_format_unregister(name);
232 }       
233
234 int usecount()
235 {
236         return glistcnt;
237 }
238
239 char *description()
240 {
241         return desc;
242 }
243
244
245 char *key()
246 {
247         return ASTERISK_GPL_KEY;
248 }