[Asterisk-code-review] format pcm: Correct behavior of fseek and ftell for G.722 (asterisk[master])

Sean Bright asteriskteam at digium.com
Wed Apr 18 13:43:02 CDT 2018


Sean Bright has uploaded this change for review. ( https://gerrit.asterisk.org/8817


Change subject: format_pcm: Correct behavior of fseek and ftell for G.722
......................................................................

format_pcm: Correct behavior of fseek and ftell for G.722

There are twice as many samples in the same number of bytes, so redefine
some of the G.722 format functions in terms of their PCM counterparts.

Change-Id: I6a8c7352624b930a5f2d9e4857f75283fa5dd9f9
---
M formats/format_pcm.c
1 file changed, 25 insertions(+), 16 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/17/8817/1

diff --git a/formats/format_pcm.c b/formats/format_pcm.c
index 35612c9..4e846d7 100644
--- a/formats/format_pcm.c
+++ b/formats/format_pcm.c
@@ -91,10 +91,7 @@
 		return NULL;
 	}
 	s->fr.datalen = res;
-	if (ast_format_cmp(s->fmt->format, ast_format_g722) == AST_FORMAT_CMP_EQUAL)
-		*whennext = s->fr.samples = res * 2;
-	else
-		*whennext = s->fr.samples = res;
+	*whennext = s->fr.samples = res;
 	return &s->fr;
 }
 
@@ -410,15 +407,10 @@
 static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
 {
 	off_t min, max, cur;
-	long offset = 0, bytes;
+	long offset = 0;
 	struct au_desc *desc = fs->_private;
 
 	min = desc->hdr_size;
-
-	if (ast_format_cmp(fs->fmt->format, ast_format_g722) == AST_FORMAT_CMP_EQUAL)
-		bytes = sample_offset / 2;
-	else
-		bytes = sample_offset;
 
 	if ((cur = ftello(fs->f)) < 0) {
 		ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
@@ -436,11 +428,11 @@
 	}
 
 	if (whence == SEEK_SET)
-		offset = bytes + min;
+		offset = sample_offset + min;
 	else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
-		offset = bytes + cur;
+		offset = sample_offset + cur;
 	else if (whence == SEEK_END)
-		offset = max - bytes;
+		offset = max - sample_offset;
 
 	if (whence != SEEK_FORCECUR) {
 		offset = (offset > max) ? max : offset;
@@ -479,6 +471,23 @@
 	return offset - desc->hdr_size;
 }
 
+static struct ast_frame *g722_read(struct ast_filestream *s, int *whennext)
+{
+	struct ast_frame *f = pcm_read(s, whennext);
+	*whennext = s->fr.samples = (*whennext * 2);
+	return f;
+}
+
+static int g722_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
+{
+	return pcm_seek(fs, sample_offset / 2, whence);
+}
+
+static off_t g722_tell(struct ast_filestream *fs)
+{
+	return pcm_tell(fs) * 2;
+}
+
 static struct ast_format_def alaw_f = {
 	.name = "alaw",
 	.exts = "alaw|al|alw",
@@ -510,10 +519,10 @@
 	.name = "g722",
 	.exts = "g722",
 	.write = pcm_write,
-	.seek = pcm_seek,
+	.seek = g722_seek,
 	.trunc = pcm_trunc,
-	.tell = pcm_tell,
-	.read = pcm_read,
+	.tell = g722_tell,
+	.read = g722_read,
 	.buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
 };
 

-- 
To view, visit https://gerrit.asterisk.org/8817
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6a8c7352624b930a5f2d9e4857f75283fa5dd9f9
Gerrit-Change-Number: 8817
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180418/9154db6b/attachment-0001.html>


More information about the asterisk-code-review mailing list