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