Home | History | Annotate | Download | only in interface
      1 /*
      2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_
     12 #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_
     13 
     14 #include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
     15 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     16 #include "webrtc/typedefs.h"
     17 
     18 namespace webrtc {
     19 
     20 // January 1970, in NTP seconds.
     21 const uint32_t kNtpJan1970 = 2208988800UL;
     22 
     23 // Magic NTP fractional unit.
     24 const double kMagicNtpFractionalUnit = 4.294967296E+9;
     25 
     26 // A clock interface that allows reading of absolute and relative timestamps.
     27 class Clock {
     28  public:
     29   virtual ~Clock() {}
     30 
     31   // Return a timestamp in milliseconds relative to some arbitrary source; the
     32   // source is fixed for this clock.
     33   virtual int64_t TimeInMilliseconds() const = 0;
     34 
     35   // Return a timestamp in microseconds relative to some arbitrary source; the
     36   // source is fixed for this clock.
     37   virtual int64_t TimeInMicroseconds() const = 0;
     38 
     39   // Retrieve an NTP absolute timestamp in seconds and fractions of a second.
     40   virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const = 0;
     41 
     42   // Retrieve an NTP absolute timestamp in milliseconds.
     43   virtual int64_t CurrentNtpInMilliseconds() const = 0;
     44 
     45   // Converts an NTP timestamp to a millisecond timestamp.
     46   static int64_t NtpToMs(uint32_t seconds, uint32_t fractions);
     47 
     48   // Returns an instance of the real-time system clock implementation.
     49   static Clock* GetRealTimeClock();
     50 };
     51 
     52 class SimulatedClock : public Clock {
     53  public:
     54   explicit SimulatedClock(int64_t initial_time_us);
     55 
     56   virtual ~SimulatedClock();
     57 
     58   // Return a timestamp in milliseconds relative to some arbitrary source; the
     59   // source is fixed for this clock.
     60   virtual int64_t TimeInMilliseconds() const OVERRIDE;
     61 
     62   // Return a timestamp in microseconds relative to some arbitrary source; the
     63   // source is fixed for this clock.
     64   virtual int64_t TimeInMicroseconds() const OVERRIDE;
     65 
     66   // Retrieve an NTP absolute timestamp in milliseconds.
     67   virtual void CurrentNtp(uint32_t& seconds,
     68                           uint32_t& fractions) const OVERRIDE;
     69 
     70   // Converts an NTP timestamp to a millisecond timestamp.
     71   virtual int64_t CurrentNtpInMilliseconds() const OVERRIDE;
     72 
     73   // Advance the simulated clock with a given number of milliseconds or
     74   // microseconds.
     75   void AdvanceTimeMilliseconds(int64_t milliseconds);
     76   void AdvanceTimeMicroseconds(int64_t microseconds);
     77 
     78  private:
     79   int64_t time_us_;
     80   scoped_ptr<RWLockWrapper> lock_;
     81 };
     82 
     83 };  // namespace webrtc
     84 
     85 #endif  // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_
     86