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

#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
22 23
#include "gitversion.h"
#else
24 25 26
#   ifndef MEDIASTREAMER_VERSION
#   define MEDIASTREAMER_VERSION "unknown"
#   endif
27 28 29
#	ifndef GIT_VERSION
#	define GIT_VERSION "unknown"
#	endif
aymeric's avatar
aymeric committed
30 31 32
#endif

#include "mediastreamer2/mscommon.h"
33
#include "mediastreamer2/mscodecutils.h"
aymeric's avatar
aymeric committed
34 35
#include "mediastreamer2/msfilter.h"

36
#include "basedescs.h"
aymeric's avatar
aymeric committed
37 38 39 40 41 42 43 44 45

#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
46
#define PACKAGE_PLUGINS_DIR "lib\\mediastreamer\\plugins\\"
aymeric's avatar
aymeric committed
47 48 49 50 51 52 53 54 55
#else
#define PACKAGE_PLUGINS_DIR "."
#endif
#endif
#endif
#ifdef HAVE_DLOPEN
#include <dlfcn.h>
#endif

56 57
#ifdef __APPLE__
   #include "TargetConditionals.h"
aymeric's avatar
aymeric committed
58 59
#endif

jehan's avatar
jehan committed
60 61 62 63
#ifdef ANDROID
#include <android/log.h>
#endif

64 65 66 67
#if defined(WIN32) && !defined(_WIN32_WCE)
static MSList *ms_plugins_loaded_list;
#endif

68 69 70 71 72 73 74 75 76 77 78
static unsigned int cpu_count = 1;

unsigned int ms_get_cpu_count() {
	return cpu_count;
}

void ms_set_cpu_count(unsigned int c) {
	ms_message("CPU count set to %d", c);
	cpu_count = c;
}

aymeric's avatar
aymeric committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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
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;
}

MSList * ms_list_append(MSList *elem, void * data){
	MSList *new_elem=ms_list_new(data);
	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;
}

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;
}

MSList * ms_list_free(MSList *list){
	MSList *elem = list;
	MSList *tmp;
	if (list==NULL) return NULL;
	while(elem->next!=NULL) {
		tmp = elem;
		elem = elem->next;
		ms_free(tmp);
	}
	ms_free(elem);
	return NULL;
}

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;
	}
}

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;
}

184
MSList *ms_list_find_custom(MSList *list, int (*compare_func)(const void *, const void*), const void *user_data){
aymeric's avatar
aymeric committed
185 186 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
	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
274 275
#ifndef PLUGINS_EXT
	#define PLUGINS_EXT ".so"
aymeric's avatar
aymeric committed
276 277 278 279
#endif
typedef void (*init_func_t)(void);

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

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

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

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

309 310 311 312
		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
313
			ms_message("Fail to load plugin %s with altered search path: error %i",szPluginFile,(int)GetLastError());
314 315
			os_handle = LoadLibraryEx (szPluginFile, NULL, 0);
		}
316
		if (!debug) SetErrorMode (em);
317
		if (os_handle==NULL)
318
			ms_error("Fail to load plugin %s", szPluginFile);
319 320
		else{
			init_func_t initroutine;
321
			char szPluginName[256];
322 323 324 325 326 327 328 329 330 331 332 333
			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);
334 335
					// 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);
336 337 338 339 340 341 342
					num++;
				}else{
					ms_warning("Could not locate init routine of plugin %s. Should be %s",
					szPluginFile, szMethodName);
				}
		}
		if (!FindNextFile(hSearch, &FileData)) {
343 344 345 346 347 348 349 350
			if (GetLastError() == ERROR_NO_MORE_FILES){
				fFinished = TRUE;
			}
			else
			{
				ms_error("couldn't find next plugin dll.");
				fFinished = TRUE;
			}
351
		}
352
	}
353 354
	/* Close the search handle. */
	FindClose(hSearch);
aymeric's avatar
aymeric committed
355 356

