| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  | /*! 
 | 
					
						
							|  |  |  |  * \file | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \brief Old-style G.723.1 frame/timestamp format. | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  *  | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \arg Extensions: g723, g723sf | 
					
						
							|  |  |  |  * \ingroup formats | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-07-14 20:13:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  |   | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 00:53:49 +00:00
										 |  |  | #include "asterisk/mod_format.h"
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:12:19 +00:00
										 |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | #define G723_MAX_SIZE 1024
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext) | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 	unsigned short size; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	int delay; | 
					
						
							|  |  |  | 	/* Read the delay for the next packet, and schedule again if necessary */ | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	/* XXX is this ignored ? */ | 
					
						
							| 
									
										
										
										
											2005-10-16 16:12:51 +00:00
										 |  |  | 	if (fread(&delay, 1, 4, s->f) == 4)  | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 		delay = ntohl(delay); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		delay = -1; | 
					
						
							| 
									
										
										
										
											2005-10-16 16:12:51 +00:00
										 |  |  | 	if (fread(&size, 1, 2, s->f) != 2) { | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 		/* Out of data, or the file is no longer valid.  In any case
 | 
					
						
							|  |  |  | 		   go ahead and stop the stream */ | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 	/* Looks like we have a frame to read from here */ | 
					
						
							|  |  |  | 	size = ntohs(size); | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	if (size > G723_MAX_SIZE) { | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Size %d is invalid\n", size); | 
					
						
							|  |  |  | 		/* The file is apparently no longer any good, as we
 | 
					
						
							|  |  |  | 		   shouldn't ever get frames even close to this  | 
					
						
							|  |  |  | 		   size.  */ | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 	/* Read the data into the buffer */ | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	s->fr.frametype = AST_FRAME_VOICE; | 
					
						
							| 
									
										
										
										
											2009-11-04 14:05:12 +00:00
										 |  |  | 	s->fr.subclass.codec = AST_FORMAT_G723_1; | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	s->fr.mallocd = 0; | 
					
						
							| 
									
										
										
										
											2006-04-09 22:31:38 +00:00
										 |  |  | 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size); | 
					
						
							| 
									
										
										
										
											2008-05-22 16:29:54 +00:00
										 |  |  | 	if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != size) { | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno)); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	*whennext = s->fr.samples = 240; | 
					
						
							|  |  |  | 	return &s->fr; | 
					
						
							| 
									
										
										
										
											2003-02-06 22:11:43 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | static int g723_write(struct ast_filestream *s, struct ast_frame *f) | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-09-18 22:02:04 +00:00
										 |  |  | 	uint32_t delay; | 
					
						
							|  |  |  | 	uint16_t size; | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	/* XXX there used to be a check s->fr means a read stream */ | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 	if (f->frametype != AST_FRAME_VOICE) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-04 14:05:12 +00:00
										 |  |  | 	if (f->subclass.codec != AST_FORMAT_G723_1) { | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 	delay = 0; | 
					
						
							| 
									
										
										
										
											2000-07-24 03:28:43 +00:00
										 |  |  | 	if (f->datalen <= 0) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	if ((res = fwrite(&delay, 1, 4, s->f)) != 4) { | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno)); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	size = htons(f->datalen); | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	if ((res = fwrite(&size, 1, 2, s->f)) != 2) { | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno)); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-05-22 16:29:54 +00:00
										 |  |  | 	if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) { | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno)); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	}	 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-20 23:35:12 +00:00
										 |  |  | static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence) | 
					
						
							| 
									
										
										
										
											2003-02-06 22:11:43 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int g723_trunc(struct ast_filestream *fs) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 	/* Truncate file to current length */ | 
					
						
							| 
									
										
										
										
											2006-02-20 23:35:12 +00:00
										 |  |  | 	if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) | 
					
						
							| 
									
										
										
										
											2004-12-29 12:12:59 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2003-02-06 22:11:43 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-20 23:35:12 +00:00
										 |  |  | static off_t g723_tell(struct ast_filestream *fs) | 
					
						
							| 
									
										
										
										
											2003-02-06 22:11:43 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | static const struct ast_format g723_1_f = { | 
					
						
							|  |  |  | 	.name = "g723sf", | 
					
						
							|  |  |  | 	.exts = "g723|g723sf", | 
					
						
							|  |  |  | 	.format = AST_FORMAT_G723_1, | 
					
						
							|  |  |  | 	.write = g723_write, | 
					
						
							|  |  |  | 	.seek =	g723_seek, | 
					
						
							|  |  |  | 	.trunc = g723_trunc, | 
					
						
							|  |  |  | 	.tell =	g723_tell, | 
					
						
							|  |  |  | 	.read =	g723_read, | 
					
						
							|  |  |  | 	.buf_size = G723_MAX_SIZE + AST_FRIENDLY_OFFSET, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-10-31 19:24:29 +00:00
										 |  |  | 	if (ast_format_register(&g723_1_f)) | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_FAILURE; | 
					
						
							|  |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-04 12:59:25 +00:00
										 |  |  | 	return ast_format_unregister(g723_1_f.name); | 
					
						
							| 
									
										
										
										
											2010-07-20 19:35:02 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1999-12-05 01:29:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-26 03:27:06 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "G.723.1 Simple Timestamp File Format", | 
					
						
							|  |  |  | 	.load = load_module, | 
					
						
							|  |  |  | 	.unload = unload_module, | 
					
						
							|  |  |  | 	.load_pri = AST_MODPRI_APP_DEPEND | 
					
						
							|  |  |  | ); |