]> git.alsa-project.org Git - tinycompress.git/commitdiff
compress_hw_get_hpointer: use SNDRV_COMPRESS_AVAIL64 master
authorGeorge Verhaegen <verhaegen@google.com>
Mon, 21 Jul 2025 15:21:11 +0000 (16:21 +0100)
committerVinod Koul <3178504+vinodkoul@users.noreply.github.com>
Wed, 21 Jan 2026 06:29:57 +0000 (11:59 +0530)
Refactor compress_hw_get_hpointer to use the new SNDRV_COMPRESS_AVAIL64
which benefits from overflow safety.

src/lib/compress_hw.c

index a07fa8dadf56cc05ed20c7e44320779f1b99ac1d..02314cd98e6efc300a70ad4255d3a08bdc6c0909 100644 (file)
@@ -224,25 +224,49 @@ static void compress_hw_close(void *data)
        free(compress);
 }
 
+static void compress_hw_avail64_from_32(struct snd_compr_avail64 *avail64,
+                                        const struct snd_compr_avail *avail32) {
+  avail64->avail = avail32->avail;
+
+  avail64->tstamp.byte_offset = avail32->tstamp.byte_offset;
+  avail64->tstamp.copied_total = avail32->tstamp.copied_total;
+  avail64->tstamp.pcm_frames = avail32->tstamp.pcm_frames;
+  avail64->tstamp.pcm_io_frames = avail32->tstamp.pcm_io_frames;
+  avail64->tstamp.sampling_rate = avail32->tstamp.sampling_rate;
+}
+
 static int compress_hw_get_hpointer(void *data,
                unsigned int *avail, struct timespec *tstamp)
 {
        struct compress_hw_data *compress = (struct compress_hw_data *)data;
-       struct snd_compr_avail kavail;
+       struct snd_compr_avail kavail32;
+       struct snd_compr_avail64 kavail64;
        __u64 time;
 
        if (!is_compress_hw_ready(compress))
                return oops(compress, ENODEV, "device not ready");
 
-       if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &kavail))
-               return oops(compress, errno, "cannot get avail");
-       if (0 == kavail.tstamp.sampling_rate)
+       const int version = get_compress_hw_version(compress);
+       if (version <= 0)
+               return -1;
+
+       if (version < SNDRV_PROTOCOL_VERSION(0, 4, 0)) {
+               /* SNDRV_COMPRESS_AVAIL64 not supported, fallback to SNDRV_COMPRESS_AVAIL */
+               if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &kavail32))
+                       return oops(compress, errno, "cannot get avail");
+               compress_hw_avail64_from_32(&kavail64, &kavail32);
+       } else {
+               if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL64, &kavail64))
+                       return oops(compress, errno, "cannot get avail64");
+       }
+
+       if (0 == kavail64.tstamp.sampling_rate)
                return oops(compress, ENODATA, "sample rate unknown");
-       *avail = (unsigned int)kavail.avail;
-       time = kavail.tstamp.pcm_io_frames / kavail.tstamp.sampling_rate;
+       *avail = (unsigned int)kavail64.avail;
+       time = kavail64.tstamp.pcm_io_frames / kavail64.tstamp.sampling_rate;
        tstamp->tv_sec = time;
-       time = kavail.tstamp.pcm_io_frames % kavail.tstamp.sampling_rate;
-       tstamp->tv_nsec = time * 1000000000 / kavail.tstamp.sampling_rate;
+       time = kavail64.tstamp.pcm_io_frames % kavail64.tstamp.sampling_rate;
+       tstamp->tv_nsec = time * 1000000000 / kavail64.tstamp.sampling_rate;
        return 0;
 }