msfilter.c 11.4 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 22
/*
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"

static MSList *desc_list=NULL;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
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
40 41 42 43 44 45 46 47 48 49

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(){
50 51 52 53 54 55 56 57 58
	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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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
}

bool_t ms_filter_codec_supported(const char *mime){
	if (ms_filter_get_encoder(mime)!=NULL
		&& ms_filter_get_decoder(mime)!=NULL) return TRUE;
	return FALSE;
}

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;
		if (desc->category==MS_FILTER_ENCODER && 
			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;
		if (desc->category==MS_FILTER_DECODER && 
			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);
110 111 112 113

	if (statistics_enabled){
		obj->stats=find_or_create_stats(desc);
	}
aymeric's avatar
aymeric committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	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;
}

135
MSFilterDesc *ms_filter_lookup_by_name(const char *filter_name){
aymeric's avatar
aymeric committed
136 137 138 139
	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){
140
			return desc;
aymeric's avatar
aymeric committed
141 142 143 144 145
		}
	}
	return NULL;
}

146 147 148 149 150 151
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
152 153 154 155 156 157 158

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;
159
	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
160 161 162 163 164 165 166 167 168 169 170 171 172 173
	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;
	ms_return_val_if_fail(f1, -1);
	ms_return_val_if_fail(f2, -1);
Simon Morlat's avatar
Simon Morlat committed
174
	ms_message("ms_filter_unlink: %s:%p,%i-->%s:%p,%i",f1->desc->name,f1,pin1,f2->desc->name,f2,pin2);
aymeric's avatar
aymeric committed
175 176 177 178 179 180 181 182 183 184 185 186
	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;
}

#define MS_FILTER_METHOD_GET_FID(id)	(((id)>>16) & 0xFFFF)
187
#define MS_FILTER_METHOD_GET_INDEX(id) ( ((id)>>8) & 0XFF) 
aymeric's avatar
aymeric committed
188

189 190 191 192
static inline bool_t is_interface_method(unsigned int magic){
	return magic==MS_FILTER_BASE_ID || magic>MSFilterInterfaceBegin;
}

aymeric's avatar
aymeric committed
193 194 195 196
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);
197
	if (!is_interface_method(magic) && magic!=f->desc->id) {
198
		ms_fatal("Method type checking failed when calling %u on filter %s",id,f->desc->name);
aymeric's avatar
aymeric committed
199 200 201 202
		return -1;
	}
	for(i=0;methods!=NULL && methods[i].method!=NULL; i++){
		unsigned int mm=MS_FILTER_METHOD_GET_FID(methods[i].id);
203
		if (mm!=f->desc->id && !is_interface_method(mm)) {
204
			ms_fatal("Bad method definition on filter %s. fid=%u , mm=%u",f->desc->name,f->desc->id,mm);
aymeric's avatar
aymeric committed
205 206 207 208 209 210
			return -1;
		}
		if (methods[i].id==id){
			return methods[i].method(f,arg);
		}
	}
211 212
	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
213 214 215 216 217 218 219 220 221 222 223 224
	return -1;
}

int ms_filter_call_method_noarg(MSFilter *f, unsigned int id){
	return ms_filter_call_method(f,id,NULL);
}

void ms_filter_set_notify_callback(MSFilter *f, MSFilterNotifyFunc fn, void *ud){
	f->notify=fn;
	f->notify_ud=ud;
}

225 226 227 228
void ms_filter_enable_synchronous_notifcations(MSFilter *f, bool_t yesno){
	f->synchronous_notifies=yesno;
}

aymeric's avatar
aymeric committed
229 230 231 232 233 234 235 236 237
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);
	ms_free(f);
}

238

239 240

static uint64_t get_cur_time_ns(void)
241 242 243
{
#if defined(_WIN32_WCE)
	DWORD timemillis = GetTickCount();
244
	return (uint64_t)timemillis*1000000;
245
#elif defined(WIN32)
246
	return timeGetTime()*1000000LL ;
247 248 249
#elif defined(__MACH__) && defined(__GNUC__) && (__GNUC__ >= 3)
	struct timeval tv;
	gettimeofday(&tv, NULL);
250
	return (tv.tv_sec*1000000000LL) + (tv.tv_usec*1000LL);
251 252 253 254 255 256 257
#elif defined(__MACH__)
	struct timespec ts;
	struct timeb time_val;

	ftime (&time_val);
	ts.tv_sec = time_val.time;
	ts.tv_nsec = time_val.millitm * 1000000;
258
	return (ts.tv_sec*1000000000LL) + ts.tv_nsec;
259 260 261
#else
	struct timespec ts;
	if (clock_gettime(CLOCK_MONOTONIC,&ts)<0){
262
		ms_fatal("clock_gettime() doesn't work: %s",strerror(errno));
263
	}
264
	return (ts.tv_sec*1000000000LL) + ts.tv_nsec;
265 266
#endif
}
267

aymeric's avatar
aymeric committed
268 269

void ms_filter_process(MSFilter *f){
270
	uint64_t start=0,stop;
aymeric's avatar
aymeric committed
271
	ms_debug("Executing process of filter %s:%p",f->desc->name,f);
272 273 274 275

	if (f->stats)
		start = get_cur_time_ns();

aymeric's avatar
aymeric committed
276
	f->desc->process(f);
277 278 279 280
	if (f->stats){
		stop = get_cur_time_ns();
		f->stats->count++;
		f->stats->elapsed+=stop-start;
281
	}
282

aymeric's avatar
aymeric committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
}

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

307

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

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

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
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;
}

384 385 386 387 388 389 390 391
void ms_filter_enable_statistics(bool_t enabled){
	statistics_enabled=enabled;
}

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

392 393 394 395 396 397 398 399 400 401
void ms_filter_reset_statistics(void){
	MSList *elem;
	
	for(elem=stats_list;elem!=NULL;elem=elem->next){
		MSFilterStats *stats=(MSFilterStats *)elem->data;
		stats->elapsed=0;
		stats->count=0;
	}
}

402 403 404 405 406 407 408 409 410 411
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;
412
	uint64_t total=1;
413 414 415 416 417 418
	ms_message("Filter usage statistics:");
	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;
	}
Simon Morlat's avatar
Simon Morlat committed
419
	ms_message("Name\tCount\tTime/tick (ms)\tCPU Usage");
420 421 422
	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
423 424
		double tpt=((double)stats->elapsed*1e-6)/((double)stats->count+1.0);
		ms_message("%s %i %g %g",stats->name,stats->count,tpt,percentage);
425 426 427
	}
	ms_list_free(sorted);
}
428