RTS API Documentation  1.10.11
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_core_timer_check()

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(), SWITCH_STATUS_GENERR, switch_timer_interface::timer_check, and switch_timer::timer_interface.

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_core_timer_destroy()

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 switch_timer::memory_pool, 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, switch_timer_interface::timer_destroy, switch_timer::timer_interface, and UNPROTECT_INTERFACE.

Referenced by audio_write_thread(), 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_text_factory_destroy(), switch_rtp_udptl_mode(), video_bug_thread(), 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:642
#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:693
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_core_timer_init()

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 switch_timer::interval, switch_timer::memory_pool, memset(), pool, switch_timer::samplecount, switch_timer::samples, 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, switch_timer::timer_interface, and UNPROTECT_INTERFACE.

Referenced by audio_write_thread(), switch_core_session_write_video_frame(), switch_ivr_play_file(), switch_ivr_speak_text(), switch_rtp_change_interval(), switch_rtp_create(), switch_rtp_text_factory_create(), video_bug_thread(), 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:633
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:700
#define SWITCH_CHANNEL_LOG
switch_memory_pool_t * pool
A table of functions that a timer module implements.
switch_status_t(* timer_init)(switch_timer_t *)
#define UNPROTECT_INTERFACE(_it)
switch_timer_interface_t * timer_interface
switch_memory_pool_t * memory_pool
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_core_timer_next()

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, SWITCH_STATUS_SUCCESS, switch_timer::timer_interface, and switch_timer_interface::timer_next.

Referenced by audio_write_thread(), get_next_write_ts(), rtp_common_read(), switch_ivr_play_file(), switch_ivr_speak_text_handle(), video_bug_thread(), 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_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_next)(switch_timer_t *)

◆ switch_core_timer_step()

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(), SWITCH_STATUS_GENERR, switch_timer::timer_interface, and switch_timer_interface::timer_step.

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
switch_status_t(* timer_step)(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.

◆ switch_core_timer_sync()

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(), SWITCH_STATUS_GENERR, switch_timer::timer_interface, and switch_timer_interface::timer_sync.

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

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 }
switch_status_t(* timer_sync)(switch_timer_t *)
#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_time_calibrate_clock()

void switch_time_calibrate_clock ( void  )

Definition at line 208 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().

209 {
210  int x;
211  switch_interval_time_t avg, val = 1000, want = 1000;
212  int over = 0, under = 0, good = 0, step = 50, diff = 0, retry = 0, lastgood = 0, one_k = 0;
213 
214 #ifdef HAVE_CLOCK_GETRES
215  struct timespec ts;
216  long res = 0;
217  clock_getres(CLOCK_MONOTONIC, &ts);
218  res = ts.tv_nsec / 1000;
219 
220 
221  if (res > 900 && res < 1100) {
222  one_k = 1;
223  }
224 
225  if (res > 1500) {
227  "Timer resolution of %ld microseconds detected!\n"
228  "Do you have your kernel timer frequency set to lower than 1,000Hz? "
229  "You may experience audio problems. Step MS %d\n", ts.tv_nsec / 1000, runtime.microseconds_per_tick / 1000);
230  do_sleep(5000000);
232  return;
233  }
234 #endif
235 
236  top:
237  val = 1000;
238  step = 50;
239  over = under = good = 0;
240  OFFSET = 0;
241 
242  for (x = 0; x < 100; x++) {
243  avg = average_time(val, 50);
244  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Test: %ld Average: %ld Step: %d\n", (long) val, (long) avg, step);
245 
246  diff = abs((int) (want - avg));
247  if (diff > 1500) {
249  "Abnormally large timer gap %d detected!\n"
250  "Do you have your kernel timer frequency set to lower than 1,000Hz? You may experience audio problems.\n", diff);
251  do_sleep(5000000);
253  return;
254  }
255 
256  if (diff <= 100) {
257  lastgood = (int) val;
258  }
259 
260  if (diff <= 2) {
261  under = over = 0;
262  lastgood = (int) val;
263  if (++good > 10) {
264  break;
265  }
266  } else if (avg > want) {
267  if (under) {
268  calc_step();
269  }
270  under = good = 0;
271  if ((val - step) < 0) {
272  if (++retry > 2)
273  break;
274  goto top;
275  }
276  val -= step;
277  over++;
278  } else if (avg < want) {
279  if (over) {
280  calc_step();
281  }
282  over = good = 0;
283  if ((val - step) < 0) {
284  if (++retry > 2)
285  break;
286  goto top;
287  }
288  val += step;
289  under++;
290  }
291  }
292 
293  if (good >= 10) {
294  OFFSET = (int) (want - val);
295  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer offset of %d calculated\n", OFFSET);
296  } else if (lastgood) {
297  OFFSET = (int) (want - lastgood);
298  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer offset of %d calculated (fallback)\n", OFFSET);
300  } else if (one_k) {
301  OFFSET = 900;
302  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer offset CANNOT BE DETECTED, forcing OFFSET to 900\n");
304  } else {
305  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer offset NOT calculated\n");
307  }
308 }
#define SWITCH_CHANNEL_LOG
struct switch_runtime runtime
Definition: switch_core.c:86
uint32_t microseconds_per_tick
static switch_interval_time_t average_time(switch_interval_time_t t, int reps)
Definition: switch_time.c:191
static int OFFSET
Definition: switch_time.c:85
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:373
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:207
static void do_sleep(switch_interval_time_t t)
Definition: switch_time.c:151