| 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) 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 (desc->ninputs==0 && desc->noutputs==0) |
|---|
| 87 | ms_fatal("A filter cannot have no inputs and outputs"); |
|---|
| 88 | if (obj->desc->init!=NULL) |
|---|
| 89 | obj->desc->init(obj); |
|---|
| 90 | return obj; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | MSFilter *ms_filter_new(MSFilterId id){ |
|---|
| 94 | MSList *elem; |
|---|
| 95 | if (id==MS_FILTER_PLUGIN_ID){ |
|---|
| 96 | ms_warning("cannot create plugin filters with ms_filter_new_from_id()"); |
|---|
| 97 | return NULL; |
|---|
| 98 | } |
|---|
| 99 | for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){ |
|---|
| 100 | MSFilterDesc *desc=(MSFilterDesc*)elem->data; |
|---|
| 101 | if (desc->id==id){ |
|---|
| 102 | return ms_filter_new_from_desc(desc); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | ms_error("No such filter with id %i",id); |
|---|
| 106 | return NULL; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | MSFilter *ms_filter_new_from_name(const char *filter_name){ |
|---|
| 110 | MSList *elem; |
|---|
| 111 | for (elem=desc_list;elem!=NULL;elem=ms_list_next(elem)){ |
|---|
| 112 | MSFilterDesc *desc=(MSFilterDesc*)elem->data; |
|---|
| 113 | if (strcmp(desc->name,filter_name)==0){ |
|---|
| 114 | return ms_filter_new_from_desc(desc); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | ms_error("No such filter with name %s",filter_name); |
|---|
| 118 | return NULL; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | MSFilterId ms_filter_get_id(MSFilter *f){ |
|---|
| 123 | return f->desc->id; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | int ms_filter_link(MSFilter *f1, int pin1, MSFilter *f2, int pin2){ |
|---|
| 127 | MSQueue *q; |
|---|
| 128 | ms_return_val_if_fail(pin1<f1->desc->noutputs, -1); |
|---|
| 129 | ms_return_val_if_fail(pin2<f2->desc->ninputs, -1); |
|---|
| 130 | ms_return_val_if_fail(f1->outputs[pin1]==NULL,-1); |
|---|
| 131 | ms_return_val_if_fail(f2->inputs[pin2]==NULL,-1); |
|---|
| 132 | q=ms_queue_new(f1,pin1,f2,pin2); |
|---|
| 133 | f1->outputs[pin1]=q; |
|---|
| 134 | f2->inputs[pin2]=q; |
|---|
| 135 | ms_message("ms_filter_link: %s:%p,%i-->%s:%p,%i",f1->desc->name,f1,pin1,f2->desc->name,f2,pin2); |
|---|
| 136 | return 0; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | int ms_filter_unlink(MSFilter *f1, int pin1, MSFilter *f2, int pin2){ |
|---|
| 140 | MSQueue *q; |
|---|
| 141 | ms_return_val_if_fail(f1, -1); |
|---|
| 142 | ms_return_val_if_fail(f2, -1); |
|---|
| 143 | ms_return_val_if_fail(pin1<f1->desc->noutputs, -1); |
|---|
| 144 | ms_return_val_if_fail(pin2<f2->desc->ninputs, -1); |
|---|
| 145 | ms_return_val_if_fail(f1->outputs[pin1]!=NULL,-1); |
|---|
| 146 | ms_return_val_if_fail(f2->inputs[pin2]!=NULL,-1); |
|---|
| 147 | ms_return_val_if_fail(f1->outputs[pin1]==f2->inputs[pin2],-1); |
|---|
| 148 | q=f1->outputs[pin1]; |
|---|
| 149 | f1->outputs[pin1]=f2->inputs[pin2]=0; |
|---|
| 150 | ms_queue_destroy(q); |
|---|
| 151 | ms_message("ms_filter_unlink: %s:%p,%i-->%s:%p,%i",f1->desc->name,f1,pin1,f2->desc->name,f2,pin2); |
|---|
| 152 | return 0; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | #define MS_FILTER_METHOD_GET_FID(id) (((id)>>16) & 0xFFFF) |
|---|
| 156 | |
|---|
| 157 | int ms_filter_call_method(MSFilter *f, unsigned int id, void *arg){ |
|---|
| 158 | MSFilterMethod *methods=f->desc->methods; |
|---|
| 159 | int i; |
|---|
| 160 | unsigned int magic=MS_FILTER_METHOD_GET_FID(id); |
|---|
| 161 | if (magic!=MS_FILTER_BASE_ID && magic!=f->desc->id) { |
|---|
| 162 | ms_fatal("Bad method definition in filter %s",f->desc->name); |
|---|
| 163 | return -1; |
|---|
| 164 | } |
|---|
| 165 | for(i=0;methods!=NULL && methods[i].method!=NULL; i++){ |
|---|
| 166 | unsigned int mm=MS_FILTER_METHOD_GET_FID(methods[i].id); |
|---|
| 167 | if (mm!=f->desc->id && mm!=MS_FILTER_BASE_ID) { |
|---|
| 168 | ms_fatal("MSFilter method mismatch: bad call."); |
|---|
| 169 | return -1; |
|---|
| 170 | } |
|---|
| 171 | if (methods[i].id==id){ |
|---|
| 172 | return methods[i].method(f,arg); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | if (magic!=MS_FILTER_BASE_ID) ms_error("no such method on filter %s",f->desc->name); |
|---|
| 176 | return -1; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | int ms_filter_call_method_noarg(MSFilter *f, unsigned int id){ |
|---|
| 180 | return ms_filter_call_method(f,id,NULL); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | void ms_filter_set_notify_callback(MSFilter *f, MSFilterNotifyFunc fn, void *ud){ |
|---|
| 184 | f->notify=fn; |
|---|
| 185 | f->notify_ud=ud; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | void ms_filter_destroy(MSFilter *f){ |
|---|
| 189 | if (f->desc->uninit!=NULL) |
|---|
| 190 | f->desc->uninit(f); |
|---|
| 191 | if (f->inputs!=NULL) ms_free(f->inputs); |
|---|
| 192 | if (f->outputs!=NULL) ms_free(f->outputs); |
|---|
| 193 | ms_mutex_destroy(&f->lock); |
|---|
| 194 | ms_free(f); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | void ms_filter_process(MSFilter *f){ |
|---|
| 199 | ms_debug("Executing process of filter %s:%p",f->desc->name,f); |
|---|
| 200 | f->desc->process(f); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | void ms_filter_preprocess(MSFilter *f, struct _MSTicker *t){ |
|---|
| 204 | f->seen=FALSE; |
|---|
| 205 | f->last_tick=0; |
|---|
| 206 | f->ticker=t; |
|---|
| 207 | if (f->desc->preprocess!=NULL) |
|---|
| 208 | f->desc->preprocess(f); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | void ms_filter_postprocess(MSFilter *f){ |
|---|
| 212 | if (f->desc->postprocess!=NULL) |
|---|
| 213 | f->desc->postprocess(f); |
|---|
| 214 | f->seen=FALSE; |
|---|
| 215 | f->ticker=NULL; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | bool_t ms_filter_inputs_have_data(MSFilter *f){ |
|---|
| 219 | int i; |
|---|
| 220 | for(i=0;i<f->desc->ninputs;i++){ |
|---|
| 221 | MSQueue *q=f->inputs[i]; |
|---|
| 222 | if (q!=NULL && q->q.q_mcount>0) return TRUE; |
|---|
| 223 | } |
|---|
| 224 | return FALSE; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | void ms_filter_notify(MSFilter *f, unsigned int id, void *arg){ |
|---|
| 228 | if (f->notify!=NULL) |
|---|
| 229 | f->notify(f->notify_ud,id,arg); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | void ms_filter_notify_no_arg(MSFilter *f, unsigned int id){ |
|---|
| 233 | if (f->notify!=NULL) |
|---|
| 234 | f->notify(f->notify_ud,id,NULL); |
|---|
| 235 | } |
|---|