Repository: xbmc/xbmc
File: xbmc/cores/AudioEngine/Sinks/AESinkAUDIOTRACK.cpp (C++)

Code before:
        (CurrentHostCounter() - m_timestamp.get_nanoTime()) < 2 * 1000 * 1000 * 1000)
    {
      if (usesAdvancedLogging)
      {
        CLog::Log(LOGDEBUG, "Framecounter: {} Time: {} Current-Time: {}",
                  (m_timestamp.get_framePosition() & UINT64_LOWER_BYTES), m_timestamp.get_nanoTime(),
                  CurrentHostCounter());
      }
      uint64_t delta = static_cast<uint64_t>(CurrentHostCounter() - m_timestamp.get_nanoTime());
      uint64_t stamphead =
          static_cast<uint64_t>(m_timestamp.get_framePosition() & UINT64_LOWER_BYTES) +
          delta * m_sink_sampleRate / 1000000000.0;
      // wrap around
      // e.g. 0xFFFFFFFFFFFF0123 -> 0x0000000000002478
      // because we only query each second the simple smaller comparison won't suffice
      // as delay can fluctuate minimally
      if (stamphead < m_timestampPos && (m_timestampPos - stamphead) > 0x7FFFFFFFFFFFFFFFULL)
      {
        uint64_t stamp = m_timestampPos;
        stamp += (1ULL << 32);
        stamphead = (stamp & UINT64_UPPER_BYTES) | stamphead;
        CLog::Log(LOGDEBUG, "Wraparound happened old: {} new: {}", m_timestampPos, stamphead);
      }
      m_timestampPos = stamphead;

      double playtime = m_timestampPos / static_cast<double>(m_sink_sampleRate);

      if (usesAdvancedLogging)
      {
        CLog::Log(LOGDEBUG,
                  "Delay - Timestamp: {} (ms) delta: {} (ms) playtime: {} (ms) Duration: {} ms",
                  1000.0 * (m_duration_written - playtime), delta / 1000000.0, playtime * 1000,
                  m_duration_written * 1000);
        CLog::Log(LOGDEBUG, "Head-Position {} Timestamp Position {} Delay-Offset: {} ms", m_headPos,
                  m_timestampPos,
                 1000.0 * (static_cast<int64_t>(m_headPos - m_timestampPos)) / m_sink_sampleRate);
      }
      // especially on seek timestamp reports nonsense
      // AE knows what it has added - and sink carries it
      m_hw_delay = m_duration_written - playtime;
      if (m_hw_delay < delay)
        m_hw_delay = 0;
      else
        m_hw_delay -= delay; // part that delay would not have
    }
  }

  if (usesAdvancedLogging)
  {
    CLog::Log(LOGDEBUG, "Delay below Audiotrack: {:f} ms Timestamp Delay: {:f} ms Delay with Pause: {:f} ms",
                         m_hw_delay * 1000, delay * 1000, (delay + m_hw_delay) * 1000 + m_pause_ms);

Review comment (suggestion):
```suggestion
      const double playtime = m_timestampPos / static_cast<double>(m_sink_sampleRate);
```

Code after:
        (CurrentHostCounter() - m_timestamp.get_nanoTime()) < 2 * 1000 * 1000 * 1000)
    {
      if (usesAdvancedLogging)
      {
        CLog::Log(LOGDEBUG, "Framecounter: {} Time: {} Current-Time: {}",
                  (m_timestamp.get_framePosition() & UINT64_LOWER_BYTES),
                  m_timestamp.get_nanoTime(), CurrentHostCounter());
      }
      uint64_t delta = static_cast<uint64_t>(CurrentHostCounter() - m_timestamp.get_nanoTime());
      uint64_t stamphead =
          static_cast<uint64_t>(m_timestamp.get_framePosition() & UINT64_LOWER_BYTES) +
          delta * m_sink_sampleRate / 1000000000.0;
      // wrap around
      // e.g. 0xFFFFFFFFFFFF0123 -> 0x0000000000002478
      // because we only query each second the simple smaller comparison won't suffice
      // as delay can fluctuate minimally
      if (stamphead < m_timestampPos && (m_timestampPos - stamphead) > 0x7FFFFFFFFFFFFFFFULL)
      {
        uint64_t stamp = m_timestampPos;
        stamp += (1ULL << 32);
        stamphead = (stamp & UINT64_UPPER_BYTES) | stamphead;
        CLog::Log(LOGDEBUG, "Wraparound happened old: {} new: {}", m_timestampPos, stamphead);
      }
      m_timestampPos = stamphead;

      const double playtime = m_timestampPos / static_cast<double>(m_sink_sampleRate);

      if (usesAdvancedLogging)
      {
        CLog::Log(LOGDEBUG,
                  "Delay - Timestamp: {} (ms) delta: {} (ms) playtime: {} (ms) Duration: {} ms",
                  1000.0 * (m_duration_written - playtime), delta / 1000000.0, playtime * 1000,
                  m_duration_written * 1000);
        CLog::Log(LOGDEBUG, "Head-Position {} Timestamp Position {} Delay-Offset: {} ms", m_headPos,
                  m_timestampPos,
                  1000.0 * (static_cast<int64_t>(m_headPos - m_timestampPos)) / m_sink_sampleRate);
      }
      // especially on seek timestamp reports nonsense
      // AE knows what it has added - and sink carries it
      m_hw_delay = m_duration_written - playtime;
      if (m_hw_delay < delay)
        m_hw_delay = 0;
      else
        m_hw_delay -= delay; // part that delay would not have
    }
  }

  if (usesAdvancedLogging)
  {
    CLog::Log(LOGDEBUG,
              "Delay below Audiotrack: {:f} ms Timestamp Delay: {:f} ms Delay with Pause: {:f} ms",