msticker.c 16.6 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "mediastreamer2/msticker.h"

22 23 24 25 26
#ifndef WIN32
#include <sys/time.h>
#include <sys/resource.h>
#endif

27 28 29 30 31 32
static const double smooth_coef=0.9;

#ifndef TICKER_MEASUREMENTS

#define TICKER_MEASUREMENTS 1

33
#if defined(__ARM_ARCH__)
34 35 36
#	if __ARM_ARCH__ < 7
/* as MSTicker load computation requires floating point, we prefer to disable it on ARM processors without FPU*/
#		undef TICKER_MEASUREMENTS
37
#		define TICKER_MEASUREMENTS 0
38 39 40 41
#	endif
#endif

#endif
aymeric's avatar
aymeric committed
42

43 44
#define TICKER_INTERVAL 10

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);
aymeric's avatar
aymeric committed
49

50
static void ms_ticker_start(MSTicker *s){
aymeric's avatar
aymeric committed
51 52 53 54
	s->run=TRUE;
	ms_thread_create(&s->thread,NULL,ms_ticker_run,s);
}

55
static void ms_ticker_init(MSTicker *ticker, const MSTickerParams *params)
aymeric's avatar
aymeric committed
56 57 58
{
	ms_mutex_init(&ticker->lock,NULL);
	ticker->execution_list=NULL;
59
	ticker->task_list=NULL;
aymeric's avatar
aymeric committed
60 61
	ticker->ticks=1;
	ticker->time=0;
62
	ticker->interval=TICKER_INTERVAL;
aymeric's avatar
aymeric committed
63 64
	ticker->run=FALSE;
	ticker->exec_id=0;
65
	ticker->get_cur_time_ptr=&get_cur_time_ms;
aymeric's avatar
aymeric committed
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 71
	ticker->wait_next_tick=wait_next_tick;
	ticker->wait_next_tick_data=ticker;
aymeric's avatar
aymeric committed
72 73 74 75
	ms_ticker_start(ticker);
}

MSTicker *ms_ticker_new(){
76 77 78 79 80 81 82
	MSTickerParams params;
	params.name="MSTicker";
	params.prio=MS_TICKER_PRIO_NORMAL;
	return ms_ticker_new_with_params(&params);
}

MSTicker *ms_ticker_new_with_params(const MSTickerParams *params){
83
	MSTicker *obj=(MSTicker *)ms_new0(MSTicker,1);
84
	ms_ticker_init(obj,params);
aymeric's avatar
aymeric committed
85 86 87
	return obj;
}

88
static void ms_ticker_stop(MSTicker *s){
aymeric's avatar
aymeric committed
89 90 91
	ms_mutex_lock(&s->lock);
	s->run=FALSE;
	ms_mutex_unlock(&s->lock);
92 93
	if(s->thread)
		ms_thread_join(s->thread,NULL);
aymeric's avatar
aymeric committed
94 95
}

smorlat's avatar
smorlat committed
96 97 98 99
void ms_ticker_set_name(MSTicker *s, const char *name){
	if (s->name) ms_free(s->name);
	s->name=ms_strdup(name);
}
aymeric's avatar
aymeric committed
100

101 102 103 104
void ms_ticker_set_priority(MSTicker *ticker, MSTickerPrio prio){
	ticker->prio=prio;
}

105
static void ms_ticker_uninit(MSTicker *ticker)
aymeric's avatar
aymeric committed
106 107
{
	ms_ticker_stop(ticker);
smorlat's avatar
smorlat committed
108
	ms_free(ticker->name);
aymeric's avatar
aymeric committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	ms_mutex_destroy(&ticker->lock);
}

void ms_ticker_destroy(MSTicker *ticker){
	ms_ticker_uninit(ticker);
	ms_free(ticker);
}


static MSList *get_sources(MSList *filters){
	MSList *sources=NULL;
	MSFilter *f;
	for(;filters!=NULL;filters=filters->next){
		f=(MSFilter*)filters->data;
		if (f->desc->ninputs==0){
			sources=ms_list_append(sources,f);
		}
	}
	return sources;
}

