2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Save to raw, headerless GSM data.
22 * \arg File name extension: gsm
28 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
30 #include "asterisk/mod_format.h"
31 #include "asterisk/module.h"
32 #include "asterisk/endian.h"
36 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
38 /* Portions of the conversion code are by guido@sienanet.it */
40 #define GSM_FRAME_SIZE 33
41 #define GSM_SAMPLES 160
43 /* silent gsm frame */
44 /* begin binary data: */
45 char gsm_silence
[] = /* 33 */
46 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
47 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
49 /* end binary data. size = 33 bytes */
51 static struct ast_frame
*gsm_read(struct ast_filestream
*s
, int *whennext
)
55 s
->fr
.frametype
= AST_FRAME_VOICE
;
56 s
->fr
.subclass
= AST_FORMAT_GSM
;
57 AST_FRAME_SET_BUFFER(&(s
->fr
), s
->buf
, AST_FRIENDLY_OFFSET
, GSM_FRAME_SIZE
)
59 if ((res
= fread(s
->fr
.data
, 1, GSM_FRAME_SIZE
, s
->f
)) != GSM_FRAME_SIZE
) {
61 ast_log(LOG_WARNING
, "Short read (%d) (%s)!\n", res
, strerror(errno
));
64 *whennext
= s
->fr
.samples
= GSM_SAMPLES
;
68 static int gsm_write(struct ast_filestream
*fs
, struct ast_frame
*f
)
71 unsigned char gsm
[2*GSM_FRAME_SIZE
];
73 if (f
->frametype
!= AST_FRAME_VOICE
) {
74 ast_log(LOG_WARNING
, "Asked to write non-voice frame!\n");
77 if (f
->subclass
!= AST_FORMAT_GSM
) {
78 ast_log(LOG_WARNING
, "Asked to write non-GSM frame (%d)!\n", f
->subclass
);
81 if (!(f
->datalen
% 65)) {
82 /* This is in MSGSM format, need to be converted */
84 while(len
< f
->datalen
) {
85 conv65(f
->data
+ len
, gsm
);
86 if ((res
= fwrite(gsm
, 1, 2*GSM_FRAME_SIZE
, fs
->f
)) != 2*GSM_FRAME_SIZE
) {
87 ast_log(LOG_WARNING
, "Bad write (%d/66): %s\n", res
, strerror(errno
));
93 if (f
->datalen
% GSM_FRAME_SIZE
) {
94 ast_log(LOG_WARNING
, "Invalid data length, %d, should be multiple of 33\n", f
->datalen
);
97 if ((res
= fwrite(f
->data
, 1, f
->datalen
, fs
->f
)) != f
->datalen
) {
98 ast_log(LOG_WARNING
, "Bad write (%d/33): %s\n", res
, strerror(errno
));
105 static int gsm_seek(struct ast_filestream
*fs
, off_t sample_offset
, int whence
)
107 off_t offset
=0,min
,cur
,max
,distance
;
111 fseeko(fs
->f
, 0, SEEK_END
);
113 /* have to fudge to frame here, so not fully to sample */
114 distance
= (sample_offset
/GSM_SAMPLES
) * GSM_FRAME_SIZE
;
115 if(whence
== SEEK_SET
)
117 else if(whence
== SEEK_CUR
|| whence
== SEEK_FORCECUR
)
118 offset
= distance
+ cur
;
119 else if(whence
== SEEK_END
)
120 offset
= max
- distance
;
121 /* Always protect against seeking past the begining. */
122 offset
= (offset
< min
)?min
:offset
;
123 if (whence
!= SEEK_FORCECUR
) {
124 offset
= (offset
> max
)?max
:offset
;
125 } else if (offset
> max
) {
127 fseeko(fs
->f
, 0, SEEK_END
);
128 for (i
=0; i
< (offset
- max
) / GSM_FRAME_SIZE
; i
++) {
129 fwrite(gsm_silence
, 1, GSM_FRAME_SIZE
, fs
->f
);
132 return fseeko(fs
->f
, offset
, SEEK_SET
);
135 static int gsm_trunc(struct ast_filestream
*fs
)
137 return ftruncate(fileno(fs
->f
), ftello(fs
->f
));
140 static off_t
gsm_tell(struct ast_filestream
*fs
)
142 off_t offset
= ftello(fs
->f
);
143 return (offset
/GSM_FRAME_SIZE
)*GSM_SAMPLES
;
146 static const struct ast_format gsm_f
= {
149 .format
= AST_FORMAT_GSM
,
155 .buf_size
= 2*GSM_FRAME_SIZE
+ AST_FRIENDLY_OFFSET
, /* 2 gsm frames */
158 static int load_module(void)
160 if (ast_format_register(&gsm_f
))
161 return AST_MODULE_LOAD_FAILURE
;
162 return AST_MODULE_LOAD_SUCCESS
;
165 static int unload_module(void)
167 return ast_format_unregister(gsm_f
.name
);
170 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Raw GSM data");