Debian Codegraph @

formats/format_ilbc.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Brian K. West <[email protected]>
5 *
6 * Copyright (C) 1999 - 2005, Digium, Inc.
7 *
8 * Mark Spencer <[email protected]>
9 *
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
15 *
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
19 */
20
21 /*! \file
22 *
23 * \brief Save to raw, headerless iLBC data.
24 * \arg File name extension: ilbc
25 * \ingroup formats
26 */
27
28 /*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/mod_format.h"
35 #include "asterisk/module.h"
36 #include "asterisk/endian.h"
37 #include "asterisk/format_cache.h"
38
39 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
40
41 /* Portions of the conversion code are by [email protected] */
42
43 #define ILBC_BUF_SIZE 50 /* One Real iLBC Frame */
44 #define ILBC_SAMPLES 240
45
46 static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
47 {
48 size_t res;
49
50 /* Send a frame from the file to the appropriate channel */
51 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE);
52 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
53 if (res) {
54 ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
55 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
56 strerror(errno));
57 }
58 return NULL;
59 }
60 *whennext = s->fr.samples = ILBC_SAMPLES;
61 return &s->fr;
62 }
63
64 static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
65 {
66 int res;
67 if (f->datalen % 50) {
68 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
69 return -1;
70 }
71 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
72 ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno));
73 return -1;
74 }
75 return 0;
76 }
77
78 static int ilbc_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
79 {
80 long bytes;
81 off_t min,cur,max,offset=0;
82 min = 0;
83 cur = ftello(fs->f);
84 fseeko(fs->f, 0, SEEK_END);
85 max = ftello(fs->f);
86
87 bytes = ILBC_BUF_SIZE * (sample_offset / ILBC_SAMPLES);
88 if (whence == SEEK_SET)
89 offset = bytes;
90 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
91 offset = cur + bytes;
92 else if (whence == SEEK_END)
93 offset = max - bytes;
94 if (whence != SEEK_FORCECUR) {
95 offset = (offset > max)?max:offset;
96 }
97 /* protect against seeking beyond begining. */
98 offset = (offset < min)?min:offset;
99 if (fseeko(fs->f, offset, SEEK_SET) < 0)
100 return -1;
101 return 0;
102 }
103
104 static int ilbc_trunc(struct ast_filestream *fs)
105 {
106 int fd;
107 off_t cur;
108
109 if ((fd = fileno(fs->f)) < 0) {
110 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for iLBC filestream %p: %s\n", fs, strerror(errno));
111 return -1;
112 }
113 if ((cur = ftello(fs->f)) < 0) {
114 ast_log(AST_LOG_WARNING, "Unable to determine current position in iLBC filestream %p: %s\n", fs, strerror(errno));
115 return -1;
116 }
117 /* Truncate file to current length */
118 return ftruncate(fd, cur);
119 }
120
121 static off_t ilbc_tell(struct ast_filestream *fs)
122 {
123 off_t offset = ftello(fs->f);
124 return (offset/ILBC_BUF_SIZE)*ILBC_SAMPLES;
125 }
126
127 static struct ast_format_def ilbc_f = {
128 .name = "iLBC",
129 .exts = "ilbc",
130 .write = ilbc_write,
131 .seek = ilbc_seek,
132 .trunc = ilbc_trunc,
133 .tell = ilbc_tell,
134 .read = ilbc_read,
135 .buf_size = ILBC_BUF_SIZE + AST_FRIENDLY_OFFSET,
136 };
137
138 static int load_module(void)
139 {
140 ilbc_f.format = ast_format_ilbc;
141 if (ast_format_def_register(&ilbc_f))
142 return AST_MODULE_LOAD_DECLINE;
143 return AST_MODULE_LOAD_SUCCESS;
144 }
145
146 static int unload_module(void)
147 {
148 return ast_format_def_unregister(ilbc_f.name);
149 }
150
151 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw iLBC data",
152 .support_level = AST_MODULE_SUPPORT_CORE,
153 .load = load_module,
154 .unload = unload_module,
155 .load_pri = AST_MODPRI_APP_DEPEND
156 );