Skip to contentSkip to conversation

add pcm_file support in MMAP read access mode

PATCH In review Adam Miartus 2 participants 4 messages May 23, 2019 – May 24, 2019

lore
Share & tools
lore
#0 lore raw

cover add pcm_file support in MMAP read access mode

adds support for overwritting input buffer in MMAP access mode for in pcm_file plugin

new api pcm_fast_ops callback mmap_begin is introduced to ease the operation needed to overwrite mmaped buffer. this allows to modify only the next continuous part of data in ring buffer.

Adam Miartus (2):
pcm: add mmap_begin callback to snd_pcm_fast_ops_t api
pcm_file: add infile read support for mmap mode

 src/pcm/pcm.c       |  6 ++++++
 src/pcm/pcm_file.c  | 31 +++++++++++++++++++++++++++++++
 src/pcm/pcm_local.h |  1 +
 3 files changed, 38 insertions(+)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/2 pcm: add mmap_begin callback to snd_pcm_fast_ops_t api

main motivation for adding the callback is to use it to enable operation on mmaped buffer before user access for pcm_file plugin

support for MMAP read access with masking by data from input file is not implemented for pcm_file plugin, by adding this callback implementing such feature can be done by rewriting next continuous portion of buffer on each mmap_begin call

plugins like softvol use pcm_plugin interface and overwrite the buffer by looping around it in avail_update callback, this patch hopes to simplify the task by adding new api callback, removing the need for rewriting pcm_file (to use pcm_plugin callbacks) and careful checking when looping around whole mmaped buffer

---
 src/pcm/pcm.c       | 6 ++++++
 src/pcm/pcm_local.h | 1 +
 2 files changed, 7 insertions(+)
src/pcm/pcm.c +6 -0
index 3a71d79..323926e 100644
@@ -7129,7 +7129,13 @@ int __snd_pcm_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas,
 	snd_pcm_uframes_t f;
 	snd_pcm_uframes_t avail;
 	const snd_pcm_channel_area_t *xareas;
+
 	assert(pcm && areas && offset && frames);
+
+	if (pcm->fast_ops->mmap_begin)
+		return pcm->fast_ops->mmap_begin(pcm->fast_op_arg, areas, offset, frames);
+
+	/* fallback for plugins that do not specify new callback */
 	xareas = snd_pcm_mmap_areas(pcm);
 	if (xareas == NULL)
 		return -EBADFD;
src/pcm/pcm_local.h +1 -0
index d52229d..d5726eb 100644
@@ -184,6 +184,7 @@ typedef struct {
 	int (*poll_descriptors)(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int space); /* locked */
 	int (*poll_revents)(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int nfds, unsigned short *revents); /* locked */
 	int (*may_wait_for_avail_min)(snd_pcm_t *pcm, snd_pcm_uframes_t avail);
+	int (*mmap_begin)(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames); /* locked */
 } snd_pcm_fast_ops_t;
 
 struct _snd_pcm {
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/2 pcm_file: add infile read support for mmap mode

In reply to Adam Miartus

mmap_begin callback is used to copy data from input file to mmaped buffer

guard for corner use of api (multiple mmap_begin calls by user) is introduced to check if next continuous buffer was already overwritten

buffer is overwritten with input file data only in case of stream capture

---
 src/pcm/pcm_file.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
src/pcm/pcm_file.c +31 -0
index 8e2c70b..52cc10a 100644
@@ -88,6 +88,7 @@ typedef struct {
 	size_t buffer_bytes;
 	struct wav_fmt wav_header;
 	size_t filelen;
+	char ifmmap_overwritten;
 } snd_pcm_file_t;
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
@@ -630,6 +631,8 @@ static snd_pcm_sframes_t snd_pcm_file_mmap_commit(snd_pcm_t *pcm,
 	const snd_pcm_channel_area_t *areas;
 	snd_pcm_sframes_t result;
 
+	file->ifmmap_overwritten = 0;
+
 	result = snd_pcm_mmap_begin(file->gen.slave, &areas, &ofs, &siz);
 	if (result >= 0) {
 		assert(ofs == offset && siz == size);
@@ -640,6 +643,32 @@ static snd_pcm_sframes_t snd_pcm_file_mmap_commit(snd_pcm_t *pcm,
 	return result;
 }
 
+static int snd_pcm_file_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas,
+	snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames)
+{
+	snd_pcm_file_t *file = pcm->private_data;
+	snd_pcm_channel_area_t areas_if[pcm->channels];
+	snd_pcm_uframes_t frames_if;
+	void *buffer = NULL;
+	int result;
+
+	result = snd_pcm_mmap_begin(file->gen.slave, areas, offset, frames);
+	if (result < 0)
+		return result;
+
+	if (pcm->stream != SND_PCM_STREAM_CAPTURE)
+		return result;
+
+	/* user may run mmap_begin without mmap_commit multiple times in row */
+	if (file->ifmmap_overwritten)
+		return result;
+	file->ifmmap_overwritten = 1;
+
+	snd_pcm_file_areas_read_infile(pcm, *areas, *offset, *frames);
+
+	return result;
+}
+
 static int snd_pcm_file_hw_free(snd_pcm_t *pcm)
 {
 	snd_pcm_file_t *file = pcm->private_data;
@@ -666,6 +695,7 @@ static int snd_pcm_file_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
 	file->wbuf_size = slave->buffer_size * 2;
 	file->wbuf_size_bytes = snd_pcm_frames_to_bytes(slave, file->wbuf_size);
 	file->wbuf_used_bytes = 0;
+	file->ifmmap_overwritten = 0;
 	assert(!file->wbuf);
 	file->wbuf = malloc(file->wbuf_size_bytes);
 	if (file->wbuf == NULL) {
@@ -777,6 +807,7 @@ static const snd_pcm_fast_ops_t snd_pcm_file_fast_ops = {
 	.poll_descriptors = snd_pcm_generic_poll_descriptors,
 	.poll_revents = snd_pcm_generic_poll_revents,
 	.htimestamp = snd_pcm_generic_htimestamp,
+	.mmap_begin = snd_pcm_file_mmap_begin,
 };
 
 /**
Signature · 1 line
2.7.4
#3 lore raw
In reply to Adam Miartus
Quoted 11 lines · Adam Miartus

adds support for overwritting input buffer in MMAP access mode for in pcm_file plugin

new api pcm_fast_ops callback mmap_begin is introduced to ease the operation needed to overwrite mmaped buffer. this allows to modify only the next continuous part of data in ring buffer.

Adam Miartus (2):
pcm: add mmap_begin callback to snd_pcm_fast_ops_t api
pcm_file: add infile read support for mmap mode

OK, now applied both patches. Thanks!

Takashi