Files
BPI-CM9-Linux/aml-comp/multimedia/ffmpeg_ctc/libavformat/dashdec.c
T
2026-08-24 10:03:24 +08:00

2965 lines
102 KiB
C

/*
* Dynamic Adaptive Streaming over HTTP demux
* Copyright (c) 2017 [email protected] based on HLS demux
* Copyright (c) 2017 Steven Liu
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <libxml/parser.h>
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include "libavutil/parseutils.h"
#include "internal.h"
#include "avio_internal.h"
#include "dash.h"
#include "amconfigutils.h"
#include "libavutil/encryption_info.h"
#include "libavutil/base64.h"
#define INITIAL_BUFFER_SIZE 32768
struct fragment {
int64_t url_offset;
int64_t size;
char *url;
};
/*
* reference to : ISO_IEC_23009-1-DASH-2012
* Section: 5.3.9.6.2
* Table: Table 17 — Semantics of SegmentTimeline element
* */
struct timeline {
/* starttime: Element or Attribute Name
* specifies the MPD start time, in @timescale units,
* the first Segment in the series starts relative to the beginning of the Period.
* The value of this attribute must be equal to or greater than the sum of the previous S
* element earliest presentation time and the sum of the contiguous Segment durations.
* If the value of the attribute is greater than what is expressed by the previous S element,
* it expresses discontinuities in the timeline.
* If not present then the value shall be assumed to be zero for the first S element
* and for the subsequent S elements, the value shall be assumed to be the sum of
* the previous S element's earliest presentation time and contiguous duration
* (i.e. previous S@starttime + @duration * (@repeat + 1)).
* */
int64_t starttime;
/* repeat: Element or Attribute Name
* specifies the repeat count of the number of following contiguous Segments with
* the same duration expressed by the value of @duration. This value is zero-based
* (e.g. a value of three means four Segments in the contiguous series).
* */
int64_t repeat;
/* duration: Element or Attribute Name
* specifies the Segment duration, in units of the value of the @timescale.
* */
int64_t duration;
};
typedef struct {
struct fragment *index_range;
uint8_t *index_range_buf;
uint32_t index_range_size;
} DashSegBaseData;
typedef enum {
MIME_TYPE_NONE = -1,
MIME_TYPE_MP4 = 0,
MIME_TYPE_MP2T = 1,
MIME_TYPE_VTT = 2,
MIME_TYPE_WEBM = 3,
} FILE_MIME_TYPE;
typedef struct {
uint8_t is_valid;
char* key_id;
char* pssh;
char* system_id;
} ContentProtection;
/*
* Each playlist has its own demuxer. If it is currently active,
* it has an opened AVIOContext too, and potentially an AVPacket
* containing the next packet from this stream.
*/
struct representation {
char *url_template;
AVIOContext pb;
AVIOContext *input;
AVFormatContext *parent;
AVFormatContext *ctx;
AVPacket pkt;
int rep_idx;
int rep_count;
int stream_index;
enum AVMediaType type;
char id[20];
int bandwidth;
AVRational framerate;
AVStream *assoc_stream; /* demuxer stream associated with this representation */
int n_fragments;
struct fragment **fragments; /* VOD list of fragment for profile */
DashSegBaseData segBase;
int n_timelines;
struct timeline **timelines;
int64_t first_seq_no;
int64_t last_seq_no;
int64_t start_number; /* used in case when we have dynamic list of segment to know which segments are new one*/
int64_t fragment_duration;
int64_t fragment_timescale;
int64_t presentation_timeoffset;
int64_t cur_seq_no;
int64_t cur_seg_offset;
int64_t cur_seg_size;
struct fragment *cur_seg;
/* Currently active Media Initialization Section */
struct fragment *init_section;
uint8_t *init_sec_buf;
uint32_t init_sec_buf_size;
uint32_t init_sec_data_len;
uint32_t init_sec_buf_read_offset;
int64_t cur_timestamp;
int is_restart_needed;
FILE_MIME_TYPE file_mime_type;
ContentProtection content_protection;
};
typedef enum {
START_MODE_FIX_HIGH = 0,
START_MODE_FIX_LOW,
START_MODE_AUTO_HIGH,
START_MODE_AUTO_LOW,
} StartModeType;
typedef enum {
DASH_BW_Begin,
DASH_BW_Normal,
DASH_BW_Underload,
DASH_BW_FIX,
} DashBandwidthState;
typedef struct {
DashBandwidthState state;
int playable_msec;
int playable_threshold_download; // ms
int target_duration;
int increase_cnt;
} DashBandWidth;
typedef struct DASHContext {
const AVClass *class;
char *base_url;
int n_videos;
struct representation **videos;
int n_audios;
struct representation **audios;
/* MediaPresentationDescription Attribute */
uint64_t media_presentation_duration;
uint64_t suggested_presentation_delay;
uint64_t availability_start_time;
uint64_t publish_time;
uint64_t minimum_update_period;
uint64_t time_shift_buffer_depth;
uint64_t min_buffer_time;
/* Period Attribute */
uint64_t period_duration;
uint64_t period_start;
int is_live;
AVIOInterruptCB *interrupt_callback;
char *user_agent; ///< holds HTTP user agent set as an AVOption to the HTTP protocol context
char *cookies; ///< holds HTTP cookie values set in either the initial response or as an AVOption to the HTTP protocol context
char *headers; ///< holds HTTP headers set as an AVOption to the HTTP protocol context
char *allowed_extensions;
AVDictionary *avio_opts;
int max_url_size;
StartModeType quality; //0: high; 1: low; 2: Auto high begin; 3: Auto low begin;
int cur_video_idx;
int parse_seg_base;
int64_t seek_seq_no;
int playable_msec;
int bw_adjust_target;
DashBandWidth band_width;
int is_mp2t;
int is_mp4;
int enable_raw_data;
int first_drop_packet_after_seek;
} DASHContext;
static int ishttp(char *url)
{
const char *proto_name = avio_find_protocol_name(url);
return av_strstart(proto_name, "http", NULL);
}
static int aligned(int val)
{
return ((val + 0x3F) >> 6) << 6;
}
static uint64_t get_current_time_in_sec(void)
{
return av_gettime() / 1000000;
}
static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
{
struct tm timeinfo;
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int ret = 0;
float second = 0.0;
/* ISO-8601 date parser */
if (!datetime)
return 0;
ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
/* year, month, day, hour, minute, second 6 arguments */
if (ret != 6) {
av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
}
timeinfo.tm_year = year - 1900;
timeinfo.tm_mon = month - 1;
timeinfo.tm_mday = day;
timeinfo.tm_hour = hour;
timeinfo.tm_min = minute;
timeinfo.tm_sec = (int)second;
return av_timegm(&timeinfo);
}
static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
{
/* ISO-8601 duration parser */
uint32_t days = 0;
uint32_t hours = 0;
uint32_t mins = 0;
uint32_t secs = 0;
int size = 0;
float value = 0;
char type = '\0';
const char *ptr = duration;
while (*ptr) {
if (*ptr == 'P' || *ptr == 'T') {
ptr++;
continue;
}
if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
return 0; /* parser error */
}
switch (type) {
case 'D':
days = (uint32_t)value;
break;
case 'H':
hours = (uint32_t)value;
break;
case 'M':
mins = (uint32_t)value;
break;
case 'S':
secs = (uint32_t)value;
break;
default:
// handle invalid type
break;
}
ptr += size;
}
return ((days * 24 + hours) * 60 + mins) * 60 + secs;
}
static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
{
int64_t start_time = 0;
int64_t i = 0;
int64_t j = 0;
int64_t num = 0;
if (pls->n_timelines) {
for (i = 0; i < pls->n_timelines; i++) {
if (pls->timelines[i]->starttime > 0) {
start_time = pls->timelines[i]->starttime;
}
if (num == cur_seq_no)
goto finish;
start_time += pls->timelines[i]->duration;
for (j = 0; j < pls->timelines[i]->repeat; j++) {
num++;
if (num == cur_seq_no)
goto finish;
start_time += pls->timelines[i]->duration;
}
num++;
}
}
finish:
return start_time;
}
static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
{
int64_t i = 0;
int64_t j = 0;
int64_t num = 0;
int64_t start_time = 0;
for (i = 0; i < pls->n_timelines; i++) {
if (pls->timelines[i]->starttime > 0) {
start_time = pls->timelines[i]->starttime;
}
if (start_time > cur_time)
goto finish;
start_time += pls->timelines[i]->duration;
for (j = 0; j < pls->timelines[i]->repeat; j++) {
num++;
if (start_time > cur_time)
goto finish;
start_time += pls->timelines[i]->duration;
}
num++;
}
return -1;
finish:
return num;
}
static void free_fragment(struct fragment **seg)
{
if (!(*seg)) {
return;
}
av_freep(&(*seg)->url);
av_freep(seg);
}
static void free_fragment_list(struct representation *pls)
{
int i;
for (i = 0; i < pls->n_fragments; i++) {
free_fragment(&pls->fragments[i]);
}
av_freep(&pls->fragments);
pls->n_fragments = 0;
}
static void free_timelines_list(struct representation *pls)
{
int i;
for (i = 0; i < pls->n_timelines; i++) {
av_freep(&pls->timelines[i]);
}
av_freep(&pls->timelines);
pls->n_timelines = 0;
}
static void free_representation(struct representation *pls)
{
free_fragment_list(pls);
free_timelines_list(pls);
free_fragment(&pls->cur_seg);
free_fragment(&pls->segBase.index_range);
av_freep(&pls->segBase.index_range_buf);
pls->segBase.index_range_size = 0;
free_fragment(&pls->init_section);
av_freep(&pls->init_sec_buf);
av_freep(&pls->pb.buffer);
if (pls->input)
ff_format_io_close(pls->parent, &pls->input);
if (pls->ctx) {
pls->ctx->pb = NULL;
avformat_close_input(&pls->ctx);
}
av_freep(&pls->url_template);
av_freep(&pls);
}
static void free_video_list(DASHContext *c)
{
int i;
for (i = 0; i < c->n_videos; i++) {
struct representation *pls = c->videos[i];
free_representation(pls);
}
av_freep(&c->videos);
c->n_videos = 0;
}
static void free_audio_list(DASHContext *c)
{
int i;
for (i = 0; i < c->n_audios; i++) {
struct representation *pls = c->audios[i];
free_representation(pls);
}
av_freep(&c->audios);
c->n_audios = 0;
}
static void set_httpheader_options(DASHContext *c, AVDictionary **opts)
{
// broker prior HTTP options that should be consistent across requests
av_dict_set(opts, "user-agent", c->user_agent, 0);
av_dict_set(opts, "cookies", c->cookies, 0);
av_dict_set(opts, "headers", c->headers, 0);
if (c->is_live) {
av_dict_set(opts, "seekable", "0", 0);
}
}
static void update_options(char **dest, const char *name, void *src)
{
av_freep(dest);
av_opt_get(src, name, AV_OPT_SEARCH_CHILDREN, (uint8_t**)dest);
if (*dest)
av_freep(dest);
}
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
AVDictionary *opts, AVDictionary *opts2, int *is_http)
{
DASHContext *c = s->priv_data;
AVDictionary *tmp = NULL;
const char *proto_name = NULL;
int ret;
av_dict_copy(&tmp, opts, 0);
av_dict_copy(&tmp, opts2, 0);
if (av_strstart(url, "crypto", NULL)) {
if (url[6] == '+' || url[6] == ':')
proto_name = avio_find_protocol_name(url + 7);
}
if (!proto_name)
proto_name = avio_find_protocol_name(url);
if (!proto_name)
return AVERROR_INVALIDDATA;
// only http(s) & file are allowed
if (av_strstart(proto_name, "file", NULL)) {
if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
av_log(s, AV_LOG_ERROR,
"Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
"If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
url);
return AVERROR_INVALIDDATA;
}
} else if (av_strstart(proto_name, "http", NULL)) {
;
} else
return AVERROR_INVALIDDATA;
if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
;
else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
;
else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
return AVERROR_INVALIDDATA;
av_freep(pb);
ret = amlio_open_whitelist(pb, url, AVIO_FLAG_READ, &s->interrupt_callback, &tmp, NULL, NULL); // use aml http
if (ret >= 0) {
// update cookies on http response with setcookies.
char *new_cookies = NULL;
if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
if (new_cookies) {
av_free(c->cookies);
c->cookies = new_cookies;
}
av_dict_set(&opts, "cookies", c->cookies, 0);
}
av_dict_free(&tmp);
if (is_http)
*is_http = av_strstart(proto_name, "http", NULL);
return ret;
}
static char *get_content_url(xmlNodePtr *baseurl_nodes,
int n_baseurl_nodes,
int max_url_size,
char *rep_id_val,
char *rep_bandwidth_val,
char *val)
{
int i;
char *text;
char *url = NULL;
char *tmp_str = av_mallocz(max_url_size);
char *tmp_str_2 = av_mallocz(max_url_size);
if (!tmp_str || !tmp_str_2) {
return NULL;
}
for (i = 0; i < n_baseurl_nodes; ++i) {
if (baseurl_nodes[i] &&
baseurl_nodes[i]->children &&
baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
text = xmlNodeGetContent(baseurl_nodes[i]->children);
if (text) {
memset(tmp_str, 0, max_url_size);
memset(tmp_str_2, 0, max_url_size);
ff_make_absolute_url(tmp_str_2, max_url_size, tmp_str, text);
av_strlcpy(tmp_str, tmp_str_2, max_url_size);
xmlFree(text);
}
}
}
if (val)
av_strlcat(tmp_str, (const char*)val, max_url_size);
if (rep_id_val) {
url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
if (!url) {
goto end;
}
av_strlcpy(tmp_str, url, max_url_size);
}
if (rep_bandwidth_val && tmp_str[0] != '\0') {
// free any previously assigned url before reassigning
av_free(url);
url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
if (!url) {
goto end;
}
}
end:
av_free(tmp_str);
av_free(tmp_str_2);
return url;
}
static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
{
int i;
char *val;
for (i = 0; i < n_nodes; ++i) {
if (nodes[i]) {
val = xmlGetProp(nodes[i], attrname);
if (val)
return val;
}
}
return NULL;
}
static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
{
xmlNodePtr node = rootnode;
if (!node) {
return NULL;
}
node = xmlFirstElementChild(node);
while (node) {
if (!av_strcasecmp(node->name, nodename)) {
return node;
}
node = xmlNextElementSibling(node);
}
return NULL;
}
static FILE_MIME_TYPE get_mime_type(xmlNodePtr node)
{
FILE_MIME_TYPE type = MIME_TYPE_NONE;
if (node == NULL) {
return type;
}
char *val = xmlGetProp(node, "mimeType");
if (val == NULL) {
return type;
}
if (av_stristr((const char *)val, "mp4")) {
type = MIME_TYPE_MP4;
} else if (av_stristr((const char *)val, "mp2t")) {
type = MIME_TYPE_MP2T;
} else if (av_stristr((const char *)val, "vtt")) {
type = MIME_TYPE_VTT;
} else if (av_stristr((const char *)val, "webm")) {
type = MIME_TYPE_WEBM;
}
xmlFree(val);
return type;
}
static FILE_MIME_TYPE get_representation_file_type(xmlNodePtr representation_node, xmlNodePtr content_component_node,
xmlNodePtr adaptionset_node)
{
FILE_MIME_TYPE type = MIME_TYPE_NONE;
if (type == MIME_TYPE_NONE) {
type = get_mime_type(representation_node);
}
if (type == MIME_TYPE_NONE) {
type = get_mime_type(content_component_node);
}
if (type == MIME_TYPE_NONE) {
type = get_mime_type(adaptionset_node);
}
if (type == MIME_TYPE_NONE) {
type = MIME_TYPE_MP4;
av_log(0, AV_LOG_WARNING, "get file mime type fail, so set it as default:MIME_TYPE_MP4\n");
}
return type;
}
static enum AVMediaType get_content_type(xmlNodePtr node)
{
enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
int i = 0;
const char *attr;
char *val = NULL;
if (node) {
for (i = 0; i < 2; i++) {
attr = i ? "mimeType" : "contentType";
val = xmlGetProp(node, attr);
if (val) {
if (av_stristr((const char *)val, "video")) {
type = AVMEDIA_TYPE_VIDEO;
} else if (av_stristr((const char *)val, "audio")) {
type = AVMEDIA_TYPE_AUDIO;
}
xmlFree(val);
}
}
}
return type;
}
static struct fragment * get_Fragment(char *range)
{
struct fragment * seg = av_mallocz(sizeof(struct fragment));
if (!seg)
return NULL;
seg->size = -1;
if (range) {
char *str_end_offset;
char *str_offset = av_strtok(range, "-", &str_end_offset);
seg->url_offset = strtoll(str_offset, NULL, 10);
seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset + 1;
}
return seg;
}
static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep,
xmlNodePtr fragmenturl_node,
xmlNodePtr *baseurl_nodes,
char *rep_id_val,
char *rep_bandwidth_val)
{
DASHContext *c = s->priv_data;
char *initialization_val = NULL;
char *media_val = NULL;
char *range_val = NULL;
int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
range_val = xmlGetProp(fragmenturl_node, "range");
if (initialization_val || range_val) {
rep->init_section = get_Fragment(range_val);
if (!rep->init_section) {
xmlFree(initialization_val);
xmlFree(range_val);
return AVERROR(ENOMEM);
}
rep->init_section->url = get_content_url(baseurl_nodes, 4,
max_url_size,
rep_id_val,
rep_bandwidth_val,
initialization_val);
if (!rep->init_section->url) {
av_free(rep->init_section);
xmlFree(initialization_val);
xmlFree(range_val);
return AVERROR(ENOMEM);
}
xmlFree(initialization_val);
xmlFree(range_val);
}
} else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
media_val = xmlGetProp(fragmenturl_node, "media");
range_val = xmlGetProp(fragmenturl_node, "mediaRange");
if (media_val || range_val) {
struct fragment *seg = get_Fragment(range_val);
if (!seg) {
xmlFree(media_val);
xmlFree(range_val);
return AVERROR(ENOMEM);
}
seg->url = get_content_url(baseurl_nodes, 4,
max_url_size,
rep_id_val,
rep_bandwidth_val,
media_val);
if (!seg->url) {
av_free(seg);
xmlFree(media_val);
xmlFree(range_val);
return AVERROR(ENOMEM);
}
dynarray_add(&rep->fragments, &rep->n_fragments, seg);
xmlFree(media_val);
xmlFree(range_val);
}
}
return 0;
}
static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep,
xmlNodePtr fragment_timeline_node)
{
xmlAttrPtr attr = NULL;
char *val = NULL;
if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
struct timeline *tml = av_mallocz(sizeof(struct timeline));
if (!tml) {
return AVERROR(ENOMEM);
}
attr = fragment_timeline_node->properties;
while (attr) {
val = xmlGetProp(fragment_timeline_node, attr->name);
if (!val) {
av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
continue;
}
if (!av_strcasecmp(attr->name, (const char *)"t")) {
tml->starttime = (int64_t)strtoll(val, NULL, 10);
} else if (!av_strcasecmp(attr->name, (const char *)"r")) {
tml->repeat =(int64_t) strtoll(val, NULL, 10);
} else if (!av_strcasecmp(attr->name, (const char *)"d")) {
tml->duration = (int64_t)strtoll(val, NULL, 10);
}
attr = attr->next;
xmlFree(val);
}
dynarray_add(&rep->timelines, &rep->n_timelines, tml);
}
return 0;
}
static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes) {
char *tmp_str = NULL;
char *path = NULL;
char *mpdName = NULL;
xmlNodePtr node = NULL;
char *baseurl = NULL;
char *root_url = NULL;
char *text = NULL;
int isRootHttp = 0;
char token ='/';
int start = 0;
int rootId = 0;
int updated = 0;
int size = 0;
int i;
int tmp_max_url_size = strlen(url);
for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
text = xmlNodeGetContent(baseurl_nodes[i]);
if (!text)
continue;
tmp_max_url_size += strlen(text);
if (ishttp(text)) {
xmlFree(text);
break;
}
xmlFree(text);
}
tmp_max_url_size = aligned(tmp_max_url_size);
text = av_mallocz(tmp_max_url_size);
if (!text) {
updated = AVERROR(ENOMEM);
goto end;
}
av_strlcpy(text, url, strlen(url)+1);
while (mpdName = av_strtok(text, "/", &text)) {
size = strlen(mpdName);
}
path = av_mallocz(tmp_max_url_size);
tmp_str = av_mallocz(tmp_max_url_size);
if (!tmp_str || !path) {
updated = AVERROR(ENOMEM);
goto end;
}
av_strlcpy (path, url, strlen(url) - size + 1);
for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
if (!(node = baseurl_nodes[rootId])) {
continue;
}
if (ishttp(xmlNodeGetContent(node))) {
break;
}
}
node = baseurl_nodes[rootId];
baseurl = xmlNodeGetContent(node);
root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
if (node) {
xmlNodeSetContent(node, root_url);
updated = 1;
}
size = strlen(root_url);
isRootHttp = ishttp(root_url);
if (root_url[size - 1] != token) {
av_strlcat(root_url, "/", size + 2);
size += 2;
}
for (i = 0; i < n_baseurl_nodes; ++i) {
if (i == rootId) {
continue;
}
text = xmlNodeGetContent(baseurl_nodes[i]);
if (text) {
memset(tmp_str, 0, strlen(tmp_str));
if (!ishttp(text) && isRootHttp) {
av_strlcpy(tmp_str, root_url, size + 1);
}
start = (text[0] == token);
av_strlcat(tmp_str, text + start, tmp_max_url_size);
xmlNodeSetContent(baseurl_nodes[i], tmp_str);
updated = 1;
xmlFree(text);
}
}
end:
if (tmp_max_url_size > *max_url_size) {
*max_url_size = tmp_max_url_size;
}
av_free(path);
av_free(tmp_str);
return updated;
}
static int parse_manifest_segmentbasenode(AVFormatContext *s, struct representation *rep,
xmlNodePtr representation_segmentbase_node, char *url)
{
if (url == NULL) {
return AVERROR(ENOMEM);
}
DASHContext *c = s->priv_data;
if (c->parse_seg_base != 1) {
free(url);
return -1;
}
char *index_range = xmlGetProp(representation_segmentbase_node, "indexRange");
if (index_range == NULL) {
free(url);
return -1;
}
rep->segBase.index_range = get_Fragment(index_range);
if (rep->segBase.index_range == NULL) {
xmlFree(index_range);
free(url);
return AVERROR(ENOMEM);
}
rep->segBase.index_range->url = url;
xmlFree(index_range);
xmlNodePtr segmentbas_element_node = xmlFirstElementChild(representation_segmentbase_node);
while (segmentbas_element_node) {
if (!av_strcasecmp(segmentbas_element_node->name, (const char *)"Initialization")) {
if (rep->init_section != NULL) {
/* only first Initialization take effect */
break;
}
char *init_range = xmlGetProp(segmentbas_element_node, "range");
if (init_range == NULL) {
/* don't care other element */
continue;
}
rep->init_section = get_Fragment(init_range);
if (rep->init_section == NULL) {
xmlFree(init_range);
free_fragment(&rep->segBase.index_range);
return AVERROR(ENOMEM);
}
xmlFree(init_range);
rep->init_section->url = strdup(url);
if (rep->init_section->url == NULL) {
av_freep(&rep->init_section);
free_fragment(&rep->segBase.index_range);
return AVERROR(ENOMEM);
}
}
segmentbas_element_node = xmlNextElementSibling(segmentbas_element_node);
}
return 0;
}
static int parse_manifest_representation(AVFormatContext *s, const char *url,
xmlNodePtr node,
xmlNodePtr adaptionset_node,
xmlNodePtr mpd_baseurl_node,
xmlNodePtr period_baseurl_node,
xmlNodePtr period_segmenttemplate_node,
xmlNodePtr period_segmentlist_node,
xmlNodePtr fragment_template_node,
xmlNodePtr content_component_node,
xmlNodePtr adaptionset_baseurl_node,
xmlNodePtr adaptionset_segmentlist_node,
xmlNodePtr adaptionset_supplementalproperty_node,
xmlNodePtr content_protection_node)
{
int32_t ret = 0;
int32_t audio_rep_idx = 0;
int32_t video_rep_idx = 0;
DASHContext *c = s->priv_data;
struct representation *rep = NULL;
struct fragment *seg = NULL;
xmlNodePtr representation_segmenttemplate_node = NULL;
xmlNodePtr representation_baseurl_node = NULL;
xmlNodePtr representation_segmentlist_node = NULL;
xmlNodePtr representation_segmentbase_node = NULL;
xmlNodePtr segmentlists_tab[2];
xmlNodePtr fragment_timeline_node = NULL;
xmlNodePtr fragment_templates_tab[5];
char *duration_val = NULL;
char *presentation_timeoffset_val = NULL;
char *startnumber_val = NULL;
char *timescale_val = NULL;
char *initialization_val = NULL;
char *media_val = NULL;
char *val = NULL;
xmlNodePtr baseurl_nodes[4];
xmlNodePtr representation_node = node;
char *rep_id_val = xmlGetProp(representation_node, "id");
char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN;
// try get information from representation
if (type == AVMEDIA_TYPE_UNKNOWN)
type = get_content_type(representation_node);
// try get information from contentComponen
if (type == AVMEDIA_TYPE_UNKNOWN)
type = get_content_type(content_component_node);
// try get information from adaption set
if (type == AVMEDIA_TYPE_UNKNOWN)
type = get_content_type(adaptionset_node);
if (type == AVMEDIA_TYPE_UNKNOWN) {
av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
} else if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO) {
// convert selected representation to our internal struct
rep = av_mallocz(sizeof(struct representation));
if (!rep) {
ret = AVERROR(ENOMEM);
goto end;
}
rep->file_mime_type = get_representation_file_type(representation_node, content_component_node, adaptionset_node);
representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
baseurl_nodes[0] = mpd_baseurl_node;
baseurl_nodes[1] = period_baseurl_node;
baseurl_nodes[2] = adaptionset_baseurl_node;
baseurl_nodes[3] = representation_baseurl_node;
ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
c->max_url_size = aligned(c->max_url_size + strlen(rep_id_val) + strlen(rep_bandwidth_val));
if (ret == AVERROR(ENOMEM) || ret == 0) {
goto end;
}
if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
fragment_timeline_node = NULL;
fragment_templates_tab[0] = representation_segmenttemplate_node;
fragment_templates_tab[1] = adaptionset_segmentlist_node;
fragment_templates_tab[2] = fragment_template_node;
fragment_templates_tab[3] = period_segmenttemplate_node;
fragment_templates_tab[4] = period_segmentlist_node;
presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
if (initialization_val) {
rep->init_section = av_mallocz(sizeof(struct fragment));
if (!rep->init_section) {
av_free(rep);
ret = AVERROR(ENOMEM);
goto end;
}
c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
if (!rep->init_section->url) {
av_free(rep->init_section);
av_free(rep);
ret = AVERROR(ENOMEM);
goto end;
}
rep->init_section->size = -1;
xmlFree(initialization_val);
}
if (media_val) {
c->max_url_size = aligned(c->max_url_size + strlen(media_val));
rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
xmlFree(media_val);
}
if (presentation_timeoffset_val) {
rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
xmlFree(presentation_timeoffset_val);
}
if (duration_val) {
rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
xmlFree(duration_val);
}
if (timescale_val) {
rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
xmlFree(timescale_val);
}
if (startnumber_val) {
rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
xmlFree(startnumber_val);
}
if (adaptionset_supplementalproperty_node) {
if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
if (!val) {
av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
} else {
rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
xmlFree(val);
}
}
}
fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
if (!fragment_timeline_node)
fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
if (!fragment_timeline_node)
fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
if (!fragment_timeline_node)
fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
if (fragment_timeline_node) {
fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
while (fragment_timeline_node) {
ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
if (ret < 0) {
return ret;
}
fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
}
}
} else if (representation_baseurl_node && !representation_segmentlist_node) {
representation_segmentbase_node = find_child_node_by_name(representation_node, "SegmentBase");
if (representation_segmentbase_node == NULL ||
(rep->file_mime_type != MIME_TYPE_MP4) ||
parse_manifest_segmentbasenode(s, rep, representation_segmentbase_node,
get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL)) != 0) {
seg = av_mallocz(sizeof(struct fragment));
if (!seg) {
ret = AVERROR(ENOMEM);
goto end;
}
seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL);
if (!seg->url) {
av_free(seg);
ret = AVERROR(ENOMEM);
goto end;
}
seg->size = -1;
dynarray_add(&rep->fragments, &rep->n_fragments, seg);
}
} else if (representation_segmentlist_node) {
// TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
// http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
xmlNodePtr fragmenturl_node = NULL;
segmentlists_tab[0] = representation_segmentlist_node;
segmentlists_tab[1] = adaptionset_segmentlist_node;
duration_val = get_val_from_nodes_tab(segmentlists_tab, 2, "duration");
timescale_val = get_val_from_nodes_tab(segmentlists_tab, 2, "timescale");
if (duration_val) {
rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
xmlFree(duration_val);
}
if (timescale_val) {
rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
xmlFree(timescale_val);
}
fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
while (fragmenturl_node) {
ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
baseurl_nodes,
rep_id_val,
rep_bandwidth_val);
if (ret < 0) {
return ret;
}
fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
}
fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
if (!fragment_timeline_node)
fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
if (!fragment_timeline_node)
fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
if (!fragment_timeline_node)
fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
if (fragment_timeline_node) {
fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
while (fragment_timeline_node) {
ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
if (ret < 0) {
return ret;
}
fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
}
}
} else {
free_representation(rep);
rep = NULL;
av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
}
if (rep) {
if (content_protection_node) {
rep->content_protection.is_valid = 1;
rep->content_protection.key_id = xmlGetProp(content_protection_node, "default_KID");
rep->content_protection.system_id = xmlGetProp(content_protection_node, "schemeIdUri");
xmlNodePtr pssh_node = find_child_node_by_name(content_protection_node, "pssh");
rep->content_protection.pssh = xmlNodeGetContent(pssh_node);
}
if (rep->fragment_duration > 0 && !rep->fragment_timescale)
rep->fragment_timescale = 1;
rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
rep->framerate = av_make_q(0, 0);
if (type == AVMEDIA_TYPE_VIDEO && rep_framerate_val) {
ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
if (ret < 0)
av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
}
rep->type = type;
if (type == AVMEDIA_TYPE_VIDEO) {
rep->rep_idx = video_rep_idx;
dynarray_add(&c->videos, &c->n_videos, rep);
} else {
rep->rep_idx = audio_rep_idx;
dynarray_add(&c->audios, &c->n_audios, rep);
}
}
}
video_rep_idx += type == AVMEDIA_TYPE_VIDEO;
audio_rep_idx += type == AVMEDIA_TYPE_AUDIO;
end:
if (rep_id_val)
xmlFree(rep_id_val);
if (rep_bandwidth_val)
xmlFree(rep_bandwidth_val);
if (rep_framerate_val)
xmlFree(rep_framerate_val);
return ret;
}
static int parse_manifest_adaptationset(AVFormatContext *s, const char *url,
xmlNodePtr adaptionset_node,
xmlNodePtr mpd_baseurl_node,
xmlNodePtr period_baseurl_node,
xmlNodePtr period_segmenttemplate_node,
xmlNodePtr period_segmentlist_node)
{
int ret = 0;
xmlNodePtr fragment_template_node = NULL;
xmlNodePtr content_component_node = NULL;
xmlNodePtr adaptionset_baseurl_node = NULL;
xmlNodePtr adaptionset_segmentlist_node = NULL;
xmlNodePtr adaptionset_supplementalproperty_node = NULL;
xmlNodePtr content_protection_node = NULL;
xmlNodePtr node = NULL;
node = xmlFirstElementChild(adaptionset_node);
while (node) {
if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
fragment_template_node = node;
} else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
content_component_node = node;
} else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
adaptionset_baseurl_node = node;
} else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
adaptionset_segmentlist_node = node;
} else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
adaptionset_supplementalproperty_node = node;
} else if (!av_strcasecmp(node->name, (const char *)"ContentProtection")) {
content_protection_node = node;
} else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
ret = parse_manifest_representation(s, url, node,
adaptionset_node,
mpd_baseurl_node,
period_baseurl_node,
period_segmenttemplate_node,
period_segmentlist_node,
fragment_template_node,
content_component_node,
adaptionset_baseurl_node,
adaptionset_segmentlist_node,
adaptionset_supplementalproperty_node,
content_protection_node);
if (ret < 0) {
return ret;
}
}
node = xmlNextElementSibling(node);
}
return 0;
}
static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
{
DASHContext *c = s->priv_data;
int ret = 0;
int close_in = 0;
uint8_t *new_url = NULL;
int64_t filesize = 0;
char *buffer = NULL;
AVDictionary *opts = NULL;
xmlDoc *doc = NULL;
xmlNodePtr root_element = NULL;
xmlNodePtr node = NULL;
xmlNodePtr period_node = NULL;
xmlNodePtr mpd_baseurl_node = NULL;
xmlNodePtr period_baseurl_node = NULL;
xmlNodePtr period_segmenttemplate_node = NULL;
xmlNodePtr period_segmentlist_node = NULL;
xmlNodePtr adaptionset_node = NULL;
xmlAttrPtr attr = NULL;
char *val = NULL;
uint32_t period_duration_sec = 0;
uint32_t period_start_sec = 0;
if (!in) {
close_in = 1;
set_httpheader_options(c, &opts);
ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
av_dict_free(&opts);
if (ret < 0)
return ret;
}
if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
c->base_url = av_strdup(new_url);
} else {
c->base_url = av_strdup(url);
}
filesize = avio_size(in);
if (filesize <= 0) {
filesize = 8 * 1024;
}
buffer = av_mallocz(filesize);
if (!buffer) {
av_free(c->base_url);
return AVERROR(ENOMEM);
}
filesize = avio_read(in, buffer, filesize);
if (filesize <= 0) {
av_log(s, AV_LOG_ERROR, "Unable to read to offset '%s'\n", url);
ret = AVERROR_INVALIDDATA;
} else {
LIBXML_TEST_VERSION
doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
root_element = xmlDocGetRootElement(doc);
node = root_element;
if (!node) {
ret = AVERROR_INVALIDDATA;
av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
goto cleanup;
}
if (node->type != XML_ELEMENT_NODE ||
av_strcasecmp(node->name, (const char *)"MPD")) {
ret = AVERROR_INVALIDDATA;
av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
goto cleanup;
}
val = xmlGetProp(node, "type");
if (!val) {
av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
ret = AVERROR_INVALIDDATA;
goto cleanup;
}
if (!av_strcasecmp(val, (const char *)"dynamic"))
c->is_live = 1;
xmlFree(val);
attr = node->properties;
while (attr) {
val = xmlGetProp(node, attr->name);
if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
} else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
c->publish_time = get_utc_date_time_insec(s, (const char *)val);
} else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
c->minimum_update_period = get_duration_insec(s, (const char *)val);
} else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
} else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
c->min_buffer_time = get_duration_insec(s, (const char *)val);
} else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
} else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
c->media_presentation_duration = get_duration_insec(s, (const char *)val);
}
attr = attr->next;
xmlFree(val);
}
mpd_baseurl_node = find_child_node_by_name(node, "BaseURL");
if (!mpd_baseurl_node) {
mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
}
// at now we can handle only one period, with the longest duration
node = xmlFirstElementChild(node);
while (node) {
if (!av_strcasecmp(node->name, (const char *)"Period")) {
period_duration_sec = 0;
period_start_sec = 0;
attr = node->properties;
while (attr) {
val = xmlGetProp(node, attr->name);
if (!av_strcasecmp(attr->name, (const char *)"duration")) {
period_duration_sec = get_duration_insec(s, (const char *)val);
} else if (!av_strcasecmp(attr->name, (const char *)"start")) {
period_start_sec = get_duration_insec(s, (const char *)val);
}
attr = attr->next;
xmlFree(val);
}
if ((period_duration_sec) >= (c->period_duration)) {
period_node = node;
c->period_duration = period_duration_sec;
c->period_start = period_start_sec;
if (c->period_start > 0)
c->media_presentation_duration = c->period_duration;
}
}
node = xmlNextElementSibling(node);
}
if (!period_node) {
av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
ret = AVERROR_INVALIDDATA;
goto cleanup;
}
if (c->is_live == 0 && c->period_duration <= 0 && c->media_presentation_duration > 0) {
c->period_duration = c->media_presentation_duration;
}
adaptionset_node = xmlFirstElementChild(period_node);
int parse_video_adaptation = 0;
while (adaptionset_node) {
if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
period_baseurl_node = adaptionset_node;
} else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentTemplate")) {
period_segmenttemplate_node = adaptionset_node;
} else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentList")) {
period_segmentlist_node = adaptionset_node;
} else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
if (get_content_type(adaptionset_node) != AVMEDIA_TYPE_VIDEO ||
get_content_type(adaptionset_node) == AVMEDIA_TYPE_VIDEO && parse_video_adaptation == 0) {
parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
if (get_content_type(adaptionset_node) == AVMEDIA_TYPE_VIDEO) {
parse_video_adaptation = 1;
}
}
}
adaptionset_node = xmlNextElementSibling(adaptionset_node);
}
cleanup:
/*free the document */
xmlFreeDoc(doc);
xmlCleanupParser();
}
av_free(new_url);
av_free(buffer);
if (close_in) {
avio_close(in);
}
return ret;
}
static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
{
DASHContext *c = s->priv_data;
int64_t num = 0;
int64_t start_time_offset = 0;
if (c->is_live) {
if (pls->n_fragments) {
num = pls->first_seq_no;
} else if (pls->n_timelines) {
start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
num = calc_next_seg_no_from_timelines(pls, start_time_offset);
if (num == -1)
num = pls->first_seq_no;
else
num += pls->first_seq_no;
} else if (pls->fragment_duration){
if (pls->presentation_timeoffset) {
num = pls->presentation_timeoffset * pls->fragment_timescale / pls->fragment_duration;
} else if (c->publish_time > 0 && !c->availability_start_time) {
num = pls->first_seq_no + (((c->publish_time - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
} else {
num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->suggested_presentation_delay) * pls->fragment_timescale) / pls->fragment_duration;
}
}
} else {
num = pls->first_seq_no;
}
return num;
}
static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
{
DASHContext *c = s->priv_data;
int64_t num = 0;
if (c->is_live && pls->fragment_duration) {
num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time) - c->time_shift_buffer_depth) * pls->fragment_timescale) / pls->fragment_duration;
} else {
num = pls->first_seq_no;
}
return num;
}
static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
{
int64_t num = 0;
if (pls->n_fragments) {
num = pls->first_seq_no + pls->n_fragments - 1;
} else if (pls->n_timelines) {
int i = 0;
num = pls->first_seq_no + pls->n_timelines - 1;
for (i = 0; i < pls->n_timelines; i++) {
num += pls->timelines[i]->repeat;
}
} else if (c->is_live && pls->fragment_duration) {
num = pls->first_seq_no + (((get_current_time_in_sec() - c->availability_start_time)) * pls->fragment_timescale) / pls->fragment_duration;
} else if (pls->fragment_duration) {
num = pls->first_seq_no + (c->media_presentation_duration * pls->fragment_timescale) / pls->fragment_duration;
}
return num;
}
static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
{
if (rep_dest && rep_src ) {
free_timelines_list(rep_dest);
rep_dest->timelines = rep_src->timelines;
rep_dest->n_timelines = rep_src->n_timelines;
rep_dest->first_seq_no = rep_src->first_seq_no;
rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
rep_src->timelines = NULL;
rep_src->n_timelines = 0;
rep_dest->cur_seq_no = rep_src->cur_seq_no;
}
}
static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
{
if (rep_dest && rep_src ) {
free_fragment_list(rep_dest);
if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
rep_dest->cur_seq_no = 0;
else
rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
rep_dest->fragments = rep_src->fragments;
rep_dest->n_fragments = rep_src->n_fragments;
rep_dest->parent = rep_src->parent;
rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
rep_src->fragments = NULL;
rep_src->n_fragments = 0;
}
}
static int refresh_manifest(AVFormatContext *s)
{
int ret = 0, i;
DASHContext *c = s->priv_data;
// save current context
int n_videos = c->n_videos;
struct representation **videos = c->videos;
int n_audios = c->n_audios;
struct representation **audios = c->audios;
char *base_url = c->base_url;
c->base_url = NULL;
c->n_videos = 0;
c->videos = NULL;
c->n_audios = 0;
c->audios = NULL;
ret = parse_manifest(s, s->filename, NULL);
if (ret)
goto finish;
if (c->n_videos != n_videos) {
av_log(c, AV_LOG_ERROR,
"new manifest has mismatched no. of video representations, %d -> %d\n",
n_videos, c->n_videos);
return AVERROR_INVALIDDATA;
}
if (c->n_audios != n_audios) {
av_log(c, AV_LOG_ERROR,
"new manifest has mismatched no. of audio representations, %d -> %d\n",
n_audios, c->n_audios);
return AVERROR_INVALIDDATA;
}
for (i = 0; i < n_videos; i++) {
struct representation *cur_video = videos[i];
struct representation *ccur_video = c->videos[i];
if (cur_video->timelines) {
// calc current time
int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
// update segments
ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
if (ccur_video->cur_seq_no >= 0) {
move_timelines(ccur_video, cur_video, c);
}
}
if (cur_video->fragments) {
move_segments(ccur_video, cur_video, c);
}
}
for (i = 0; i < n_audios; i++) {
struct representation *cur_audio = audios[i];
struct representation *ccur_audio = c->audios[i];
if (cur_audio->timelines) {
// calc current time
int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
// update segments
ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
if (ccur_audio->cur_seq_no >= 0) {
move_timelines(ccur_audio, cur_audio, c);
}
}
if (cur_audio->fragments) {
move_segments(ccur_audio, cur_audio, c);
}
}
finish:
// restore context
if (c->base_url)
av_free(base_url);
else
c->base_url = base_url;
if (c->audios)
free_audio_list(c);
if (c->videos)
free_video_list(c);
c->n_audios = n_audios;
c->audios = audios;
c->n_videos = n_videos;
c->videos = videos;
return ret;
}
static struct fragment *get_current_fragment(struct representation *pls)
{
int64_t min_seq_no = 0;
int64_t max_seq_no = 0;
struct fragment *seg = NULL;
struct fragment *seg_ptr = NULL;
DASHContext *c = pls->parent->priv_data;
while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
if (pls->cur_seq_no < pls->n_fragments) {
seg_ptr = pls->fragments[pls->cur_seq_no];
seg = av_mallocz(sizeof(struct fragment));
if (!seg) {
return NULL;
}
if (seg_ptr->url == NULL && pls->segBase.index_range != NULL && pls->segBase.index_range->url != NULL) {
seg->url = av_strdup(pls->segBase.index_range->url);
} else {
seg->url = av_strdup(seg_ptr->url);
}
if (!seg->url) {
av_free(seg);
return NULL;
}
seg->size = seg_ptr->size;
seg->url_offset = seg_ptr->url_offset;
return seg;
} else if (c->is_live) {
refresh_manifest(pls->parent);
} else {
break;
}
}
if (c->is_live) {
min_seq_no = calc_min_seg_no(pls->parent, pls);
max_seq_no = calc_max_seg_no(pls, c);
if (pls->timelines || pls->fragments) {
refresh_manifest(pls->parent);
}
if (pls->cur_seq_no <= min_seq_no) {
av_log(pls->parent, AV_LOG_VERBOSE, "old fragment: cur[%"PRId64"] min[%"PRId64"] max[%"PRId64"], playlist %d\n", (int64_t)pls->cur_seq_no, min_seq_no, max_seq_no, (int)pls->rep_idx);
pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
} else if (pls->cur_seq_no > max_seq_no) {
av_log(pls->parent, AV_LOG_VERBOSE, "new fragment: min[%"PRId64"] max[%"PRId64"], playlist %d\n", min_seq_no, max_seq_no, (int)pls->rep_idx);
}
seg = av_mallocz(sizeof(struct fragment));
if (!seg) {
return NULL;
}
} else if (pls->cur_seq_no <= pls->last_seq_no) {
seg = av_mallocz(sizeof(struct fragment));
if (!seg) {
return NULL;
}
}
if (seg) {
char *tmpfilename= av_mallocz(c->max_url_size);
if (!tmpfilename) {
return NULL;
}
ff_dash_fill_tmpl_params(tmpfilename, c->max_url_size, pls->url_template, 0, pls->cur_seq_no, 0, get_segment_start_time_based_on_timeline(pls, pls->cur_seq_no));
seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
if (!seg->url) {
av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
seg->url = av_strdup(pls->url_template);
if (!seg->url) {
av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
av_free(tmpfilename);
return NULL;
}
}
av_free(tmpfilename);
seg->size = -1;
}
return seg;
}
enum ReadFromURLMode {
READ_NORMAL,
READ_COMPLETE,
};
static int read_from_url(struct representation *pls, struct fragment *seg,
uint8_t *buf, int buf_size,
enum ReadFromURLMode mode)
{
int ret;
/* limit read if the fragment was only a part of a file */
if (seg->size >= 0)
buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
if (mode == READ_COMPLETE) {
ret = avio_read(pls->input, buf, buf_size);
if (ret < buf_size) {
av_log(pls->parent, AV_LOG_WARNING, "Could not read complete fragment.\n");
}
} else {
ret = avio_read(pls->input, buf, buf_size);
}
if (ret > 0)
pls->cur_seg_offset += ret;
return ret;
}
static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
{
AVDictionary *opts = NULL;
char *url = NULL;
int ret = 0;
url = av_mallocz(c->max_url_size);
if (!url) {
goto cleanup;
}
set_httpheader_options(c, &opts);
if (seg->size >= 0) {
/* try to restrict the HTTP request to the part we want
* (if this is in fact a HTTP request) */
av_dict_set_int(&opts, "offset", seg->url_offset, 0);
av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
}
ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n",
url, seg->url_offset, pls->rep_idx);
ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL);
if (ret < 0) {
goto cleanup;
}
cleanup:
av_free(url);
av_dict_free(&opts);
pls->cur_seg_offset = 0;
pls->cur_seg_size = seg->size;
return ret;
}
static int update_single_media_index_range(struct representation *pls)
{
DASHContext *c = pls->parent->priv_data;
if (pls == NULL || pls->n_fragments > 0 ||
pls->init_section == NULL || pls->init_section->size <= 0 ||
pls->segBase.index_range == NULL || pls->segBase.index_range->size <= 0) {
return 0;
}
if (pls->segBase.index_range_buf != NULL) {
return 0;
}
av_fast_malloc(&pls->segBase.index_range_buf, &pls->segBase.index_range_size, pls->segBase.index_range->size);
if (pls->segBase.index_range_buf == NULL || pls->segBase.index_range_size == 0) {
return AVERROR(ENOMEM);
}
int ret = open_input(c, pls, pls->segBase.index_range);
if (ret < 0) {
av_freep(&pls->segBase.index_range_buf);
pls->segBase.index_range_size = 0;
av_log(pls->parent, AV_LOG_WARNING,
"Failed to open an index range section in playlist %d\n",
pls->rep_idx);
return ret;
}
ret = read_from_url(pls, pls->segBase.index_range, pls->segBase.index_range_buf,
pls->segBase.index_range_size, READ_COMPLETE);
ff_format_io_close(pls->parent, &pls->input);
if (ret < 0) {
av_freep(&pls->segBase.index_range_buf);
pls->segBase.index_range_size = 0;
return ret;
}
int64_t offset = pls->init_section->size + pls->segBase.index_range->size;
const char *buf = pls->segBase.index_range_buf;
buf += 8;
int version = AV_RB8(buf);
buf++;
if (version > 1) {
av_log(0, AV_LOG_ERROR, "mov index range version invalid\n");
return AVERROR_INVALIDDATA;
}
buf += 11; // flags: 3 track id:4 timescale:4
if (version == 0) {
buf += 4; // pts
offset += AV_RB32(buf);
buf +=4;
} else {
buf += 8; // pts
offset += AV_RB64(buf);
buf += 8;
}
buf += 2; // reserved
int cnt = AV_RB16(buf);
buf +=2;
if (cnt <= 0) {
av_log(0, AV_LOG_ERROR, "mov index range cnt invalid\n");
return AVERROR_INVALIDDATA;
}
for (int i = 0; i < cnt; i++) {
uint32_t size = AV_RB32(buf);
if (size & 0x80000000) {
av_log(0, AV_LOG_ERROR, "mov index range size invalid\n");
return AVERROR_PATCHWELCOME;
}
buf += 12;
char range_val[100];
snprintf(range_val, sizeof(range_val), "%lld-%lld", offset, offset + size - 1);
struct fragment *seg = get_Fragment(range_val);
if (!seg) {
return AVERROR(ENOMEM);
}
seg->url = NULL;
dynarray_add(&pls->fragments, &pls->n_fragments, seg);
offset += size;
}
pls->last_seq_no = calc_max_seg_no(pls, c);
return 0;
}
static int update_init_section(struct representation *pls)
{
static const int max_init_section_size = 1024 * 1024;
DASHContext *c = pls->parent->priv_data;
int64_t sec_size;
int64_t urlsize;
int ret;
if (!pls->init_section || pls->init_sec_buf)
return 0;
ret = open_input(c, pls, pls->init_section);
if (ret < 0) {
av_log(pls->parent, AV_LOG_WARNING,
"Failed to open an initialization section in playlist %d\n",
pls->rep_idx);
return ret;
}
if (pls->init_section->size >= 0)
sec_size = pls->init_section->size;
else if ((urlsize = avio_size(pls->input)) >= 0)
sec_size = urlsize;
else
sec_size = max_init_section_size;
av_log(pls->parent, AV_LOG_DEBUG,
"Downloading an initialization section of size %"PRId64"\n",
sec_size);
sec_size = FFMIN(sec_size, max_init_section_size);
av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
pls->init_sec_buf_size, READ_COMPLETE);
ff_format_io_close(pls->parent, &pls->input);
if (ret < 0)
return ret;
pls->init_sec_data_len = ret;
pls->init_sec_buf_read_offset = 0;
ret = update_single_media_index_range(pls);
if (ret < 0) {
return ret;
}
return 0;
}
static int64_t seek_data(void *opaque, int64_t offset, int whence)
{
struct representation *v = opaque;
if (v->n_fragments && !v->init_sec_data_len) {
return avio_seek(v->input, offset, whence);
}
return AVERROR(ENOSYS);
}
static int read_data(void *opaque, uint8_t *buf, int buf_size)
{
int ret = 0;
struct representation *v = opaque;
DASHContext *c = v->parent->priv_data;
restart:
if (!v->input) {
/* load/update Media Initialization Section, if any */
ret = update_init_section(v);
if (ret) {
printf("read_data update_init_section ret:%d\n", ret);
goto end;
}
free_fragment(&v->cur_seg);
v->cur_seg = get_current_fragment(v);
if (!v->cur_seg) {
ret = AVERROR_EOF;
printf("read_data get_current_fragment ret:%d\n", ret);
goto end;
}
ret = open_input(c, v, v->cur_seg);
if (ret < 0) {
if (ff_check_interrupt(c->interrupt_callback)) {
goto end;
ret = AVERROR_EXIT;
}
av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist %d\n", v->rep_idx);
v->cur_seq_no++;
goto restart;
}
}
if (v->init_sec_buf_read_offset < v->init_sec_data_len) {
/* Push init section out first before first actual fragment */
int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
memcpy(buf, v->init_sec_buf, copy_size);
v->init_sec_buf_read_offset += copy_size;
ret = copy_size;
goto end;
}
/* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
if (!v->cur_seg) {
v->cur_seg = get_current_fragment(v);
}
if (!v->cur_seg) {
ret = AVERROR_EOF;
goto end;
}
ret = read_from_url(v, v->cur_seg, buf, buf_size, READ_NORMAL);
if (ret > 0)
goto end;
if (c->is_live || v->cur_seq_no < v->last_seq_no) {
if (!v->is_restart_needed)
v->cur_seq_no++;
v->is_restart_needed = 1;
}
end:
return ret;
}
static int save_avio_options(AVFormatContext *s)
{
DASHContext *c = s->priv_data;
const char *opts[] = { "headers", "user_agent", "user-agent", "cookies", NULL }, **opt = opts;
uint8_t *buf = NULL;
int ret = 0;
while (*opt) {
if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
if (buf[0] != '\0') {
ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
if (ret < 0) {
av_freep(&buf);
return ret;
}
} else {
av_freep(&buf);
}
}
opt++;
}
return ret;
}
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
int flags, AVDictionary **opts)
{
av_log(s, AV_LOG_ERROR,
"A DASH playlist item '%s' referred to an external file '%s'. "
"Opening this file was forbidden for security reasons\n",
s->filename, url);
return AVERROR(EPERM);
}
static void close_demux_for_component(struct representation *pls)
{
/* note: the internal buffer could have changed */
av_freep(&pls->pb.buffer);
memset(&pls->pb, 0x00, sizeof(AVIOContext));
pls->ctx->pb = NULL;
avformat_close_input(&pls->ctx);
pls->ctx = NULL;
}
static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
{
DASHContext *c = s->priv_data;
AVInputFormat *in_fmt = NULL;
AVDictionary *in_fmt_opts = NULL;
uint8_t *avio_ctx_buffer = NULL;
int ret = 0, i;
int find_one_stream = 0;
if (pls->ctx) {
close_demux_for_component(pls);
}
if (!(pls->ctx = avformat_alloc_context())) {
ret = AVERROR(ENOMEM);
goto fail;
}
avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
if (!avio_ctx_buffer ) {
ret = AVERROR(ENOMEM);
avformat_free_context(pls->ctx);
pls->ctx = NULL;
goto fail;
}
if (c->is_live) {
ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
} else {
ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
}
pls->pb.seekable = 0;
if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
goto fail;
pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
pls->ctx->probesize = 1024 * 4;
pls->ctx->max_analyze_duration = 4 * AV_TIME_BASE;
ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Error when loading first fragment, playlist %d\n", (int)pls->rep_idx);
avformat_free_context(pls->ctx);
pls->ctx = NULL;
goto fail;
}
pls->ctx->pb = &pls->pb;
pls->ctx->io_open = nested_io_open;
// provide additional information from mpd if available
ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
av_dict_free(&in_fmt_opts);
if (ret < 0)
goto fail;
for (i = 0; i < pls->ctx->nb_streams; i++) {
if (pls->ctx->iformat != NULL && pls->ctx->iformat->name != NULL &&
av_strncasecmp(pls->ctx->iformat->name, "mov", 3) == 0) {
if (pls->ctx->streams[i]->codecpar->codec_type != pls->type || find_one_stream == 1) {
av_log(0, AV_LOG_INFO, "dash mp4, not support SubRepresentation\n");
pls->ctx->streams[i]->discard = AVDISCARD_ALL;
continue;
} else {
find_one_stream = 1;
}
}
}
if (pls->n_fragments) {
#if FF_API_R_FRAME_RATE
if (pls->framerate.den) {
for (i = 0; i < pls->ctx->nb_streams; i++)
pls->ctx->streams[i]->r_frame_rate = pls->framerate;
}
#endif
if (c->enable_raw_data == 0) {
ret = avformat_find_stream_info(pls->ctx, NULL);
if (ret < 0)
goto fail;
}
}
fail:
return ret;
}
static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
{
int ret = 0;
int i;
pls->parent = s;
pls->cur_seq_no = calc_cur_seg_no(s, pls);
if (!pls->last_seq_no) {
pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
}
ret = reopen_demux_for_component(s, pls);
if (ret < 0) {
goto fail;
}
for (i = 0; i < pls->ctx->nb_streams; i++) {
if (pls->ctx->streams[i]->discard == AVDISCARD_ALL) {
continue;
}
AVStream *st = avformat_new_stream(s, NULL);
AVStream *ist = pls->ctx->streams[i];
if (!st) {
ret = AVERROR(ENOMEM);
goto fail;
}
if (pls->file_mime_type == MIME_TYPE_MP2T) {
st->id = ist->id;
} else {
st->id = i;
}
avcodec_parameters_copy(st->codecpar, pls->ctx->streams[i]->codecpar);
avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
{
// copy content protection info from ist to st
int origin_side_data_size = 0;
uint8_t* origin_side_data = av_stream_get_side_data(ist, AV_PKT_DATA_ENCRYPTION_INIT_INFO, &origin_side_data_size);
if (origin_side_data) {
AVEncryptionInitInfo* new_encryption_init_info = av_encryption_init_info_get_side_data(origin_side_data, origin_side_data_size);
// check if the side data is already added, if so, append the new one to the end
int old_side_data_size = 0;
uint8_t* old_side_data = av_stream_get_side_data(st, AV_PKT_DATA_ENCRYPTION_INIT_INFO, &old_side_data_size);
AVEncryptionInitInfo *old_init_info = NULL;
if (old_side_data) {
old_init_info = av_encryption_init_info_get_side_data(old_side_data, old_side_data_size);
if (old_init_info) {
for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
if (!cur->next) {
cur->next = new_encryption_init_info;
break;
}
}
new_encryption_init_info = old_init_info;
}
}
int new_side_data_size = 0;
uint8_t* new_side_data = av_encryption_init_info_add_side_data(new_encryption_init_info, &new_side_data_size);
av_stream_add_side_data(st, AV_PKT_DATA_ENCRYPTION_INIT_INFO, new_side_data, new_side_data_size);
av_encryption_init_info_free(new_encryption_init_info);
}
}
}
return 0;
fail:
return ret;
}
static void sort_representation(DASHContext *c)
{
if (c == NULL || c->n_videos <= 1) {
return;
}
for (int i = 0; i < c->n_videos - 1; i++) {
int count = 0;
for (int j = 0; j < c->n_videos - 1 - i; j++) {
if (c->videos[j]->bandwidth > c->videos[j + 1]->bandwidth) {
struct representation *tmp = c->videos[j];
c->videos[j] = c->videos[j + 1];
c->videos[j + 1] = tmp;
count = 1;
}
}
if (count == 0) {
break;
}
}
}
static void dash_bandwidth_init(AVFormatContext *s)
{
DASHContext *c = s->priv_data;
c->band_width.state = DASH_BW_Begin;
c->band_width.playable_msec = -1;
if (c->quality == START_MODE_FIX_LOW || c->quality == START_MODE_FIX_HIGH) {
c->band_width.state = DASH_BW_FIX;
}
}
static void dash_get_download_speed(AVIOContext *input, int64_t *average_speed, int64_t *last_speed)
{
int64_t _average_speed = -1, _last_speed = -1;
av_opt_get_int(input, "average_speed", AV_OPT_SEARCH_CHILDREN, &_average_speed);
av_opt_get_int(input, "last_speed", AV_OPT_SEARCH_CHILDREN, &_last_speed);
if (average_speed != NULL) {
*average_speed = _average_speed;
}
if (last_speed != NULL) {
*last_speed = _last_speed;
}
}
static struct representation *dash_switch_representation(AVFormatContext *s, struct representation *cur_video, int bw_new)
{
DASHContext *c = s->priv_data;
if (bw_new < 0 || bw_new >= c->n_videos || c->cur_video_idx == bw_new) {
return cur_video;
}
int old_video_idx = c->cur_video_idx;
if (cur_video->input != NULL) {
ff_format_io_close(cur_video->parent, &cur_video->input);
}
if (cur_video->ctx != NULL) {
close_demux_for_component(cur_video);
}
c->cur_video_idx = bw_new;
struct representation *new_cur_video = c->videos[bw_new];
new_cur_video->parent = s;
new_cur_video->cur_seq_no = calc_cur_seg_no(s, new_cur_video);
if (cur_video->cur_seq_no != new_cur_video->cur_seq_no) {
new_cur_video->cur_seq_no = cur_video->cur_seq_no;
}
if (!new_cur_video->last_seq_no) {
new_cur_video->last_seq_no = calc_max_seg_no(new_cur_video, s->priv_data);
}
av_log(0, AV_LOG_INFO, "dash switc old:[%d:%d] - new:[%d:%d]\n",
old_video_idx, cur_video->bandwidth, c->cur_video_idx, new_cur_video->bandwidth);
return new_cur_video;
}
static int dash_bw_find_best_speed_representation(DASHContext *c, int64_t average_speed)
{
for (int i = c->n_videos - 1; i > 0; i--) {
struct representation *pls = c->videos[i];
if (pls && average_speed >= pls->bandwidth) {
return i;
}
}
return 0;
}
static void dash_update_bandwidth_info(AVFormatContext *s, struct representation *pls)
{
int seg_dur = 0;
DASHContext *c = s->priv_data;
c->band_width.playable_msec = c->playable_msec;
if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
seg_dur = pls->timelines[0]->duration * 1000 / pls->fragment_timescale;
} else if (pls->fragment_duration > 0 && pls->fragment_timescale > 0) {
seg_dur = pls->fragment_duration * 1000 / pls->fragment_timescale;
} else if (c->media_presentation_duration > 0 && pls->n_fragments > 0) {
seg_dur = c->media_presentation_duration * 1000 / pls->n_fragments;
}
if (seg_dur < 10 * 1000) {
seg_dur = 10 * 1000;
}
c->band_width.playable_threshold_download = seg_dur * c->bw_adjust_target;
c->band_width.target_duration = seg_dur;
if (c->enable_raw_data) {
c->band_width.state = DASH_BW_Normal; /* raw mode, playable_msec is invalid, so switch bw use cnt */
}
}
static struct representation *dash_check_switch_for_raw_mode(AVFormatContext *s,
struct representation *cur_video, int new_higher, int new_lower)
{
DASHContext *c = s->priv_data;
if (new_lower < c->cur_video_idx) {
c->band_width.increase_cnt = 0;
av_log(NULL, AV_LOG_INFO, "raw mode, reduce new_lower:%d, cur:%d\n", new_lower, c->cur_video_idx);
cur_video = dash_switch_representation(s, cur_video, new_lower);
} else if (new_higher > c->cur_video_idx) {
c->band_width.increase_cnt++;
av_log(NULL, AV_LOG_INFO, "Begin, incrase cnt:%d new_higher:%d, cur:%d\n", c->band_width.increase_cnt, new_higher, c->cur_video_idx);
if (c->band_width.increase_cnt >= 3) {
c->band_width.increase_cnt = 0;
cur_video = dash_switch_representation(s, cur_video, c->cur_video_idx + 1);
}
} else {
c->band_width.increase_cnt = 0;
}
return cur_video;
}
static struct representation *dash_check_representation(AVFormatContext *s, struct representation *cur_video)
{
DASHContext *c = s->priv_data;
if (cur_video == NULL || cur_video->input == NULL) {
return cur_video;
}
if (c->n_videos <= 1 || c->band_width.state == DASH_BW_FIX) {
return cur_video;
}
if (c->playable_msec <= 0 && c->enable_raw_data == 0) {
return cur_video;
}
int64_t average_speed = -1;
int64_t last_speed = -1;
dash_get_download_speed(cur_video->input, &average_speed, &last_speed);
int new_higher = dash_bw_find_best_speed_representation(c, average_speed * 8 / 10);
int new_lower = dash_bw_find_best_speed_representation(c, average_speed);
dash_update_bandwidth_info(s, cur_video);
DashBandWidth *dashBandWidth = &c->band_width;
switch (dashBandWidth->state) {
case DASH_BW_Begin: {
if (new_lower == 0) {
dashBandWidth->state = DASH_BW_Underload;
av_log(NULL, AV_LOG_INFO, "Begin to Underload, old playable_msec:%d, average_speed:%" PRId64 "\n",
dashBandWidth->playable_msec, average_speed);
cur_video = dash_switch_representation(s, cur_video, 0);
break;
}
if (new_lower < c->cur_video_idx) {
av_log(NULL, AV_LOG_INFO, "Begin, new_lower:%d\n", new_lower);
cur_video = dash_switch_representation(s, cur_video, new_lower);
}
if (dashBandWidth->playable_msec > dashBandWidth->playable_threshold_download) {
dashBandWidth->state = DASH_BW_Normal;
av_log(NULL, AV_LOG_INFO, "Begin to Normal\n");
}
break;
}
case DASH_BW_Normal: {
if (c->enable_raw_data) {
cur_video = dash_check_switch_for_raw_mode(s, cur_video, new_higher, new_lower);
break;
}
if (dashBandWidth->playable_msec <
dashBandWidth->playable_threshold_download - dashBandWidth->target_duration) {
dashBandWidth->state = DASH_BW_Underload;
av_log(NULL, AV_LOG_WARNING, "Normal to Underload, old playable_msec:%d less than threshold_download - target, "
"average_speed:%" PRId64 "\n",
dashBandWidth->playable_msec, average_speed);
cur_video = dash_switch_representation(s, cur_video, 0);
break;
}
if (new_lower < c->cur_video_idx) {
if (new_lower == 0) {
dashBandWidth->state = DASH_BW_Underload;
av_log(NULL, AV_LOG_WARNING, "Normal to Underload, old playable_msec:%d, average_speed:%" PRId64 "\n",
dashBandWidth->playable_msec, average_speed);
cur_video = dash_switch_representation(s, cur_video, 0);
} else {
av_log(NULL, AV_LOG_INFO, "Normal to lower Normal, new_lower:%d\n", new_lower);
cur_video = dash_switch_representation(s, cur_video, new_lower);
}
break;
}
if (dashBandWidth->playable_msec >= dashBandWidth->playable_threshold_download) {
if (new_higher > c->cur_video_idx) {
av_log(NULL, AV_LOG_INFO, "Normal to higher Normal, new_higher:%d\n", new_higher);
cur_video = dash_switch_representation(s, cur_video, new_higher);
}
break;
}
break;
}
case DASH_BW_Underload: {
if (dashBandWidth->playable_msec > dashBandWidth->playable_threshold_download) {
if (new_higher == c->cur_video_idx) {
av_log(NULL, AV_LOG_INFO,"Underload to Normal, new varrant = used :%d\n", c->cur_video_idx);
dashBandWidth->state = DASH_BW_Normal;
} else {
av_log(NULL, AV_LOG_INFO,"Underload to Normal, new_higher:%d\n", new_higher);
cur_video = dash_switch_representation(s, cur_video, new_higher);
dashBandWidth->state = DASH_BW_Normal;
}
}
break;
}
default:
break;
}
return cur_video;
}
static void key_id_to_hex(const char* input, uint8_t* output, size_t outputSize) {
if (input == NULL) {
return;
}
size_t inputLength = strlen(input);
size_t outputIndex = 0;
for (size_t i = 0; i < inputLength; i += 2) {
if (input[i] == '-') {
i++;
}
sscanf(&input[i], "%2hhx", &output[outputIndex]);
outputIndex++;
}
}
static void add_encryption_init_info(AVFormatContext *s, struct representation *pls)
{
// key id and pssh data
uint8_t system_id[16] = {0};
uint8_t key_id[16] = {0};
uint8_t *pssh = NULL;
size_t pssh_size = 0;
if (pls->content_protection.key_id) {
key_id_to_hex(pls->content_protection.key_id, key_id, 16);
}
if (pls->content_protection.system_id) {
int len = strlen("urn:uuid:");
key_id_to_hex(pls->content_protection.system_id + len, system_id, 16);
}
if (pls->content_protection.pssh) {
size_t size = AV_BASE64_DECODE_SIZE(strlen(pls->content_protection.pssh));
pssh = av_malloc(size);
pssh_size = av_base64_decode(pssh, pls->content_protection.pssh, size);
}
AVEncryptionInitInfo *info = av_encryption_init_info_alloc(16, 1, 16, pssh_size);
memcpy(info->system_id, system_id, 16);
memcpy(info->key_ids[0], key_id, 16);
info->data_is_pssh = 1;
if (pssh_size) {
memcpy(info->data, pssh, pssh_size);
}
if (pssh)
av_free(pssh);
// check if the side data is already added, if so, append the new one to the end
int old_side_data_size = 0;
uint8_t* old_side_data = av_stream_get_side_data(s->streams[pls->stream_index], AV_PKT_DATA_ENCRYPTION_INIT_INFO, &old_side_data_size);
AVEncryptionInitInfo *old_init_info = NULL;
if (old_side_data) {
old_init_info = av_encryption_init_info_get_side_data(old_side_data, old_side_data_size);
if (old_init_info) {
for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
if (!cur->next) {
cur->next = info;
break;
}
}
info = old_init_info;
}
}
int new_side_data_size = 0;
uint8_t* new_side_data = av_encryption_init_info_add_side_data(info, &new_side_data_size);
av_stream_add_side_data(s->streams[pls->stream_index], AV_PKT_DATA_ENCRYPTION_INIT_INFO, new_side_data, new_side_data_size);
av_encryption_init_info_free(info);
}
static int dash_read_header(AVFormatContext *s)
{
void *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb;
DASHContext *c = s->priv_data;
int ret = 0;
int stream_index = 0;
int i;
c->interrupt_callback = &s->interrupt_callback;
// if the URL context is good, read important options we must broker later
if (u) {
update_options(&c->user_agent, "user-agent", u);
update_options(&c->cookies, "cookies", u);
update_options(&c->headers, "headers", u);
}
c->parse_seg_base = am_getconfig_bool_def("media.player.dash.parse_seg_base", 1);
if ((ret = parse_manifest(s, s->filename, s->pb)) < 0)
goto fail;
if ((ret = save_avio_options(s)) < 0)
goto fail;
/* If this isn't a live stream, fill the total duration of the
* stream. */
if (!c->is_live) {
s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
}
sort_representation(c);
c->quality = am_getconfig_int_def("media.amplayer.dash.quality", START_MODE_FIX_HIGH);
if (c->quality < START_MODE_FIX_HIGH || c->quality > START_MODE_AUTO_LOW) {
c->quality = START_MODE_FIX_HIGH;
av_log(0, AV_LOG_WARNING, "get start mode from prop is invalid, so set it to default:%d\n", START_MODE_FIX_HIGH);
}
if (c->n_videos > 1) {
if (c->quality == START_MODE_FIX_HIGH || c->quality == START_MODE_AUTO_HIGH) {
c->cur_video_idx = c->n_videos - 1;
} else {
c->cur_video_idx = 0;
}
} else {
c->cur_video_idx = -1;
}
av_log(0, AV_LOG_INFO, "set start mode:%d, cur_idx:%d\n", c->quality, c->cur_video_idx);
/* Open the demuxer for video and audio components if available */
for (i = 0; i < c->n_videos; i++) {
struct representation *cur_video = c->videos[i];
if (c->cur_video_idx >= 0) {
cur_video = c->videos[c->cur_video_idx];
c->is_mp2t = cur_video->file_mime_type == MIME_TYPE_MP2T ? 1 : 0;
c->is_mp4 = cur_video->file_mime_type == MIME_TYPE_MP4 ? 1 : 0;
if (cur_video->ctx != NULL) {
av_log(0, AV_LOG_INFO, "only open cur demux\n");
break;
}
}
ret = open_demux_for_component(s, cur_video);
if (ret)
goto fail;
cur_video->stream_index = stream_index;
++stream_index;
}
for (i = 0; i < c->n_audios; i++) {
struct representation *cur_audio = c->audios[i];
ret = open_demux_for_component(s, cur_audio);
if (ret)
goto fail;
cur_audio->stream_index = stream_index;
++stream_index;
}
if (!stream_index) {
ret = AVERROR_INVALIDDATA;
goto fail;
}
/* Create a program */
if (!ret) {
AVProgram *program;
program = av_new_program(s, 0);
if (!program) {
goto fail;
}
for (i = 0; i < c->n_videos; i++) {
struct representation *pls = c->videos[i];
av_program_add_stream_index(s, 0, pls->stream_index);
pls->assoc_stream = s->streams[pls->stream_index];
if (pls->bandwidth > 0)
av_dict_set_int(&pls->assoc_stream->metadata, "variant_bitrate", pls->bandwidth, 0);
if (pls->id[0])
av_dict_set(&pls->assoc_stream->metadata, "id", pls->id, 0);
if (pls->content_protection.is_valid)
add_encryption_init_info(s, pls);
}
for (i = 0; i < c->n_audios; i++) {
struct representation *pls = c->audios[i];
av_program_add_stream_index(s, 0, pls->stream_index);
pls->assoc_stream = s->streams[pls->stream_index];
if (pls->bandwidth > 0)
av_dict_set_int(&pls->assoc_stream->metadata, "variant_bitrate", pls->bandwidth, 0);
if (pls->id[0])
av_dict_set(&pls->assoc_stream->metadata, "id", pls->id, 0);
if (pls->content_protection.is_valid)
add_encryption_init_info(s, pls);
}
}
dash_bandwidth_init(s);
return 0;
fail:
return ret;
}
static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
{
int i, j;
for (i = 0; i < n; i++) {
struct representation *pls = p[i];
int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
if (needed && !pls->ctx) {
pls->cur_seg_offset = 0;
pls->init_sec_buf_read_offset = 0;
/* Catch up */
for (j = 0; j < n; j++) {
pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
}
reopen_demux_for_component(s, pls);
av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
} else if (!needed && pls->ctx) {
close_demux_for_component(pls);
if (pls->input)
ff_format_io_close(pls->parent, &pls->input);
av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
}
}
}
static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
{
DASHContext *c = s->priv_data;
int ret = 0, i;
int64_t mints = 0;
struct representation *cur = NULL;
//recheck_discard_flags(s, c->videos, c->n_videos);
recheck_discard_flags(s, c->audios, c->n_audios);
int64_t minVideoTs = 0;
for (i = 0; i < c->n_videos; i++) {
struct representation *pls = c->videos[i];
if (!pls->ctx)
continue;
if (!cur || pls->cur_timestamp < mints) {
cur = pls;
mints = pls->cur_timestamp;
}
if (!minVideoTs || pls->cur_timestamp < minVideoTs) {
minVideoTs = pls->cur_timestamp;
}
}
for (i = 0; i < c->n_audios; i++) {
struct representation *pls = c->audios[i];
if (!pls->ctx)
continue;
if (!cur || pls->cur_timestamp < mints) {
cur = pls;
mints = pls->cur_timestamp;
}
}
int needDropAudio = 0;
if (c->first_drop_packet_after_seek) {
if (cur->type == AVMEDIA_TYPE_AUDIO && minVideoTs - mints > 90 * 100) {
c->first_drop_packet_after_seek = 0;
needDropAudio = 1;
}
}
if (!cur) {
return AVERROR_INVALIDDATA;
}
while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
if (c->enable_raw_data) {
av_init_packet(pkt);
int size_packet = 188 * 100;
av_new_packet(pkt, size_packet);
ret = avio_read(&cur->pb, pkt->data, size_packet);
if (ret <= 0) {
av_packet_unref(pkt);
} else {
pkt->size = ret;
}
} else {
ret = av_read_frame(cur->ctx, pkt);
}
if (ret >= 0) {
/* If we got a packet, return it */
cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
if (c->is_mp2t && c->enable_raw_data == 0) {
/* dash + mp2t and is es mode, so pkt stream idx is same to ori mpegtsdemux */
} else {
pkt->stream_index = cur->stream_index;
}
if (needDropAudio && cur->type == AVMEDIA_TYPE_AUDIO && minVideoTs - cur->cur_timestamp > 90 * 100) {
ret = 0;
av_packet_unref(pkt);
continue;
}
return 0;
}
if (cur->is_restart_needed) {
cur->cur_seg_offset = 0;
cur->init_sec_buf_read_offset = 0;
if (cur->type == AVMEDIA_TYPE_VIDEO) {
cur = dash_check_representation(s, cur);
}
if (cur->input)
ff_format_io_close(cur->parent, &cur->input);
ret = reopen_demux_for_component(s, cur);
cur->is_restart_needed = 0;
}
}
return AVERROR_EOF;
}
static int dash_close(AVFormatContext *s)
{
DASHContext *c = s->priv_data;
free_audio_list(c);
free_video_list(c);
av_freep(&c->cookies);
av_freep(&c->user_agent);
av_dict_free(&c->avio_opts);
av_freep(&c->base_url);
return 0;
}
static int dash_single_stream_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec)
{
int i = 0;
int64_t file_size = -1;
int64_t seek_file_pos = -1;
DASHContext *c = s->priv_data;
if (c->seek_seq_no >= 0) {
return c->seek_seq_no;
}
c->seek_seq_no = pls->cur_seq_no;
/* single stream, calc seek position from index range */
file_size = pls->fragments[pls->n_fragments - 1]->url_offset + pls->fragments[pls->n_fragments - 1]->size;
if (file_size <= 0 || c->media_presentation_duration == 0) {
c->seek_seq_no = pls->cur_seq_no;;
goto end;
}
seek_file_pos = seek_pos_msec * file_size / (c->media_presentation_duration * 1000);
if (seek_file_pos <= pls->fragments[0]->url_offset) {
c->seek_seq_no = pls->first_seq_no;
} else if (seek_file_pos >= pls->fragments[pls->n_fragments - 1]->url_offset) {
c->seek_seq_no = pls->n_fragments - 1;
} else {
for (i = 0; i < pls->n_fragments - 1; i++) {
if (seek_file_pos > pls->fragments[i]->url_offset && seek_file_pos < pls->fragments[i + 1]->url_offset) {
c->seek_seq_no = i;
}
}
}
end:
return c->seek_seq_no;
}
static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
{
int ret = 0;
int i = 0;
int j = 0;
int64_t duration = 0;
av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d%s\n",
seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
// single fragment mode
if (pls->n_fragments == 1) {
pls->cur_timestamp = 0;
pls->cur_seg_offset = 0;
if (dry_run)
return 0;
ff_read_frame_flush(pls->ctx);
return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
}
if (pls->input)
ff_format_io_close(pls->parent, &pls->input);
// find the nearest fragment
if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
int64_t num = pls->first_seq_no;
av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
"last_seq_no[%"PRId64"], playlist %d.\n",
(int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
for (i = 0; i < pls->n_timelines; i++) {
if (pls->timelines[i]->starttime > 0) {
duration = pls->timelines[i]->starttime;
}
duration += pls->timelines[i]->duration;
if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
goto set_seq_num;
}
for (j = 0; j < pls->timelines[i]->repeat; j++) {
duration += pls->timelines[i]->duration;
num++;
if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
goto set_seq_num;
}
}
num++;
}
set_seq_num:
pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
(int64_t)pls->cur_seq_no, (int)pls->rep_idx);
} else if (pls->fragment_duration > 0) {
pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
} else {
if (pls->segBase.index_range != NULL && pls->n_fragments > 1) {
pls->cur_seq_no = dash_single_stream_seek(s, pls, seek_pos_msec);
av_log(0, AV_LOG_INFO, "dash seek, change cur_seg_no to :%lld\n", pls->cur_seq_no);
} else {
av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
pls->cur_seq_no = pls->first_seq_no;
}
}
pls->cur_timestamp = 0;
pls->cur_seg_offset = 0;
pls->init_sec_buf_read_offset = 0;
ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
return ret;
}
static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
{
int ret = 0, i;
DASHContext *c = s->priv_data;
int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
s->streams[stream_index]->time_base.den,
flags & AVSEEK_FLAG_BACKWARD ?
AV_ROUND_DOWN : AV_ROUND_UP);
if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
return AVERROR(ENOSYS);
c->seek_seq_no = -1; // seek seq no will change it in dash_seek
c->first_drop_packet_after_seek = 1;
/* Seek in discarded streams with dry_run=1 to avoid reopening them */
for (i = 0; i < c->n_videos; i++) {
if (!ret)
ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
}
for (i = 0; i < c->n_audios; i++) {
if (!ret)
ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
}
return ret;
}
static int dash_probe(AVProbeData *p)
{
if (!av_stristr(p->buf, "<MPD"))
return 0;
if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
av_stristr(p->buf, "dash:profile:isoff-main:2011")) {
return AVPROBE_SCORE_MAX;
}
if (av_stristr(p->buf, "dash:profile")) {
return AVPROBE_SCORE_MAX;
}
return 0;
}
#define OFFSET(x) offsetof(DASHContext, x)
#define FLAGS AV_OPT_FLAG_DECODING_PARAM
static const AVOption dash_options[] = {
{"playable_msec", "", OFFSET(playable_msec), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
{"dash_bw_adjust_target", "bandwith adjust target dur", OFFSET(bw_adjust_target), AV_OPT_TYPE_INT,
{ .i64 = 1 }, 1, INT_MAX, .flags = FLAGS },
{"allowed_extensions", "List of file extensions that dash is allowed to access",
OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
{.str = "aac,m4a,m4s,m4v,mov,mp4"},
INT_MIN, INT_MAX, FLAGS},
{"dash_quality", "", OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, FLAGS },
{"dash_is_mp4", "", OFFSET(is_mp4), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
{"dash_is_ts", "", OFFSET(is_mp2t), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
{"enable_raw_data", "", OFFSET(enable_raw_data), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
{NULL}
};
static const AVClass dash_class = {
.class_name = "dash",
.item_name = av_default_item_name,
.option = dash_options,
.version = LIBAVUTIL_VERSION_INT,
};
AVInputFormat ff_dash_demuxer = {
.name = "dash",
.long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
.priv_class = &dash_class,
.priv_data_size = sizeof(DASHContext),
.read_probe = dash_probe,
.read_header = dash_read_header,
.read_packet = dash_read_packet,
.read_close = dash_close,
.read_seek = dash_read_seek,
.flags = AVFMT_NO_BYTE_SEEK,
};