2
The mediastreamer library aims at providing modular media processing and I/O
3
for linphone, but also for any telephony application.
4
Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2.1 of the License, or (at your option) any later version.
11
This library 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 GNU
14
Lesser General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
#include <sys/types.h>
28
static MSTimerClass *ms_timer_class=NULL;
30
static void dummy_handler(int signum)
32
//posix_timer_time+=POSIXTIMER_INTERVAL/1000;
34
//printf("tick !!!\n");
37
static void posix_timer_init(MSTimer *posix_timer)
39
struct itimerval timer;
41
/* block the SIGALRM signal
43
sigaddset(&set,SIGALRM);
44
sigprocmask(SIG_BLOCK,&set,NULL);*/
46
timer.it_value.tv_sec=posix_timer->interval.tv_sec;
47
timer.it_value.tv_usec=posix_timer->interval.tv_usec;
48
timer.it_interval.tv_sec=posix_timer->interval.tv_sec;
49
timer.it_interval.tv_usec=posix_timer->interval.tv_usec;
51
signal(SIGALRM,dummy_handler);
52
setitimer(ITIMER_REAL,&timer,NULL);
53
posix_timer->state=MS_TIMER_RUNNING;
54
gettimeofday(&posix_timer->orig,NULL);
55
posix_timer->late_ticks=0;
61
static void posix_timer_do(MSTimer *posix_timer)
67
if (posix_timer->late_ticks>0){
68
posix_timer->late_ticks--;
69
posix_timer->time+=posix_timer->milisec;
70
MS_SYNC(posix_timer)->time=posix_timer->time;
75
sigdelset(&set,SIGALRM);
77
//printf("time=%i\n",time);
79
/* here we receive the signal */
80
posix_timer->time+=posix_timer->milisec;
81
MS_SYNC(posix_timer)->time=posix_timer->time;
83
gettimeofday(&posix_timer->cur,NULL);
84
time=((posix_timer->cur.tv_usec-posix_timer->orig.tv_usec)/1000 ) + ((posix_timer->cur.tv_sec-posix_timer->orig.tv_sec)*1000 );
85
diff=time-posix_timer->time;
87
if (diff> posix_timer->milisec){
88
posix_timer->late_ticks=diff/posix_timer->milisec;
89
if (posix_timer->late_ticks>5) g_warning("MSTimer: must catchup %i ticks.",posix_timer->late_ticks);
94
static void posix_timer_uninit(MSTimer *posix_timer)
96
struct itimerval timer;
98
memset(&timer,0,sizeof(struct itimerval));
99
setitimer(ITIMER_REAL,&timer,NULL);
100
posix_timer->state=MS_TIMER_STOPPED;
104
void ms_timer_init(MSTimer *sync)
106
ms_sync_init(MS_SYNC(sync));
107
MS_SYNC(sync)->attached_filters=sync->filters;
108
memset(sync->filters,0,MSTIMER_MAX_FILTERS*sizeof(MSFilter*));
109
MS_SYNC(sync)->samples_per_tick=160;
110
ms_timer_set_interval(sync,20);
111
sync->state=MS_TIMER_STOPPED;
114
void ms_timer_class_init(MSTimerClass *klass)
116
ms_sync_class_init(MS_SYNC_CLASS(klass));
117
MS_SYNC_CLASS(klass)->max_filters=MSTIMER_MAX_FILTERS;
118
MS_SYNC_CLASS(klass)->synchronize=(MSSyncSyncFunc)ms_timer_synchronize;
119
MS_SYNC_CLASS(klass)->destroy=(MSSyncDestroyFunc)ms_timer_destroy;
120
/* no need to overload these function*/
121
MS_SYNC_CLASS(klass)->attach=ms_sync_attach_generic;
122
MS_SYNC_CLASS(klass)->detach=ms_sync_detach_generic;
125
void ms_timer_destroy(MSTimer *timer)
131
void ms_timer_synchronize(MSTimer *timer)
133
//printf("ticks=%i \n",MS_SYNC(timer)->ticks);
134
if (timer->state==MS_TIMER_STOPPED){
135
posix_timer_init(timer);
136
//printf("ms_timer_synchronize: starting timer.\n");
138
else posix_timer_do(timer);
143
MSSync *ms_timer_new()
147
timer=g_malloc(sizeof(MSTimer));
148
ms_timer_init(timer);
149
if (ms_timer_class==NULL)
151
ms_timer_class=g_new(MSTimerClass,1);
152
ms_timer_class_init(ms_timer_class);
154
MS_SYNC(timer)->klass=MS_SYNC_CLASS(ms_timer_class);
155
return(MS_SYNC(timer));
158
void ms_timer_set_interval(MSTimer *timer, int milisec)
161
MS_SYNC(timer)->ticks=0;
162
MS_SYNC(timer)->interval=milisec;
163
timer->interval.tv_sec=milisec/1000;
164
timer->interval.tv_usec=(milisec % 1000)*1000;
165
timer->milisec=milisec;