~ubuntu-branches/ubuntu/trusty/linphone/trusty

« back to all changes in this revision

Viewing changes to mediastreamer2/src/base/msticker.c

  • Committer: Package Import Robot
  • Author(s): Luk Claes
  • Date: 2013-09-11 19:08:43 UTC
  • mfrom: (1.1.19) (16.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20130911190843-fkydjxsdvy1fmx24
Tags: 3.6.1-2.1
* Non-maintainer upload.
* Apply Sebastian Ramacher's patch to fix FTBFS (Closes: #720668).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
mediastreamer2 library - modular sound and video processing and streaming
 
3
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)
 
4
 
 
5
This program is free software; you can redistribute it and/or
 
6
modify it under the terms of the GNU General Public License
 
7
as published by the Free Software Foundation; either version 2
 
8
of the License, or (at your option) any later version.
 
9
 
 
10
This program is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
*/
 
19
 
 
20
#include "mediastreamer2/msticker.h"
 
21
 
 
22
#ifndef WIN32
 
23
#include <sys/time.h>
 
24
#include <sys/resource.h>
 
25
#endif
 
26
 
 
27
static const double smooth_coef=0.9;
 
28
 
 
29
#ifndef TICKER_MEASUREMENTS
 
30
 
 
31
#define TICKER_MEASUREMENTS 1
 
32
 
 
33
#if defined(__ARM_ARCH__) 
 
34
#       if __ARM_ARCH__ < 7
 
35
/* as MSTicker load computation requires floating point, we prefer to disable it on ARM processors without FPU*/
 
36
#               undef TICKER_MEASUREMENTS
 
37
#               define TICKER_MEASUREMENTS 0 
 
38
#       endif
 
39
#endif
 
40
 
 
41
#endif
 
42
 
 
43
#define TICKER_INTERVAL 10
 
44
 
 
45
static void * ms_ticker_run(void *s);
 
46
static uint64_t get_cur_time_ms(void *);
 
47
static int wait_next_tick(void *, uint64_t virt_ticker_time);
 
48
static void remove_tasks_for_filter(MSTicker *ticker, MSFilter *f);
 
49
 
 
50
static void ms_ticker_start(MSTicker *s){
 
51
        s->run=TRUE;
 
52
        ms_thread_create(&s->thread,NULL,ms_ticker_run,s);
 
53
}
 
54
 
 
55
static void ms_ticker_init(MSTicker *ticker, const MSTickerParams *params)
 
56
{
 
57
        ms_mutex_init(&ticker->lock,NULL);
 
58
        ticker->execution_list=NULL;
 
59
        ticker->task_list=NULL;
 
60
        ticker->ticks=1;
 
61
        ticker->time=0;
 
62
        ticker->interval=TICKER_INTERVAL;
 
63
        ticker->run=FALSE;
 
64
        ticker->exec_id=0;
 
65
        ticker->get_cur_time_ptr=&get_cur_time_ms;
 
66
        ticker->get_cur_time_data=NULL;
 
67
        ticker->name=ms_strdup(params->name);
 
68
        ticker->av_load=0;
 
69
        ticker->prio=params->prio;
 
70
        ticker->wait_next_tick=wait_next_tick;
 
71
        ticker->wait_next_tick_data=ticker;
 
72
        ms_ticker_start(ticker);
 
73
}
 
74
 
 
75
MSTicker *ms_ticker_new(){
 
76
        MSTickerParams params;
 
77
        params.name="MSTicker";
 
78
        params.prio=MS_TICKER_PRIO_NORMAL;
 
79
        return ms_ticker_new_with_params(&params);
 
80
}
 
81
 
 
82
MSTicker *ms_ticker_new_with_params(const MSTickerParams *params){
 
83
        MSTicker *obj=(MSTicker *)ms_new(MSTicker,1);
 
84
        ms_ticker_init(obj,params);
 
85
        return obj;
 
86
}
 
87
 
 
88
static void ms_ticker_stop(MSTicker *s){
 
89
        ms_mutex_lock(&s->lock);
 
90
        s->run=FALSE;
 
91
        ms_mutex_unlock(&s->lock);
 
92
        if(s->thread)
 
93
                ms_thread_join(s->thread,NULL);
 
94
}
 
95
 
 
96
void ms_ticker_set_name(MSTicker *s, const char *name){
 
97
        if (s->name) ms_free(s->name);
 
98
        s->name=ms_strdup(name);
 
99
}
 
