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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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 #GstToc.entries #GstTocEntry.subentries
39 * lists. You should use GST_TOC_ENTRY_TYPE_CHAPTER for generic TOC entry and
40 * GST_TOC_ENTRY_TYPE_EDITION for the entries which are considered to be alternatives
41 * (like DVD angles, Matroska editions and so on).
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 * Please, use #GstToc.info and #GstTocEntry.info fields in that way: create a #GstStructure,
49 * put all info related to your element there and put this structure into the info field under
50 * the name of your element. Some fields in the info structure can be used for internal purposes,
51 * so you should use it in the way described above to not to overwrite already existent fields.
53 * Use gst_event_new_toc() to create a new TOC #GstEvent, and gst_event_parse_toc() to
54 * parse received TOC event. Use gst_event_new_toc_select() to create a new TOC select #GstEvent,
55 * and gst_event_parse_toc_select() to parse received TOC select event. The same rule for
56 * the #GstMessage: gst_message_new_toc() to create new TOC #GstMessage, and
57 * gst_message_parse_toc() to parse received TOC message. Also you can create a new TOC query
58 * with gst_query_new_toc(), set it with gst_query_set_toc() and parse it with
59 * gst_query_parse_toc().
66 #include "gst_private.h"
67 #include "gstenumtypes.h"
68 #include "gsttaglist.h"
69 #include "gststructure.h"
77 GstMiniObject mini_object;
81 GstClockTime start, stop;
88 GstMiniObject mini_object;
95 static GstToc *gst_toc_copy (const GstToc * toc);
96 static void gst_toc_free (GstToc * toc);
97 #undef gst_toc_entry_copy
98 static GstTocEntry *gst_toc_entry_copy (const GstTocEntry * toc);
99 static void gst_toc_entry_free (GstTocEntry * toc);
101 GST_DEFINE_MINI_OBJECT_TYPE (GstToc, gst_toc);
102 GST_DEFINE_MINI_OBJECT_TYPE (GstTocEntry, gst_toc_entry);
107 * Create a new #GstToc structure.
109 * Returns: (transfer full): newly allocated #GstToc structure, free it
110 * with gst_toc_unref().
119 toc = g_slice_new0 (GstToc);
121 gst_mini_object_init (GST_MINI_OBJECT_CAST (toc), 0, GST_TYPE_TOC,
122 (GstMiniObjectCopyFunction) gst_toc_copy, NULL,
123 (GstMiniObjectFreeFunction) gst_toc_free);
125 toc->tags = gst_tag_list_new_empty ();
132 * @toc: A #GstToc instance
133 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
135 * Set a #GstTagList with tags for the complete @toc.
140 gst_toc_set_tags (GstToc * toc, GstTagList * tags)
142 g_return_if_fail (toc != NULL);
143 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
146 gst_tag_list_unref (toc->tags);
151 * gst_toc_merge_tags:
152 * @toc: A #GstToc instance
153 * @tags: (allow-none): A #GstTagList or %NULL
154 * @mode: A #GstTagMergeMode
156 * Merge @tags into the existing tags of @toc using @mode.
161 gst_toc_merge_tags (GstToc * toc, GstTagList * tags, GstTagMergeMode mode)
163 g_return_if_fail (toc != NULL);
164 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
167 toc->tags = gst_tag_list_ref (tags);
169 GstTagList *tmp = gst_tag_list_merge (toc->tags, tags, mode);
170 gst_tag_list_unref (toc->tags);
177 * @toc: A #GstToc instance
179 * Gets the tags for @toc.
181 * Returns: (transfer none): A #GstTagList for @entry
186 gst_toc_get_tags (const GstToc * toc)
188 g_return_val_if_fail (toc != NULL, NULL);
194 * gst_toc_append_entry:
195 * @toc: A #GstToc instance
196 * @entry: (transfer full): A #GstTocEntry
198 * Appends the #GstTocEntry @entry to @toc.
203 gst_toc_append_entry (GstToc * toc, GstTocEntry * entry)
205 g_return_if_fail (toc != NULL);
206 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
208 toc->entries = g_list_append (toc->entries, entry);
210 GST_LOG ("appended %s entry with uid %s to toc %p",
211 gst_toc_entry_type_get_nick (entry->type), entry->uid, toc);
217 * gst_toc_get_entries:
218 * @toc: A #GstToc instance
220 * Gets the list of #GstTocEntry of @toc.
222 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry for @entry
227 gst_toc_get_entries (const GstToc * toc)
229 g_return_val_if_fail (toc != NULL, NULL);
235 gst_toc_entry_new_internal (GstTocEntryType type, const gchar * uid)
239 entry = g_slice_new0 (GstTocEntry);
241 gst_mini_object_init (GST_MINI_OBJECT_CAST (entry), 0, GST_TYPE_TOC_ENTRY,
242 (GstMiniObjectCopyFunction) gst_toc_entry_copy, NULL,
243 (GstMiniObjectFreeFunction) gst_toc_entry_free);
245 entry->uid = g_strdup (uid);
248 entry->start = entry->stop = GST_CLOCK_TIME_NONE;
256 * @uid: unique ID (UID) in the whole TOC.
258 * Create new #GstTocEntry structure.
260 * Returns: newly allocated #GstTocEntry structure, free it with gst_toc_entry_unref().
265 gst_toc_entry_new (GstTocEntryType type, const gchar * uid)
267 g_return_val_if_fail (uid != NULL, NULL);
269 return gst_toc_entry_new_internal (type, uid);
273 gst_toc_free (GstToc * toc)
275 g_list_foreach (toc->entries, (GFunc) gst_mini_object_unref, NULL);
276 g_list_free (toc->entries);
278 if (toc->tags != NULL)
279 gst_tag_list_unref (toc->tags);
281 g_slice_free (GstToc, toc);
285 gst_toc_entry_free (GstTocEntry * entry)
287 g_return_if_fail (entry != NULL);
289 g_list_foreach (entry->subentries, (GFunc) gst_mini_object_unref, NULL);
290 g_list_free (entry->subentries);
294 if (entry->tags != NULL)
295 gst_tag_list_unref (entry->tags);
297 g_slice_free (GstTocEntry, entry);
301 gst_toc_check_entry_for_uid (const GstTocEntry * entry, const gchar * uid)
305 g_return_val_if_fail (entry != NULL, FALSE);
306 g_return_val_if_fail (uid != NULL, FALSE);
308 if (g_strcmp0 (entry->uid, uid) == 0)
311 cur = entry->subentries;
312 while (cur != NULL) {
313 if (gst_toc_check_entry_for_uid (cur->data, uid))
322 * gst_toc_find_entry:
323 * @toc: #GstToc to search in.
324 * @uid: UID to find #GstTocEntry with.
326 * Find #GstTocEntry with given @uid in the @toc.
328 * Returns: #GstTocEntry with specified @uid from the @toc, or NULL if not found.
333 gst_toc_find_entry (const GstToc * toc, const gchar * uid)
337 g_return_val_if_fail (toc != NULL, NULL);
338 g_return_val_if_fail (uid != NULL, NULL);
341 while (cur != NULL) {
342 if (gst_toc_check_entry_for_uid (cur->data, uid))
351 * gst_toc_entry_copy:
352 * @entry: #GstTocEntry to copy.
354 * Copy #GstTocEntry with all subentries (deep copy).
356 * Returns: newly allocated #GstTocEntry in case of success, NULL otherwise;
357 * free it when done with gst_toc_entry_unref().
362 gst_toc_entry_copy (const GstTocEntry * entry)
364 GstTocEntry *ret, *sub;
368 g_return_val_if_fail (entry != NULL, NULL);
370 ret = gst_toc_entry_new (entry->type, entry->uid);
372 ret->start = entry->start;
373 ret->stop = entry->stop;
375 if (GST_IS_TAG_LIST (entry->tags)) {
376 list = gst_tag_list_copy (entry->tags);
378 gst_tag_list_unref (ret->tags);
382 cur = entry->subentries;
383 while (cur != NULL) {
384 sub = gst_toc_entry_copy (cur->data);
387 ret->subentries = g_list_prepend (ret->subentries, sub);
391 ret->subentries = g_list_reverse (ret->subentries);
398 * @toc: #GstToc to copy.
400 * Copy #GstToc with all subentries (deep copy).
402 * Returns: newly allocated #GstToc in case of success, NULL otherwise;
403 * free it when done with gst_toc_free().
408 gst_toc_copy (const GstToc * toc)
415 g_return_val_if_fail (toc != NULL, NULL);
417 ret = gst_toc_new ();
419 if (GST_IS_TAG_LIST (toc->tags)) {
420 list = gst_tag_list_copy (toc->tags);
421 gst_tag_list_unref (ret->tags);
426 while (cur != NULL) {
427 entry = gst_toc_entry_copy (cur->data);
430 ret->entries = g_list_prepend (ret->entries, entry);
434 ret->entries = g_list_reverse (ret->entries);
440 * gst_toc_entry_set_start_stop_times:
441 * @entry: #GstTocEntry to set values.
442 * @start: start value to set.
443 * @stop: stop value to set.
445 * Set @start and @stop values for the @entry.
450 gst_toc_entry_set_start_stop_times (GstTocEntry * entry, gint64 start,
453 g_return_if_fail (entry != NULL);
455 entry->start = start;
460 * gst_toc_entry_get_start_stop_times:
461 * @entry: #GstTocEntry to get values from.
462 * @start: (out): the storage for the start value, leave #NULL if not need.
463 * @stop: (out): the storage for the stop value, leave #NULL if not need.
465 * Get start and stop values from the @entry and write them into appropriate storages.
467 * Returns: TRUE if all non-NULL storage pointers were filled with appropriate values,
473 gst_toc_entry_get_start_stop_times (const GstTocEntry * entry, gint64 * start,
478 g_return_val_if_fail (entry != NULL, FALSE);
481 *start = entry->start;
489 * gst_toc_entry_type_get_nick:
490 * @type: a #GstTocEntryType.
492 * Converts @type to a string representation.
494 * Returns: Returns a human-readable string for @type. This string is
495 * only for debugging purpose and should not be displayed in a user
499 gst_toc_entry_type_get_nick (GstTocEntryType type)
502 case GST_TOC_ENTRY_TYPE_ANGLE:
504 case GST_TOC_ENTRY_TYPE_VERSION:
506 case GST_TOC_ENTRY_TYPE_EDITION:
508 case GST_TOC_ENTRY_TYPE_TITLE:
510 case GST_TOC_ENTRY_TYPE_TRACK:
512 case GST_TOC_ENTRY_TYPE_CHAPTER:
521 * gst_toc_entry_get_entry_type:
522 * @entry: a #GstTocEntry
524 * Returns: @entry's entry type
527 gst_toc_entry_get_entry_type (const GstTocEntry * entry)
529 g_return_val_if_fail (entry != NULL, GST_TOC_ENTRY_TYPE_INVALID);
535 * gst_toc_entry_is_alternative:
536 * @entry: a #GstTocEntry
538 * Returns: %TRUE if @entry's type is an alternative type, otherwise %FALSE
541 gst_toc_entry_is_alternative (const GstTocEntry * entry)
543 g_return_val_if_fail (entry != NULL, FALSE);
545 return GST_TOC_ENTRY_TYPE_IS_ALTERNATIVE (entry->type);
549 * gst_toc_entry_is_sequence:
550 * @entry: a #GstTocEntry
552 * Returns: %TRUE if @entry's type is a sequence type, otherwise %FALSE
555 gst_toc_entry_is_sequence (const GstTocEntry * entry)
557 g_return_val_if_fail (entry != NULL, FALSE);
559 return GST_TOC_ENTRY_TYPE_IS_SEQUENCE (entry->type);
563 * gst_toc_entry_get_uid:
564 * @entry: A #GstTocEntry instance
566 * Gets the UID of @entry.
568 * Returns: (transfer none): The UID of @entry
573 gst_toc_entry_get_uid (const GstTocEntry * entry)
575 g_return_val_if_fail (entry != NULL, NULL);
581 * gst_toc_entry_append_sub_entry:
582 * @entry: A #GstTocEntry instance
583 * @subentry: (transfer full): A #GstTocEntry
585 * Appends the #GstTocEntry @subentry to @entry.
590 gst_toc_entry_append_sub_entry (GstTocEntry * entry, GstTocEntry * subentry)
592 g_return_if_fail (entry != NULL);
593 g_return_if_fail (subentry != NULL);
594 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
596 entry->subentries = g_list_append (entry->subentries, subentry);
598 GST_LOG ("appended %s subentry with uid %s to entry %s",
599 gst_toc_entry_type_get_nick (subentry->type), subentry->uid, entry->uid);
603 * gst_toc_entry_get_uid:
604 * @entry: A #GstTocEntry instance
606 * Gets the sub-entries of @entry.
608 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry of @entry
613 gst_toc_entry_get_sub_entries (const GstTocEntry * entry)
615 g_return_val_if_fail (entry != NULL, NULL);
617 return entry->subentries;
621 * gst_toc_entry_set_tags:
622 * @entry: A #GstTocEntry instance
623 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
625 * Set a #GstTagList with tags for the complete @entry.
630 gst_toc_entry_set_tags (GstTocEntry * entry, GstTagList * tags)
632 g_return_if_fail (entry != NULL);
633 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
636 gst_tag_list_unref (entry->tags);
641 * gst_toc_entry_merge_tags:
642 * @entry: A #GstTocEntry instance
643 * @tags: (allow-none): A #GstTagList or %NULL
644 * @mode: A #GstTagMergeMode
646 * Merge @tags into the existing tags of @entry using @mode.
651 gst_toc_entry_merge_tags (GstTocEntry * entry, GstTagList * tags,
652 GstTagMergeMode mode)
654 g_return_if_fail (entry != NULL);
655 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
658 entry->tags = gst_tag_list_ref (tags);
660 GstTagList *tmp = gst_tag_list_merge (entry->tags, tags, mode);
661 gst_tag_list_unref (entry->tags);
667 * gst_toc_entry_get_tags:
668 * @entry: A #GstTocEntry instance
670 * Gets the tags for @entry.
672 * Returns: (transfer none): A #GstTagList for @entry
677 gst_toc_entry_get_tags (const GstTocEntry * entry)
679 g_return_val_if_fail (entry != NULL, NULL);
684 #ifndef GST_DISABLE_GST_DEBUG
686 gst_toc_dump_entries (GList * entries, guint depth)
691 indent = g_malloc0 (depth + 1);
692 memset (indent, ' ', depth);
693 for (e = entries; e != NULL; e = e->next) {
694 GstTocEntry *entry = e->data;
696 GST_TRACE ("%s+ %s (%s), %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT ", "
697 "tags: %" GST_PTR_FORMAT, indent, entry->uid,
698 gst_toc_entry_type_get_nick (entry->type),
699 GST_TIME_ARGS (entry->start), GST_TIME_ARGS (entry->stop), entry->tags);
701 if (entry->subentries != NULL)
702 gst_toc_dump_entries (entry->subentries, depth + 2);
709 gst_toc_dump (GstToc * toc)
711 #ifndef GST_DISABLE_GST_DEBUG
712 GST_TRACE (" Toc %p, tags: %" GST_PTR_FORMAT, toc, toc->tags);
713 gst_toc_dump_entries (toc->entries, 2);