diff --git a/lib/upipe-modules/upipe_dup.c b/lib/upipe-modules/upipe_dup.c index 1618d31aa..c09510cea 100644 --- a/lib/upipe-modules/upipe_dup.c +++ b/lib/upipe-modules/upipe_dup.c @@ -216,9 +216,10 @@ static void upipe_dup_input(struct upipe *upipe, struct uref *uref, ulist_delete_foreach (&upipe_dup->outputs, uchain, uchain_tmp) { struct upipe_dup_output *upipe_dup_output = upipe_dup_output_from_uchain(uchain); - struct upipe *output = upipe_dup_output_to_upipe(upipe_dup_output); + struct upipe *sub = upipe_dup_output_to_upipe(upipe_dup_output); + /* the last sub takes the original when no direct output wants it */ if (ulist_is_last(&upipe_dup->outputs, uchain) && !output) { - upipe_dup_output_output(output, uref, upump_p); + upipe_dup_output_output(sub, uref, upump_p); uref = NULL; } else { struct uref *new_uref = uref_dup(uref); @@ -227,7 +228,7 @@ static void upipe_dup_input(struct upipe *upipe, struct uref *uref, upipe_throw_fatal(upipe, UBASE_ERR_ALLOC); return; } - upipe_dup_output_output(output, new_uref, upump_p); + upipe_dup_output_output(sub, new_uref, upump_p); } } diff --git a/lib/upipe-ts/upipe_ts_psi_split.c b/lib/upipe-ts/upipe_ts_psi_split.c index ab7619598..6bcf5e411 100644 --- a/lib/upipe-ts/upipe_ts_psi_split.c +++ b/lib/upipe-ts/upipe_ts_psi_split.c @@ -218,11 +218,13 @@ static void upipe_ts_psi_split_input(struct upipe *upipe, struct uref *uref, if (ubase_check(uref_ts_flow_get_psi_filter(output->flow_def, &filter, &mask, &size)) && ubase_check(uref_block_match(uref, filter, mask, size))) { - if (likely(uchain->next == NULL)) { + /* the list is circular: the last sub is followed by the head */ + if (likely(uchain->next == &upipe_ts_psi_split->subs)) { upipe_ts_psi_split_sub_output( upipe_ts_psi_split_sub_to_upipe(output), uref, upump_p); - uref = NULL; + /* the output may have added a sub after this one */ + return; } else { struct uref *new_uref = uref_dup(uref); if (likely(new_uref != NULL)) diff --git a/lib/upipe-ts/upipe_ts_split.c b/lib/upipe-ts/upipe_ts_split.c index 9c6266bb0..b23760e98 100644 --- a/lib/upipe-ts/upipe_ts_split.c +++ b/lib/upipe-ts/upipe_ts_split.c @@ -336,7 +336,8 @@ static void upipe_ts_split_input(struct upipe *upipe, struct uref *uref, ulist_delete_foreach(&upipe_ts_split->pids[pid].subs, uchain, uchain_tmp) { struct upipe_ts_split_sub *output = upipe_ts_split_sub_from_uchain_pid(uchain); - if (likely(uchain->next == NULL)) { + /* the list is circular: the last sub is followed by the head */ + if (likely(uchain->next == &upipe_ts_split->pids[pid].subs)) { upipe_ts_split_sub_output(upipe_ts_split_sub_to_upipe(output), uref, upump_p); uref = NULL; diff --git a/tests/upipe_dup_test.c b/tests/upipe_dup_test.c index 0cf7a5390..d3ea7520f 100644 --- a/tests/upipe_dup_test.c +++ b/tests/upipe_dup_test.c @@ -35,6 +35,9 @@ #define UPROBE_LOG_LEVEL UPROBE_LOG_DEBUG static int counter = 0; +/* the uref given to the dup, and how many sinks received it rather than a copy */ +static struct uref *original = NULL; +static int originals = 0; static int flow_foo_counter = 0; static int flow_bar_counter = 0; @@ -71,6 +74,8 @@ static void test_input(struct upipe *upipe, struct uref *uref, { assert(uref != NULL); counter++; + if (uref == original) + originals++; uref_free(uref); } @@ -154,8 +159,11 @@ int main(int argc, char *argv[]) uref = uref_alloc(uref_mgr); assert(uref != NULL); + original = uref; upipe_input(upipe_dup, uref, NULL); assert(counter == 1); + /* the only sub takes the original: nothing was copied */ + assert(originals == 1); counter = 0; struct upipe *upipe_dup_output1 = upipe_void_alloc_sub(upipe_dup, @@ -172,8 +180,11 @@ int main(int argc, char *argv[]) uref = uref_alloc(uref_mgr); assert(uref != NULL); + original = uref; upipe_input(upipe_dup, uref, NULL); assert(counter == 2); + /* one copy for the first sub, the original to the last */ + assert(originals == 2); assert(flow_foo_counter == 1); assert(flow_bar_counter == 2); diff --git a/tests/upipe_ts_psi_split_test.c b/tests/upipe_ts_psi_split_test.c index 44fef95f5..bfb600279 100644 --- a/tests/upipe_ts_psi_split_test.c +++ b/tests/upipe_ts_psi_split_test.c @@ -71,6 +71,33 @@ struct test { struct upipe upipe; }; +static struct uref_mgr *uref_mgr; +static struct uprobe *uprobe_stdio; +static struct upipe *upipe_ts_psi_split; +static struct upipe *upipe_ts_psi_split_output70 = NULL; + +/** adds a sub from inside the output of the last one */ +static void test_add_sub70(void) +{ + struct uref *flow_def = uref_block_flow_alloc_def(uref_mgr, "mpegtspsi."); + assert(flow_def != NULL); + uint8_t filter[PSI_HEADER_SIZE_SYNTAX1]; + memset(filter, 0, PSI_HEADER_SIZE_SYNTAX1); + filter[1] = 0x80; + uint8_t mask[PSI_HEADER_SIZE_SYNTAX1]; + memset(mask, 0, PSI_HEADER_SIZE_SYNTAX1); + mask[1] = 0x80; + psi_set_tableid(mask, 0xff); + psi_set_tableid(filter, 70); + ubase_assert(uref_ts_flow_set_psi_filter(flow_def, filter, mask, + PSI_HEADER_SIZE_SYNTAX1)); + upipe_ts_psi_split_output70 = upipe_flow_alloc_sub(upipe_ts_psi_split, + uprobe_pfx_alloc(uprobe_use(uprobe_stdio), UPROBE_LOG_LEVEL, + "ts psi split output 70"), flow_def); + assert(upipe_ts_psi_split_output70 != NULL); + uref_free(flow_def); +} + /** helper phony pipe */ static struct upipe *test_alloc(struct upipe_mgr *mgr, struct uprobe *uprobe, uint32_t signature, va_list args) @@ -107,6 +134,8 @@ static void test_input(struct upipe *upipe, struct uref *uref, } uref_block_unmap(uref, 0); uref_free(uref); + if (test->table_id == 69 && upipe_ts_psi_split_output70 == NULL) + test_add_sub70(); } /** helper phony pipe */ @@ -145,8 +174,7 @@ int main(int argc, char *argv[]) struct udict_mgr *udict_mgr = udict_inline_mgr_alloc(UDICT_POOL_DEPTH, umem_mgr, -1, -1); assert(udict_mgr != NULL); - struct uref_mgr *uref_mgr = uref_std_mgr_alloc(UREF_POOL_DEPTH, udict_mgr, - 0); + uref_mgr = uref_std_mgr_alloc(UREF_POOL_DEPTH, udict_mgr, 0); assert(uref_mgr != NULL); struct ubuf_mgr *ubuf_mgr = ubuf_block_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, @@ -154,8 +182,7 @@ int main(int argc, char *argv[]) assert(ubuf_mgr != NULL); struct uprobe uprobe; uprobe_init(&uprobe, catch, NULL); - struct uprobe *uprobe_stdio = uprobe_stdio_alloc(&uprobe, stdout, - UPROBE_LOG_LEVEL); + uprobe_stdio = uprobe_stdio_alloc(&uprobe, stdout, UPROBE_LOG_LEVEL); assert(uprobe_stdio != NULL); struct uref *uref; @@ -164,7 +191,7 @@ int main(int argc, char *argv[]) struct upipe_mgr *upipe_ts_psi_split_mgr = upipe_ts_psi_split_mgr_alloc(); assert(upipe_ts_psi_split_mgr != NULL); - struct upipe *upipe_ts_psi_split = upipe_void_alloc(upipe_ts_psi_split_mgr, + upipe_ts_psi_split = upipe_void_alloc(upipe_ts_psi_split_mgr, uprobe_pfx_alloc(uprobe_use(uprobe_stdio), UPROBE_LOG_LEVEL, "ts psi split")); assert(upipe_ts_psi_split != NULL); @@ -248,6 +275,8 @@ int main(int argc, char *argv[]) upipe_release(upipe_ts_psi_split_output68); upipe_release(upipe_ts_psi_split_output69); + assert(upipe_ts_psi_split_output70 != NULL); + upipe_release(upipe_ts_psi_split_output70); upipe_release(upipe_ts_psi_split); upipe_mgr_release(upipe_ts_psi_split_mgr); // nop