commit f7f468d5a36b2c2992b4a1828979bd18a444a634
Author: Mark Spieth <mspieth@digivation.com.au>
Date:   Sun Mar 3 08:43:07 2019 +1100

    Use monotonic time for avsync2 when possible

diff --git a/mythtv/libs/libmythtv/mythplayer.cpp b/mythtv/libs/libmythtv/mythplayer.cpp
index 84c903bf1c..837fd5ca2d 100644
--- a/mythtv/libs/libmythtv/mythplayer.cpp
+++ b/mythtv/libs/libmythtv/mythplayer.cpp
@@ -2167,15 +2167,16 @@ void MythPlayer::AVSync(VideoFrame *buffer, bool limit_delay)
     }
 }
 
-static void wait_for_time(int64_t framedue);
-
-void wait_for_time(int64_t framedue)
+void MythPlayer::WaitForTime(int64_t framedue)
 {
-    QDateTime now = QDateTime::currentDateTimeUtc();
-    int64_t unow = now.toMSecsSinceEpoch() * 1000;
+    int64_t unow = m_avTimer.nsecsElapsed() / 1000;
     int64_t delay = framedue - unow;
-    if (delay > 0)
-        QThread::usleep(delay);
+    while (delay > 0)
+    {
+        int64_t stepDelay = std::min(delay, (int64_t)2000);
+        QThread::usleep(stepDelay);
+        delay -= stepDelay;
+    }
 }
 
 #define AVSYNC_MAX_LATE 10000000
@@ -2194,7 +2195,6 @@ void MythPlayer::AVSync2(VideoFrame *buffer)
     bool pause_audio = false;
     int64_t framedue = 0;
     int64_t audio_adjustment = 0;
-    QDateTime now;
     int64_t unow = 0;
     int64_t lateness = 0;
     int64_t playspeed1000 = (float)1000 / play_speed;
@@ -2210,8 +2210,7 @@ void MythPlayer::AVSync2(VideoFrame *buffer)
                 videotimecode = maxtcval;
         }
 
-        now = QDateTime::currentDateTimeUtc();
-        unow = now.toMSecsSinceEpoch() * 1000;
+        unow = m_avTimer.nsecsElapsed() / 1000;
 
         if (!normal_speed || FlagIsSet(kMusicChoice))
         {
@@ -2350,7 +2349,7 @@ void MythPlayer::AVSync2(VideoFrame *buffer)
         // Don't wait for sync if this is a secondary PBP otherwise
         // the primary PBP will become out of sync
         if (!player_ctx->IsPBP() || player_ctx->IsPrimaryPBP())
-            wait_for_time(framedue);
+            WaitForTime(framedue);
         // get time codes for calculating difference next time
         prior_audiotimecode = audio.GetAudioTime();
         videoOutput->Show(ps);
@@ -2381,13 +2380,13 @@ void MythPlayer::AVSync2(VideoFrame *buffer)
             if (!player_ctx->IsPBP() || player_ctx->IsPrimaryPBP())
             {
                 int64_t due = framedue + frame_interval / 2;
-                wait_for_time(due);
+                WaitForTime(due);
             }
             videoOutput->Show(ps);
         }
     }
     else
-        wait_for_time(framedue);
+        WaitForTime(framedue);
 
     LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
         QString("A/V timecodes audio=%1 video=%2 frameinterval=%3 "
diff --git a/mythtv/libs/libmythtv/mythplayer.h b/mythtv/libs/libmythtv/mythplayer.h
index 4b295af28f..0a54a25ca3 100644
--- a/mythtv/libs/libmythtv/mythplayer.h
+++ b/mythtv/libs/libmythtv/mythplayer.h
@@ -625,6 +625,7 @@ class MTV_PUBLIC MythPlayer
     int64_t AVSyncGetAudiotime(void);
     void  SetFrameInterval(FrameScanType scan, double speed);
     void  FallbackDeint(void);
+    void WaitForTime(int64_t framedue);
 
     // Private LiveTV stuff
     void  SwitchToProgram(void);
@@ -711,6 +712,7 @@ class MTV_PUBLIC MythPlayer
     int64_t   totalDuration;
     long long rewindtime;
     int64_t   m_latestVideoTimecode;
+    QElapsedTimer m_avTimer;
 
     // -- end state stuff --
 
