FreeSWITCH API Documentation  1.7.0
Functions
Timer Functions
+ Collaboration diagram for Timer Functions:

Functions

switch_status_t switch_core_timer_init (switch_timer_t *timer, const char *timer_name, int interval, int samples, switch_memory_pool_t *pool)
 Request a timer handle using given time module. More...
 
void switch_time_calibrate_clock (void)
 
switch_status_t switch_core_timer_next (switch_timer_t *timer)
 Wait for one cycle on an existing timer. More...
 
switch_status_t switch_core_timer_step (switch_timer_t *timer)
 Step the timer one step. More...
 
switch_status_t switch_core_timer_sync (switch_timer_t *timer)
 
switch_status_t switch_core_timer_check (switch_timer_t *timer, switch_bool_t step)
 Check if the current step has been exceeded. More...
 
switch_status_t switch_core_timer_destroy (switch_timer_t *timer)
 Destroy an allocated timer. More...
 

Detailed Description

Function Documentation

switch_status_t switch_core_timer_check ( switch_timer_t timer,
switch_bool_t  step 
)

Check if the current step has been exceeded.

Parameters
timerthe timer to wait on
stepincrement timer if a tick was detected
Returns
the newest sample count

Definition at line 103 of file switch_core_timer.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), and SWITCH_STATUS_GENERR.

104 {
105  if (!timer->timer_interface || !timer->timer_interface->timer_check) {
106  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
107  return SWITCH_STATUS_GENERR;
108  }
109 
110  return timer->timer_interface->timer_check(timer, step);
111 }
#define SWITCH_CHANNEL_LOG
switch_timer_interface_t * timer_interface
switch_status_t(* timer_check)(switch_timer_t *, switch_bool_t)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_core_timer_destroy ( switch_timer_t timer)

Destroy an allocated timer.

Parameters
timertimer to destroy
Returns
SWITCH_STATUS_SUCCESS after destruction

Definition at line 114 of file switch_core_timer.c.

References memset(), SWITCH_CHANNEL_LOG, switch_core_destroy_memory_pool, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, switch_test_flag, SWITCH_TIMER_FLAG_FREE_POOL, and UNPROTECT_INTERFACE.

Referenced by switch_ivr_clear_speech_cache(), switch_ivr_play_file(), switch_ivr_speak_text(), switch_media_handle_destroy(), switch_rtp_change_interval(), switch_rtp_destroy(), switch_rtp_udptl_mode(), and video_write_thread().

115 {
116  if (!timer->timer_interface || !timer->timer_interface->timer_destroy) {
117  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
118  return SWITCH_STATUS_GENERR;
119  }
120 
121  timer->timer_interface->timer_destroy(timer);
123 
126  }
127 
128  memset(timer, 0, sizeof(*timer));
129 
130  return SWITCH_STATUS_SUCCESS;
131 }
#define SWITCH_CHANNEL_LOG
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
#define UNPROTECT_INTERFACE(_it)
switch_timer_interface_t * timer_interface
switch_memory_pool_t * memory_pool
switch_status_t(* timer_destroy)(switch_timer_t *)
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
memset(buf, 0, buflen)
switch_status_t switch_core_timer_init ( switch_timer_t timer,
const char *  timer_name,
int  interval,
int  samples,
switch_memory_pool_t pool 
)

Request a timer handle using given time module.

Parameters
timera timer object to allocate to
timer_namethe name of the timer module to use
intervaldesired interval
samplesthe number of samples to increment on each cycle
poolthe memory pool to use for allocation
Returns

Definition at line 38 of file switch_core_timer.c.

References memset(), pool, SWITCH_CHANNEL_LOG, switch_core_new_memory_pool, switch_loadable_module_get_timer_interface(), SWITCH_LOG_ERROR, switch_log_printf(), switch_set_flag, SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, SWITCH_TIMER_FLAG_FREE_POOL, switch_timer_interface::timer_init, and UNPROTECT_INTERFACE.

Referenced by switch_core_session_write_video_frame(), switch_ivr_play_file(), switch_ivr_speak_text(), switch_rtp_change_interval(), switch_rtp_create(), and video_write_thread().

40 {
41  switch_timer_interface_t *timer_interface;
42  switch_status_t status;
43  memset(timer, 0, sizeof(*timer));
44  if ((timer_interface = switch_loadable_module_get_timer_interface(timer_name)) == 0 || !timer_interface->timer_init) {
45  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "invalid timer %s!\n", timer_name);
46  return SWITCH_STATUS_GENERR;
47  }
48 
49  timer->interval = interval;
50  timer->samples = samples;
51  timer->samplecount = samples;
52  timer->timer_interface = timer_interface;
53 
54  if (pool) {
55  timer->memory_pool = pool;
56  } else {
59  return status;
60  }
62  }
63 
64  return timer->timer_interface->timer_init(timer);
65 }
switch_timer_interface_t * switch_loadable_module_get_timer_interface(const char *name)
Retrieve the timer interface by it's registered name.
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:631
#define SWITCH_CHANNEL_LOG
switch_memory_pool_t * pool
A table of functions that a timer module implements.
#define UNPROTECT_INTERFACE(_it)
switch_timer_interface_t * timer_interface
switch_memory_pool_t * memory_pool
switch_status_t(* timer_init)(switch_timer_t *)
switch_status_t
Common return values.
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
memset(buf, 0, buflen)
switch_status_t switch_core_timer_next ( switch_timer_t timer)

Wait for one cycle on an existing timer.

Parameters
timerthe timer to wait on
Returns
the newest sample count

Definition at line 67 of file switch_core_timer.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

Referenced by rtp_common_read(), switch_ivr_play_file(), switch_ivr_speak_text_handle(), and video_write_thread().

