msfilter.c 13.9 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
/*
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/msfilter.h"
21
#include "mediastreamer2/msticker.h"
aymeric's avatar
aymeric committed
22

23
#define MS_FILTER_METHOD_GET_FID(id)	(((id)>>16) & 0xFFFF)
24
#define MS_FILTER_METHOD_GET_INDEX(id) ( ((id)>>8) & 0XFF)
Ghislain MARY's avatar
Ghislain MARY committed
25

aymeric's avatar
aymeric committed
26
static MSList *desc_list=NULL;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
static bool_t statistics_enabled=FALSE;
static MSList *stats_list=NULL;

static int compare_stats_with_name(const MSFilterStats *stat, const char *name){
	return strcmp(stat->name,name);
}

static MSFilterStats *find_or_create_stats(MSFilterDesc *desc){
	MSList *elem=ms_list_find_custom(stats_list,(MSCompareFunc)compare_stats_with_name,desc->name);
	MSFilterStats *ret=NULL;
	if (elem==NULL){
		ret=ms_new0(MSFilterStats,1);
		ret->name=desc->name;
		stats_list=ms_list_append(stats_list,ret);
	}else ret=(MSFilterStats*)elem->data;
	return ret;
}
aymeric's avatar
aymeric committed
44 45 46 47 48 49 50 51 52 53

void ms_filter_register(MSFilterDesc *desc){
	if (desc->id==MS_FILTER_NOT_SET_ID){
		ms_fatal("MSFilterId for %s not set !",desc->name);
	}
	/*lastly registered encoder/decoders may replace older ones*/
	desc_list=ms_list_prepend(desc_list,desc);
}

void ms_filter_unregister_all(){
54 55 56 57 58 59 60 61 62
	if (desc_list!=NULL) {
		ms_list_free(desc_list);
		desc_list=NULL;
	}
	if (stats_list!=NULL){
		ms_list_for_each(stats_list,ms_free);
		ms_list_free(stats_list);
		stats_list=NULL;
	}
aymeric's avatar
aymeric committed
63 64 65
}

bool_t ms_filter_codec_supported(const char *mime){
66 67 68 69 70
	MSFilterDesc *enc = ms_filter_get_encoding_capturer(mime);
	MSFilterDesc *dec = ms_filter_get_decoding_renderer(mime);

	if (enc == NULL) enc = ms_filter_get_encoder(mime);
	if (dec == NULL) dec = ms_filter_get_decoder(mime);
71 72 73 74 75

	if(enc!=NULL && dec!=NULL) return TRUE;

	if(enc==NULL) ms_message("Could not find encoder for %s", mime);
	if(dec==NULL) ms_message("Could not find decoder for %s", mime);
aymeric's avatar
aymeric committed
76 77 78
	return FALSE;
}

79 80 81 82 83 84
MSFilterDesc * ms_filter_get_encoding_capturer(const char *mime) {
	MSList *elem;

	for (elem = desc_list; elem != NULL; elem = ms_list_next(elem)) {
		MSFilterDesc *desc = (MSFilterDesc *)elem->data;
		if (desc->category == MS_FILTER_ENCODING_CAPTURER) {
85
			char *saveptr=NULL;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
			char *enc_fmt = ms_strdup(desc->enc_fmt);
			char *token = strtok_r(enc_fmt, " ", &saveptr);
			while (token != NULL) {
				if (strcasecmp(token, mime) == 0) {
					break;
				}
				token = strtok_r(NULL, " ", &saveptr);
			}
			ms_free(enc_fmt);
			if (token != NULL) return desc;
		}
	}

	return NULL;
}

MSFilterDesc * ms_filter_get_decoding_renderer(const char *mime) {
	MSList *elem;

	for (elem = desc_list; elem != NULL; elem = ms_list_next(elem)) {
		MSFilterDesc *desc = (MSFilterDesc *)elem->data;
107
		if (desc->category == MS_FILTER_DECODER_RENDERER) {
108
			char *saveptr=NULL;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
			char *enc_fmt = ms_strdup(desc->enc_fmt);
			char *token = strtok_r(enc_fmt, " ", &saveptr);
			while (token != NULL) {
				if (strcasecmp(token, mime) == 0) {
					break;
				}
				token = strtok_r(NULL, " ", &saveptr);
			}
			ms_free(enc_fmt);
			if (token != NULL) return desc;
		}
	}

	return NULL;
}

