Asterisk - The Open Source Telephony Project GIT-master-3dae2cf
Macros | Functions | Variables
format_gsm.c File Reference

Save to raw, headerless GSM data. More...

#include "asterisk.h"
#include "asterisk/mod_format.h"
#include "asterisk/module.h"
#include "asterisk/endian.h"
#include "asterisk/format_cache.h"
#include "msgsm.h"
Include dependency graph for format_gsm.c:

Go to the source code of this file.

Macros

#define GSM_FRAME_SIZE   33
 
#define GSM_SAMPLES   160
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static struct ast_framegsm_read (struct ast_filestream *s, int *whennext)
 
static int gsm_seek (struct ast_filestream *fs, off_t sample_offset, int whence)
 
static off_t gsm_tell (struct ast_filestream *fs)
 
static int gsm_trunc (struct ast_filestream *fs)
 
static int gsm_write (struct ast_filestream *fs, struct ast_frame *f)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Raw GSM data" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_format_def gsm_f
 
static const char gsm_silence []
 

Detailed Description

Save to raw, headerless GSM data.

Definition in file format_gsm.c.

Macro Definition Documentation

◆ GSM_FRAME_SIZE

#define GSM_FRAME_SIZE   33

Definition at line 43 of file format_gsm.c.

◆ GSM_SAMPLES

#define GSM_SAMPLES   160

Definition at line 44 of file format_gsm.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 203 of file format_gsm.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 203 of file format_gsm.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 203 of file format_gsm.c.

◆ gsm_read()

static struct ast_frame * gsm_read ( struct ast_filestream s,
int *  whennext 
)
static

Definition at line 54 of file format_gsm.c.

55{
56 size_t res;
57
59 if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
60 if (res) {
61 ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
63 strerror(errno));
64 }
65 return NULL;
66 }
67 *whennext = s->fr.samples = GSM_SAMPLES;
68 return &s->fr;
69}
#define ast_log
Definition: astobj2.c:42
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define GSM_FRAME_SIZE
Definition: format_gsm.c:43
#define GSM_SAMPLES
Definition: format_gsm.c:44
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
#define LOG_WARNING
int errno
#define NULL
Definition: resample.c:96
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
struct ast_format * format
struct ast_frame_subclass subclass
union ast_frame::@226 data

References ast_format_get_name(), AST_FRAME_SET_BUFFER, AST_FRIENDLY_OFFSET, ast_log, ast_filestream::buf, ast_frame::data, errno, ast_filestream::f, ast_frame_subclass::format, ast_filestream::fr, GSM_FRAME_SIZE, GSM_SAMPLES, LOG_WARNING, NULL, ast_frame::ptr, ast_frame::samples, and ast_frame::subclass.

◆ gsm_seek()

static int gsm_seek ( struct ast_filestream fs,
off_t  sample_offset,
int  whence 
)
static

Definition at line 100 of file format_gsm.c.

101{
102 off_t offset = 0, min = 0, cur, max, distance;
103
104 if ((cur = ftello(fs->f)) < 0) {
105 ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
106 return -1;
107 }
108
109 if (fseeko(fs->f, 0, SEEK_END) < 0) {
110 ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
111 return -1;
112 }
113
114 if ((max = ftello(fs->f)) < 0) {
115 ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
116 return -1;
117 }
118
119 /* have to fudge to frame here, so not fully to sample */
120 distance = (sample_offset / GSM_SAMPLES) * GSM_FRAME_SIZE;
121 if (whence == SEEK_SET) {
122 offset = distance;
123 } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
124 offset = distance + cur;
125 } else if (whence == SEEK_END) {
126 offset = max - distance;
127 }
128
129 /* Always protect against seeking past the begining. */
130 offset = (offset < min)?min:offset;
131 if (whence != SEEK_FORCECUR) {
132 offset = (offset > max)?max:offset;
133 } else if (offset > max) {
134 int i;
135 fseeko(fs->f, 0, SEEK_END);
136 for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
137 if (fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f) != GSM_FRAME_SIZE) {
138 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
139 }
140 }
141 }
142 return fseeko(fs->f, offset, SEEK_SET);
143}
#define min(a, b)
Definition: f2c.h:197
#define max(a, b)
Definition: f2c.h:198
#define SEEK_FORCECUR
Definition: file.h:51
static const char gsm_silence[]
Definition: format_gsm.c:48
#define AST_LOG_WARNING

