update MANY more files with proper copyright/license info (thanks Ian!)
[asterisk/asterisk.git] / formats / format_sln.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Anthony Minessale (anthmct@yahoo.com)
5  * Derived from format_pcm.c in the asterisk distro
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*
19  *
20  * RAW SLINEAR Format
21  *
22  */
23  
24 #include <unistd.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <stdlib.h>
28 #include <sys/time.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include "asterisk/lock.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/file.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/sched.h"
42 #include "asterisk/module.h"
43 #include "asterisk/endian.h"
44
45 #define BUF_SIZE 320            /* 320 samples */
46
47 struct ast_filestream {
48         void *reserved[AST_RESERVED_POINTERS];
49         /* This is what a filestream means to us */
50         int fd; /* Descriptor */
51         struct ast_channel *owner;
52         struct ast_frame fr;                            /* Frame information */
53         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
54         char empty;                                                     /* Empty character */
55         unsigned char buf[BUF_SIZE];                            /* Output Buffer */
56         struct timeval last;
57 };
58
59
60 AST_MUTEX_DEFINE_STATIC(slinear_lock);
61 static int glistcnt = 0;
62
63 static char *name = "sln";
64 static char *desc = "Raw Signed Linear Audio support (SLN)";
65 static char *exts = "sln|raw";
66
67 static struct ast_filestream *slinear_open(int fd)
68 {
69         /* We don't have any header to read or anything really, but
70            if we did, it would go here.  We also might want to check
71            and be sure it's a valid file.  */
72         struct ast_filestream *tmp;
73         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
74                 memset(tmp, 0, sizeof(struct ast_filestream));
75                 if (ast_mutex_lock(&slinear_lock)) {
76                         ast_log(LOG_WARNING, "Unable to lock slinear list\n");
77                         free(tmp);
78                         return NULL;
79                 }
80                 tmp->fd = fd;
81                 tmp->fr.data = tmp->buf;
82                 tmp->fr.frametype = AST_FRAME_VOICE;
83                 tmp->fr.subclass = AST_FORMAT_SLINEAR;
84                 /* datalen will vary for each frame */
85                 tmp->fr.src = name;
86                 tmp->fr.mallocd = 0;
87                 glistcnt++;
88                 ast_mutex_unlock(&slinear_lock);
89                 ast_update_use_count();
90         }
91         return tmp;
92 }
93
94 static struct ast_filestream *slinear_rewrite(int fd, const char *comment)
95 {
96         /* We don't have any header to read or anything really, but
97            if we did, it would go here.  We also might want to check
98            and be sure it's a valid file.  */
99         struct ast_filestream *tmp;
100         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
101                 memset(tmp, 0, sizeof(struct ast_filestream));
102                 if (ast_mutex_lock(&slinear_lock)) {
103                         ast_log(LOG_WARNING, "Unable to lock slinear list\n");
104                         free(tmp);
105                         return NULL;
106                 }
107                 tmp->fd = fd;
108                 glistcnt++;
109                 ast_mutex_unlock(&slinear_lock);
110                 ast_update_use_count();
111         } else
112                 ast_log(LOG_WARNING, "Out of memory\n");
113         return tmp;
114 }
115
116 static void slinear_close(struct ast_filestream *s)
117 {
118         if (ast_mutex_lock(&slinear_lock)) {
119                 ast_log(LOG_WARNING, "Unable to lock slinear list\n");
120                 return;
121         }
122         glistcnt--;
123         ast_mutex_unlock(&slinear_lock);
124         ast_update_use_count();
125         close(s->fd);
126         free(s);
127         s = NULL;
128 }
129
130 static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext)
131 {
132         int res;
133         int delay;
134         /* Send a frame from the file to the appropriate channel */
135
136         s->fr.frametype = AST_FRAME_VOICE;
137         s->fr.subclass = AST_FORMAT_SLINEAR;
138         s->fr.offset = AST_FRIENDLY_OFFSET;
139         s->fr.mallocd = 0;
140         s->fr.data = s->buf;
141         if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) {
142                 if (res)
143                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
144                 return NULL;
145         }
146         s->fr.samples = res/2;
147         s->fr.datalen = res;
148         delay = s->fr.samples;
149         *whennext = delay;
150         return &s->fr;
151 }
152
153 static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
154 {
155         int res;
156         if (f->frametype != AST_FRAME_VOICE) {
157                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
158                 return -1;
159         }
160         if (f->subclass != AST_FORMAT_SLINEAR) {
161                 ast_log(LOG_WARNING, "Asked to write non-slinear frame (%d)!\n", f->subclass);
162                 return -1;
163         }
164         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
165                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
166                         return -1;
167         }
168         return 0;
169 }
170
171 static int slinear_seek(struct ast_filestream *fs, long sample_offset, int whence)
172 {
173         off_t offset=0,min,cur,max;
174
175         min = 0;
176         sample_offset <<= 1;
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) / 2;
191 }
192
193 static int slinear_trunc(struct ast_filestream *fs)
194 {
195         return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
196 }
197
198 static long slinear_tell(struct ast_filestream *fs)
199 {
200         off_t offset;
201         offset = lseek(fs->fd, 0, SEEK_CUR);
202         return offset / 2;
203 }
204
205 static char *slinear_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_SLINEAR,
213                                                                 slinear_open,
214                                                                 slinear_rewrite,
215                                                                 slinear_write,
216                                                                 slinear_seek,
217                                                                 slinear_trunc,
218                                                                 slinear_tell,
219                                                                 slinear_read,
220                                                                 slinear_close,
221                                                                 slinear_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 }