100
 
 
101
void ms_ticker_set_priority(MSTicker *ticker, MSTickerPrio prio){
 
102
        ticker->prio=prio;
 
103
}
 
104
 
 
105
static void ms_ticker_uninit(MSTicker *ticker)
 
106
{
 
107
        ms_ticker_stop(ticker);
 
108
        ms_free(ticker->name);
 
109
        ms_mutex_destroy(&ticker->lock);
 
110
}
 
111
 
 
112
void ms_ticker_destroy(MSTicker *ticker){
 
113
        ms_ticker_uninit(ticker);
 
114
        ms_free(ticker);
 
115
}
 
116
 
 
117
 
 
118
static MSList *get_sources(MSList *filters){
 
119
        MSList *sources=NULL;
 
120
        MSFilter *f;
 
121
        for(;filters!=NULL;filters=filters->next){
 
122
                f=(MSFilter*)filters->data;
 
123
                if (f->desc->ninputs==0){
 
124
                        sources=ms_list_append(sources,f);
 
125
                }
 
126
        }
 
127
        return sources;
 
128
}
 
129
 
 
130
int ms_ticker_attach(MSTicker *ticker, MSFilter *f){
 
131
        return ms_ticker_attach_multiple(ticker,f,NULL);
 
132
}
 
133
 
 
134
int ms_ticker_attach_multiple(MSTicker *ticker,MSFilter *f,...)
 
135
{
 
136
        MSList *sources=NULL;
 
137
        MSList *filters=NULL;
 
138
        MSList *it;
 
139
        MSList *total_sources=NULL;
 
140
        va_list l;
 
141
 
 
142
        va_start(l,f);
 
143
 
 
144
        do{
 
145
                if (f->ticker==NULL) {
 
146
                        filters=ms_filter_find_neighbours(f);
 
147
                        sources=get_sources(filters);
 
148
                        if (sources==NULL){
 
149
                                ms_fatal("No sources found around filter %s",f->desc->name);
 
150
                                ms_list_free(filters);
 
151
                                break;
 
152
                        }
 
153
                        /*run preprocess on each filter: */
 
154
                        for(it=filters;it!=NULL;it=it->next)
 
155
                                ms_filter_preprocess((MSFilter*)it->data,ticker);
 
156
                        ms_list_free(filters);
 
157
                        total_sources=ms_list_concat(total_sources,sources);                    
 
158
                }else ms_message("Filter %s is already being scheduled; nothing to do.",f->desc->name);
 
159
        }while ((f=va_arg(l,MSFilter*))!=NULL);
 
160
        va_end(l);
 
161
        if (total_sources){
 
162
                ms_mutex_lock(&ticker->lock);
 
163
                ticker->execution_list=ms_list_concat(ticker->execution_list,total_sources);
 
164
                ms_mutex_unlock(&ticker->lock);
 
165
        }
 
166
        return 0;
 
167
}
 
168
 
 
169
static void call_postprocess(MSFilter *f){
 
170
        if (f->postponed_task) remove_tasks_for_filter(f->ticker,f);
 
171
        ms_filter_postprocess(f);
 
172
}
 
173
 
 
174
int ms_ticker_detach(MSTicker *ticker,MSFilter *f){
 
175
        MSList *sources=NULL;
 
176
        MSList *filters=NULL;
 
177
        MSList *it;
 
178
 
 
179
        if (f->ticker==NULL) {
 
180
                ms_message("Filter %s is not scheduled; nothing to do.",f->desc->name);
 
181
                return 0;
 
182
        }
 
183
 
 
184
        ms_mutex_lock(&ticker->lock);
 
185
 
 
186
        filters=ms_filter_find_neighbours(f);
 
187
        sources=get_sources(filters);
 
188
        if (sources==NULL){
 
189
                ms_fatal("No sources found around filter %s",f->desc->name);
 
190
                ms_list_free(filters);
 
191
                ms_mutex_unlock(&ticker->lock);
 
192
                return -1;
 
193
        }
 
194
 
 
195
        for(it=sources;it!=NULL;it=ms_list_next(it)){
 
196
                ticker->execution_list=ms_list_remove(ticker->execution_list,it->data);
 
197
        }
 
198
        ms_mutex_unlock(&ticker->lock);
 
199
        ms_list_for_each(filters,(void (*)(void*))call_postprocess);
 
200
        ms_list_free(filters);
 
201
        ms_list_free(sources);
 
202
        return 0;
 
203
}
 
