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.
69 #include "gst_private.h"
70 #include "gstenumtypes.h"
71 #include "gsttaglist.h"
72 #include "gststructure.h"
80 GstMiniObject mini_object;
87 GstClockTime start, stop;
94 GstMiniObject mini_object;
102 static GstToc *gst_toc_copy (const GstToc * toc);
103 static void gst_toc_free (GstToc * toc);
104 #undef gst_toc_entry_copy
105 static GstTocEntry *gst_toc_entry_copy (const GstTocEntry * toc);
106 static void gst_toc_entry_free (GstTocEntry * toc);
108 GST_DEFINE_MINI_OBJECT_TYPE (GstToc, gst_toc);
109 GST_DEFINE_MINI_OBJECT_TYPE (GstTocEntry, gst_toc_entry);
113 * @scope: scope of this TOC
115 * Create a new #GstToc structure.
117 * Returns: (transfer full): newly allocated #GstToc structure, free it
118 * with gst_toc_unref().
121 gst_toc_new (GstTocScope scope)
125 g_return_val_if_fail (scope == GST_TOC_SCOPE_GLOBAL ||
126 scope == GST_TOC_SCOPE_CURRENT, NULL);
128 toc = g_slice_new0 (GstToc);
130 gst_mini_object_init (GST_MINI_OBJECT_CAST (toc), 0, GST_TYPE_TOC,
131 (GstMiniObjectCopyFunction) gst_toc_copy, NULL,
132 (GstMiniObjectFreeFunction) gst_toc_free);
135 toc->tags = gst_tag_list_new_empty ();
142 * @toc: a #GstToc instance
144 * Returns: scope of @toc
147 gst_toc_get_scope (const GstToc * toc)
149 g_return_val_if_fail (toc != NULL, GST_TOC_SCOPE_GLOBAL);
156 * @toc: A #GstToc instance
157 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
159 * Set a #GstTagList with tags for the complete @toc.
162 gst_toc_set_tags (GstToc * toc, GstTagList * tags)
164 g_return_if_fail (toc != NULL);
165 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
168 gst_tag_list_unref (toc->tags);
173 * gst_toc_merge_tags:
174 * @toc: A #GstToc instance
175 * @tags: (allow-none): A #GstTagList or %NULL
176 * @mode: A #GstTagMergeMode
178 * Merge @tags into the existing tags of @toc using @mode.
181 gst_toc_merge_tags (GstToc * toc, GstTagList * tags, GstTagMergeMode mode)
183 g_return_if_fail (toc != NULL);
184 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
187 toc->tags = gst_tag_list_ref (tags);
189 GstTagList *tmp = gst_tag_list_merge (toc->tags, tags, mode);
190 gst_tag_list_unref (toc->tags);
197 * @toc: A #GstToc instance
199 * Gets the tags for @toc.
201 * Returns: (transfer none): A #GstTagList for @entry
204 gst_toc_get_tags (const GstToc * toc)
206 g_return_val_if_fail (toc != NULL, NULL);
212 * gst_toc_append_entry:
213 * @toc: A #GstToc instance
214 * @entry: (transfer full): A #GstTocEntry
216 * Appends the #GstTocEntry @entry to @toc.
219 gst_toc_append_entry (GstToc * toc, GstTocEntry * entry)
221 g_return_if_fail (toc != NULL);
222 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
223 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
224 g_return_if_fail (entry->toc == NULL);
225 g_return_if_fail (entry->parent == NULL);
227 toc->entries = g_list_append (toc->entries, entry);
230 GST_LOG ("appended %s entry with uid %s to toc %p",
231 gst_toc_entry_type_get_nick (entry->type), entry->uid, toc);
237 * gst_toc_get_entries:
238 * @toc: A #GstToc instance
240 * Gets the list of #GstTocEntry of @toc.
242 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry for @entry
245 gst_toc_get_entries (const GstToc * toc)
247 g_return_val_if_fail (toc != NULL, NULL);
253 gst_toc_entry_new_internal (GstTocEntryType type, const gchar * uid)
257 entry = g_slice_new0 (GstTocEntry);
259 gst_mini_object_init (GST_MINI_OBJECT_CAST (entry), 0, GST_TYPE_TOC_ENTRY,
260 (GstMiniObjectCopyFunction) gst_toc_entry_copy, NULL,
261 (GstMiniObjectFreeFunction) gst_toc_entry_free);
263 entry->uid = g_strdup (uid);
266 entry->start = entry->stop = GST_CLOCK_TIME_NONE;
274 * @uid: unique ID (UID) in the whole TOC.
276 * Create new #GstTocEntry structure.
278 * Returns: newly allocated #GstTocEntry structure, free it with gst_toc_entry_unref().
281 gst_toc_entry_new (GstTocEntryType type, const gchar * uid)
283 g_return_val_if_fail (uid != NULL, NULL);
285 return gst_toc_entry_new_internal (type, uid);
289 gst_toc_free (GstToc * toc)
291 g_list_foreach (toc->entries, (GFunc) gst_mini_object_unref, NULL);
292 g_list_free (toc->entries);
294 if (toc->tags != NULL)
295 gst_tag_list_unref (toc->tags);
297 g_slice_free (GstToc, toc);
301 gst_toc_entry_free (GstTocEntry * entry)
303 g_return_if_fail (entry != NULL);
305 g_list_foreach (entry->subentries, (GFunc) gst_mini_object_unref, NULL);
306 g_list_free (entry->subentries);
310 if (entry->tags != NULL)
311 gst_tag_list_unref (entry->tags);
313 g_slice_free (GstTocEntry, entry);
317 gst_toc_entry_find_sub_entry (const GstTocEntry * entry, const gchar * uid)
320 GstTocEntry *subentry, *subsubentry;
322 g_return_val_if_fail (entry != NULL, NULL);
323 g_return_val_if_fail (uid != NULL, NULL);
325 cur = entry->subentries;
326 while (cur != NULL) {
327 subentry = cur->data;
329 if (g_strcmp0 (subentry->uid, uid) == 0)
332 subsubentry = gst_toc_entry_find_sub_entry (subentry, uid);
333 if (subsubentry != NULL)
343 * gst_toc_find_entry:
344 * @toc: #GstToc to search in.
345 * @uid: UID to find #GstTocEntry with.
347 * Find #GstTocEntry with given @uid in the @toc.
349 * Returns: (transfer none): #GstTocEntry with specified @uid from the @toc, or NULL if not found.
352 gst_toc_find_entry (const GstToc * toc, const gchar * uid)
355 GstTocEntry *entry, *subentry;
357 g_return_val_if_fail (toc != NULL, NULL);
358 g_return_val_if_fail (uid != NULL, NULL);
361 while (cur != NULL) {
364 if (g_strcmp0 (entry->uid, uid) == 0)
367 subentry = gst_toc_entry_find_sub_entry (entry, uid);
368 if (subentry != NULL)
377 * gst_toc_entry_copy:
378 * @entry: #GstTocEntry to copy.
380 * Copy #GstTocEntry with all subentries (deep copy).
382 * Returns: newly allocated #GstTocEntry in case of success, NULL otherwise;
383 * free it when done with gst_toc_entry_unref().
386 gst_toc_entry_copy (const GstTocEntry * entry)
388 GstTocEntry *ret, *sub;
392 g_return_val_if_fail (entry != NULL, NULL);
394 ret = gst_toc_entry_new (entry->type, entry->uid);
396 ret->start = entry->start;
397 ret->stop = entry->stop;
399 if (GST_IS_TAG_LIST (entry->tags)) {
400 list = gst_tag_list_copy (entry->tags);
402 gst_tag_list_unref (ret->tags);
406 cur = entry->subentries;
407 while (cur != NULL) {
408 sub = gst_toc_entry_copy (cur->data);
411 ret->subentries = g_list_prepend (ret->subentries, sub);
415 ret->subentries = g_list_reverse (ret->subentries);
422 * @toc: #GstToc to copy.
424 * Copy #GstToc with all subentries (deep copy).
426 * Returns: newly allocated #GstToc in case of success, NULL otherwise;
427 * free it when done with gst_toc_unref().
430 gst_toc_copy (const GstToc * toc)
437 g_return_val_if_fail (toc != NULL, NULL);
439 ret = gst_toc_new (toc->scope);
441 if (GST_IS_TAG_LIST (toc->tags)) {
442 list = gst_tag_list_copy (toc->tags);
443 gst_tag_list_unref (ret->tags);
448 while (cur != NULL) {
449 entry = gst_toc_entry_copy (cur->data);
452 ret->entries = g_list_prepend (ret->entries, entry);
456 ret->entries = g_list_reverse (ret->entries);
461 * gst_toc_entry_set_start_stop_times:
462 * @entry: #GstTocEntry to set values.
463 * @start: start value to set.
464 * @stop: stop value to set.
466 * Set @start and @stop values for the @entry.
469 gst_toc_entry_set_start_stop_times (GstTocEntry * entry, gint64 start,
472 g_return_if_fail (entry != NULL);
474 entry->start = start;
479 * gst_toc_entry_get_start_stop_times:
480 * @entry: #GstTocEntry to get values from.
481 * @start: (out): the storage for the start value, leave #NULL if not need.
482 * @stop: (out): the storage for the stop value, leave #NULL if not need.
484 * Get start and stop values from the @entry and write them into appropriate storages.
486 * Returns: TRUE if all non-NULL storage pointers were filled with appropriate values,
490 gst_toc_entry_get_start_stop_times (const GstTocEntry * entry, gint64 * start,
495 g_return_val_if_fail (entry != NULL, FALSE);
498 *start = entry->start;
506 * gst_toc_entry_type_get_nick:
507 * @type: a #GstTocEntryType.
509 * Converts @type to a string representation.
511 * Returns: Returns a human-readable string for @type. This string is
512 * only for debugging purpose and should not be displayed in a user
516 gst_toc_entry_type_get_nick (GstTocEntryType type)
519 case GST_TOC_ENTRY_TYPE_ANGLE:
521 case GST_TOC_ENTRY_TYPE_VERSION:
523 case GST_TOC_ENTRY_TYPE_EDITION:
525 case GST_TOC_ENTRY_TYPE_TITLE:
527 case GST_TOC_ENTRY_TYPE_TRACK:
529 case GST_TOC_ENTRY_TYPE_CHAPTER:
538 * gst_toc_entry_get_entry_type:
539 * @entry: a #GstTocEntry
541 * Returns: @entry's entry type
544 gst_toc_entry_get_entry_type (const GstTocEntry * entry)
546 g_return_val_if_fail (entry != NULL, GST_TOC_ENTRY_TYPE_INVALID);
552 * gst_toc_entry_is_alternative:
553 * @entry: a #GstTocEntry
555 * Returns: %TRUE if @entry's type is an alternative type, otherwise %FALSE
558 gst_toc_entry_is_alternative (const GstTocEntry * entry)
560 g_return_val_if_fail (entry != NULL, FALSE);
562 return GST_TOC_ENTRY_TYPE_IS_ALTERNATIVE (entry->type);
566 * gst_toc_entry_is_sequence:
567 * @entry: a #GstTocEntry
569 * Returns: %TRUE if @entry's type is a sequence type, otherwise %FALSE
572 gst_toc_entry_is_sequence (const GstTocEntry * entry)
574 g_return_val_if_fail (entry != NULL, FALSE);
576 return GST_TOC_ENTRY_TYPE_IS_SEQUENCE (entry->type);
580 * gst_toc_entry_get_uid:
581 * @entry: A #GstTocEntry instance
583 * Gets the UID of @entry.
585 * Returns: (transfer none): The UID of @entry
588 gst_toc_entry_get_uid (const GstTocEntry * entry)
590 g_return_val_if_fail (entry != NULL, NULL);
596 * gst_toc_entry_append_sub_entry:
597 * @entry: A #GstTocEntry instance
598 * @subentry: (transfer full): A #GstTocEntry
600 * Appends the #GstTocEntry @subentry to @entry.
603 gst_toc_entry_append_sub_entry (GstTocEntry * entry, GstTocEntry * subentry)
605 g_return_if_fail (entry != NULL);
606 g_return_if_fail (subentry != NULL);
607 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
608 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST
610 g_return_if_fail (subentry->toc == NULL);
611 g_return_if_fail (subentry->parent == NULL);
613 entry->subentries = g_list_append (entry->subentries, subentry);
614 subentry->toc = entry->toc;
615 subentry->parent = entry;
617 GST_LOG ("appended %s subentry with uid %s to entry %s",
618 gst_toc_entry_type_get_nick (subentry->type), subentry->uid, entry->uid);
622 * gst_toc_entry_get_sub_entries:
623 * @entry: A #GstTocEntry instance
625 * Gets the sub-entries of @entry.
627 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry of @entry
630 gst_toc_entry_get_sub_entries (const GstTocEntry * entry)
632 g_return_val_if_fail (entry != NULL, NULL);
634 return entry->subentries;
638 * gst_toc_entry_set_tags:
639 * @entry: A #GstTocEntry instance
640 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
642 * Set a #GstTagList with tags for the complete @entry.
645 gst_toc_entry_set_tags (GstTocEntry * entry, GstTagList * tags)
647 g_return_if_fail (entry != NULL);
648 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
651 gst_tag_list_unref (entry->tags);
656 * gst_toc_entry_merge_tags:
657 * @entry: A #GstTocEntry instance
658 * @tags: (allow-none): A #GstTagList or %NULL
659 * @mode: A #GstTagMergeMode
661 * Merge @tags into the existing tags of @entry using @mode.
664 gst_toc_entry_merge_tags (GstTocEntry * entry, GstTagList * tags,
665 GstTagMergeMode mode)
667 g_return_if_fail (entry != NULL);
668 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
671 entry->tags = gst_tag_list_ref (tags);
673 GstTagList *tmp = gst_tag_list_merge (entry->tags, tags, mode);
674 gst_tag_list_unref (entry->tags);
680 * gst_toc_entry_get_tags:
681 * @entry: A #GstTocEntry instance
683 * Gets the tags for @entry.
685 * Returns: (transfer none): A #GstTagList for @entry
688 gst_toc_entry_get_tags (const GstTocEntry * entry)
690 g_return_val_if_fail (entry != NULL, NULL);
696 * gst_toc_entry_get_toc:
697 * @entry: A #GstTocEntry instance
699 * Gets the parent #GstToc of @entry.
701 * Returns: (transfer none): The parent #GstToc of @entry
704 gst_toc_entry_get_toc (GstTocEntry * entry)
706 g_return_val_if_fail (entry != NULL, NULL);
712 * gst_toc_entry_get_parent:
713 * @entry: A #GstTocEntry instance
715 * Gets the parent #GstTocEntry of @entry.
717 * Returns: (transfer none): The parent #GstTocEntry of @entry
720 gst_toc_entry_get_parent (GstTocEntry * entry)
722 g_return_val_if_fail (entry != NULL, NULL);
724 return entry->parent;
727 #ifndef GST_DISABLE_GST_DEBUG
729 gst_toc_dump_entries (GList * entries, guint depth)
734 indent = g_malloc0 (depth + 1);
735 memset (indent, ' ', depth);
736 for (e = entries; e != NULL; e = e->next) {
737 GstTocEntry *entry = e->data;
739 GST_TRACE ("%s+ %s (%s), %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT ", "
740 "tags: %" GST_PTR_FORMAT, indent, entry->uid,
741 gst_toc_entry_type_get_nick (entry->type),
742 GST_TIME_ARGS (entry->start), GST_TIME_ARGS (entry->stop), entry->tags);
744 if (entry->subentries != NULL)
745 gst_toc_dump_entries (entry->subentries, depth + 2);
752 gst_toc_dump (GstToc * toc)
754 #ifndef GST_DISABLE_GST_DEBUG
755 GST_TRACE (" Toc %p, scope: %s, tags: %" GST_PTR_FORMAT, toc,
756 (toc->scope == GST_TOC_SCOPE_GLOBAL) ? "global" : "current", toc->tags);
757 gst_toc_dump_entries (toc->entries, 2);