gstclock.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* GStreamer
  2. * Copyright (C) 1999,2000 Erik Walthinsen <[email protected]>
  3. * 2000 Wim Taymans <[email protected]>
  4. * 2005 Wim Taymans <[email protected]>
  5. *
  6. * gstclock.h: Header for clock subsystem
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  21. * Boston, MA 02110-1301, USA.
  22. */
  23. #ifndef __GST_CLOCK_H__
  24. #define __GST_CLOCK_H__
  25. G_BEGIN_DECLS
  26. /* --- standard type macros --- */
  27. #define GST_TYPE_CLOCK (gst_clock_get_type ())
  28. #define GST_CLOCK(clock) (G_TYPE_CHECK_INSTANCE_CAST ((clock), GST_TYPE_CLOCK, GstClock))
  29. #define GST_IS_CLOCK(clock) (G_TYPE_CHECK_INSTANCE_TYPE ((clock), GST_TYPE_CLOCK))
  30. #define GST_CLOCK_CLASS(cclass) (G_TYPE_CHECK_CLASS_CAST ((cclass), GST_TYPE_CLOCK, GstClockClass))
  31. #define GST_IS_CLOCK_CLASS(cclass) (G_TYPE_CHECK_CLASS_TYPE ((cclass), GST_TYPE_CLOCK))
  32. #define GST_CLOCK_GET_CLASS(clock) (G_TYPE_INSTANCE_GET_CLASS ((clock), GST_TYPE_CLOCK, GstClockClass))
  33. #define GST_CLOCK_CAST(clock) ((GstClock*)(clock))
  34. /**
  35. * GstClockTime:
  36. *
  37. * A datatype to hold a time, measured in nanoseconds.
  38. */
  39. typedef guint64 GstClockTime;
  40. /**
  41. * GST_TYPE_CLOCK_TIME:
  42. *
  43. * The #GType of a #GstClockTime.
  44. */
  45. #define GST_TYPE_CLOCK_TIME G_TYPE_UINT64
  46. /**
  47. * GstClockTimeDiff:
  48. *
  49. * A datatype to hold a time difference, measured in nanoseconds.
  50. */
  51. typedef gint64 GstClockTimeDiff;
  52. /**
  53. * GstClockID:
  54. *
  55. * A datatype to hold the handle to an outstanding sync or async clock callback.
  56. */
  57. typedef gpointer GstClockID;
  58. /**
  59. * GST_CLOCK_TIME_NONE:
  60. *
  61. * Constant to define an undefined clock time.
  62. *
  63. * Value: 18446744073709551615
  64. * Type: GstClockTime
  65. */
  66. #define GST_CLOCK_TIME_NONE ((GstClockTime) -1)
  67. /**
  68. * GST_CLOCK_TIME_IS_VALID:
  69. * @time: clock time to validate
  70. *
  71. * Tests if a given #GstClockTime represents a valid defined time.
  72. */
  73. #define GST_CLOCK_TIME_IS_VALID(time) (((GstClockTime)(time)) != GST_CLOCK_TIME_NONE)
  74. /* FIXME: still need to explicitly force types on the defines below */
  75. /**
  76. * GST_SECOND:
  77. *
  78. * Constant that defines one GStreamer second.
  79. *
  80. * Value: 1000000000
  81. * Type: GstClockTime
  82. */
  83. #define GST_SECOND (G_USEC_PER_SEC * G_GINT64_CONSTANT (1000))
  84. /**
  85. * GST_MSECOND:
  86. *
  87. * Constant that defines one GStreamer millisecond.
  88. *
  89. * Value: 1000000
  90. * Type: GstClockTime
  91. */
  92. #define GST_MSECOND (GST_SECOND / G_GINT64_CONSTANT (1000))
  93. /**
  94. * GST_USECOND:
  95. *
  96. * Constant that defines one GStreamer microsecond.
  97. *
  98. * Value: 1000
  99. * Type: GstClockTime
  100. */
  101. #define GST_USECOND (GST_SECOND / G_GINT64_CONSTANT (1000000))
  102. /**
  103. * GST_NSECOND:
  104. *
  105. * Constant that defines one GStreamer nanosecond
  106. *
  107. * Value: 1
  108. * Type: GstClockTime
  109. */
  110. #define GST_NSECOND (GST_SECOND / G_GINT64_CONSTANT (1000000000))
  111. /**
  112. * GST_TIME_AS_SECONDS:
  113. * @time: the time
  114. *
  115. * Convert a #GstClockTime to seconds.
  116. */
  117. #define GST_TIME_AS_SECONDS(time) ((time) / GST_SECOND)
  118. /**
  119. * GST_TIME_AS_MSECONDS:
  120. * @time: the time
  121. *
  122. * Convert a #GstClockTime to milliseconds (1/1000 of a second).
  123. */
  124. #define GST_TIME_AS_MSECONDS(time) ((time) / G_GINT64_CONSTANT (1000000))
  125. /**
  126. * GST_TIME_AS_USECONDS:
  127. * @time: the time
  128. *
  129. * Convert a #GstClockTime to microseconds (1/1000000 of a second).
  130. */
  131. #define GST_TIME_AS_USECONDS(time) ((time) / G_GINT64_CONSTANT (1000))
  132. /**
  133. * GST_TIME_AS_NSECONDS:
  134. * @time: the time
  135. *
  136. * Convert a #GstClockTime to nanoseconds (1/1000000000 of a second).
  137. */
  138. #define GST_TIME_AS_NSECONDS(time) (time)
  139. /**
  140. * GST_CLOCK_DIFF:
  141. * @s: the first time
  142. * @e: the second time
  143. *
  144. * Calculate a difference between two clock times as a #GstClockTimeDiff.
  145. * The difference is calculated as @e - @s.
  146. */
  147. #define GST_CLOCK_DIFF(s, e) (GstClockTimeDiff)((e) - (s))
  148. /**
  149. * GST_TIMEVAL_TO_TIME:
  150. * @tv: the timeval to convert
  151. *
  152. * Convert a #GTimeVal to a #GstClockTime.
  153. */
  154. #define GST_TIMEVAL_TO_TIME(tv) (GstClockTime)((tv).tv_sec * GST_SECOND + (tv).tv_usec * GST_USECOND)
  155. /**
  156. * GST_TIME_TO_TIMEVAL:
  157. * @t: The #GstClockTime to convert
  158. * @tv: The target timeval
  159. *
  160. * Convert a #GstClockTime to a #GTimeVal
  161. *
  162. * <note>on 32-bit systems, a timeval has a range of only 2^32 - 1 seconds,
  163. * which is about 68 years. Expect trouble if you want to schedule stuff
  164. * in your pipeline for 2038.</note>
  165. */
  166. #define GST_TIME_TO_TIMEVAL(t,tv) \
  167. G_STMT_START { \
  168. g_assert ("Value of time " #t " is out of timeval's range" && \
  169. ((t) / GST_SECOND) < G_MAXLONG); \
  170. (tv).tv_sec = (glong) (((GstClockTime) (t)) / GST_SECOND); \
  171. (tv).tv_usec = (glong) ((((GstClockTime) (t)) - \
  172. ((GstClockTime) (tv).tv_sec) * GST_SECOND) \
  173. / GST_USECOND); \
  174. } G_STMT_END
  175. /**
  176. * GST_TIMESPEC_TO_TIME:
  177. * @ts: the timespec to convert
  178. *
  179. * Convert a struct timespec (see man pselect) to a #GstClockTime.
  180. */
  181. #define GST_TIMESPEC_TO_TIME(ts) (GstClockTime)((ts).tv_sec * GST_SECOND + (ts).tv_nsec * GST_NSECOND)
  182. /**
  183. * GST_TIME_TO_TIMESPEC:
  184. * @t: The #GstClockTime to convert
  185. * @ts: The target timespec
  186. *
  187. * Convert a #GstClockTime to a struct timespec (see man pselect)
  188. */
  189. #define GST_TIME_TO_TIMESPEC(t,ts) \
  190. G_STMT_START { \
  191. g_assert ("Value of time " #t " is out of timespec's range" && \
  192. ((t) / GST_SECOND) < G_MAXLONG); \
  193. (ts).tv_sec = (glong) ((t) / GST_SECOND); \
  194. (ts).tv_nsec = (glong) (((t) - (ts).tv_sec * GST_SECOND) / GST_NSECOND); \
  195. } G_STMT_END
  196. /* timestamp debugging macros */
  197. /**
  198. * GST_TIME_FORMAT:
  199. *
  200. * A string that can be used in printf-like format strings to display a
  201. * #GstClockTime value in h:m:s format. Use GST_TIME_ARGS() to construct
  202. * the matching arguments.
  203. *
  204. * Example:
  205. * |[
  206. * printf("%" GST_TIME_FORMAT "\n", GST_TIME_ARGS(ts));
  207. * ]|
  208. */
  209. #define GST_TIME_FORMAT "u:%02u:%02u.%09u"
  210. /**
  211. * GST_TIME_ARGS:
  212. * @t: a #GstClockTime
  213. *
  214. * Format @t for the #GST_TIME_FORMAT format string. Note: @t will be
  215. * evaluated more than once.
  216. */
  217. #define GST_TIME_ARGS(t) \
  218. GST_CLOCK_TIME_IS_VALID (t) ? \
  219. (guint) (((GstClockTime)(t)) / (GST_SECOND * 60 * 60)) : 99, \
  220. GST_CLOCK_TIME_IS_VALID (t) ? \
  221. (guint) ((((GstClockTime)(t)) / (GST_SECOND * 60)) % 60) : 99, \
  222. GST_CLOCK_TIME_IS_VALID (t) ? \
  223. (guint) ((((GstClockTime)(t)) / GST_SECOND) % 60) : 99, \
  224. GST_CLOCK_TIME_IS_VALID (t) ? \
  225. (guint) (((GstClockTime)(t)) % GST_SECOND) : 999999999
  226. typedef struct _GstClockEntry GstClockEntry;
  227. typedef struct _GstClock GstClock;
  228. typedef struct _GstClockClass GstClockClass;
  229. typedef struct _GstClockPrivate GstClockPrivate;
  230. /* --- prototype for async callbacks --- */
  231. /**
  232. * GstClockCallback:
  233. * @clock: The clock that triggered the callback
  234. * @time: The time it was triggered
  235. * @id: The #GstClockID that expired
  236. * @user_data: user data passed in the gst_clock_id_wait_async() function
  237. *
  238. * The function prototype of the callback.
  239. *
  240. * Returns: %TRUE or %FALSE (currently unused)
  241. */
  242. typedef gboolean (*GstClockCallback) (GstClock *clock, GstClockTime time,
  243. GstClockID id, gpointer user_data);
  244. /**
  245. * GstClockReturn:
  246. * @GST_CLOCK_OK: The operation succeeded.
  247. * @GST_CLOCK_EARLY: The operation was scheduled too late.
  248. * @GST_CLOCK_UNSCHEDULED: The clockID was unscheduled
  249. * @GST_CLOCK_BUSY: The ClockID is busy
  250. * @GST_CLOCK_BADTIME: A bad time was provided to a function.
  251. * @GST_CLOCK_ERROR: An error occurred
  252. * @GST_CLOCK_UNSUPPORTED: Operation is not supported
  253. * @GST_CLOCK_DONE: The ClockID is done waiting
  254. *
  255. * The return value of a clock operation.
  256. */
  257. typedef enum
  258. {
  259. GST_CLOCK_OK = 0,
  260. GST_CLOCK_EARLY = 1,
  261. GST_CLOCK_UNSCHEDULED = 2,
  262. GST_CLOCK_BUSY = 3,
  263. GST_CLOCK_BADTIME = 4,
  264. GST_CLOCK_ERROR = 5,
  265. GST_CLOCK_UNSUPPORTED = 6,
  266. GST_CLOCK_DONE = 7
  267. } GstClockReturn;
  268. /**
  269. * GstClockEntryType:
  270. * @GST_CLOCK_ENTRY_SINGLE: a single shot timeout
  271. * @GST_CLOCK_ENTRY_PERIODIC: a periodic timeout request
  272. *
  273. * The type of the clock entry
  274. */
  275. typedef enum {
  276. GST_CLOCK_ENTRY_SINGLE,
  277. GST_CLOCK_ENTRY_PERIODIC
  278. } GstClockEntryType;
  279. /**
  280. * GST_CLOCK_ENTRY:
  281. * @entry: the entry to cast
  282. *
  283. * Cast to a clock entry
  284. */
  285. #define GST_CLOCK_ENTRY(entry) ((GstClockEntry *)(entry))
  286. /**
  287. * GST_CLOCK_ENTRY_CLOCK:
  288. * @entry: the entry to query
  289. *
  290. * Get the owner clock of the entry
  291. */
  292. #define GST_CLOCK_ENTRY_CLOCK(entry) ((entry)->clock)
  293. /**
  294. * GST_CLOCK_ENTRY_TYPE:
  295. * @entry: the entry to query
  296. *
  297. * Get the type of the clock entry
  298. */
  299. #define GST_CLOCK_ENTRY_TYPE(entry) ((entry)->type)
  300. /**
  301. * GST_CLOCK_ENTRY_TIME:
  302. * @entry: the entry to query
  303. *
  304. * Get the requested time of this entry
  305. */
  306. #define GST_CLOCK_ENTRY_TIME(entry) ((entry)->time)
  307. /**
  308. * GST_CLOCK_ENTRY_INTERVAL:
  309. * @entry: the entry to query
  310. *
  311. * Get the interval of this periodic entry
  312. */
  313. #define GST_CLOCK_ENTRY_INTERVAL(entry) ((entry)->interval)
  314. /**
  315. * GST_CLOCK_ENTRY_STATUS:
  316. * @entry: the entry to query
  317. *
  318. * The status of the entry
  319. */
  320. #define GST_CLOCK_ENTRY_STATUS(entry) ((entry)->status)
  321. /**
  322. * GstClockEntry:
  323. * @refcount: reference counter (read-only)
  324. *
  325. * All pending timeouts or periodic notifies are converted into
  326. * an entry.
  327. * Note that GstClockEntry should be treated as an opaque structure. It must
  328. * not be extended or allocated using a custom allocator.
  329. */
  330. struct _GstClockEntry {
  331. gint refcount;
  332. /*< protected >*/
  333. GstClock *clock;
  334. GstClockEntryType type;
  335. GstClockTime time;
  336. GstClockTime interval;
  337. GstClockReturn status;
  338. GstClockCallback func;
  339. gpointer user_data;
  340. GDestroyNotify destroy_data;
  341. gboolean unscheduled;
  342. gboolean woken_up;
  343. /*< private >*/
  344. gpointer _gst_reserved[GST_PADDING];
  345. };
  346. #include <gst/gstobject.h>
  347. /**
  348. * GstClockFlags:
  349. * @GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC: clock can do a single sync timeout request
  350. * @GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC: clock can do a single async timeout request
  351. * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC: clock can do sync periodic timeout requests
  352. * @GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC: clock can do async periodic timeout callbacks
  353. * @GST_CLOCK_FLAG_CAN_SET_RESOLUTION: clock's resolution can be changed
  354. * @GST_CLOCK_FLAG_CAN_SET_MASTER: clock can be slaved to a master clock
  355. * @GST_CLOCK_FLAG_LAST: subclasses can add additional flags starting from this flag
  356. *
  357. * The capabilities of this clock
  358. */
  359. typedef enum {
  360. GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC = (GST_OBJECT_FLAG_LAST << 0),
  361. GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC = (GST_OBJECT_FLAG_LAST << 1),
  362. GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC = (GST_OBJECT_FLAG_LAST << 2),
  363. GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC = (GST_OBJECT_FLAG_LAST << 3),
  364. GST_CLOCK_FLAG_CAN_SET_RESOLUTION = (GST_OBJECT_FLAG_LAST << 4),
  365. GST_CLOCK_FLAG_CAN_SET_MASTER = (GST_OBJECT_FLAG_LAST << 5),
  366. /* padding */
  367. GST_CLOCK_FLAG_LAST = (GST_OBJECT_FLAG_LAST << 8)
  368. } GstClockFlags;
  369. /**
  370. * GST_CLOCK_FLAGS:
  371. * @clock: the clock to query
  372. *
  373. * Gets the #GstClockFlags clock flags.
  374. */
  375. #define GST_CLOCK_FLAGS(clock) GST_OBJECT_FLAGS(clock)
  376. /**
  377. * GstClock:
  378. *
  379. * #GstClock base structure. The values of this structure are
  380. * protected for subclasses, use the methods to use the #GstClock.
  381. */
  382. struct _GstClock {
  383. GstObject object;
  384. /*< private >*/
  385. GstClockPrivate *priv;
  386. gpointer _gst_reserved[GST_PADDING];
  387. };
  388. /**
  389. * GstClockClass:
  390. * @parent_class: the parent class structure
  391. * @change_resolution: change the resolution of the clock. Not all values might
  392. * be acceptable. The new resolution should be returned.
  393. * @get_resolution: get the resolution of the clock.
  394. * @get_internal_time: get the internal unadjusted time of the clock.
  395. * implement @wait_jitter instead.
  396. * @wait: perform a blocking wait on the given #GstClockEntry and return
  397. * the jitter.
  398. * @wait_async: perform an asynchronous wait for the given #GstClockEntry.
  399. * @unschedule: unblock a blocking or async wait operation.
  400. *
  401. * GStreamer clock class. Override the vmethods to implement the clock
  402. * functionality.
  403. */
  404. struct _GstClockClass {
  405. GstObjectClass parent_class;
  406. /*< public >*/
  407. /* vtable */
  408. GstClockTime (*change_resolution) (GstClock *clock,
  409. GstClockTime old_resolution,
  410. GstClockTime new_resolution);
  411. GstClockTime (*get_resolution) (GstClock *clock);
  412. GstClockTime (*get_internal_time) (GstClock *clock);
  413. /* waiting on an ID */
  414. GstClockReturn (*wait) (GstClock *clock, GstClockEntry *entry,
  415. GstClockTimeDiff *jitter);
  416. GstClockReturn (*wait_async) (GstClock *clock, GstClockEntry *entry);
  417. void (*unschedule) (GstClock *clock, GstClockEntry *entry);
  418. /*< private >*/
  419. gpointer _gst_reserved[GST_PADDING];
  420. };
  421. GType gst_clock_get_type (void);
  422. GstClockTime gst_clock_set_resolution (GstClock *clock,
  423. GstClockTime resolution);
  424. GstClockTime gst_clock_get_resolution (GstClock *clock);
  425. GstClockTime gst_clock_get_time (GstClock *clock);
  426. void gst_clock_set_calibration (GstClock *clock, GstClockTime internal,
  427. GstClockTime external,
  428. GstClockTime rate_num,
  429. GstClockTime rate_denom);
  430. void gst_clock_get_calibration (GstClock *clock, GstClockTime *internal,
  431. GstClockTime *external,
  432. GstClockTime *rate_num,
  433. GstClockTime *rate_denom);
  434. /* master/slave clocks */
  435. gboolean gst_clock_set_master (GstClock *clock, GstClock *master);
  436. GstClock* gst_clock_get_master (GstClock *clock);
  437. void gst_clock_set_timeout (GstClock *clock,
  438. GstClockTime timeout);
  439. GstClockTime gst_clock_get_timeout (GstClock *clock);
  440. gboolean gst_clock_add_observation (GstClock *clock, GstClockTime slave,
  441. GstClockTime master, gdouble *r_squared);
  442. /* getting and adjusting internal/external time */
  443. GstClockTime gst_clock_get_internal_time (GstClock *clock);
  444. GstClockTime gst_clock_adjust_unlocked (GstClock *clock, GstClockTime internal);
  445. GstClockTime gst_clock_unadjust_unlocked (GstClock * clock, GstClockTime external);
  446. /* creating IDs that can be used to get notifications */
  447. GstClockID gst_clock_new_single_shot_id (GstClock *clock,
  448. GstClockTime time);
  449. GstClockID gst_clock_new_periodic_id (GstClock *clock,
  450. GstClockTime start_time,
  451. GstClockTime interval);
  452. /* reference counting */
  453. GstClockID gst_clock_id_ref (GstClockID id);
  454. void gst_clock_id_unref (GstClockID id);
  455. /* operations on IDs */
  456. gint gst_clock_id_compare_func (gconstpointer id1, gconstpointer id2);
  457. GstClockTime gst_clock_id_get_time (GstClockID id);
  458. GstClockReturn gst_clock_id_wait (GstClockID id,
  459. GstClockTimeDiff *jitter);
  460. GstClockReturn gst_clock_id_wait_async (GstClockID id,
  461. GstClockCallback func,
  462. gpointer user_data,
  463. GDestroyNotify destroy_data);
  464. void gst_clock_id_unschedule (GstClockID id);
  465. gboolean gst_clock_single_shot_id_reinit (GstClock * clock,
  466. GstClockID id,
  467. GstClockTime time);
  468. gboolean gst_clock_periodic_id_reinit (GstClock * clock,
  469. GstClockID id,
  470. GstClockTime start_time,
  471. GstClockTime interval);
  472. G_END_DECLS
  473. #endif /* __GST_CLOCK_H__ */