204
 
 
205
 
 
206
static bool_t filter_can_process(MSFilter *f, int tick){
 
207
        /* look if filters before this one have run */
 
208
        int i;
 
209
        MSQueue *l;
 
210
        for(i=0;i<f->desc->ninputs;i++){
 
211
                l=f->inputs[i];
 
212
                if (l!=NULL){
 
213
                        if (l->prev.filter->last_tick!=tick) return FALSE;
 
214
                }
 
215
        }
 
216
        return TRUE;
 
217
}
 
218
 
 
219
static void call_process(MSFilter *f){
 
220
        bool_t process_done=FALSE;
 
221
        if (f->desc->ninputs==0 || f->desc->flags & MS_FILTER_IS_PUMP){
 
222
                ms_filter_process(f);
 
223
        }else{
 
224
                while (ms_filter_inputs_have_data(f)) {
 
225
                        if (process_done){
 
226
                                ms_warning("Re-scheduling filter %s: all data should be consumed in one process call, so fix it.",f->desc->name);
 
227
                        }
 
228
                        ms_filter_process(f);
 
229
                        if (f->postponed_task) break;
 
230
                        process_done=TRUE;
 
231
                }
 
232
        }
 
233
}
 
234
 
 
235
static void run_graph(MSFilter *f, MSTicker *s, MSList **unschedulable, bool_t force_schedule){
 
236
        int i;
 
237
        MSQueue *l;
 
238
        if (f->last_tick!=s->ticks ){
 
239
                if (filter_can_process(f,s->ticks) || force_schedule) {
 
240
                        /* this is a candidate */
 
241
                        f->last_tick=s->ticks;
 
242
                        call_process(f);        
 
243
                        /* now recurse to next filters */               
 
244
                        for(i=0;i<f->desc->noutputs;i++){
 
245
                                l=f->outputs[i];
 
246
                                if (l!=NULL){
 
247
                                        run_graph(l->next.filter,s,unschedulable, force_schedule);
 
248
                                }
 
249
                        }
 
250
                }else{
 
251
                        /* this filter has not all inputs that have been filled by filters before it. */
 
252
                        *unschedulable=ms_list_prepend(*unschedulable,f);
 
253
                }
 
254
        }
 
255
}
 
256
 
 
257
static void run_graphs(MSTicker *s, MSList *execution_list, bool_t force_schedule){
 
258
        MSList *it;
 
259
        MSList *unschedulable=NULL;
 
260
        for(it=execution_list;it!=NULL;it=it->next){
 
261
                run_graph((MSFilter*)it->data,s,&unschedulable,force_schedule);
 
262
        }
 
263
        /* filters that are part of a loop haven't been called in process() because one of their input refers to a filter that could not be scheduled (because they could not be scheduled themselves)... Do you understand ?*/
 
264
        /* we resolve this by simply assuming that they must be called anyway 
 
265
        for the loop to run correctly*/
 
266
        /* we just recall run_graphs on them, as if they were source filters */
 
267
        if (unschedulable!=NULL) {
 
268
                run_graphs(s,unschedulable,TRUE);
 
269
                ms_list_free(unschedulable);
 
270
        }
 
271
}
 
272
 
 
273
static void run_tasks(MSTicker *ticker){
 
274
        MSList *elem,*prevelem=NULL;
 
275
        for (elem=ticker->task_list;elem!=NULL;){
 
276
                MSFilterTask *t=(MSFilterTask*)elem->data;
 
277
                ms_filter_task_process(t);
 
278
                ms_free(t);
 
279
                prevelem=elem;
 
280
                elem=elem->next;
 
281
                ms_free(prevelem);
 
282
        }
 
283
        ticker->task_list=NULL;
 
284
}
 
285
 
 
286
static void remove_tasks_for_filter(MSTicker *ticker, MSFilter *f){
 
287
        MSList *elem,*nextelem;
 
288
        for (elem=ticker->task_list;elem!=NULL;elem=nextelem){
 
289
                MSFilterTask *t=(MSFilterTask*)elem->data;
 
290
                nextelem=elem->next;
 
291
                if (t->f==f){
 
292
                        ticker->task_list=ms_list_remove_link(ticker->task_list,elem);
 
293
                        ms_free(t);
 
294
                }
 
295
        }
 
296
}
 
297
 
 
298
static uint64_t get_cur_time_ms(void *unused){
 
299
        MSTimeSpec ts;
 
300
        ms_get_cur_time(&ts);
 
301
        return (ts.tv_sec*1000LL) + ((ts.tv_nsec+500000LL)/1000000LL);
 
302
}
 