68 {
69  if (!timer->timer_interface || !timer->timer_interface->timer_next) {
70  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
71  return SWITCH_STATUS_GENERR;
72  }
73 
74  if (timer->timer_interface->timer_next(timer) == SWITCH_STATUS_SUCCESS) {
75  return SWITCH_STATUS_SUCCESS;
76  } else {
77  return SWITCH_STATUS_GENERR;
78  }
79 
80 }
#define SWITCH_CHANNEL_LOG
switch_status_t(* timer_next)(switch_timer_t *)
switch_timer_interface_t * timer_interface
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_core_timer_step ( switch_timer_t timer)

Step the timer one step.

Parameters
timerthe timer to wait on
Returns
the newest sample count

Definition at line 82 of file switch_core_timer.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), and SWITCH_STATUS_GENERR.

83 {
84  if (!timer->timer_interface || !timer->timer_interface->timer_step) {
85  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
86  return SWITCH_STATUS_GENERR;
87  }
88 
89  return timer->timer_interface->timer_step(timer);
90 }
#define SWITCH_CHANNEL_LOG
switch_timer_interface_t * timer_interface
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t(* timer_step)(switch_timer_t *)
switch_status_t switch_core_timer_sync ( switch_timer_t timer)

Definition at line 93 of file switch_core_timer.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), and SWITCH_STATUS_GENERR.

Referenced by read_rtp_packet(), rtp_common_read(), rtp_common_write(), switch_core_session_write_video_frame(), switch_ivr_play_file(), switch_ivr_speak_text(), and switch_rtp_get_media_timer().

94 {
95  if (!timer->timer_interface || !timer->timer_interface->timer_sync) {
96  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Timer is not properly configured.\n");
97  return SWITCH_STATUS_GENERR;
98  }
99 
100  return timer->timer_interface->timer_sync(timer);
101 }
#define SWITCH_CHANNEL_LOG
switch_timer_interface_t * timer_interface
switch_status_t(* timer_sync)(switch_timer_t *)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
void switch_time_calibrate_clock ( void  )

Definition at line 207 of file switch_time.c.

References average_time(), calc_step, do_sleep(), switch_runtime::microseconds_per_tick, OFFSET, runtime, SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), SWITCH_LOG_WARNING, switch_time_set_cond_yield(), and SWITCH_TRUE.

Referenced by switch_core_session_ctl(), and SWITCH_MODULE_LOAD_FUNCTION().

208 {
209  int x;
210  switch_interval_time_t avg, val = 1000, want = 1000;
211  int over = 0, under = 0, good = 0, step = 50, diff = 0, retry = 0, lastgood = 0, one_k = 0;
212 
213 #ifdef HAVE_CLOCK_GETRES
214  struct timespec ts;
215  long res = 0;
216  clock_getres(CLOCK_MONOTONIC, &ts);
217  res = ts.tv_nsec / 1000;
218 
219 
220  if (res > 900 && res < 1100) {
221  one_k = 1;
222  }
223 
224  if (res > 1500) {
226  "Timer resolution of %ld microseconds detected!\n"
227  "Do you have your kernel timer frequency set to lower than 1,000Hz? "
228  "You may experience audio problems. Step MS %d\n", ts.tv_nsec / 1000, runtime.microseconds_per_tick / 1000);
229  do_sleep(5000000);
231  return;
232  }
233 #endif
234 
235  top:
236  val = 1000;
237  step = 50;
238  over = under = good = 0;
239  OFFSET = 0;
240 
241  for (x = 0; x < 100; x++) {
242  avg = average_time(val, 50);
243  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Test: %ld Average: %ld Step: %d\n", (long) val, (long) avg, step);
244 
245  diff = abs((int) (want - avg));
246  if (diff > 1500) {
248  "Abnormally large timer gap %d detected!\n"
249  "Do you have your kernel timer frequency set to lower than 1,000Hz? You may experience audio problems.\n", diff);
250  do_sleep(5000000);
252  return;
253  }
254 
255  if (diff <= 100) {
256  lastgood = (int) val;
257  }
258 
259  if (diff <= 2) {
260  under = over = 0;
261  lastgood = (int) val;
262  if (++good > 10) {
263  break;
264  }
265  } else if (avg > want) {
266  if (under) {
267  calc_step();
268  }
269  under = good = 0;
270  if ((val - step) < 0) {
271  if (++retry > 2)
272  break;
273  goto top;
274  }
275  val -= step;
276  over++;
277  } else if (avg < want) {
278  if (over) {
279  calc_step();
280  }
281  over = good = 0;
282  if ((val - step) < 0) {
283  if (++retry > 2)
284  break;
285  goto top;
286  }
287  val += step;
288  under++;
289  }
290  }
291 
292  if (good >= 10) {
293  OFFSET = (int) (want - val);
294  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer offset of %d calculated\n", OFFSET);
295  } else if (lastgood) {
296  OFFSET = (int) (want - lastgood);
297  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer offset of %d calculated (fallback)\n", OFFSET);
299  } else if (one_k) {
300  OFFSET = 900;
301  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer offset CANNOT BE DETECTED, forcing OFFSET to 900\n");
303  } else {
304  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer offset NOT calculated\n");
306  }
307 }
#define SWITCH_CHANNEL_LOG
struct switch_runtime runtime
Definition: switch_core.c:64
uint32_t microseconds_per_tick
static switch_interval_time_t average_time(switch_interval_time_t t, int reps)
Definition: switch_time.c:190
static int OFFSET
Definition: switch_time.c:84
int64_t switch_interval_time_t
Definition: switch_apr.h:191
void switch_time_set_cond_yield(switch_bool_t enable)
Definition: switch_time.c:372
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define calc_step()
Definition: switch_time.c:206
static void do_sleep(switch_interval_time_t t)
Definition: switch_time.c:150