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