mscommon.c 16 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
/*
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.
*/

Simon Morlat's avatar
Simon Morlat committed
20

aymeric's avatar
aymeric committed
21 22

#include "mediastreamer2/mscommon.h"
23
#include "mediastreamer2/mscodecutils.h"
aymeric's avatar
aymeric committed
24 25 26 27 28 29 30 31 32 33
#include "mediastreamer2/msfilter.h"

#if !defined(_WIN32_WCE)
#include <sys/types.h>
#endif
#ifndef WIN32
#include <dirent.h>
#else
#ifndef PACKAGE_PLUGINS_DIR
#if defined(WIN32) || defined(_WIN32_WCE)
Simon Morlat's avatar
Simon Morlat committed
34
#define PACKAGE_PLUGINS_DIR "lib\\mediastreamer\\plugins\\"
aymeric's avatar
aymeric committed
35 36 37 38 39 40 41 42 43
#else
#define PACKAGE_PLUGINS_DIR "."
#endif
#endif
#endif
#ifdef HAVE_DLOPEN
#include <dlfcn.h>
#endif

44 45
#ifdef __APPLE__
   #include "TargetConditionals.h"
aymeric's avatar
aymeric committed
46 47
#endif

48 49 50 51
#ifdef __QNX__
#include <sys/syspage.h>
#endif

52
unsigned int ms_get_cpu_count() {
Simon Morlat's avatar
Simon Morlat committed
53
	return ms_factory_get_cpu_count(ms_factory_get_fallback());
54 55 56
}

void ms_set_cpu_count(unsigned int c) {
Simon Morlat's avatar
Simon Morlat committed
57
	ms_factory_set_cpu_count(ms_factory_get_fallback(),c);
58 59
}

aymeric's avatar
aymeric committed
60 61 62 63 64 65 66
MSList *ms_list_new(void *data){
	MSList *new_elem=(MSList *)ms_new(MSList,1);
	new_elem->prev=new_elem->next=NULL;
	new_elem->data=data;
	return new_elem;
}

Simon Morlat's avatar
Simon Morlat committed
67
MSList *ms_list_append_link(MSList *elem, MSList *new_elem){
aymeric's avatar
aymeric committed
68 69 70 71 72 73 74 75
	MSList *it=elem;
	if (elem==NULL) return new_elem;
	while (it->next!=NULL) it=ms_list_next(it);
	it->next=new_elem;
	new_elem->prev=it;
	return elem;
}

Simon Morlat's avatar
Simon Morlat committed
76 77 78 79 80
MSList * ms_list_append(MSList *elem, void * data){
	MSList *new_elem=ms_list_new(data);
	return ms_list_append_link(elem,new_elem);
}

aymeric's avatar
aymeric committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
MSList * ms_list_prepend(MSList *elem, void *data){
	MSList *new_elem=ms_list_new(data);
	if (elem!=NULL) {
		new_elem->next=elem;
		elem->prev=new_elem;
	}
	return new_elem;
}


MSList * ms_list_concat(MSList *first, MSList *second){
	MSList *it=first;
	if (it==NULL) return second;
	while(it->next!=NULL) it=ms_list_next(it);
	it->next=second;
	second->prev=it;
	return first;
}

100 101
MSList * ms_list_free_with_data(MSList *list, void (*freefunc)(void*)){
	MSList *elem;
aymeric's avatar
aymeric committed
102
	MSList *tmp;
103

104 105 106 107 108
	for (elem=list;elem!=NULL;){
		tmp=elem->next;
		if (freefunc) freefunc(elem->data);
		ms_free(elem);
		elem=tmp;
aymeric's avatar
aymeric committed
109 110 111 112
	}
	return NULL;
}

113 114 115 116
MSList * ms_list_free(MSList *elem){
	return ms_list_free_with_data(elem,NULL);
}

aymeric's avatar
aymeric committed
117 118 119 120 121 122 123 124 125 126
MSList * ms_list_remove(MSList *first, void *data){
	MSList *it;
	it=ms_list_find(first,data);
	if (it) return ms_list_remove_link(first,it);
	else {
		ms_warning("ms_list_remove: no element with %p data was in the list", data);
		return first;
	}
}

