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