Remove pthread.h from source. We should be using asterisk/lock.h everywhere instead...
[asterisk/asterisk.git] / formats / format_pcm_alaw.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Flat, binary, alaw 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 <sys/times.h>
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <string.h>
30 #ifdef __linux__
31 #include <endian.h>
32 #else
33 #include <machine/endian.h>
34 #endif
35
36 #define BUF_SIZE 160            /* 160 samples */
37
38 // #define REALTIME_WRITE
39
40 struct ast_filestream {
41         void *reserved[AST_RESERVED_POINTERS];
42         /* Believe it or not, we must decode/recode to account for the
43            weird MS format */
44         /* This is what a filestream means to us */
45         int fd; /* Descriptor */
46         struct ast_frame fr;                            /* Frame information */
47         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
48         char empty;                                                     /* Empty character */
49         unsigned char buf[BUF_SIZE];                            /* Output Buffer */
50 #ifdef REALTIME_WRITE
51         unsigned long start_time;
52 #endif
53 };
54
55
56 AST_MUTEX_DEFINE_STATIC(pcm_lock);
57 static int glistcnt = 0;
58
59 static char *name = "alaw";
60 static char *desc = "Raw aLaw 8khz PCM Audio support";
61 static char *exts = "alaw|al";
62
63
64 #if 0
65 /* Returns time in msec since system boot. */
66 static unsigned long get_time(void)
67 {
68         struct tms buf;
69         clock_t cur;
70
71         cur = times( &buf );
72         if( cur < 0 )
73         {
74                 ast_log( LOG_WARNING, "Cannot get current time\n" );
75                 return 0;
76         }
77         return cur * 1000 / sysconf( _SC_CLK_TCK );
78 }
79 #endif
80
81 static struct ast_filestream *pcm_open(int fd)
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(&pcm_lock)) {
90                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
91                         free(tmp);
92                         return NULL;
93                 }
94                 tmp->fd = fd;
95                 tmp->fr.data = tmp->buf;
96                 tmp->fr.frametype = AST_FRAME_VOICE;
97                 tmp->fr.subclass = AST_FORMAT_ALAW;
98                 /* datalen will vary for each frame */
99                 tmp->fr.src = name;
100                 tmp->fr.mallocd = 0;
101 #ifdef REALTIME_WRITE
102                 tmp->start_time = get_time();
103 #endif
104                 glistcnt++;
105                 ast_mutex_unlock(&pcm_lock);
106                 ast_update_use_count();
107         }
108         return tmp;
109 }
110
111 static struct ast_filestream *pcm_rewrite(int fd, char *comment)
112 {
113         /* We don't have any header to read or anything really, but
114            if we did, it would go here.  We also might want to check
115            and be sure it's a valid file.  */
116         struct ast_filestream *tmp;
117         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
118                 memset(tmp, 0, sizeof(struct ast_filestream));
119                 if (ast_mutex_lock(&pcm_lock)) {
120                         ast_log(LOG_WARNING, "Unable to lock pcm list\n");
121                         free(tmp);
122                         return NULL;
123                 }
124                 tmp->fd = fd;
125 #ifdef REALTIME_WRITE
126                 tmp->start_time = get_time();
127 #endif
128                 glistcnt++;
129                 ast_mutex_unlock(&pcm_lock);
130                 ast_update_use_count();
131         } else
132                 ast_log(LOG_WARNING, "Out of memory\n");
133         return tmp;
134 }
135
136 static void pcm_close(struct ast_filestream *s)
137 {
138         if (ast_mutex_lock(&pcm_lock)) {
139                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
140                 return;
141         }
142         glistcnt--;
143         ast_mutex_unlock(&pcm_lock);
144         ast_update_use_count();
145         close(s->fd);
146         free(s);
147         s = NULL;
148 }
149
150 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
151 {
152         int res;
153         /* Send a frame from the file to the appropriate channel */
154
155         s->fr.frametype = AST_FRAME_VOICE;
156         s->fr.subclass = AST_FORMAT_ALAW;
157         s->fr.offset = AST_FRIENDLY_OFFSET;
158         s->fr.mallocd = 0;
159         s->fr.data = s->buf;
160         if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) {
161                 if (res)
162                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
163                 return NULL;
164         }
165         s->fr.samples = res;
166         s->fr.datalen = res;
167         *whennext = s->fr.samples;
168         return &s->fr;
169 }
170
171 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
172 {
173         int res;
174 #ifdef REALTIME_WRITE
175         unsigned long cur_time;
176         unsigned long fpos;
177         struct stat stat_buf;
178 #endif
179
180         if (f->frametype != AST_FRAME_VOICE) {
181                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
182                 return -1;
183         }
184         if (f->subclass != AST_FORMAT_ALAW) {
185                 ast_log(LOG_WARNING, "Asked to write non-alaw frame (%d)!\n", f->subclass);
186                 return -1;
187         }
188
189 #ifdef REALTIME_WRITE
190         cur_time = get_time();
191         fpos = ( cur_time - fs->start_time ) * 8;       // 8 bytes per msec
192         // Check if we have written to this position yet. If we have, then increment pos by one frame
193         // for some degree of protection against receiving packets in the same clock tick.
194         fstat( fs->fd, &stat_buf );
195         if( stat_buf.st_size > fpos )
196         {
197                 fpos += f->datalen;     // Incrementing with the size of this current frame
198         }
199
200         if( stat_buf.st_size < fpos )
201         {
202                 // fill the gap with 0x55 rather than 0.
203                 char buf[ 512 ];
204                 unsigned long cur, to_write;
205
206                 cur = stat_buf.st_size;
207                 if( lseek( fs->fd, cur, SEEK_SET ) < 0 )
208                 {
209                         ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
210                         return -1;
211                 }
212                 memset( buf, 0x55, 512 );
213                 while( cur < fpos )
214                 {
215                         to_write = fpos - cur;
216                         if( to_write > 512 )
217                         {
218                                 to_write = 512;
219                         }
220                         write( fs->fd, buf, to_write );
221                         cur += to_write;
222                 }
223         }
224
225
226         if( lseek( fs->fd, fpos, SEEK_SET ) < 0 )
227         {
228                 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
229                 return -1;
230         }
231 #endif  // REALTIME_WRITE
232         
233         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
234                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
235                         return -1;
236         }
237         return 0;
238 }
239
240 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
241 {
242         off_t offset=0,min,cur,max;
243
244         min = 0;
245         cur = lseek(fs->fd, 0, SEEK_CUR);
246         max = lseek(fs->fd, 0, SEEK_END);
247         if (whence == SEEK_SET)
248                 offset = sample_offset;
249         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
250                 offset = sample_offset + cur;
251         else if (whence == SEEK_END)
252                 offset = max - sample_offset;
253         if (whence != SEEK_FORCECUR) {
254                 offset = (offset > max)?max:offset;
255         }
256         // Always protect against seeking past begining
257         offset = (offset < min)?min:offset;
258         return lseek(fs->fd, offset, SEEK_SET);
259 }
260
261 static int pcm_trunc(struct ast_filestream *fs)
262 {
263         return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
264 }
265
266 static long pcm_tell(struct ast_filestream *fs)
267 {
268         off_t offset;
269         offset = lseek(fs->fd, 0, SEEK_CUR);
270         return offset;
271 }
272
273
274 static char *pcm_getcomment(struct ast_filestream *s)
275 {
276         return NULL;
277 }
278
279 int load_module()
280 {
281         return ast_format_register(name, exts, AST_FORMAT_ALAW,
282                                                                 pcm_open,
283                                                                 pcm_rewrite,
284                                                                 pcm_write,
285                                                                 pcm_seek,
286                                                                 pcm_trunc,
287                                                                 pcm_tell,
288                                                                 pcm_read,
289                                                                 pcm_close,
290                                                                 pcm_getcomment);
291 }
292
293 int unload_module()
294 {
295         return ast_format_unregister(name);
296 }       
297
298 int usecount()
299 {
300         int res;
301         if (ast_mutex_lock(&pcm_lock)) {
302                 ast_log(LOG_WARNING, "Unable to lock pcm list\n");
303                 return -1;
304         }
305         res = glistcnt;
306         ast_mutex_unlock(&pcm_lock);
307         return res;
308 }
309
310 char *description()
311 {
312         return desc;
313 }
314
315
316 char *key()
317 {
318         return ASTERISK_GPL_KEY;
319 }