Asterisk - The Open Source Telephony Project GIT-master-a358458
format_ilbc.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Brian K. West <brian@bkw.org>
5 *
6 * Copyright (C) 1999 - 2005, Digium, Inc.
7 *
8 * Mark Spencer <markster@digium.com>
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"
38
39/* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
40
41/* Portions of the conversion code are by guido@sienanet.it */
42
43#define ILBC_BUF_SIZE 50 /* One Real iLBC Frame */
44#define ILBC_SAMPLES 240
45
46static 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
64static 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
78static 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) {
96 }
97 /* protect against seeking beyond begining. */
99 if (fseeko(fs->f, offset, SEEK_SET) < 0)
100 return -1;
101 return 0;
102}
103
104static 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
121static off_t ilbc_tell(struct ast_filestream *fs)
122{
123 off_t offset = ftello(fs->f);
125}
126
127static 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,
136};
137
138static int load_module(void)
139{
144}
145
146static int unload_module(void)
147{
149}
150
152 .support_level = AST_MODULE_SUPPORT_CORE,
153 .load = load_module,
154 .unload = unload_module,
155 .load_pri = AST_MODPRI_APP_DEPEND
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
Asterisk architecture endianess compatibility definitions.
#define min(a, b)
Definition: f2c.h:197
#define max(a, b)
Definition: f2c.h:198
#define SEEK_FORCECUR
Definition: file.h:51
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
Media Format Cache API.
struct ast_format * ast_format_ilbc
Built-in cached ilbc format.
Definition: format_cache.c:121
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
#define ILBC_BUF_SIZE
Definition: format_ilbc.c:43
static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
Definition: format_ilbc.c:64
#define ILBC_SAMPLES
Definition: format_ilbc.c:44
static int ilbc_trunc(struct ast_filestream *fs)
Definition: format_ilbc.c:104
static off_t ilbc_tell(struct ast_filestream *fs)
Definition: format_ilbc.c:121
static int load_module(void)
Definition: format_ilbc.c:138
static int unload_module(void)
Definition: format_ilbc.c:146
static struct ast_format_def ilbc_f
Definition: format_ilbc.c:127
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
#define AST_LOG_WARNING
#define LOG_WARNING
int errno
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_format_def_register(f)
Definition: mod_format.h:136
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODPRI_APP_DEPEND
Definition: module.h:328
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define NULL
Definition: resample.c:96
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
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
Each supported file format is described by the following structure.
Definition: mod_format.h:43
char name[80]
Definition: mod_format.h:44
struct ast_format * format
Definition: mod_format.h:48
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@226 data