130 131 132 133 134
int ms_ticker_attach(MSTicker *ticker, MSFilter *f){
	return ms_ticker_attach_multiple(ticker,f,NULL);
}

int ms_ticker_attach_multiple(MSTicker *ticker,MSFilter *f,...)
aymeric's avatar
aymeric committed
135 136 137 138
{
	MSList *sources=NULL;
	MSList *filters=NULL;
	MSList *it;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	MSList *total_sources=NULL;
	va_list l;

	va_start(l,f);

	do{
		if (f->ticker==NULL) {
			filters=ms_filter_find_neighbours(f);
			sources=get_sources(filters);
			if (sources==NULL){
				ms_fatal("No sources found around filter %s",f->desc->name);
				ms_list_free(filters);
				break;
			}
			/*run preprocess on each filter: */
			for(it=filters;it!=NULL;it=it->next)
				ms_filter_preprocess((MSFilter*)it->data,ticker);
			ms_list_free(filters);
157
			total_sources=ms_list_concat(total_sources,sources);
158 159 160 161 162 163 164
		}else ms_message("Filter %s is already being scheduled; nothing to do.",f->desc->name);
	}while ((f=va_arg(l,MSFilter*))!=NULL);
	va_end(l);
	if (total_sources){
		ms_mutex_lock(&ticker->lock);
		ticker->execution_list=ms_list_concat(ticker->execution_list,total_sources);
		ms_mutex_unlock(&ticker->lock);
aymeric's avatar
aymeric committed
165 166 167 168
	}
	return 0;
}

169 170 171 172
static void call_postprocess(MSFilter *f){
	if (f->postponed_task) remove_tasks_for_filter(f->ticker,f);
	ms_filter_postprocess(f);
}
aymeric's avatar
aymeric committed
173 174 175 176 177 178 179 180 181 182 183 184 185

int ms_ticker_detach(MSTicker *ticker,MSFilter *f){
	MSList *sources=NULL;
	MSList *filters=NULL;
	MSList *it;

	if (f->ticker==NULL) {
		ms_message("Filter %s is not scheduled; nothing to do.",f->desc->name);
		return 0;
	}

	ms_mutex_lock(&ticker->lock);

186
	filters=ms_filter_find_neighbours(f);
aymeric's avatar
aymeric committed
187 188 189 190 191 192 193 194 195 196 197 198
	sources=get_sources(filters);
	if (sources==NULL){
		ms_fatal("No sources found around filter %s",f->desc->name);
		ms_list_free(filters);
		ms_mutex_unlock(&ticker->lock);
		return -1;
	}

	for(it=sources;it!=NULL;it=ms_list_next(it)){
		ticker->execution_list=ms_list_remove(ticker->execution_list,it->data);
	}
	ms_mutex_unlock(&ticker->lock);
199
	ms_list_for_each(filters,(void (*)(void*))call_postprocess);
aymeric's avatar
aymeric committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	ms_list_free(filters);
	ms_list_free(sources);
	return 0;
}


static bool_t filter_can_process(MSFilter *f, int tick){
	/* look if filters before this one have run */
	int i;
	MSQueue *l;
	for(i=0;i<f->desc->ninputs;i++){
		l=f->inputs[i];
		if (l!=NULL){
			if (l->prev.filter->last_tick!=tick) return FALSE;
		}
	}
	return TRUE;
}

static void call_process(MSFilter *f){
	bool_t process_done=FALSE;
221
	if (f->desc->ninputs==0 || f->desc->flags & MS_FILTER_IS_PUMP){
aymeric's avatar
aymeric committed
222 223 224 225 226 227 228
		ms_filter_process(f);
	}else{
		while (ms_filter_inputs_have_data(f)) {
			if (process_done){
				ms_warning("Re-scheduling filter %s: all data should be consumed in one process call, so fix it.",f->desc->name);
			}
			ms_filter_process(f);
229
			if (f->postponed_task) break;
aymeric's avatar
aymeric committed
230 231 232 233 234 235 236 237 238 239 240 241
			process_done=TRUE;
		}
	}
}

