void gf_filter_remove_task(GF_FSTask *task)
{
	s32 res;
	GF_Filter *f = task->filter;
	u32 count = gf_fq_count(f->tasks);

	//do not destroy filters if tasks for this filter are pending or some ref packets are still present
	if (f->out_pid_connection_pending || f->detach_pid_tasks_pending || f->nb_ref_packets) {
		task->requeue_request = GF_TRUE;
		return;
	}

	assert(f->finalized);

	if (count!=1) {
		task->requeue_request = GF_TRUE;
		task->can_swap = GF_TRUE;
#ifndef GPAC_DISABLE_LOG
		if (gf_log_tool_level_on(GF_LOG_FILTER, GF_LOG_DEBUG) ) {
			gf_fq_enum(f->tasks, task_postponed_log, NULL);
		}
#endif //GPAC_DISABLE_LOG
		return;
	}
	GF_LOG(GF_LOG_INFO, GF_LOG_FILTER, ("Filter %s destruction task\n", f->name));

	//avoid destruction of the task
	gf_fq_pop(f->tasks);

	if (f->freg->finalize) {
		FSESS_CHECK_THREAD(f)
		f->freg->finalize(f);
	}

	gf_mx_p(f->session->filters_mx);

	res = gf_list_del_item(f->session->filters, f);
	if (res<0) {
		GF_LOG(GF_LOG_WARNING, GF_LOG_FILTER, ("Filter %s destruction task on already removed filter\n", f->name));
	}

	gf_mx_v(f->session->filters_mx);

	gf_mx_p(f->tasks_mx);
	//detach all input pids
	while (gf_list_count(f->input_pids)) {
		GF_FilterPidInst *pidinst = gf_list_pop_back(f->input_pids);
		pidinst->filter = NULL;
	}
	gf_mx_v(f->tasks_mx);

	gf_filter_del(f);
	task->filter = NULL;
	task->requeue_request = GF_FALSE;
}
void gf_filter_remove_task(GF_FSTask *task)
{
	s32 res;
	GF_Filter *f = task->filter;
	u32 count = gf_fq_count(f->tasks);

	 
	if (f->out_pid_connection_pending || f->detach_pid_tasks_pending || f->nb_ref_packets) {
		task->requeue_request = GF_TRUE;
		return;
	}

	assert(f->finalized);

	if (count!=1) {
		task->requeue_request = GF_TRUE;
		task->can_swap = GF_TRUE;
#ifndef GPAC_DISABLE_LOG
		if (gf_log_tool_level_on(GF_LOG_FILTER, GF_LOG_DEBUG) ) {
			gf_fq_enum(f->tasks, task_postponed_log, NULL);
		}
#endif  
		return;
	}
	GF_LOG(GF_LOG_INFO, GF_LOG_FILTER, ("Filter %s destruction task\n", f->name));

	 
	gf_fq_pop(f->tasks);

	if (f->freg->finalize) {
		FSESS_CHECK_THREAD(f)
		f->freg->finalize(f);
	}

	gf_mx_p(f->session->filters_mx);

	res = gf_list_del_item(f->session->filters, f);
	if (res<0) {
		GF_LOG(GF_LOG_WARNING, GF_LOG_FILTER, ("Filter %s destruction task on already removed filter\n", f->name));
	}

	gf_mx_v(f->session->filters_mx);

	gf_mx_p(f->tasks_mx);
	 
	while (gf_list_count(f->input_pids)) {
		GF_FilterPidInst *pidinst = gf_list_pop_back(f->input_pids);
		pidinst->filter = NULL;
	}
	gf_mx_v(f->tasks_mx);

	gf_filter_del(f);
	task->filter = NULL;
	task->requeue_request = GF_FALSE;
}