303
 
 
304
static void sleepMs(int ms){
 
305
#ifdef WIN32
 
306
        Sleep(ms);
 
307
#else
 
308
        struct timespec ts;
 
309
        ts.tv_sec=0;
 
310
        ts.tv_nsec=ms*1000000LL;
 
311
        nanosleep(&ts,NULL);
 
312
#endif
 
313
}
 
314
 
 
315
static int set_high_prio(MSTicker *obj){
 
316
        int precision=2;
 
317
        int prio=obj->prio;
 
318
        
 
319
        if (prio>MS_TICKER_PRIO_NORMAL){
 
320
#ifdef WIN32
 
321
                MMRESULT mm;
 
322
                TIMECAPS ptc;
 
323
                mm=timeGetDevCaps(&ptc,sizeof(ptc));
 
324
                if (mm==0){
 
325
                        if (ptc.wPeriodMin<(UINT)precision)
 
326
                                ptc.wPeriodMin=precision;
 
327
                        else
 
328
                                precision = ptc.wPeriodMin;
 
329
                        mm=timeBeginPeriod(ptc.wPeriodMin);
 
330
                        if (mm!=TIMERR_NOERROR){
 
331
                                ms_warning("timeBeginPeriod failed.");
 
332
                        }
 
333
                        ms_message("win32 timer resolution set to %i ms",ptc.wPeriodMin);
 
334
                }else{
 
335
                        ms_warning("timeGetDevCaps failed.");
 
336
                }
 
337
 
 
338
                if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST)){
 
339
                        ms_warning("SetThreadPriority() failed (%d)\n", (int)GetLastError());
 
340
                }
 
341
#else
 
342
                struct sched_param param;
 
343
                int policy=SCHED_RR;
 
344
                memset(&param,0,sizeof(param));
 
345
                int result=0;
 
346
                char* env_prio_c=NULL;
 
347
                int min_prio, max_prio, env_prio;
 
348
 
 
349
                if (prio==MS_TICKER_PRIO_REALTIME)
 
350
                        policy=SCHED_FIFO;
 
351
                
 
352
                min_prio = sched_get_priority_min(policy);
 
353
                max_prio = sched_get_priority_max(policy);
 
354
                env_prio_c = getenv("MS_TICKER_SCHEDPRIO");
 
355
 
 
356
                env_prio = (env_prio_c == NULL)?max_prio:atoi(env_prio_c);
 
357
 
 
358
                env_prio = MAX(MIN(env_prio, max_prio), min_prio);
 
359
                ms_message("Priority used: %d", env_prio);
 
360
 
 
361
                param.sched_priority=env_prio;
 
362
                if((result=pthread_setschedparam(pthread_self(),policy, &param))) {
 
363
                        if (result==EPERM){
 
364
                                /*
 
365
                                        The linux kernel has 
 
366
                                        sched_get_priority_max(SCHED_OTHER)=sched_get_priority_max(SCHED_OTHER)=0.
 
367
                                        As long as we can't use SCHED_RR or SCHED_FIFO, the only way to increase priority of a calling thread
 
368
                                        is to use setpriority().
 
369
                                */
 
370
                                if (setpriority(PRIO_PROCESS,0,-20)==-1){
 
371
                                        ms_message("%s setpriority() failed: %s, nevermind.",obj->name,strerror(errno));
 
372
                                }else{
 
373
                                        ms_message("%s priority increased to maximum.",obj->name);
 
374
                                }
 
375
                        }else ms_warning("%s: Set pthread_setschedparam failed: %s",obj->name,strerror(result));
 
376
                } else {
 
377
                        ms_message("%s priority set to %s and value (%i)",obj->name,
 
378
                                   policy==SCHED_FIFO ? "SCHED_FIFO" : "SCHED_RR", param.sched_priority);
 
379
                }
 
380
#endif
 
381
        }else ms_message("%s priority left to normal.",obj->name);
 
382
        return precision;
 
383
}
 
384
 
 
385
static void unset_high_prio(int precision){
 
386
#ifdef WIN32
 
387
        if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL)){
 
388
                ms_warning("SetThreadPriority() failed (%d)\n", (int)GetLastError());
 
389
        }
 
390
        timeEndPeriod(precision);
 
391
#endif
 
392
}
 