static void run_graph(MSFilter *f, MSTicker *s, MSList **unschedulable, bool_t force_schedule){
	int i;
	MSQueue *l;
	if (f->last_tick!=s->ticks ){
		if (filter_can_process(f,s->ticks) || force_schedule) {
			/* this is a candidate */
			f->last_tick=s->ticks;
242 243
			call_process(f);
			/* now recurse to next filters */
aymeric's avatar
aymeric committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
			for(i=0;i<f->desc->noutputs;i++){
				l=f->outputs[i];
				if (l!=NULL){
					run_graph(l->next.filter,s,unschedulable, force_schedule);
				}
			}
		}else{
			/* this filter has not all inputs that have been filled by filters before it. */
			*unschedulable=ms_list_prepend(*unschedulable,f);
		}
	}
}

static void run_graphs(MSTicker *s, MSList *execution_list, bool_t force_schedule){
	MSList *it;
	MSList *unschedulable=NULL;
	for(it=execution_list;it!=NULL;it=it->next){
		run_graph((MSFilter*)it->data,s,&unschedulable,force_schedule);
	}
	/* 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
aymeric's avatar
aymeric committed
265 266 267 268 269 270 271 272
	for the loop to run correctly*/
	/* we just recall run_graphs on them, as if they were source filters */
	if (unschedulable!=NULL) {
		run_graphs(s,unschedulable,TRUE);
		ms_list_free(unschedulable);
	}
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
static void run_tasks(MSTicker *ticker){
	MSList *elem,*prevelem=NULL;
	for (elem=ticker->task_list;elem!=NULL;){
		MSFilterTask *t=(MSFilterTask*)elem->data;
		ms_filter_task_process(t);
		ms_free(t);
		prevelem=elem;
		elem=elem->next;
		ms_free(prevelem);
	}
	ticker->task_list=NULL;
}

static void remove_tasks_for_filter(MSTicker *ticker, MSFilter *f){
	MSList *elem,*nextelem;
	for (elem=ticker->task_list;elem!=NULL;elem=nextelem){
		MSFilterTask *t=(MSFilterTask*)elem->data;
		nextelem=elem->next;
		if (t->f==f){
			ticker->task_list=ms_list_remove_link(ticker->task_list,elem);
			ms_free(t);
		}
	}
}

298
static uint64_t get_cur_time_ms(void *unused){
299
	return ms_get_cur_time_ms();
aymeric's avatar
aymeric committed
300 301 302 303
}

static void sleepMs(int ms){
#ifdef WIN32
304 305 306
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
	Sleep(ms);
#else
307 308 309 310
	HANDLE sleepEvent = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
	if (!sleepEvent) return;
	WaitForSingleObjectEx(sleepEvent, ms, FALSE);
#endif
aymeric's avatar
aymeric committed
311 312 313 314 315 316 317 318
#else
	struct timespec ts;
	ts.tv_sec=0;
	ts.tv_nsec=ms*1000000LL;
	nanosleep(&ts,NULL);
#endif
}

319
static int set_high_prio(MSTicker *obj){
aymeric's avatar
aymeric committed
320
	int precision=2;
321
	int prio=obj->prio;
322

323
	if (prio>MS_TICKER_PRIO_NORMAL){
aymeric's avatar
aymeric committed
324
#ifdef WIN32
325
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		MMRESULT mm;
		TIMECAPS ptc;
		mm=timeGetDevCaps(&ptc,sizeof(ptc));
		if (mm==0){
			if (ptc.wPeriodMin<(UINT)precision)
				ptc.wPeriodMin=precision;
			else
				precision = ptc.wPeriodMin;
			mm=timeBeginPeriod(ptc.wPeriodMin);
			if (mm!=TIMERR_NOERROR){
				ms_warning("timeBeginPeriod failed.");
			}
			ms_message("win32 timer resolution set to %i ms",ptc.wPeriodMin);
		}else{
			ms_warning("timeGetDevCaps failed.");
aymeric's avatar
aymeric committed
341 342
		}

343
		if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST)){
Simon Morlat's avatar
Simon Morlat committed
344
			ms_warning("SetThreadPriority() failed (%d)\n", (int)GetLastError());
345
		}
346
#endif
347
#else
348 349
		struct sched_param param;
		int policy=SCHED_RR;
Simon Morlat's avatar
Simon Morlat committed
350 351 352
		int result=0;
		char* env_prio_c=NULL;
		int min_prio, max_prio, env_prio;
353

354
		memset(&param,0,sizeof(param));
355 356
		if (prio==MS_TICKER_PRIO_REALTIME)
			policy=SCHED_FIFO;
357

358 359 360 361 362 363 364 365 366 367 368 369 370
		min_prio = sched_get_priority_min(policy);
		max_prio = sched_get_priority_max(policy);
		env_prio_c = getenv("MS_TICKER_SCHEDPRIO");

		env_prio = (env_prio_c == NULL)?max_prio:atoi(env_prio_c);

		env_prio = MAX(MIN(env_prio, max_prio), min_prio);
		ms_message("Priority used: %d", env_prio);

		param.sched_priority=env_prio;
		if((result=pthread_setschedparam(pthread_self(),policy, &param))) {
			if (result==EPERM){
				/*
371
					The linux kernel has
372 373 374 375 376 377 378 379 380 381 382 383 384 385
					sched_get_priority_max(SCHED_OTHER)=sched_get_priority_max(SCHED_OTHER)=0.
					As long as we can't use SCHED_RR or SCHED_FIFO, the only way to increase priority of a calling thread
					is to use setpriority().
				*/
				if (setpriority(PRIO_PROCESS,0,-20)==-1){
					ms_message("%s setpriority() failed: %s, nevermind.",obj->name,strerror(errno));
				}else{
					ms_message("%s priority increased to maximum.",obj->name);
				}
			}else ms_warning("%s: Set pthread_setschedparam failed: %s",obj->name,strerror(result));
		} else {
			ms_message("%s priority set to %s and value (%i)",obj->name,
			           policy==SCHED_FIFO ? "SCHED_FIFO" : "SCHED_RR", param.sched_priority);
		}
aymeric's avatar
aymeric committed
386
#endif
387
	}else ms_message("%s priority left to normal.",obj->name);
