Skip to content

Commit

Permalink
add dash_representation_segment_url + dash_representation_get_initial…
Browse files Browse the repository at this point in the history
…ization
  • Loading branch information
ireader committed Jun 27, 2020
1 parent 10ba13f commit 79b03c8
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 190 deletions.
1 change: 1 addition & 0 deletions libdash/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ LOCAL_LDLIBS +=
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libmov/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libhls/include

LOCAL_SRC_FILES := $(wildcard src/*.c)
LOCAL_SRC_FILES += $(wildcard src/*.cpp)
Expand Down
3 changes: 2 additions & 1 deletion libdash/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ OUTFILE = libdash.a
#--------------------------------------------------------------------
INCLUDES = . \
./include \
../libmov/include
../libmov/include \
../libhls/include

#-------------------------------Source-------------------------------
#
Expand Down
34 changes: 21 additions & 13 deletions libdash/include/dash-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ int dash_mpd_parse(struct dash_mpd_t** mpd, const char* data, size_t bytes);

int dash_mpd_free(struct dash_mpd_t** mpd);

/// @return -1-live, >=0-duration(ms), other-error
int64_t dash_get_duration(const struct dash_mpd_t* mpd);

/// Period

/// location period by time
Expand All @@ -449,25 +452,30 @@ int dash_adaptation_set_media_type(const struct dash_adaptation_set_t* set);
/// @return >=0-representation index, -1-error
int dash_adaptation_set_best_representation(const struct dash_adaptation_set_t* set);

/// Representation

const struct dash_url_t* dash_representation_get_base_url(const struct dash_representation_t* representation);

const struct dash_segment_t* dash_representation_get_segment(const struct dash_representation_t* representation);
/// Get initialization segment url(MUST use with dash_representation_get_base_url)
/// @return >0-ok with url length, =0-don't have initialization segment, <0-error
int dash_representation_get_initialization(const struct dash_representation_t* representation, char* url, size_t size);

/// Find segment by start time
/// @param[in] time segment time, range: (previous segment end, segment start + duration)
/// @return -1-not found, >=0-segment item index(index is not the startnumber)
int dash_representation_find(const struct dash_representation_t* representation, int64_t time);

/// @return -1-segment template, >=0-segment count
int dash_segment_count(const struct dash_segment_t* segment);

int dash_segment_information(const struct dash_segment_t* segment, size_t index, int64_t* number, int64_t* start, int64_t* duration, const char** url, const char** range);

/// @return 0-ok, other-error
int dash_segment_template_replace(const struct dash_representation_t* representation, const char* url, int64_t number, int64_t start, char* ptr, size_t len);

/// @return 0-ok, <0-error
int dash_segment_timeline(const struct dash_segment_timeline_t* timeline, size_t index, int64_t* number, int64_t* start, int64_t* duration);
int dash_representation_find_segment(const struct dash_representation_t* representation, int64_t time);

/// @return <0-error, >=0-segment count(INT_MAX for segment template)
int dash_representation_segment_count(const struct dash_representation_t* representation);

/// Get segment url(MUST use with dash_representation_get_base_url)
/// @param[in] index segment index(!= start number)
/// @param[out] number start number of representation
/// @param[out] start start time of representation(MUST add period.start)
/// @param[out] duration segment duration, 0 if unknown
/// @param[out] range url range, NULL if don't have range
/// @return >=0-ok with url length, <0-error
int dash_representation_segment_url(const struct dash_representation_t* representation, int index, int64_t* number, int64_t* start, int64_t* duration, const char** range, char* url, size_t size);

#ifdef __cplusplus
}
Expand Down
8 changes: 4 additions & 4 deletions libdash/libdash.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;OS_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>.;include;../libmov/include;../../sdk/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>.;include;../libmov/include;../libhls/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -105,7 +105,7 @@
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_LIB;OS_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>.;include;../libmov/include;../../sdk/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>.;include;../libmov/include;../libhls/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -120,7 +120,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;OS_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>.;include;../libmov/include;../../sdk/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>.;include;../libmov/include;../libhls/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -137,7 +137,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_LIB;OS_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>.;include;../libmov/include;../../sdk/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>.;include;../libmov/include;../libhls/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
47 changes: 11 additions & 36 deletions libdash/src/dash-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#include "dash-parser.h"
#include "xs-datatype.h"
#include "hls-string.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "cstringext.h"

#ifndef offsetof
#define offsetof(s, m) (size_t)&(((s*)0)->m)
Expand Down Expand Up @@ -37,26 +37,6 @@
#define SEGMENT_URL_INCR 8
#define SEGMENT_URL_CAPACITY(count) ((count+SEGMENT_URL_INCR-1)/SEGMENT_URL_INCR*SEGMENT_URL_INCR)

enum
{
DASH_ROOT = 0x0001,
DASH_MPD = 0x0002,
DASH_PERIOD = 0x0004,
DASH_PRESELECTION = 0x0008,
DASH_ADAPTATION_SET = 0x0010,
DASH_REPRESENTATION = 0x0020,
DASH_SUBREPRESENTATION = 0x0040,

DASH_SEGMENT = 0x0100,
DASH_SEGMENT_TIMELINE = 0x0400,
DASH_PROGRAM_INFORMATION= 0x1000,
DASH_METRICS = 0x2000,
DASH_EVENT_STREAM = 0x4000,
DASH_CONTENT_COMPONENT = 0x8000,

#define DASH_ANY 0xFF
};

enum
{
DASH_TAG_ELEMENT,
Expand Down Expand Up @@ -89,11 +69,6 @@ struct dash_tag_t
struct dash_parser_t
{
size_t period_capacity;
//struct dash_subrepresentation_t* subrepresentation;
//struct dash_representation_t* representation;
//struct dash_adaptation_set_t* adaptation_set;
//struct dash_preselection_t* preselection;
//struct dash_period_t* period; // last segment has key
struct dash_mpd_t* mpd;
char* content;
void* tag; // dash tag object
Expand Down Expand Up @@ -189,12 +164,12 @@ static int dash_parse_attrs(const char* data, size_t bytes, struct dash_tag_attr
r = 0;
for (ptr = data; ptr && ptr < data + bytes && 0 == r; ptr = next)
{
n = strsplit(ptr, data + bytes, " \r\n", "\"", &next);
n = hls_strsplit(ptr, data + bytes, " \r\n", "\"", &next);

nn = strsplit(ptr, ptr + n, "=", "", &value);
name = strtrim(ptr, &nn, " \t\r\n", " \t\r\n"); // trim SP/HTAB
nn = hls_strsplit(ptr, ptr + n, "=", "", &value);
name = hls_strtrim(ptr, &nn, " \t\r\n", " \t\r\n"); // trim SP/HTAB
nv = ptr + n - value;
value = strtrim(value, &nv, " \t\r\n'\"", " \t\r\n'\""); // trim SP/HTAB/'/"
value = hls_strtrim(value, &nv, " \t\r\n'\"", " \t\r\n'\""); // trim SP/HTAB/'/"

for (i = 0; i < nattrs; i++)
{
Expand Down Expand Up @@ -244,7 +219,7 @@ static int dash_tag_mpd(struct dash_parser_t* parser, void* ptr, const char* att
DASH_TAG_ATTR_VALUE(attrs[3], ATTR_VALUE_TYPE_DATETIME, "availabilityStartTime", &mpd->availability_start_time);
DASH_TAG_ATTR_VALUE(attrs[4], ATTR_VALUE_TYPE_DATETIME, "availabilityEndTime", &mpd->availability_end_time);
DASH_TAG_ATTR_VALUE(attrs[5], ATTR_VALUE_TYPE_DATETIME, "publishTime", &mpd->publish_time);
DASH_TAG_ATTR_VALUE(attrs[6], ATTR_VALUE_TYPE_DURATION, "mediaPresentationDruation", &mpd->media_presentation_duration);
DASH_TAG_ATTR_VALUE(attrs[6], ATTR_VALUE_TYPE_DURATION, "mediaPresentationDuration", &mpd->media_presentation_duration);
DASH_TAG_ATTR_VALUE(attrs[7], ATTR_VALUE_TYPE_DURATION, "minimumUpdatePeriod", &mpd->minimum_update_period);
DASH_TAG_ATTR_VALUE(attrs[8], ATTR_VALUE_TYPE_DURATION, "minBufferTime", &mpd->min_buffer_time);
DASH_TAG_ATTR_VALUE(attrs[9], ATTR_VALUE_TYPE_DURATION, "timeShiftBufferDepath", &mpd->time_shift_buffer_depth);
Expand Down Expand Up @@ -1172,7 +1147,7 @@ static const char* dash_parser_tag(struct dash_tag_t* tag, const char* data, con
return end; // all done

tag->nlen = end - tag->ptr - 1;
tag->name = strtrim(tag->ptr + 1 /* skip '<' */, &tag->nlen, " \t\r\n", NULL); // trim SP/HTAB
tag->name = hls_strtrim(tag->ptr + 1 /* skip '<' */, &tag->nlen, " \t\r\n", NULL); // trim SP/HTAB
switch (*tag->name)
{
case '?':
Expand Down Expand Up @@ -1219,9 +1194,9 @@ static const char* dash_parser_tag(struct dash_tag_t* tag, const char* data, con
break;
}

tag->nlen = strsplit(tag->name, end, ">", "\'\"", &next);
tag->nlen = hls_strsplit(tag->name, end, ">", "\'\"", &next);
tag->end = tag->name + tag->nlen;
tag->name = strtrim(tag->name, &tag->nlen, " \t\r\n", " \t\r\n"); // trim SP/HTAB
tag->name = hls_strtrim(tag->name, &tag->nlen, " \t\r\n", " \t\r\n"); // trim SP/HTAB
if (tag->nlen > 0 && '/' == tag->name[tag->nlen - 1])
{
// line-break
Expand All @@ -1235,7 +1210,7 @@ static const char* dash_parser_tag(struct dash_tag_t* tag, const char* data, con
assert(tag->attr <= tag->name + tag->nlen);
tag->nattr = tag->nlen - (tag->attr - tag->name);
tag->nlen = tag->attr ? tag->attr - tag->name : tag->nlen;
tag->attr = strtrim(tag->attr, &tag->nattr, " \t\r\n", " \t\r\n"); // trim SP/HTAB
tag->attr = hls_strtrim(tag->attr, &tag->nattr, " \t\r\n", " \t\r\n"); // trim SP/HTAB

if (tag->nlen < 1)
{
Expand Down Expand Up @@ -1285,7 +1260,7 @@ static int dash_parser_input(struct dash_parser_t* parser, const char* data, siz
if (0 == (DASH_TAG_FLAG_LINBREAK & tag.flags))
{
ncontent = tag2.ptr - ptr;
parser->content = (char*)strtrim(ptr, &ncontent, " \t\r\n", " \t\r\n"); // trim SP/HTAB
parser->content = (char*)hls_strtrim(ptr, &ncontent, " \t\r\n", " \t\r\n"); // trim SP/HTAB
parser->content[ncontent] = '\0';
}
else
Expand Down
72 changes: 68 additions & 4 deletions libdash/src/dash-period.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,72 @@
#include "dash-parser.h"
#include "hls-string.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "cstringext.h"

int64_t dash_get_duration(const struct dash_mpd_t* mpd)
{
size_t i;
int64_t t, start;
const struct dash_period_t* period;

if (DASH_DYNAMIC == mpd->type)
return -1;

if (mpd->media_presentation_duration > 0)
return mpd->media_presentation_duration;

t = 0;
for (i = 0; i < mpd->period_count; i++)
{
period = &mpd->periods[i];
if (period->start > 0)
{
// a regular Period or an early terminated Period
start = period->start;
}
else if (i > 0 && mpd->periods[i - 1].duration > 0)
{
start = t;
}
else if (0 == i && DASH_STATIC == mpd->type)
{
start = 0;
}
else if ((0 == i || mpd->periods[i - 1].duration == 0) && DASH_DYNAMIC == mpd->type)
{
continue; // Early Available Period
}
else
{
start = t;
}

if (period->duration > 0)
{
t = start + period->duration;
}
else if (i + 1 < mpd->period_count)
{
assert(0 != mpd->periods[i + 1].start);
t = mpd->periods[i + 1].start;
}
else if (DASH_DYNAMIC == mpd->type)
{
// MPD@mediaPresentationDuration shall be present when neither
// the attribute MPD@minimumUpdatePeriod nor the Period@duration
// of the last Period are present.
assert(mpd->media_presentation_duration > 0 || mpd->minimum_update_period > 0);
t = mpd->media_presentation_duration > 0 ? mpd->media_presentation_duration : (start + mpd->minimum_update_period);
}
else
{
t = start; // ???
}
}

return t;
}

int dash_period_find(const struct dash_mpd_t* mpd, int64_t time)
{
Expand All @@ -18,7 +82,7 @@ int dash_period_find(const struct dash_mpd_t* mpd, int64_t time)
{
// 5.3.2.1 Overview (p27)
// 1. If the attribute @start is present in the Period, then the Period
// is a regular Period or an earlyterminated Period and the PeriodStart
// is a regular Period or an early terminated Period and the PeriodStart
// is equal to the value of this attribute.
// 2. If the @start attribute is absent, but the previous Period element
// contains a @duration attributethen this new Period is also a regular
Expand All @@ -39,7 +103,7 @@ int dash_period_find(const struct dash_mpd_t* mpd, int64_t time)
period = &mpd->periods[i];
if (period->start > 0)
{
// a regular Period or an earlyterminated Period
// a regular Period or an early terminated Period
start = period->start;
}
else if (i > 0 && mpd->periods[i - 1].duration > 0)
Expand Down Expand Up @@ -82,7 +146,7 @@ int dash_period_find(const struct dash_mpd_t* mpd, int64_t time)
}

if (time < t)
return period;
return i;
}

return -1; // not found
Expand Down
Loading

0 comments on commit 79b03c8

Please sign in to comment.