12024
|
1 /*
|
|
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
|
|
5
|
|
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.
|
|
10
|
|
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.
|
|
15
|
|
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
|
|
19 */
|
|
20
|
|
21
|
|
22 #include "mstimer.h"
|
|
23 #include <unistd.h>
|
|
24 #include <sys/types.h>
|
|
25 #include <sys/time.h>
|
|
26 #include <signal.h>
|
|
27
|
|
28 static MSTimerClass *ms_timer_class=NULL;
|
|
29
|
|
30
|
|
31 void ms_timer_init(MSTimer *sync)
|
|
32 {
|
|
33 ms_sync_init(MS_SYNC(sync));
|
|
34 MS_SYNC(sync)->attached_filters=sync->filters;
|
|
35 memset(sync->filters,0,MSTIMER_MAX_FILTERS*sizeof(MSFilter*));
|
|
36 MS_SYNC(sync)->samples_per_tick=160;
|
|
37 ms_timer_set_interval(sync,20);
|
|
38 sync->state=MS_TIMER_STOPPED;
|
|
39 }
|
|
40
|
|
41 void ms_timer_class_init(MSTimerClass *klass)
|
|
42 {
|
|
43 ms_sync_class_init(MS_SYNC_CLASS(klass));
|
|
44 MS_SYNC_CLASS(klass)->max_filters=MSTIMER_MAX_FILTERS;
|
|
45 MS_SYNC_CLASS(klass)->synchronize=(MSSyncSyncFunc)ms_timer_synchronize;
|
|
46 MS_SYNC_CLASS(klass)->destroy=(MSSyncDestroyFunc)ms_timer_destroy;
|
|
47 /* no need to overload these function*/
|
|
48 MS_SYNC_CLASS(klass)->attach=ms_sync_attach_generic;
|
|
49 MS_SYNC_CLASS(klass)->detach=ms_sync_detach_generic;
|
|
50 }
|
|
51
|
|
52 void ms_timer_destroy(MSTimer *timer)
|
|
53 {
|
|
54 g_free(timer);
|
|
55 }
|
|
56
|
|
57
|
|
58 void ms_timer_synchronize(MSTimer *timer)
|
|
59 {
|
|
60 //printf("ticks=%i \n",MS_SYNC(timer)->ticks);
|
|
61 if (timer->state==MS_TIMER_STOPPED){
|
|
62 timer->state=MS_TIMER_RUNNING;
|
|
63 gettimeofday(&timer->orig,NULL);
|
|
64 timer->sync.time=0;
|
|
65 }
|
|
66 else {
|
|
67 gint32 diff,time;
|
|
68 struct timeval tv,cur;
|
|
69
|
|
70 gettimeofday(&cur,NULL);
|
|
71 time=((cur.tv_usec-timer->orig.tv_usec)/1000 ) + ((cur.tv_sec-timer->orig.tv_sec)*1000 );
|
|
72 if ( (diff=time-timer->sync.time)>50){
|
|
73 g_warning("Must catchup %i miliseconds.",diff);
|
|
74 }
|
|
75 while((diff = timer->sync.time-time) > 0)
|
|
76 {
|
|
77 tv.tv_sec = diff/1000;
|
|
78 tv.tv_usec = (diff%1000)*1000;
|
|
79 select(0,NULL,NULL,NULL,&tv);
|
|
80 gettimeofday(&cur,NULL);
|
|
81 time=((cur.tv_usec-timer->orig.tv_usec)/1000 ) + ((cur.tv_sec-timer->orig.tv_sec)*1000 );
|
|
82 }
|
|
83 }
|
|
84 timer->sync.time+=timer->milisec;
|
|
85 return;
|
|
86 }
|
|
87
|
|
88
|
|
89 MSSync *ms_timer_new()
|
|
90 {
|
|
91 MSTimer *timer;
|
|
92
|
|
93 timer=g_malloc(sizeof(MSTimer));
|
|
94 ms_timer_init(timer);
|
|
95 if (ms_timer_class==NULL)
|
|
96 {
|
|
97 ms_timer_class=g_new(MSTimerClass,1);
|
|
98 ms_timer_class_init(ms_timer_class);
|
|
99 }
|
|
100 MS_SYNC(timer)->klass=MS_SYNC_CLASS(ms_timer_class);
|
|
101 return(MS_SYNC(timer));
|
|
102 }
|
|
103
|
|
104 void ms_timer_set_interval(MSTimer *timer, int milisec)
|
|
105 {
|
|
106
|
|
107 MS_SYNC(timer)->ticks=0;
|
|
108 MS_SYNC(timer)->interval=milisec;
|
|
109 timer->interval.tv_sec=milisec/1000;
|
|
110 timer->interval.tv_usec=(milisec % 1000)*1000;
|
|
111 timer->milisec=milisec;
|
|
112
|
|
113
|
|
114 }
|