Asterisk - The Open Source Telephony Project  18.5.0
format_ilbc.c
Go to the documentation of this file.
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 */
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",
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))
144 }
145 
146 static int unload_module(void)
147 {
148  return ast_format_def_unregister(ilbc_f.name);
149 }
150 
152  .support_level = AST_MODULE_SUPPORT_CORE,
153  .load = load_module,
154  .unload = unload_module,
155  .load_pri = AST_MODPRI_APP_DEPEND
156 );
Asterisk main include file. File version handling, generic pbx functions.
#define LOG_WARNING
Definition: logger.h:274
#define AST_LOG_WARNING
Definition: logger.h:279
static int unload_module(void)
Definition: format_ilbc.c:146
#define ILBC_SAMPLES
Definition: format_ilbc.c:44
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define NULL
Definition: resample.c:96
Each supported file format is described by the following structure.
Definition: mod_format.h:43
struct ast_frame_subclass subclass
Header for providers of file and format handling routines. Clients of these routines should include "...
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
#define ast_log
Definition: astobj2.c:42
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Asterisk architecture endianess compatibility definitions.
#define ast_format_def_register(f)
Definition: mod_format.h:136
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
struct ast_format * format
Definition: mod_format.h:48
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
int errno
#define ILBC_BUF_SIZE
Definition: format_ilbc.c:43
char name[80]
Definition: mod_format.h:44
static struct ast_format_def ilbc_f
Definition: format_ilbc.c:127
#define SEEK_FORCECUR
Definition: file.h:51
static int load_module(void)
Definition: format_ilbc.c:138
static off_t ilbc_tell(struct ast_filestream *fs)
Definition: format_ilbc.c:121
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
Definition: format_ilbc.c:64
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
Data structure associated with a single frame of data.
static int ilbc_trunc(struct ast_filestream *fs)
Definition: format_ilbc.c:104
union ast_frame::@263 data
struct ast_format * format
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define min(a, b)
Definition: f2c.h:197
static int ilbc_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_ilbc.c:78
static struct ast_frame * ilbc_read(struct ast_filestream *s, int *whennext)
Definition: format_ilbc.c:46
Media Format Cache API.
struct ast_format * ast_format_ilbc
Built-in cached ilbc format.
Definition: format_cache.c:126
#define max(a, b)
Definition: f2c.h:198