Asterisk - The Open Source Telephony Project  18.5.0
format_g719.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2010, Anthony Minessale and Digium, Inc.
5  * Anthony Minessale ([email protected])
6  * Kevin P. Fleming <[email protected]>
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.719 , 64kbps bitrate only
22  * \arg File name extensions: g719
23  * \ingroup formats
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include "asterisk/mod_format.h"
33 #include "asterisk/module.h"
34 #include "asterisk/endian.h"
35 #include "asterisk/format_cache.h"
36 
37 #define BUF_SIZE 160 /* 20 milliseconds == 160 bytes, 960 samples */
38 #define SAMPLES_TO_BYTES(x) ((typeof(x)) x / ((float) 960 / 160))
39 #define BYTES_TO_SAMPLES(x) ((typeof(x)) x * ((float) 960 / 160))
40 
41 static struct ast_frame *g719read(struct ast_filestream *s, int *whennext)
42 {
43  size_t res;
44 
45  /* Send a frame from the file to the appropriate channel */
47  if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
48  if (res) {
49  ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
51  strerror(errno));
52  }
53  return NULL;
54  }
55  *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
56  return &s->fr;
57 }
58 
59 static int g719write(struct ast_filestream *fs, struct ast_frame *f)
60 {
61  int res;
62 
63  if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
64  ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
65  return -1;
66  }
67  return 0;
68 }
69 
70 static int g719seek(struct ast_filestream *fs, off_t sample_offset, int whence)
71 {
72  off_t offset = 0, min = 0, cur, max;
73 
74  sample_offset = SAMPLES_TO_BYTES(sample_offset);
75 
76  if ((cur = ftello(fs->f)) < 0) {
77  ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
78  return -1;
79  }
80 
81  if (fseeko(fs->f, 0, SEEK_END) < 0) {
82  ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
83  return -1;
84  }
85 
86  if ((max = ftello(fs->f)) < 0) {
87  ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
88  return -1;
89  }
90 
91  if (whence == SEEK_SET)
92  offset = sample_offset;
93  else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
94  offset = sample_offset + cur;
95  else if (whence == SEEK_END)
96  offset = max - sample_offset;
97 
98  if (whence != SEEK_FORCECUR)
99  offset = (offset > max) ? max : offset;
100 
101  /* always protect against seeking past begining. */
102  offset = (offset < min) ? min : offset;
103 
104  return fseeko(fs->f, offset, SEEK_SET);
105 }
106 
107 static int g719trunc(struct ast_filestream *fs)
108 {
109  int fd;
110  off_t cur;
111 
112  if ((fd = fileno(fs->f)) < 0) {
113  ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g719 filestream %p: %s\n", fs, strerror(errno));
114  return -1;
115  }
116  if ((cur = ftello(fs->f)) < 0) {
117  ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
118  return -1;
119  }
120  return ftruncate(fd, cur);
121 }
122 
123 static off_t g719tell(struct ast_filestream *fs)
124 {
125  return BYTES_TO_SAMPLES(ftello(fs->f));
126 }
127 
128 static struct ast_format_def g719_f = {
129  .name = "g719",
130  .exts = "g719",
131  .write = g719write,
132  .seek = g719seek,
133  .trunc = g719trunc,
134  .tell = g719tell,
135  .read = g719read,
136  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
137 };
138 
139 static int load_module(void)
140 {
141  g719_f.format = ast_format_g719;
142 
143  if (ast_format_def_register(&g719_f))
146 }
147 
148 static int unload_module(void)
149 {
150  return ast_format_def_unregister(g719_f.name);
151 }
152 
154  .support_level = AST_MODULE_SUPPORT_CORE,
155  .load = load_module,
156  .unload = unload_module,
157  .load_pri = AST_MODPRI_APP_DEPEND
158 );
#define BUF_SIZE
Definition: format_g719.c:37
Asterisk main include file. File version handling, generic pbx functions.
static int g719trunc(struct ast_filestream *fs)
Definition: format_g719.c:107
#define LOG_WARNING
Definition: logger.h:274
#define AST_LOG_WARNING
Definition: logger.h:279
static struct ast_format_def g719_f
Definition: format_g719.c:128
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
static int g719write(struct ast_filestream *fs, struct ast_frame *f)
Definition: format_g719.c:59
#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 "...
struct ast_format * ast_format_g719
Built-in cached g719 format.
Definition: format_cache.c:161
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
#define ast_log
Definition: astobj2.c:42
static int unload_module(void)
Definition: format_g719.c:148
#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)
static int load_module(void)
Definition: format_g719.c:139
int errno
static off_t g719tell(struct ast_filestream *fs)
Definition: format_g719.c:123
char name[80]
Definition: mod_format.h:44
#define SEEK_FORCECUR
Definition: file.h:51
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
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.
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.
static int g719seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_g719.c:70
#define min(a, b)
Definition: f2c.h:197
#define BYTES_TO_SAMPLES(x)
Definition: format_g719.c:39
static struct ast_frame * g719read(struct ast_filestream *s, int *whennext)
Definition: format_g719.c:41
#define SAMPLES_TO_BYTES(x)
Definition: format_g719.c:38
Media Format Cache API.
#define max(a, b)
Definition: f2c.h:198