Check for IO stream failures in various format's truncate/seek operations
[asterisk/asterisk.git] / formats / format_siren14.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Anthony Minessale and Digium, Inc.
5  * Anthony Minessale (anthmct@yahoo.com)
6  * Kevin P. Fleming <kpfleming@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief ITU G.722.1 Annex C (Siren14, licensed from Polycom) format, 48kbps bitrate only
22  * \arg File name extensions: siren14
23  * \ingroup formats
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29  
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/mod_format.h"
35 #include "asterisk/module.h"
36 #include "asterisk/endian.h"
37
38 #define BUF_SIZE        120             /* 20 milliseconds == 120 bytes, 640 samples */
39 #define SAMPLES_TO_BYTES(x)     ((typeof(x)) x / ((float) 640 / 120))
40 #define BYTES_TO_SAMPLES(x)     ((typeof(x)) x * ((float) 640 / 120))
41
42 static struct ast_frame *siren14read(struct ast_filestream *s, int *whennext)
43 {
44         int res;
45         /* Send a frame from the file to the appropriate channel */
46
47         s->fr.frametype = AST_FRAME_VOICE;
48         ast_format_set(&s->fr.subclass.format, AST_FORMAT_SIREN14, 0);
49         s->fr.mallocd = 0;
50         AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
51         if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
52                 if (res)
53                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
54                 return NULL;
55         }
56         *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
57         return &s->fr;
58 }
59
60 static int siren14write(struct ast_filestream *fs, struct ast_frame *f)
61 {
62         int res;
63
64         if (f->frametype != AST_FRAME_VOICE) {
65                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
66                 return -1;
67         }
68         if (f->subclass.format.id != AST_FORMAT_SIREN14) {
69                 ast_log(LOG_WARNING, "Asked to write non-Siren14 frame (%s)!\n", ast_getformatname(&f->subclass.format));
70                 return -1;
71         }
72         if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
73                 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
74                 return -1;
75         }
76         return 0;
77 }
78
79 static int siren14seek(struct ast_filestream *fs, off_t sample_offset, int whence)
80 {
81         off_t offset = 0, min = 0, cur, max;
82
83         sample_offset = SAMPLES_TO_BYTES(sample_offset);
84
85         if ((cur = ftello(fs->f)) < 0) {
86                 ast_log(AST_LOG_WARNING, "Unable to determine current position in siren14 filestream %p: %s\n", fs, strerror(errno));
87                 return -1;
88         }
89
90         if (fseeko(fs->f, 0, SEEK_END) < 0) {
91                 ast_log(AST_LOG_WARNING, "Unable to seek to end of siren14 filestream %p: %s\n", fs, strerror(errno));
92                 return -1;
93         }
94
95         if ((max = ftello(fs->f) < 0)) {
96                 ast_log(AST_LOG_WARNING, "Unable to determine max position in siren14 filestream %p: %s\n", fs, strerror(errno));
97                 return -1;
98         }
99
100         if (whence == SEEK_SET)
101                 offset = sample_offset;
102         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
103                 offset = sample_offset + cur;
104         else if (whence == SEEK_END)
105                 offset = max - sample_offset;
106
107         if (whence != SEEK_FORCECUR)
108                 offset = (offset > max) ? max : offset;
109
110         /* always protect against seeking past begining. */
111         offset = (offset < min) ? min : offset;
112
113         return fseeko(fs->f, offset, SEEK_SET);
114 }
115
116 static int siren14trunc(struct ast_filestream *fs)
117 {
118         int fd;
119         off_t cur;
120
121         if ((fd = fileno(fs->f)) < 0) {
122                 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for siren14 filestream %p: %s\n", fs, strerror(errno));
123                 return -1;
124         }
125         if ((cur = ftello(fs->f) < 0)) {
126                 ast_log(AST_LOG_WARNING, "Unable to determine current position in siren14 filestream %p: %s\n", fs, strerror(errno));
127                 return -1;
128         }
129         /* Truncate file to current length */
130         return ftruncate(fd, cur);
131 }
132
133 static off_t siren14tell(struct ast_filestream *fs)
134 {
135         return BYTES_TO_SAMPLES(ftello(fs->f));
136 }
137
138 static struct ast_format_def siren14_f = {
139         .name = "siren14",
140         .exts = "siren14",
141         .write = siren14write,
142         .seek = siren14seek,
143         .trunc = siren14trunc,
144         .tell = siren14tell,
145         .read = siren14read,
146         .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
147 };
148
149 static int load_module(void)
150 {
151         ast_format_set(&siren14_f.format, AST_FORMAT_SIREN14, 0);
152         if (ast_format_def_register(&siren14_f))
153                 return AST_MODULE_LOAD_DECLINE;
154
155         return AST_MODULE_LOAD_SUCCESS;
156 }
157
158 static int unload_module(void)
159 {
160         return ast_format_def_unregister(siren14_f.name);
161 }
162
163 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ITU G.722.1 Annex C (Siren14, licensed from Polycom)",
164         .load = load_module,
165         .unload = unload_module,
166         .load_pri = AST_MODPRI_APP_DEPEND
167 );