audio: Fix time diff to always return positive numbers
authorAndreas Schneider <asn@cryptomilk.org>
Mon, 13 Feb 2017 15:48:17 +0000 (16:48 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 14 Feb 2017 16:42:14 +0000 (17:42 +0100)
Change-Id: I62f835f95fc1d1a671cfdb2e6e6ac1758524235f

audio/audio_hw.c

index ff855d579e02560ad0f3de16564166467bc3c94c..ea69f255734b2431e8736b889481e532a98b63e1 100644 (file)
@@ -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;