Fix synchronization of recorded files when using Monitor application
[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 <arpa/inet.h>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <pthread.h>
28 #ifdef __linux__
29 #include <endian.h>
30 #else
31 #include <machine/endian.h>
32 #endif
33
34 #define BUF_SIZE 160            /* 160 samples */
35
36 struct ast_filestream {
37         void *reserved[AST_RESERVED_POINTERS];
38         /* This is what a filestream means to us */
39         int fd; /* Descriptor */
40         struct ast_channel *owner;
41         struct ast_frame fr;                            /* Frame information */
42         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
43         char empty;                                                     /* Empty character */
44         unsigned char buf[BUF_SIZE];                            /* Output Buffer */
45         struct timeval last;
46 };
47
48
49 static ast_mutex_t pcm_lock = AST_MUTEX_INITIALIZER;
50 static int glistcnt = 0;
51
52 static char *name = "pcm";
53 static char *desc = "Raw uLaw 8khz Audio support (PCM)";
54 static char *exts = "pcm|ulaw|ul|mu";
55
56 static struct ast_filestream *pcm_open(int fd)
57 {
58         /* We don't have any header to read or anything really, but
59            if we did, it would go here.  We also might want to check
60            and be sure it's a valid file.  */
61         struct ast_filestream *tmp;
62         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
63                 memset(tmp, 0, sizeof(struct ast_filestream));
64                 if (ast_mutex_lock(&pcm_lock)) {
65                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
66                         free(tmp);
67                         return NULL;
68                 }
69                 tmp->fd = fd;
70                 tmp->fr.data = tmp->buf;
71                 tmp->fr.frametype = AST_FRAME_VOICE;
72                 tmp->fr.subclass = AST_FORMAT_ULAW;
73                 /* datalen will vary for each frame */
74                 tmp->fr.src = name;
75                 tmp->fr.mallocd = 0;
76                 glistcnt++;
77                 ast_mutex_unlock(&pcm_lock);
78                 ast_update_use_count();
79         }
80         return tmp;
81 }
82
83 static struct ast_filestream *pcm_rewrite(int fd, char *comment)
84 {
85         /* We don't have any header to read or anything really, but
86            if we did, it would go here.  We also might want to check
87            and be sure it's a valid file.  */
88         struct ast_filestream *tmp;
89         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
90                 memset(tmp, 0, sizeof(struct ast_filestream));
91                 if (ast_mutex_lock(&pcm_lock)) {
92                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
93                         free(tmp);
94                         return NULL;
95                 }
96                 tmp->fd = fd;
97                 glistcnt++;
98                 ast_mutex_unlock(&pcm_lock);
99                 ast_update_use_count();
100         } else
101                 ast_log(LOG_WARNING, "Out of memory\n");
102         return tmp;
103 }
104
105 static void pcm_close(struct ast_filestream *s)
106 {
107         if (ast_mutex_lock(&pcm_lock)) {
108                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
109                 return;
110         }
111         ast_mutex_unlock(&pcm_lock);
112         ast_update_use_count();
113         close(s->fd);
114         free(s);
115         s = NULL;
116 }
117
118 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
119 {
120         int res;
121         int delay;
122         /* Send a frame from the file to the appropriate channel */
123
124         s->fr.frametype = AST_FRAME_VOICE;
125         s->fr.subclass = AST_FORMAT_ULAW;
126         s->fr.offset = AST_FRIENDLY_OFFSET;
127         s->fr.mallocd = 0;
128         s->fr.data = s->buf;
129         if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) {
130                 if (res)
131                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
132                 return NULL;
133         }
134         s->fr.samples = res;
135         s->fr.datalen = res;
136         delay = s->fr.samples;
137         *whennext = delay;
138         return &s->fr;
139 }
140
141 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
142 {
143         int res;
144         if (f->frametype != AST_FRAME_VOICE) {
145                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
146                 return -1;
147         }
148         if (f->subclass != AST_FORMAT_ULAW) {
149                 ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
150                 return -1;
151         }
152         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
153                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
154                         return -1;
155         }
156         return 0;
157 }
158
159 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
160 {
161         off_t offset=0,min,cur,max;
162
163         min = 0;
164         cur = lseek(fs->fd, 0, SEEK_CUR);
165         max = lseek(fs->fd, 0, SEEK_END);
166         if (whence == SEEK_SET)
167                 offset = sample_offset;
168         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
169                 offset = sample_offset + cur;
170         else if (whence == SEEK_END)
171                 offset = max - sample_offset;
172         if (whence != SEEK_FORCECUR) {
173                 offset = (offset > max)?max:offset;
174                 offset = (offset < min)?min:offset;
175         }
176         return lseek(fs->fd, offset, SEEK_SET);
177 }
178
179 static int pcm_trunc(struct ast_filestream *fs)
180 {
181         return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
182 }
183
184 static long pcm_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 *pcm_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_ULAW,
199                                                                 pcm_open,
200                                                                 pcm_rewrite,
201                                                                 pcm_write,
202                                                                 pcm_seek,
203                                                                 pcm_trunc,
204                                                                 pcm_tell,
205                                                                 pcm_read,
206                                                                 pcm_close,
207                                                                 pcm_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(&pcm_lock)) {
221                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
222                 return -1;
223         }
224         res = glistcnt;
225         ast_mutex_unlock(&pcm_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 }