mscommon.c 12.2 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
#include "mediastreamer2/msfilter.h"

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

48 49
#ifdef __APPLE__
   #include "TargetConditionals.h"
aymeric's avatar
aymeric committed
50 51
#endif

52 53 54 55
#ifdef __QNX__
#include <sys/syspage.h>
#endif

56
unsigned int ms_get_cpu_count() {
Simon Morlat's avatar
Simon Morlat committed
57
	return ms_factory_get_cpu_count(ms_factory_get_fallback());
58 59 60
}

void ms_set_cpu_count(unsigned int c) {
Simon Morlat's avatar
Simon Morlat committed
61
	ms_factory_set_cpu_count(ms_factory_get_fallback(),c);
62 63
}

aymeric's avatar
aymeric committed
64 65 66 67 68 69 70
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
71
MSList *ms_list_append_link(MSList *elem, MSList *new_elem){
aymeric's avatar
aymeric committed
72 73 74 75 76 77 78 79
	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
80 81 82 83 84
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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
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;
}

104 105
MSList * ms_list_free_with_data(MSList *list, void (*freefunc)(void*)){
	MSList *elem;
aymeric's avatar
aymeric committed
106
	MSList *tmp;
107

108 109 110 111 112
	for (elem=list;elem!=NULL;){
		tmp=elem->next;
		if (freefunc) freefunc(elem->data);
		ms_free(elem);
		elem=tmp;
aymeric's avatar
aymeric committed
113 114 115 116
	}
	return NULL;
}

117 118 119 120
MSList * ms_list_free(MSList *elem){
	return ms_list_free_with_data(elem,NULL);
}

aymeric's avatar
aymeric committed
121 122 123 124 125 126 127 128 129 130
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;
	}
}

131 132 133 134 135 136 137 138 139 140 141 142 143
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
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 186 187 188 189
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;
}

190
MSList *ms_list_find_custom(MSList *list, int (*compare_func)(const void *, const void*), const void *user_data){
aymeric's avatar
aymeric committed
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 276 277 278 279
	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;
}

int ms_load_plugins(const char *dir){
Simon Morlat's avatar
Simon Morlat committed
280
	return ms_factory_load_plugins(ms_factory_get_fallback(),dir);
aymeric's avatar
aymeric committed
281
}
282

283
static int ms_base_ref=0;
284
static int ms_plugins_ref=0;
285

286
void ms_base_init(){
aymeric's avatar
aymeric committed
287

288 289 290 291
	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
292
	ms_factory_get_fallback();
aymeric's avatar
aymeric committed
293 294
}

295
void ms_base_exit(){
296 297 298 299
	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
300
	ms_factory_destroy(ms_factory_get_fallback());
aymeric's avatar
aymeric committed
301 302
}

303
void ms_plugins_init(void) {
304 305 306 307
	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
308
	ms_factory_init_plugins(ms_factory_get_fallback());
309 310 311 312 313 314 315 316
}

void ms_plugins_exit(void) {
	if (--ms_plugins_ref >0 ) {
		ms_message ("Skiping ms_plugins_exit, still [%i] ref",ms_plugins_ref);
		return;
	}
	ms_factory_uninit_plugins(ms_factory_get_fallback());
317 318 319
}

void ms_set_plugins_dir(const char *path) {
Simon Morlat's avatar
Simon Morlat committed
320
	ms_factory_set_plugins_dir(ms_factory_get_fallback(),path);
321 322
}

aymeric's avatar
aymeric committed
323 324
void ms_sleep(int seconds){
#ifdef WIN32
325 326 327
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
	Sleep(seconds*1000);
#else
328 329 330 331
	HANDLE sleepEvent = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
	if (!sleepEvent) return;
	WaitForSingleObjectEx(sleepEvent, seconds * 1000, FALSE);
#endif
aymeric's avatar
aymeric committed
332 333 334 335 336 337 338 339 340 341 342 343
#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
}

344 345
void ms_usleep(uint64_t usec){
#ifdef WIN32
346 347 348
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
	Sleep((DWORD)(usec/1000));
#else
349 350 351 352
	HANDLE sleepEvent = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
	if (!sleepEvent) return;
	WaitForSingleObjectEx(sleepEvent, usec / 1000, FALSE);
#endif
353 354 355 356 357 358 359 360 361 362 363 364
#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
365
int ms_get_payload_max_size(){
Simon Morlat's avatar
Simon Morlat committed
366
	return ms_factory_get_payload_max_size(ms_factory_get_fallback());
aymeric's avatar
aymeric committed
367 368 369
}

void ms_set_payload_max_size(int size){
Simon Morlat's avatar
Simon Morlat committed
370
	ms_factory_set_payload_max_size(ms_factory_get_fallback(),size);
aymeric's avatar
aymeric committed
371
}
jehan's avatar
jehan committed
372

373
extern void _android_key_cleanup(void*);
374

375 376 377 378 379 380 381
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
382 383 384
#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
385
}
jehan's avatar
jehan committed
386 387


388
struct _MSConcealerContext {
389 390
	int64_t sample_time;
	int64_t plc_start_time;
391
	unsigned long total_number_for_plc;
392
	unsigned int max_plc_time;
393 394
};

jehan's avatar
jehan committed
395 396 397 398
/*** plc context begin***/
unsigned long ms_concealer_context_get_total_number_of_plc(MSConcealerContext* obj) {
	return obj->total_number_for_plc;
}
399

400
MSConcealerContext* ms_concealer_context_new(unsigned int max_plc_time){
401
	MSConcealerContext *obj=(MSConcealerContext *) ms_new(MSConcealerContext,1);
402 403
	obj->sample_time=-1;
	obj->plc_start_time=-1;
jehan's avatar
jehan committed
404
	obj->total_number_for_plc=0;
405
	obj->max_plc_time=max_plc_time;
406 407
	return obj;
}
408

409 410
void ms_concealer_context_destroy(MSConcealerContext* context) {
	ms_free(context);
411
}
412

413 414 415 416 417 418 419 420 421 422 423 424 425
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
426

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

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

431 432 433 434 435 436 437 438 439 440 441 442
	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;
443 444
		}
	}
445
	return 0;
446
}
447

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

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*/
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	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
508
/*** plc context end***/