393
 
 
394
static int wait_next_tick(void *data, uint64_t virt_ticker_time){
 
395
        MSTicker *s=(MSTicker*)data;
 
396
        uint64_t realtime;
 
397
        int64_t diff;
 
398
        int late;
 
399
        
 
400
        while(1){
 
401
                realtime=s->get_cur_time_ptr(s->get_cur_time_data)-s->orig;
 
402
                diff=s->time-realtime;
 
403
                if (diff>0){
 
404
                        /* sleep until next tick */
 
405
                        sleepMs((int)diff);
 
406
                }else{
 
407
                        late=(int)-diff;
 
408
                        break; /*exit the while loop */
 
409
                }
 
410
        }
 
411
        return late;
 
412
}
 
413
 
 
414
/*the ticker thread function that executes the filters */
 
415
void * ms_ticker_run(void *arg)
 
416
{
 
417
        MSTicker *s=(MSTicker*)arg;
 
418
        int lastlate=0;
 
419
        int precision=2;
 
420
        int late;
 
421
        
 
422
        precision = set_high_prio(s);
 
423
 
 
424
        s->ticks=1;
 
425
        s->orig=s->get_cur_time_ptr(s->get_cur_time_data);
 
426
 
 
427
        ms_mutex_lock(&s->lock);
 
428
        
 
429
        while(s->run){
 
430
                s->ticks++;
 
431
                /*Step 1: run the graphs*/
 
432
                {
 
433
#if TICKER_MEASUREMENTS
 
434
                        MSTimeSpec begin,end;/*used to measure time spent in processing one tick*/
 
435
                        double iload;
 
436
 
 
437
                        ms_get_cur_time(&begin);
 
438
#endif
 
439
                        run_tasks(s);
 
440
                        run_graphs(s,s->execution_list,FALSE);
 
441
#if TICKER_MEASUREMENTS
 
442
                        ms_get_cur_time(&end);
 
443
                        iload=100*((end.tv_sec-begin.tv_sec)*1000.0 + (end.tv_nsec-begin.tv_nsec)/1000000.0)/(double)s->interval;
 
444
                        s->av_load=(smooth_coef*s->av_load)+((1.0-smooth_coef)*iload);
 
445
#endif
 
446
                }
 
447
                ms_mutex_unlock(&s->lock);
 
448
                /*Step 2: wait for next tick*/
 
449
                s->time+=s->interval;
 
450
                late=s->wait_next_tick(s->wait_next_tick_data,s->time);
 
451
                if (late>s->interval*5 && late>lastlate){
 
452
                        ms_warning("%s: We are late of %d miliseconds.",s->name,late);
 
453
                }
 
454
                lastlate=late;
 
455
                ms_mutex_lock(&s->lock);
 
456
        }
 
457
        ms_mutex_unlock(&s->lock);
 
458
        unset_high_prio(precision);
 
459
        ms_message("%s thread exiting",s->name);
 
460
 
 
461
        ms_thread_exit(NULL);
 
462
        return NULL;
 
463
}
 
464
 
 
465
void ms_ticker_set_time_func(MSTicker *ticker, MSTickerTimeFunc func, void *user_data){
 
466
        if (func==NULL) func=get_cur_time_ms;
 
467
        
 
468
        ticker->get_cur_time_ptr=func;
 
469
        ticker->get_cur_time_data=user_data;
 
470
        /*re-set the origin to take in account that previous function ptr and the
 
471
        new one may return different times*/
 
472
        ticker->orig=func(user_data)-ticker->time;
 
473
        
 
474
        ms_message("ms_ticker_set_time_func: ticker's time method updated.");
 
475
}
 
476
 
 
477
void ms_ticker_set_tick_func(MSTicker *ticker, MSTickerTickFunc func, void *user_data){
 
478
        if (func==NULL) {
 
479
                func=wait_next_tick;
 
480
                user_data=ticker;
 
481
        }
 
482
        ticker->wait_next_tick=func;
 
483
        ticker->wait_next_tick_data=user_data;
 
484
        /*re-set the origin to take in account that previous function ptr and the
 
485
        new one may return different times*/
 
486
        ticker->orig=ticker->get_cur_time_ptr(user_data)-ticker->time;
 
487
        ms_message("ms_ticker_set_tick_func: ticker's tick method updated.");
 
488
}
 