#elif HAVE_DLOPEN
357
	char plugin_name[64];
aymeric's avatar
aymeric committed
358
	DIR *ds;
359
	MSList *loaded_plugins = NULL;
aymeric's avatar
aymeric committed
360
	struct dirent *de;
361
	char *ext;
aymeric's avatar
aymeric committed
362
	char *fullpath;
363
	ds=opendir(dir);
aymeric's avatar
aymeric committed
364 365 366 367 368
	if (ds==NULL){
		ms_message("Cannot open directory %s: %s",dir,strerror(errno));
		return -1;
	}
	while( (de=readdir(ds))!=NULL){
369
		if ((de->d_type==DT_REG || de->d_type==DT_UNKNOWN || de->d_type==DT_LNK) && (ext=strstr(de->d_name,PLUGINS_EXT))!=NULL) {
aymeric's avatar
aymeric committed
370
			void *handle;
371 372 373
			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
374 375
			fullpath=ms_strdup_printf("%s/%s",dir,de->d_name);
			ms_message("Loading plugin %s...",fullpath);
376

aymeric's avatar
aymeric committed
377 378 379 380 381 382 383 384
			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);
385 386 387 388
				if (p!=NULL){
					strcpy(p,"_init");
					initroutine=dlsym(handle,initroutine_name);
				}
aymeric's avatar
aymeric committed
389 390 391

#ifdef __APPLE__
				if (initroutine==NULL){
392 393 394 395 396 397 398 399
					/* 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
400 401 402 403 404 405
				}
#endif

				if (initroutine!=NULL){
					init_func_t func=(init_func_t)initroutine;
					func();
aymeric's avatar
aymeric committed
406
					ms_message("Plugin loaded (%s)", fullpath);
aymeric's avatar
aymeric committed
407 408 409 410 411 412 413 414 415
					num++;
				}else{
					ms_warning("Could not locate init routine of plugin %s",de->d_name);
				}
				ms_free(initroutine_name);
			}
			ms_free(fullpath);
		}
	}
416 417
	ms_list_for_each(loaded_plugins, ms_free);
	ms_list_free(loaded_plugins);
aymeric's avatar
aymeric committed
418 419 420 421 422 423 424 425
	closedir(ds);
#else
	ms_warning("no loadable plugin support: plugins cannot be loaded.");
	num=-1;
#endif
	return num;
}

426 427 428
void ms_unload_plugins(){
#if defined(WIN32) && !defined(_WIN32_WCE)
	 MSList *elem;
429

430 431 432 433 434 435 436 437 438
	for(elem=ms_plugins_loaded_list;elem!=NULL;elem=elem->next)
	{
		HINSTANCE handle=(HINSTANCE )elem->data;
        FreeLibrary(handle) ;
	}

	ms_list_free(ms_plugins_loaded_list);
#endif
}
aymeric's avatar
aymeric committed
439

jehan's avatar
jehan committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
#ifdef ANDROID
#define LOG_DOMAIN "mediastreamer"
static void ms_android_log_handler(OrtpLogLevel lev, const char *fmt, va_list args){
	int prio;
	switch(lev){
	case ORTP_DEBUG:	prio = ANDROID_LOG_DEBUG;	break;
	case ORTP_MESSAGE:	prio = ANDROID_LOG_INFO;	break;
	case ORTP_WARNING:	prio = ANDROID_LOG_WARN;	break;
	case ORTP_ERROR:	prio = ANDROID_LOG_ERROR;	break;
	case ORTP_FATAL:	prio = ANDROID_LOG_FATAL;	break;
	default:		prio = ANDROID_LOG_DEFAULT;	break;
	}
	__android_log_vprint(prio, LOG_DOMAIN, fmt, args);
}
#endif

456
void ms_base_init(){
aymeric's avatar
aymeric committed
457 458
	int i;

Simon Morlat's avatar
Simon Morlat committed
459 460 461 462
#if defined(ENABLE_NLS)
	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
#endif

aymeric's avatar
aymeric committed
463 464 465 466
#if !defined(_WIN32_WCE)
	if (getenv("MEDIASTREAMER_DEBUG")!=NULL){
		ortp_set_log_level_mask(ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
	}
jehan's avatar
jehan committed
467
#endif
468 469 470 471
//#ifdef ANDROID
//	ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
//	ortp_set_log_handler(ms_android_log_handler);
//#endif
472
	ms_message("Mediastreamer2 " MEDIASTREAMER_VERSION " (git: " GIT_VERSION ") starting.");
aymeric's avatar
aymeric committed
473
	/* register builtin MSFilter's */
474 475
	for (i=0;ms_base_filter_descs[i]!=NULL;i++){
		ms_filter_register(ms_base_filter_descs[i]);
aymeric's avatar
aymeric committed
476
	}
477

478
	ms_message("ms_base_init() done");
aymeric's avatar
aymeric committed
479 480
}

481
void ms_base_exit(){
aymeric's avatar
aymeric committed
482
	ms_filter_unregister_all();
483
	ms_unload_plugins();
aymeric's avatar
aymeric committed
484 485
}

486 487 488 489 490 491 492
void ms_plugins_init(void) {
#ifdef PACKAGE_PLUGINS_DIR
	ms_message("Loading ms plugins from [%s]",PACKAGE_PLUGINS_DIR);
	ms_load_plugins(PACKAGE_PLUGINS_DIR);
#endif
}

aymeric's avatar
aymeric committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
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
}

