Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Improve capturing from ALSA device
Browse files Browse the repository at this point in the history
Determine number of available frames using snd_pcm_avail_update() function.
Read audio frames on sufficient number of available frames only.
Avoid partially filled buffers.
  • Loading branch information
RoEdAl committed Jun 14, 2024
1 parent 9ef84cc commit 8633dfa
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "at_command.h"
#include "at_queue.h" /* write_all() TODO: move out */
#include "at_read.h"
#include "chan_quectel.h"
#include "helpers.h" /* get_at_clir_value() */

Expand Down Expand Up @@ -369,21 +370,11 @@ static int channel_digit_begin(struct ast_channel* channel, char digit)

static int channel_digit_end(attribute_unused struct ast_channel* channel, attribute_unused char digit, attribute_unused unsigned int duration) { return 0; }

static ssize_t get_iov_total_len(const struct iovec* const iov, int iovcnt)
{
ssize_t len = 0;

for (int i = 0; i < iovcnt; ++i) {
len += iov[i].iov_len;
}
return len;
}

#/* ARCH: move to cpvt level */

static ssize_t iov_write(struct pvt* pvt, int fd, const struct iovec* const iov, int iovcnt)
{
const ssize_t len = get_iov_total_len(iov, iovcnt);
const ssize_t len = (ssize_t)at_get_iov_size_n(iov, iovcnt);
const ssize_t w = writev(fd, iov, iovcnt);

if (w < 0) {
Expand Down Expand Up @@ -568,6 +559,15 @@ static struct ast_frame* channel_read_uac(struct cpvt* cpvt, struct pvt* pvt, si
return NULL;
}

const snd_pcm_sframes_t avail_frames = snd_pcm_avail_update(pvt->icard);
if (avail_frames < 0) {
ast_log(LOG_ERROR, "[%s][ALSA][CAPTURE] Cannot determine available samples: %s\n", PVT_ID(pvt), snd_strerror((int)avail_frames));
return NULL;
} else if (frames < (size_t)avail_frames) {
ast_log(LOG_WARNING, "[%s][ALSA][CAPTURE] Not enough samples: %d/%d\n", PVT_ID(pvt), (int)avail_frames, (int)frames);
return NULL;
}

char* const buf = cpvt->read_buf + AST_FRIENDLY_OFFSET;
const int res = snd_pcm_mmap_readi(pvt->icard, buf, frames);

Expand Down

0 comments on commit 8633dfa

Please sign in to comment.