refclock.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //------------------------------------------------------------------------------
  2. // File: RefClock.h
  3. //
  4. // Desc: DirectShow base classes - defines the IReferenceClock interface.
  5. //
  6. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8. #ifndef __BASEREFCLOCK__
  9. #define __BASEREFCLOCK__
  10. #include <Schedule.h>
  11. const UINT RESOLUTION = 1; /* High resolution timer */
  12. const INT ADVISE_CACHE = 4; /* Default cache size */
  13. const LONGLONG MAX_TIME = 0x7FFFFFFFFFFFFFFF; /* Maximum LONGLONG value */
  14. inline LONGLONG WINAPI ConvertToMilliseconds(const REFERENCE_TIME& RT)
  15. {
  16. /* This converts an arbitrary value representing a reference time
  17. into a MILLISECONDS value for use in subsequent system calls */
  18. return (RT / (UNITS / MILLISECONDS));
  19. }
  20. /* This class hierarchy will support an IReferenceClock interface so
  21. that an audio card (or other externally driven clock) can update the
  22. system wide clock that everyone uses.
  23. The interface will be pretty thin with probably just one update method
  24. This interface has not yet been defined.
  25. */
  26. /* This abstract base class implements the IReferenceClock
  27. * interface. Classes that actually provide clock signals (from
  28. * whatever source) have to be derived from this class.
  29. *
  30. * The abstract class provides implementations for:
  31. * CUnknown support
  32. * locking support (CCritSec)
  33. * client advise code (creates a thread)
  34. *
  35. * Question: what can we do about quality? Change the timer
  36. * resolution to lower the system load? Up the priority of the
  37. * timer thread to force more responsive signals?
  38. *
  39. * During class construction we create a worker thread that is destroyed during
  40. * destuction. This thread executes a series of WaitForSingleObject calls,
  41. * waking up when a command is given to the thread or the next wake up point
  42. * is reached. The wakeup points are determined by clients making Advise
  43. * calls.
  44. *
  45. * Each advise call defines a point in time when they wish to be notified. A
  46. * periodic advise is a series of these such events. We maintain a list of
  47. * advise links and calculate when the nearest event notification is due for.
  48. * We then call WaitForSingleObject with a timeout equal to this time. The
  49. * handle we wait on is used by the class to signal that something has changed
  50. * and that we must reschedule the next event. This typically happens when
  51. * someone comes in and asks for an advise link while we are waiting for an
  52. * event to timeout.
  53. *
  54. * While we are modifying the list of advise requests we
  55. * are protected from interference through a critical section. Clients are NOT
  56. * advised through callbacks. One shot clients have an event set, while
  57. * periodic clients have a semaphore released for each event notification. A
  58. * semaphore allows a client to be kept up to date with the number of events
  59. * actually triggered and be assured that they can't miss multiple events being
  60. * set.
  61. *
  62. * Keeping track of advises is taken care of by the CAMSchedule class.
  63. */
  64. class CBaseReferenceClock
  65. : public CUnknown, public IReferenceClock, public CCritSec, public IReferenceClockTimerControl
  66. {
  67. protected:
  68. virtual ~CBaseReferenceClock(); // Don't let me be created on the stack!
  69. public:
  70. CBaseReferenceClock(__in_opt LPCTSTR pName,
  71. __inout_opt LPUNKNOWN pUnk,
  72. __inout HRESULT *phr,
  73. __inout_opt CAMSchedule * pSched = 0 );
  74. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void ** ppv);
  75. DECLARE_IUNKNOWN
  76. /* IReferenceClock methods */
  77. // Derived classes must implement GetPrivateTime(). All our GetTime
  78. // does is call GetPrivateTime and then check so that time does not
  79. // go backwards. A return code of S_FALSE implies that the internal
  80. // clock has gone backwards and GetTime time has halted until internal
  81. // time has caught up. (Don't know if this will be much use to folk,
  82. // but it seems odd not to use the return code for something useful.)
  83. STDMETHODIMP GetTime(__out REFERENCE_TIME *pTime);
  84. // When this is called, it sets m_rtLastGotTime to the time it returns.
  85. /* Provide standard mechanisms for scheduling events */
  86. /* Ask for an async notification that a time has elapsed */
  87. STDMETHODIMP AdviseTime(
  88. REFERENCE_TIME baseTime, // base reference time
  89. REFERENCE_TIME streamTime, // stream offset time
  90. HEVENT hEvent, // advise via this event
  91. __out DWORD_PTR *pdwAdviseCookie// where your cookie goes
  92. );
  93. /* Ask for an asynchronous periodic notification that a time has elapsed */
  94. STDMETHODIMP AdvisePeriodic(
  95. REFERENCE_TIME StartTime, // starting at this time
  96. REFERENCE_TIME PeriodTime, // time between notifications
  97. HSEMAPHORE hSemaphore, // advise via a semaphore
  98. __out DWORD_PTR *pdwAdviseCookie// where your cookie goes
  99. );
  100. /* Cancel a request for notification(s) - if the notification was
  101. * a one shot timer then this function doesn't need to be called
  102. * as the advise is automatically cancelled, however it does no
  103. * harm to explicitly cancel a one-shot advise. It is REQUIRED that
  104. * clients call Unadvise to clear a Periodic advise setting.
  105. */
  106. STDMETHODIMP Unadvise(DWORD_PTR dwAdviseCookie);
  107. /* Methods for the benefit of derived classes or outer objects */
  108. // GetPrivateTime() is the REAL clock. GetTime is just a cover for
  109. // it. Derived classes will probably override this method but not
  110. // GetTime() itself.
  111. // The important point about GetPrivateTime() is it's allowed to go
  112. // backwards. Our GetTime() will keep returning the LastGotTime
  113. // until GetPrivateTime() catches up.
  114. virtual REFERENCE_TIME GetPrivateTime();
  115. /* Provide a method for correcting drift */
  116. STDMETHODIMP SetTimeDelta( const REFERENCE_TIME& TimeDelta );
  117. CAMSchedule * GetSchedule() const { return m_pSchedule; }
  118. // IReferenceClockTimerControl methods
  119. //
  120. // Setting a default of 0 disables the default of 1ms
  121. STDMETHODIMP SetDefaultTimerResolution(
  122. REFERENCE_TIME timerResolution // in 100ns
  123. );
  124. STDMETHODIMP GetDefaultTimerResolution(
  125. __out REFERENCE_TIME* pTimerResolution // in 100ns
  126. );
  127. private:
  128. REFERENCE_TIME m_rtPrivateTime; // Current best estimate of time
  129. DWORD m_dwPrevSystemTime; // Last vaule we got from timeGetTime
  130. REFERENCE_TIME m_rtLastGotTime; // Last time returned by GetTime
  131. REFERENCE_TIME m_rtNextAdvise; // Time of next advise
  132. UINT m_TimerResolution;
  133. #ifdef PERF
  134. int m_idGetSystemTime;
  135. #endif
  136. // Thread stuff
  137. public:
  138. void TriggerThread() // Wakes thread up. Need to do this if
  139. { // time to next advise needs reevaluating.
  140. EXECUTE_ASSERT(SetEvent(m_pSchedule->GetEvent()));
  141. }
  142. private:
  143. BOOL m_bAbort; // Flag used for thread shutdown
  144. HANDLE m_hThread; // Thread handle
  145. HRESULT AdviseThread(); // Method in which the advise thread runs
  146. static DWORD __stdcall AdviseThreadFunction(__in LPVOID); // Function used to get there
  147. protected:
  148. CAMSchedule * m_pSchedule;
  149. void Restart (IN REFERENCE_TIME rtMinTime = 0I64) ;
  150. };
  151. #endif