127 128 129 130 131 132 133 134 135 136 137 138 139
MSList * ms_list_remove_custom(MSList *first, MSCompareFunc compare_func, const void *user_data) {
	MSList *cur;
	MSList *elem = first;
	while (elem != NULL) {
		cur = elem;
		elem = elem->next;
		if (compare_func(cur->data, user_data) == 0) {
			first = ms_list_remove(first, cur->data);
		}
	}
	return first;
}

aymeric's avatar
aymeric committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
int ms_list_size(const MSList *first){
	int n=0;
	while(first!=NULL){
		++n;
		first=first->next;
	}
	return n;
}

void ms_list_for_each(const MSList *list, void (*func)(void *)){
	for(;list!=NULL;list=list->next){
		func(list->data);
	}
}

void ms_list_for_each2(const MSList *list, void (*func)(void *, void *), void *user_data){
	for(;list!=NULL;list=list->next){
		func(list->data,user_data);
	}
}

MSList *ms_list_remove_link(MSList *list, MSList *elem){
	MSList *ret;
	if (elem==list){
		ret=elem->next;
		elem->prev=NULL;
		elem->next=NULL;
		if (ret!=NULL) ret->prev=NULL;
		ms_free(elem);
		return ret;
	}
	elem->prev->next=elem->next;
	if (elem->next!=NULL) elem->next->prev=elem->prev;
	elem->next=NULL;
	elem->prev=NULL;
	ms_free(elem);
	return list;
}

MSList *ms_list_find(MSList *list, void *data){
	for(;list!=NULL;list=list->next){
		if (list->data==data) return list;
	}
	return NULL;
}

186
MSList *ms_list_find_custom(MSList *list, int (*compare_func)(const void *, const void*), const void *user_data){
aymeric's avatar
aymeric committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	for(;list!=NULL;list=list->next){
		if (compare_func(list->data,user_data)==0) return list;
	}
	return NULL;
}

void * ms_list_nth_data(const MSList *list, int index){
	int i;
	for(i=0;list!=NULL;list=list->next,++i){
		if (i==index) return list->data;
	}
	ms_error("ms_list_nth_data: no such index in list.");
	return NULL;
}

int ms_list_position(const MSList *list, MSList *elem){
	int i;
	for(i=0;list!=NULL;list=list->next,++i){
		if (elem==list) return i;
	}
	ms_error("ms_list_position: no such element in list.");
	return -1;
}

int ms_list_index(const MSList *list, void *data){
	int i;
	for(i=0;list!=NULL;list=list->next,++i){
		if (data==list->data) return i;
	}
	ms_error("ms_list_index: no such element in list.");
	return -1;
}

MSList *ms_list_insert_sorted(MSList *list, void *data, int (*compare_func)(const void *, const void*)){
	MSList *it,*previt=NULL;
	MSList *nelem;
	MSList *ret=list;
	if (list==NULL) return ms_list_append(list,data);
	else{
		nelem=ms_list_new(data);
		for(it=list;it!=NULL;it=it->next){
			previt=it;
			if (compare_func(data,it->data)<=0){
				nelem->prev=it->prev;
				nelem->next=it;
				if (it->prev!=NULL)
					it->prev->next=nelem;
				else{
					ret=nelem;
				}
				it->prev=nelem;
				return ret;
			}
		}
		previt->next=nelem;
		nelem->prev=previt;
	}
	return ret;
}

MSList *ms_list_insert(MSList *list, MSList *before, void *data){
	MSList *elem;
	if (list==NULL || before==NULL) return ms_list_append(list,data);
	for(elem=list;elem!=NULL;elem=ms_list_next(elem)){
		if (elem==before){
			if (elem->prev==NULL)
				return ms_list_prepend(list,data);
			else{
				MSList *nelem=ms_list_new(data);
				nelem->prev=elem->prev;
				nelem->next=elem;
				elem->prev->next=nelem;
				elem->prev=nelem;
			}
		}
	}
	return list;
}

MSList *ms_list_copy(const MSList *list){
	MSList *copy=NULL;
	const MSList *iter;
	for(iter=list;iter!=NULL;iter=ms_list_next(iter)){
		copy=ms_list_append(copy,iter->data);
	}
	return copy;
}


