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;
99 GstTocLoopType loop_type;
105 GstMiniObject mini_object;
113 static GstToc *gst_toc_copy (const GstToc * toc);
114 static void gst_toc_free (GstToc * toc);
115 #undef gst_toc_entry_copy
116 static GstTocEntry *gst_toc_entry_copy (const GstTocEntry * toc);
117 static void gst_toc_entry_free (GstTocEntry * toc);
119 GST_DEFINE_MINI_OBJECT_TYPE (GstToc, gst_toc);
120 GST_DEFINE_MINI_OBJECT_TYPE (GstTocEntry, gst_toc_entry);
124 * @scope: scope of this TOC
126 * Create a new #GstToc structure.
128 * Returns: (transfer full): newly allocated #GstToc structure, free it
129 * with gst_toc_unref().
132 gst_toc_new (GstTocScope scope)
136 g_return_val_if_fail (scope == GST_TOC_SCOPE_GLOBAL ||
137 scope == GST_TOC_SCOPE_CURRENT, NULL);
139 toc = g_slice_new0 (GstToc);
141 gst_mini_object_init (GST_MINI_OBJECT_CAST (toc), 0, GST_TYPE_TOC,
142 (GstMiniObjectCopyFunction) gst_toc_copy, NULL,
143 (GstMiniObjectFreeFunction) gst_toc_free);
146 toc->tags = gst_tag_list_new_empty ();
153 * @toc: a #GstToc instance
155 * Returns: scope of @toc
158 gst_toc_get_scope (const GstToc * toc)
160 g_return_val_if_fail (toc != NULL, GST_TOC_SCOPE_GLOBAL);
167 * @toc: A #GstToc instance
168 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
170 * Set a #GstTagList with tags for the complete @toc.
173 gst_toc_set_tags (GstToc * toc, GstTagList * tags)
175 g_return_if_fail (toc != NULL);
176 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
179 gst_tag_list_unref (toc->tags);
184 * gst_toc_merge_tags:
185 * @toc: A #GstToc instance
186 * @tags: (allow-none): A #GstTagList or %NULL
187 * @mode: A #GstTagMergeMode
189 * Merge @tags into the existing tags of @toc using @mode.
192 gst_toc_merge_tags (GstToc * toc, GstTagList * tags, GstTagMergeMode mode)
194 g_return_if_fail (toc != NULL);
195 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
198 toc->tags = gst_tag_list_ref (tags);
200 GstTagList *tmp = gst_tag_list_merge (toc->tags, tags, mode);
201 gst_tag_list_unref (toc->tags);
208 * @toc: A #GstToc instance
210 * Gets the tags for @toc.
212 * Returns: (transfer none): A #GstTagList for @entry
215 gst_toc_get_tags (const GstToc * toc)
217 g_return_val_if_fail (toc != NULL, NULL);
223 * gst_toc_append_entry:
224 * @toc: A #GstToc instance
225 * @entry: (transfer full): A #GstTocEntry
227 * Appends the #GstTocEntry @entry to @toc.
230 gst_toc_append_entry (GstToc * toc, GstTocEntry * entry)
232 g_return_if_fail (toc != NULL);
233 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
234 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
235 g_return_if_fail (entry->toc == NULL);
236 g_return_if_fail (entry->parent == NULL);
238 toc->entries = g_list_append (toc->entries, entry);
241 GST_LOG ("appended %s entry with uid %s to toc %p",
242 gst_toc_entry_type_get_nick (entry->type), entry->uid, toc);
248 * gst_toc_get_entries:
249 * @toc: A #GstToc instance
251 * Gets the list of #GstTocEntry of @toc.
253 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry for @entry
256 gst_toc_get_entries (const GstToc * toc)
258 g_return_val_if_fail (toc != NULL, NULL);
264 gst_toc_entry_new_internal (GstTocEntryType type, const gchar * uid)
268 entry = g_slice_new0 (GstTocEntry);
270 gst_mini_object_init (GST_MINI_OBJECT_CAST (entry), 0, GST_TYPE_TOC_ENTRY,
271 (GstMiniObjectCopyFunction) gst_toc_entry_copy, NULL,
272 (GstMiniObjectFreeFunction) gst_toc_entry_free);
274 entry->uid = g_strdup (uid);
277 entry->start = entry->stop = GST_CLOCK_TIME_NONE;
285 * @uid: unique ID (UID) in the whole TOC.
287 * Create new #GstTocEntry structure.
289 * Returns: newly allocated #GstTocEntry structure, free it with gst_toc_entry_unref().
292 gst_toc_entry_new (GstTocEntryType type, const gchar * uid)
294 g_return_val_if_fail (uid != NULL, NULL);
296 return gst_toc_entry_new_internal (type, uid);
300 gst_toc_free (GstToc * toc)
302 g_list_foreach (toc->entries, (GFunc) gst_mini_object_unref, NULL);
303 g_list_free (toc->entries);
305 if (toc->tags != NULL)
306 gst_tag_list_unref (toc->tags);
308 g_slice_free (GstToc, toc);
312 gst_toc_entry_free (GstTocEntry * entry)
314 g_return_if_fail (entry != NULL);
316 g_list_foreach (entry->subentries, (GFunc) gst_mini_object_unref, NULL);
317 g_list_free (entry->subentries);
321 if (entry->tags != NULL)
322 gst_tag_list_unref (entry->tags);
324 g_slice_free (GstTocEntry, entry);
328 gst_toc_entry_find_sub_entry (const GstTocEntry * entry, const gchar * uid)
331 GstTocEntry *subentry, *subsubentry;
333 g_return_val_if_fail (entry != NULL, NULL);
334 g_return_val_if_fail (uid != NULL, NULL);
336 cur = entry->subentries;
337 while (cur != NULL) {
338 subentry = cur->data;
340 if (g_strcmp0 (subentry->uid, uid) == 0)
343 subsubentry = gst_toc_entry_find_sub_entry (subentry, uid);
344 if (subsubentry != NULL)
354 * gst_toc_find_entry:
355 * @toc: #GstToc to search in.
356 * @uid: UID to find #GstTocEntry with.
358 * Find #GstTocEntry with given @uid in the @toc.
360 * Returns: (transfer none): #GstTocEntry with specified @uid from the @toc, or %NULL if not found.
363 gst_toc_find_entry (const GstToc * toc, const gchar * uid)
366 GstTocEntry *entry, *subentry;
368 g_return_val_if_fail (toc != NULL, NULL);
369 g_return_val_if_fail (uid != NULL, NULL);
372 while (cur != NULL) {
375 if (g_strcmp0 (entry->uid, uid) == 0)
378 subentry = gst_toc_entry_find_sub_entry (entry, uid);
379 if (subentry != NULL)
388 * gst_toc_entry_copy:
389 * @entry: #GstTocEntry to copy.
391 * Copy #GstTocEntry with all subentries (deep copy).
393 * Returns: newly allocated #GstTocEntry in case of success, %NULL otherwise;
394 * free it when done with gst_toc_entry_unref().
397 gst_toc_entry_copy (const GstTocEntry * entry)
399 GstTocEntry *ret, *sub;
403 g_return_val_if_fail (entry != NULL, NULL);
405 ret = gst_toc_entry_new (entry->type, entry->uid);
407 ret->start = entry->start;
408 ret->stop = entry->stop;
410 if (GST_IS_TAG_LIST (entry->tags)) {
411 list = gst_tag_list_copy (entry->tags);
413 gst_tag_list_unref (ret->tags);
417 cur = entry->subentries;
418 while (cur != NULL) {
419 sub = gst_toc_entry_copy (cur->data);
422 ret->subentries = g_list_prepend (ret->subentries, sub);
426 ret->subentries = g_list_reverse (ret->subentries);
433 * @toc: #GstToc to copy.
435 * Copy #GstToc with all subentries (deep copy).
437 * Returns: newly allocated #GstToc in case of success, %NULL otherwise;
438 * free it when done with gst_toc_unref().
441 gst_toc_copy (const GstToc * toc)
448 g_return_val_if_fail (toc != NULL, NULL);
450 ret = gst_toc_new (toc->scope);
452 if (GST_IS_TAG_LIST (toc->tags)) {
453 list = gst_tag_list_copy (toc->tags);
454 gst_tag_list_unref (ret->tags);
459 while (cur != NULL) {
460 entry = gst_toc_entry_copy (cur->data);
463 ret->entries = g_list_prepend (ret->entries, entry);
467 ret->entries = g_list_reverse (ret->entries);
472 * gst_toc_entry_set_start_stop_times:
473 * @entry: #GstTocEntry to set values.
474 * @start: start value to set.
475 * @stop: stop value to set.
477 * Set @start and @stop values for the @entry.
480 gst_toc_entry_set_start_stop_times (GstTocEntry * entry, gint64 start,
483 g_return_if_fail (entry != NULL);
485 entry->start = start;
490 * gst_toc_entry_get_start_stop_times:
491 * @entry: #GstTocEntry to get values from.
492 * @start: (out): the storage for the start value, leave %NULL if not need.
493 * @stop: (out): the storage for the stop value, leave %NULL if not need.
495 * Get @start and @stop values from the @entry and write them into appropriate
498 * Returns: %TRUE if all non-%NULL storage pointers were filled with appropriate
499 * values, %FALSE otherwise.
502 gst_toc_entry_get_start_stop_times (const GstTocEntry * entry, gint64 * start,
505 g_return_val_if_fail (entry != NULL, FALSE);
508 *start = entry->start;
516 * gst_toc_entry_set_loop:
517 * @entry: #GstTocEntry to set values.
518 * @loop_type: loop_type value to set.
519 * @repeat_count: repeat_count value to set.
521 * Set @loop_type and @repeat_count values for the @entry.
526 gst_toc_entry_set_loop (GstTocEntry * entry, GstTocLoopType loop_type,
529 g_return_if_fail (entry != NULL);
531 entry->loop_type = loop_type;
532 entry->repeat_count = repeat_count;
536 * gst_toc_entry_get_loop:
537 * @entry: #GstTocEntry to get values from.
538 * @loop_type: (out): the storage for the loop_type value, leave %NULL if not
540 * @repeat_count: (out): the storage for the repeat_count value, leave %NULL if
543 * Get @loop_type and @repeat_count values from the @entry and write them into
544 * appropriate storages. Loops are e.g. used by sampled instruments. GStreamer
545 * is not automatically applying the loop. The application can process this
546 * meta data and use it e.g. to send a seek-event to loop a section.
548 * Returns: %TRUE if all non-%NULL storage pointers were filled with appropriate
549 * values, %FALSE otherwise.
554 gst_toc_entry_get_loop (const GstTocEntry * entry, GstTocLoopType * loop_type,
557 g_return_val_if_fail (entry != NULL, FALSE);
559 if (loop_type != NULL)
560 *loop_type = entry->loop_type;
561 if (repeat_count != NULL)
562 *repeat_count = entry->repeat_count;
569 * gst_toc_entry_type_get_nick:
570 * @type: a #GstTocEntryType.
572 * Converts @type to a string representation.
574 * Returns: Returns a human-readable string for @type. This string is
575 * only for debugging purpose and should not be displayed in a user
579 gst_toc_entry_type_get_nick (GstTocEntryType type)
582 case GST_TOC_ENTRY_TYPE_ANGLE:
584 case GST_TOC_ENTRY_TYPE_VERSION:
586 case GST_TOC_ENTRY_TYPE_EDITION:
588 case GST_TOC_ENTRY_TYPE_TITLE:
590 case GST_TOC_ENTRY_TYPE_TRACK:
592 case GST_TOC_ENTRY_TYPE_CHAPTER:
601 * gst_toc_entry_get_entry_type:
602 * @entry: a #GstTocEntry
604 * Returns: @entry's entry type
607 gst_toc_entry_get_entry_type (const GstTocEntry * entry)
609 g_return_val_if_fail (entry != NULL, GST_TOC_ENTRY_TYPE_INVALID);
615 * gst_toc_entry_is_alternative:
616 * @entry: a #GstTocEntry
618 * Returns: %TRUE if @entry's type is an alternative type, otherwise %FALSE
621 gst_toc_entry_is_alternative (const GstTocEntry * entry)
623 g_return_val_if_fail (entry != NULL, FALSE);
625 return GST_TOC_ENTRY_TYPE_IS_ALTERNATIVE (entry->type);
629 * gst_toc_entry_is_sequence:
630 * @entry: a #GstTocEntry
632 * Returns: %TRUE if @entry's type is a sequence type, otherwise %FALSE
635 gst_toc_entry_is_sequence (const GstTocEntry * entry)
637 g_return_val_if_fail (entry != NULL, FALSE);
639 return GST_TOC_ENTRY_TYPE_IS_SEQUENCE (entry->type);
643 * gst_toc_entry_get_uid:
644 * @entry: A #GstTocEntry instance
646 * Gets the UID of @entry.
648 * Returns: (transfer none): The UID of @entry
651 gst_toc_entry_get_uid (const GstTocEntry * entry)
653 g_return_val_if_fail (entry != NULL, NULL);
659 * gst_toc_entry_append_sub_entry:
660 * @entry: A #GstTocEntry instance
661 * @subentry: (transfer full): A #GstTocEntry
663 * Appends the #GstTocEntry @subentry to @entry.
666 gst_toc_entry_append_sub_entry (GstTocEntry * entry, GstTocEntry * subentry)
668 g_return_if_fail (entry != NULL);
669 g_return_if_fail (subentry != NULL);
670 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
671 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST
673 g_return_if_fail (subentry->toc == NULL);
674 g_return_if_fail (subentry->parent == NULL);
676 entry->subentries = g_list_append (entry->subentries, subentry);
677 subentry->toc = entry->toc;
678 subentry->parent = entry;
680 GST_LOG ("appended %s subentry with uid %s to entry %s",
681 gst_toc_entry_type_get_nick (subentry->type), subentry->uid, entry->uid);
685 * gst_toc_entry_get_sub_entries:
686 * @entry: A #GstTocEntry instance
688 * Gets the sub-entries of @entry.
690 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry of @entry
693 gst_toc_entry_get_sub_entries (const GstTocEntry * entry)
695 g_return_val_if_fail (entry != NULL, NULL);
697 return entry->subentries;
701 * gst_toc_entry_set_tags:
702 * @entry: A #GstTocEntry instance
703 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
705 * Set a #GstTagList with tags for the complete @entry.
708 gst_toc_entry_set_tags (GstTocEntry * entry, GstTagList * tags)
710 g_return_if_fail (entry != NULL);
711 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
714 gst_tag_list_unref (entry->tags);
719 * gst_toc_entry_merge_tags:
720 * @entry: A #GstTocEntry instance
721 * @tags: (allow-none): A #GstTagList or %NULL
722 * @mode: A #GstTagMergeMode
724 * Merge @tags into the existing tags of @entry using @mode.
727 gst_toc_entry_merge_tags (GstTocEntry * entry, GstTagList * tags,
728 GstTagMergeMode mode)
730 g_return_if_fail (entry != NULL);
731 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
734 entry->tags = gst_tag_list_ref (tags);
736 GstTagList *tmp = gst_tag_list_merge (entry->tags, tags, mode);
737 gst_tag_list_unref (entry->tags);
743 * gst_toc_entry_get_tags:
744 * @entry: A #GstTocEntry instance
746 * Gets the tags for @entry.
748 * Returns: (transfer none): A #GstTagList for @entry
751 gst_toc_entry_get_tags (const GstTocEntry * entry)
753 g_return_val_if_fail (entry != NULL, NULL);
759 * gst_toc_entry_get_toc:
760 * @entry: A #GstTocEntry instance
762 * Gets the parent #GstToc of @entry.
764 * Returns: (transfer none): The parent #GstToc of @entry
767 gst_toc_entry_get_toc (GstTocEntry * entry)
769 g_return_val_if_fail (entry != NULL, NULL);
775 * gst_toc_entry_get_parent:
776 * @entry: A #GstTocEntry instance
778 * Gets the parent #GstTocEntry of @entry.
780 * Returns: (transfer none): The parent #GstTocEntry of @entry
783 gst_toc_entry_get_parent (GstTocEntry * entry)
785 g_return_val_if_fail (entry != NULL, NULL);
787 return entry->parent;
790 #ifndef GST_DISABLE_GST_DEBUG
792 gst_toc_dump_entries (GList * entries, guint depth)
797 indent = g_malloc0 (depth + 1);
798 memset (indent, ' ', depth);
799 for (e = entries; e != NULL; e = e->next) {
800 GstTocEntry *entry = e->data;
802 GST_TRACE ("%s+ %s (%s), %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT ", "
803 "tags: %" GST_PTR_FORMAT, indent, entry->uid,
804 gst_toc_entry_type_get_nick (entry->type),
805 GST_TIME_ARGS (entry->start), GST_TIME_ARGS (entry->stop), entry->tags);
807 if (entry->subentries != NULL)
808 gst_toc_dump_entries (entry->subentries, depth + 2);
815 gst_toc_dump (GstToc * toc)
817 #ifndef GST_DISABLE_GST_DEBUG
818 GST_TRACE (" Toc %p, scope: %s, tags: %" GST_PTR_FORMAT, toc,
819 (toc->scope == GST_TOC_SCOPE_GLOBAL) ? "global" : "current", toc->tags);
820 gst_toc_dump_entries (toc->entries, 2);