aymeric's avatar
aymeric committed
388 389 390 391 392
	return precision;
}

static void unset_high_prio(int precision){
#ifdef WIN32
393
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
aymeric's avatar
aymeric committed
394
	if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL)){
Simon Morlat's avatar
Simon Morlat committed
395
		ms_warning("SetThreadPriority() failed (%d)\n", (int)GetLastError());
aymeric's avatar
aymeric committed
396
	}
Simon Morlat's avatar
Simon Morlat committed
397
	timeEndPeriod(precision);
aymeric's avatar
aymeric committed
398
#endif
399
#endif
aymeric's avatar
aymeric committed
400 401
}

402 403 404 405 406
static int wait_next_tick(void *data, uint64_t virt_ticker_time){
	MSTicker *s=(MSTicker*)data;
	uint64_t realtime;
	int64_t diff;
	int late;
407

408 409 410 411 412 413 414 415 416 417 418 419 420 421
	while(1){
		realtime=s->get_cur_time_ptr(s->get_cur_time_data)-s->orig;
		diff=s->time-realtime;
		if (diff>0){
			/* sleep until next tick */
			sleepMs((int)diff);
		}else{
			late=(int)-diff;
			break; /*exit the while loop */
		}
	}
	return late;
}

422
/*the ticker thread function that executes the filters */
aymeric's avatar
aymeric committed
423 424 425 426 427 428
void * ms_ticker_run(void *arg)
{
	MSTicker *s=(MSTicker*)arg;
	int lastlate=0;
	int precision=2;
	int late;
429

430
	precision = set_high_prio(s);
aymeric's avatar
aymeric committed
431 432 433 434

	s->ticks=1;
	s->orig=s->get_cur_time_ptr(s->get_cur_time_data);

435
	ms_mutex_lock(&s->lock);
436

aymeric's avatar
aymeric committed
437
	while(s->run){
438 439
		uint64_t late_tick_time=0;
		
aymeric's avatar
aymeric committed
440
		s->ticks++;
441
		/*Step 1: run the graphs*/
442 443 444 445 446 447 448
		{
#if TICKER_MEASUREMENTS
			MSTimeSpec begin,end;/*used to measure time spent in processing one tick*/
			double iload;

			ms_get_cur_time(&begin);
#endif
449
			run_tasks(s);
450 451 452
			run_graphs(s,s->execution_list,FALSE);
#if TICKER_MEASUREMENTS
			ms_get_cur_time(&end);
453
			iload=100*((end.tv_sec-begin.tv_sec)*1000.0 + (end.tv_nsec-begin.tv_nsec)/1000000.0)/(double)s->interval;
454 455 456
			s->av_load=(smooth_coef*s->av_load)+((1.0-smooth_coef)*iload);
#endif
		}
457 458
		ms_mutex_unlock(&s->lock);
		/*Step 2: wait for next tick*/
aymeric's avatar
aymeric committed
459
		s->time+=s->interval;
460 461 462
		late=s->wait_next_tick(s->wait_next_tick_data,s->time);
		if (late>s->interval*5 && late>lastlate){
			ms_warning("%s: We are late of %d miliseconds.",s->name,late);
463
			late_tick_time=ms_get_cur_time_ms();
aymeric's avatar
aymeric committed
464
		}
465
		lastlate=late;
aymeric's avatar
aymeric committed
466
		ms_mutex_lock(&s->lock);
467 468 469 470
		if (late_tick_time){
			s->late_event.lateMs=late;
			s->late_event.time=late_tick_time;
		}
aymeric's avatar
aymeric committed
471 472 473
	}
	ms_mutex_unlock(&s->lock);
	unset_high_prio(precision);
smorlat's avatar
smorlat committed
474
	ms_message("%s thread exiting",s->name);
aymeric's avatar
aymeric committed
475

476
	ms_thread_exit(NULL);
aymeric's avatar
aymeric committed
477 478 479 480
	return NULL;
}

