Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


format_g723.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 <markster@digium.com>
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 /*!
20  * \file
21  *
22  * \brief Old-style G.723.1 frame/timestamp format.
23  *
24  * \arg Extensions: g723, g723sf
25  * \ingroup formats
26  */
27 
28 /*** MODULEINFO
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 364578 $")
35 
36 #include "asterisk/mod_format.h"
37 #include "asterisk/module.h"
38 
39 #define G723_MAX_SIZE 1024
40 
41 static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
42 {
43  unsigned short size;
44  int res;
45  int delay;
46  /* Read the delay for the next packet, and schedule again if necessary */
47  /* XXX is this ignored ? */
48  if (fread(&delay, 1, 4, s->f) == 4)
49  delay = ntohl(delay);
50  else
51  delay = -1;
52  if (fread(&size, 1, 2, s->f) != 2) {
53  /* Out of data, or the file is no longer valid. In any case
54  go ahead and stop the stream */
55  return NULL;
56  }
57  /* Looks like we have a frame to read from here */
58  size = ntohs(size);
59  if (size > G723_MAX_SIZE) {
60  ast_log(LOG_WARNING, "Size %d is invalid\n", size);
61  /* The file is apparently no longer any good, as we
62  shouldn't ever get frames even close to this
63  size. */
64  return NULL;
65  }
66  /* Read the data into the buffer */
69  s->fr.mallocd = 0;
71  if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != size) {
72  ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
73  return NULL;
74  }
75  *whennext = s->fr.samples = 240;
76  return &s->fr;
77 }
78 
79 static int g723_write(struct ast_filestream *s, struct ast_frame *f)
80 {
81  uint32_t delay;
82  uint16_t size;
83  int res;
84  /* XXX there used to be a check s->fr means a read stream */
85  if (f->frametype != AST_FRAME_VOICE) {
86  ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
87  return -1;
88  }
89  if (f->subclass.codec != AST_FORMAT_G723_1) {
90  ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
91  return -1;
92  }
93  delay = 0;
94  if (f->datalen <= 0) {
95  ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
96  return 0;
97  }
98  if ((res = fwrite(&delay, 1, 4, s->f)) != 4) {
99  ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
100  return -1;
101  }
102  size = htons(f->datalen);
103  if ((res = fwrite(&size, 1, 2, s->f)) != 2) {
104  ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
105  return -1;
106  }
107  if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
108  ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
109  return -1;
110  }
111  return 0;
112 }
113 
114 static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
115 {
116  return -1;
117 }
118 
119 static int g723_trunc(struct ast_filestream *fs)
120 {
121  int fd;
122  off_t cur;
123 
124  if ((fd = fileno(fs->f)) < 0) {
125  ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g723 filestream %p: %s\n", fs, strerror(errno));
126  return -1;
127  }
128  if ((cur = ftello(fs->f)) < 0) {
129  ast_log(AST_LOG_WARNING, "Unable to determine current position in g723 filestream %p: %s\n", fs, strerror(errno));
130  return -1;
131  }
132  /* Truncate file to current length */
133  return ftruncate(fd, cur);
134 }
135 
136 static off_t g723_tell(struct ast_filestream *fs)
137 {
138  return -1;
139 }
140 
141 static const struct ast_format g723_1_f = {
142  .name = "g723sf",
143  .exts = "g723|g723sf",
144  .format = AST_FORMAT_G723_1,
145  .write = g723_write,
146  .seek = g723_seek,
147  .trunc = g723_trunc,
148  .tell = g723_tell,
149  .read = g723_read,
150  .buf_size = G723_MAX_SIZE + AST_FRIENDLY_OFFSET,
151 };
152 
153 static int load_module(void)
154 {
155  if (ast_format_register(&g723_1_f))
158 }
159 
160 static int unload_module(void)
161 {
162  return ast_format_unregister(g723_1_f.name);
163 }
164 
165 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "G.723.1 Simple Timestamp File Format",
166  .load = load_module,
167  .unload = unload_module,
168  .load_pri = AST_MODPRI_APP_DEPEND
169 );
union ast_frame_subclass subclass
Definition: frame.h:146
Asterisk main include file. File version handling, generic pbx functions.
#define ast_format_register(f)
Definition: mod_format.h:131
static int g723_trunc(struct ast_filestream *fs)
Definition: format_g723.c:119
#define AST_FORMAT_G723_1
Definition: frame.h:242
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
#define G723_MAX_SIZE
Definition: format_g723.c:39
#define AST_LOG_WARNING
Definition: logger.h:149
static int g723_write(struct ast_filestream *s, struct ast_frame *f)
Definition: format_g723.c:79
Each supported file format is described by the following structure.
Definition: mod_format.h:43
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
format_t codec
Definition: frame.h:137
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
Definition: frame.h:183
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
int datalen
Definition: frame.h:148
struct ast_frame fr
Definition: mod_format.h:117
static off_t g723_tell(struct ast_filestream *fs)
Definition: format_g723.c:136
static int unload_module(void)
Definition: format_g723.c:160
int ast_format_unregister(const char *name)
Unregisters a file format.
Definition: file.c:104
char name[80]
Definition: mod_format.h:44
static struct ast_frame * g723_read(struct ast_filestream *s, int *whennext)
Definition: format_g723.c:41
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
static struct ast_format f[]
Definition: format_g726.c:181
static int load_module(void)
Definition: format_g723.c:153
int mallocd
Definition: frame.h:152
static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_g723.c:114
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
Data structure associated with a single frame of data.
Definition: frame.h:142
static struct ast_format g723_1_f
Definition: format_g723.c:141
enum ast_frame_type frametype
Definition: frame.h:144
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
union ast_frame::@172 data
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150