508 509
void ms_usleep(uint64_t usec){
#ifdef WIN32
Aymeric Moizard's avatar
Aymeric Moizard committed
510
	Sleep((DWORD)(usec/1000));
511 512 513 514 515 516 517 518 519 520 521 522
#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
523 524 525 526 527 528 529 530 531 532 533 534
#define DEFAULT_MAX_PAYLOAD_SIZE 1440

static int max_payload_size=DEFAULT_MAX_PAYLOAD_SIZE;

int ms_get_payload_max_size(){
	return max_payload_size;
}

void ms_set_payload_max_size(int size){
	if (size<=0) size=DEFAULT_MAX_PAYLOAD_SIZE;
	max_payload_size=size;
}
jehan's avatar
jehan committed
535

536
extern void _android_key_cleanup(void*);
537

538 539 540 541 542 543 544
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
545 546 547
#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
548
}
jehan's avatar
jehan committed
549 550


551
struct _MSConcealerContext {
552 553
	int64_t sample_time;
	int64_t plc_start_time;
554
	unsigned long total_number_for_plc;
555
	unsigned int max_plc_time;
556 557
};

jehan's avatar
jehan committed
558 559 560 561
/*** plc context begin***/
unsigned long ms_concealer_context_get_total_number_of_plc(MSConcealerContext* obj) {
	return obj->total_number_for_plc;
}
562

563
MSConcealerContext* ms_concealer_context_new(unsigned int max_plc_time){
564
	MSConcealerContext *obj=(MSConcealerContext *) ms_new(MSConcealerContext,1);	
565 566
	obj->sample_time=-1;
	obj->plc_start_time=-1;
jehan's avatar
jehan committed
567
	obj->total_number_for_plc=0;
568
	obj->max_plc_time=max_plc_time;
569 570
	return obj;
}
571

572 573
void ms_concealer_context_destroy(MSConcealerContext* context) {
	ms_free(context);
574
}
575

576 577 578 579 580 581 582 583 584 585 586 587 588
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
589

jehan's avatar
jehan committed
590 591
unsigned int ms_concealer_context_is_concealement_required(MSConcealerContext* obj,uint64_t current_time) {
	
592
	if(obj->sample_time == -1) return 0; /*no valid value*/
jehan's avatar
jehan committed
593
	
594 595 596 597 598 599 600 601 602 603 604 605
	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;
606 607
		}
	}
608
	return 0;
609
}
610

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