more doxygenification (issue #5513)
[asterisk/asterisk.git] / formats / format_sln.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Anthony Minessale
5  * Anthony Minessale (anthmct@yahoo.com)
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 /*! \file
19  *
20  * \brief 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         FILE *f; /* 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(FILE *f)
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->f = f;
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(FILE *f, 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->f = f;
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         fclose(s->f);
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 = fread(s->buf, 1, BUF_SIZE, s->f)) < 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 = fwrite(f->data, 1, f->datalen, fs->f)) != 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 = ftell(fs->f);
178         fseek(fs->f, 0, SEEK_END);
179         max = ftell(fs->f);
180         if (whence == SEEK_SET)
181                 offset = sample_offset;
182         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
183                 offset = sample_offset + cur;
184         else if (whence == SEEK_END)
185                 offset = max - sample_offset;
186         if (whence != SEEK_FORCECUR) {
187                 offset = (offset > max)?max:offset;
188         }
189         /* always protect against seeking past begining. */
190         offset = (offset < min)?min:offset;
191         return fseek(fs->f, offset, SEEK_SET) / 2;
192 }
193
194 static int slinear_trunc(struct ast_filestream *fs)
195 {
196         return ftruncate(fileno(fs->f), ftell(fs->f));
197 }
198
199 static long slinear_tell(struct ast_filestream *fs)
200 {
201         off_t offset;
202         offset = ftell(fs->f);
203         return offset / 2;
204 }
205
206 static char *slinear_getcomment(struct ast_filestream *s)
207 {
208         return NULL;
209 }
210
211 int load_module()
212 {
213         return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
214                                                                 slinear_open,
215                                                                 slinear_rewrite,
216                                                                 slinear_write,
217                                                                 slinear_seek,
218                                                                 slinear_trunc,
219                                                                 slinear_tell,
220                                                                 slinear_read,
221                                                                 slinear_close,
222                                                                 slinear_getcomment);
223                                                                 
224                                                                 
225 }
226
227 int unload_module()
228 {
229         return ast_format_unregister(name);
230 }       
231
232 int usecount()
233 {
234         return glistcnt;
235 }
236
237 char *description()
238 {
239         return desc;
240 }
241
242
243 char *key()
244 {
245         return ASTERISK_GPL_KEY;
246 }