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;
86 gpointer _gst_reserved[GST_PADDING];
91 GstMiniObject mini_object;
97 gpointer _gst_reserved[GST_PADDING];
101 static GstToc *gst_toc_copy (const GstToc * toc);
102 static void gst_toc_free (GstToc * toc);
103 #undef gst_toc_entry_copy
104 static GstTocEntry *gst_toc_entry_copy (const GstTocEntry * toc);
105 static void gst_toc_entry_free (GstTocEntry * toc);
107 GST_DEFINE_MINI_OBJECT_TYPE (GstToc, gst_toc);
108 GST_DEFINE_MINI_OBJECT_TYPE (GstTocEntry, gst_toc_entry);
113 * Create a new #GstToc structure.
115 * Returns: (transfer full): newly allocated #GstToc structure, free it
116 * with gst_toc_unref().
125 toc = g_slice_new0 (GstToc);
127 gst_mini_object_init (GST_MINI_OBJECT_CAST (toc), 0, GST_TYPE_TOC,
128 (GstMiniObjectCopyFunction) gst_toc_copy, NULL,
129 (GstMiniObjectFreeFunction) gst_toc_free);
131 toc->tags = gst_tag_list_new_empty ();
138 * @toc: A #GstToc instance
139 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
141 * Set a #GstTagList with tags for the complete @toc.
146 gst_toc_set_tags (GstToc * toc, GstTagList * tags)
148 g_return_if_fail (toc != NULL);
149 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
152 gst_tag_list_unref (toc->tags);
157 * gst_toc_merge_tags:
158 * @toc: A #GstToc instance
159 * @tags: (allow-none): A #GstTagList or %NULL
160 * @mode: A #GstTagMergeMode
162 * Merge @tags into the existing tags of @toc using @mode.
167 gst_toc_merge_tags (GstToc * toc, GstTagList * tags, GstTagMergeMode mode)
169 g_return_if_fail (toc != NULL);
170 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
173 toc->tags = gst_tag_list_ref (tags);
175 GstTagList *tmp = gst_tag_list_merge (toc->tags, tags, mode);
176 gst_tag_list_unref (toc->tags);
183 * @toc: A #GstToc instance
185 * Gets the tags for @toc.
187 * Returns: (transfer none): A #GstTagList for @entry
192 gst_toc_get_tags (const GstToc * toc)
194 g_return_val_if_fail (toc != NULL, NULL);
200 * gst_toc_append_entry:
201 * @toc: A #GstToc instance
202 * @entry: (transfer full): A #GstTocEntry
204 * Appends the #GstTocEntry @entry to @toc.
209 gst_toc_append_entry (GstToc * toc, GstTocEntry * entry)
211 g_return_if_fail (toc != NULL);
212 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
214 toc->entries = g_list_append (toc->entries, entry);
218 * gst_toc_get_entries:
219 * @toc: A #GstToc instance
221 * Gets the list of #GstTocEntry of @toc.
223 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry for @entry
228 gst_toc_get_entries (const GstToc * toc)
230 g_return_val_if_fail (toc != NULL, NULL);
236 gst_toc_entry_new_internal (GstTocEntryType type, const gchar * uid)
240 entry = g_slice_new0 (GstTocEntry);
242 gst_mini_object_init (GST_MINI_OBJECT_CAST (entry), 0, GST_TYPE_TOC_ENTRY,
243 (GstMiniObjectCopyFunction) gst_toc_entry_copy, NULL,
244 (GstMiniObjectFreeFunction) gst_toc_entry_free);
246 entry->uid = g_strdup (uid);
249 entry->start = entry->stop = GST_CLOCK_TIME_NONE;
257 * @uid: unique ID (UID) in the whole TOC.
259 * Create new #GstTocEntry structure.
261 * Returns: newly allocated #GstTocEntry structure, free it with gst_toc_entry_unref().
266 gst_toc_entry_new (GstTocEntryType type, const gchar * uid)
268 g_return_val_if_fail (uid != NULL, NULL);
270 return gst_toc_entry_new_internal (type, uid);
274 gst_toc_free (GstToc * toc)
276 g_list_foreach (toc->entries, (GFunc) gst_mini_object_unref, NULL);
277 g_list_free (toc->entries);
279 if (toc->tags != NULL)
280 gst_tag_list_unref (toc->tags);
282 g_slice_free (GstToc, toc);
286 gst_toc_entry_free (GstTocEntry * entry)
288 g_return_if_fail (entry != NULL);
290 g_list_foreach (entry->subentries, (GFunc) gst_mini_object_unref, NULL);
291 g_list_free (entry->subentries);
295 if (entry->tags != NULL)
296 gst_tag_list_unref (entry->tags);
298 g_slice_free (GstTocEntry, entry);
302 gst_toc_check_entry_for_uid (const GstTocEntry * entry, const gchar * uid)
306 g_return_val_if_fail (entry != NULL, FALSE);
307 g_return_val_if_fail (uid != NULL, FALSE);
309 if (g_strcmp0 (entry->uid, uid) == 0)
312 cur = entry->subentries;
313 while (cur != NULL) {
314 if (gst_toc_check_entry_for_uid (cur->data, uid))
323 * gst_toc_find_entry:
324 * @toc: #GstToc to search in.
325 * @uid: UID to find #GstTocEntry with.
327 * Find #GstTocEntry with given @uid in the @toc.
329 * Returns: #GstTocEntry with specified @uid from the @toc, or NULL if not found.
334 gst_toc_find_entry (const GstToc * toc, const gchar * uid)
338 g_return_val_if_fail (toc != NULL, NULL);
339 g_return_val_if_fail (uid != NULL, NULL);
342 while (cur != NULL) {
343 if (gst_toc_check_entry_for_uid (cur->data, uid))
352 * gst_toc_entry_copy:
353 * @entry: #GstTocEntry to copy.
355 * Copy #GstTocEntry with all subentries (deep copy).
357 * Returns: newly allocated #GstTocEntry in case of success, NULL otherwise;
358 * free it when done with gst_toc_entry_unref().
363 gst_toc_entry_copy (const GstTocEntry * entry)
365 GstTocEntry *ret, *sub;
369 g_return_val_if_fail (entry != NULL, NULL);
371 ret = gst_toc_entry_new (entry->type, entry->uid);
373 ret->start = entry->start;
374 ret->stop = entry->stop;
376 if (GST_IS_TAG_LIST (entry->tags)) {
377 list = gst_tag_list_copy (entry->tags);
379 gst_tag_list_unref (ret->tags);
383 cur = entry->subentries;
384 while (cur != NULL) {
385 sub = gst_toc_entry_copy (cur->data);
388 ret->subentries = g_list_prepend (ret->subentries, sub);
392 ret->subentries = g_list_reverse (ret->subentries);
399 * @toc: #GstToc to copy.
401 * Copy #GstToc with all subentries (deep copy).
403 * Returns: newly allocated #GstToc in case of success, NULL otherwise;
404 * free it when done with gst_toc_free().
409 gst_toc_copy (const GstToc * toc)
416 g_return_val_if_fail (toc != NULL, NULL);
418 ret = gst_toc_new ();
420 if (GST_IS_TAG_LIST (toc->tags)) {
421 list = gst_tag_list_copy (toc->tags);
422 gst_tag_list_unref (ret->tags);
427 while (cur != NULL) {
428 entry = gst_toc_entry_copy (cur->data);
431 ret->entries = g_list_prepend (ret->entries, entry);
435 ret->entries = g_list_reverse (ret->entries);
441 * gst_toc_entry_set_start_stop_times:
442 * @entry: #GstTocEntry to set values.
443 * @start: start value to set.
444 * @stop: stop value to set.
446 * Set @start and @stop values for the @entry.
451 gst_toc_entry_set_start_stop_times (GstTocEntry * entry, gint64 start,
454 g_return_if_fail (entry != NULL);
456 entry->start = start;
461 * gst_toc_entry_get_start_stop_times:
462 * @entry: #GstTocEntry to get values from.
463 * @start: (out): the storage for the start value, leave #NULL if not need.
464 * @stop: (out): the storage for the stop value, leave #NULL if not need.
466 * Get start and stop values from the @entry and write them into appropriate storages.
468 * Returns: TRUE if all non-NULL storage pointers were filled with appropriate values,
474 gst_toc_entry_get_start_stop_times (const GstTocEntry * entry, gint64 * start,
479 g_return_val_if_fail (entry != NULL, FALSE);
482 *start = entry->start;
490 * gst_toc_entry_type_get_nick:
491 * @type: a #GstTocEntryType.
493 * Converts @type to a string representation.
495 * Returns: Returns a human-readable string for @type. This string is
496 * only for debugging purpose and should not be displayed in a user
500 gst_toc_entry_type_get_nick (GstTocEntryType type)
503 case GST_TOC_ENTRY_TYPE_ANGLE:
505 case GST_TOC_ENTRY_TYPE_VERSION:
507 case GST_TOC_ENTRY_TYPE_EDITION:
509 case GST_TOC_ENTRY_TYPE_TITLE:
511 case GST_TOC_ENTRY_TYPE_TRACK:
513 case GST_TOC_ENTRY_TYPE_CHAPTER:
522 * gst_toc_entry_get_entry_type:
523 * @entry: a #GstTocEntry
525 * Returns: @entry's entry type
528 gst_toc_entry_get_entry_type (const GstTocEntry * entry)
530 g_return_val_if_fail (entry != NULL, GST_TOC_ENTRY_TYPE_INVALID);
536 * gst_toc_entry_is_alternative:
537 * @entry: a #GstTocEntry
539 * Returns: %TRUE if @entry's type is an alternative type, otherwise %FALSE
542 gst_toc_entry_is_alternative (const GstTocEntry * entry)
544 g_return_val_if_fail (entry != NULL, FALSE);
546 return GST_TOC_ENTRY_TYPE_IS_ALTERNATIVE (entry->type);
550 * gst_toc_entry_is_sequence:
551 * @entry: a #GstTocEntry
553 * Returns: %TRUE if @entry's type is a sequence type, otherwise %FALSE
556 gst_toc_entry_is_sequence (const GstTocEntry * entry)
558 g_return_val_if_fail (entry != NULL, FALSE);
560 return GST_TOC_ENTRY_TYPE_IS_SEQUENCE (entry->type);
564 * gst_toc_entry_get_uid:
565 * @entry: A #GstTocEntry instance
567 * Gets the UID of @entry.
569 * Returns: (transfer none): The UID of @entry
574 gst_toc_entry_get_uid (const GstTocEntry * entry)
576 g_return_val_if_fail (entry != NULL, NULL);
582 * gst_toc_entry_append_sub_entry:
583 * @entry: A #GstTocEntry instance
584 * @subentry: (transfer full): A #GstTocEntry
586 * Appends the #GstTocEntry @subentry to @entry.
591 gst_toc_entry_append_sub_entry (GstTocEntry * entry, GstTocEntry * subentry)
593 g_return_if_fail (entry != NULL);
594 g_return_if_fail (subentry != NULL);
595 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
597 entry->subentries = g_list_append (entry->subentries, subentry);
601 * gst_toc_entry_get_uid:
602 * @entry: A #GstTocEntry instance
604 * Gets the sub-entries of @entry.
606 * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry of @entry
611 gst_toc_entry_get_sub_entries (const GstTocEntry * entry)
613 g_return_val_if_fail (entry != NULL, NULL);
615 return entry->subentries;
619 * gst_toc_entry_set_tags:
620 * @entry: A #GstTocEntry instance
621 * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
623 * Set a #GstTagList with tags for the complete @entry.
628 gst_toc_entry_set_tags (GstTocEntry * entry, GstTagList * tags)
630 g_return_if_fail (entry != NULL);
631 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
634 gst_tag_list_unref (entry->tags);
639 * gst_toc_entry_merge_tags:
640 * @entry: A #GstTocEntry instance
641 * @tags: (allow-none): A #GstTagList or %NULL
642 * @mode: A #GstTagMergeMode
644 * Merge @tags into the existing tags of @entry using @mode.
649 gst_toc_entry_merge_tags (GstTocEntry * entry, GstTagList * tags,
650 GstTagMergeMode mode)
652 g_return_if_fail (entry != NULL);
653 g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
656 entry->tags = gst_tag_list_ref (tags);
658 GstTagList *tmp = gst_tag_list_merge (entry->tags, tags, mode);
659 gst_tag_list_unref (entry->tags);
665 * gst_toc_entry_get_tags:
666 * @entry: A #GstTocEntry instance
668 * Gets the tags for @entry.
670 * Returns: (transfer none): A #GstTagList for @entry
675 gst_toc_entry_get_tags (const GstTocEntry * entry)
677 g_return_val_if_fail (entry != NULL, NULL);