summaryrefslogtreecommitdiffstats
path: root/main/asterisk/ASTERISK-18995.patch
blob: cd144847beca3f3b73814f560d16f68580a42517 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
--- /dev/null	2011-11-29 09:02:40.279581283 +0200
+++ b/formats/format_ogg_speex.c	2011-12-08 15:57:12.000000000 +0200
@@ -0,0 +1,355 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2011, Timo Teräs
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief OGG/Speex streams.
+ * \arg File name extension: spx
+ * \ingroup formats
+ */
+
+/*** MODULEINFO
+	<depend>speex</depend>
+	<depend>ogg</depend>
+	<support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/mod_format.h"
+#include "asterisk/module.h"
+
+#include <speex/speex_header.h>
+#include <ogg/ogg.h>
+
+#define BLOCK_SIZE	4096		/* buffer size for feeding OGG routines */
+#define	BUF_SIZE	200
+
+struct speex_desc {	/* format specific parameters */
+	/* structures for handling the Ogg container */
+	ogg_sync_state oy;
+	ogg_stream_state os;
+	ogg_page og;
+	ogg_packet op;
+
+	int format_id;
+	int serialno;
+
+	/*! \brief Indicates whether an End of Stream condition has been detected. */
+	int eos;
+};
+
+static int read_packet(struct ast_filestream *fs)
+{
+	struct speex_desc *s = (struct speex_desc *)fs->_private;
+	char *buffer;
+	int result;
+	size_t bytes;
+
+	while (1) {
+		/* Get one packet */
+		result = ogg_stream_packetout(&s->os, &s->op);
+		if (result > 0) {
+			if (s->op.bytes>=5 && !memcmp(s->op.packet, "Speex", 5))
+				s->serialno = s->os.serialno;
+			if (s->serialno == -1 || s->os.serialno != s->serialno)
+				continue;
+			return 0;
+		}
+
+		if (result < 0)
+			ast_log(LOG_WARNING,
+				"Corrupt or missing data at this page position; continuing...\n");
+
+		/* No more packets left in the current page... */
+		if (s->eos) {
+			/* No more pages left in the stream */
+			return -1;
+		}
+
+		while (!s->eos) {
+			/* See if OGG has any pages in it's internal buffers */
+			result = ogg_sync_pageout(&s->oy, &s->og);
+			if (result > 0) {
+				/* Read all streams. */
+				if (ogg_page_serialno(&s->og) != s->os.serialno)
+					ogg_stream_reset_serialno(&s->os, ogg_page_serialno(&s->og));
+				/* Yes, OGG has more pages in it's internal buffers,
+				   add the page to the stream state */
+				result = ogg_stream_pagein(&s->os, &s->og);
+				if (result == 0) {
+					/* Yes, got a new,valid page */
+					if (ogg_page_eos(&s->og) &&
+					    ogg_page_serialno(&s->og) == s->serialno)
+						s->eos = 1;
+					break;
+				}
+				ast_log(LOG_WARNING,
+					"Invalid page in the bitstream; continuing...\n");
+			}
+
+			if (result < 0)
+				ast_log(LOG_WARNING,
+					"Corrupt or missing data in bitstream; continuing...\n");
+
+			/* No, we need to read more data from the file descrptor */
+			/* get a buffer from OGG to read the data into */
+			buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
+			bytes = fread(buffer, 1, BLOCK_SIZE, fs->f);
+			ogg_sync_wrote(&s->oy, bytes);
+			if (bytes == 0)
+				s->eos = 1;
+		}
+	}
+}
+
+/*!
+ * \brief Create a new OGG/Speex filestream and set it up for reading.
+ * \param fs File that points to on disk storage of the OGG/Speex data.
+ * \param expected_rate The expected Speex format (sampling rate).
+ * \return The new filestream.
+ */
+static int ogg_speex_open(struct ast_filestream *fs, int format_id, int expected_rate)
+{
+	char *buffer;
+	size_t bytes;
+	struct speex_desc *s = (struct speex_desc *)fs->_private;
+	SpeexHeader *hdr = NULL;
+	int i, result;
+
+	s->format_id = format_id;
+	s->serialno = -1;
+	ogg_sync_init(&s->oy);
+
+	buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
+	bytes = fread(buffer, 1, BLOCK_SIZE, fs->f);
+	ogg_sync_wrote(&s->oy, bytes);
+
+	result = ogg_sync_pageout(&s->oy, &s->og);
+	if (result != 1) {
+		if(bytes < BLOCK_SIZE) {
+			ast_log(LOG_ERROR, "Run out of data...\n");
+		} else {
+			ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
+		}
+		ogg_sync_clear(&s->oy);
+		return -1;
+	}
+
+	ogg_stream_init(&s->os, ogg_page_serialno(&s->og));
+	if (ogg_stream_pagein(&s->os, &s->og) < 0) { 
+		ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
+		goto error;
+	}
+
+	if (read_packet(fs) < 0) {
+		ast_log(LOG_ERROR, "Error reading initial header packet.\n");
+		goto error;
+	}
+
+	hdr = speex_packet_to_header((char*)s->op.packet, s->op.bytes);
+	if (memcmp(hdr->speex_string, "Speex   ", 8)) {
+		ast_log(LOG_ERROR, "OGG container does not contain Speex audio!\n");
+		goto error;
+	}
+	if (hdr->frames_per_packet != 1) {
+		ast_log(LOG_ERROR, "Only one frame-per-packet OGG/Speex files are currently supported!\n");
+		goto error;
+	}
+	if (hdr->nb_channels != 1) {
+		ast_log(LOG_ERROR, "Only monophonic OGG/Speex files are currently supported!\n");
+		goto error;
+	}
+	if (hdr->rate != expected_rate) {
+		ast_log(LOG_ERROR, "Unexpected sampling rate (%d != %d)!\n",
+			hdr->rate, expected_rate);
+		goto error;
+	}
+
+	/* this packet is the comment */
+	if (read_packet(fs) < 0) {
+		ast_log(LOG_ERROR, "Error reading comment packet.\n");
+		goto error;
+	}
+	for (i = 0; i < hdr->extra_headers; i++) {
+		if (read_packet(fs) < 0) {
+			ast_log(LOG_ERROR, "Error reading extra header packet %d.\n", i+1);
+			goto error;
+		}
+	}
+	free(hdr);
+
+	return 0;
+error:
+	if (hdr)
+		free(hdr);
+	ogg_stream_clear(&s->os);
+	ogg_sync_clear(&s->oy);
+	return -1;
+}
+
+/*!
+ * \brief Close a OGG/Speex filestream.
+ * \param fs A OGG/Speex filestream.
+ */
+static void ogg_speex_close(struct ast_filestream *fs)
+{
+	struct speex_desc *s = (struct speex_desc *)fs->_private;
+
+	ogg_stream_clear(&s->os);
+	ogg_sync_clear(&s->oy);
+}
+
+/*!
+ * \brief Read a frame full of audio data from the filestream.
+ * \param fs The filestream.
+ * \param whennext Number of sample times to schedule the next call.
+ * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
+ */
+static struct ast_frame *ogg_speex_read(struct ast_filestream *fs,
+					 int *whennext)
+{
+	struct speex_desc *s = (struct speex_desc *)fs->_private;
+
+	if (read_packet(fs) < 0)
+		return NULL;
+
+	fs->fr.frametype = AST_FRAME_VOICE;
+	ast_format_set(&fs->fr.subclass.format, s->format_id, 0);
+	fs->fr.mallocd = 0;
+	AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
+	memcpy(fs->fr.data.ptr, s->op.packet, s->op.bytes);
+	fs->fr.datalen = s->op.bytes;
+	fs->fr.samples = *whennext = ast_codec_get_samples(&fs->fr);
+
+	return &fs->fr;
+}
+
+/*!
+ * \brief Trucate an OGG/Speex filestream.
+ * \param s The filestream to truncate.
+ * \return 0 on success, -1 on failure.
+ */
+
+static int ogg_speex_trunc(struct ast_filestream *s)
+{
+	ast_log(LOG_WARNING, "Truncation is not supported on OGG/Speex streams!\n");
+	return -1;
+}
+
+/*!
+ * \brief Seek to a specific position in an OGG/Speex filestream.
+ * \param s The filestream to truncate.
+ * \param sample_offset New position for the filestream, measured in 8KHz samples.
+ * \param whence Location to measure 
+ * \return 0 on success, -1 on failure.
+ */
+static int ogg_speex_seek(struct ast_filestream *s, off_t sample_offset, int whence)
+{
+	ast_log(LOG_WARNING, "Seeking is not supported on OGG/Speex streams!\n");
+	return -1;
+}
+
+static off_t ogg_speex_tell(struct ast_filestream *s)
+{
+	ast_log(LOG_WARNING, "Telling is not supported on OGG/Speex streams!\n");
+	return -1;
+}
+
+static int ogg_speex_open_nb(struct ast_filestream *fs) 
+{
+	return ogg_speex_open(fs, AST_FORMAT_SPEEX, 8000);
+}
+
+static struct ast_format_def speex_f = {
+	.name = "ogg_speex",
+	.exts = "spx",
+	.open = ogg_speex_open_nb,
+	.seek = ogg_speex_seek,
+	.trunc = ogg_speex_trunc,
+	.tell = ogg_speex_tell,
+	.read = ogg_speex_read,
+	.close = ogg_speex_close,
+	.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
+	.desc_size = sizeof(struct speex_desc),
+};
+
+static int ogg_speex_open_wb(struct ast_filestream *fs) 
+{
+	return ogg_speex_open(fs, AST_FORMAT_SPEEX16, 16000);
+}
+
+static struct ast_format_def speex16_f = {
+	.name = "ogg_speex16",
+	.exts = "spx16",
+	.open = ogg_speex_open_wb,
+	.seek = ogg_speex_seek,
+	.trunc = ogg_speex_trunc,
+	.tell = ogg_speex_tell,
+	.read = ogg_speex_read,
+	.close = ogg_speex_close,
+	.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
+	.desc_size = sizeof(struct speex_desc),
+};
+
+static int ogg_speex_open_uwb(struct ast_filestream *fs) 
+{
+	return ogg_speex_open(fs, AST_FORMAT_SPEEX32, 32000);
+}
+
+static struct ast_format_def speex32_f = {
+	.name = "ogg_speex32",
+	.exts = "spx32",
+	.open = ogg_speex_open_uwb,
+	.seek = ogg_speex_seek,
+	.trunc = ogg_speex_trunc,
+	.tell = ogg_speex_tell,
+	.read = ogg_speex_read,
+	.close = ogg_speex_close,
+	.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
+	.desc_size = sizeof(struct speex_desc),
+};
+
+static int load_module(void)
+{
+	ast_format_set(&speex_f.format, AST_FORMAT_SPEEX, 0);
+	ast_format_set(&speex16_f.format, AST_FORMAT_SPEEX16, 0);
+	ast_format_set(&speex32_f.format, AST_FORMAT_SPEEX32, 0);
+
+	if (ast_format_def_register(&speex_f) ||
+	    ast_format_def_register(&speex16_f) ||
+	    ast_format_def_register(&speex32_f))
+		return AST_MODULE_LOAD_FAILURE;
+
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	int res = 0;
+	res |= ast_format_def_unregister(speex_f.name);
+	res |= ast_format_def_unregister(speex16_f.name);
+	res |= ast_format_def_unregister(speex32_f.name);
+	return res;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "OGG/Speex audio",
+	.load = load_module,
+	.unload = unload_module,
+	.load_pri = AST_MODPRI_APP_DEPEND
+);