| 1 | /* |
|---|
| 2 | mediastreamer2 library - modular sound and video processing and streaming |
|---|
| 3 | Copyright (C) 2006 Simon MORLAT (simon.morlat@linphone.org) |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or |
|---|
| 6 | modify it under the terms of the GNU General Public License |
|---|
| 7 | as published by the Free Software Foundation; either version 2 |
|---|
| 8 | of the License, or (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include "mediastreamer2/msfilter.h" |
|---|
| 21 | #include "mediastreamer2/mscommon.h" |
|---|
| 22 | |
|---|
| 23 | static MSList *desc_list=NULL; |
|---|
| 24 | |
|---|
| 25 | void ms_filter_register(MSFilterDesc *desc){ |
|---|
| 26 | if (desc->id==MS_FILTER_NOT_SET_ID){ |
|---|
| 27 | ms_fatal("MSFilterId for %s not set !",desc->name); |
|---|
| 28 | } |
|---|
| 29 | /*lastly registered encoder/decoders may replace older ones*/ |
|---|
| 30 | desc_list=ms_list_prepend(desc_list,desc); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | void ms_filter_unregister_all(){ |
|---|
| 34 | if (desc_list!=NULL) desc_list=ms_list_free(desc_list); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | bool_t ms_filter_codec_supported(const char *mime){ |
|---|
| 38 | if (ms_filter_get_encoder(mime)!=NULL |
|---|
| 39 | && ms_filter_get_decoder(mime)!=NULL) return TRUE; |
|---|
| 40 | return FALSE; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | MSFilterDesc * ms_filter_get_encoder(const char *mime){ |
|---|
| 44 | MSList *elem; |
|---|
| 45 | for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){ |
|---|
| 46 | MSFilterDesc *desc=(MSFilterDesc*)elem->data; |
|---|
| 47 | if (desc->category==MS_FILTER_ENCODER && |
|---|
| 48 | strcasecmp(desc->enc_fmt,mime)==0){ |
|---|
| 49 | return desc; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | return NULL; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | MSFilterDesc * ms_filter_get_decoder(const char *mime){ |
|---|
| 56 | MSList *elem; |
|---|
| 57 | for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){ |
|---|
| 58 | MSFilterDesc *desc=(MSFilterDesc*)elem->data; |
|---|
| 59 | if (desc->category==MS_FILTER_DECODER && |
|---|
| 60 | strcasecmp(desc->enc_fmt,mime)==0){ |
|---|
| 61 | return desc; |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | return NULL; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | MSFilter * ms_filter_create_encoder(const char *mime){ |
|---|
| 68 | MSFilterDesc *desc=ms_filter_get_encoder(mime); |
|---|
| 69 | if (desc!=NULL) return ms_filter_new_from_desc(desc); |
|---|
| 70 | return NULL; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | MSFilter * ms_filter_create_decoder(const char *mime){ |
|---|
| 74 | MSFilterDesc *desc=ms_filter_get_decoder(mime); |
|---|
| 75 | if (desc!=NULL) return ms_filter_new_from_desc(desc); |
|---|
| 76 | return NULL; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | MSFilter *ms_filter_new_from_desc(MSFilterDesc *desc){ |
|---|
| 80 | MSFilter *obj; |
|---|
| 81 | obj=(MSFilter *)ms_new0(MSFilter,1); |
|---|
| 82 | ms_mutex_init(&obj->lock,NULL); |
|---|
| 83 | obj->desc=desc; |
|---|
| 84 | if (desc->ninputs>0) obj->inputs=(MSQueue**)ms_new0(MSQueue*,desc->ninputs); |
|---|
| 85 | if (desc->noutputs>0) obj->outputs=(MSQueue**)ms_new0(MSQueue*,desc->noutputs); |
|---|
| 86 | if (obj->desc->init!=NULL) |
|---|
| 87 | obj->desc->init(obj); |
|---|
| 88 | return obj; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | MSFilter *ms_filter_new(MSFilterId id){ |
|---|
| 92 | MSList *elem; |
|---|
| 93 | if (id==MS_FILTER_PLUGIN_ID){ |
|---|
| 94 | ms_warning("cannot create plugin filters with ms_filter_new_from_id()"); |
|---|
| 95 | return NULL; |
|---|
| 96 | } |
|---|
| 97 | for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){ |
|---|
| 98 | MSFilterDesc *desc=(MSFilterDesc*)elem->data; |
|---|
| 99 | if (desc->id==id){ |
|---|
| 100 | return ms_filter_new_from_desc(desc); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | ms_error("No such filter with id %i",id); |
|---|
| 104 | return NULL; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | MSFilter *ms_filter_new_from_name(const char *filter_name){ |
|---|
| 108 | MSList *elem; |
|---|
| 109 | for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){ |
|---|
| 110 | MSFilterDesc *desc=(MSFilterDesc*)elem->data; |
|---|
| 111 | if (strcmp(desc->name,filter_name)==0){ |
|---|
| 112 | return ms_filter_new_from_desc(desc); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | ms_error("No such filter with name %s",filter_name); |
|---|
| 116 | return NULL; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | MSFilterId ms_filter_get_id(MSFilter *f){ |
|---|
| 121 | return f->desc->id; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | int ms_filter_link(MSFilter *f1, int pin1, MSFilter *f2, int pin2){ |
|---|
| 125 | MSQueue *q; |
|---|
| 126 | ms_message("ms_filter_link: %s:%p,%i-->%s:%p,%i",f1->desc->name,f1,pin1,f2->desc->name,f2,pin2); |
|---|
| 127 | ms_return_val_if_fail(pin1<f1->desc->noutputs, -1); |
|---|
| 128 | ms_return_val_if_fail(pin2<f2->desc->ninputs, -1); |
|---|
| 129 | ms_return_val_if_fail(f1->outputs[pin1]==NULL,-1); |
|---|
| 130 | ms_return_val_if_fail(f2->inputs[pin2]==NULL,-1); |
|---|
| 131 | q=ms_queue_new(f1,pin1,f2,pin2); |
|---|
| 132 | f1->outputs[pin1]=q; |
|---|
| 133 | f2->inputs[pin2]=q; |
|---|
| 134 | return 0; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | int ms_filter_unlink(MSFilter *f1, int pin1, MSFilter *f2, int pin2){ |
|---|
| 138 | MSQueue *q; |
|---|
| 139 | ms_return_val_if_fail(f1, -1); |
|---|
| 140 | ms_return_val_if_fail(f2, -1); |
|---|
| 141 | ms_message("ms_filter_unlink: %s:%p,%i-->%s:%p,%i",f1->desc->name,f1,pin1,f2->desc->name,f2,pin2); |
|---|
| 142 | ms_return_val_if_fail(pin1<f1->desc->noutputs, -1); |
|---|
| 143 | ms_return_val_if_fail(pin2<f2->desc->ninputs, -1); |
|---|
| 144 | ms_return_val_if_fail(f1->outputs[pin1]!=NULL,-1); |
|---|
| 145 | ms_return_val_if_fail(f2->inputs[pin2]!=NULL,-1); |
|---|
| 146 | ms_return_val_if_fail(f1->outputs[pin1]==f2->inputs[pin2],-1); |
|---|
| 147 | q=f1->outputs[pin1]; |
|---|
| 148 | f1->outputs[pin1]=f2->inputs[pin2]=0; |
|---|
| 149 | ms_queue_destroy(q); |
|---|
| 150 | return 0; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | #define MS_FILTER_METHOD_GET_FID(id) (((id)>>16) & 0xFFFF) |
|---|
| 154 | #define MS_FILTER_METHOD_GET_INDEX(id) ( ((id)>>8) & 0XFF) |
|---|
| 155 | |
|---|
| 156 | static inline bool_t is_interface_method(unsigned int magic){ |
|---|
| 157 | return magic==MS_FILTER_BASE_ID || magic>MSFilterInterfaceBegin; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | int ms_filter_call_method(MSFilter *f, unsigned int id, void *arg){ |
|---|
| 161 | MSFilterMethod *methods=f->desc->methods; |
|---|
| 162 | int i; |
|---|
| 163 | unsigned int magic=MS_FILTER_METHOD_GET_FID(id); |
|---|
| 164 | if (!is_interface_method(magic) && magic!=f->desc->id) { |
|---|
| 165 | ms_fatal("Method type checking failed when calling %u on filter %s",id,f->desc->name); |
|---|
| 166 | return -1; |
|---|
| 167 | } |
|---|
| 168 | for(i=0;methods!=NULL && methods[i].method!=NULL; i++){ |
|---|
| 169 | unsigned int mm=MS_FILTER_METHOD_GET_FID(methods[i].id); |
|---|
| 170 | if (mm!=f->desc->id && !is_interface_method(mm)) { |
|---|
| 171 | ms_fatal("Bad method definition on filter %s. fid=%u , mm=%u",f->desc->name,f->desc->id,mm); |
|---|
| 172 | return -1; |
|---|
| 173 | } |
|---|
| 174 | if (methods[i].id==id){ |
|---|
| 175 | return methods[i].method(f,arg); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | if (magic!=MS_FILTER_BASE_ID) ms_error("no such method on filter %s, fid=%i method index=%i",f->desc->name,magic, |
|---|
| 179 | MS_FILTER_METHOD_GET_INDEX(id) ); |
|---|
| 180 | return -1; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | int ms_filter_call_method_noarg(MSFilter *f, unsigned int id){ |
|---|
| 184 | return ms_filter_call_method(f,id,NULL); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | void ms_filter_set_notify_callback(MSFilter *f, MSFilterNotifyFunc fn, void *ud){ |
|---|
| 188 | f->notify=fn; |
|---|
| 189 | f->notify_ud=ud; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | void ms_filter_enable_synchronous_notifcations(MSFilter *f, bool_t yesno){ |
|---|
| 193 | f->synchronous_notifies=yesno; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | void ms_filter_destroy(MSFilter *f){ |
|---|
| 197 | if (f->desc->uninit!=NULL) |
|---|
| 198 | f->desc->uninit(f); |
|---|
| 199 | if (f->inputs!=NULL) ms_free(f->inputs); |
|---|
| 200 | if (f->outputs!=NULL) ms_free(f->outputs); |
|---|
| 201 | ms_mutex_destroy(&f->lock); |
|---|
| 202 | ms_free(f); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | #ifdef DEBUG |
|---|
| 206 | |
|---|
| 207 | static long filter_get_cur_time(void *unused) |
|---|
| 208 | { |
|---|
| 209 | #if defined(_WIN32_WCE) |
|---|
| 210 | DWORD timemillis = GetTickCount(); |
|---|
| 211 | return timemillis; |
|---|
| 212 | #elif defined(WIN32) |
|---|
| 213 | return timeGetTime() ; |
|---|
| 214 | #elif defined(__MACH__) && defined(__GNUC__) && (__GNUC__ >= 3) |
|---|
| 215 | struct timeval tv; |
|---|
| 216 | gettimeofday(&tv, NULL); |
|---|
| 217 | return (tv.tv_sec*1000LL) + (tv.tv_usec/1000LL); |
|---|
| 218 | #elif defined(__MACH__) |
|---|
| 219 | struct timespec ts; |
|---|
| 220 | struct timeb time_val; |
|---|
| 221 | |
|---|
| 222 | ftime (&time_val); |
|---|
| 223 | ts.tv_sec = time_val.time; |
|---|
| 224 | ts.tv_nsec = time_val.millitm * 1000000; |
|---|
| 225 | return (ts.tv_sec*1000LL) + (ts.tv_nsec/1000000LL); |
|---|
| 226 | #else |
|---|
| 227 | struct timespec ts; |
|---|
| 228 | if (clock_gettime(CLOCK_MONOTONIC,&ts)<0){ |
|---|
| 229 | fprintf(stderr, "clock_gettime() doesn't work: %s",strerror(errno)); |
|---|
| 230 | } |
|---|
| 231 | return (ts.tv_sec*1000LL) + (ts.tv_nsec/1000000LL); |
|---|
| 232 | #endif |
|---|
| 233 | } |
|---|
| 234 | #endif |
|---|
| 235 | |
|---|
| 236 | void ms_filter_process(MSFilter *f){ |
|---|
| 237 | ms_debug("Executing process of filter %s:%p",f->desc->name,f); |
|---|
| 238 | #ifdef DEBUG |
|---|
| 239 | long start,stop; |
|---|
| 240 | start = filter_get_cur_time(NULL); |
|---|
| 241 | #endif |
|---|
| 242 | f->desc->process(f); |
|---|
| 243 | #ifdef DEBUG |
|---|
| 244 | stop = filter_get_cur_time(NULL); |
|---|
| 245 | if(stop-start > 10) |
|---|
| 246 | { |
|---|
| 247 | ms_warning("%s take too much time:%ldms\n",f->desc->name,stop-start); |
|---|
| 248 | } |
|---|
| 249 | else |
|---|
| 250 | { |
|---|
| 251 | ms_debug("%s take:%ldms\n",f->desc->name,stop-start); |
|---|
| 252 | } |
|---|
| 253 | #endif |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | void ms_filter_preprocess(MSFilter *f, struct _MSTicker *t){ |
|---|
| 257 | f->last_tick=0; |
|---|
| 258 | f->ticker=t; |
|---|
| 259 | if (f->desc->preprocess!=NULL) |
|---|
| 260 | f->desc->preprocess(f); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | void ms_filter_postprocess(MSFilter *f){ |
|---|
| 264 | if (f->desc->postprocess!=NULL) |
|---|
| 265 | f->desc->postprocess(f); |
|---|
| 266 | f->ticker=NULL; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | bool_t ms_filter_inputs_have_data(MSFilter *f){ |
|---|
| 270 | int i; |
|---|
| 271 | for(i=0;i<f->desc->ninputs;i++){ |
|---|
| 272 | MSQueue *q=f->inputs[i]; |
|---|
| 273 | if (q!=NULL && q->q.q_mcount>0) return TRUE; |
|---|
| 274 | } |
|---|
| 275 | return FALSE; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | static void find_filters(MSList **filters, MSFilter *f ){ |
|---|
| 281 | int i,found; |
|---|
| 282 | MSQueue *link; |
|---|
| 283 | if (f==NULL) ms_fatal("Bad graph."); |
|---|
| 284 | /*ms_message("seeing %s, seen=%i",f->desc->name,f->seen);*/ |
|---|
| 285 | if (f->seen){ |
|---|
| 286 | return; |
|---|
| 287 | } |
|---|
| 288 | f->seen=TRUE; |
|---|
| 289 | *filters=ms_list_append(*filters,f); |
|---|
| 290 | /* go upstream */ |
|---|
| 291 | for(i=0;i<f->desc->ninputs;i++){ |
|---|
| 292 | link=f->inputs[i]; |
|---|
| 293 | if (link!=NULL) find_filters(filters,link->prev.filter); |
|---|
| 294 | } |
|---|
| 295 | /* go downstream */ |
|---|
| 296 | for(i=0,found=0;i<f->desc->noutputs;i++){ |
|---|
| 297 | link=f->outputs[i]; |
|---|
| 298 | if (link!=NULL) { |
|---|
| 299 | found++; |
|---|
| 300 | find_filters(filters,link->next.filter); |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | if (f->desc->noutputs>=1 && found==0){ |
|---|
| 304 | ms_fatal("Bad graph: filter %s has %i outputs, none is connected.",f->desc->name,f->desc->noutputs); |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | MSList * ms_filter_find_neighbours(MSFilter *me){ |
|---|
| 309 | MSList *l=NULL; |
|---|
| 310 | MSList *it; |
|---|
| 311 | find_filters(&l,me); |
|---|
| 312 | /*reset seen boolean for further lookups to succeed !*/ |
|---|
| 313 | for(it=l;it!=NULL;it=it->next){ |
|---|
| 314 | MSFilter *f=(MSFilter*)it->data; |
|---|
| 315 | f->seen=FALSE; |
|---|
| 316 | } |
|---|
| 317 | return l; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | void ms_connection_helper_start(MSConnectionHelper *h){ |
|---|
| 321 | h->last.filter=0; |
|---|
| 322 | h->last.pin=-1; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | int ms_connection_helper_link(MSConnectionHelper *h, MSFilter *f, int inpin, int outpin){ |
|---|
| 326 | int err=0; |
|---|
| 327 | if (h->last.filter==NULL){ |
|---|
| 328 | h->last.filter=f; |
|---|
| 329 | h->last.pin=outpin; |
|---|
| 330 | }else{ |
|---|
| 331 | err=ms_filter_link(h->last.filter,h->last.pin,f,inpin); |
|---|
| 332 | if (err==0){ |
|---|
| 333 | h->last.filter=f; |
|---|
| 334 | h->last.pin=outpin; |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | return err; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | int ms_connection_helper_unlink(MSConnectionHelper *h, MSFilter *f, int inpin, int outpin){ |
|---|
| 341 | int err=0; |
|---|
| 342 | if (h->last.filter==NULL){ |
|---|
| 343 | h->last.filter=f; |
|---|
| 344 | h->last.pin=outpin; |
|---|
| 345 | }else{ |
|---|
| 346 | err=ms_filter_unlink(h->last.filter,h->last.pin,f,inpin); |
|---|
| 347 | if (err==0){ |
|---|
| 348 | h->last.filter=f; |
|---|
| 349 | h->last.pin=outpin; |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| 352 | return err; |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | |
|---|