aymeric's avatar
aymeric committed
125 126 127 128
MSFilterDesc * ms_filter_get_encoder(const char *mime){
	MSList *elem;
	for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){
		MSFilterDesc *desc=(MSFilterDesc*)elem->data;
129
		if (desc->category==MS_FILTER_ENCODER &&
aymeric's avatar
aymeric committed
130 131 132 133 134 135 136 137 138 139 140
			strcasecmp(desc->enc_fmt,mime)==0){
			return desc;
		}
	}
	return NULL;
}

MSFilterDesc * ms_filter_get_decoder(const char *mime){
	MSList *elem;
	for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){
		MSFilterDesc *desc=(MSFilterDesc*)elem->data;
141
		if (desc->category==MS_FILTER_DECODER &&
aymeric's avatar
aymeric committed
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
			strcasecmp(desc->enc_fmt,mime)==0){
			return desc;
		}
	}
	return NULL;
}

MSFilter * ms_filter_create_encoder(const char *mime){
	MSFilterDesc *desc=ms_filter_get_encoder(mime);
	if (desc!=NULL) return ms_filter_new_from_desc(desc);
	return NULL;
}

MSFilter * ms_filter_create_decoder(const char *mime){
	MSFilterDesc *desc=ms_filter_get_decoder(mime);
	if (desc!=NULL) return ms_filter_new_from_desc(desc);
	return NULL;
}

MSFilter *ms_filter_new_from_desc(MSFilterDesc *desc){
	MSFilter *obj;
	obj=(MSFilter *)ms_new0(MSFilter,1);
	ms_mutex_init(&obj->lock,NULL);
	obj->desc=desc;
	if (desc->ninputs>0)	obj->inputs=(MSQueue**)ms_new0(MSQueue*,desc->ninputs);
	if (desc->noutputs>0)	obj->outputs=(MSQueue**)ms_new0(MSQueue*,desc->noutputs);
168 169 170 171

	if (statistics_enabled){
		obj->stats=find_or_create_stats(desc);
	}
aymeric's avatar
aymeric committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	if (obj->desc->init!=NULL)
		obj->desc->init(obj);
	return obj;
}

MSFilter *ms_filter_new(MSFilterId id){
	MSList *elem;
	if (id==MS_FILTER_PLUGIN_ID){
		ms_warning("cannot create plugin filters with ms_filter_new_from_id()");
		return NULL;
	}
	for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){
		MSFilterDesc *desc=(MSFilterDesc*)elem->data;
		if (desc->id==id){
			return ms_filter_new_from_desc(desc);
		}
	}
	ms_error("No such filter with id %i",id);
	return NULL;
}

193
MSFilterDesc *ms_filter_lookup_by_name(const char *filter_name){
aymeric's avatar
aymeric committed
194 195 196 197
	MSList *elem;
	for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){
		MSFilterDesc *desc=(MSFilterDesc*)elem->data;
		if (strcmp(desc->name,filter_name)==0){
198
			return desc;
aymeric's avatar
aymeric committed
199 200 201 202 203
		}
	}
	return NULL;
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
bool_t ms_filter_desc_implements_interface(MSFilterDesc *desc, MSFilterInterfaceId id){
	MSFilterMethod *methods=desc->methods;
	if (!methods) return FALSE;
	for(;methods->id!=0;methods++){
		unsigned int fid=MS_FILTER_METHOD_GET_FID(methods->id);
		if (fid==id) return TRUE;
	}
	return FALSE;
}

MSList *ms_filter_lookup_by_interface(MSFilterInterfaceId id){
	MSList *ret=NULL;
	MSList *elem;
	for(elem=desc_list;elem!=NULL;elem=elem->next){
		MSFilterDesc *desc=(MSFilterDesc*)elem->data;
		if (ms_filter_desc_implements_interface(desc,id))
			ret=ms_list_append(ret,desc);
	}
	return ret;
}