jehan's avatar
jehan committed
276 277
#ifndef PLUGINS_EXT
	#define PLUGINS_EXT ".so"
aymeric's avatar
aymeric committed
278 279 280 281
#endif
typedef void (*init_func_t)(void);

int ms_load_plugins(const char *dir){
282
	int num=0;
aymeric's avatar
aymeric committed
283
#if defined(WIN32) && !defined(_WIN32_WCE)
284 285 286 287
	WIN32_FIND_DATA FileData;
	HANDLE hSearch;
	char szDirPath[1024];
	char szPluginFile[1024];
288
	BOOL fFinished = FALSE;
289 290
	const char *tmp=getenv("DEBUG");
	BOOL debug=(tmp!=NULL && atoi(tmp)==1);
aymeric's avatar
aymeric committed
291
	snprintf(szDirPath, sizeof(szDirPath), "%s", dir);
292 293 294

	// Start searching for .dll files in the current directory.

aymeric's avatar
aymeric committed
295
	snprintf(szDirPath, sizeof(szDirPath), "%s\\*.dll", dir);
296 297 298 299
	hSearch = FindFirstFile(szDirPath, &FileData);
	if (hSearch == INVALID_HANDLE_VALUE)
	{
		ms_message("no plugin (*.dll) found in %s.", szDirPath);
aymeric's avatar
aymeric committed
300
		return 0;
301
	}
aymeric's avatar
aymeric committed
302 303
	snprintf(szDirPath, sizeof(szDirPath), "%s", dir);

304
	while (!fFinished)
305 306 307
	{
		/* load library */
		HINSTANCE os_handle;
308
		UINT em=0;
309
		if (!debug) em = SetErrorMode (SEM_FAILCRITICALERRORS);
310

311 312 313 314
		snprintf(szPluginFile, sizeof(szPluginFile), "%s\\%s", szDirPath, FileData.cFileName);
		os_handle = LoadLibraryEx (szPluginFile, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
		if (os_handle==NULL)
		{
Simon Morlat's avatar
Simon Morlat committed
315
			ms_message("Fail to load plugin %s with altered search path: error %i",szPluginFile,(int)GetLastError());
316 317
			os_handle = LoadLibraryEx (szPluginFile, NULL, 0);
		}
318
		if (!debug) SetErrorMode (em);
319
		if (os_handle==NULL)
320
			ms_error("Fail to load plugin %s", szPluginFile);
321 322
		else{
			init_func_t initroutine;
323
			char szPluginName[256];
324 325 326 327 328 329 330 331 332 333 334 335
			char szMethodName[256];
			char *minus;
			snprintf(szPluginName, 256, "%s", FileData.cFileName);
			/*on mingw, dll names might be libsomething-3.dll. We must skip the -X.dll stuff*/
			minus=strchr(szPluginName,'-');
			if (minus) *minus='\0';
			else szPluginName[strlen(szPluginName)-4]='\0'; /*remove .dll*/
			snprintf(szMethodName, 256, "%s_init", szPluginName);
			initroutine = (init_func_t) GetProcAddress (os_handle, szMethodName);
				if (initroutine!=NULL){
					initroutine();
					ms_message("Plugin loaded (%s)", szPluginFile);
336 337
					// Add this new loaded plugin to the list (useful for FreeLibrary at the end)
					ms_plugins_loaded_list=ms_list_append(ms_plugins_loaded_list,os_handle);
338 339 340 341 342 343 344
					num++;
				}else{
					ms_warning("Could not locate init routine of plugin %s. Should be %s",
					szPluginFile, szMethodName);
				}
		}
		if (!FindNextFile(hSearch, &FileData)) {
345 346 347 348 349 350 351 352
			if (GetLastError() == ERROR_NO_MORE_FILES){
				fFinished = TRUE;
			}
			else
			{
				ms_error("couldn't find next plugin dll.");
				fFinished = TRUE;
			}
353
		}
354
	}
355 356
	/* Close the search handle. */
	FindClose(hSearch);
aymeric's avatar
aymeric committed
357 358

#elif HAVE_DLOPEN
359
	char plugin_name[64];
aymeric's avatar
aymeric committed
360
	DIR *ds;
361
	MSList *loaded_plugins = NULL;
aymeric's avatar
aymeric committed
362
	struct dirent *de;
363
	char *ext;
aymeric's avatar
aymeric committed
364
	char *fullpath;
365
	ds=opendir(dir);
aymeric's avatar
aymeric committed
366 367 368 369 370
	if (ds==NULL){
		ms_message("Cannot open directory %s: %s",dir,strerror(errno));
		return -1;
	}
	while( (de=readdir(ds))!=NULL){
Ghislain MARY's avatar
Ghislain MARY committed
371 372 373 374 375
		if (
#ifndef __QNX__
			(de->d_type==DT_REG || de->d_type==DT_UNKNOWN || de->d_type==DT_LNK) &&
#endif
			(ext=strstr(de->d_name,PLUGINS_EXT))!=NULL) {
aymeric's avatar
aymeric committed
376
			void *handle;
377 378 379
			snprintf(plugin_name, MIN(sizeof(plugin_name), ext - de->d_name + 1), "%s", de->d_name);
			if (ms_list_find_custom(loaded_plugins, (MSCompareFunc)strcmp, plugin_name) != NULL) continue;
			loaded_plugins = ms_list_append(loaded_plugins, ms_strdup(plugin_name));
aymeric's avatar
aymeric committed
380 381
			fullpath=ms_strdup_printf("%s/%s",dir,de->d_name);
			ms_message("Loading plugin %s...",fullpath);
382

aymeric's avatar
aymeric committed
383 384 385 386 387 388 389 390
			if ( (handle=dlopen(fullpath,RTLD_NOW))==NULL){
				ms_warning("Fail to load plugin %s : %s",fullpath,dlerror());
			}else {
				char *initroutine_name=ms_malloc0(strlen(de->d_name)+10);
				char *p;
				void *initroutine=NULL;
				strcpy(initroutine_name,de->d_name);
				p=strstr(initroutine_name,PLUGINS_EXT);
391 392 393 394
				if (p!=NULL){
					strcpy(p,"_init");
					initroutine=dlsym(handle,initroutine_name);
				}
aymeric's avatar
aymeric committed
395 396 397

#ifdef __APPLE__
				if (initroutine==NULL){
398 399 400 401 402 403 404 405
					/* on macosx: library name are libxxxx.1.2.3.dylib */
					/* -> MUST remove the .1.2.3 */
					p=strstr(initroutine_name,".");
					if (p!=NULL)
					{
						strcpy(p,"_init");
						initroutine=dlsym(handle,initroutine_name);
					}
aymeric's avatar
aymeric committed
406 407 408 409 410 411
				}
#endif

				if (initroutine!=NULL){
					init_func_t func=(init_func_t)initroutine;
					func();
aymeric's avatar
aymeric committed
412
					ms_message("Plugin loaded (%s)", fullpath);
aymeric's avatar
aymeric committed
413 414 415 416 417 418 419 420 421
					num++;
				}else{
					ms_warning("Could not locate init routine of plugin %s",de->d_name);
				}
				ms_free(initroutine_name);
			}
			ms_free(fullpath);
		}
	}
422 423
	ms_list_for_each(loaded_plugins, ms_free);
	ms_list_free(loaded_plugins);
aymeric's avatar
aymeric committed
424 425 426 427 428 429 430
	closedir(ds);
#else
	ms_warning("no loadable plugin support: plugins cannot be loaded.");
	num=-1;
#endif
	return num;
}
431
static int ms_plugins_ref=0;
Simon Morlat's avatar
Simon Morlat committed
432

433
void ms_unload_plugins(){
434 435 436 437
	if (--ms_plugins_ref >0 ) {
		ms_message ("Skiping ms_unload_plugins, still [%i] ref",ms_plugins_ref);
		return;
	}
Simon Morlat's avatar
Simon Morlat committed
438
	ms_factory_uninit_plugins(ms_factory_get_fallback());
439
}
aymeric's avatar
aymeric committed
440

jehan's avatar
jehan committed
441

442 443
static int ms_base_ref=0;

444
void ms_base_init(){
445 446 447 448
	if (ms_base_ref++ >0 ) {
		ms_message ("Skiping ms_base_init, because [%i] ref",ms_base_ref);
		return;
	}
Simon Morlat's avatar
Simon Morlat committed
449
	ms_factory_get_fallback();
aymeric's avatar
aymeric committed
450 451
}

452
void ms_base_exit(){
453 454 455 456
	if (--ms_base_ref >0 ) {
		ms_message ("Skiping ms_base_exit, still [%i] ref",ms_base_ref);
		return;
	}
Simon Morlat's avatar
Simon Morlat committed
457
	ms_factory_destroy(ms_factory_get_fallback());
aymeric's avatar
aymeric committed
458 459
}

460 461


462
void ms_plugins_init(void) {
463 464 465 466
	if (ms_plugins_ref++ >0 ) {
		ms_message ("Skiping ms_plugins_init, because [%i] ref",ms_plugins_ref);
		return;
	}
Simon Morlat's avatar
Simon Morlat committed
467
	ms_factory_init_plugins(ms_factory_get_fallback());
468 469 470
}

void ms_set_plugins_dir(const char *path) {
Simon Morlat's avatar
Simon Morlat committed
471
	ms_factory_set_plugins_dir(ms_factory_get_fallback(),path);
472 473
}

aymeric's avatar
aymeric committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
void ms_sleep(int seconds){
#ifdef WIN32
	Sleep(seconds*1000);
#else
	struct timespec ts,rem;
	int err;
	ts.tv_sec=seconds;
	ts.tv_nsec=0;
	do {
		err=nanosleep(&ts,&rem);
		ts=rem;
	}while(err==-1 && errno==EINTR);
#endif
}

489 490
void ms_usleep(uint64_t usec){
#ifdef WIN32
Aymeric Moizard's avatar
Aymeric Moizard committed
491
	Sleep((DWORD)(usec/1000));
492 493 494 495 496 497 498 499 500 501 502 503
#else
	struct timespec ts,rem;
	int err;
	ts.tv_sec=usec/1000000LL;
	ts.tv_nsec=(usec%1000000LL)*1000;
	do {
		err=nanosleep(&ts,&rem);
		ts=rem;
	}while(err==-1 && errno==EINTR);
#endif
}

aymeric's avatar
aymeric committed
504
int ms_get_payload_max_size(){
Simon Morlat's avatar
Simon Morlat committed
505
	return ms_factory_get_payload_max_size(ms_factory_get_fallback());
aymeric's avatar
aymeric committed
506 507 508
}

void ms_set_payload_max_size(int size){
Simon Morlat's avatar
Simon Morlat committed
509
	ms_factory_set_payload_max_size(ms_factory_get_fallback(),size);
aymeric's avatar
aymeric committed
510
}
jehan's avatar
jehan committed
511

512
extern void _android_key_cleanup(void*);
513

514 515 516 517 518 519 520
void ms_thread_exit(void* ref_val) {
#ifdef ANDROID
	// due to a bug in old Bionic version
	// cleanup of jni manually
	// works directly with Android 2.2
	_android_key_cleanup(NULL);
#endif
521 522 523
#if !defined(__linux) || defined(ANDROID)
	ortp_thread_exit(ref_val); // pthread_exit futex issue: http://lkml.indiana.edu/hypermail/linux/kernel/0902.0/00153.html
#endif
524
}
jehan's avatar
jehan committed
525 526


527
struct _MSConcealerContext {
528 529
	int64_t sample_time;
	int64_t plc_start_time;
530
	unsigned long total_number_for_plc;
531
	unsigned int max_plc_time;
532 533
};

jehan's avatar
jehan committed
534 535 536 537
/*** plc context begin***/
unsigned long ms_concealer_context_get_total_number_of_plc(MSConcealerContext* obj) {
	return obj->total_number_for_plc;
}
538

539
MSConcealerContext* ms_concealer_context_new(unsigned int max_plc_time){
540
	MSConcealerContext *obj=(MSConcealerContext *) ms_new(MSConcealerContext,1);
541 542
	obj->sample_time=-1;
	obj->plc_start_time=-1;
jehan's avatar
jehan committed
543
	obj->total_number_for_plc=0;
544
	obj->max_plc_time=max_plc_time;
545 546
	return obj;
}
547

548 549
void ms_concealer_context_destroy(MSConcealerContext* context) {
	ms_free(context);
550
}
551

552 553 554 555 556 557 558 559 560 561 562 563 564
int ms_concealer_inc_sample_time(MSConcealerContext* obj, uint64_t current_time, int time_increment, int got_packet){
	int plc_duration=0;
	if (obj->sample_time==-1){
		obj->sample_time=(int64_t)current_time;
	}
	obj->sample_time+=time_increment;
	if (obj->plc_start_time!=-1 && got_packet){
		plc_duration=current_time-obj->plc_start_time;
		obj->plc_start_time=-1;
		if (plc_duration>obj->max_plc_time) plc_duration=obj->max_plc_time;
	}
	return plc_duration;
}
jehan's avatar
jehan committed
565

jehan's avatar
jehan committed
566
unsigned int ms_concealer_context_is_concealement_required(MSConcealerContext* obj,uint64_t current_time) {
567

568
	if(obj->sample_time == -1) return 0; /*no valid value*/
569

570 571 572 573 574 575 576 577 578 579 580 581
	if (obj->sample_time < current_time){
		int plc_duration;
		if (obj->plc_start_time==-1)
			obj->plc_start_time=obj->sample_time;
		plc_duration=current_time-obj->plc_start_time;
		if (plc_duration<obj->max_plc_time) {
			obj->total_number_for_plc++;
			return 1;
		}else{
			/*reset sample time, so that we don't do PLC anymore and can resync properly when the stream restarts*/
			obj->sample_time=-1;
			return 0;
582 583
		}
	}
584
	return 0;
585
}
586

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628

struct _MSConcealerTsContext {
	int64_t sample_ts;
	int64_t plc_start_ts;
	unsigned long total_number_for_plc;
	unsigned int max_plc_ts;
};

/*** plc context begin***/
unsigned long ms_concealer_ts_context_get_total_number_of_plc(MSConcealerTsContext* obj) {
	return obj->total_number_for_plc;
}

MSConcealerTsContext* ms_concealer_ts_context_new(unsigned int max_plc_ts){
	MSConcealerTsContext *obj=(MSConcealerTsContext *) ms_new(MSConcealerTsContext,1);
	obj->sample_ts=-1;
	obj->plc_start_ts=-1;
	obj->total_number_for_plc=0;
	obj->max_plc_ts=max_plc_ts;
	return obj;
}

void ms_concealer_ts_context_destroy(MSConcealerTsContext* context) {
	ms_free(context);
}

int ms_concealer_ts_context_inc_sample_ts(MSConcealerTsContext* obj, uint64_t current_ts, int ts_increment, int got_packet){
	int plc_duration=0;
	if (obj->sample_ts==-1){
		obj->sample_ts=(int64_t)current_ts;
	}
	obj->sample_ts+=ts_increment;
	if (obj->plc_start_ts!=-1 && got_packet){
		plc_duration=current_ts-obj->plc_start_ts;
		obj->plc_start_ts=-1;
		if (plc_duration>obj->max_plc_ts) plc_duration=obj->max_plc_ts;
	}
	return plc_duration;
}

unsigned int ms_concealer_ts_context_is_concealement_required(MSConcealerTsContext* obj, uint64_t current_ts) {
	if(obj->sample_ts == -1) return 0; /*no valid value*/
629

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	if (obj->sample_ts < current_ts){
		int plc_duration;
		if (obj->plc_start_ts==-1)
			obj->plc_start_ts=obj->sample_ts;
		plc_duration=current_ts-obj->plc_start_ts;
		if (plc_duration<obj->plc_start_ts) {
			obj->total_number_for_plc++;
			return 1;
		}else{
			/*reset sample time, so that we don't do PLC anymore and can resync properly when the stream restarts*/
			obj->sample_ts=-1;
			return 0;
		}
	}
	return 0;
}

jehan's avatar
jehan committed
647
/*** plc context end***/