Skip to content

Commit

Permalink
upipe_m3u_reader: add variable definitions parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nto committed Mar 12, 2024
1 parent 78da2b4 commit 5532dd0
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/upipe/uref_m3u.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ extern "C" {

UREF_ATTR_STRING(m3u, uri, "m3u.uri", uri)

UREF_ATTR_STRING(m3u_variable, name, "m3u.variable.name", variable name);
UREF_ATTR_STRING(m3u_variable, value, "m3u.variable.value", variable value);

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 3 additions & 1 deletion lib/upipe-hls/upipe_hls_playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,9 @@ static void upipe_hls_playlist_input(struct upipe *upipe,
upipe_hls_playlist->reloading = true;
}

if (unlikely(ubase_check(uref_m3u_playlist_daterange_get_id(uref, NULL)))) {
if (unlikely(
ubase_check(uref_m3u_playlist_daterange_get_id(uref, NULL)) ||
ubase_check(uref_m3u_variable_get_name(uref, NULL)))) {
uref_free(uref);
return;
}
Expand Down
51 changes: 51 additions & 0 deletions lib/upipe-modules/upipe_m3u_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,56 @@ static int upipe_m3u_reader_gap(struct upipe *upipe,
return UBASE_ERR_NONE;
}

/** @internal @This checks and parses a "#EXT-X-DEFINE" tag.
*
* @param upipe description structure of the pipe
* @param flow_def the current flow definition
* @param line the trailing characters of the line
* @return an error code
*/
static int upipe_m3u_reader_define(struct upipe *upipe,
struct uref *flow_def,
const char *line)
{
struct upipe_m3u_reader *upipe_m3u_reader =
upipe_m3u_reader_from_upipe(upipe);

UBASE_RETURN(uref_flow_match_def(flow_def, M3U_FLOW_DEF));

struct uref *item = uref_sibling_alloc_control(flow_def);
if (unlikely(item == NULL)) {
upipe_throw_fatal(upipe, UBASE_ERR_ALLOC);
return UBASE_ERR_ALLOC;
}

const char *iterator = line;
struct ustring name, value;
while (ubase_check(attribute_iterate(&iterator, &name, &value)) &&
iterator != NULL) {
char value_str[value.len + 1];
int err = ustring_cpy(value, value_str, sizeof (value_str));
if (unlikely(!ubase_check(err))) {
upipe_err_va(upipe, "fail to copy ustring %.*s",
(int)value.len, value.at);
continue;
}

if (!ustring_cmp_str(name, "NAME")) {
err = uref_m3u_variable_set_name(item, value_str);
if (unlikely(!ubase_check(err)))
upipe_err_va(upipe, "fail to set variable name to %s", value_str);
}
else if (!ustring_cmp_str(name, "VALUE")) {
err = uref_m3u_variable_set_value(item, value_str);
if (unlikely(!ubase_check(err)))
upipe_err_va(upipe, "fail to set variable value to %s", value_str);
}
}

ulist_add(&upipe_m3u_reader->items, uref_to_uchain(item));
return UBASE_ERR_NONE;
}

/** @internal @This checks and parses a "#EXT-X-DISCONTINUITY-SEQUENCE" tag.
*
* @param upipe description structure of the pipe
Expand Down Expand Up @@ -1230,6 +1280,7 @@ static int upipe_m3u_reader_process_line(struct upipe *upipe,
{ "#EXT-X-DISCONTINUITY-SEQUENCE:", upipe_m3u_reader_discontinuity_sequence },
{ "#EXT-X-DISCONTINUITY", upipe_m3u_reader_discontinuity },
{ "#EXT-X-GAP", upipe_m3u_reader_gap },
{ "#EXT-X-DEFINE:", upipe_m3u_reader_define },
};

size_t block_size;
Expand Down
7 changes: 7 additions & 0 deletions tests/upipe_m3u_reader_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ static int catch_uref(struct uprobe *uprobe,
if (ubase_check(uref_m3u_master_get_media_autoselect(uref)))
printf("master media_autoselect\n");

const char *str;
if (ubase_check(uref_m3u_variable_get_name(uref, &str))) {
printf("variable name: %s\n", str);
ubase_assert(uref_m3u_variable_get_value(uref, &str));
printf("variable value: %s\n", str);
}

return UBASE_ERR_NONE;
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/upipe_m3u_reader_test_files/10.m3u
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#EXT-X-MEDIA-SEQUENCE:2680
#EXT-X-DISCONTINUITY-SEQUENCE:42
#EXT-X-PROGRAM-DATE-TIME:2023-10-11T13:13:20.635Z
#EXT-X-DEFINE:NAME="abc",VALUE="def"

#EXTINF:6.000,
https://priv.example.com/fileSequence2680.ts
Expand Down
2 changes: 2 additions & 0 deletions tests/upipe_m3u_reader_test_files/10.m3u.logs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ version: 3
playlist target duration: 162000000
playlist media sequence: 2680
playlist discontinuity sequence: 42
variable name: abc
variable value: def
uri: https://priv.example.com/fileSequence2680.ts
playlist sequence duration: 162000000
playlist program date time: 45819615617145000
Expand Down

0 comments on commit 5532dd0

Please sign in to comment.