489
 
 
490
static void print_graph(MSFilter *f, MSTicker *s, MSList **unschedulable, bool_t force_schedule){
 
491
        int i;
 
492
        MSQueue *l;
 
493
        if (f->last_tick!=s->ticks ){
 
494
                if (filter_can_process(f,s->ticks) || force_schedule) {
 
495
                        /* this is a candidate */
 
496
                        f->last_tick=s->ticks;
 
497
                        ms_message("print_graphs: %s", f->desc->name);
 
498
                        /* now recurse to next filters */               
 
499
                        for(i=0;i<f->desc->noutputs;i++){
 
500
                                l=f->outputs[i];
 
501
                                if (l!=NULL){
 
502
                                        print_graph(l->next.filter,s,unschedulable, force_schedule);
 
503
                                }
 
504
                        }
 
505
                }else{
 
506
                        /* this filter has not all inputs that have been filled by filters before it. */
 
507
                        *unschedulable=ms_list_prepend(*unschedulable,f);
 
508
                }
 
509
        }
 
510
}
 
511
 
 
512
static void print_graphs(MSTicker *s, MSList *execution_list, bool_t force_schedule){
 
513
        MSList *it;
 
514
        MSList *unschedulable=NULL;
 
515
        for(it=execution_list;it!=NULL;it=it->next){
 
516
                print_graph((MSFilter*)it->data,s,&unschedulable,force_schedule);
 
517
        }
 
518
        /* filters that are part of a loop haven't been called in process() because one of their input refers to a filter that could not be scheduled (because they could not be scheduled themselves)... Do you understand ?*/
 
519
        /* we resolve this by simply assuming that they must be called anyway 
 
520
        for the loop to run correctly*/
 
521
        /* we just recall run_graphs on them, as if they were source filters */
 
522
        if (unschedulable!=NULL) {
 
523
                print_graphs(s,unschedulable,TRUE);
 
524
                ms_list_free(unschedulable);
 
525
        }
 
526
}
 
527
 
 
528
void ms_ticker_print_graphs(MSTicker *ticker){
 
529
        print_graphs(ticker,ticker->execution_list,FALSE);
 
530
}
 
531
 
 
532
float ms_ticker_get_average_load(MSTicker *ticker){
 
533
#if     !TICKER_MEASUREMENTS
 
534
        static bool_t once=FALSE;
 
535
        if (once==FALSE){
 
536
                ms_warning("ms_ticker_get_average_load(): ticker load measurements disabled for performance reasons.");
 
537
                once=TRUE;
 
538
        }
 
539
#endif
 
540
        return ticker->av_load;
 
541
}
 
542
 
 
543
 
 
544
static uint64_t get_ms(const MSTimeSpec *ts){
 
545
        return (ts->tv_sec*1000LL) + ((ts->tv_nsec+500000LL)/1000000LL);
 
546
}
 
547
 
 
548
static uint64_t get_wallclock_ms(void){
 
549
        MSTimeSpec ts;
 
550
        ms_get_cur_time(&ts);
 
551
        return get_ms(&ts);
 
552
}
 
553
 
 
554
static const double clock_coef = .01;
 
555
 
 
556
MSTickerSynchronizer* ms_ticker_synchronizer_new(void) {
 
557
        MSTickerSynchronizer *obj=(MSTickerSynchronizer *)ms_new(MSTickerSynchronizer,1);
 
558
        obj->av_skew = 0;
 
559
        obj->offset = 0;
 
560
        return obj;
 
561
}
 
562
 
 
563
double ms_ticker_synchronizer_set_external_time(MSTickerSynchronizer* ts, const MSTimeSpec *time) {
 
564
        int64_t sound_time;
 
565
        int64_t diff;
 
566
        uint64_t wc = get_wallclock_ms();
 
567
        uint64_t ms = get_ms(time);
 
568
        if (ts->offset == 0) {
 
569
                ts->offset = wc - ms;
 
570
        }
 
571
        sound_time = ts->offset + ms;
 
572
        diff = wc - sound_time;
 
573
        ts->av_skew = (ts->av_skew * (1.0 - clock_coef)) + ((double) diff * clock_coef);
 
574
        return ts->av_skew;
 
575
}
 
576
 
 
577
 
 
578
 
 
579
uint64_t ms_ticker_synchronizer_get_corrected_time(MSTickerSynchronizer* ts) {
 
580
        /* round skew to timer resolution in order to avoid adapt the ticker just with statistical "noise" */
 
581
        int64_t rounded_skew=( ((int64_t)ts->av_skew)/(int64_t)TICKER_INTERVAL) * (int64_t)TICKER_INTERVAL;
 
582
        return get_wallclock_ms() - rounded_skew;
 
583
}
 
584
 
 
585
void ms_ticker_synchronizer_destroy(MSTickerSynchronizer* ts) {
 
586
        ms_free(ts);
 
587
}