void ms_ticker_set_time_func(MSTicker *ticker, MSTickerTimeFunc func, void *user_data){
481
	if (func==NULL) func=get_cur_time_ms;
482

aymeric's avatar
aymeric committed
483 484 485 486 487
	ticker->get_cur_time_ptr=func;
	ticker->get_cur_time_data=user_data;
	/*re-set the origin to take in account that previous function ptr and the
	new one may return different times*/
	ticker->orig=func(user_data)-ticker->time;
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502
	ms_message("ms_ticker_set_time_func: ticker's time method updated.");
}

void ms_ticker_set_tick_func(MSTicker *ticker, MSTickerTickFunc func, void *user_data){
	if (func==NULL) {
		func=wait_next_tick;
		user_data=ticker;
	}
	ticker->wait_next_tick=func;
	ticker->wait_next_tick_data=user_data;
	/*re-set the origin to take in account that previous function ptr and the
	new one may return different times*/
	ticker->orig=ticker->get_cur_time_ptr(user_data)-ticker->time;
	ms_message("ms_ticker_set_tick_func: ticker's tick method updated.");
aymeric's avatar
aymeric committed
503 504 505 506 507 508 509 510 511 512
}

static void print_graph(MSFilter *f, MSTicker *s, MSList **unschedulable, bool_t force_schedule){
	int i;
	MSQueue *l;
	if (f->last_tick!=s->ticks ){
		if (filter_can_process(f,s->ticks) || force_schedule) {
			/* this is a candidate */
			f->last_tick=s->ticks;
			ms_message("print_graphs: %s", f->desc->name);
513
			/* now recurse to next filters */
aymeric's avatar
aymeric committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
			for(i=0;i<f->desc->noutputs;i++){
				l=f->outputs[i];
				if (l!=NULL){
					print_graph(l->next.filter,s,unschedulable, force_schedule);
				}
			}
		}else{
			/* this filter has not all inputs that have been filled by filters before it. */
			*unschedulable=ms_list_prepend(*unschedulable,f);
		}
	}
}

