コード例 #1
0
static int wav_write(struct ast_filestream *s, struct ast_frame *f)
{
	int len;
	int size;
	struct wavg_desc *fs = (struct wavg_desc *)s->_private;

	if (f->frametype != AST_FRAME_VOICE) {
		ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
		return -1;
	}
	if (f->subclass.format.id != AST_FORMAT_GSM) {
		ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(&f->subclass.format));
		return -1;
	}
	/* XXX this might fail... if the input is a multiple of MSGSM_FRAME_SIZE
	 * we assume it is already in the correct format.
	 */
	if (!(f->datalen % MSGSM_FRAME_SIZE)) {
		size = MSGSM_FRAME_SIZE;
		fs->secondhalf = 0;
	} else {
		size = GSM_FRAME_SIZE;
	}
	for (len = 0; len < f->datalen ; len += size) {
		int res;
		unsigned char *src, msdata[MSGSM_FRAME_SIZE];
		if (fs->secondhalf) {	/* second half of raw gsm to be converted */
			memcpy(s->buf + GSM_FRAME_SIZE, f->data.ptr + len, GSM_FRAME_SIZE);
			conv66((unsigned char *) s->buf, msdata);
			src = msdata;
			fs->secondhalf = 0;
		} else if (size == GSM_FRAME_SIZE) {	/* first half of raw gsm */
			memcpy(s->buf, f->data.ptr + len, GSM_FRAME_SIZE);
			src = NULL;	/* nothing to write */
			fs->secondhalf = 1;
		} else {	/* raw msgsm data */
			src = f->data.ptr + len;
		}
		if (src && (res = fwrite(src, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
			ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
			return -1;
		}
		update_header(s->f); /* XXX inefficient! */
	}
	return 0;
}
コード例 #2
0
static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
{
	int res;
	char msdata[66];
	int len =0;
	int alreadyms=0;
	if (f->frametype != AST_FRAME_VOICE) {
		ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
		return -1;
	}
	if (f->subclass != AST_FORMAT_GSM) {
		ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
		return -1;
	}
	if (!(f->datalen % 65)) 
		alreadyms = 1;
	while(len < f->datalen) {
		if (alreadyms) {
			fs->secondhalf = 0;
			if ((res = write(fs->fd, f->data + len, 65)) != 65) {
				ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
				return -1;
			}
			fs->bytes += 65;
			update_header(fs->fd);
			len += 65;
		} else {
			if (fs->secondhalf) {
				memcpy(fs->gsm + 33, f->data + len, 33);
				conv66(fs->gsm, msdata);
				if ((res = write(fs->fd, msdata, 65)) != 65) {
					ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
					return -1;
				}
				fs->bytes += 65;
				update_header(fs->fd);
			} else {
				/* Copy the data and do nothing */
				memcpy(fs->gsm, f->data + len, 33);
			}
			fs->secondhalf = !fs->secondhalf;
			len += 33;
		}
	}
	return 0;
}