225 226 227 228 229 230
MSFilter *ms_filter_new_from_name(const char *filter_name){
	MSFilterDesc *desc=ms_filter_lookup_by_name(filter_name);
	if (desc==NULL) return NULL;
	return ms_filter_new_from_desc(desc);
}

aymeric's avatar
aymeric committed
231 232 233 234 235 236 237

MSFilterId ms_filter_get_id(MSFilter *f){
	return f->desc->id;
}

int ms_filter_link(MSFilter *f1, int pin1, MSFilter *f2, int pin2){
	MSQueue *q;
238
	ms_message("ms_filter_link: %s:%p,%i-->%s:%p,%i",f1->desc->name,f1,pin1,f2->desc->name,f2,pin2);
aymeric's avatar
aymeric committed
239 240 241 242 243 244 245 246 247 248 249 250
	ms_return_val_if_fail(pin1<f1->desc->noutputs, -1);
	ms_return_val_if_fail(pin2<f2->desc->ninputs, -1);
	ms_return_val_if_fail(f1->outputs[pin1]==NULL,-1);
	ms_return_val_if_fail(f2->inputs[pin2]==NULL,-1);
	q=ms_queue_new(f1,pin1,f2,pin2);
	f1->outputs[pin1]=q;
	f2->inputs[pin2]=q;
	return 0;
}

int ms_filter_unlink(MSFilter *f1, int pin1, MSFilter *f2, int pin2){
	MSQueue *q;
251
	ms_message("ms_filter_unlink: %s:%p,%i-->%s:%p,%i",f1 ? f1->desc->name : "!NULL!",f1,pin1,f2 ? f2->desc->name : "!NULL!",f2,pin2);
aymeric's avatar
aymeric committed
252 253 254 255 256 257 258 259 260 261 262
	ms_return_val_if_fail(pin1<f1->desc->noutputs, -1);
	ms_return_val_if_fail(pin2<f2->desc->ninputs, -1);
	ms_return_val_if_fail(f1->outputs[pin1]!=NULL,-1);
	ms_return_val_if_fail(f2->inputs[pin2]!=NULL,-1);
	ms_return_val_if_fail(f1->outputs[pin1]==f2->inputs[pin2],-1);
	q=f1->outputs[pin1];
	f1->outputs[pin1]=f2->inputs[pin2]=0;
	ms_queue_destroy(q);
	return 0;
}

263 264 265 266
static inline bool_t is_interface_method(unsigned int magic){
	return magic==MS_FILTER_BASE_ID || magic>MSFilterInterfaceBegin;
}

aymeric's avatar
aymeric committed
267 268 269 270
int ms_filter_call_method(MSFilter *f, unsigned int id, void *arg){
	MSFilterMethod *methods=f->desc->methods;
	int i;
	unsigned int magic=MS_FILTER_METHOD_GET_FID(id);
271
	if (!is_interface_method(magic) && magic!=f->desc->id) {
272
		ms_fatal("Method type checking failed when calling %u on filter %s",id,f->desc->name);
aymeric's avatar
aymeric committed
273 274 275 276
		return -1;
	}
	for(i=0;methods!=NULL && methods[i].method!=NULL; i++){
		unsigned int mm=MS_FILTER_METHOD_GET_FID(methods[i].id);
277
		if (mm!=f->desc->id && !is_interface_method(mm)) {
278
			ms_fatal("Bad method definition on filter %s. fid=%u , mm=%u",f->desc->name,f->desc->id,mm);
aymeric's avatar
aymeric committed
279 280 281 282 283 284
			return -1;
		}
		if (methods[i].id==id){
			return methods[i].method(f,arg);
		}
	}
285 286
	if (magic!=MS_FILTER_BASE_ID) ms_error("no such method on filter %s, fid=%i method index=%i",f->desc->name,magic,
	                           MS_FILTER_METHOD_GET_INDEX(id) );
aymeric's avatar
aymeric committed
287 288 289
	return -1;
}

