1
/* $Id: clock.h 2394 2008-12-23 17:27:53Z bennylp $ */
3
* Copyright (C) 2008-2009 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
* Additional permission under GNU GPL version 3 section 7:
22
* If you modify this program, or any covered work, by linking or
23
* combining it with the OpenSSL project's OpenSSL library (or a
24
* modified version of that library), containing parts covered by the
25
* terms of the OpenSSL or SSLeay licenses, Teluu Inc. (http://www.teluu.com)
26
* grants you additional permission to convey the resulting work.
27
* Corresponding Source for a non-source form of such a combination
28
* shall include the source code for the parts of OpenSSL used as well
29
* as that of the covered work.
31
#ifndef __PJMEDIA_CLOCK_H__
32
#define __PJMEDIA_CLOCK_H__
38
#include <pjmedia/types.h>
42
* @defgroup PJMEDIA_PORT_CLOCK Clock/Timing
43
* @ingroup PJMEDIA_PORT
44
* @brief Various types of classes that provide timing.
47
The media clock/timing extends the media port concept that is explained
48
in @ref PJMEDIA_PORT. When clock is present in the ports
49
interconnection, media will flow automatically (and with correct timing too!)
50
from one media port to another.
52
There are few objects in PJMEDIA that are able to provide clock/timing
53
to media ports interconnection:
55
- @ref PJMED_SND_PORT\n
56
The sound device makes a good candidate as the clock source, and
57
PJMEDIA @ref PJMED_SND is designed so that it is able to invoke
58
operations according to timing driven by the sound hardware clock
59
(this may sound complicated, but actually it just means that
60
the sound device abstraction provides callbacks to be called when
61
it has/wants media frames).\n
62
See @ref PJMED_SND_PORT for more details.
64
- @ref PJMEDIA_MASTER_PORT\n
65
The master port uses @ref PJMEDIA_CLOCK as the clock source. By using
66
@ref PJMEDIA_MASTER_PORT, it is possible to interconnect passive
67
media ports and let the frames flow automatically in timely manner.\n
68
Please see @ref PJMEDIA_MASTER_PORT for more details.
75
* @addtogroup PJMEDIA_CLOCK Clock Generator
76
* @ingroup PJMEDIA_PORT_CLOCK
77
* @brief Interface for generating clock.
80
* The clock generator provides the application with media timing,
81
* and it is used by the @ref PJMEDIA_MASTER_PORT for its sound clock.
83
* The clock generator may be configured to run <b>asynchronously</b>
84
* (the default behavior) or <b>synchronously</b>. When it is run
85
* asynchronously, it will call the application's callback every time
86
* the clock <b>tick</b> expires. When it is run synchronously,
87
* application must continuously polls the clock generator to synchronize
95
* Opaque declaration for media clock.
97
typedef struct pjmedia_clock pjmedia_clock;
101
* Options when creating the clock.
103
enum pjmedia_clock_options
106
* Prevents the clock from running asynchronously. In this case,
107
* application must poll the clock continuously by calling
108
* #pjmedia_clock_wait() in order to synchronize timing.
110
PJMEDIA_CLOCK_NO_ASYNC = 1,
113
* Prevent the clock from setting it's thread to highest priority.
115
PJMEDIA_CLOCK_NO_HIGHEST_PRIO = 2
120
* Type of media clock callback.
122
* @param ts Current timestamp, in samples.
123
* @param user_data Application data that is passed when
124
* the clock was created.
126
typedef void pjmedia_clock_callback(const pj_timestamp *ts,
132
* Create media clock.
134
* @param pool Pool to allocate memory.
135
* @param clock_rate Number of samples per second.
136
* @param channel_count Number of channel.
137
* @param samples_per_frame Number of samples per frame. This argument
138
* along with clock_rate and channel_count, specifies
139
* the interval of each clock run (or clock ticks).
140
* @param options Bitmask of pjmedia_clock_options.
141
* @param cb Callback to be called for each clock tick.
142
* @param user_data User data, which will be passed to the callback.
143
* @param p_clock Pointer to receive the clock instance.
145
* @return PJ_SUCCESS on success, or the appropriate error
148
PJ_DECL(pj_status_t) pjmedia_clock_create( pj_pool_t *pool,
150
unsigned channel_count,
151
unsigned samples_per_frame,
153
pjmedia_clock_callback *cb,
155
pjmedia_clock **p_clock);
158
* Start the clock. For clock created with asynchronous flag set to TRUE,
159
* this may start a worker thread for the clock (depending on the
160
* backend clock implementation being used).
162
* @param clock The media clock.
164
* @return PJ_SUCCES on success.
166
PJ_DECL(pj_status_t) pjmedia_clock_start(pjmedia_clock *clock);
172
* @param clock The media clock.
174
* @return PJ_SUCCES on success.
176
PJ_DECL(pj_status_t) pjmedia_clock_stop(pjmedia_clock *clock);
181
* Poll the media clock, and execute the callback when the clock tick has
182
* elapsed. This operation is only valid if the clock is created with async
185
* @param clock The media clock.
186
* @param wait If non-zero, then the function will block until
187
* a clock tick elapsed and callback has been called.
188
* @param ts Optional argument to receive the current
191
* @return Non-zero if clock tick has elapsed, or FALSE if
192
* the function returns before a clock tick has
195
PJ_DECL(pj_bool_t) pjmedia_clock_wait(pjmedia_clock *clock,
203
* @param clock The media clock.
205
* @return PJ_SUCCES on success.
207
PJ_DECL(pj_status_t) pjmedia_clock_destroy(pjmedia_clock *clock);
218
#endif /* __PJMEDIA_CLOCK_H__ */