From aede739778b584f8cb1840de0f679c591b6d905a Mon Sep 17 00:00:00 2001 From: eyalhasson <eyal@kolhl.com> Date: Tue, 22 Jan 2019 17:24:23 +0200 Subject: [PATCH] format_g726: add support for seeking Added support for the seek function in format_g726 so playback can start from anywhere. Before the fix, playback of g726 files always started from the beginning. ASTERISK-28246 Change-Id: I626235bc4642df1479050d3d06828412603a9b40 --- formats/format_g726.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/formats/format_g726.c b/formats/format_g726.c index bb769bc60c..934f9c315c 100644 --- a/formats/format_g726.c +++ b/formats/format_g726.c @@ -155,7 +155,38 @@ static int g726_write(struct ast_filestream *s, struct ast_frame *f) static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence) { - return -1; + off_t offset = 0, min = 0, cur, max, distance; + + if ((cur = ftello(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine current position in g726 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + + if (fseeko(fs->f, 0, SEEK_END) < 0) { + ast_log(AST_LOG_WARNING, "Unable to seek to end of g726 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + + if ((max = ftello(fs->f)) < 0) { + ast_log(AST_LOG_WARNING, "Unable to determine max position in g726 filestream %p: %s\n", fs, strerror(errno)); + return -1; + } + + /* have to fudge to frame here, so not fully to sample */ + distance = sample_offset / 2; + if (whence == SEEK_SET) { + offset = distance; + } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) { + offset = distance + cur; + } else if (whence == SEEK_END) { + offset = max - distance; + } + + if (whence != SEEK_FORCECUR) { + offset = offset > max ? max : offset; + offset = offset < min ? min : offset; + } + return fseeko(fs->f, offset, SEEK_SET); } static int g726_trunc(struct ast_filestream *fs) @@ -165,7 +196,7 @@ static int g726_trunc(struct ast_filestream *fs) static off_t g726_tell(struct ast_filestream *fs) { - return -1; + return ftello(fs->f) << 1; } static struct ast_format_def f[] = { -- GitLab