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