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