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.
25 * @short_description: Generic table of contents support
26 * @see_also: #GstStructure, #GstEvent, #GstMessage, #GstQuery
28 * #GstToc functions are used to create/free #GstToc and #GstTocEntry structures.
29 * Also they are used to convert #GstToc into #GstStructure and vice versa.
31 * #GstToc lets you to inform other elements in pipeline or application that playing
32 * source has some kind of table of contents (TOC). These may be chapters, editions,
33 * angles or other types. For example: DVD chapters, Matroska chapters or cue sheet
34 * TOC. Such TOC will be useful for applications to display instead of just a
37 * Using TOC is very easy. Firstly, create #GstToc structure which represents root
38 * contents of the source. You can also attach TOC-specific tags to it. Then fill
39 * it with #GstTocEntry entries by appending them to the #GstToc using
40 * gst_toc_append_entry(), and appending subentries to a #GstTocEntry using
41 * gst_toc_entry_append_sub_entry().
43 * Note that root level of the TOC can contain only either editions or chapters. You
44 * should not mix them together at the same level. Otherwise you will get serialization
45 * /deserialization errors. Make sure that no one of the entries has negative start and
48 * Use gst_event_new_toc() to create a new TOC #GstEvent, and gst_event_parse_toc() to
49 * parse received TOC event. Use gst_event_new_toc_select() to create a new TOC select #GstEvent,
50 * and gst_event_parse_toc_select() to parse received TOC select event. The same rule for
51 * the #GstMessage: gst_message_new_toc() to create new TOC #GstMessage, and
52 * gst_message_parse_toc() to parse received TOC message.
54 * TOCs can have global scope or current scope. Global scope TOCs contain
55 * all entries that can possibly be selected using a toc select event, and
56 * are what an application is usually interested in. TOCs with current scope
57 * only contain the parts of the TOC relevant to the currently selected/playing
58 * stream; the current scope TOC is used by downstream elements such as muxers
59 * to write correct TOC entries when transcoding files, for example. When
60 * playing a DVD, the global TOC would contain a hierarchy of all titles,
61 * chapters and angles, for example, while the current TOC would only contain
62 * the chapters for the currently playing title if playback of a specific
63 * title was requested.
65 * Applications and plugins should not rely on TOCs having a certain kind of
66 * structure, but should allow for different alternatives. For example, a
67 * simple CUE sheet embedded in a file may be presented as a flat list of
68 * track entries, or could have a top-level edition node (or some other
69 * alternative type entry) with track entries underneath that node; or even
70 * multiple top-level edition nodes (or some other alternative type entries)
71 * each with track entries underneath, in case the source file has extracted
72 * a track listing from different sources).
79 #include "gst_private.h"
80 #include "gstenumtypes.h"
81 #include "gsttaglist.h"
82 #include "gststructure.h"
90 GstMiniObject mini_object;
97 GstClockTime start, stop;
100 GstTocLoopType loop_type;
106 GstMiniObject mini_object;
114 static GstToc *gst_toc_copy (const GstToc * toc);
115 static void gst_toc_free (GstToc * toc);
116 #undef gst_toc_entry_copy
117 static GstTocEntry *gst_toc_entry_copy (const GstTocEntry * toc);
118 static void gst_toc_entry_free (GstTocEntry * toc);
120 GType _gst_toc_type = 0;
121 GType _gst_toc_entry_type = 0;
123 GST_DEFINE_MINI_OBJECT_TYPE (GstToc, gst_toc);
124 GST_DEFINE_MINI_OBJECT_TYPE (GstTocEntry, gst_toc_entry);
128 * @scope: scope of this TOC
130 * Create a new #GstToc structure.
132 * Returns: (transfer full): newly allocated #GstToc structure, free it
133 * with gst_toc_unref().
136 gst_toc_new (GstTocScope scope)
140 g_return_val_if_fail (scope == GST_TOC_SCOPE_GLOBAL ||
141 scope == GST_TOC_SCOPE_CURRENT, NULL);
143 toc = g_slice_new0 (GstToc);
145 gst_mini_object_init (GST_MINI_OBJECT_CAST (toc), 0, GST_TYPE_TOC,
146 (GstMiniObjectCopyFunction) gst_toc_copy, NULL,
147 (GstMiniObjectFreeFunction) gst_toc_free);
150 toc->tags = gst_tag_list_new_empty ();
157 * @toc: a #GstToc instance
159 * Returns: scope of @toc
162 gst_toc_get_scope (const GstToc * toc)
164 g_return_val_if_fail (toc != NULL, GST_TOC_SCOPE_GLOBAL);
171 * @toc: A #GstToc instance
172 * @tags: (nullable) (transfer full): A #GstTagList or %NULL
174 * Set a #GstTagList with tags for the complete @toc.
177 gst_toc_set_tags (GstToc * toc, GstTagList * tags)
179 g_return_if_fail (toc != NULL);
180 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
183 gst_tag_list_unref (toc->tags);
188 * gst_toc_merge_tags:
189 * @toc: A #GstToc instance
190 * @tags: (nullable): A #GstTagList or %NULL
191 * @mode: A #GstTagMergeMode
193 * Merge @tags into the existing tags of @toc using @mode.
196 gst_toc_merge_tags (GstToc * toc, GstTagList * tags, GstTagMergeMode mode)
198 g_return_if_fail (toc != NULL);
199 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
202 toc->tags = gst_tag_list_ref (tags);
204 GstTagList *tmp = gst_tag_list_merge (toc->tags, tags, mode);
205 gst_tag_list_unref (toc->tags);
212 * @toc: A #GstToc instance
214 * Gets the tags for @toc.
216 * Returns: (transfer none) (nullable): A #GstTagList for @entry
219 gst_toc_get_tags (const GstToc * toc)
221 g_return_val_if_fail (toc != NULL, NULL);
227 * gst_toc_append_entry:
228 * @toc: A #GstToc instance
229 * @entry: (transfer full): A #GstTocEntry
231 * Appends the #GstTocEntry @entry to @toc.
234 gst_toc_append_entry (GstToc * toc, GstTocEntry * entry)
236 g_return_if_fail (toc != NULL);
237 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
238 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
239 g_return_if_fail (entry->toc == NULL);
240 g_return_if_fail (entry->parent == NULL);
242 toc->entries = g_list_append (toc->entries, entry);
245 GST_LOG ("appended %s entry with uid %s to toc %p",
246 gst_toc_entry_type_get_nick (entry->type), entry->uid, toc);
252 * gst_toc_get_entries:
253 * @toc: A #GstToc instance
255 * Gets the list of #GstTocEntry of @toc.
257 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry for @entry
260 gst_toc_get_entries (const GstToc * toc)
262 g_return_val_if_fail (toc != NULL, NULL);
268 gst_toc_entry_new_internal (GstTocEntryType type, const gchar * uid)
272 entry = g_slice_new0 (GstTocEntry);
274 gst_mini_object_init (GST_MINI_OBJECT_CAST (entry), 0, GST_TYPE_TOC_ENTRY,
275 (GstMiniObjectCopyFunction) gst_toc_entry_copy, NULL,
276 (GstMiniObjectFreeFunction) gst_toc_entry_free);
278 entry->uid = g_strdup (uid);
281 entry->start = entry->stop = GST_CLOCK_TIME_NONE;
289 * @uid: unique ID (UID) in the whole TOC.
291 * Create new #GstTocEntry structure.
293 * Returns: (transfer full): newly allocated #GstTocEntry structure, free it with gst_toc_entry_unref().
296 gst_toc_entry_new (GstTocEntryType type, const gchar * uid)
298 g_return_val_if_fail (uid != NULL, NULL);
300 return gst_toc_entry_new_internal (type, uid);
304 gst_toc_free (GstToc * toc)
306 g_list_foreach (toc->entries, (GFunc) gst_mini_object_unref, NULL);
307 g_list_free (toc->entries);
309 if (toc->tags != NULL)
310 gst_tag_list_unref (toc->tags);
313 memset (toc, 0xff, sizeof (GstToc));
316 g_slice_free (GstToc, toc);
320 gst_toc_entry_free (GstTocEntry * entry)
322 g_return_if_fail (entry != NULL);
324 g_list_foreach (entry->subentries, (GFunc) gst_mini_object_unref, NULL);
325 g_list_free (entry->subentries);
329 if (entry->tags != NULL)
330 gst_tag_list_unref (entry->tags);
333 memset (entry, 0xff, sizeof (GstTocEntry));
336 g_slice_free (GstTocEntry, entry);
340 gst_toc_entry_find_sub_entry (const GstTocEntry * entry, const gchar * uid)
343 GstTocEntry *subentry, *subsubentry;
345 g_return_val_if_fail (entry != NULL, NULL);
346 g_return_val_if_fail (uid != NULL, NULL);
348 cur = entry->subentries;
349 while (cur != NULL) {
350 subentry = cur->data;
352 if (g_strcmp0 (subentry->uid, uid) == 0)
355 subsubentry = gst_toc_entry_find_sub_entry (subentry, uid);
356 if (subsubentry != NULL)
366 * gst_toc_find_entry:
367 * @toc: #GstToc to search in.
368 * @uid: UID to find #GstTocEntry with.
370 * Find #GstTocEntry with given @uid in the @toc.
372 * Returns: (transfer none) (nullable): #GstTocEntry with specified
373 * @uid from the @toc, or %NULL if not found.
376 gst_toc_find_entry (const GstToc * toc, const gchar * uid)
379 GstTocEntry *entry, *subentry;
381 g_return_val_if_fail (toc != NULL, NULL);
382 g_return_val_if_fail (uid != NULL, NULL);
385 while (cur != NULL) {
388 if (g_strcmp0 (entry->uid, uid) == 0)
391 subentry = gst_toc_entry_find_sub_entry (entry, uid);
392 if (subentry != NULL)
401 gst_toc_deep_copy_toc_entries (GList * entry_list)
403 GQueue new_entries = G_QUEUE_INIT;
406 for (l = entry_list; l != NULL; l = l->next)
407 g_queue_push_tail (&new_entries, gst_toc_entry_copy (l->data));
409 return new_entries.head;
413 * gst_toc_entry_copy:
414 * @entry: #GstTocEntry to copy.
416 * Copy #GstTocEntry with all subentries (deep copy).
418 * Returns: (transfer full): newly allocated #GstTocEntry; free it when done with
419 * gst_toc_entry_unref().
422 gst_toc_entry_copy (const GstTocEntry * entry)
427 g_return_val_if_fail (entry != NULL, NULL);
429 ret = gst_toc_entry_new (entry->type, entry->uid);
431 ret->start = entry->start;
432 ret->stop = entry->stop;
434 if (GST_IS_TAG_LIST (entry->tags)) {
435 list = gst_tag_list_copy (entry->tags);
437 gst_tag_list_unref (ret->tags);
441 ret->subentries = gst_toc_deep_copy_toc_entries (entry->subentries);
448 * @toc: #GstToc to copy.
450 * Copy #GstToc with all subentries (deep copy).
452 * Returns: (transfer full): newly allocated #GstToc; free it when done with gst_toc_unref().
455 gst_toc_copy (const GstToc * toc)
460 g_return_val_if_fail (toc != NULL, NULL);
462 ret = gst_toc_new (toc->scope);
464 if (GST_IS_TAG_LIST (toc->tags)) {
465 list = gst_tag_list_copy (toc->tags);
466 gst_tag_list_unref (ret->tags);
470 ret->entries = gst_toc_deep_copy_toc_entries (toc->entries);
476 * gst_toc_entry_set_start_stop_times:
477 * @entry: #GstTocEntry to set values.
478 * @start: start value to set.
479 * @stop: stop value to set.
481 * Set @start and @stop values for the @entry.
484 gst_toc_entry_set_start_stop_times (GstTocEntry * entry, gint64 start,
487 g_return_if_fail (entry != NULL);
489 entry->start = start;
494 * gst_toc_entry_get_start_stop_times:
495 * @entry: #GstTocEntry to get values from.
496 * @start: (out) (optional): the storage for the start value, leave
498 * @stop: (out) (optional): the storage for the stop value, leave
501 * Get @start and @stop values from the @entry and write them into appropriate
504 * Returns: %TRUE if all non-%NULL storage pointers were filled with appropriate
505 * values, %FALSE otherwise.
508 gst_toc_entry_get_start_stop_times (const GstTocEntry * entry, gint64 * start,
511 g_return_val_if_fail (entry != NULL, FALSE);
514 *start = entry->start;
522 * gst_toc_entry_set_loop:
523 * @entry: #GstTocEntry to set values.
524 * @loop_type: loop_type value to set.
525 * @repeat_count: repeat_count value to set.
527 * Set @loop_type and @repeat_count values for the @entry.
532 gst_toc_entry_set_loop (GstTocEntry * entry, GstTocLoopType loop_type,
535 g_return_if_fail (entry != NULL);
537 entry->loop_type = loop_type;
538 entry->repeat_count = repeat_count;
542 * gst_toc_entry_get_loop:
543 * @entry: #GstTocEntry to get values from.
544 * @loop_type: (out) (optional): the storage for the loop_type
545 * value, leave %NULL if not need.
546 * @repeat_count: (out) (optional): the storage for the repeat_count
547 * value, leave %NULL if not need.
549 * Get @loop_type and @repeat_count values from the @entry and write them into
550 * appropriate storages. Loops are e.g. used by sampled instruments. GStreamer
551 * is not automatically applying the loop. The application can process this
552 * meta data and use it e.g. to send a seek-event to loop a section.
554 * Returns: %TRUE if all non-%NULL storage pointers were filled with appropriate
555 * values, %FALSE otherwise.
560 gst_toc_entry_get_loop (const GstTocEntry * entry, GstTocLoopType * loop_type,
563 g_return_val_if_fail (entry != NULL, FALSE);
565 if (loop_type != NULL)
566 *loop_type = entry->loop_type;
567 if (repeat_count != NULL)
568 *repeat_count = entry->repeat_count;
575 * gst_toc_entry_type_get_nick:
576 * @type: a #GstTocEntryType.
578 * Converts @type to a string representation.
580 * Returns: Returns a human-readable string for @type. This string is
581 * only for debugging purpose and should not be displayed in a user
585 gst_toc_entry_type_get_nick (GstTocEntryType type)
588 case GST_TOC_ENTRY_TYPE_ANGLE:
590 case GST_TOC_ENTRY_TYPE_VERSION:
592 case GST_TOC_ENTRY_TYPE_EDITION:
594 case GST_TOC_ENTRY_TYPE_TITLE:
596 case GST_TOC_ENTRY_TYPE_TRACK:
598 case GST_TOC_ENTRY_TYPE_CHAPTER:
607 * gst_toc_entry_get_entry_type:
608 * @entry: a #GstTocEntry
610 * Returns: @entry's entry type
613 gst_toc_entry_get_entry_type (const GstTocEntry * entry)
615 g_return_val_if_fail (entry != NULL, GST_TOC_ENTRY_TYPE_INVALID);
621 * gst_toc_entry_is_alternative:
622 * @entry: a #GstTocEntry
624 * Returns: %TRUE if @entry's type is an alternative type, otherwise %FALSE
627 gst_toc_entry_is_alternative (const GstTocEntry * entry)
629 g_return_val_if_fail (entry != NULL, FALSE);
631 return GST_TOC_ENTRY_TYPE_IS_ALTERNATIVE (entry->type);
635 * gst_toc_entry_is_sequence:
636 * @entry: a #GstTocEntry
638 * Returns: %TRUE if @entry's type is a sequence type, otherwise %FALSE
641 gst_toc_entry_is_sequence (const GstTocEntry * entry)
643 g_return_val_if_fail (entry != NULL, FALSE);
645 return GST_TOC_ENTRY_TYPE_IS_SEQUENCE (entry->type);
649 * gst_toc_entry_get_uid:
650 * @entry: A #GstTocEntry instance
652 * Gets the UID of @entry.
654 * Returns: (transfer none): The UID of @entry
657 gst_toc_entry_get_uid (const GstTocEntry * entry)
659 g_return_val_if_fail (entry != NULL, NULL);
665 * gst_toc_entry_append_sub_entry:
666 * @entry: A #GstTocEntry instance
667 * @subentry: (transfer full): A #GstTocEntry
669 * Appends the #GstTocEntry @subentry to @entry.
672 gst_toc_entry_append_sub_entry (GstTocEntry * entry, GstTocEntry * subentry)
674 g_return_if_fail (entry != NULL);
675 g_return_if_fail (subentry != NULL);
676 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
677 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST
679 g_return_if_fail (subentry->toc == NULL);
680 g_return_if_fail (subentry->parent == NULL);
682 entry->subentries = g_list_append (entry->subentries, subentry);
683 subentry->toc = entry->toc;
684 subentry->parent = entry;
686 GST_LOG ("appended %s subentry with uid %s to entry %s",
687 gst_toc_entry_type_get_nick (subentry->type), subentry->uid, entry->uid);
691 * gst_toc_entry_get_sub_entries:
692 * @entry: A #GstTocEntry instance
694 * Gets the sub-entries of @entry.
696 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry of @entry
699 gst_toc_entry_get_sub_entries (const GstTocEntry * entry)
701 g_return_val_if_fail (entry != NULL, NULL);
703 return entry->subentries;
707 * gst_toc_entry_set_tags:
708 * @entry: A #GstTocEntry instance
709 * @tags: (nullable) (transfer full): A #GstTagList or %NULL
711 * Set a #GstTagList with tags for the complete @entry.
714 gst_toc_entry_set_tags (GstTocEntry * entry, GstTagList * tags)
716 g_return_if_fail (entry != NULL);
717 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
720 gst_tag_list_unref (entry->tags);
725 * gst_toc_entry_merge_tags:
726 * @entry: A #GstTocEntry instance
727 * @tags: (nullable): A #GstTagList or %NULL
728 * @mode: A #GstTagMergeMode
730 * Merge @tags into the existing tags of @entry using @mode.
733 gst_toc_entry_merge_tags (GstTocEntry * entry, GstTagList * tags,
734 GstTagMergeMode mode)
736 g_return_if_fail (entry != NULL);
737 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
740 entry->tags = gst_tag_list_ref (tags);
742 GstTagList *tmp = gst_tag_list_merge (entry->tags, tags, mode);
743 gst_tag_list_unref (entry->tags);
749 * gst_toc_entry_get_tags:
750 * @entry: A #GstTocEntry instance
752 * Gets the tags for @entry.
754 * Returns: (nullable) (transfer none): A #GstTagList for @entry
757 gst_toc_entry_get_tags (const GstTocEntry * entry)
759 g_return_val_if_fail (entry != NULL, NULL);
765 * gst_toc_entry_get_toc:
766 * @entry: A #GstTocEntry instance
768 * Gets the parent #GstToc of @entry.
770 * Returns: (transfer none) (nullable): The parent #GstToc of @entry
773 gst_toc_entry_get_toc (GstTocEntry * entry)
775 g_return_val_if_fail (entry != NULL, NULL);
781 * gst_toc_entry_get_parent:
782 * @entry: A #GstTocEntry instance
784 * Gets the parent #GstTocEntry of @entry.
786 * Returns: (transfer none) (nullable): The parent #GstTocEntry of @entry
789 gst_toc_entry_get_parent (GstTocEntry * entry)
791 g_return_val_if_fail (entry != NULL, NULL);
793 return entry->parent;
796 #ifndef GST_DISABLE_GST_DEBUG
798 gst_toc_dump_entries (GList * entries, guint depth)
803 indent = g_malloc0 (depth + 1);
804 memset (indent, ' ', depth);
805 for (e = entries; e != NULL; e = e->next) {
806 GstTocEntry *entry = e->data;
808 GST_TRACE ("%s+ %s (%s), %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT ", "
809 "tags: %" GST_PTR_FORMAT, indent, entry->uid,
810 gst_toc_entry_type_get_nick (entry->type),
811 GST_TIME_ARGS (entry->start), GST_TIME_ARGS (entry->stop), entry->tags);
813 if (entry->subentries != NULL)
814 gst_toc_dump_entries (entry->subentries, depth + 2);
821 gst_toc_dump (GstToc * toc)
823 #ifndef GST_DISABLE_GST_DEBUG
824 GST_TRACE (" Toc %p, scope: %s, tags: %" GST_PTR_FORMAT, toc,
825 (toc->scope == GST_TOC_SCOPE_GLOBAL) ? "global" : "current", toc->tags);
826 gst_toc_dump_entries (toc->entries, 2);
831 _priv_gst_toc_initialize (void)
833 _gst_toc_type = gst_toc_get_type ();
834 _gst_toc_entry_type = gst_toc_entry_get_type ();