]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
format_g726: add support for seeking
authoreyalhasson <eyal@kolhl.com>
Tue, 22 Jan 2019 15:24:23 +0000 (17:24 +0200)
committerSean Bright <sean.bright@gmail.com>
Wed, 23 Jan 2019 14:56:51 +0000 (08:56 -0600)
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

index 8379925c227e4427548e9fb23699fbbc8c787162..0f31db9b87bdefa6d4a93e36f31b4c96421a6842 100644 (file)
@@ -157,7 +157,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)
@@ -167,7 +198,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[] = {