From 58735a9cf911eb09ba830a505d1dc2c9d130ef9e Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 13 Feb 2017 16:48:17 +0100 Subject: [PATCH] audio: Fix time diff to always return positive numbers Change-Id: I62f835f95fc1d1a671cfdb2e6e6ac1758524235f --- audio/audio_hw.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/audio/audio_hw.c b/audio/audio_hw.c index ff855d5..ea69f25 100644 --- a/audio/audio_hw.c +++ b/audio/audio_hw.c @@ -248,7 +248,6 @@ static const struct string_to_enum out_channels_name_to_enum_table[] = { struct timespec time_spec_diff(struct timespec time1, struct timespec time0) { struct timespec ret; int xsec = 0; - int sign = 1; if (time0.tv_nsec > time1.tv_nsec) { xsec = (int) ((time0.tv_nsec - time1.tv_nsec) / (1E9 + 1)); @@ -262,14 +261,8 @@ struct timespec time_spec_diff(struct timespec time1, struct timespec time0) { time0.tv_sec -= xsec; } - ret.tv_sec = time1.tv_sec - time0.tv_sec; - ret.tv_nsec = time1.tv_nsec - time0.tv_nsec; - - if (time1.tv_sec < time0.tv_sec) { - sign = -1; - } - - ret.tv_sec = ret.tv_sec * sign; + ret.tv_sec = abs(time1.tv_sec - time0.tv_sec); + ret.tv_nsec = abs(time1.tv_nsec - time0.tv_nsec); return ret; } @@ -817,8 +810,8 @@ static int enable_snd_device(struct audio_device *adev, #ifdef DSP_POWEROFF_DELAY clock_gettime(CLOCK_MONOTONIC, &activation_time); - elapsed_time = time_spec_diff(mixer_card->dsp_poweroff_time, - activation_time); + elapsed_time = time_spec_diff(activation_time, + mixer_card->dsp_poweroff_time); if (elapsed_time.tv_sec == 0) { long elapsed_usec = elapsed_time.tv_nsec / 1000; -- 2.20.1