References ast_log, AST_LOG_WARNING, errno, ast_filestream::f, GSM_FRAME_SIZE, GSM_SAMPLES, gsm_silence, LOG_WARNING, max, min, ast_frame::offset, and SEEK_FORCECUR.

◆ gsm_tell()

static off_t gsm_tell ( struct ast_filestream fs)
static

Definition at line 162 of file format_gsm.c.

163{
164 off_t offset = ftello(fs->f);
165
166 if (offset < 0) {
167 ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno));
168 return 0;
169 }
170
171 return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES;
172}

References ast_log, AST_LOG_WARNING, errno, ast_filestream::f, GSM_FRAME_SIZE, GSM_SAMPLES, and ast_frame::offset.

◆ gsm_trunc()

static int gsm_trunc ( struct ast_filestream fs)
static

Definition at line 145 of file format_gsm.c.

146{
147 int fd;
148 off_t cur;
149
150 if ((fd = fileno(fs->f)) < 0) {
151 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm filestream %p: %s\n", fs, strerror(errno));
152 return -1;
153 }
154 if ((cur = ftello(fs->f)) < 0) {
155 ast_log(AST_LOG_WARNING, "Unable to determine current position in gsm filestream %p: %s\n", fs, strerror(errno));
156 return -1;
157 }
158 /* Truncate file to current length */
159 return ftruncate(fd, cur);
160}

References ast_log, AST_LOG_WARNING, errno, and ast_filestream::f.

◆ gsm_write()

static int gsm_write ( struct ast_filestream fs,
struct ast_frame f 
)
static

Definition at line 71 of file format_gsm.c.

72{
73 int res;
74 unsigned char gsm[2*GSM_FRAME_SIZE];
75
76 if (!(f->datalen % 65)) {
77 /* This is in MSGSM format, need to be converted */
78 int len=0;
79 while(len < f->datalen) {
80 conv65(f->data.ptr + len, gsm);
81 if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
82 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
83 return -1;
84 }
85 len += 65;
86 }
87 } else {
88 if (f->datalen % GSM_FRAME_SIZE) {
89 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
90 return -1;
91 }
92 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
93 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
94 return -1;
95 }
96 }
97 return 0;
98}
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static void conv65(wav_byte *c, gsm_byte *d)
Definition: msgsm.h:457

References ast_log, conv65(), ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, GSM_FRAME_SIZE, len(), LOG_WARNING, and ast_frame::ptr.

◆ load_module()

static int load_module ( void  )
static

Definition at line 185 of file format_gsm.c.

186{
191}
struct ast_format * ast_format_gsm
Built-in cached gsm format.
Definition: format_cache.c:96
static struct ast_format_def gsm_f
Definition: format_gsm.c:174
#define ast_format_def_register(f)
Definition: mod_format.h:136
@ 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
struct ast_format * format
Definition: mod_format.h:48

References ast_format_def_register, ast_format_gsm, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_format_def::format, and gsm_f.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 193 of file format_gsm.c.

194{
196}
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
char name[80]
Definition: mod_format.h:44

References ast_format_def_unregister(), gsm_f, and ast_format_def::name.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Raw GSM data" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND }
static

Definition at line 203 of file format_gsm.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 203 of file format_gsm.c.

◆ gsm_f

struct ast_format_def gsm_f
static

Definition at line 174 of file format_gsm.c.

Referenced by load_module(), and unload_module().

◆ gsm_silence

const char gsm_silence[]
static
Initial value:
=
{0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
,0x92,0x49,0x24}

Definition at line 48 of file format_gsm.c.

Referenced by gsm_seek().