Simon Morlat's avatar
Simon Morlat committed
290 291 292 293 294 295 296 297
bool_t ms_filter_has_method(MSFilter *f, unsigned int id){
	MSFilterMethod *methods=f->desc->methods;
	int i;
	for(i=0;methods!=NULL && methods[i].method!=NULL; i++)
		if (methods[i].id==id) return TRUE;
	return FALSE;
}

aymeric's avatar
aymeric committed
298 299 300 301 302 303 304 305 306 307
int ms_filter_call_method_noarg(MSFilter *f, unsigned int id){
	return ms_filter_call_method(f,id,NULL);
}

void ms_filter_destroy(MSFilter *f){
	if (f->desc->uninit!=NULL)
		f->desc->uninit(f);
	if (f->inputs!=NULL)	ms_free(f->inputs);
	if (f->outputs!=NULL)	ms_free(f->outputs);
	ms_mutex_destroy(&f->lock);
308
	ms_filter_clear_notify_callback(f);
309
	ms_filter_clean_pending_events(f);
aymeric's avatar
aymeric committed
310 311 312 313
	ms_free(f);
}

void ms_filter_process(MSFilter *f){
314
	MSTimeSpec start,stop;
aymeric's avatar
aymeric committed
315
	ms_debug("Executing process of filter %s:%p",f->desc->name,f);
316 317

	if (f->stats)
318
		ms_get_cur_time(&start);
319

aymeric's avatar
aymeric committed
320
	f->desc->process(f);
321
	if (f->stats){
322
		ms_get_cur_time(&stop);
323
		f->stats->count++;
324
		f->stats->elapsed+=(stop.tv_sec-start.tv_sec)*1000000000LL + (stop.tv_nsec-start.tv_nsec);
325
	}
326

aymeric's avatar
aymeric committed
327 328
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
void ms_filter_task_process(MSFilterTask *task){
	MSTimeSpec start,stop;
	MSFilter *f=task->f;
	/*ms_message("Executing task of filter %s:%p",f->desc->name,f);*/

	if (f->stats)
		ms_get_cur_time(&start);

	task->taskfunc(f);
	if (f->stats){
		ms_get_cur_time(&stop);
		f->stats->count++;
		f->stats->elapsed+=(stop.tv_sec-start.tv_sec)*1000000000LL + (stop.tv_nsec-start.tv_nsec);
	}
	f->postponed_task--;
}

aymeric's avatar
aymeric committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
void ms_filter_preprocess(MSFilter *f, struct _MSTicker *t){
	f->last_tick=0;
	f->ticker=t;
	if (f->desc->preprocess!=NULL)
		f->desc->preprocess(f);
}

void ms_filter_postprocess(MSFilter *f){
	if (f->desc->postprocess!=NULL)
		f->desc->postprocess(f);
	f->ticker=NULL;
}

bool_t ms_filter_inputs_have_data(MSFilter *f){
	int i;
	for(i=0;i<f->desc->ninputs;i++){
		MSQueue *q=f->inputs[i];
		if (q!=NULL && q->q.q_mcount>0) return TRUE;
	}
	return FALSE;
}

368 369 370 371 372 373 374 375 376 377 378 379 380
void ms_filter_postpone_task(MSFilter *f, MSFilterFunc taskfunc){
	MSFilterTask *task;
	MSTicker *ticker=f->ticker;
	if (ticker==NULL){
		ms_error("ms_filter_postpone_task(): this method cannot be called outside of filter's process method.");
		return;
	}
	task=ms_new(MSFilterTask,1);
	task->f=f;
	task->taskfunc=taskfunc;
	ticker->task_list=ms_list_prepend(ticker->task_list,task);
	f->postponed_task++;
}
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

static void find_filters(MSList **filters, MSFilter *f ){
	int i,found;
	MSQueue *link;
	if (f==NULL) ms_fatal("Bad graph.");
	/*ms_message("seeing %s, seen=%i",f->desc->name,f->seen);*/
	if (f->seen){
		return;
	}
	f->seen=TRUE;
	*filters=ms_list_append(*filters,f);
	/* go upstream */
	for(i=0;i<f->desc->ninputs;i++){
		link=f->inputs[i];
		if (link!=NULL) find_filters(filters,link->prev.filter);
	}
	/* go downstream */
	for(i=0,found=0;i<f->desc->noutputs;i++){
		link=f->outputs[i];
		if (link!=NULL) {
			found++;
			find_filters(filters,link->next.filter);
		}
	}
	if (f->desc->noutputs>=1 && found==0){
		ms_fatal("Bad graph: filter %s has %i outputs, none is connected.",f->desc->name,f->desc->noutputs);
	}
}

MSList * ms_filter_find_neighbours(MSFilter *me){
	MSList *l=NULL;
	MSList *it;
	find_filters(&l,me);
	/*reset seen boolean for further lookups to succeed !*/
	for(it=l;it!=NULL;it=it->next){
		MSFilter *f=(MSFilter*)it->data;
		f->seen=FALSE;
	}
	return l;
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
void ms_connection_helper_start(MSConnectionHelper *h){
	h->last.filter=0;
	h->last.pin=-1;
}

int ms_connection_helper_link(MSConnectionHelper *h, MSFilter *f, int inpin, int outpin){
	int err=0;
	if (h->last.filter==NULL){
		h->last.filter=f;
		h->last.pin=outpin;
	}else{
		err=ms_filter_link(h->last.filter,h->last.pin,f,inpin);
		if (err==0){
			h->last.filter=f;
			h->last.pin=outpin;
		}
	}
	return err;
}

int ms_connection_helper_unlink(MSConnectionHelper *h, MSFilter *f, int inpin, int outpin){
	int err=0;
	if (h->last.filter==NULL){
		h->last.filter=f;
		h->last.pin=outpin;
	}else{
		err=ms_filter_unlink(h->last.filter,h->last.pin,f,inpin);
		if (err==0){
			h->last.filter=f;
			h->last.pin=outpin;
		}
	}
	return err;
}

457 458 459 460 461 462 463 464
void ms_filter_enable_statistics(bool_t enabled){
	statistics_enabled=enabled;
}

const MSList * ms_filter_get_statistics(void){
	return stats_list;
}

465 466
void ms_filter_reset_statistics(void){
	MSList *elem;
467

468 469 470 471 472 473 474
	for(elem=stats_list;elem!=NULL;elem=elem->next){
		MSFilterStats *stats=(MSFilterStats *)elem->data;
		stats->elapsed=0;
		stats->count=0;
	}
}

475 476 477 478 479 480 481 482 483 484
static int usage_compare(const MSFilterStats *s1, const MSFilterStats *s2){
	if (s1->elapsed==s2->elapsed) return 0;
	if (s1->elapsed<s2->elapsed) return 1;
	return -1;
}


void ms_filter_log_statistics(void){
	MSList *sorted=NULL;
	MSList *elem;
485
	uint64_t total=1;
486 487 488 489 490
	for(elem=stats_list;elem!=NULL;elem=elem->next){
		MSFilterStats *stats=(MSFilterStats *)elem->data;
		sorted=ms_list_insert_sorted(sorted,stats,(MSCompareFunc)usage_compare);
		total+=stats->elapsed;
	}
491 492 493 494
	ms_message("===========================================================");
	ms_message("                  FILTER USAGE STATISTICS                  ");
	ms_message("Name                Count     Time/tick (ms)      CPU Usage");
	ms_message("-----------------------------------------------------------");
495 496 497
	for(elem=sorted;elem!=NULL;elem=elem->next){
		MSFilterStats *stats=(MSFilterStats *)elem->data;
		double percentage=100.0*((double)stats->elapsed)/(double)total;
Simon Morlat's avatar
Simon Morlat committed
498
		double tpt=((double)stats->elapsed*1e-6)/((double)stats->count+1.0);
499
		ms_message("%-19s %-9i %-19g %-10g",stats->name,stats->count,tpt,percentage);
500
	}
501
	ms_message("===========================================================");
502 503
	ms_list_free(sorted);
}
504