Merged revisions 115511 via svnmerge from
[asterisk-bristuff.git] / formats / format_gsm.c
blobf997af1190e7944422ffb47b3e5c396a1a915c8b
1 /*
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.
19 /*! \file
21 * \brief Save to raw, headerless GSM data.
22 * \arg File name extension: gsm
23 * \ingroup formats
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <unistd.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <string.h>
39 #include "asterisk/lock.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/file.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/sched.h"
44 #include "asterisk/module.h"
45 #include "asterisk/endian.h"
47 #include "msgsm.h"
49 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
51 /* Portions of the conversion code are by guido@sienanet.it */
53 #define GSM_FRAME_SIZE 33
54 #define GSM_SAMPLES 160
56 /* silent gsm frame */
57 /* begin binary data: */
58 char gsm_silence[] = /* 33 */
59 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
60 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
61 ,0x92,0x49,0x24};
62 /* end binary data. size = 33 bytes */
64 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
66 int res;
68 s->fr.frametype = AST_FRAME_VOICE;
69 s->fr.subclass = AST_FORMAT_GSM;
70 AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
71 s->fr.mallocd = 0;
72 if ((res = fread(s->fr.data, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
73 if (res)
74 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
75 return NULL;
77 *whennext = s->fr.samples = GSM_SAMPLES;
78 return &s->fr;
81 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
83 int res;
84 unsigned char gsm[2*GSM_FRAME_SIZE];
86 if (f->frametype != AST_FRAME_VOICE) {
87 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
88 return -1;
90 if (f->subclass != AST_FORMAT_GSM) {
91 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
92 return -1;
94 if (!(f->datalen % 65)) {
95 /* This is in MSGSM format, need to be converted */
96 int len=0;
97 while(len < f->datalen) {
98 conv65(f->data + len, gsm);
99 if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
100 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
101 return -1;
103 len += 65;
105 } else {
106 if (f->datalen % GSM_FRAME_SIZE) {
107 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
108 return -1;
110 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
111 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
112 return -1;
115 return 0;
118 static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
120 off_t offset=0,min,cur,max,distance;
122 min = 0;
123 cur = ftello(fs->f);
124 fseeko(fs->f, 0, SEEK_END);
125 max = ftello(fs->f);
126 /* have to fudge to frame here, so not fully to sample */
127 distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE;
128 if(whence == SEEK_SET)
129 offset = distance;
130 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
131 offset = distance + cur;
132 else if(whence == SEEK_END)
133 offset = max - distance;
134 /* Always protect against seeking past the begining. */
135 offset = (offset < min)?min:offset;
136 if (whence != SEEK_FORCECUR) {
137 offset = (offset > max)?max:offset;
138 } else if (offset > max) {
139 int i;
140 fseeko(fs->f, 0, SEEK_END);
141 for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
142 fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f);
145 return fseeko(fs->f, offset, SEEK_SET);
148 static int gsm_trunc(struct ast_filestream *fs)
150 return ftruncate(fileno(fs->f), ftello(fs->f));
153 static off_t gsm_tell(struct ast_filestream *fs)
155 off_t offset = ftello(fs->f);
156 return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES;
159 static const struct ast_format gsm_f = {
160 .name = "gsm",
161 .exts = "gsm",
162 .format = AST_FORMAT_GSM,
163 .write = gsm_write,
164 .seek = gsm_seek,
165 .trunc = gsm_trunc,
166 .tell = gsm_tell,
167 .read = gsm_read,
168 .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */
171 static int load_module(void)
173 return ast_format_register(&gsm_f);
176 static int unload_module(void)
178 return ast_format_unregister(gsm_f.name);
181 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw GSM data");