summaryrefslogtreecommitdiff
path: root/Audio/AudioStreamClock.hpp
blob: 65e5b1e1e07f6ddbf87a682c3e5a2fda029bc29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// -*- Mode: C++ -*-
#ifndef AUDIO_STREAM_CLOCK_HPP__
#define AUDIO_STREAM_CLOCK_HPP__

#include <QtGlobal>

#include "Audio/AudioStreamDescriptor.hpp"

class AudioStreamClock
{
public:
  enum class Reference
  {
    ArrivalWallClock,
    CaptureAnchor
  };

  void setDescriptor (AudioStreamDescriptor descriptor) noexcept
  {
    descriptor_ = descriptor;
    captured_frames_ = 0;
  }

  Reference reference () const noexcept
  {
    return descriptor_.isValid ()
      && descriptor_.clock_domain
         == AudioStreamDescriptor::ClockDomain::SystemClock
      && descriptor_.timing_evidence
         == AudioStreamDescriptor::TimingEvidence::CaptureTimeAnchored
      && descriptor_.capture_anchor_utc_ms > 0
      ? Reference::CaptureAnchor : Reference::ArrivalWallClock;
  }

  qint64 timestamp (qint64 arrival_utc_ms) const noexcept
  {
    if (Reference::CaptureAnchor != reference ())
      {
        return arrival_utc_ms;
      }

    return descriptor_.capture_anchor_utc_ms
      + captured_frames_ * 1000 / descriptor_.sample_rate_hz;
  }

  void advance (qint64 frames) noexcept
  {
    if (Reference::CaptureAnchor == reference () && frames > 0)
      {
        captured_frames_ += frames;
      }
  }

private:
  AudioStreamDescriptor descriptor_;
  qint64 captured_frames_ {0};
};

#endif