static void print_graphs(MSTicker *s, MSList *execution_list, bool_t force_schedule){
	MSList *it;
	MSList *unschedulable=NULL;
	for(it=execution_list;it!=NULL;it=it->next){
		print_graph((MSFilter*)it->data,s,&unschedulable,force_schedule);
	}
	/* 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 ?*/
534
	/* we resolve this by simply assuming that they must be called anyway
aymeric's avatar
aymeric committed
535 536 537 538 539 540 541 542 543 544 545
	for the loop to run correctly*/
	/* we just recall run_graphs on them, as if they were source filters */
	if (unschedulable!=NULL) {
		print_graphs(s,unschedulable,TRUE);
		ms_list_free(unschedulable);
	}
}

void ms_ticker_print_graphs(MSTicker *ticker){
	print_graphs(ticker,ticker->execution_list,FALSE);
}
546 547 548 549 550 551 552 553 554 555 556 557

float ms_ticker_get_average_load(MSTicker *ticker){
#if	!TICKER_MEASUREMENTS
	static bool_t once=FALSE;
	if (once==FALSE){
		ms_warning("ms_ticker_get_average_load(): ticker load measurements disabled for performance reasons.");
		once=TRUE;
	}
#endif
	return ticker->av_load;
}

558 559 560 561 562
void ms_ticker_get_last_late_tick(MSTicker *ticker, MSTickerLateEvent *ev){
	ms_mutex_lock(&ticker->lock);
	memcpy(ev,&ticker->late_event,sizeof(MSTickerLateEvent));
	ms_mutex_unlock(&ticker->lock);
}
563 564 565 566 567 568 569 570 571 572 573 574 575 576

static uint64_t get_ms(const MSTimeSpec *ts){
	return (ts->tv_sec*1000LL) + ((ts->tv_nsec+500000LL)/1000000LL);
}

static uint64_t get_wallclock_ms(void){
	MSTimeSpec ts;
	ms_get_cur_time(&ts);
	return get_ms(&ts);
}

static const double clock_coef = .01;

MSTickerSynchronizer* ms_ticker_synchronizer_new(void) {
577
	MSTickerSynchronizer *obj=(MSTickerSynchronizer *)ms_new0(MSTickerSynchronizer,1);
578 579 580 581 582 583
	obj->av_skew = 0;
	obj->offset = 0;
	return obj;
}

double ms_ticker_synchronizer_set_external_time(MSTickerSynchronizer* ts, const MSTimeSpec *time) {
584 585
	int64_t sound_time;
	int64_t diff;
586 587 588 589 590
	uint64_t wc = get_wallclock_ms();
	uint64_t ms = get_ms(time);
	if (ts->offset == 0) {
		ts->offset = wc - ms;
	}
591 592
	sound_time = ts->offset + ms;
	diff = wc - sound_time;
593 594 595 596
	ts->av_skew = (ts->av_skew * (1.0 - clock_coef)) + ((double) diff * clock_coef);
	return ts->av_skew;
}

597 598


599
uint64_t ms_ticker_synchronizer_get_corrected_time(MSTickerSynchronizer* ts) {
600 601 602
	/* round skew to timer resolution in order to avoid adapt the ticker just with statistical "noise" */
	int64_t rounded_skew=( ((int64_t)ts->av_skew)/(int64_t)TICKER_INTERVAL) * (int64_t)TICKER_INTERVAL;
	return get_wallclock_ms() - rounded_skew;
603 604 605 606 607
}

void ms_ticker_synchronizer_destroy(MSTickerSynchronizer* ts) {
	ms_free(ts);
}