2 * (c) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
4 * gsttoc.c: GstToc initialization and parsing/creation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
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 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 * @short_description: Generic table of contents support
25 * @see_also: #GstStructure, #GstEvent, #GstMessage, #GstQuery
27 * #GstToc functions are used to create/free #GstToc and #GstTocEntry structures.
28 * Also they are used to convert #GstToc into #GstStructure and vice versa.
30 * #GstToc lets you to inform other elements in pipeline or application that playing
31 * source has some kind of table of contents (TOC). These may be chapters, editions,
32 * angles or other types. For example: DVD chapters, Matroska chapters or cue sheet
33 * TOC. Such TOC will be useful for applications to display instead of just a
36 * Using TOC is very easy. Firstly, create #GstToc structure which represents root
37 * contents of the source. You can also attach TOC-specific tags to it. Then fill
38 * it with #GstTocEntry entries by appending them to the #GstToc using
39 * gst_toc_append_entry(), and appending subentries to a #GstTocEntry using
40 * gst_toc_entry_append_sub_entry().
42 * Note that root level of the TOC can contain only either editions or chapters. You
43 * should not mix them together at the same level. Otherwise you will get serialization
44 * /deserialization errors. Make sure that no one of the entries has negative start and
47 * Use gst_event_new_toc() to create a new TOC #GstEvent, and gst_event_parse_toc() to
48 * parse received TOC event. Use gst_event_new_toc_select() to create a new TOC select #GstEvent,
49 * and gst_event_parse_toc_select() to parse received TOC select event. The same rule for
50 * the #GstMessage: gst_message_new_toc() to create new TOC #GstMessage, and
51 * gst_message_parse_toc() to parse received TOC message.
53 * TOCs can have global scope or current scope. Global scope TOCs contain
54 * all entries that can possibly be selected using a toc select event, and
55 * are what an application is usually interested in. TOCs with current scope
56 * only contain the parts of the TOC relevant to the currently selected/playing
57 * stream; the current scope TOC is used by downstream elements such as muxers
58 * to write correct TOC entries when transcoding files, for example. When
59 * playing a DVD, the global TOC would contain a hierarchy of all titles,
60 * chapters and angles, for example, while the current TOC would only contain
61 * the chapters for the currently playing title if playback of a specific
62 * title was requested.
64 * Applications and plugins should not rely on TOCs having a certain kind of
65 * structure, but should allow for different alternatives. For example, a
66 * simple CUE sheet embedded in a file may be presented as a flat list of
67 * track entries, or could have a top-level edition node (or some other
68 * alternative type entry) with track entries underneath that node; or even
69 * multiple top-level edition nodes (or some other alternative type entries)
70 * each with track entries underneath, in case the source file has extracted
71 * a track listing from different sources).
78 #include "gst_private.h"
79 #include "gstenumtypes.h"
80 #include "gsttaglist.h"
81 #include "gststructure.h"
89 GstMiniObject mini_object;
96 GstClockTime start, stop;
103 GstMiniObject mini_object;
111 static GstToc *gst_toc_copy (const GstToc * toc);
112 static void gst_toc_free (GstToc * toc);
113 #undef gst_toc_entry_copy
114 static GstTocEntry *gst_toc_entry_copy (const GstTocEntry * toc);
115 static void gst_toc_entry_free (GstTocEntry * toc);
117 GST_DEFINE_MINI_OBJECT_TYPE (GstToc, gst_toc);
118 GST_DEFINE_MINI_OBJECT_TYPE (GstTocEntry, gst_toc_entry);
122 * @scope: scope of this TOC
124 * Create a new #GstToc structure.
126 * Returns: (transfer full): newly allocated #GstToc structure, free it
127 * with gst_toc_unref().
130 gst_toc_new (GstTocScope scope)
134 g_return_val_if_fail (scope == GST_TOC_SCOPE_GLOBAL ||
135 scope == GST_TOC_SCOPE_CURRENT, NULL);
137 toc = g_slice_new0 (GstToc);
139 gst_mini_object_init (GST_MINI_OBJECT_CAST (toc), 0, GST_TYPE_TOC,
140 (GstMiniObjectCopyFunction) gst_toc_copy, NULL,
141 (GstMiniObjectFreeFunction) gst_toc_free);
144 toc->tags = gst_tag_list_new_empty ();
151 * @toc: a #GstToc instance
153 * Returns: scope of @toc
156 gst_toc_get_scope (const GstToc * toc)
158 g_return_val_if_fail (toc != NULL, GST_TOC_SCOPE_GLOBAL);
165 * @toc: A #GstToc instance
166 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
168 * Set a #GstTagList with tags for the complete @toc.
171 gst_toc_set_tags (GstToc * toc, GstTagList * tags)
173 g_return_if_fail (toc != NULL);
174 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
177 gst_tag_list_unref (toc->tags);
182 * gst_toc_merge_tags:
183 * @toc: A #GstToc instance
184 * @tags: (allow-none): A #GstTagList or %NULL
185 * @mode: A #GstTagMergeMode
187 * Merge @tags into the existing tags of @toc using @mode.
190 gst_toc_merge_tags (GstToc * toc, GstTagList * tags, GstTagMergeMode mode)
192 g_return_if_fail (toc != NULL);
193 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
196 toc->tags = gst_tag_list_ref (tags);
198 GstTagList *tmp = gst_tag_list_merge (toc->tags, tags, mode);
199 gst_tag_list_unref (toc->tags);
206 * @toc: A #GstToc instance
208 * Gets the tags for @toc.
210 * Returns: (transfer none): A #GstTagList for @entry
213 gst_toc_get_tags (const GstToc * toc)
215 g_return_val_if_fail (toc != NULL, NULL);
221 * gst_toc_append_entry:
222 * @toc: A #GstToc instance
223 * @entry: (transfer full): A #GstTocEntry
225 * Appends the #GstTocEntry @entry to @toc.
228 gst_toc_append_entry (GstToc * toc, GstTocEntry * entry)
230 g_return_if_fail (toc != NULL);
231 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
232 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
233 g_return_if_fail (entry->toc == NULL);
234 g_return_if_fail (entry->parent == NULL);
236 toc->entries = g_list_append (toc->entries, entry);
239 GST_LOG ("appended %s entry with uid %s to toc %p",
240 gst_toc_entry_type_get_nick (entry->type), entry->uid, toc);
246 * gst_toc_get_entries:
247 * @toc: A #GstToc instance
249 * Gets the list of #GstTocEntry of @toc.
251 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry for @entry
254 gst_toc_get_entries (const GstToc * toc)
256 g_return_val_if_fail (toc != NULL, NULL);
262 gst_toc_entry_new_internal (GstTocEntryType type, const gchar * uid)
266 entry = g_slice_new0 (GstTocEntry);
268 gst_mini_object_init (GST_MINI_OBJECT_CAST (entry), 0, GST_TYPE_TOC_ENTRY,
269 (GstMiniObjectCopyFunction) gst_toc_entry_copy, NULL,
270 (GstMiniObjectFreeFunction) gst_toc_entry_free);
272 entry->uid = g_strdup (uid);
275 entry->start = entry->stop = GST_CLOCK_TIME_NONE;
283 * @uid: unique ID (UID) in the whole TOC.
285 * Create new #GstTocEntry structure.
287 * Returns: newly allocated #GstTocEntry structure, free it with gst_toc_entry_unref().
290 gst_toc_entry_new (GstTocEntryType type, const gchar * uid)
292 g_return_val_if_fail (uid != NULL, NULL);
294 return gst_toc_entry_new_internal (type, uid);
298 gst_toc_free (GstToc * toc)
300 g_list_foreach (toc->entries, (GFunc) gst_mini_object_unref, NULL);
301 g_list_free (toc->entries);
303 if (toc->tags != NULL)
304 gst_tag_list_unref (toc->tags);
306 g_slice_free (GstToc, toc);
310 gst_toc_entry_free (GstTocEntry * entry)
312 g_return_if_fail (entry != NULL);
314 g_list_foreach (entry->subentries, (GFunc) gst_mini_object_unref, NULL);
315 g_list_free (entry->subentries);
319 if (entry->tags != NULL)
320 gst_tag_list_unref (entry->tags);
322 g_slice_free (GstTocEntry, entry);
326 gst_toc_entry_find_sub_entry (const GstTocEntry * entry, const gchar * uid)
329 GstTocEntry *subentry, *subsubentry;
331 g_return_val_if_fail (entry != NULL, NULL);
332 g_return_val_if_fail (uid != NULL, NULL);
334 cur = entry->subentries;
335 while (cur != NULL) {
336 subentry = cur->data;
338 if (g_strcmp0 (subentry->uid, uid) == 0)
341 subsubentry = gst_toc_entry_find_sub_entry (subentry, uid);
342 if (subsubentry != NULL)
352 * gst_toc_find_entry:
353 * @toc: #GstToc to search in.
354 * @uid: UID to find #GstTocEntry with.
356 * Find #GstTocEntry with given @uid in the @toc.
358 * Returns: (transfer none): #GstTocEntry with specified @uid from the @toc, or NULL if not found.
361 gst_toc_find_entry (const GstToc * toc, const gchar * uid)
364 GstTocEntry *entry, *subentry;
366 g_return_val_if_fail (toc != NULL, NULL);
367 g_return_val_if_fail (uid != NULL, NULL);
370 while (cur != NULL) {
373 if (g_strcmp0 (entry->uid, uid) == 0)
376 subentry = gst_toc_entry_find_sub_entry (entry, uid);
377 if (subentry != NULL)
386 * gst_toc_entry_copy:
387 * @entry: #GstTocEntry to copy.
389 * Copy #GstTocEntry with all subentries (deep copy).
391 * Returns: newly allocated #GstTocEntry in case of success, NULL otherwise;
392 * free it when done with gst_toc_entry_unref().
395 gst_toc_entry_copy (const GstTocEntry * entry)
397 GstTocEntry *ret, *sub;
401 g_return_val_if_fail (entry != NULL, NULL);
403 ret = gst_toc_entry_new (entry->type, entry->uid);
405 ret->start = entry->start;
406 ret->stop = entry->stop;
408 if (GST_IS_TAG_LIST (entry->tags)) {
409 list = gst_tag_list_copy (entry->tags);
411 gst_tag_list_unref (ret->tags);
415 cur = entry->subentries;
416 while (cur != NULL) {
417 sub = gst_toc_entry_copy (cur->data);
420 ret->subentries = g_list_prepend (ret->subentries, sub);
424 ret->subentries = g_list_reverse (ret->subentries);
431 * @toc: #GstToc to copy.
433 * Copy #GstToc with all subentries (deep copy).
435 * Returns: newly allocated #GstToc in case of success, NULL otherwise;
436 * free it when done with gst_toc_unref().
439 gst_toc_copy (const GstToc * toc)
446 g_return_val_if_fail (toc != NULL, NULL);
448 ret = gst_toc_new (toc->scope);
450 if (GST_IS_TAG_LIST (toc->tags)) {
451 list = gst_tag_list_copy (toc->tags);
452 gst_tag_list_unref (ret->tags);
457 while (cur != NULL) {
458 entry = gst_toc_entry_copy (cur->data);
461 ret->entries = g_list_prepend (ret->entries, entry);
465 ret->entries = g_list_reverse (ret->entries);
470 * gst_toc_entry_set_start_stop_times:
471 * @entry: #GstTocEntry to set values.
472 * @start: start value to set.
473 * @stop: stop value to set.
475 * Set @start and @stop values for the @entry.
478 gst_toc_entry_set_start_stop_times (GstTocEntry * entry, gint64 start,
481 g_return_if_fail (entry != NULL);
483 entry->start = start;
488 * gst_toc_entry_get_start_stop_times:
489 * @entry: #GstTocEntry to get values from.
490 * @start: (out): the storage for the start value, leave #NULL if not need.
491 * @stop: (out): the storage for the stop value, leave #NULL if not need.
493 * Get start and stop values from the @entry and write them into appropriate storages.
495 * Returns: TRUE if all non-NULL storage pointers were filled with appropriate values,
499 gst_toc_entry_get_start_stop_times (const GstTocEntry * entry, gint64 * start,
504 g_return_val_if_fail (entry != NULL, FALSE);
507 *start = entry->start;
515 * gst_toc_entry_type_get_nick:
516 * @type: a #GstTocEntryType.
518 * Converts @type to a string representation.
520 * Returns: Returns a human-readable string for @type. This string is
521 * only for debugging purpose and should not be displayed in a user
525 gst_toc_entry_type_get_nick (GstTocEntryType type)
528 case GST_TOC_ENTRY_TYPE_ANGLE:
530 case GST_TOC_ENTRY_TYPE_VERSION:
532 case GST_TOC_ENTRY_TYPE_EDITION:
534 case GST_TOC_ENTRY_TYPE_TITLE:
536 case GST_TOC_ENTRY_TYPE_TRACK:
538 case GST_TOC_ENTRY_TYPE_CHAPTER:
547 * gst_toc_entry_get_entry_type:
548 * @entry: a #GstTocEntry
550 * Returns: @entry's entry type
553 gst_toc_entry_get_entry_type (const GstTocEntry * entry)
555 g_return_val_if_fail (entry != NULL, GST_TOC_ENTRY_TYPE_INVALID);
561 * gst_toc_entry_is_alternative:
562 * @entry: a #GstTocEntry
564 * Returns: %TRUE if @entry's type is an alternative type, otherwise %FALSE
567 gst_toc_entry_is_alternative (const GstTocEntry * entry)
569 g_return_val_if_fail (entry != NULL, FALSE);
571 return GST_TOC_ENTRY_TYPE_IS_ALTERNATIVE (entry->type);
575 * gst_toc_entry_is_sequence:
576 * @entry: a #GstTocEntry
578 * Returns: %TRUE if @entry's type is a sequence type, otherwise %FALSE
581 gst_toc_entry_is_sequence (const GstTocEntry * entry)
583 g_return_val_if_fail (entry != NULL, FALSE);
585 return GST_TOC_ENTRY_TYPE_IS_SEQUENCE (entry->type);
589 * gst_toc_entry_get_uid:
590 * @entry: A #GstTocEntry instance
592 * Gets the UID of @entry.
594 * Returns: (transfer none): The UID of @entry
597 gst_toc_entry_get_uid (const GstTocEntry * entry)
599 g_return_val_if_fail (entry != NULL, NULL);
605 * gst_toc_entry_append_sub_entry:
606 * @entry: A #GstTocEntry instance
607 * @subentry: (transfer full): A #GstTocEntry
609 * Appends the #GstTocEntry @subentry to @entry.
612 gst_toc_entry_append_sub_entry (GstTocEntry * entry, GstTocEntry * subentry)
614 g_return_if_fail (entry != NULL);
615 g_return_if_fail (subentry != NULL);
616 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
617 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST
619 g_return_if_fail (subentry->toc == NULL);
620 g_return_if_fail (subentry->parent == NULL);
622 entry->subentries = g_list_append (entry->subentries, subentry);
623 subentry->toc = entry->toc;
624 subentry->parent = entry;
626 GST_LOG ("appended %s subentry with uid %s to entry %s",
627 gst_toc_entry_type_get_nick (subentry->type), subentry->uid, entry->uid);
631 * gst_toc_entry_get_sub_entries:
632 * @entry: A #GstTocEntry instance
634 * Gets the sub-entries of @entry.
636 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry of @entry
639 gst_toc_entry_get_sub_entries (const GstTocEntry * entry)
641 g_return_val_if_fail (entry != NULL, NULL);
643 return entry->subentries;
647 * gst_toc_entry_set_tags:
648 * @entry: A #GstTocEntry instance
649 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
651 * Set a #GstTagList with tags for the complete @entry.
654 gst_toc_entry_set_tags (GstTocEntry * entry, GstTagList * tags)
656 g_return_if_fail (entry != NULL);
657 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
660 gst_tag_list_unref (entry->tags);
665 * gst_toc_entry_merge_tags:
666 * @entry: A #GstTocEntry instance
667 * @tags: (allow-none): A #GstTagList or %NULL
668 * @mode: A #GstTagMergeMode
670 * Merge @tags into the existing tags of @entry using @mode.
673 gst_toc_entry_merge_tags (GstTocEntry * entry, GstTagList * tags,
674 GstTagMergeMode mode)
676 g_return_if_fail (entry != NULL);
677 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
680 entry->tags = gst_tag_list_ref (tags);
682 GstTagList *tmp = gst_tag_list_merge (entry->tags, tags, mode);
683 gst_tag_list_unref (entry->tags);
689 * gst_toc_entry_get_tags:
690 * @entry: A #GstTocEntry instance
692 * Gets the tags for @entry.
694 * Returns: (transfer none): A #GstTagList for @entry
697 gst_toc_entry_get_tags (const GstTocEntry * entry)
699 g_return_val_if_fail (entry != NULL, NULL);
705 * gst_toc_entry_get_toc:
706 * @entry: A #GstTocEntry instance
708 * Gets the parent #GstToc of @entry.
710 * Returns: (transfer none): The parent #GstToc of @entry
713 gst_toc_entry_get_toc (GstTocEntry * entry)
715 g_return_val_if_fail (entry != NULL, NULL);
721 * gst_toc_entry_get_parent:
722 * @entry: A #GstTocEntry instance
724 * Gets the parent #GstTocEntry of @entry.
726 * Returns: (transfer none): The parent #GstTocEntry of @entry
729 gst_toc_entry_get_parent (GstTocEntry * entry)
731 g_return_val_if_fail (entry != NULL, NULL);
733 return entry->parent;
736 #ifndef GST_DISABLE_GST_DEBUG
738 gst_toc_dump_entries (GList * entries, guint depth)
743 indent = g_malloc0 (depth + 1);
744 memset (indent, ' ', depth);
745 for (e = entries; e != NULL; e = e->next) {
746 GstTocEntry *entry = e->data;
748 GST_TRACE ("%s+ %s (%s), %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT ", "
749 "tags: %" GST_PTR_FORMAT, indent, entry->uid,
750 gst_toc_entry_type_get_nick (entry->type),
751 GST_TIME_ARGS (entry->start), GST_TIME_ARGS (entry->stop), entry->tags);
753 if (entry->subentries != NULL)
754 gst_toc_dump_entries (entry->subentries, depth + 2);
761 gst_toc_dump (GstToc * toc)
763 #ifndef GST_DISABLE_GST_DEBUG
764 GST_TRACE (" Toc %p, scope: %s, tags: %" GST_PTR_FORMAT, toc,
765 (toc->scope == GST_TOC_SCOPE_GLOBAL) ? "global" : "current", toc->tags);
766 gst_toc_dump_entries (toc->entries, 2);