clock.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_CLOCK_H__
  20. #define __PJMEDIA_CLOCK_H__
  21. /**
  22. * @file clock.h
  23. * @brief Media clock.
  24. */
  25. #include <pjmedia/types.h>
  26. /**
  27. * @defgroup PJMEDIA_PORT_CLOCK Clock/Timing
  28. * @ingroup PJMEDIA_PORT
  29. * @brief Various types of classes that provide timing.
  30. * @{
  31. The media clock/timing extends the media port concept that is explained
  32. in @ref PJMEDIA_PORT. When clock is present in the ports
  33. interconnection, media will flow automatically (and with correct timing too!)
  34. from one media port to another.
  35. There are few objects in PJMEDIA that are able to provide clock/timing
  36. to media ports interconnection:
  37. - @ref PJMED_SND_PORT \n
  38. The sound device makes a good candidate as the clock source, and
  39. PJMEDIA @ref PJMED_SND is designed so that it is able to invoke
  40. operations according to timing driven by the sound hardware clock
  41. (this may sound complicated, but actually it just means that
  42. the sound device abstraction provides callbacks to be called when
  43. it has/wants media frames).\n
  44. See @ref PJMED_SND_PORT for more details.
  45. - @ref PJMEDIA_MASTER_PORT \n
  46. The master port uses @ref PJMEDIA_CLOCK as the clock source. By using
  47. @ref PJMEDIA_MASTER_PORT, it is possible to interconnect passive
  48. media ports and let the frames flow automatically in timely manner.\n
  49. Please see @ref PJMEDIA_MASTER_PORT for more details.
  50. @}
  51. */
  52. /**
  53. * @addtogroup PJMEDIA_CLOCK Clock Generator
  54. * @ingroup PJMEDIA_PORT_CLOCK
  55. * @brief Interface for generating clock.
  56. * @{
  57. *
  58. * The clock generator provides the application with media timing,
  59. * and it is used by the @ref PJMEDIA_MASTER_PORT for its sound clock.
  60. *
  61. * The clock generator may be configured to run <b>asynchronously</b>
  62. * (the default behavior) or <b>synchronously</b>. When it is run
  63. * asynchronously, it will call the application's callback every time
  64. * the clock <b>tick</b> expires. When it is run synchronously,
  65. * application must continuously polls the clock generator to synchronize
  66. * the timing.
  67. */
  68. PJ_BEGIN_DECL
  69. /**
  70. * Media clock source.
  71. */
  72. typedef struct pjmedia_clock_src
  73. {
  74. pjmedia_type media_type; /**< Media type. */
  75. unsigned clock_rate; /**< Clock rate. */
  76. unsigned ptime_usec; /**< Frame interval (in usec). */
  77. /**
  78. * The timestamp field holds an increasing value in samples and its
  79. * value is expected to be increased by clock_rate samples per second.
  80. */
  81. pj_timestamp timestamp;
  82. /**
  83. * Timestamp's last update. The last_update field contains a value in
  84. * ticks, and it is expected to be increased by pj_get_timestamp_freq()
  85. * ticks per second.
  86. */
  87. pj_timestamp last_update;
  88. } pjmedia_clock_src;
  89. /**
  90. * This is an auxiliary function to initialize the media clock source.
  91. *
  92. * @param clocksrc The clock source to be initialized.
  93. * @param media_type The media type.
  94. * @param clock_rate The clock rate.
  95. * @param ptime_usec Media frame interval (in usec).
  96. *
  97. * @return PJ_SUCCESS on success.
  98. */
  99. PJ_DECL(pj_status_t) pjmedia_clock_src_init( pjmedia_clock_src *clocksrc,
  100. pjmedia_type media_type,
  101. unsigned clock_rate,
  102. unsigned ptime_usec );
  103. /**
  104. * This function updates the clock source's timestamp. Application should
  105. * use this function instead of updating the timestamp directly since this
  106. * function will also update the last_update field of the clock source.
  107. *
  108. * @param clocksrc The clock source to be updated.
  109. * @param timestamp The new timestamp, can be NULL if the current
  110. * timestamp does not change (in this case it
  111. * will only update the last_update field).
  112. *
  113. * @return PJ_SUCCESS on success.
  114. */
  115. PJ_DECL(pj_status_t) pjmedia_clock_src_update( pjmedia_clock_src *clocksrc,
  116. const pj_timestamp *timestamp );
  117. /**
  118. * This function gets the clock source's current timestamp. Application
  119. * should use this function instead of accessing the timestamp directly
  120. * since this function will calculate the predicted timestamp for current
  121. * time, based on the values of timestamp, last_update, and clock_rate.
  122. *
  123. * @param clocksrc The clock source.
  124. * @param timestamp Argument to receive the current timestamp
  125. *
  126. * @return PJ_SUCCESS on success.
  127. */
  128. PJ_DECL(pj_status_t)
  129. pjmedia_clock_src_get_current_timestamp( const pjmedia_clock_src *clocksrc,
  130. pj_timestamp *timestamp);
  131. /**
  132. * This function gets the clock source's time in msec.
  133. *
  134. * @param clocksrc The clock source.
  135. *
  136. * @return The clock source's time (in msec).
  137. */
  138. PJ_DECL(pj_uint32_t)
  139. pjmedia_clock_src_get_time_msec( const pjmedia_clock_src *clocksrc );
  140. /**
  141. * Opaque declaration for media clock.
  142. */
  143. typedef struct pjmedia_clock pjmedia_clock;
  144. /**
  145. * Options when creating the clock.
  146. */
  147. enum pjmedia_clock_options
  148. {
  149. /**
  150. * Prevents the clock from running asynchronously. In this case,
  151. * application must poll the clock continuously by calling
  152. * #pjmedia_clock_wait() in order to synchronize timing.
  153. */
  154. PJMEDIA_CLOCK_NO_ASYNC = 1,
  155. /**
  156. * Prevent the clock from setting it's thread to highest priority.
  157. */
  158. PJMEDIA_CLOCK_NO_HIGHEST_PRIO = 2
  159. };
  160. typedef struct pjmedia_clock_param
  161. {
  162. /**
  163. * The frame interval, in microseconds.
  164. */
  165. unsigned usec_interval;
  166. /**
  167. * The media clock rate, to determine timestamp
  168. * increment for each call.
  169. */
  170. unsigned clock_rate;
  171. } pjmedia_clock_param;
  172. /**
  173. * Type of media clock callback.
  174. *
  175. * @param ts Current timestamp, in samples.
  176. * @param user_data Application data that is passed when
  177. * the clock was created.
  178. */
  179. typedef void pjmedia_clock_callback(const pj_timestamp *ts,
  180. void *user_data);
  181. /**
  182. * Create media clock. This creates a media clock object that will run
  183. * periodically at an interval that is calculated from the audio parameters.
  184. * Once created, application must call #pjmedia_clock_start() to actually
  185. * start the clock.
  186. *
  187. * @see pjmedia_clock_create2()
  188. *
  189. * @param pool Pool to allocate memory.
  190. * @param clock_rate Number of samples per second.
  191. * @param channel_count Number of channel.
  192. * @param samples_per_frame Number of samples per frame. This argument
  193. * along with clock_rate and channel_count, specifies
  194. * the interval of each clock run (or clock ticks).
  195. * @param options Bitmask of pjmedia_clock_options.
  196. * @param cb Callback to be called for each clock tick.
  197. * @param user_data User data, which will be passed to the callback.
  198. * @param p_clock Pointer to receive the clock instance.
  199. *
  200. * @return PJ_SUCCESS on success, or the appropriate error
  201. * code.
  202. */
  203. PJ_DECL(pj_status_t) pjmedia_clock_create( pj_pool_t *pool,
  204. unsigned clock_rate,
  205. unsigned channel_count,
  206. unsigned samples_per_frame,
  207. unsigned options,
  208. pjmedia_clock_callback *cb,
  209. void *user_data,
  210. pjmedia_clock **p_clock);
  211. /**
  212. * Create media clock. This creates a media clock object that will run
  213. * periodically at the specified interval. Once created, application must
  214. * call #pjmedia_clock_start() to actually start the clock.
  215. *
  216. * @param pool Pool to allocate memory.
  217. * @param param The clock parameter.
  218. * @param options Bitmask of pjmedia_clock_options.
  219. * @param cb Callback to be called for each clock tick.
  220. * @param user_data User data, which will be passed to the callback.
  221. * @param p_clock Pointer to receive the clock instance.
  222. *
  223. * @return PJ_SUCCESS on success, or the appropriate error
  224. * code.
  225. */
  226. PJ_DECL(pj_status_t) pjmedia_clock_create2(pj_pool_t *pool,
  227. const pjmedia_clock_param *param,
  228. unsigned options,
  229. pjmedia_clock_callback *cb,
  230. void *user_data,
  231. pjmedia_clock **p_clock);
  232. /**
  233. * Start the clock. For clock created with asynchronous flag set to TRUE,
  234. * this may start a worker thread for the clock (depending on the
  235. * backend clock implementation being used).
  236. *
  237. * @param clock The media clock.
  238. *
  239. * @return PJ_SUCCES on success.
  240. */
  241. PJ_DECL(pj_status_t) pjmedia_clock_start(pjmedia_clock *clock);
  242. /**
  243. * Stop the clock. When the function returns PJ_SUCCESS, it is guaranteed
  244. * that the clock thread (if any) has stopped and the callback has completed.
  245. * But if the function is called from within the clock's callback itself,
  246. * the callback will still be running until completion. In that case,
  247. * the function will only stop future callbacks and returns PJ_EBUSY.
  248. *
  249. * @param clock The media clock.
  250. *
  251. * @return PJ_SUCCES on success, or PJ_EBUSY.
  252. */
  253. PJ_DECL(pj_status_t) pjmedia_clock_stop(pjmedia_clock *clock);
  254. /**
  255. * Modify the clock's parameter.
  256. *
  257. * @param clock The media clock.
  258. * @param param The clock's new parameter.
  259. * @return PJ_SUCCES on success.
  260. */
  261. PJ_DECL(pj_status_t) pjmedia_clock_modify(pjmedia_clock *clock,
  262. const pjmedia_clock_param *param);
  263. /**
  264. * Poll the media clock, and execute the callback when the clock tick has
  265. * elapsed. This operation is only valid if the clock is created with async
  266. * flag set to FALSE.
  267. *
  268. * @param clock The media clock.
  269. * @param wait If non-zero, then the function will block until
  270. * a clock tick elapsed and callback has been called.
  271. * @param ts Optional argument to receive the current
  272. * timestamp.
  273. *
  274. * @return Non-zero if clock tick has elapsed, or FALSE if
  275. * the function returns before a clock tick has
  276. * elapsed.
  277. */
  278. PJ_DECL(pj_bool_t) pjmedia_clock_wait(pjmedia_clock *clock,
  279. pj_bool_t wait,
  280. pj_timestamp *ts);
  281. /**
  282. * Destroy the clock.
  283. *
  284. * @param clock The media clock.
  285. *
  286. * @return PJ_SUCCES on success.
  287. */
  288. PJ_DECL(pj_status_t) pjmedia_clock_destroy(pjmedia_clock *clock);
  289. PJ_END_DECL
  290. /**
  291. * @}
  292. */
  293. #endif /* __PJMEDIA_CLOCK_H__ */