Dne 30.11.2017 (Ä?et) ob 20:44 +0200 je Tanu Kaskinen napisal(a): > On Tue, 2017-11-14 at 20:22 +0100, Samo PogaÄ?nik wrote: > > > > Using 'cat /run/user/1000/pulse/fifo_output > some-file' upon the > > loaded > > "pipe-sink" module: > > ... > > load-module module-pipe-sink sink_name=pipe_sink format=s16le > > rate=44100 channels=2 > > ..., > > caused audio players (totem, rhytmbox) to run right through played > > music > > files without proper timing. Also, resulting "raw" files weren't > > correct. > > > > I tried to fix that. Initial attempts weren't that successfull, > > unUsing 'cat /run/user/1000/pulse/fifo_output > some-file' upon > > the > > loaded > > "pipe-sink" module: > > ... > > load-module module-pipe-sink sink_name=pipe_sink format=s16le > > rate=44100 channels=2 > > ..., > > caused audio players (totem, rhytmbox) to run right through played > > music > > files without proper timing. Also, resulting "raw" files weren't > > correct. > > > > I tried to fix that. Initial attempts weren't that successfull, > > until i > > merged some of the mechanics from the "null-sink" module. > > > > So, here is the patch of my "pipe-sink" module modification. > > > > Many thanks for considering the proposed change. > > > > Samo > Thanks for the patch! Since this changes the pipe-sink behaviour > rather > drastically, other people using this module might not like this. You > could add a new module argument: "use_system_clock_for_timing". If > the > option is not set, the old behaviour would be used. > > More comments below: > Thank you for your comments. Regarding your initial observation about the change, would it be sensible to make a separate "pipe-sink" module (i.e. pipe-timed-sink) or do you prefer a single module with additional option? Anyway i'll try to follow your comments about the code as i can and provide another patch in some time. I have some additional remarks below: > > > > --- > > Â src/modules/module-pipe-sink.c | 170 ++++++++++++++++++++++++++--- > > ------------ > > Â 1 file changed, 108 insertions(+), 62 deletions(-) > > > > diff --git a/src/modules/module-pipe-sink.c b/src/modules/module- > > pipe-sink.c > > index 8396a63..ca600e9 100644 > > --- a/src/modules/module-pipe-sink.c > > +++ b/src/modules/module-pipe-sink.c > > @@ -34,6 +34,9 @@ > > Â #endif > > Â > > Â #include <pulse/xmalloc.h> > > +#include <pulse/timeval.h> > > +#include <pulse/util.h> > > +#include <pulse/rtclock.h> > > Â > > Â #include <pulsecore/core-error.h> > > Â #include <pulsecore/sink.h> > > @@ -82,6 +85,8 @@ struct userdata { > > Â Â Â Â Â pa_rtpoll_item *rtpoll_item; > > Â > > Â Â Â Â Â int write_type; > > +Â Â Â Â pa_usec_t block_usec; > > +Â Â Â Â pa_usec_t timestamp; > > Â }; > > Â > > Â static const char* const valid_modargs[] = { > > @@ -99,20 +104,21 @@ static int sink_process_msg(pa_msgobject *o, > > int code, void *data, int64_t offse > > Â Â Â Â Â struct userdata *u = PA_SINK(o)->userdata; > > Â > > Â Â Â Â Â switch (code) { > > +Â Â Â Â Â Â Â Â case PA_SINK_MESSAGE_SET_STATE: > > Â > > -Â Â Â Â Â Â Â Â case PA_SINK_MESSAGE_GET_LATENCY: { > > -Â Â Â Â Â Â Â Â Â Â Â Â size_t n = 0; > > +Â Â Â Â Â Â Â Â Â Â Â Â if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED || > > pa_sink_get_state(u->sink) == PA_SINK_INIT) { > > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING || > > PA_PTR_TO_UINT(data) == PA_SINK_IDLE) > > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â u->timestamp = pa_rtclock_now(); > > +Â Â Â Â Â Â Â Â Â Â Â Â } > > Â > > -#ifdef FIONREAD > > -Â Â Â Â Â Â Â Â Â Â Â Â int l; > > +Â Â Â Â Â Â Â Â Â Â Â Â break; > > Â > > -Â Â Â Â Â Â Â Â Â Â Â Â if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0) > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â n = (size_t) l; > > -#endif > > +Â Â Â Â Â Â Â Â case PA_SINK_MESSAGE_GET_LATENCY: { > > +Â Â Â Â Â Â Â Â Â Â Â Â pa_usec_t now; > > Â > > -Â Â Â Â Â Â Â Â Â Â Â Â n += u->memchunk.length; > > +Â Â Â Â Â Â Â Â Â Â Â Â now = pa_rtclock_now(); > > +Â Â Â Â Â Â Â Â Â Â Â Â *((int64_t*) data) = (int64_t)u->timestamp - > > (int64_t)now; > > Â > > -Â Â Â Â Â Â Â Â Â Â Â Â *((int64_t*) data) = pa_bytes_to_usec(n, &u->sink- > > >sample_spec); > > Â Â Â Â Â Â Â Â Â Â Â Â Â return 0; > > Â Â Â Â Â Â Â Â Â } > > Â Â Â Â Â } > > @@ -120,47 +126,93 @@ static int sink_process_msg(pa_msgobject *o, > > int code, void *data, int64_t offse > > Â Â Â Â Â return pa_sink_process_msg(o, code, data, offset, chunk); > > Â } > > Â > > -static int process_render(struct userdata *u) { > > +static void sink_update_requested_latency_cb(pa_sink *s) { > > +Â Â Â Â struct userdata *u; > > +Â Â Â Â size_t nbytes; > > + > > +Â Â Â Â pa_sink_assert_ref(s); > > +Â Â Â Â pa_assert_se(u = s->userdata); > > + > > +Â Â Â Â u->block_usec = > > pa_sink_get_requested_latency_within_thread(s); > > + > > +Â Â Â Â if (u->block_usec == (pa_usec_t) -1) > > +Â Â Â Â Â Â Â Â u->block_usec = s->thread_info.max_latency; > > + > > +Â Â Â Â nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec); > > +Â Â Â Â pa_sink_set_max_rewind_within_thread(s, nbytes); > > +Â Â Â Â pa_sink_set_max_request_within_thread(s, nbytes); > > +} > > + > > +static void process_rewind(struct userdata *u, pa_usec_t now) { > > +Â Â Â Â size_t rewind_nbytes, in_buffer; > > +Â Â Â Â pa_usec_t delay; > > + > > Â Â Â Â Â pa_assert(u); > > Â > > -Â Â Â Â if (u->memchunk.length <= 0) > > -Â Â Â Â Â Â Â Â pa_sink_render(u->sink, u->buffer_size, &u->memchunk); > > +Â Â Â Â rewind_nbytes = u->sink->thread_info.rewind_nbytes; > > Â > > -Â Â Â Â pa_assert(u->memchunk.length > 0); > > +Â Â Â Â if (!PA_SINK_IS_OPENED(u->sink->thread_info.state) || > > rewind_nbytes <= 0) > > +Â Â Â Â Â Â Â Â goto do_nothing; > > Â > > -Â Â Â Â for (;;) { > > +Â Â Â Â pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) > > rewind_nbytes); > > + > > +Â Â Â Â if (u->timestamp <= now) > > +Â Â Â Â Â Â Â Â goto do_nothing; > > + > > +Â Â Â Â delay = u->timestamp - now; > > +Â Â Â Â in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec); > > + > > +Â Â Â Â if (in_buffer <= 0) > > +Â Â Â Â Â Â Â Â goto do_nothing; > > + > > +Â Â Â Â if (rewind_nbytes > in_buffer) > > +Â Â Â Â Â Â Â Â rewind_nbytes = in_buffer; > > + > > +Â Â Â Â pa_sink_process_rewind(u->sink, rewind_nbytes); > > +Â Â Â Â u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink- > > >sample_spec); > > + > > +Â Â Â Â pa_log_debug("Rewound %lu bytes.", (unsigned long) > > rewind_nbytes); > > +Â Â Â Â return; > Since this is copied from the null sink, this doesn't actually do any > real rewinding. Rewinding means discarding previously buffered audio. > If you don't have anything buffered that you could discard, then you > shouldn't pretend to be rewinding. > Ok, i'll have to better understand this "null-sink" mechanics. > > > > + > > +do_nothing: > > + > > +Â Â Â Â pa_sink_process_rewind(u->sink, 0); > > +} > > + > > +static void process_render(struct userdata *u, pa_usec_t now) { > > +Â Â Â Â size_t ate = 0; > > + > > +Â Â Â Â pa_assert(u); > > + > > +Â Â Â Â /* This is the configured latency. Sink inputs connected to us > > +Â Â Â Â might not have a single frame more than the maxrequest value > > +Â Â Â Â queued. Hence: at maximum read this many bytes from the sink > > +Â Â Â Â inputs. */ > > + > > +Â Â Â Â /* Fill the buffer up the latency size */ > > +Â Â Â Â while (u->timestamp < now + u->block_usec) { > > Â Â Â Â Â Â Â Â Â ssize_t l; > > Â Â Â Â Â Â Â Â Â void *p; > > Â > > +Â Â Â Â Â Â Â Â pa_sink_render(u->sink, u->sink->thread_info.max_request, > > &u->memchunk); > > + > > +Â Â Â Â Â Â Â Â pa_assert(u->memchunk.length > 0); > > + > > Â Â Â Â Â Â Â Â Â p = pa_memblock_acquire(u->memchunk.memblock); > > Â Â Â Â Â Â Â Â Â l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u- > > >memchunk.length, &u->write_type); > > Â Â Â Â Â Â Â Â Â pa_memblock_release(u->memchunk.memblock); > > Â > > Â Â Â Â Â Â Â Â Â pa_assert(l != 0); > > Â > > -Â Â Â Â Â Â Â Â if (l < 0) { > > - > > -Â Â Â Â Â Â Â Â Â Â Â Â if (errno == EINTR) > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â continue; > > -Â Â Â Â Â Â Â Â Â Â Â Â else if (errno == EAGAIN) > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return 0; > > -Â Â Â Â Â Â Â Â Â Â Â Â else { > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pa_log("Faied to write data to FIFO: %s", > > pa_cstrerror(errno)); > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return -1; > > -Â Â Â Â Â Â Â Â Â Â Â Â } > You shouldn't just remove error handling. Also, if pa_write() writes > only part of the rendered audio, you probably shouldn't just drop the > unwritten data, or at least the drop-outs should be logged. > I agree about logging about drop-outs in some limited way, since that may be a constant situation when the pipe is full and there is no reader. Regarding this topic, i also have a test implementation of self- draining the pipe before each first write into the pipe after entering the "running" state. If there is no real reader, each "start play" flushes all potentially old data first. What is your opinion about that? > > > > - > > -Â Â Â Â Â Â Â Â } else { > > +Â Â Â Â Â Â Â Â pa_memblock_unref(u->memchunk.memblock); > > Â > > -Â Â Â Â Â Â Â Â Â Â Â Â u->memchunk.index += (size_t) l; > > -Â Â Â Â Â Â Â Â Â Â Â Â u->memchunk.length -= (size_t) l; > > +/*Â Â Â Â Â Â Â Â Â pa_log_debug("Ate %lu bytes.", (unsigned long) > > chunk.length); */ > This comment provides no value. > Ok. > > > > +Â Â Â Â Â Â Â Â u->timestamp += pa_bytes_to_usec(u->memchunk.length, &u- > > >sink->sample_spec); > > Â > > -Â Â Â Â Â Â Â Â Â Â Â Â if (u->memchunk.length <= 0) { > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pa_memblock_unref(u->memchunk.memblock); > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pa_memchunk_reset(&u->memchunk); > > -Â Â Â Â Â Â Â Â Â Â Â Â } > > -Â Â Â Â Â Â Â Â } > > +Â Â Â Â Â Â Â Â ate += u->memchunk.length; > > Â > > -Â Â Â Â Â Â Â Â return 0; > > +Â Â Â Â Â Â Â Â if (ate >= u->sink->thread_info.max_request) > > +Â Â Â Â Â Â Â Â Â Â Â Â break; > > Â Â Â Â Â } > > Â } > > Â > > @@ -173,40 +225,33 @@ static void thread_func(void *userdata) { > > Â > > Â Â Â Â Â pa_thread_mq_install(&u->thread_mq); > > Â > > +Â Â Â Â u->timestamp = pa_rtclock_now(); > > + > > Â Â Â Â Â for (;;) { > > -Â Â Â Â Â Â Â Â struct pollfd *pollfd; > > +Â Â Â Â Â Â Â Â pa_usec_t now = 0; > > Â Â Â Â Â Â Â Â Â int ret; > > Â > > -Â Â Â Â Â Â Â Â pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); > > +Â Â Â Â Â Â Â Â if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) > > +Â Â Â Â Â Â Â Â Â Â Â Â now = pa_rtclock_now(); > > Â > > Â Â Â Â Â Â Â Â Â if (PA_UNLIKELY(u->sink->thread_info.rewind_requested)) > > -Â Â Â Â Â Â Â Â Â Â Â Â pa_sink_process_rewind(u->sink, 0); > > +Â Â Â Â Â Â Â Â Â Â Â Â process_rewind(u, now); > > Â > > -Â Â Â Â Â Â Â Â /* Render some data and write it to the fifo */ > > +Â Â Â Â Â Â Â Â /* Render some data and drop it immediately */ > This isn't the null sink, the data isn't dropped. > Yp. > > > > Â Â Â Â Â Â Â Â Â if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) { > > -Â Â Â Â Â Â Â Â Â Â Â Â if (pollfd->revents) { > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (process_render(u) < 0) > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto fail; > > +Â Â Â Â Â Â Â Â Â Â Â Â if (u->timestamp <= now) > > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â process_render(u, now); > > Â > > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pollfd->revents = 0; > > -Â Â Â Â Â Â Â Â Â Â Â Â } > > -Â Â Â Â Â Â Â Â } > > +Â Â Â Â Â Â Â Â Â Â Â Â pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp); > > +Â Â Â Â Â Â Â Â } else > > +Â Â Â Â Â Â Â Â Â Â Â Â pa_rtpoll_set_timer_disabled(u->rtpoll); > > Â > > Â Â Â Â Â Â Â Â Â /* Hmm, nothing to do. Let's sleep */ > > -Â Â Â Â Â Â Â Â pollfd->events = (short) (u->sink->thread_info.state == > > PA_SINK_RUNNING ? POLLOUT : 0); > > - > > Â Â Â Â Â Â Â Â Â if ((ret = pa_rtpoll_run(u->rtpoll)) < 0) > > Â Â Â Â Â Â Â Â Â Â Â Â Â goto fail; > > Â > > Â Â Â Â Â Â Â Â Â if (ret == 0) > > Â Â Â Â Â Â Â Â Â Â Â Â Â goto finish; > > - > > -Â Â Â Â Â Â Â Â pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); > > - > > -Â Â Â Â Â Â Â Â if (pollfd->revents & ~POLLOUT) { > > -Â Â Â Â Â Â Â Â Â Â Â Â pa_log("FIFO shutdown."); > > -Â Â Â Â Â Â Â Â Â Â Â Â goto fail; > > -Â Â Â Â Â Â Â Â } > > Â Â Â Â Â } > > Â > > Â fail: > > @@ -248,19 +293,16 @@ int pa__init(pa_module *m) { > > Â Â Â Â Â m->userdata = u; > > Â Â Â Â Â pa_memchunk_reset(&u->memchunk); > > Â Â Â Â Â u->rtpoll = pa_rtpoll_new(); > > - > > -Â Â Â Â if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u- > > >rtpoll) < 0) { > > -Â Â Â Â Â Â Â Â pa_log("pa_thread_mq_init() failed."); > > -Â Â Â Â Â Â Â Â goto fail; > > -Â Â Â Â } > > - > > +Â Â Â Â pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u- > > >rtpoll); > Why did you remove the error handling? > I removed this check by mistake, while testing on an older version:( > > > > Â Â Â Â Â u->write_type = 0; > > Â > > Â Â Â Â Â u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", > > DEFAULT_FILE_NAME)); > > Â > > Â Â Â Â Â if (mkfifo(u->filename, 0666) < 0) { > > +Â Â Â Â Â Â Â Â int errno_save = errno; > > Â Â Â Â Â Â Â Â Â pa_log("mkfifo('%s'): %s", u->filename, > > pa_cstrerror(errno)); > > -Â Â Â Â Â Â Â Â goto fail; > > +Â Â Â Â Â Â Â Â if (errno_save != EEXIST) > > +Â Â Â Â Â Â Â Â Â Â goto fail; > This change is not related to the timing changes. If you want to > allow > using an already existing file, that should be in a separate patch. > Ok. > Also, please use 4 spaces for each level of indentation. > Sure. > > > > Â Â Â Â Â } > > Â Â Â Â Â if ((u->fd = pa_open_cloexec(u->filename, O_RDWR, 0)) < 0) { > > Â Â Â Â Â Â Â Â Â pa_log("open('%s'): %s", u->filename, > > pa_cstrerror(errno)); > > @@ -294,7 +336,7 @@ int pa__init(pa_module *m) { > > Â Â Â Â Â Â Â Â Â goto fail; > > Â Â Â Â Â } > > Â > > -Â Â Â Â u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY); > > +Â Â Â Â u->sink = pa_sink_new(m->core, &data, > > PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY); > > Â Â Â Â Â pa_sink_new_data_done(&data); > > Â > > Â Â Â Â Â if (!u->sink) { > > @@ -303,14 +345,16 @@ int pa__init(pa_module *m) { > > Â Â Â Â Â } > > Â > > Â Â Â Â Â u->sink->parent.process_msg = sink_process_msg; > > +Â Â Â Â u->sink->update_requested_latency = > > sink_update_requested_latency_cb; > > Â Â Â Â Â u->sink->userdata = u; > > Â > > Â Â Â Â Â pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); > > Â Â Â Â Â pa_sink_set_rtpoll(u->sink, u->rtpoll); > > Â > > Â Â Â Â Â u->buffer_size = pa_frame_align(pa_pipe_buf(u->fd), &u->sink- > > >sample_spec); > > +Â Â Â Â u->block_usec = pa_bytes_to_usec(u->buffer_size, &u->sink- > > >sample_spec); > > +Â Â Â Â pa_sink_set_max_rewind(u->sink, u->buffer_size); > > Â Â Â Â Â pa_sink_set_max_request(u->sink, u->buffer_size); > > -Â Â Â Â pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(u- > > >buffer_size, &u->sink->sample_spec)); > > Â > > Â Â Â Â Â u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, > > PA_RTPOLL_NEVER, 1); > > Â Â Â Â Â pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); > > @@ -322,6 +366,8 @@ int pa__init(pa_module *m) { > > Â Â Â Â Â Â Â Â Â goto fail; > > Â Â Â Â Â } > > Â > > +Â Â Â Â pa_sink_set_latency_range(u->sink, 0, u->block_usec); > Nitpicking: I think this would fit better where the > pa_sink_set_fixed_latency() call used to be. > Ok. -- regards, Samo