1
/* $Id: clock.h 3664 2011-07-19 03:42:28Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJMEDIA_CLOCK_H__
21
#define __PJMEDIA_CLOCK_H__
27
#include <pjmedia/types.h>
31
* @defgroup PJMEDIA_PORT_CLOCK Clock/Timing
32
* @ingroup PJMEDIA_PORT
33
* @brief Various types of classes that provide timing.
36
The media clock/timing extends the media port concept that is explained
37
in @ref PJMEDIA_PORT. When clock is present in the ports
38
interconnection, media will flow automatically (and with correct timing too!)
39
from one media port to another.
41
There are few objects in PJMEDIA that are able to provide clock/timing
42
to media ports interconnection:
44
- @ref PJMED_SND_PORT\n
45
The sound device makes a good candidate as the clock source, and
46
PJMEDIA @ref PJMED_SND is designed so that it is able to invoke
47
operations according to timing driven by the sound hardware clock
48
(this may sound complicated, but actually it just means that
49
the sound device abstraction provides callbacks to be called when
50
it has/wants media frames).\n
51
See @ref PJMED_SND_PORT for more details.
53
- @ref PJMEDIA_MASTER_PORT\n
54
The master port uses @ref PJMEDIA_CLOCK as the clock source. By using
55
@ref PJMEDIA_MASTER_PORT, it is possible to interconnect passive
56
media ports and let the frames flow automatically in timely manner.\n
57
Please see @ref PJMEDIA_MASTER_PORT for more details.
64
* @addtogroup PJMEDIA_CLOCK Clock Generator
65
* @ingroup PJMEDIA_PORT_CLOCK
66
* @brief Interface for generating clock.
69
* The clock generator provides the application with media timing,
70
* and it is used by the @ref PJMEDIA_MASTER_PORT for its sound clock.
72
* The clock generator may be configured to run <b>asynchronously</b>
73
* (the default behavior) or <b>synchronously</b>. When it is run
74
* asynchronously, it will call the application's callback every time
75
* the clock <b>tick</b> expires. When it is run synchronously,
76
* application must continuously polls the clock generator to synchronize
85
typedef struct pjmedia_clock_src
87
pjmedia_type media_type; /**< Media type. */
88
unsigned clock_rate; /**< Clock rate. */
89
unsigned ptime_usec; /**< Frame interval (in usec). */
91
* The timestamp field holds an increasing value in samples and its
92
* value is expected to be increased by clock_rate samples per second.
94
pj_timestamp timestamp;
96
* Timestamp's last update. The last_update field contains a value in
97
* ticks, and it is expected to be increased by pj_get_timestamp_freq()
100
pj_timestamp last_update;
104
* This is an auxiliary function to initialize the media clock source.
106
* @param clocksrc The clock source to be initialized.
107
* @param media_type The media type.
108
* @param clock_rate The clock rate.
109
* @param ptime_usec Media frame interval (in usec).
111
* @return PJ_SUCCESS on success.
113
PJ_DECL(pj_status_t) pjmedia_clock_src_init( pjmedia_clock_src *clocksrc,
114
pjmedia_type media_type,
116
unsigned ptime_usec );
119
* This function updates the clock source's timestamp. Application should
120
* use this function instead of updating the timestamp directly since this
121
* function will also update the last_update field of the clock source.
123
* @param clocksrc The clock source to be updated.
124
* @param timestamp The new timestamp, can be NULL if the current
125
* timestamp does not change (in this case it
126
* will only update the last_update field).
128
* @return PJ_SUCCESS on success.
130
PJ_DECL(pj_status_t) pjmedia_clock_src_update( pjmedia_clock_src *clocksrc,
131
const pj_timestamp *timestamp );
134
* This function gets the clock source's current timestamp. Application
135
* should use this function instead of accessing the timestamp directly
136
* since this function will calculate the predicted timestamp for current
137
* time, based on the values of timestamp, last_update, and clock_rate.
139
* @param clocksrc The clock source.
140
* @param timestamp Argument to receive the current timestamp
142
* @return PJ_SUCCESS on success.
145
pjmedia_clock_src_get_current_timestamp( const pjmedia_clock_src *clocksrc,
146
pj_timestamp *timestamp);
149
* This function gets the clock source's time in msec.
151
* @param clocksrc The clock source.
153
* @return The clock source's time (in msec).
156
pjmedia_clock_src_get_time_msec( const pjmedia_clock_src *clocksrc );
160
* Opaque declaration for media clock.
162
typedef struct pjmedia_clock pjmedia_clock;
166
* Options when creating the clock.
168
enum pjmedia_clock_options
171
* Prevents the clock from running asynchronously. In this case,
172
* application must poll the clock continuously by calling
173
* #pjmedia_clock_wait() in order to synchronize timing.
175
PJMEDIA_CLOCK_NO_ASYNC = 1,
178
* Prevent the clock from setting it's thread to highest priority.
180
PJMEDIA_CLOCK_NO_HIGHEST_PRIO = 2
184
typedef struct pjmedia_clock_param
187
* The frame interval, in microseconds.
189
unsigned usec_interval;
191
* The media clock rate, to determine timestamp
192
* increment for each call.
195
} pjmedia_clock_param;
198
* Type of media clock callback.
200
* @param ts Current timestamp, in samples.
201
* @param user_data Application data that is passed when
202
* the clock was created.
204
typedef void pjmedia_clock_callback(const pj_timestamp *ts,
210
* Create media clock. This creates a media clock object that will run
211
* periodically at an interval that is calculated from the audio parameters.
212
* Once created, application must call #pjmedia_clock_start() to actually
215
* @see pjmedia_clock_create2()
217
* @param pool Pool to allocate memory.
218
* @param clock_rate Number of samples per second.
219
* @param channel_count Number of channel.
220
* @param samples_per_frame Number of samples per frame. This argument
221
* along with clock_rate and channel_count, specifies
222
* the interval of each clock run (or clock ticks).
223
* @param options Bitmask of pjmedia_clock_options.
224
* @param cb Callback to be called for each clock tick.
225
* @param user_data User data, which will be passed to the callback.
226
* @param p_clock Pointer to receive the clock instance.
228
* @return PJ_SUCCESS on success, or the appropriate error
231
PJ_DECL(pj_status_t) pjmedia_clock_create( pj_pool_t *pool,
233
unsigned channel_count,
234
unsigned samples_per_frame,
236
pjmedia_clock_callback *cb,
238
pjmedia_clock **p_clock);
242
* Create media clock. This creates a media clock object that will run
243
* periodically at the specified interval. Once created, application must
244
* call #pjmedia_clock_start() to actually start the clock.
246
* @param pool Pool to allocate memory.
247
* @param param The clock parameter.
248
* @param options Bitmask of pjmedia_clock_options.
249
* @param cb Callback to be called for each clock tick.
250
* @param user_data User data, which will be passed to the callback.
251
* @param p_clock Pointer to receive the clock instance.
253
* @return PJ_SUCCESS on success, or the appropriate error
256
PJ_DECL(pj_status_t) pjmedia_clock_create2(pj_pool_t *pool,
257
const pjmedia_clock_param *param,
259
pjmedia_clock_callback *cb,
261
pjmedia_clock **p_clock);
264
* Start the clock. For clock created with asynchronous flag set to TRUE,
265
* this may start a worker thread for the clock (depending on the
266
* backend clock implementation being used).
268
* @param clock The media clock.
270
* @return PJ_SUCCES on success.
272
PJ_DECL(pj_status_t) pjmedia_clock_start(pjmedia_clock *clock);
278
* @param clock The media clock.
280
* @return PJ_SUCCES on success.
282
PJ_DECL(pj_status_t) pjmedia_clock_stop(pjmedia_clock *clock);
286
* Modify the clock's parameter.
288
* @param clock The media clock.
289
* @param param The clock's new parameter.
290
* @return PJ_SUCCES on success.
292
PJ_DECL(pj_status_t) pjmedia_clock_modify(pjmedia_clock *clock,
293
const pjmedia_clock_param *param);
297
* Poll the media clock, and execute the callback when the clock tick has
298
* elapsed. This operation is only valid if the clock is created with async
301
* @param clock The media clock.
302
* @param wait If non-zero, then the function will block until
303
* a clock tick elapsed and callback has been called.
304
* @param ts Optional argument to receive the current
307
* @return Non-zero if clock tick has elapsed, or FALSE if
308
* the function returns before a clock tick has
311
PJ_DECL(pj_bool_t) pjmedia_clock_wait(pjmedia_clock *clock,
319
* @param clock The media clock.
321
* @return PJ_SUCCES on success.
323
PJ_DECL(pj_status_t) pjmedia_clock_destroy(pjmedia_clock *clock);
334
#endif /* __PJMEDIA_CLOCK_H__ */