Asterisk - The Open Source Telephony Project  18.5.0
format_g729.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <[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 Save to raw, headerless G729 data.
22  * \note This is not an encoder/decoder. The codec for g729 is only
23  * available with a commercial license from Digium, due to patent
24  * restrictions. Check http://www.digium.com for information.
25  * \arg Extensions: g729
26  * \ingroup formats
27  */
28 
29 /*** MODULEINFO
30  <support_level>core</support_level>
31  ***/
32 
33 #include "asterisk.h"
34 
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/endian.h"
38 #include "asterisk/format_cache.h"
39 
40 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
41 
42 /* Portions of the conversion code are by [email protected] */
43 
44 #define BUF_SIZE 20 /* two G729 frames */
45 #define G729A_SAMPLES 160
46 
47 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
48 {
49  size_t res;
50 
51  /* Send a frame from the file to the appropriate channel */
54  if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
55  if (res && res != 10) /* XXX what for ? */ {
56  ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
58  strerror(errno));
59  }
60  return NULL;
61  }
62  *whennext = s->fr.samples;
63  return &s->fr;
64 }
65 
66 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
67 {
68  int res;
69 
70  if (f->datalen % 10) {
71  ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
72  return -1;
73  }
74  if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
75  ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
76  return -1;
77  }
78  return 0;
79 }
80 
81 static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
82 {
83  long bytes;
84  off_t min,cur,max,offset=0;
85  min = 0;
86  cur = ftello(fs->f);
87  fseeko(fs->f, 0, SEEK_END);
88  max = ftello(fs->f);
89 
90  bytes = BUF_SIZE * (sample_offset / G729A_SAMPLES);
91  if (whence == SEEK_SET)
92  offset = bytes;
93  else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
94  offset = cur + bytes;
95  else if (whence == SEEK_END)
96  offset = max - bytes;
97  if (whence != SEEK_FORCECUR) {
98  offset = (offset > max)?max:offset;
99  }
100  /* protect against seeking beyond begining. */
101  offset = (offset < min)?min:offset;
102  if (fseeko(fs->f, offset, SEEK_SET) < 0)
103  return -1;
104  return 0;
105 }
106 
107 static int g729_trunc(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 g729 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 g729 filestream %p: %s\n", fs, strerror(errno));
118  return -1;
119  }
120  /* Truncate file to current length */
121  return ftruncate(fd, cur);
122 }
123 
124 static off_t g729_tell(struct ast_filestream *fs)
125 {
126  off_t offset = ftello(fs->f);
127  return (offset/BUF_SIZE)*G729A_SAMPLES;
128 }
129 
130 static struct ast_format_def g729_f = {
131  .name = "g729",
132  .exts = "g729",
133  .write = g729_write,
134  .seek = g729_seek,
135  .trunc = g729_trunc,
136  .tell = g729_tell,
137  .read = g729_read,
138  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
139 };
140 
141 static int load_module(void)
142 {
143  g729_f.format = ast_format_g729;
144  if (ast_format_def_register(&g729_f))
147 }
148 
149 static int unload_module(void)
150 {
151  return ast_format_def_unregister(g729_f.name);
152 }
153 
155  .support_level = AST_MODULE_SUPPORT_CORE,
156  .load = load_module,
157  .unload = unload_module,
158  .load_pri = AST_MODPRI_APP_DEPEND
159 );
#define BUF_SIZE
Definition: format_g729.c:44
Asterisk main include file. File version handling, generic pbx functions.
#define G729A_SAMPLES
Definition: format_g729.c:45
static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_g729.c:81
#define LOG_WARNING
Definition: logger.h:274
#define AST_LOG_WARNING
Definition: logger.h:279
static struct ast_frame * g729_read(struct ast_filestream *s, int *whennext)
Definition: format_g729.c:47
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
static int unload_module(void)
Definition: format_g729.c:149
#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 struct ast_format_def g729_f
Definition: format_g729.c:130
static int load_module(void)
Definition: format_g729.c:141
int errno
static int g729_trunc(struct ast_filestream *fs)
Definition: format_g729.c:107
char name[80]
Definition: mod_format.h:44
static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
Definition: format_g729.c:66
#define SEEK_FORCECUR
Definition: file.h:51
static off_t g729_tell(struct ast_filestream *fs)
Definition: format_g729.c:124
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 * ast_format_g729
Built-in cached g729 format.
Definition: format_cache.c:156
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
Media Format Cache API.
#define max(a, b)
Definition: f2c.h:198