Debian Codegraph @

ext/adaptivedemux2/dash/gstmpdclient.c

raw
1 /* GStreamer
2 *
3 * Copyright (C) 2019 Collabora Ltd.
4 * Author: Stéphane Cerveau <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "gstmpdclient.h"
23 #include "gstmpdparser.h"
24
25 GST_DEBUG_CATEGORY_STATIC (gst_dash_mpd_client_debug);
26 #undef GST_CAT_DEFAULT
27 #define GST_CAT_DEFAULT gst_dash_mpd_client_debug
28
29 G_DEFINE_TYPE (GstMPDClient2, gst_mpd_client2, GST_TYPE_OBJECT);
30
31 static GstMPDSegmentBaseNode *gst_mpd_client2_get_segment_base (GstMPDPeriodNode
32 * Period, GstMPDAdaptationSetNode * AdaptationSet,
33 GstMPDRepresentationNode * Representation);
34 static GstMPDSegmentListNode *gst_mpd_client2_get_segment_list (GstMPDClient2 *
35 client, GstMPDPeriodNode * Period, GstMPDAdaptationSetNode * AdaptationSet,
36 GstMPDRepresentationNode * Representation);
37 /* Segments */
38 static guint gst_mpd_client2_get_segments_counts (GstMPDClient2 * client,
39 GstActiveStream * stream);
40
41 static GList *gst_mpd_client2_fetch_external_periods (GstMPDClient2 * client,
42 GstMPDPeriodNode * period_node);
43 static GList *gst_mpd_client2_fetch_external_adaptation_set (GstMPDClient2 *
44 client, GstMPDPeriodNode * period, GstMPDAdaptationSetNode * adapt_set);
45
46 static GstMPDRepresentationNode
47 * gst_mpd_client2_get_lowest_representation (GList * Representations);
48 static GstStreamPeriod *gst_mpd_client2_get_stream_period (GstMPDClient2 *
49 client);
50
51 typedef GstMPDNode *(*MpdClientStringIDFilter) (GList * list, gchar * data);
52 typedef GstMPDNode *(*MpdClientIDFilter) (GList * list, guint data);
53
54 static GstMPDNode *
55 gst_mpd_client2_get_period_with_id (GList * periods, gchar * period_id)
56 {
57 GstMPDPeriodNode *period;
58 GList *list = NULL;
59
60 for (list = g_list_first (periods); list; list = g_list_next (list)) {
61 period = (GstMPDPeriodNode *) list->data;
62 if (!g_strcmp0 (period->id, period_id))
63 return GST_MPD_NODE (period);
64 }
65 return NULL;
66 }
67
68 static GstMPDNode *
69 gst_mpd_client2_get_adaptation_set_with_id (GList * adaptation_sets, guint id)
70 {
71 GstMPDAdaptationSetNode *adaptation_set;
72 GList *list = NULL;
73
74 for (list = g_list_first (adaptation_sets); list; list = g_list_next (list)) {
75 adaptation_set = (GstMPDAdaptationSetNode *) list->data;
76 if (adaptation_set->id == id)
77 return GST_MPD_NODE (adaptation_set);
78 }
79 return NULL;
80 }
81
82 GstMPDRepresentationNode *
83 gst_mpd_client2_get_representation_with_id (GList * representations,
84 gchar * rep_id)
85 {
86 GstMPDRepresentationNode *representation;
87 GList *list = NULL;
88
89 for (list = g_list_first (representations); list; list = g_list_next (list)) {
90 representation = (GstMPDRepresentationNode *) list->data;
91 if (!g_strcmp0 (representation->id, rep_id))
92 return GST_MPD_REPRESENTATION_NODE (representation);
93 }
94 return NULL;
95 }
96
97 static GstMPDNode *
98 gst_mpd_client2_get_representation_with_id_filter (GList * representations,
99 gchar * rep_id)
100 {
101 GstMPDRepresentationNode *representation =
102 gst_mpd_client2_get_representation_with_id (representations, rep_id);
103
104 if (representation != NULL)
105 return GST_MPD_NODE (representation);
106
107 return NULL;
108 }
109
110 static gchar *
111 _generate_new_string_id (GList * list, const gchar * tuple,
112 MpdClientStringIDFilter filter)
113 {
114 guint i = 0;
115 gchar *id = NULL;
116 GstMPDNode *node;
117 do {
118 g_free (id);
119 id = g_strdup_printf (tuple, i);
120 node = filter (list, id);
121 i++;
122 } while (node);
123
124 return id;
125 }
126
127 static guint
128 _generate_new_id (GList * list, MpdClientIDFilter filter)
129 {
130 guint id = 0;
131 GstMPDNode *node;
132 do {
133 node = filter (list, id);
134 id++;
135 } while (node);
136
137 return id;
138 }
139
140 static GstMPDRepresentationNode *
141 gst_mpd_client2_get_lowest_representation (GList * Representations)
142 {
143 GList *list = NULL;
144 GstMPDRepresentationNode *rep = NULL;
145 GstMPDRepresentationNode *lowest = NULL;
146
147 if (Representations == NULL)
148 return NULL;
149
150 for (list = g_list_first (Representations); list; list = g_list_next (list)) {
151 rep = (GstMPDRepresentationNode *) list->data;
152 if (rep && (!lowest || rep->bandwidth < lowest->bandwidth)) {
153 lowest = rep;
154 }
155 }
156
157 return lowest;
158 }
159
160 #if 0
161 static GstMPDRepresentationNode *
162 gst_mpdparser_get_highest_representation (GList * Representations)
163 {
164 GList *list = NULL;
165
166 if (Representations == NULL)
167 return NULL;
168
169 list = g_list_last (Representations);
170
171 return list ? (GstMPDRepresentationNode *) list->data : NULL;
172 }
173
174 static GstMPDRepresentationNode *
175 gst_mpdparser_get_representation_with_max_bandwidth (GList * Representations,
176 gint max_bandwidth)
177 {
178 GList *list = NULL;
179 GstMPDRepresentationNode *representation, *best_rep = NULL;
180
181 if (Representations == NULL)
182 return NULL;
183
184 if (max_bandwidth <= 0) /* 0 => get highest representation available */
185 return gst_mpdparser_get_highest_representation (Representations);
186
187 for (list = g_list_first (Representations); list; list = g_list_next (list)) {
188 representation = (GstMPDRepresentationNode *) list->data;
189 if (representation && representation->bandwidth <= max_bandwidth) {
190 best_rep = representation;
191 }
192 }
193
194 return best_rep;
195 }
196 #endif
197
198 static GstMPDSegmentListNode *
199 gst_mpd_client2_fetch_external_segment_list (GstMPDClient2 * client,
200 GstMPDPeriodNode * Period,
201 GstMPDAdaptationSetNode * AdaptationSet,
202 GstMPDRepresentationNode * Representation,
203 GstMPDSegmentListNode * parent, GstMPDSegmentListNode * segment_list)
204 {
205 DownloadRequest *download;
206 GstBuffer *segment_list_buffer = NULL;
207 GError *err = NULL;
208
209 GstUri *base_uri, *uri;
210 gchar *query = NULL;
211 gchar *uri_string;
212 GstMPDSegmentListNode *new_segment_list = NULL;
213
214 /* ISO/IEC 23009-1:2014 5.5.3 4)
215 * Remove nodes that resolve to nothing when resolving
216 */
217 if (strcmp (segment_list->xlink_href,
218 "urn:mpeg:dash:resolve-to-zero:2013") == 0) {
219 return NULL;
220 }
221
222 if (!client->download_helper) {
223 return NULL;
224 }
225
226 /* Build absolute URI */
227
228 /* Get base URI at the MPD level */
229 base_uri =
230 gst_uri_from_string (client->mpd_base_uri ? client->
231 mpd_base_uri : client->mpd_uri);
232
233 /* combine a BaseURL at the MPD level with the current base url */
234 base_uri =
235 gst_mpd_helper_combine_urls (base_uri, client->mpd_root_node->BaseURLs,
236 &query, 0);
237
238 /* combine a BaseURL at the Period level with the current base url */
239 base_uri =
240 gst_mpd_helper_combine_urls (base_uri, Period->BaseURLs, &query, 0);
241
242 if (AdaptationSet) {
243 /* combine a BaseURL at the AdaptationSet level with the current base url */
244 base_uri =
245 gst_mpd_helper_combine_urls (base_uri, AdaptationSet->BaseURLs, &query,
246 0);
247
248 if (Representation) {
249 /* combine a BaseURL at the Representation level with the current base url */
250 base_uri =
251 gst_mpd_helper_combine_urls (base_uri, Representation->BaseURLs,
252 &query, 0);
253 }
254 }
255
256 uri = gst_uri_from_string_with_base (base_uri, segment_list->xlink_href);
257 if (query)
258 gst_uri_set_query_string (uri, query);
259 g_free (query);
260 uri_string = gst_uri_to_string (uri);
261 gst_uri_unref (base_uri);
262 gst_uri_unref (uri);
263
264 download =
265 downloadhelper_fetch_uri (client->download_helper,
266 uri_string, client->mpd_uri,
267 DOWNLOAD_FLAG_COMPRESS | DOWNLOAD_FLAG_FORCE_REFRESH, &err);
268 g_free (uri_string);
269
270 if (!download) {
271 GST_ERROR ("Failed to download external SegmentList node at '%s': %s",
272 segment_list->xlink_href, err->message);
273 g_clear_error (&err);
274 return NULL;
275 }
276
277 segment_list_buffer = download_request_take_buffer (download);
278 download_request_unref (download);
279
280 if (segment_list_buffer) {
281 GstMapInfo map;
282
283 gst_buffer_map (segment_list_buffer, &map, GST_MAP_READ);
284 new_segment_list =
285 gst_mpdparser_get_external_segment_list ((const gchar *) map.data,
286 map.size, parent);
287
288 gst_buffer_unmap (segment_list_buffer, &map);
289 gst_buffer_unref (segment_list_buffer);
290 }
291
292 return new_segment_list;
293 }
294
295 static GstMPDSegmentBaseNode *
296 gst_mpd_client2_get_segment_base (GstMPDPeriodNode * Period,
297 GstMPDAdaptationSetNode * AdaptationSet,
298 GstMPDRepresentationNode * Representation)
299 {
300 GstMPDSegmentBaseNode *SegmentBase = NULL;
301
302 if (Representation && Representation->SegmentBase) {
303 SegmentBase = Representation->SegmentBase;
304 } else if (AdaptationSet && AdaptationSet->SegmentBase) {
305 SegmentBase = AdaptationSet->SegmentBase;
306 } else if (Period && Period->SegmentBase) {
307 SegmentBase = Period->SegmentBase;
308 }
309 /* the SegmentBase element could be encoded also inside a SegmentList element */
310 if (SegmentBase == NULL) {
311 if (Representation && Representation->SegmentList
312 && GST_MPD_MULT_SEGMENT_BASE_NODE (Representation->SegmentList)
313 && GST_MPD_MULT_SEGMENT_BASE_NODE (Representation->
314 SegmentList)->SegmentBase) {
315 SegmentBase =
316 GST_MPD_MULT_SEGMENT_BASE_NODE (Representation->
317 SegmentList)->SegmentBase;
318 } else if (AdaptationSet && AdaptationSet->SegmentList
319 && GST_MPD_MULT_SEGMENT_BASE_NODE (AdaptationSet->SegmentList)
320 && GST_MPD_MULT_SEGMENT_BASE_NODE (AdaptationSet->
321 SegmentList)->SegmentBase) {
322 SegmentBase =
323 GST_MPD_MULT_SEGMENT_BASE_NODE (AdaptationSet->
324 SegmentList)->SegmentBase;
325 } else if (Period && Period->SegmentList
326 && GST_MPD_MULT_SEGMENT_BASE_NODE (Period->SegmentList)
327 && GST_MPD_MULT_SEGMENT_BASE_NODE (Period->SegmentList)->SegmentBase) {
328 SegmentBase =
329 GST_MPD_MULT_SEGMENT_BASE_NODE (Period->SegmentList)->SegmentBase;
330 }
331 }
332
333 return SegmentBase;
334 }
335
336 static GstMPDSegmentListNode *
337 gst_mpd_client2_get_segment_list (GstMPDClient2 * client,
338 GstMPDPeriodNode * Period, GstMPDAdaptationSetNode * AdaptationSet,
339 GstMPDRepresentationNode * Representation)
340 {
341 GstMPDSegmentListNode **SegmentList;
342 GstMPDSegmentListNode *ParentSegmentList = NULL;
343
344 if (Representation && Representation->SegmentList) {
345 SegmentList = &Representation->SegmentList;
346 ParentSegmentList = AdaptationSet->SegmentList;
347 } else if (AdaptationSet && AdaptationSet->SegmentList) {
348 SegmentList = &AdaptationSet->SegmentList;
349 ParentSegmentList = Period->SegmentList;
350 Representation = NULL;
351 } else {
352 Representation = NULL;
353 AdaptationSet = NULL;
354 SegmentList = &Period->SegmentList;
355 }
356
357 /* Resolve external segment list here. */
358 if (*SegmentList && (*SegmentList)->xlink_href) {
359 GstMPDSegmentListNode *new_segment_list;
360
361 /* TODO: Use SegmentList of parent if
362 * - Parent has its own SegmentList
363 * - Fail to get SegmentList from external xml
364 */
365 new_segment_list =
366 gst_mpd_client2_fetch_external_segment_list (client, Period,
367 AdaptationSet, Representation, ParentSegmentList, *SegmentList);
368
369 gst_mpd_segment_list_node_free (*SegmentList);
370 *SegmentList = new_segment_list;
371 }
372
373 return *SegmentList;
374 }
375
376 static GstClockTime
377 gst_mpd_client2_get_segment_duration (GstMPDClient2 * client,
378 GstActiveStream * stream, guint64 * scale_dur)
379 {
380 GstStreamPeriod *stream_period;
381 GstMPDMultSegmentBaseNode *base = NULL;
382 GstClockTime duration = 0;
383
384 g_return_val_if_fail (stream != NULL, GST_CLOCK_TIME_NONE);
385 stream_period = gst_mpd_client2_get_stream_period (client);
386 g_return_val_if_fail (stream_period != NULL, GST_CLOCK_TIME_NONE);
387
388 if (stream->cur_segment_list) {
389 base = GST_MPD_MULT_SEGMENT_BASE_NODE (stream->cur_segment_list);
390 } else if (stream->cur_seg_template) {
391 base = GST_MPD_MULT_SEGMENT_BASE_NODE (stream->cur_seg_template);
392 }
393
394 if (base == NULL || base->SegmentBase == NULL) {
395 /* this may happen when we have a single segment */
396 duration = stream_period->duration;
397 if (scale_dur)
398 *scale_dur = duration;
399 } else {
400 /* duration is guint so this cannot overflow */
401 duration = base->duration * GST_SECOND;
402 if (scale_dur)
403 *scale_dur = duration;
404 duration /= base->SegmentBase->timescale;
405 }
406
407 return duration;
408 }
409
410 void
411 gst_mpd_client2_active_streams_free (GstMPDClient2 * client)
412 {
413 if (client->active_streams) {
414 g_list_foreach (client->active_streams,
415 (GFunc) gst_mpdparser_free_active_stream, NULL);
416 g_list_free (client->active_streams);
417 client->active_streams = NULL;
418 }
419 }
420
421 static void
422 gst_mpd_client2_finalize (GObject * object)
423 {
424 GstMPDClient2 *client = GST_MPD_CLIENT (object);
425
426 if (client->mpd_root_node)
427 gst_mpd_root_node_free (client->mpd_root_node);
428
429 if (client->periods) {
430 g_list_free_full (client->periods,
431 (GDestroyNotify) gst_mpdparser_free_stream_period);
432 }
433
434 gst_mpd_client2_active_streams_free (client);
435
436 g_free (client->mpd_uri);
437 client->mpd_uri = NULL;
438 g_free (client->mpd_base_uri);
439 client->mpd_base_uri = NULL;
440
441 G_OBJECT_CLASS (gst_mpd_client2_parent_class)->finalize (object);
442 }
443
444 static void
445 gst_mpd_client2_class_init (GstMPDClient2Class * klass)
446 {
447 GObjectClass *object_class = G_OBJECT_CLASS (klass);
448 object_class->finalize = gst_mpd_client2_finalize;
449 }
450
451 static void
452 gst_mpd_client2_init (GstMPDClient2 * client)
453 {
454 }
455
456 GstMPDClient2 *
457 gst_mpd_client2_new (void)
458 {
459 GST_DEBUG_CATEGORY_INIT (gst_dash_mpd_client_debug, "dashmpdclient2", 0,
460 "DashmMpdClient");
461 return g_object_new (GST_TYPE_MPD_CLIENT, NULL);
462 }
463
464 GstMPDClient2 *
465 gst_mpd_client2_new_static (void)
466 {
467 GstMPDClient2 *client = gst_mpd_client2_new ();
468
469 client->mpd_root_node = gst_mpd_root_node_new ();
470 client->mpd_root_node->default_namespace =
471 g_strdup ("urn:mpeg:dash:schema:mpd:2011");
472 client->mpd_root_node->profiles =
473 g_strdup ("urn:mpeg:dash:profile:isoff-main:2011");
474 client->mpd_root_node->type = GST_MPD_FILE_TYPE_STATIC;
475 client->mpd_root_node->minBufferTime = 1500;
476
477 return client;
478 }
479
480 void
481 gst_mpd_client2_free (GstMPDClient2 * client)
482 {
483 if (client)
484 gst_object_unref (client);
485 }
486
487 gboolean
488 gst_mpd_client2_parse (GstMPDClient2 * client, const gchar * data, gint size)
489 {
490 gboolean ret = FALSE;
491
492
493 ret = gst_mpdparser_get_mpd_root_node (&client->mpd_root_node, data, size);
494
495 if (ret) {
496 gst_mpd_client2_check_profiles (client);
497 gst_mpd_client2_fetch_on_load_external_resources (client);
498 }
499
500 return ret;
501 }
502
503
504 gboolean
505 gst_mpd_client2_get_xml_content (GstMPDClient2 * client, gchar ** data,
506 gint * size)
507 {
508 gboolean ret = FALSE;
509
510 g_return_val_if_fail (client != NULL, ret);
511 g_return_val_if_fail (client->mpd_root_node != NULL, ret);
512
513 ret = gst_mpd_node_get_xml_buffer (GST_MPD_NODE (client->mpd_root_node),
514 data, (int *) size);
515
516 return ret;
517 }
518
519 GstDateTime *
520 gst_mpd_client2_get_availability_start_time (GstMPDClient2 * client)
521 {
522 GstDateTime *start_time;
523
524 if (client == NULL)
525 return (GstDateTime *) NULL;
526
527 start_time = client->mpd_root_node->availabilityStartTime;
528 if (start_time)
529 gst_date_time_ref (start_time);
530 return start_time;
531 }
532
533 void
534 gst_mpd_client2_set_download_helper (GstMPDClient2 * client,
535 DownloadHelper * dh)
536 {
537 client->download_helper = dh;
538 }
539
540 void
541 gst_mpd_client2_check_profiles (GstMPDClient2 * client)
542 {
543 GST_DEBUG ("Profiles: %s",
544 client->mpd_root_node->profiles ? client->mpd_root_node->
545 profiles : "<none>");
546
547 if (!client->mpd_root_node->profiles)
548 return;
549
550 if (g_strstr_len (client->mpd_root_node->profiles, -1,
551 "urn:mpeg:dash:profile:isoff-on-demand:2011")) {
552 client->profile_isoff_ondemand = TRUE;
553 GST_DEBUG ("Found ISOFF on demand profile (2011)");
554 }
555 }
556
557 void
558 gst_mpd_client2_fetch_on_load_external_resources (GstMPDClient2 * client)
559 {
560 GList *l;
561
562 for (l = client->mpd_root_node->Periods; l; /* explicitly advanced below */ ) {
563 GstMPDPeriodNode *period = l->data;
564 GList *m;
565
566 if (period->xlink_href && period->actuate == GST_MPD_XLINK_ACTUATE_ON_LOAD) {
567 GList *new_periods, *prev, *next;
568
569 new_periods = gst_mpd_client2_fetch_external_periods (client, period);
570
571 prev = l->prev;
572 client->mpd_root_node->Periods =
573 g_list_delete_link (client->mpd_root_node->Periods, l);
574 gst_mpd_period_node_free (period);
575 period = NULL;
576
577 /* Get new next node, we will insert before this */
578 if (prev)
579 next = prev->next;
580 else
581 next = client->mpd_root_node->Periods;
582
583 while (new_periods) {
584 client->mpd_root_node->Periods =
585 g_list_insert_before (client->mpd_root_node->Periods, next,
586 new_periods->data);
587 new_periods = g_list_delete_link (new_periods, new_periods);
588 }
589 next = NULL;
590
591 /* Update our iterator to the first new period if any, or the next */
592 if (prev)
593 l = prev->next;
594 else
595 l = client->mpd_root_node->Periods;
596
597 continue;
598 }
599
600 if (period->SegmentList && period->SegmentList->xlink_href
601 && period->SegmentList->actuate == GST_MPD_XLINK_ACTUATE_ON_LOAD) {
602 GstMPDSegmentListNode *new_segment_list;
603
604 new_segment_list =
605 gst_mpd_client2_fetch_external_segment_list (client, period, NULL,
606 NULL, NULL, period->SegmentList);
607
608 gst_mpd_segment_list_node_free (period->SegmentList);
609 period->SegmentList = new_segment_list;
610 }
611
612 for (m = period->AdaptationSets; m; /* explicitly advanced below */ ) {
613 GstMPDAdaptationSetNode *adapt_set = m->data;
614 GList *n;
615
616 if (adapt_set->xlink_href
617 && adapt_set->actuate == GST_MPD_XLINK_ACTUATE_ON_LOAD) {
618 GList *new_adapt_sets, *prev, *next;
619
620 new_adapt_sets =
621 gst_mpd_client2_fetch_external_adaptation_set (client, period,
622 adapt_set);
623
624 prev = m->prev;
625 period->AdaptationSets = g_list_delete_link (period->AdaptationSets, m);
626 gst_mpd_adaptation_set_node_free (adapt_set);
627 adapt_set = NULL;
628
629 /* Get new next node, we will insert before this */
630 if (prev)
631 next = prev->next;
632 else
633 next = period->AdaptationSets;
634
635 while (new_adapt_sets) {
636 period->AdaptationSets =
637 g_list_insert_before (period->AdaptationSets, next,
638 new_adapt_sets->data);
639 new_adapt_sets = g_list_delete_link (new_adapt_sets, new_adapt_sets);
640 }
641 next = NULL;
642
643 /* Update our iterator to the first new adapt_set if any, or the next */
644 if (prev)
645 m = prev->next;
646 else
647 m = period->AdaptationSets;
648
649 continue;
650 }
651
652 if (adapt_set->SegmentList && adapt_set->SegmentList->xlink_href
653 && adapt_set->SegmentList->actuate == GST_MPD_XLINK_ACTUATE_ON_LOAD) {
654 GstMPDSegmentListNode *new_segment_list;
655
656 new_segment_list =
657 gst_mpd_client2_fetch_external_segment_list (client, period,
658 adapt_set, NULL, period->SegmentList, adapt_set->SegmentList);
659
660 gst_mpd_segment_list_node_free (adapt_set->SegmentList);
661 adapt_set->SegmentList = new_segment_list;
662 }
663
664 for (n = adapt_set->Representations; n; n = n->next) {
665 GstMPDRepresentationNode *representation = n->data;
666
667 if (representation->SegmentList
668 && representation->SegmentList->xlink_href
669 && representation->SegmentList->actuate ==
670 GST_MPD_XLINK_ACTUATE_ON_LOAD) {
671
672 GstMPDSegmentListNode *new_segment_list;
673
674 new_segment_list =
675 gst_mpd_client2_fetch_external_segment_list (client, period,
676 adapt_set, representation, adapt_set->SegmentList,
677 representation->SegmentList);
678
679 gst_mpd_segment_list_node_free (representation->SegmentList);
680 representation->SegmentList = new_segment_list;
681
682 }
683 }
684
685 m = m->next;
686 }
687
688 l = l->next;
689 }
690 }
691
692
693 static GstStreamPeriod *
694 gst_mpd_client2_get_stream_period (GstMPDClient2 * client)
695 {
696 g_return_val_if_fail (client != NULL, NULL);
697 g_return_val_if_fail (client->periods != NULL, NULL);
698
699 return g_list_nth_data (client->periods, client->period_idx);
700 }
701
702 const gchar *
703 gst_mpd_client2_get_baseURL (GstMPDClient2 * client, guint indexStream)
704 {
705 GstActiveStream *stream;
706
707 g_return_val_if_fail (client != NULL, NULL);
708 g_return_val_if_fail (client->active_streams != NULL, NULL);
709 stream = g_list_nth_data (client->active_streams, indexStream);
710 g_return_val_if_fail (stream != NULL, NULL);
711
712 return stream->baseURL;
713 }
714
715 /* select a stream and extract the baseURL (if present) */
716 gchar *
717 gst_mpd_client2_parse_baseURL (GstMPDClient2 * client, GstActiveStream * stream,
718 gchar ** query)
719 {
720 GstStreamPeriod *stream_period;
721 static const gchar empty[] GST_UNUSED_CHECKS = "";
722 gchar *ret = NULL;
723 GstUri *abs_url;
724
725 g_return_val_if_fail (stream != NULL, g_strdup (empty));
726 stream_period = gst_mpd_client2_get_stream_period (client);
727 g_return_val_if_fail (stream_period != NULL, g_strdup (empty));
728 g_return_val_if_fail (stream_period->period != NULL, g_strdup (empty));
729
730 /* NULLify query return before we start */
731 if (query)
732 *query = NULL;
733
734 /* initialise base url */
735 abs_url =
736 gst_uri_from_string (client->mpd_base_uri ? client->
737 mpd_base_uri : client->mpd_uri);
738
739 /* combine a BaseURL at the MPD level with the current base url */
740 abs_url =
741 gst_mpd_helper_combine_urls (abs_url, client->mpd_root_node->BaseURLs,
742 query, stream->baseURL_idx);
743
744 /* combine a BaseURL at the Period level with the current base url */
745 abs_url =
746 gst_mpd_helper_combine_urls (abs_url, stream_period->period->BaseURLs,
747 query, stream->baseURL_idx);
748
749 GST_DEBUG ("Current adaptation set id %i (%s)", stream->cur_adapt_set->id,
750 stream->cur_adapt_set->contentType);
751 /* combine a BaseURL at the AdaptationSet level with the current base url */
752 abs_url =
753 gst_mpd_helper_combine_urls (abs_url, stream->cur_adapt_set->BaseURLs,
754 query, stream->baseURL_idx);
755
756 /* combine a BaseURL at the Representation level with the current base url */
757 abs_url =
758 gst_mpd_helper_combine_urls (abs_url,
759 stream->cur_representation->BaseURLs, query, stream->baseURL_idx);
760
761 ret = gst_uri_to_string (abs_url);
762 gst_uri_unref (abs_url);
763
764 return ret;
765 }
766
767 static GstClockTime
768 gst_mpd_client2_get_segment_end_time (GstMPDClient2 * client,
769 GPtrArray * segments, const GstMediaSegment * segment, gint index)
770 {
771 const GstStreamPeriod *stream_period;
772 GstClockTime end;
773
774 if (segment->repeat >= 0)
775 return segment->start + (segment->repeat + 1) * segment->duration;
776
777 if (index < segments->len - 1) {
778 const GstMediaSegment *next_segment =
779 g_ptr_array_index (segments, index + 1);
780 end = next_segment->start;
781 } else {
782 stream_period = gst_mpd_client2_get_stream_period (client);
783 end = stream_period->start + stream_period->duration;
784 }
785 return end;
786 }
787
788 static gboolean
789 gst_mpd_client2_add_media_segment (GstActiveStream * stream,
790 GstMPDSegmentURLNode * url_node, guint number, gint repeat,
791 guint64 scale_start, guint64 scale_duration,
792 GstClockTime start, GstClockTime duration)
793 {
794 GstMediaSegment *media_segment;
795
796 g_return_val_if_fail (stream->segments != NULL, FALSE);
797
798 media_segment = g_new0 (GstMediaSegment, 1);
799
800 media_segment->SegmentURL = url_node;
801 media_segment->number = number;
802 media_segment->scale_start = scale_start;
803 media_segment->scale_duration = scale_duration;
804 media_segment->start = start;
805 media_segment->duration = duration;
806 media_segment->repeat = repeat;
807
808 g_ptr_array_add (stream->segments, media_segment);
809 GST_LOG ("Added new segment: number %d, repeat %d, "
810 "ts: %" GST_TIME_FORMAT ", dur: %"
811 GST_TIME_FORMAT, number, repeat,
812 GST_TIME_ARGS (start), GST_TIME_ARGS (duration));
813
814 return TRUE;
815 }
816
817 static void
818 gst_mpd_client2_stream_update_presentation_time_offset (GstMPDClient2 * client,
819 GstActiveStream * stream)
820 {
821 GstMPDSegmentBaseNode *segbase = NULL;
822
823 /* Find the used segbase */
824 if (stream->cur_segment_list) {
825 segbase =
826 GST_MPD_MULT_SEGMENT_BASE_NODE (stream->cur_segment_list)->SegmentBase;
827 } else if (stream->cur_seg_template) {
828 segbase =
829 GST_MPD_MULT_SEGMENT_BASE_NODE (stream->cur_seg_template)->SegmentBase;
830 } else if (stream->cur_segment_base) {
831 segbase = stream->cur_segment_base;
832 }
833
834 if (segbase) {
835 /* Avoid overflows */
836 stream->presentationTimeOffset =
837 gst_util_uint64_scale (segbase->presentationTimeOffset, GST_SECOND,
838 segbase->timescale);
839 } else {
840 stream->presentationTimeOffset = 0;
841 }
842
843 GST_LOG ("Setting stream's presentation time offset to %" GST_TIME_FORMAT,
844 GST_TIME_ARGS (stream->presentationTimeOffset));
845 }
846
847 gboolean
848 gst_mpd_client2_setup_representation (GstMPDClient2 * client,
849 GstActiveStream * stream, GstMPDRepresentationNode * representation)
850 {
851 GstStreamPeriod *stream_period;
852 GList *rep_list;
853 GstClockTime PeriodStart, PeriodEnd, start_time, duration;
854 guint i;
855 guint64 start;
856
857 if (stream->cur_adapt_set == NULL) {
858 GST_WARNING ("No valid AdaptationSet node in the MPD file, aborting...");
859 return FALSE;
860 }
861
862 rep_list = stream->cur_adapt_set->Representations;
863 stream->cur_representation = representation;
864 stream->representation_idx = g_list_index (rep_list, representation);
865
866 /* clean the old segment list, if any */
867 if (stream->segments) {
868 g_ptr_array_unref (stream->segments);
869 stream->segments = NULL;
870 }
871
872 stream_period = gst_mpd_client2_get_stream_period (client);
873 g_return_val_if_fail (stream_period != NULL, FALSE);
874 g_return_val_if_fail (stream_period->period != NULL, FALSE);
875
876 PeriodStart = stream_period->start;
877 if (GST_CLOCK_TIME_IS_VALID (stream_period->duration))
878 PeriodEnd = stream_period->start + stream_period->duration;
879 else
880 PeriodEnd = GST_CLOCK_TIME_NONE;
881
882 GST_LOG ("Building segment list for Period from %" GST_TIME_FORMAT " to %"
883 GST_TIME_FORMAT, GST_TIME_ARGS (PeriodStart), GST_TIME_ARGS (PeriodEnd));
884
885 if (representation->SegmentBase != NULL
886 || representation->SegmentList != NULL) {
887 GList *SegmentURL;
888
889 /* We have a fixed list of segments for any of the cases here,
890 * init the segments list */
891 gst_mpdparser_init_active_stream_segments (stream);
892
893 /* get the first segment_base of the selected representation */
894 if ((stream->cur_segment_base =
895 gst_mpd_client2_get_segment_base (stream_period->period,
896 stream->cur_adapt_set, representation)) == NULL) {
897 GST_DEBUG ("No useful SegmentBase node for the current Representation");
898 }
899
900 /* get the first segment_list of the selected representation */
901 if ((stream->cur_segment_list =
902 gst_mpd_client2_get_segment_list (client, stream_period->period,
903 stream->cur_adapt_set, representation)) == NULL) {
904 GST_DEBUG ("No useful SegmentList node for the current Representation");
905 /* here we should have a single segment for each representation, whose URL is encoded in the baseURL element */
906 if (!gst_mpd_client2_add_media_segment (stream, NULL, 1, 0, 0,
907 PeriodEnd - PeriodStart, PeriodStart, PeriodEnd - PeriodStart)) {
908 return FALSE;
909 }
910 } else {
911 /* build the list of GstMediaSegment nodes from the SegmentList node */
912 SegmentURL = stream->cur_segment_list->SegmentURL;
913 if (SegmentURL == NULL) {
914 GST_WARNING
915 ("No valid list of SegmentURL nodes in the MPD file, aborting...");
916 return FALSE;
917 }
918
919 /* build segment list */
920 i = GST_MPD_MULT_SEGMENT_BASE_NODE (stream->
921 cur_segment_list)->startNumber;
922 start = 0;
923 start_time = PeriodStart;
924
925 GST_LOG ("Building media segment list using a SegmentList node");
926 if (GST_MPD_MULT_SEGMENT_BASE_NODE (stream->
927 cur_segment_list)->SegmentTimeline) {
928 GstMPDSegmentTimelineNode *timeline;
929 GstMPDSNode *S;
930 GList *list;
931 GstClockTime presentationTimeOffset;
932 GstMPDSegmentBaseNode *segbase;
933
934 segbase =
935 GST_MPD_MULT_SEGMENT_BASE_NODE (stream->
936 cur_segment_list)->SegmentBase;
937 presentationTimeOffset =
938 gst_util_uint64_scale (segbase->presentationTimeOffset, GST_SECOND,
939 segbase->timescale);
940 GST_LOG ("presentationTimeOffset = %" G_GUINT64_FORMAT,
941 presentationTimeOffset);
942
943 timeline =
944 GST_MPD_MULT_SEGMENT_BASE_NODE (stream->
945 cur_segment_list)->SegmentTimeline;
946 for (list = g_queue_peek_head_link (&timeline->S); list;
947 list = g_list_next (list)) {
948 guint timescale;
949
950 S = (GstMPDSNode *) list->data;
951 GST_LOG ("Processing S node: d=%" G_GUINT64_FORMAT " r=%d t=%"
952 G_GUINT64_FORMAT, S->d, S->r, S->t);
953 timescale =
954 GST_MPD_MULT_SEGMENT_BASE_NODE (stream->
955 cur_segment_list)->SegmentBase->timescale;
956 duration = gst_util_uint64_scale (S->d, GST_SECOND, timescale);
957
958 if (S->t > 0) {
959 start = S->t;
960 start_time = gst_util_uint64_scale (S->t, GST_SECOND, timescale)
961 + PeriodStart - presentationTimeOffset;
962 }
963
964 if (!SegmentURL) {
965 GST_WARNING
966 ("SegmentTimeline does not have a matching SegmentURL, aborting...");
967 return FALSE;
968 }
969
970 if (!gst_mpd_client2_add_media_segment (stream, SegmentURL->data, i,
971 S->r, start, S->d, start_time, duration)) {
972 return FALSE;
973 }
974 i += S->r + 1;
975 start_time += duration * (S->r + 1);
976 start += S->d * (S->r + 1);
977 SegmentURL = g_list_next (SegmentURL);
978 }
979 } else {
980 guint64 scale_dur;
981
982 duration =
983 gst_mpd_client2_get_segment_duration (client, stream, &scale_dur);
984 if (!GST_CLOCK_TIME_IS_VALID (duration))
985 return FALSE;
986
987 while (SegmentURL) {
988 if (!gst_mpd_client2_add_media_segment (stream, SegmentURL->data, i,
989 0, start, scale_dur, start_time, duration)) {
990 return FALSE;
991 }
992 i++;
993 start += scale_dur;
994 start_time += duration;
995 SegmentURL = g_list_next (SegmentURL);
996 }
997 }
998 }
999 } else {
1000 if (representation->SegmentTemplate != NULL) {
1001 stream->cur_seg_template = representation->SegmentTemplate;
1002 } else if (stream->cur_adapt_set->SegmentTemplate != NULL) {
1003 stream->cur_seg_template = stream->cur_adapt_set->SegmentTemplate;
1004 } else if (stream_period->period->SegmentTemplate != NULL) {
1005 stream->cur_seg_template = stream_period->period->SegmentTemplate;
1006 }
1007
1008 if (stream->cur_seg_template == NULL) {
1009
1010 gst_mpdparser_init_active_stream_segments (stream);
1011 /* here we should have a single segment for each representation, whose URL is encoded in the baseURL element */
1012 if (!gst_mpd_client2_add_media_segment (stream, NULL, 1, 0, 0,
1013 PeriodEnd - PeriodStart, 0, PeriodEnd - PeriodStart)) {
1014 return FALSE;
1015 }
1016 } else {
1017 GstClockTime presentationTimeOffset;
1018 GstMPDMultSegmentBaseNode *mult_seg =
1019 GST_MPD_MULT_SEGMENT_BASE_NODE (stream->cur_seg_template);
1020 presentationTimeOffset =
1021 gst_util_uint64_scale (mult_seg->SegmentBase->presentationTimeOffset,
1022 GST_SECOND, mult_seg->SegmentBase->timescale);
1023 GST_LOG ("presentationTimeOffset = %" GST_TIME_FORMAT,
1024 GST_TIME_ARGS (presentationTimeOffset));
1025 /* build segment list */
1026 i = mult_seg->startNumber;
1027 start = 0;
1028 start_time = 0;
1029
1030 GST_LOG ("Building media segment list using this template: %s",
1031 stream->cur_seg_template->media);
1032
1033 if (mult_seg->SegmentTimeline) {
1034 GstMPDSegmentTimelineNode *timeline;
1035 GstMPDSNode *S;
1036 GList *list;
1037
1038 timeline = mult_seg->SegmentTimeline;
1039 gst_mpdparser_init_active_stream_segments (stream);
1040 for (list = g_queue_peek_head_link (&timeline->S); list;
1041 list = g_list_next (list)) {
1042 guint timescale;
1043
1044 S = (GstMPDSNode *) list->data;
1045 GST_LOG ("Processing S node: d=%" G_GUINT64_FORMAT " r=%u t=%"
1046 G_GUINT64_FORMAT, S->d, S->r, S->t);
1047 timescale = mult_seg->SegmentBase->timescale;
1048 duration = gst_util_uint64_scale (S->d, GST_SECOND, timescale);
1049 if (S->t > 0) {
1050 start = S->t;
1051 start_time = gst_util_uint64_scale (S->t, GST_SECOND, timescale)
1052 + PeriodStart - presentationTimeOffset;
1053 }
1054
1055 if (!gst_mpd_client2_add_media_segment (stream, NULL, i, S->r, start,
1056 S->d, start_time, duration)) {
1057 return FALSE;
1058 }
1059 i += S->r + 1;
1060 start += S->d * (S->r + 1);
1061 start_time += duration * (S->r + 1);
1062 }
1063 } else {
1064 /* NOP - The segment is created on demand with the template, no need
1065 * to build a list */
1066 }
1067 }
1068 }
1069
1070 /* clip duration of segments to stop at period end */
1071 if (stream->segments && stream->segments->len) {
1072 if (GST_CLOCK_TIME_IS_VALID (PeriodEnd)) {
1073 guint n;
1074
1075 for (n = 0; n < stream->segments->len; ++n) {
1076 GstMediaSegment *media_segment =
1077 g_ptr_array_index (stream->segments, n);
1078 if (media_segment) {
1079 if (media_segment->start + media_segment->duration > PeriodEnd) {
1080 GstClockTime stop = PeriodEnd;
1081 if (n < stream->segments->len - 1) {
1082 GstMediaSegment *next_segment =
1083 g_ptr_array_index (stream->segments, n + 1);
1084 if (next_segment && next_segment->start < PeriodEnd)
1085 stop = next_segment->start;
1086 }
1087 media_segment->duration =
1088 media_segment->start > stop ? 0 : stop - media_segment->start;
1089 GST_LOG ("Fixed duration of segment %u: %" GST_TIME_FORMAT, n,
1090 GST_TIME_ARGS (media_segment->duration));
1091
1092 /* If the segment was clipped entirely, we discard it and all
1093 * subsequent ones */
1094 if (media_segment->duration == 0) {
1095 GST_WARNING ("Discarding %u segments outside period",
1096 stream->segments->len - n);
1097 /* _set_size should properly unref elements */
1098 g_ptr_array_set_size (stream->segments, n);
1099 break;
1100 }
1101 }
1102 }
1103 }
1104 }
1105 #ifndef GST_DISABLE_GST_DEBUG
1106 if (stream->segments->len > 0) {
1107 GstMediaSegment *last_media_segment =
1108 g_ptr_array_index (stream->segments, stream->segments->len - 1);
1109 GST_LOG ("Built a list of %d segments", last_media_segment->number);
1110 } else {
1111 GST_LOG ("All media segments were clipped");
1112 }
1113 #endif
1114 }
1115
1116 g_free (stream->baseURL);
1117 g_free (stream->queryURL);
1118 stream->baseURL =
1119 gst_mpd_client2_parse_baseURL (client, stream, &stream->queryURL);
1120
1121 gst_mpd_client2_stream_update_presentation_time_offset (client, stream);
1122
1123 return TRUE;
1124 }
1125
1126 #define CUSTOM_WRAPPER_START "<custom_wrapper>"
1127 #define CUSTOM_WRAPPER_END "</custom_wrapper>"
1128
1129 static GList *
1130 gst_mpd_client2_fetch_external_periods (GstMPDClient2 * client,
1131 GstMPDPeriodNode * period_node)
1132 {
1133 DownloadRequest *download;
1134 GstBuffer *period_buffer;
1135 GError *err = NULL;
1136
1137 GstUri *base_uri, *uri;
1138 gchar *query = NULL;
1139 gchar *uri_string;
1140 GList *new_periods = NULL;
1141
1142 /* ISO/IEC 23009-1:2014 5.5.3 4)
1143 * Remove nodes that resolve to nothing when resolving
1144 */
1145 if (strcmp (period_node->xlink_href,
1146 "urn:mpeg:dash:resolve-to-zero:2013") == 0) {
1147 return NULL;
1148 }
1149
1150 if (!client->download_helper) {
1151 return NULL;
1152 }
1153
1154 /* Build absolute URI */
1155
1156 /* Get base URI at the MPD level */
1157 base_uri =
1158 gst_uri_from_string (client->mpd_base_uri ? client->
1159 mpd_base_uri : client->mpd_uri);
1160
1161 /* combine a BaseURL at the MPD level with the current base url */
1162 base_uri =
1163 gst_mpd_helper_combine_urls (base_uri, client->mpd_root_node->BaseURLs,
1164 &query, 0);
1165 uri = gst_uri_from_string_with_base (base_uri, period_node->xlink_href);
1166 if (query)
1167 gst_uri_set_query_string (uri, query);
1168 g_free (query);
1169 uri_string = gst_uri_to_string (uri);
1170 gst_uri_unref (base_uri);
1171 gst_uri_unref (uri);
1172
1173 download =
1174 downloadhelper_fetch_uri (client->download_helper,
1175 uri_string, client->mpd_uri,
1176 DOWNLOAD_FLAG_COMPRESS | DOWNLOAD_FLAG_FORCE_REFRESH, &err);
1177 g_free (uri_string);
1178
1179 if (!download) {
1180 GST_ERROR ("Failed to download external Period node at '%s': %s",
1181 period_node->xlink_href, err->message);
1182 g_clear_error (&err);
1183 return NULL;
1184 }
1185
1186 period_buffer = download_request_take_buffer (download);
1187 download_request_unref (download);
1188
1189 if (period_buffer) {
1190 gsize alloc_size, buffer_size;
1191 GString *data;
1192
1193 /* external xml could have multiple period without root xmlNode.
1194 * To avoid xml parsing error caused by no root node, wrapping it with
1195 * custom root node */
1196
1197 buffer_size = gst_buffer_get_size (period_buffer);
1198 alloc_size =
1199 buffer_size + sizeof (CUSTOM_WRAPPER_START) +
1200 sizeof (CUSTOM_WRAPPER_END) - 2;
1201 data = g_string_sized_new (alloc_size);
1202 g_string_append_len (data, CUSTOM_WRAPPER_START,
1203 sizeof (CUSTOM_WRAPPER_START) - 1);
1204
1205 gst_buffer_extract (period_buffer, 0, data->str + data->len, buffer_size);
1206 gst_buffer_unref (period_buffer);
1207 data->len += buffer_size;
1208
1209 g_string_append_len (data, CUSTOM_WRAPPER_END,
1210 sizeof (CUSTOM_WRAPPER_END) - 1);
1211
1212 new_periods = gst_mpdparser_get_external_periods (data->str, alloc_size);
1213
1214 g_string_free (data, TRUE);
1215 }
1216
1217 return new_periods;
1218 }
1219
1220 gboolean
1221 gst_mpd_client2_setup_media_presentation (GstMPDClient2 * client,
1222 GstClockTime time, gint period_idx, const gchar * period_id)
1223 {
1224 GstStreamPeriod *stream_period;
1225 GstClockTime start, duration;
1226 GList *list, *next;
1227 guint idx;
1228 gboolean ret = FALSE;
1229
1230 g_return_val_if_fail (client != NULL, FALSE);
1231 g_return_val_if_fail (client->mpd_root_node != NULL, FALSE);
1232
1233 /* Check if we set up the media presentation far enough already */
1234 for (list = client->periods; list; list = list->next) {
1235 GstStreamPeriod *stream_period = list->data;
1236
1237 if ((time != GST_CLOCK_TIME_NONE
1238 && stream_period->duration != GST_CLOCK_TIME_NONE
1239 && stream_period->start + stream_period->duration >= time)
1240 || (time != GST_CLOCK_TIME_NONE && stream_period->start >= time))
1241 return TRUE;
1242
1243 if (period_idx != -1 && stream_period->number >= period_idx)
1244 return TRUE;
1245
1246 if (period_id != NULL && stream_period->period->id != NULL
1247 && strcmp (stream_period->period->id, period_id) == 0)
1248 return TRUE;
1249
1250 }
1251
1252 GST_DEBUG ("Building the list of Periods in the Media Presentation");
1253 /* clean the old period list, if any */
1254 /* TODO: In theory we could reuse the ones we have so far but that
1255 * seems more complicated than the overhead caused here
1256 */
1257 if (client->periods) {
1258 g_list_foreach (client->periods,
1259 (GFunc) gst_mpdparser_free_stream_period, NULL);
1260 g_list_free (client->periods);
1261 client->periods = NULL;
1262 }
1263
1264 idx = 0;
1265 start = 0;
1266 duration = GST_CLOCK_TIME_NONE;
1267
1268 if (client->mpd_root_node->mediaPresentationDuration <= 0 &&
1269 client->mpd_root_node->mediaPresentationDuration != -1) {
1270 /* Invalid MPD file: MPD duration is negative or zero */
1271 goto syntax_error;
1272 }
1273
1274 for (list = client->mpd_root_node->Periods; list;
1275 /* explicitly advanced below */ ) {
1276 GstMPDPeriodNode *period_node = list->data;
1277 GstMPDPeriodNode *next_period_node = NULL;
1278
1279 /* Download external period */
1280 if (period_node->xlink_href) {
1281 GList *new_periods;
1282 GList *prev;
1283
1284 new_periods =
1285 gst_mpd_client2_fetch_external_periods (client, period_node);
1286
1287 prev = list->prev;
1288 client->mpd_root_node->Periods =
1289 g_list_delete_link (client->mpd_root_node->Periods, list);
1290 gst_mpd_period_node_free (period_node);
1291 period_node = NULL;
1292
1293 /* Get new next node, we will insert before this */
1294 if (prev)
1295 next = prev->next;
1296 else
1297 next = client->mpd_root_node->Periods;
1298
1299 while (new_periods) {
1300 client->mpd_root_node->Periods =
1301 g_list_insert_before (client->mpd_root_node->Periods, next,
1302 new_periods->data);
1303 new_periods = g_list_delete_link (new_periods, new_periods);
1304 }
1305 next = NULL;
1306
1307 /* Update our iterator to the first new period if any, or the next */
1308 if (prev)
1309 list = prev->next;
1310 else
1311 list = client->mpd_root_node->Periods;
1312
1313 /* And try again */
1314 continue;
1315 }
1316
1317 if (period_node->start != -1) {
1318 /* we have a regular period */
1319 /* start cannot be smaller than previous start */
1320 if (list != g_list_first (client->mpd_root_node->Periods)
1321 && start >= period_node->start * GST_MSECOND) {
1322 /* Invalid MPD file: duration would be negative or zero */
1323 goto syntax_error;
1324 }
1325 start = period_node->start * GST_MSECOND;
1326 } else if (duration != GST_CLOCK_TIME_NONE) {
1327 /* start time inferred from previous period, this is still a regular period */
1328 start += duration;
1329 } else if (idx == 0
1330 && client->mpd_root_node->type == GST_MPD_FILE_TYPE_STATIC) {
1331 /* first period of a static MPD file, start time is 0 */
1332 start = 0;
1333 } else if (client->mpd_root_node->type == GST_MPD_FILE_TYPE_DYNAMIC) {
1334 /* this should be a live stream, let this pass */
1335 } else {
1336 /* this is an 'Early Available Period' */
1337 goto early;
1338 }
1339
1340 /* compute duration.
1341 If there is a start time for the next period, or this is the last period
1342 and mediaPresentationDuration was set, those values will take precedence
1343 over a configured period duration in computing this period's duration
1344
1345 ISO/IEC 23009-1:2014(E), chapter 5.3.2.1
1346 "The Period extends until the PeriodStart of the next Period, or until
1347 the end of the Media Presentation in the case of the last Period."
1348 */
1349
1350 while ((next = g_list_next (list)) != NULL) {
1351 /* try to infer this period duration from the start time of the next period */
1352 next_period_node = next->data;
1353
1354 if (next_period_node->xlink_href) {
1355 GList *new_periods;
1356
1357 new_periods =
1358 gst_mpd_client2_fetch_external_periods (client, next_period_node);
1359
1360 client->mpd_root_node->Periods =
1361 g_list_delete_link (client->mpd_root_node->Periods, next);
1362 gst_mpd_period_node_free (next_period_node);
1363 next_period_node = NULL;
1364 /* Get new next node, we will insert before this */
1365 next = g_list_next (list);
1366 while (new_periods) {
1367 client->mpd_root_node->Periods =
1368 g_list_insert_before (client->mpd_root_node->Periods, next,
1369 new_periods->data);
1370 new_periods = g_list_delete_link (new_periods, new_periods);
1371 }
1372
1373 /* And try again, getting the next list element which is now our newly
1374 * inserted nodes. If any */
1375 } else {
1376 /* Got the next period and it doesn't have to be downloaded first */
1377 break;
1378 }
1379 }
1380
1381 if (next_period_node) {
1382 if (next_period_node->start != -1) {
1383 if (start >= next_period_node->start * GST_MSECOND) {
1384 /* Invalid MPD file: duration would be negative or zero */
1385 goto syntax_error;
1386 }
1387 duration = next_period_node->start * GST_MSECOND - start;
1388 } else if (period_node->duration != -1) {
1389 if (period_node->duration <= 0) {
1390 /* Invalid MPD file: duration would be negative or zero */
1391 goto syntax_error;
1392 }
1393 duration = period_node->duration * GST_MSECOND;
1394 } else if (client->mpd_root_node->type == GST_MPD_FILE_TYPE_DYNAMIC) {
1395 /* might be a live file, ignore unspecified duration */
1396 } else {
1397 /* Invalid MPD file! */
1398 goto syntax_error;
1399 }
1400 } else if (client->mpd_root_node->mediaPresentationDuration != -1) {
1401 /* last Period of the Media Presentation */
1402 if (client->mpd_root_node->mediaPresentationDuration * GST_MSECOND <=
1403 start) {
1404 /* Invalid MPD file: duration would be negative or zero */
1405 goto syntax_error;
1406 }
1407 duration =
1408 client->mpd_root_node->mediaPresentationDuration * GST_MSECOND -
1409 start;
1410 } else if (period_node->duration != -1) {
1411 duration = period_node->duration * GST_MSECOND;
1412 } else if (client->mpd_root_node->type == GST_MPD_FILE_TYPE_DYNAMIC) {
1413 /* might be a live file, ignore unspecified duration */
1414 } else {
1415 /* Invalid MPD file! */
1416 GST_ERROR
1417 ("Invalid MPD file. The MPD is static without a valid duration");
1418 goto syntax_error;
1419 }
1420
1421 stream_period = g_new0 (GstStreamPeriod, 1);
1422 client->periods = g_list_append (client->periods, stream_period);
1423 stream_period->period = period_node;
1424 stream_period->number = idx++;
1425 stream_period->start = start;
1426 stream_period->duration = duration;
1427 ret = TRUE;
1428 GST_LOG (" - added Period %d start=%" GST_TIME_FORMAT " duration=%"
1429 GST_TIME_FORMAT, idx, GST_TIME_ARGS (start), GST_TIME_ARGS (duration));
1430
1431 if ((time != GST_CLOCK_TIME_NONE
1432 && stream_period->duration != GST_CLOCK_TIME_NONE
1433 && stream_period->start + stream_period->duration >= time)
1434 || (time != GST_CLOCK_TIME_NONE && stream_period->start >= time))
1435 break;
1436
1437 if (period_idx != -1 && stream_period->number >= period_idx)
1438 break;
1439
1440 if (period_id != NULL && stream_period->period->id != NULL
1441 && strcmp (stream_period->period->id, period_id) == 0)
1442 break;
1443
1444 list = list->next;
1445 }
1446
1447 GST_DEBUG
1448 ("Found a total of %d valid Periods in the Media Presentation up to this point",
1449 idx);
1450 return ret;
1451
1452 early:
1453 GST_WARNING
1454 ("Found an Early Available Period, skipping the rest of the Media Presentation");
1455 return ret;
1456
1457 syntax_error:
1458 GST_WARNING
1459 ("Cannot get the duration of the Period %d, skipping the rest of the Media Presentation",
1460 idx);
1461 return ret;
1462 }
1463
1464 static GList *
1465 gst_mpd_client2_fetch_external_adaptation_set (GstMPDClient2 * client,
1466 GstMPDPeriodNode * period, GstMPDAdaptationSetNode * adapt_set)
1467 {
1468 DownloadRequest *download;
1469 GstBuffer *adapt_set_buffer;
1470 GError *err = NULL;
1471 GstUri *base_uri, *uri;
1472 gchar *query = NULL;
1473 gchar *uri_string;
1474 GList *new_adapt_sets = NULL;
1475
1476 /* ISO/IEC 23009-1:2014 5.5.3 4)
1477 * Remove nodes that resolve to nothing when resolving
1478 */
1479 if (strcmp (adapt_set->xlink_href, "urn:mpeg:dash:resolve-to-zero:2013") == 0) {
1480 return NULL;
1481 }
1482
1483 if (!client->download_helper) {
1484 return NULL;
1485 }
1486
1487 /* Build absolute URI */
1488
1489 /* Get base URI at the MPD level */
1490 base_uri =
1491 gst_uri_from_string (client->mpd_base_uri ? client->
1492 mpd_base_uri : client->mpd_uri);
1493
1494 /* combine a BaseURL at the MPD level with the current base url */
1495 base_uri =
1496 gst_mpd_helper_combine_urls (base_uri, client->mpd_root_node->BaseURLs,
1497 &query, 0);
1498
1499 /* combine a BaseURL at the Period level with the current base url */
1500 base_uri =
1501 gst_mpd_helper_combine_urls (base_uri, period->BaseURLs, &query, 0);
1502
1503 uri = gst_uri_from_string_with_base (base_uri, adapt_set->xlink_href);
1504 if (query)
1505 gst_uri_set_query_string (uri, query);
1506 g_free (query);
1507 uri_string = gst_uri_to_string (uri);
1508 gst_uri_unref (base_uri);
1509 gst_uri_unref (uri);
1510
1511 download =
1512 downloadhelper_fetch_uri (client->download_helper,
1513 uri_string, client->mpd_uri,
1514 DOWNLOAD_FLAG_COMPRESS | DOWNLOAD_FLAG_FORCE_REFRESH, &err);
1515 g_free (uri_string);
1516
1517 if (!download) {
1518 GST_ERROR ("Failed to download external AdaptationSet node at '%s': %s",
1519 adapt_set->xlink_href, err->message);
1520 g_clear_error (&err);
1521 return NULL;
1522 }
1523
1524 adapt_set_buffer = download_request_take_buffer (download);
1525 download_request_unref (download);
1526
1527 if (adapt_set_buffer) {
1528 GstMapInfo map;
1529 gst_buffer_map (adapt_set_buffer, &map, GST_MAP_READ);
1530
1531 new_adapt_sets =
1532 gst_mpdparser_get_external_adaptation_sets ((const gchar *) map.data,
1533 map.size, period);
1534
1535 gst_buffer_unmap (adapt_set_buffer, &map);
1536 gst_buffer_unref (adapt_set_buffer);
1537 }
1538
1539 return new_adapt_sets;
1540 }
1541
1542 static GList *
1543 gst_mpd_client2_get_adaptation_sets_for_period (GstMPDClient2 * client,
1544 GstStreamPeriod * period)
1545 {
1546 GList *list;
1547
1548 g_return_val_if_fail (period != NULL, NULL);
1549
1550 /* Resolve all external adaptation sets of this period. Every user of
1551 * the adaptation sets would need to know the content of all adaptation sets
1552 * to decide which one to use, so we have to resolve them all here
1553 */
1554 for (list = period->period->AdaptationSets; list;
1555 /* advanced explicitly below */ ) {
1556 GstMPDAdaptationSetNode *adapt_set = (GstMPDAdaptationSetNode *) list->data;
1557 GList *new_adapt_sets = NULL, *prev, *next;
1558
1559 if (!adapt_set->xlink_href) {
1560 list = list->next;
1561 continue;
1562 }
1563
1564 new_adapt_sets =
1565 gst_mpd_client2_fetch_external_adaptation_set (client, period->period,
1566 adapt_set);
1567
1568 prev = list->prev;
1569 period->period->AdaptationSets =
1570 g_list_delete_link (period->period->AdaptationSets, list);
1571 gst_mpd_adaptation_set_node_free (adapt_set);
1572 adapt_set = NULL;
1573
1574 /* Get new next node, we will insert before this */
1575 if (prev)
1576 next = prev->next;
1577 else
1578 next = period->period->AdaptationSets;
1579
1580 while (new_adapt_sets) {
1581 period->period->AdaptationSets =
1582 g_list_insert_before (period->period->AdaptationSets, next,
1583 new_adapt_sets->data);
1584 new_adapt_sets = g_list_delete_link (new_adapt_sets, new_adapt_sets);
1585 }
1586
1587 /* Update our iterator to the first new adaptation set if any, or the next */
1588 if (prev)
1589 list = prev->next;
1590 else
1591 list = period->period->AdaptationSets;
1592 }
1593
1594 return period->period->AdaptationSets;
1595 }
1596
1597 GList *
1598 gst_mpd_client2_get_adaptation_sets (GstMPDClient2 * client)
1599 {
1600 GstStreamPeriod *stream_period;
1601
1602 stream_period = gst_mpd_client2_get_stream_period (client);
1603 if (stream_period == NULL || stream_period->period == NULL) {
1604 GST_DEBUG ("No more Period nodes in the MPD file, terminating...");
1605 return NULL;
1606 }
1607
1608 return gst_mpd_client2_get_adaptation_sets_for_period (client, stream_period);
1609 }
1610
1611 gboolean
1612 gst_mpd_client2_setup_streaming (GstMPDClient2 * client,
1613 GstMPDAdaptationSetNode * adapt_set, gint64 max_bandwidth,
1614 gint max_video_width, gint max_video_height,
1615 gint max_video_framerate_n, gint max_video_framerate_d)
1616 {
1617 GstMPDRepresentationNode *representation = NULL;
1618 GList *rep_list = NULL;
1619 GstActiveStream *stream;
1620 gint rep_id;
1621
1622 rep_list = adapt_set->Representations;
1623 if (!rep_list) {
1624 GST_WARNING ("Can not retrieve any representation, aborting...");
1625 return FALSE;
1626 }
1627
1628 stream = g_new0 (GstActiveStream, 1);
1629 gst_mpdparser_init_active_stream_segments (stream);
1630
1631 stream->baseURL_idx = 0;
1632 stream->cur_adapt_set = adapt_set;
1633
1634 GST_DEBUG ("0. Current stream %p", stream);
1635
1636 rep_id = gst_mpd_client2_get_rep_idx_with_max_bandwidth (rep_list,
1637 max_bandwidth, max_video_width, max_video_height,
1638 max_video_framerate_n, max_video_framerate_d);
1639
1640 if (rep_id >= 0) {
1641 GList *best_rep;
1642
1643 best_rep = g_list_nth (rep_list, rep_id);
1644 if (best_rep)
1645 representation = (GstMPDRepresentationNode *) best_rep->data;
1646 }
1647
1648 if (!representation) {
1649 GST_WARNING ("No representation with the requested bandwidth or video "
1650 "resolution/framerate restriction");
1651 representation = gst_mpd_client2_get_lowest_representation (rep_list);
1652 }
1653
1654 if (!representation) {
1655 GST_WARNING ("No valid representation in the MPD file, aborting...");
1656 gst_mpdparser_free_active_stream (stream);
1657 return FALSE;
1658 }
1659 stream->mimeType =
1660 gst_mpdparser_representation_get_mimetype (adapt_set, representation);
1661 if (stream->mimeType == GST_STREAM_UNKNOWN) {
1662 GST_WARNING ("Unknown mime type in the representation, aborting...");
1663 gst_mpdparser_free_active_stream (stream);
1664 return FALSE;
1665 }
1666
1667 client->active_streams = g_list_append (client->active_streams, stream);
1668 if (!gst_mpd_client2_setup_representation (client, stream, representation)) {
1669 GST_WARNING ("Failed to setup the representation, aborting...");
1670 return FALSE;
1671 }
1672
1673 GST_INFO ("Successfully setup the download pipeline for mimeType %d",
1674 stream->mimeType);
1675
1676 return TRUE;
1677 }
1678
1679 gboolean
1680 gst_mpd_client2_stream_seek (GstMPDClient2 * client, GstActiveStream * stream,
1681 gboolean forward, GstSeekFlags flags, GstClockTime ts,
1682 GstClockTime * final_ts)
1683 {
1684 gint index = 0;
1685 gint repeat_index = 0;
1686 GstMediaSegment *selectedChunk = NULL;
1687
1688 g_return_val_if_fail (stream != NULL, 0);
1689
1690 if (stream->segments) {
1691 for (index = 0; index < stream->segments->len; index++) {
1692 gboolean in_segment = FALSE;
1693 GstMediaSegment *segment = g_ptr_array_index (stream->segments, index);
1694 GstClockTime end_time;
1695
1696 GST_DEBUG ("Looking at fragment sequence chunk %d / %d", index,
1697 stream->segments->len);
1698
1699 end_time =
1700 gst_mpd_client2_get_segment_end_time (client, stream->segments,
1701 segment, index);
1702
1703 /* avoid downloading another fragment just for 1ns in reverse mode */
1704 if (forward)
1705 in_segment = ts < end_time;
1706 else
1707 in_segment = ts <= end_time;
1708
1709 if (in_segment) {
1710 GstClockTime chunk_time;
1711
1712 selectedChunk = segment;
1713 if (ts < segment->start) {
1714 /* Seek time lies in a gap between segments. Continue playing from
1715 * the first repetition of selectedChunk. */
1716 repeat_index = 0;
1717 } else {
1718 repeat_index =
1719 ((ts - segment->start) +
1720 ((GstMediaSegment *) stream->segments->pdata[0])->start) /
1721 segment->duration;
1722 }
1723
1724 chunk_time = segment->start + segment->duration * repeat_index;
1725
1726 /* At the end of a segment in reverse mode, start from the previous fragment */
1727 if (!forward && repeat_index > 0
1728 && ((ts - segment->start) % segment->duration == 0))
1729 repeat_index--;
1730
1731 if ((flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST) {
1732 if (repeat_index + 1 < segment->repeat) {
1733 if (ts - chunk_time > chunk_time + segment->duration - ts)
1734 repeat_index++;
1735 } else if (index + 1 < stream->segments->len) {
1736 GstMediaSegment *next_segment =
1737 g_ptr_array_index (stream->segments, index + 1);
1738
1739 if (ts - chunk_time > next_segment->start - ts) {
1740 repeat_index = 0;
1741 selectedChunk = next_segment;
1742 index++;
1743 }
1744 }
1745 } else if (((forward && flags & GST_SEEK_FLAG_SNAP_AFTER) ||
1746 (!forward && flags & GST_SEEK_FLAG_SNAP_BEFORE)) &&
1747 ts > chunk_time) {
1748
1749 if (repeat_index + 1 < segment->repeat) {
1750 repeat_index++;
1751 } else {
1752 repeat_index = 0;
1753 if (index + 1 >= stream->segments->len) {
1754 selectedChunk = NULL;
1755 } else {
1756 selectedChunk = g_ptr_array_index (stream->segments, ++index);
1757 }
1758 }
1759 }
1760 break;
1761 }
1762 }
1763
1764 if (selectedChunk == NULL) {
1765 stream->segment_index = stream->segments->len;
1766 stream->segment_repeat_index = 0;
1767 GST_DEBUG ("Seek to after last segment");
1768 return FALSE;
1769 }
1770
1771 if (final_ts)
1772 *final_ts = selectedChunk->start + selectedChunk->duration * repeat_index;
1773 } else {
1774 GstClockTime duration =
1775 gst_mpd_client2_get_segment_duration (client, stream, NULL);
1776 GstStreamPeriod *stream_period = gst_mpd_client2_get_stream_period (client);
1777 guint segments_count = gst_mpd_client2_get_segments_counts (client, stream);
1778 GstClockTime index_time;
1779
1780 g_return_val_if_fail (GST_MPD_MULT_SEGMENT_BASE_NODE
1781 (stream->cur_seg_template)->SegmentTimeline == NULL, FALSE);
1782 if (!GST_CLOCK_TIME_IS_VALID (duration) || duration == 0) {
1783 return FALSE;
1784 }
1785
1786 if (ts > stream_period->start)
1787 ts -= stream_period->start;
1788 else
1789 ts = 0;
1790
1791 index = ts / duration;
1792
1793 /* At the end of a segment in reverse mode, start from the previous fragment */
1794 if (!forward && index > 0 && ts % duration == 0)
1795 index--;
1796
1797 index_time = index * duration;
1798
1799 if ((flags & GST_SEEK_FLAG_SNAP_NEAREST) == GST_SEEK_FLAG_SNAP_NEAREST) {
1800 if (ts - index_time > index_time + duration - ts)
1801 index++;
1802 } else if (((forward && flags & GST_SEEK_FLAG_SNAP_AFTER) ||
1803 (!forward && flags & GST_SEEK_FLAG_SNAP_BEFORE))
1804 && ts != index_time) {
1805 index++;
1806 }
1807
1808 if (segments_count > 0 && index >= segments_count) {
1809 stream->segment_index = segments_count;
1810 stream->segment_repeat_index = 0;
1811 GST_DEBUG ("Seek to after last segment");
1812 return FALSE;
1813 }
1814 if (final_ts)
1815 *final_ts = index * duration;
1816 }
1817
1818 stream->segment_repeat_index = repeat_index;
1819 stream->segment_index = index;
1820
1821 return TRUE;
1822 }
1823
1824 GstClockTimeDiff
1825 gst_mpd_client2_calculate_time_difference (const GstDateTime * t1,
1826 const GstDateTime * t2)
1827 {
1828 GDateTime *gdt1, *gdt2;
1829 GTimeSpan diff;
1830
1831 g_assert (t1 != NULL && t2 != NULL);
1832 gdt1 = gst_date_time_to_g_date_time ((GstDateTime *) t1);
1833 gdt2 = gst_date_time_to_g_date_time ((GstDateTime *) t2);
1834 diff = g_date_time_difference (gdt2, gdt1);
1835 g_date_time_unref (gdt1);
1836 g_date_time_unref (gdt2);
1837 return diff * GST_USECOND;
1838 }
1839
1840 GstDateTime *
1841 gst_mpd_client2_add_time_difference (GstDateTime * t1, GstClockTimeDiff diff)
1842 {
1843 GDateTime *gdt;
1844 GDateTime *gdt2;
1845 GstDateTime *rv;
1846
1847 g_assert (t1 != NULL);
1848 gdt = gst_date_time_to_g_date_time (t1);
1849 g_assert (gdt != NULL);
1850 gdt2 = g_date_time_add (gdt, diff / GST_USECOND);
1851 g_assert (gdt2 != NULL);
1852 g_date_time_unref (gdt);
1853 rv = gst_date_time_new_from_g_date_time (gdt2);
1854
1855 /* Don't g_date_time_unref(gdt2) because gst_date_time_new_from_g_date_time takes
1856 * ownership of the GDateTime pointer.
1857 */
1858
1859 return rv;
1860 }
1861
1862 gboolean
1863 gst_mpd_client2_get_last_fragment_timestamp_end (GstMPDClient2 * client,
1864 guint stream_idx, GstClockTime * ts)
1865 {
1866 GstActiveStream *stream;
1867 gint segment_idx;
1868 GstMediaSegment *currentChunk;
1869 GstStreamPeriod *stream_period;
1870
1871 GST_DEBUG ("Stream index: %i", stream_idx);
1872 stream = g_list_nth_data (client->active_streams, stream_idx);
1873 g_return_val_if_fail (stream != NULL, 0);
1874
1875 if (!stream->segments) {
1876 stream_period = gst_mpd_client2_get_stream_period (client);
1877 *ts = stream_period->duration;
1878 } else {
1879 segment_idx = gst_mpd_client2_get_segments_counts (client, stream) - 1;
1880 if (segment_idx >= stream->segments->len) {
1881 GST_WARNING ("Segment index %d is outside of segment list of length %d",
1882 segment_idx, stream->segments->len);
1883 return FALSE;
1884 }
1885 currentChunk = g_ptr_array_index (stream->segments, segment_idx);
1886
1887 if (currentChunk->repeat >= 0) {
1888 *ts =
1889 currentChunk->start + (currentChunk->duration * (1 +
1890 currentChunk->repeat)) -
1891 gst_mpd_client2_get_period_start_time (client);
1892 } else {
1893 /* 5.3.9.6.1: negative repeat means repeat till the end of the
1894 * period, or the next update of the MPD (which I think is
1895 * implicit, as this will all get deleted/recreated), or the
1896 * start of the next segment, if any. */
1897 stream_period = gst_mpd_client2_get_stream_period (client);
1898 *ts = stream_period->duration;
1899 }
1900 }
1901
1902 return TRUE;
1903 }
1904
1905 gboolean
1906 gst_mpd_client2_get_next_fragment_timestamp (GstMPDClient2 * client,
1907 guint stream_idx, GstClockTime * ts)
1908 {
1909 GstActiveStream *stream;
1910 GstMediaSegment *currentChunk;
1911
1912 GST_DEBUG ("Stream index: %i", stream_idx);
1913 stream = g_list_nth_data (client->active_streams, stream_idx);
1914 g_return_val_if_fail (stream != NULL, 0);
1915
1916 if (stream->segments) {
1917 GST_DEBUG ("Looking for fragment sequence chunk %d / %d",
1918 stream->segment_index, stream->segments->len);
1919 if (stream->segment_index >= stream->segments->len)
1920 return FALSE;
1921 currentChunk = g_ptr_array_index (stream->segments, stream->segment_index);
1922
1923 *ts =
1924 currentChunk->start +
1925 (currentChunk->duration * stream->segment_repeat_index) -
1926 gst_mpd_client2_get_period_start_time (client);
1927 } else {
1928 GstClockTime duration =
1929 gst_mpd_client2_get_segment_duration (client, stream, NULL);
1930 guint segments_count = gst_mpd_client2_get_segments_counts (client, stream);
1931
1932 g_return_val_if_fail (GST_MPD_MULT_SEGMENT_BASE_NODE
1933 (stream->cur_seg_template)->SegmentTimeline == NULL, FALSE);
1934 if (!GST_CLOCK_TIME_IS_VALID (duration) || (segments_count > 0
1935 && stream->segment_index >= segments_count)) {
1936 return FALSE;
1937 }
1938 *ts = stream->segment_index * duration;
1939 }
1940
1941 return TRUE;
1942 }
1943
1944 GstClockTime
1945 gst_mpd_client2_get_stream_presentation_offset (GstMPDClient2 * client,
1946 guint stream_idx)
1947 {
1948 GstActiveStream *stream = NULL;
1949
1950 g_return_val_if_fail (client != NULL, 0);
1951 g_return_val_if_fail (client->active_streams != NULL, 0);
1952 stream = g_list_nth_data (client->active_streams, stream_idx);
1953 g_return_val_if_fail (stream != NULL, 0);
1954
1955 return stream->presentationTimeOffset;
1956 }
1957
1958 GstClockTime
1959 gst_mpd_client2_get_period_start_time (GstMPDClient2 * client)
1960 {
1961 GstStreamPeriod *stream_period = NULL;
1962
1963 g_return_val_if_fail (client != NULL, 0);
1964 stream_period = gst_mpd_client2_get_stream_period (client);
1965 g_return_val_if_fail (stream_period != NULL, 0);
1966
1967 return stream_period->start;
1968 }
1969
1970 /**
1971 * gst_mpd_client2_get_utc_timing_sources:
1972 * @client: #GstMPDClient2 to check for UTCTiming elements
1973 * @methods: A bit mask of #GstMPDUTCTimingType that specifies the methods
1974 * to search for.
1975 * @selected_method: (nullable): The selected method
1976 * Returns: (transfer none): A NULL terminated array of URLs of servers
1977 * that use @selected_method to provide a realtime clock.
1978 *
1979 * Searches the UTCTiming elements found in the manifest for an element
1980 * that uses one of the UTC timing methods specified in @selected_method.
1981 * If multiple UTCTiming elements are present that support one of the
1982 * methods specified in @selected_method, the first one is returned.
1983 *
1984 * Since: 1.6
1985 */
1986 gchar **
1987 gst_mpd_client2_get_utc_timing_sources (GstMPDClient2 * client,
1988 guint methods, GstMPDUTCTimingType * selected_method)
1989 {
1990 GList *list;
1991
1992 g_return_val_if_fail (client != NULL, NULL);
1993 g_return_val_if_fail (client->mpd_root_node != NULL, NULL);
1994 for (list = g_list_first (client->mpd_root_node->UTCTimings); list;
1995 list = g_list_next (list)) {
1996 const GstMPDUTCTimingNode *node = (const GstMPDUTCTimingNode *) list->data;
1997 if (node->method & methods) {
1998 if (selected_method) {
1999 *selected_method = node->method;
2000 }
2001 return node->urls;
2002 }
2003 }
2004 return NULL;
2005 }
2006
2007
2008 gboolean
2009 gst_mpd_client2_get_next_fragment (GstMPDClient2 * client,
2010 guint indexStream, GstMediaFragmentInfo * fragment)
2011 {
2012 GstActiveStream *stream = NULL;
2013 GstMediaSegment *currentChunk;
2014 gchar *mediaURL = NULL;
2015 gchar *indexURL = NULL;
2016 GstUri *base_url, *frag_url;
2017
2018 /* select stream */
2019 g_return_val_if_fail (client != NULL, FALSE);
2020 g_return_val_if_fail (client->active_streams != NULL, FALSE);
2021 stream = g_list_nth_data (client->active_streams, indexStream);
2022 g_return_val_if_fail (stream != NULL, FALSE);
2023 g_return_val_if_fail (stream->cur_representation != NULL, FALSE);
2024
2025 if (stream->segments) {
2026 GST_DEBUG ("Looking for fragment sequence chunk %d / %d",
2027 stream->segment_index, stream->segments->len);
2028 if (stream->segment_index >= stream->segments->len)
2029 return FALSE;
2030 } else {
2031 GstClockTime duration = gst_mpd_client2_get_segment_duration (client,
2032 stream, NULL);
2033 guint segments_count = gst_mpd_client2_get_segments_counts (client, stream);
2034
2035 g_return_val_if_fail (GST_MPD_MULT_SEGMENT_BASE_NODE
2036 (stream->cur_seg_template)->SegmentTimeline == NULL, FALSE);
2037 if (!GST_CLOCK_TIME_IS_VALID (duration) || (segments_count > 0
2038 && stream->segment_index >= segments_count)) {
2039 return FALSE;
2040 }
2041 fragment->duration = duration;
2042 }
2043
2044 /* FIXME rework discont checking */
2045 /* fragment->discontinuity = segment_idx != currentChunk.number; */
2046 fragment->range_start = 0;
2047 fragment->range_end = -1;
2048 fragment->index_uri = NULL;
2049 fragment->index_range_start = 0;
2050 fragment->index_range_end = -1;
2051
2052 if (stream->segments) {
2053 currentChunk = g_ptr_array_index (stream->segments, stream->segment_index);
2054
2055 GST_DEBUG ("currentChunk->SegmentURL = %p", currentChunk->SegmentURL);
2056 if (currentChunk->SegmentURL != NULL) {
2057 mediaURL = gst_mpdparser_get_mediaURL (stream, currentChunk->SegmentURL);
2058 indexURL = g_strdup (currentChunk->SegmentURL->index);
2059 } else if (stream->cur_seg_template != NULL) {
2060 mediaURL =
2061 gst_mpdparser_build_URL_from_template (stream->cur_seg_template->
2062 media, stream->cur_representation->id,
2063 currentChunk->number + stream->segment_repeat_index,
2064 stream->cur_representation->bandwidth,
2065 currentChunk->scale_start +
2066 stream->segment_repeat_index * currentChunk->scale_duration);
2067 if (stream->cur_seg_template->index) {
2068 indexURL =
2069 gst_mpdparser_build_URL_from_template (stream->cur_seg_template->
2070 index, stream->cur_representation->id,
2071 currentChunk->number + stream->segment_repeat_index,
2072 stream->cur_representation->bandwidth,
2073 currentChunk->scale_start +
2074 stream->segment_repeat_index * currentChunk->scale_duration);
2075 }
2076 }
2077 GST_DEBUG ("mediaURL = %s", mediaURL);
2078 GST_DEBUG ("indexURL = %s", indexURL);
2079
2080 fragment->timestamp =
2081 currentChunk->start +
2082 stream->segment_repeat_index * currentChunk->duration -
2083 gst_mpd_client2_get_period_start_time (client);
2084 fragment->duration = currentChunk->duration;
2085 if (currentChunk->SegmentURL) {
2086 if (currentChunk->SegmentURL->mediaRange) {
2087 fragment->range_start =
2088 currentChunk->SegmentURL->mediaRange->first_byte_pos;
2089 fragment->range_end =
2090 currentChunk->SegmentURL->mediaRange->last_byte_pos;
2091 }
2092 if (currentChunk->SegmentURL->indexRange) {
2093 fragment->index_range_start =
2094 currentChunk->SegmentURL->indexRange->first_byte_pos;
2095 fragment->index_range_end =
2096 currentChunk->SegmentURL->indexRange->last_byte_pos;
2097 }
2098 }
2099 } else {
2100 if (stream->cur_seg_template != NULL) {
2101 mediaURL =
2102 gst_mpdparser_build_URL_from_template (stream->cur_seg_template->
2103 media, stream->cur_representation->id,
2104 stream->segment_index +
2105 GST_MPD_MULT_SEGMENT_BASE_NODE (stream->
2106 cur_seg_template)->startNumber,
2107 stream->cur_representation->bandwidth,
2108 stream->segment_index * fragment->duration);
2109 if (stream->cur_seg_template->index) {
2110 indexURL =
2111 gst_mpdparser_build_URL_from_template (stream->cur_seg_template->
2112 index, stream->cur_representation->id,
2113 stream->segment_index +
2114 GST_MPD_MULT_SEGMENT_BASE_NODE (stream->
2115 cur_seg_template)->startNumber,
2116 stream->cur_representation->bandwidth,
2117 stream->segment_index * fragment->duration);
2118 }
2119 } else {
2120 return FALSE;
2121 }
2122
2123 GST_DEBUG ("mediaURL = %s", mediaURL);
2124 GST_DEBUG ("indexURL = %s", indexURL);
2125
2126 fragment->timestamp = stream->segment_index * fragment->duration;
2127 }
2128
2129 base_url = gst_uri_from_string (stream->baseURL);
2130 frag_url = gst_uri_from_string_with_base (base_url, mediaURL);
2131 g_free (mediaURL);
2132 if (stream->queryURL) {
2133 frag_url = gst_uri_make_writable (frag_url);
2134 gst_uri_set_query_string (frag_url, stream->queryURL);
2135 }
2136 fragment->uri = gst_uri_to_string (frag_url);
2137 gst_uri_unref (frag_url);
2138
2139 if (indexURL != NULL) {
2140 frag_url = gst_uri_make_writable (gst_uri_from_string_with_base (base_url,
2141 indexURL));
2142 gst_uri_set_query_string (frag_url, stream->queryURL);
2143 fragment->index_uri = gst_uri_to_string (frag_url);
2144 gst_uri_unref (frag_url);
2145 g_free (indexURL);
2146 } else if (indexURL == NULL && (fragment->index_range_start
2147 || fragment->index_range_end != -1)) {
2148 /* index has no specific URL but has a range, we should only use this if
2149 * the media also has a range, otherwise we are serving some data twice
2150 * (in the media fragment and again in the index) */
2151 if (!(fragment->range_start || fragment->range_end != -1)) {
2152 GST_WARNING ("Ignoring index ranges because there isn't a media range "
2153 "and URIs would be the same");
2154 /* removing index information */
2155 fragment->index_range_start = 0;
2156 fragment->index_range_end = -1;
2157 }
2158 }
2159
2160 gst_uri_unref (base_url);
2161
2162 GST_DEBUG ("Loading chunk with URL %s", fragment->uri);
2163
2164 return TRUE;
2165 }
2166
2167 gboolean
2168 gst_mpd_client2_has_next_segment (GstMPDClient2 * client,
2169 GstActiveStream * stream, gboolean forward)
2170 {
2171 if (forward) {
2172 guint segments_count = gst_mpd_client2_get_segments_counts (client, stream);
2173
2174 if (segments_count > 0 && stream->segments
2175 && stream->segment_index + 1 == segments_count) {
2176 GstMediaSegment *segment;
2177
2178 segment = g_ptr_array_index (stream->segments, stream->segment_index);
2179 if (segment->repeat >= 0
2180 && stream->segment_repeat_index >= segment->repeat)
2181 return FALSE;
2182 } else if (segments_count > 0
2183 && stream->segment_index + 1 >= segments_count) {
2184 return FALSE;
2185 }
2186 } else {
2187 if (stream->segment_index < 0)
2188 return FALSE;
2189 }
2190
2191 return TRUE;
2192 }
2193
2194 GstFlowReturn
2195 gst_mpd_client2_advance_segment (GstMPDClient2 * client,
2196 GstActiveStream * stream, gboolean forward)
2197 {
2198 GstMediaSegment *segment;
2199 GstFlowReturn ret = GST_FLOW_OK;
2200 guint segments_count = gst_mpd_client2_get_segments_counts (client, stream);
2201
2202 GST_DEBUG ("Advancing segment. Current: %d / %d r:%d", stream->segment_index,
2203 segments_count, stream->segment_repeat_index);
2204
2205 /* handle special cases first */
2206 if (forward) {
2207 if (segments_count > 0 && stream->segment_index >= segments_count) {
2208 ret = GST_FLOW_EOS;
2209 goto done;
2210 }
2211
2212 if (stream->segments == NULL) {
2213 if (stream->segment_index < 0) {
2214 stream->segment_index = 0;
2215 } else {
2216 stream->segment_index++;
2217 if (segments_count > 0 && stream->segment_index >= segments_count) {
2218 ret = GST_FLOW_EOS;
2219 }
2220 }
2221 goto done;
2222 }
2223
2224 /* special case for when playback direction is reverted right at *
2225 * the end of the segment list */
2226 if (stream->segment_index < 0) {
2227 stream->segment_index = 0;
2228 goto done;
2229 }
2230 } else {
2231 if (stream->segments == NULL)
2232 stream->segment_index--;
2233 if (stream->segment_index < 0) {
2234 stream->segment_index = -1;
2235 ret = GST_FLOW_EOS;
2236 goto done;
2237 }
2238 if (stream->segments == NULL)
2239 goto done;
2240
2241 /* special case for when playback direction is reverted right at *
2242 * the end of the segment list */
2243 if (stream->segment_index >= segments_count) {
2244 stream->segment_index = segments_count - 1;
2245 segment = g_ptr_array_index (stream->segments, stream->segment_index);
2246 if (segment->repeat >= 0) {
2247 stream->segment_repeat_index = segment->repeat;
2248 } else {
2249 GstClockTime start = segment->start;
2250 GstClockTime end =
2251 gst_mpd_client2_get_segment_end_time (client, stream->segments,
2252 segment,
2253 stream->segment_index);
2254 stream->segment_repeat_index =
2255 (guint) (end - start) / segment->duration;
2256 }
2257 goto done;
2258 }
2259 }
2260
2261 /* for the normal cases we can get the segment safely here */
2262 segment = g_ptr_array_index (stream->segments, stream->segment_index);
2263 if (forward) {
2264 if (segment->repeat >= 0 && stream->segment_repeat_index >= segment->repeat) {
2265 stream->segment_repeat_index = 0;
2266 stream->segment_index++;
2267 if (segments_count > 0 && stream->segment_index >= segments_count) {
2268 ret = GST_FLOW_EOS;
2269 goto done;
2270 }
2271 } else {
2272 stream->segment_repeat_index++;
2273 }
2274 } else {
2275 if (stream->segment_repeat_index == 0) {
2276 stream->segment_index--;
2277 if (stream->segment_index < 0) {
2278 ret = GST_FLOW_EOS;
2279 goto done;
2280 }
2281
2282 segment = g_ptr_array_index (stream->segments, stream->segment_index);
2283 /* negative repeats only seem to make sense at the end of a list,
2284 * so this one will probably not be. Needs some sanity checking
2285 * when loading the XML data. */
2286 if (segment->repeat >= 0) {
2287 stream->segment_repeat_index = segment->repeat;
2288 } else {
2289 GstClockTime start = segment->start;
2290 GstClockTime end =
2291 gst_mpd_client2_get_segment_end_time (client, stream->segments,
2292 segment,
2293 stream->segment_index);
2294 stream->segment_repeat_index =
2295 (guint) (end - start) / segment->duration;
2296 }
2297 } else {
2298 stream->segment_repeat_index--;
2299 }
2300 }
2301
2302 done:
2303 GST_DEBUG ("Advanced to segment: %d / %d r:%d (ret: %s)",
2304 stream->segment_index, segments_count,
2305 stream->segment_repeat_index, gst_flow_get_name (ret));
2306 return ret;
2307 }
2308
2309 gboolean
2310 gst_mpd_client2_get_next_header (GstMPDClient2 * client, gchar ** uri,
2311 guint stream_idx, gint64 * range_start, gint64 * range_end)
2312 {
2313 GstActiveStream *stream;
2314 GstStreamPeriod *stream_period GST_UNUSED_CHECKS;
2315
2316 stream = gst_mpd_client2_get_active_stream_by_index (client, stream_idx);
2317 g_return_val_if_fail (stream != NULL, FALSE);
2318 g_return_val_if_fail (stream->cur_representation != NULL, FALSE);
2319 #ifndef G_DISABLE_CHECKS
2320 stream_period = gst_mpd_client2_get_stream_period (client);
2321 g_return_val_if_fail (stream_period != NULL, FALSE);
2322 g_return_val_if_fail (stream_period->period != NULL, FALSE);
2323 #endif
2324
2325 *range_start = 0;
2326 *range_end = -1;
2327
2328 GST_DEBUG ("Looking for current representation header");
2329 *uri = NULL;
2330 if (stream->cur_segment_base) {
2331 if (stream->cur_segment_base->Initialization) {
2332 *uri = gst_mpdparser_get_initializationURL (stream,
2333 stream->cur_segment_base->Initialization);
2334 if (stream->cur_segment_base->Initialization->range) {
2335 *range_start =
2336 stream->cur_segment_base->Initialization->range->first_byte_pos;
2337 *range_end =
2338 stream->cur_segment_base->Initialization->range->last_byte_pos;
2339 }
2340 } else if (stream->cur_segment_base->indexRange) {
2341 *uri = gst_mpdparser_get_initializationURL (stream,
2342 stream->cur_segment_base->Initialization);
2343 *range_start = 0;
2344 *range_end = stream->cur_segment_base->indexRange->first_byte_pos - 1;
2345 }
2346 } else if (stream->cur_seg_template
2347 && stream->cur_seg_template->initialization) {
2348 *uri =
2349 gst_mpdparser_build_URL_from_template (stream->cur_seg_template->
2350 initialization, stream->cur_representation->id, 0,
2351 stream->cur_representation->bandwidth, 0);
2352 }
2353
2354 return *uri == NULL ? FALSE : TRUE;
2355 }
2356
2357 gboolean
2358 gst_mpd_client2_get_next_header_index (GstMPDClient2 * client, gchar ** uri,
2359 guint stream_idx, gint64 * range_start, gint64 * range_end)
2360 {
2361 GstActiveStream *stream;
2362 GstStreamPeriod *stream_period GST_UNUSED_CHECKS;
2363
2364 stream = gst_mpd_client2_get_active_stream_by_index (client, stream_idx);
2365 g_return_val_if_fail (stream != NULL, FALSE);
2366 g_return_val_if_fail (stream->cur_representation != NULL, FALSE);
2367 #ifndef G_DISABLE_CHECKS
2368 stream_period = gst_mpd_client2_get_stream_period (client);
2369 g_return_val_if_fail (stream_period != NULL, FALSE);
2370 g_return_val_if_fail (stream_period->period != NULL, FALSE);
2371 #endif
2372
2373 *range_start = 0;
2374 *range_end = -1;
2375
2376 GST_DEBUG ("Looking for current representation index");
2377 *uri = NULL;
2378 if (stream->cur_segment_base && stream->cur_segment_base->indexRange) {
2379 *uri = gst_mpdparser_get_initializationURL (stream,
2380 stream->cur_segment_base->RepresentationIndex);
2381 *range_start = stream->cur_segment_base->indexRange->first_byte_pos;
2382 *range_end = stream->cur_segment_base->indexRange->last_byte_pos;
2383 } else if (stream->cur_seg_template && stream->cur_seg_template->index) {
2384 *uri =
2385 gst_mpdparser_build_URL_from_template (stream->cur_seg_template->index,
2386 stream->cur_representation->id, 0,
2387 stream->cur_representation->bandwidth, 0);
2388 }
2389
2390 return *uri == NULL ? FALSE : TRUE;
2391 }
2392
2393 GstClockTime
2394 gst_mpd_client2_get_next_fragment_duration (GstMPDClient2 * client,
2395 GstActiveStream * stream)
2396 {
2397 GstMediaSegment *media_segment = NULL;
2398 gint seg_idx;
2399
2400 g_return_val_if_fail (stream != NULL, 0);
2401
2402 seg_idx = stream->segment_index;
2403
2404 if (stream->segments) {
2405 if (seg_idx < stream->segments->len && seg_idx >= 0)
2406 media_segment = g_ptr_array_index (stream->segments, seg_idx);
2407
2408 return media_segment == NULL ? 0 : media_segment->duration;
2409 } else {
2410 GstClockTime duration =
2411 gst_mpd_client2_get_segment_duration (client, stream, NULL);
2412 guint segments_count = gst_mpd_client2_get_segments_counts (client, stream);
2413
2414 g_return_val_if_fail (GST_MPD_MULT_SEGMENT_BASE_NODE
2415 (stream->cur_seg_template)->SegmentTimeline == NULL, 0);
2416
2417 if (!GST_CLOCK_TIME_IS_VALID (duration) || (segments_count > 0
2418 && seg_idx >= segments_count)) {
2419 return 0;
2420 }
2421 return duration;
2422 }
2423 }
2424
2425 GstClockTime
2426 gst_mpd_client2_get_media_presentation_duration (GstMPDClient2 * client)
2427 {
2428 GstClockTime duration;
2429
2430 g_return_val_if_fail (client != NULL, GST_CLOCK_TIME_NONE);
2431
2432 if (client->mpd_root_node->mediaPresentationDuration != -1) {
2433 duration = client->mpd_root_node->mediaPresentationDuration * GST_MSECOND;
2434 } else {
2435 /* We can only get the duration for on-demand streams */
2436 duration = GST_CLOCK_TIME_NONE;
2437 }
2438
2439 return duration;
2440 }
2441
2442 gboolean
2443 gst_mpd_client2_set_period_id (GstMPDClient2 * client, const gchar * period_id)
2444 {
2445 GstStreamPeriod *next_stream_period;
2446 gboolean ret = FALSE;
2447 GList *iter;
2448 guint period_idx;
2449
2450 g_return_val_if_fail (client != NULL, FALSE);
2451 g_return_val_if_fail (client->periods != NULL, FALSE);
2452 g_return_val_if_fail (period_id != NULL, FALSE);
2453
2454 if (!gst_mpd_client2_setup_media_presentation (client, GST_CLOCK_TIME_NONE,
2455 -1, period_id))
2456 return FALSE;
2457
2458 for (period_idx = 0, iter = client->periods; iter;
2459 period_idx++, iter = g_list_next (iter)) {
2460 next_stream_period = iter->data;
2461
2462 if (next_stream_period->period->id
2463 && strcmp (next_stream_period->period->id, period_id) == 0) {
2464 ret = TRUE;
2465 client->period_idx = period_idx;
2466 break;
2467 }
2468 }
2469
2470 return ret;
2471 }
2472
2473 gboolean
2474 gst_mpd_client2_set_period_index (GstMPDClient2 * client, guint period_idx)
2475 {
2476 GstStreamPeriod *next_stream_period;
2477 gboolean ret = FALSE;
2478
2479 g_return_val_if_fail (client != NULL, FALSE);
2480 g_return_val_if_fail (client->periods != NULL, FALSE);
2481
2482 if (!gst_mpd_client2_setup_media_presentation (client, -1, period_idx, NULL))
2483 return FALSE;
2484
2485 next_stream_period = g_list_nth_data (client->periods, period_idx);
2486 if (next_stream_period != NULL) {
2487 client->period_idx = period_idx;
2488 ret = TRUE;
2489 }
2490
2491 return ret;
2492 }
2493
2494 guint
2495 gst_mpd_client2_get_period_index (GstMPDClient2 * client)
2496 {
2497 guint period_idx;
2498
2499 g_return_val_if_fail (client != NULL, 0);
2500 period_idx = client->period_idx;
2501
2502 return period_idx;
2503 }
2504
2505 const gchar *
2506 gst_mpd_client2_get_period_id (GstMPDClient2 * client)
2507 {
2508 GstStreamPeriod *period;
2509 gchar *period_id = NULL;
2510
2511 g_return_val_if_fail (client != NULL, 0);
2512 period = g_list_nth_data (client->periods, client->period_idx);
2513 if (period && period->period)
2514 period_id = period->period->id;
2515
2516 return period_id;
2517 }
2518
2519 gboolean
2520 gst_mpd_client2_has_next_period (GstMPDClient2 * client)
2521 {
2522 GList *next_stream_period;
2523 g_return_val_if_fail (client != NULL, FALSE);
2524 g_return_val_if_fail (client->periods != NULL, FALSE);
2525
2526 if (!gst_mpd_client2_setup_media_presentation (client, GST_CLOCK_TIME_NONE,
2527 client->period_idx + 1, NULL))
2528 return FALSE;
2529
2530 next_stream_period =
2531 g_list_nth_data (client->periods, client->period_idx + 1);
2532 return next_stream_period != NULL;
2533 }
2534
2535 gboolean
2536 gst_mpd_client2_has_previous_period (GstMPDClient2 * client)
2537 {
2538 GList *next_stream_period;
2539 g_return_val_if_fail (client != NULL, FALSE);
2540 g_return_val_if_fail (client->periods != NULL, FALSE);
2541
2542 if (!gst_mpd_client2_setup_media_presentation (client, GST_CLOCK_TIME_NONE,
2543 client->period_idx - 1, NULL))
2544 return FALSE;
2545
2546 next_stream_period =
2547 g_list_nth_data (client->periods, client->period_idx - 1);
2548
2549 return next_stream_period != NULL;
2550 }
2551
2552 gint
2553 gst_mpd_client2_get_rep_idx_with_min_bandwidth (GList * Representations)
2554 {
2555 GList *list = NULL, *lowest = NULL;
2556 GstMPDRepresentationNode *rep = NULL;
2557 gint lowest_bandwidth = -1;
2558
2559 if (Representations == NULL)
2560 return -1;
2561
2562 for (list = g_list_first (Representations); list; list = g_list_next (list)) {
2563 rep = (GstMPDRepresentationNode *) list->data;
2564 if (rep && (!lowest || rep->bandwidth < lowest_bandwidth)) {
2565 lowest = list;
2566 lowest_bandwidth = rep->bandwidth;
2567 }
2568 }
2569
2570 return lowest ? g_list_position (Representations, lowest) : -1;
2571 }
2572
2573 gint
2574 gst_mpd_client2_get_rep_idx_with_max_bandwidth (GList * Representations,
2575 gint64 max_bandwidth, gint max_video_width, gint max_video_height, gint
2576 max_video_framerate_n, gint max_video_framerate_d)
2577 {
2578 GList *list = NULL, *best = NULL;
2579 GstMPDRepresentationNode *representation;
2580 gint best_bandwidth = 0;
2581
2582 GST_DEBUG ("Selecting rep with restrictions: bandwidth=%" G_GINT64_FORMAT ", "
2583 "width=%i, height=%i, framerate=%i/%i", max_bandwidth, max_video_width,
2584 max_video_height, max_video_framerate_n, max_video_framerate_d);
2585
2586 if (Representations == NULL)
2587 return -1;
2588
2589 if (max_bandwidth <= 0) /* 0 => get lowest representation available */
2590 return gst_mpd_client2_get_rep_idx_with_min_bandwidth (Representations);
2591
2592 for (list = g_list_first (Representations); list; list = g_list_next (list)) {
2593 GstXMLFrameRate *framerate = NULL;
2594
2595 representation = (GstMPDRepresentationNode *) list->data;
2596
2597 /* FIXME: Really? */
2598 if (!representation)
2599 continue;
2600
2601 framerate = GST_MPD_REPRESENTATION_BASE_NODE (representation)->frameRate;
2602 if (!framerate)
2603 framerate =
2604 GST_MPD_REPRESENTATION_BASE_NODE (representation)->maxFrameRate;
2605
2606 if (framerate && max_video_framerate_n > 0) {
2607 if (gst_util_fraction_compare (framerate->num, framerate->den,
2608 max_video_framerate_n, max_video_framerate_d) > 0)
2609 continue;
2610 }
2611
2612 if (max_video_width > 0
2613 && GST_MPD_REPRESENTATION_BASE_NODE (representation)->width >
2614 max_video_width)
2615 continue;
2616 if (max_video_height > 0
2617 && GST_MPD_REPRESENTATION_BASE_NODE (representation)->height >
2618 max_video_height)
2619 continue;
2620
2621 if (representation->bandwidth <= max_bandwidth &&
2622 representation->bandwidth > best_bandwidth) {
2623 best = list;
2624 best_bandwidth = representation->bandwidth;
2625 }
2626 }
2627
2628 return best ? g_list_position (Representations, best) : -1;
2629 }
2630
2631 void
2632 gst_mpd_client2_seek_to_first_segment (GstMPDClient2 * client)
2633 {
2634 GList *list;
2635
2636 g_return_if_fail (client != NULL);
2637 g_return_if_fail (client->active_streams != NULL);
2638
2639 for (list = g_list_first (client->active_streams); list;
2640 list = g_list_next (list)) {
2641 GstActiveStream *stream = (GstActiveStream *) list->data;
2642 if (stream) {
2643 stream->segment_index = 0;
2644 stream->segment_repeat_index = 0;
2645 }
2646 }
2647 }
2648
2649 static guint
2650 gst_mpd_client2_get_segments_counts (GstMPDClient2 * client,
2651 GstActiveStream * stream)
2652 {
2653 GstStreamPeriod *stream_period;
2654
2655 g_return_val_if_fail (stream != NULL, 0);
2656
2657 if (stream->segments)
2658 return stream->segments->len;
2659 g_return_val_if_fail (GST_MPD_MULT_SEGMENT_BASE_NODE
2660 (stream->cur_seg_template)->SegmentTimeline == NULL, 0);
2661
2662 stream_period = gst_mpd_client2_get_stream_period (client);
2663 if (stream_period->duration != -1)
2664 return gst_util_uint64_scale_ceil (stream_period->duration, 1,
2665 gst_mpd_client2_get_segment_duration (client, stream, NULL));
2666
2667 return 0;
2668 }
2669
2670 gboolean
2671 gst_mpd_client2_is_live (GstMPDClient2 * client)
2672 {
2673 g_return_val_if_fail (client != NULL, FALSE);
2674 g_return_val_if_fail (client->mpd_root_node != NULL, FALSE);
2675
2676 return client->mpd_root_node->type == GST_MPD_FILE_TYPE_DYNAMIC;
2677 }
2678
2679 guint
2680 gst_mpd_client2_get_nb_active_stream (GstMPDClient2 * client)
2681 {
2682 g_return_val_if_fail (client != NULL, 0);
2683
2684 return g_list_length (client->active_streams);
2685 }
2686
2687 guint
2688 gst_mpd_client2_get_nb_adaptationSet (GstMPDClient2 * client)
2689 {
2690 GstStreamPeriod *stream_period;
2691
2692 stream_period = gst_mpd_client2_get_stream_period (client);
2693 g_return_val_if_fail (stream_period != NULL, 0);
2694 g_return_val_if_fail (stream_period->period != NULL, 0);
2695
2696 return g_list_length (stream_period->period->AdaptationSets);
2697 }
2698
2699 GstActiveStream *
2700 gst_mpd_client2_get_active_stream_by_index (GstMPDClient2 * client,
2701 guint stream_idx)
2702 {
2703 g_return_val_if_fail (client != NULL, NULL);
2704 g_return_val_if_fail (client->active_streams != NULL, NULL);
2705
2706 return g_list_nth_data (client->active_streams, stream_idx);
2707 }
2708
2709 gboolean
2710 gst_mpd_client2_active_stream_contains_subtitles (GstActiveStream * stream)
2711 {
2712 const gchar *mimeType;
2713 const gchar *adapt_set_codecs;
2714 const gchar *rep_codecs;
2715
2716 mimeType =
2717 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_representation)->mimeType;
2718 if (!mimeType)
2719 mimeType =
2720 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->mimeType;
2721
2722 if (g_strcmp0 (mimeType, "application/ttml+xml") == 0 ||
2723 g_strcmp0 (mimeType, "application/x-subtitle-vtt") == 0 ||
2724 g_strcmp0 (mimeType, "text/vtt") == 0)
2725 return TRUE;
2726
2727 adapt_set_codecs =
2728 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->codecs;
2729 rep_codecs =
2730 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_representation)->codecs;
2731
2732 if (adapt_set_codecs) {
2733 if (g_str_has_prefix (adapt_set_codecs, "stpp"))
2734 return TRUE;
2735 if (g_str_has_prefix (adapt_set_codecs, "wvtt"))
2736 return TRUE;
2737 }
2738 if (rep_codecs) {
2739 if (g_str_has_prefix (rep_codecs, "stpp"))
2740 return TRUE;
2741 if (g_str_has_prefix (rep_codecs, "wvtt"))
2742 return TRUE;
2743 }
2744
2745 return FALSE;
2746 }
2747
2748 GstCaps *
2749 gst_mpd_client2_get_codec_caps (GstActiveStream * stream)
2750 {
2751 GstCaps *ret = NULL;
2752 GList *iter;
2753 GstMPDAdaptationSetNode *adapt_set = stream->cur_adapt_set;
2754
2755 if (adapt_set == NULL) {
2756 GST_WARNING ("No adaptation set => No caps");
2757 return NULL;
2758 }
2759 /* The adaptation set may already have caps, in which case it is the largest
2760 * set of possible caps of all representations (representations must have properties
2761 * that are smaller than the adaptation set) */
2762
2763 if (adapt_set->parent_instance.caps) {
2764 ret = gst_caps_copy (adapt_set->parent_instance.caps);
2765 GST_DEBUG ("Adaptation set caps %" GST_PTR_FORMAT, ret);
2766 return ret;
2767 }
2768
2769 /* Iterate over the current adaptation set representation */
2770 for (iter = stream->cur_adapt_set->Representations; iter; iter = iter->next) {
2771 GstMPDRepresentationBaseNode *rep =
2772 (GstMPDRepresentationBaseNode *) iter->data;
2773
2774 if (rep->caps) {
2775 GST_DEBUG ("Adding representation caps %" GST_PTR_FORMAT, rep->caps);
2776 if (ret)
2777 ret = gst_caps_merge (ret, gst_caps_ref (rep->caps));
2778 else
2779 ret = gst_caps_copy (rep->caps);
2780 }
2781 }
2782
2783 GST_DEBUG ("Merged caps %" GST_PTR_FORMAT, ret);
2784 return ret;
2785 }
2786
2787 GstCaps *
2788 gst_mpd_client2_get_stream_caps (GstActiveStream * stream)
2789 {
2790 const gchar *mimeType, *caps_string;
2791 GstCaps *ret = NULL;
2792
2793 if (stream == NULL || stream->cur_adapt_set == NULL
2794 || stream->cur_representation == NULL)
2795 return NULL;
2796
2797 mimeType =
2798 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_representation)->mimeType;
2799 if (mimeType == NULL) {
2800 mimeType =
2801 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->mimeType;
2802 }
2803
2804 caps_string = gst_mpd_helper_mimetype_to_caps (mimeType);
2805
2806 if ((g_strcmp0 (caps_string, "application/mp4") == 0)
2807 && gst_mpd_client2_active_stream_contains_subtitles (stream))
2808 caps_string = "video/quicktime";
2809
2810 if (caps_string)
2811 ret = gst_caps_from_string (caps_string);
2812
2813 return ret;
2814 }
2815
2816 gboolean
2817 gst_mpd_client2_get_bitstream_switching_flag (GstActiveStream * stream)
2818 {
2819 if (stream == NULL || stream->cur_adapt_set == NULL)
2820 return FALSE;
2821
2822 return stream->cur_adapt_set->bitstreamSwitching;
2823 }
2824
2825 guint
2826 gst_mpd_client2_get_video_stream_width (GstActiveStream * stream)
2827 {
2828 guint width;
2829
2830 if (stream == NULL || stream->cur_adapt_set == NULL
2831 || stream->cur_representation == NULL)
2832 return 0;
2833
2834 width = GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_representation)->width;
2835 if (width == 0) {
2836 width = GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->width;
2837 }
2838
2839 return width;
2840 }
2841
2842 guint
2843 gst_mpd_client2_get_video_stream_height (GstActiveStream * stream)
2844 {
2845 guint height;
2846
2847 if (stream == NULL || stream->cur_adapt_set == NULL
2848 || stream->cur_representation == NULL)
2849 return 0;
2850
2851 height =
2852 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_representation)->height;
2853 if (height == 0) {
2854 height = GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->height;
2855 }
2856
2857 return height;
2858 }
2859
2860 gboolean
2861 gst_mpd_client2_get_video_stream_framerate (GstActiveStream * stream,
2862 gint * fps_num, gint * fps_den)
2863 {
2864 if (stream == NULL)
2865 return FALSE;
2866
2867 if (stream->cur_adapt_set &&
2868 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->frameRate !=
2869 NULL) {
2870 *fps_num =
2871 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->
2872 frameRate->num;
2873 *fps_den =
2874 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->
2875 frameRate->den;
2876 return TRUE;
2877 }
2878
2879 if (stream->cur_adapt_set &&
2880 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->maxFrameRate !=
2881 NULL) {
2882 *fps_num =
2883 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->
2884 maxFrameRate->num;
2885 *fps_den =
2886 GST_MPD_REPRESENTATION_BASE_NODE (stream->cur_adapt_set)->
2887 maxFrameRate->den;
2888 return TRUE;
2889 }
2890
2891 if (stream->cur_representation &&
2892 GST_MPD_REPRESENTATION_BASE_NODE (stream->
2893 cur_representation)->frameRate != NULL) {
2894 *fps_num =
2895 GST_MPD_REPRESENTATION_BASE_NODE (stream->
2896 cur_representation)->frameRate->num;
2897 *fps_den =
2898 GST_MPD_REPRESENTATION_BASE_NODE (stream->
2899 cur_representation)->frameRate->den;
2900 return TRUE;
2901 }
2902
2903 if (stream->cur_representation &&
2904 GST_MPD_REPRESENTATION_BASE_NODE (stream->
2905 cur_representation)->maxFrameRate != NULL) {
2906 *fps_num =
2907 GST_MPD_REPRESENTATION_BASE_NODE (stream->
2908 cur_representation)->maxFrameRate->num;
2909 *fps_den =
2910 GST_MPD_REPRESENTATION_BASE_NODE (stream->
2911 cur_representation)->maxFrameRate->den;
2912 return TRUE;
2913 }
2914
2915 return FALSE;
2916 }
2917
2918 guint
2919 gst_mpd_client2_get_audio_stream_rate (GstActiveStream * stream)
2920 {
2921 const gchar *rate;
2922
2923 if (stream == NULL || stream->cur_adapt_set == NULL
2924 || stream->cur_representation == NULL)
2925 return 0;
2926
2927 rate =
2928 GST_MPD_REPRESENTATION_BASE_NODE (stream->
2929 cur_representation)->audioSamplingRate;
2930 if (rate == NULL) {
2931 rate =
2932 GST_MPD_REPRESENTATION_BASE_NODE (stream->
2933 cur_adapt_set)->audioSamplingRate;
2934 }
2935
2936 return rate ? atoi (rate) : 0;
2937 }
2938
2939 guint
2940 gst_mpd_client2_get_audio_stream_num_channels (GstActiveStream * stream)
2941 {
2942 if (stream == NULL || stream->cur_adapt_set == NULL
2943 || stream->cur_representation == NULL)
2944 return 0;
2945 /* TODO: here we have to parse the AudioChannelConfiguration descriptors */
2946 return 0;
2947 }
2948
2949 guint
2950 gst_mpd_client2_get_list_and_nb_of_audio_language (GstMPDClient2 * client,
2951 GList ** lang)
2952 {
2953 GstStreamPeriod *stream_period;
2954 GstMPDAdaptationSetNode *adapt_set;
2955 GList *adaptation_sets, *list;
2956 const gchar *this_mimeType = "audio";
2957 gchar *mimeType = NULL;
2958 guint nb_adaptation_set = 0;
2959
2960 stream_period = gst_mpd_client2_get_stream_period (client);
2961 g_return_val_if_fail (stream_period != NULL, 0);
2962 g_return_val_if_fail (stream_period->period != NULL, 0);
2963
2964 adaptation_sets =
2965 gst_mpd_client2_get_adaptation_sets_for_period (client, stream_period);
2966 for (list = adaptation_sets; list; list = g_list_next (list)) {
2967 adapt_set = (GstMPDAdaptationSetNode *) list->data;
2968 if (adapt_set && adapt_set->lang) {
2969 gchar *this_lang = adapt_set->lang;
2970 GstMPDRepresentationNode *rep;
2971 rep =
2972 gst_mpd_client2_get_lowest_representation
2973 (adapt_set->Representations);
2974 mimeType = NULL;
2975 if (GST_MPD_REPRESENTATION_BASE_NODE (rep))
2976 mimeType = GST_MPD_REPRESENTATION_BASE_NODE (rep)->mimeType;
2977 if (!mimeType && GST_MPD_REPRESENTATION_BASE_NODE (adapt_set)) {
2978 mimeType = GST_MPD_REPRESENTATION_BASE_NODE (adapt_set)->mimeType;
2979 }
2980
2981 if (gst_mpd_helper_strncmp_ext (mimeType, this_mimeType) == 0) {
2982 nb_adaptation_set++;
2983 *lang = g_list_append (*lang, this_lang);
2984 }
2985 }
2986 }
2987
2988 return nb_adaptation_set;
2989 }
2990
2991
2992 GstDateTime *
2993 gst_mpd_client2_get_next_segment_availability_start_time (GstMPDClient2 *
2994 client, GstActiveStream * stream)
2995 {
2996 GstDateTime *availability_start_time, *rv;
2997 gint seg_idx;
2998 GstMediaSegment *segment;
2999 GstClockTime segmentEndTime;
3000 const GstStreamPeriod *stream_period;
3001 GstClockTime period_start = 0;
3002
3003 g_return_val_if_fail (client != NULL, NULL);
3004 g_return_val_if_fail (stream != NULL, NULL);
3005
3006 stream_period = gst_mpd_client2_get_stream_period (client);
3007 if (stream_period && stream_period->period) {
3008 period_start = stream_period->start;
3009 }
3010
3011 seg_idx = stream->segment_index;
3012
3013 if (stream->segments && seg_idx < stream->segments->len) {
3014 segment = g_ptr_array_index (stream->segments, seg_idx);
3015
3016 if (segment->repeat >= 0) {
3017 segmentEndTime = segment->start + (stream->segment_repeat_index + 1) *
3018 segment->duration;
3019 } else if (seg_idx < stream->segments->len - 1) {
3020 const GstMediaSegment *next_segment =
3021 g_ptr_array_index (stream->segments, seg_idx + 1);
3022 segmentEndTime = next_segment->start;
3023 } else {
3024 g_return_val_if_fail (stream_period != NULL, NULL);
3025 segmentEndTime = period_start + stream_period->duration;
3026 }
3027 } else {
3028 GstClockTime seg_duration;
3029 seg_duration = gst_mpd_client2_get_segment_duration (client, stream, NULL);
3030 if (seg_duration == 0)
3031 return NULL;
3032 segmentEndTime = period_start + (1 + seg_idx) * seg_duration;
3033 }
3034
3035 availability_start_time =
3036 gst_mpd_client2_get_availability_start_time (client);
3037 if (availability_start_time == NULL) {
3038 GST_WARNING_OBJECT (client, "Failed to get availability_start_time");
3039 return NULL;
3040 }
3041
3042 rv = gst_mpd_client2_add_time_difference (availability_start_time,
3043 segmentEndTime);
3044 gst_date_time_unref (availability_start_time);
3045 if (rv == NULL) {
3046 GST_WARNING_OBJECT (client, "Failed to offset availability_start_time");
3047 return NULL;
3048 }
3049
3050 return rv;
3051 }
3052
3053 gboolean
3054 gst_mpd_client2_seek_to_time (GstMPDClient2 * client, GDateTime * time)
3055 {
3056 GDateTime *start;
3057 GTimeSpan ts_microseconds;
3058 GstClockTime ts;
3059 gboolean ret = TRUE;
3060 GList *stream;
3061
3062 g_return_val_if_fail (gst_mpd_client2_is_live (client), FALSE);
3063 g_return_val_if_fail (client->mpd_root_node->availabilityStartTime != NULL,
3064 FALSE);
3065
3066 start =
3067 gst_date_time_to_g_date_time (client->mpd_root_node->
3068 availabilityStartTime);
3069
3070 ts_microseconds = g_date_time_difference (time, start);
3071 g_date_time_unref (start);
3072
3073 /* Clamp to availability start time, otherwise calculations wrap around */
3074 if (ts_microseconds < 0)
3075 ts_microseconds = 0;
3076
3077 ts = ts_microseconds * GST_USECOND;
3078 for (stream = client->active_streams; stream; stream = g_list_next (stream)) {
3079 ret =
3080 ret & gst_mpd_client2_stream_seek (client, stream->data, TRUE, 0, ts,
3081 NULL);
3082 }
3083 return ret;
3084 }
3085
3086 gboolean
3087 gst_mpd_client2_has_isoff_ondemand_profile (GstMPDClient2 * client)
3088 {
3089 return client->profile_isoff_ondemand;
3090 }
3091
3092 /**
3093 * gst_mpd_client2_parse_default_presentation_delay:
3094 * @client: #GstMPDClient2 that has a parsed manifest
3095 * @default_presentation_delay: A string that specifies a time period
3096 * in fragments (e.g. "5 f"), seconds ("12 s") or milliseconds
3097 * ("12000 ms")
3098 * Returns: the parsed string in milliseconds
3099 *
3100 * Since: 1.6
3101 */
3102 gint64
3103 gst_mpd_client2_parse_default_presentation_delay (GstMPDClient2 * client,
3104 const gchar * default_presentation_delay)
3105 {
3106 gint64 value;
3107 char *endptr = NULL;
3108
3109 g_return_val_if_fail (client != NULL, 0);
3110 g_return_val_if_fail (default_presentation_delay != NULL, 0);
3111 value = strtol (default_presentation_delay, &endptr, 10);
3112 if (endptr == default_presentation_delay || value == 0) {
3113 return 0;
3114 }
3115 while (*endptr == ' ')
3116 endptr++;
3117 if (*endptr == 's' || *endptr == 'S') {
3118 value *= 1000; /* convert to ms */
3119 } else if (*endptr == 'f' || *endptr == 'F') {
3120 gint64 segment_duration;
3121 g_assert (client->mpd_root_node != NULL);
3122 segment_duration = client->mpd_root_node->maxSegmentDuration;
3123 value *= segment_duration;
3124 } else if (*endptr != 'm' && *endptr != 'M') {
3125 GST_ERROR ("Unable to parse default presentation delay: %s",
3126 default_presentation_delay);
3127 value = 0;
3128 }
3129 return value;
3130 }
3131
3132 GstClockTime
3133 gst_mpd_client2_get_maximum_segment_duration (GstMPDClient2 * client)
3134 {
3135 GstClockTime ret = GST_CLOCK_TIME_NONE, dur;
3136 GList *stream;
3137
3138 g_return_val_if_fail (client != NULL, GST_CLOCK_TIME_NONE);
3139 g_return_val_if_fail (client->mpd_root_node != NULL, GST_CLOCK_TIME_NONE);
3140
3141 if (client->mpd_root_node->maxSegmentDuration != GST_MPD_DURATION_NONE) {
3142 return client->mpd_root_node->maxSegmentDuration * GST_MSECOND;
3143 }
3144
3145 /* According to the DASH specification, if maxSegmentDuration is not present:
3146 "If not present, then the maximum Segment duration shall be the maximum
3147 duration of any Segment documented in this MPD"
3148 */
3149 for (stream = client->active_streams; stream; stream = g_list_next (stream)) {
3150 dur = gst_mpd_client2_get_segment_duration (client, stream->data, NULL);
3151 if (dur != GST_CLOCK_TIME_NONE && (dur > ret || ret == GST_CLOCK_TIME_NONE)) {
3152 ret = dur;
3153 }
3154 }
3155 return ret;
3156 }
3157
3158 guint
3159 gst_mpd_client2_get_period_index_at_time (GstMPDClient2 * client,
3160 GstDateTime * time)
3161 {
3162 GList *iter;
3163 guint period_idx = G_MAXUINT;
3164 guint idx;
3165 gint64 time_offset;
3166 GstDateTime *avail_start =
3167 gst_mpd_client2_get_availability_start_time (client);
3168 GstStreamPeriod *stream_period;
3169
3170 if (avail_start == NULL)
3171 return 0;
3172
3173 time_offset = gst_mpd_client2_calculate_time_difference (avail_start, time);
3174 gst_date_time_unref (avail_start);
3175
3176 if (time_offset < 0)
3177 return 0;
3178
3179 if (!gst_mpd_client2_setup_media_presentation (client, time_offset, -1, NULL))
3180 return 0;
3181
3182 for (idx = 0, iter = client->periods; iter; idx++, iter = g_list_next (iter)) {
3183 stream_period = iter->data;
3184 if (stream_period->start <= time_offset
3185 && (!GST_CLOCK_TIME_IS_VALID (stream_period->duration)
3186 || stream_period->start + stream_period->duration > time_offset)) {
3187 period_idx = idx;
3188 break;
3189 }
3190 }
3191
3192 return period_idx;
3193 }
3194
3195 /* add or set node methods */
3196
3197 gboolean
3198 gst_mpd_client2_set_root_node (GstMPDClient2 * client,
3199 const gchar * property_name, ...)
3200 {
3201 va_list myargs;
3202 g_return_val_if_fail (client != NULL, FALSE);
3203
3204 if (!client->mpd_root_node)
3205 client->mpd_root_node = gst_mpd_root_node_new ();
3206
3207 va_start (myargs, property_name);
3208 g_object_set_valist (G_OBJECT (client->mpd_root_node), property_name, myargs);
3209 va_end (myargs);
3210
3211 return TRUE;
3212 }
3213
3214 gboolean
3215 gst_mpd_client2_add_baseurl_node (GstMPDClient2 * client,
3216 const gchar * property_name, ...)
3217 {
3218 GstMPDBaseURLNode *baseurl_node = NULL;
3219 va_list myargs;
3220
3221 g_return_val_if_fail (client != NULL, FALSE);
3222 g_return_val_if_fail (client->mpd_root_node != NULL, FALSE);
3223
3224 va_start (myargs, property_name);
3225
3226 baseurl_node = gst_mpd_baseurl_node_new ();
3227 g_object_set_valist (G_OBJECT (baseurl_node), property_name, myargs);
3228 client->mpd_root_node->BaseURLs =
3229 g_list_append (client->mpd_root_node->BaseURLs, baseurl_node);
3230
3231 va_end (myargs);
3232 return TRUE;
3233 }
3234
3235 /* returns a period id */
3236 gchar *
3237 gst_mpd_client2_set_period_node (GstMPDClient2 * client,
3238 gchar * period_id, const gchar * property_name, ...)
3239 {
3240 GstMPDPeriodNode *period_node = NULL;
3241 va_list myargs;
3242
3243 g_return_val_if_fail (client != NULL, NULL);
3244 g_return_val_if_fail (client->mpd_root_node != NULL, NULL);
3245
3246 period_node =
3247 GST_MPD_PERIOD_NODE (gst_mpd_client2_get_period_with_id
3248 (client->mpd_root_node->Periods, period_id));
3249 if (!period_node) {
3250 period_node = gst_mpd_period_node_new ();
3251 if (period_id)
3252 period_node->id = g_strdup (period_id);
3253 else
3254 period_node->id =
3255 _generate_new_string_id (client->mpd_root_node->Periods,
3256 "period_%.2d", gst_mpd_client2_get_period_with_id);
3257 client->mpd_root_node->Periods =
3258 g_list_append (client->mpd_root_node->Periods, period_node);
3259 }
3260
3261 va_start (myargs, property_name);
3262 g_object_set_valist (G_OBJECT (period_node), property_name, myargs);
3263 va_end (myargs);
3264
3265 return period_node->id;
3266 }
3267
3268 /* returns an adaptation set id */
3269 guint
3270 gst_mpd_client2_set_adaptation_set_node (GstMPDClient2 * client,
3271 gchar * period_id, guint adaptation_set_id, const gchar * property_name,
3272 ...)
3273 {
3274 GstMPDAdaptationSetNode *adap_node = NULL;
3275 GstMPDPeriodNode *period_node = NULL;
3276 va_list myargs;
3277
3278 g_return_val_if_fail (client != NULL, 0);
3279 g_return_val_if_fail (client->mpd_root_node != NULL, 0);
3280
3281 period_node =
3282 GST_MPD_PERIOD_NODE (gst_mpd_client2_get_period_with_id
3283 (client->mpd_root_node->Periods, period_id));
3284 g_return_val_if_fail (period_node != NULL, 0);
3285 adap_node =
3286 GST_MPD_ADAPTATION_SET_NODE (gst_mpd_client2_get_adaptation_set_with_id
3287 (period_node->AdaptationSets, adaptation_set_id));
3288 if (!adap_node) {
3289 adap_node = gst_mpd_adaptation_set_node_new ();
3290 if (adaptation_set_id)
3291 adap_node->id = adaptation_set_id;
3292 else
3293 adap_node->id =
3294 _generate_new_id (period_node->AdaptationSets,
3295 gst_mpd_client2_get_adaptation_set_with_id);
3296 GST_DEBUG_OBJECT (client, "Add a new adaptation set with id %d",
3297 adap_node->id);
3298 period_node->AdaptationSets =
3299 g_list_append (period_node->AdaptationSets, adap_node);
3300 }
3301
3302 va_start (myargs, property_name);
3303 g_object_set_valist (G_OBJECT (adap_node), property_name, myargs);
3304 va_end (myargs);
3305
3306 return adap_node->id;
3307 }
3308
3309 /* returns a representation id */
3310 gchar *
3311 gst_mpd_client2_set_representation_node (GstMPDClient2 * client,
3312 gchar * period_id, guint adaptation_set_id, gchar * representation_id,
3313 const gchar * property_name, ...)
3314 {
3315 GstMPDRepresentationNode *rep_node = NULL;
3316 GstMPDAdaptationSetNode *adap_set_node = NULL;
3317 GstMPDPeriodNode *period_node = NULL;
3318 va_list myargs;
3319
3320 g_return_val_if_fail (client != NULL, NULL);
3321 g_return_val_if_fail (client->mpd_root_node != NULL, NULL);
3322
3323 period_node =
3324 GST_MPD_PERIOD_NODE (gst_mpd_client2_get_period_with_id
3325 (client->mpd_root_node->Periods, period_id));
3326 adap_set_node =
3327 GST_MPD_ADAPTATION_SET_NODE (gst_mpd_client2_get_adaptation_set_with_id
3328 (period_node->AdaptationSets, adaptation_set_id));
3329 g_return_val_if_fail (adap_set_node != NULL, NULL);
3330 rep_node =
3331 gst_mpd_client2_get_representation_with_id
3332 (adap_set_node->Representations, representation_id);
3333 if (!rep_node) {
3334 rep_node = gst_mpd_representation_node_new ();
3335 if (representation_id)
3336 rep_node->id = g_strdup (representation_id);
3337 else
3338 rep_node->id =
3339 _generate_new_string_id (adap_set_node->Representations,
3340 "representation_%.2d",
3341 gst_mpd_client2_get_representation_with_id_filter);
3342 GST_DEBUG_OBJECT (client, "Add a new representation with id %s",
3343 rep_node->id);
3344 adap_set_node->Representations =
3345 g_list_append (adap_set_node->Representations, rep_node);
3346 }
3347
3348 va_start (myargs, property_name);
3349 g_object_set_valist (G_OBJECT (rep_node), property_name, myargs);
3350 va_end (myargs);
3351
3352 return rep_node->id;
3353 }
3354
3355 /* add/set a segment list node */
3356 gboolean
3357 gst_mpd_client2_set_segment_list (GstMPDClient2 * client,
3358 gchar * period_id, guint adap_set_id, gchar * rep_id,
3359 const gchar * property_name, ...)
3360 {
3361 GstMPDRepresentationNode *representation = NULL;
3362 GstMPDAdaptationSetNode *adaptation_set = NULL;
3363 GstMPDPeriodNode *period = NULL;
3364 va_list myargs;
3365
3366 g_return_val_if_fail (client != NULL, FALSE);
3367 g_return_val_if_fail (client->mpd_root_node != NULL, FALSE);
3368
3369 period =
3370 GST_MPD_PERIOD_NODE (gst_mpd_client2_get_period_with_id
3371 (client->mpd_root_node->Periods, period_id));
3372 adaptation_set =
3373 GST_MPD_ADAPTATION_SET_NODE (gst_mpd_client2_get_adaptation_set_with_id
3374 (period->AdaptationSets, adap_set_id));
3375 g_return_val_if_fail (adaptation_set != NULL, FALSE);
3376
3377 representation =
3378 gst_mpd_client2_get_representation_with_id
3379 (adaptation_set->Representations, rep_id);
3380 if (!representation->SegmentList) {
3381 representation->SegmentList = gst_mpd_segment_list_node_new ();
3382 }
3383
3384 va_start (myargs, property_name);
3385 g_object_set_valist (G_OBJECT (representation->SegmentList), property_name,
3386 myargs);
3387 va_end (myargs);
3388
3389 return TRUE;
3390 }
3391
3392 /* add/set a segment template node */
3393 gboolean
3394 gst_mpd_client2_set_segment_template (GstMPDClient2 * client,
3395 gchar * period_id, guint adap_set_id, gchar * rep_id,
3396 const gchar * property_name, ...)
3397 {
3398 GstMPDRepresentationNode *representation = NULL;
3399 GstMPDAdaptationSetNode *adaptation_set = NULL;
3400 GstMPDPeriodNode *period = NULL;
3401 va_list myargs;
3402
3403 g_return_val_if_fail (client != NULL, FALSE);
3404 g_return_val_if_fail (client->mpd_root_node != NULL, FALSE);
3405
3406 period =
3407 GST_MPD_PERIOD_NODE (gst_mpd_client2_get_period_with_id
3408 (client->mpd_root_node->Periods, period_id));
3409 adaptation_set =
3410 GST_MPD_ADAPTATION_SET_NODE (gst_mpd_client2_get_adaptation_set_with_id
3411 (period->AdaptationSets, adap_set_id));
3412 g_return_val_if_fail (adaptation_set != NULL, FALSE);
3413
3414 representation =
3415 gst_mpd_client2_get_representation_with_id
3416 (adaptation_set->Representations, rep_id);
3417 if (!representation->SegmentTemplate) {
3418 representation->SegmentTemplate = gst_mpd_segment_template_node_new ();
3419 }
3420
3421 va_start (myargs, property_name);
3422 g_object_set_valist (G_OBJECT (representation->SegmentTemplate),
3423 property_name, myargs);
3424 va_end (myargs);
3425
3426 return TRUE;
3427 }
3428
3429 /* add a segmentURL node with to a SegmentList node */
3430 gboolean
3431 gst_mpd_client2_add_segment_url (GstMPDClient2 * client,
3432 gchar * period_id, guint adap_set_id, gchar * rep_id,
3433 const gchar * property_name, ...)
3434 {
3435 GstMPDRepresentationNode *representation = NULL;
3436 GstMPDAdaptationSetNode *adaptation_set = NULL;
3437 GstMPDPeriodNode *period = NULL;
3438 GstMPDSegmentURLNode *segment_url = NULL;
3439 guint64 media_presentation_duration = 0;
3440 va_list myargs;
3441
3442 g_return_val_if_fail (client != NULL, FALSE);
3443 g_return_val_if_fail (client->mpd_root_node != NULL, FALSE);
3444
3445 period =
3446 GST_MPD_PERIOD_NODE (gst_mpd_client2_get_period_with_id
3447 (client->mpd_root_node->Periods, period_id));
3448 adaptation_set =
3449 GST_MPD_ADAPTATION_SET_NODE (gst_mpd_client2_get_adaptation_set_with_id
3450 (period->AdaptationSets, adap_set_id));
3451 g_return_val_if_fail (adaptation_set != NULL, FALSE);
3452
3453 representation =
3454 gst_mpd_client2_get_representation_with_id
3455 (adaptation_set->Representations, rep_id);
3456 if (!representation->SegmentList) {
3457 representation->SegmentList = gst_mpd_segment_list_node_new ();
3458 }
3459
3460 segment_url = gst_mpd_segment_url_node_new ();
3461
3462 va_start (myargs, property_name);
3463 g_object_set_valist (G_OBJECT (segment_url), property_name, myargs);
3464 va_end (myargs);
3465
3466 gst_mpd_segment_list_node_add_segment (representation->SegmentList,
3467 segment_url);
3468
3469 /* Set the media presentation time according to the new segment duration added */
3470 g_object_get (client->mpd_root_node, "media-presentation-duration",
3471 &media_presentation_duration, NULL);
3472 media_presentation_duration +=
3473 GST_MPD_MULT_SEGMENT_BASE_NODE (representation->SegmentList)->duration;
3474 g_object_set (client->mpd_root_node, "media-presentation-duration",
3475 media_presentation_duration, NULL);
3476
3477 return TRUE;
3478 }