[pulseaudio-discuss] [PATCH RFCv3 49/51] alsa: Refactor computation of sleep usec

David Henningsson david.henningsson at canonical.com
Fri Nov 7 03:48:44 PST 2014



On 2014-11-05 00:26, Peter Meerwald wrote:
> From: Peter Meerwald <p.meerwald at bct-electronic.com>
>
> avoids duplicate code in mmap_read/write() and unix_read/write()

Looks good, but in the name of optimisation I would have expected these 
to be inlined...?

>
> Signed-off-by: Peter Meerwald <pmeerw at pmeerw.net>
> ---
>   src/modules/alsa/alsa-sink.c   | 57 +++++++++++++++++-------------------------
>   src/modules/alsa/alsa-source.c | 35 +++++++++++++-------------
>   2 files changed, 40 insertions(+), 52 deletions(-)
>
> diff --git a/src/modules/alsa/alsa-sink.c b/src/modules/alsa/alsa-sink.c
> index 0d13716..aab7ee7 100644
> --- a/src/modules/alsa/alsa-sink.c
> +++ b/src/modules/alsa/alsa-sink.c
> @@ -508,10 +508,29 @@ static size_t check_left_to_play(struct userdata *u, size_t n_bytes, bool on_tim
>       return left_to_play;
>   }
>
> +static pa_usec_t compute_sleep(struct userdata *u, size_t left_to_play) {
> +    size_t input_underrun = pa_sink_process_input_underruns(u->sink, left_to_play);
> +    pa_usec_t sleep_usec = 0;
> +
> +    if (u->use_tsched) {
> +        pa_usec_t underrun_sleep = pa_bytes_to_usec_round_up(input_underrun, &u->sink->sample_spec);
> +        sleep_usec = PA_BYTES_TO_USEC(left_to_play, &u->sink->sample_spec);
> +
> +        if (sleep_usec > u->tsched_watermark_usec)
> +            sleep_usec -= u->tsched_watermark_usec;
> +        else
> +            sleep_usec = 0;
> +
> +        sleep_usec = PA_MIN(sleep_usec, underrun_sleep);
> +    }
> +
> +    return sleep_usec;
> +}
> +
>   static int mmap_write(struct userdata *u, pa_usec_t *sleep_usec, bool polled, bool on_timeout) {
>       bool work_done = false;
>       pa_usec_t max_sleep_usec = 0, process_usec = 0;
> -    size_t left_to_play, input_underrun;
> +    size_t left_to_play;
>       size_t sleep_bytes = 0;
>       unsigned j = 0;
>
> @@ -683,22 +702,7 @@ static int mmap_write(struct userdata *u, pa_usec_t *sleep_usec, bool polled, bo
>       }
>
>   done:
> -    input_underrun = pa_sink_process_input_underruns(u->sink, left_to_play);
> -
> -    if (u->use_tsched) {
> -        pa_usec_t underrun_sleep = pa_bytes_to_usec_round_up(input_underrun, &u->sink->sample_spec);
> -
> -        *sleep_usec = PA_BYTES_TO_USEC(left_to_play, &u->sink->sample_spec);
> -        process_usec = u->tsched_watermark_usec;
> -
> -        if (*sleep_usec > process_usec)
> -            *sleep_usec -= process_usec;
> -        else
> -            *sleep_usec = 0;
> -
> -        *sleep_usec = PA_MIN(*sleep_usec, underrun_sleep);
> -    } else
> -        *sleep_usec = 0;
> +    *sleep_usec = compute_sleep(u, left_to_play);
>
>       return work_done ? 1 : 0;
>   }
> @@ -706,7 +710,7 @@ done:
>   static int unix_write(struct userdata *u, pa_usec_t *sleep_usec, bool polled, bool on_timeout) {
>       bool work_done = false;
>       pa_usec_t max_sleep_usec = 0, process_usec = 0;
> -    size_t left_to_play, input_underrun;
> +    size_t left_to_play;
>       unsigned j = 0;
>
>       pa_assert(u);
> @@ -837,22 +841,7 @@ static int unix_write(struct userdata *u, pa_usec_t *sleep_usec, bool polled, bo
>           }
>       }
>
> -    input_underrun = pa_sink_process_input_underruns(u->sink, left_to_play);
> -
> -    if (u->use_tsched) {
> -        pa_usec_t underrun_sleep = pa_bytes_to_usec_round_up(input_underrun, &u->sink->sample_spec);
> -
> -        *sleep_usec = PA_BYTES_TO_USEC(left_to_play, &u->sink->sample_spec);
> -        process_usec = u->tsched_watermark_usec;
> -
> -        if (*sleep_usec > process_usec)
> -            *sleep_usec -= process_usec;
> -        else
> -            *sleep_usec = 0;
> -
> -        *sleep_usec = PA_MIN(*sleep_usec, underrun_sleep);
> -    } else
> -        *sleep_usec = 0;
> +    *sleep_usec = compute_sleep(u, left_to_play);
>
>       return work_done ? 1 : 0;
>   }
> diff --git a/src/modules/alsa/alsa-source.c b/src/modules/alsa/alsa-source.c
> index f82f811..ac88e4b 100644
> --- a/src/modules/alsa/alsa-source.c
> +++ b/src/modules/alsa/alsa-source.c
> @@ -477,6 +477,21 @@ static size_t check_left_to_record(struct userdata *u, size_t n_bytes, bool on_t
>       return left_to_record;
>   }
>
> +static pa_usec_t compute_sleep(struct userdata *u, size_t left_to_record) {
> +    pa_usec_t sleep_usec = 0;
> +
> +    if (u->use_tsched) {
> +        sleep_usec = PA_BYTES_TO_USEC(left_to_record, &u->source->sample_spec);
> +
> +        if (sleep_usec > u->tsched_watermark_usec)
> +            sleep_usec -= u->tsched_watermark_usec;
> +        else
> +            sleep_usec = 0;
> +    }
> +
> +    return sleep_usec;
> +}
> +
>   static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec, bool polled, bool on_timeout) {
>       bool work_done = false;
>       pa_usec_t max_sleep_usec = 0, process_usec = 0;
> @@ -625,15 +640,7 @@ static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec, bool polled, boo
>           }
>       }
>
> -    if (u->use_tsched) {
> -        *sleep_usec = PA_BYTES_TO_USEC(left_to_record, &u->source->sample_spec);
> -        process_usec = u->tsched_watermark_usec;
> -
> -        if (*sleep_usec > process_usec)
> -            *sleep_usec -= process_usec;
> -        else
> -            *sleep_usec = 0;
> -    }
> +    *sleep_usec = compute_sleep(u, left_to_record);
>
>       return work_done ? 1 : 0;
>   }
> @@ -755,15 +762,7 @@ static int unix_read(struct userdata *u, pa_usec_t *sleep_usec, bool polled, boo
>           }
>       }
>
> -    if (u->use_tsched) {
> -        *sleep_usec = PA_BYTES_TO_USEC(left_to_record, &u->source->sample_spec);
> -        process_usec = u->tsched_watermark_usec;
> -
> -        if (*sleep_usec > process_usec)
> -            *sleep_usec -= process_usec;
> -        else
> -            *sleep_usec = 0;
> -    }
> +    *sleep_usec = compute_sleep(u, left_to_record);
>
>       return work_done ? 1 : 0;
>   }
>

-- 
David Henningsson, Canonical Ltd.
https://launchpad.net/~diwic


More information about the pulseaudio-discuss mailing list