toc: Fix gst_toc_find_entry()
[platform/upstream/gstreamer.git] / gst / gsttoc.c
1 /* GStreamer
2  * (c) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
3  *
4  * gsttoc.c: GstToc initialization and parsing/creation
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:gsttoc
24  * @short_description: Generic table of contents support
25  * @see_also: #GstStructure, #GstEvent, #GstMessage, #GstQuery
26  *
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.
29  *
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
34  * playlist.
35  *
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).
42  *
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
46  *  stop values.
47  *
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.
52  *
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().
60  */
61
62 #ifdef HAVE_CONFIG_H
63 #  include "config.h"
64 #endif
65
66 #include "gst_private.h"
67 #include "gstenumtypes.h"
68 #include "gsttaglist.h"
69 #include "gststructure.h"
70 #include "gstvalue.h"
71 #include "gsttoc.h"
72 #include "gstpad.h"
73 #include "gstquark.h"
74
75 struct _GstTocEntry
76 {
77   GstMiniObject mini_object;
78
79   gchar *uid;
80   GstTocEntryType type;
81   GstClockTime start, stop;
82   GList *subentries;
83   GstTagList *tags;
84 };
85
86 struct _GstToc
87 {
88   GstMiniObject mini_object;
89
90   GList *entries;
91   GstTagList *tags;
92 };
93
94 #undef gst_toc_copy
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);
100
101 GST_DEFINE_MINI_OBJECT_TYPE (GstToc, gst_toc);
102 GST_DEFINE_MINI_OBJECT_TYPE (GstTocEntry, gst_toc_entry);
103
104 /**
105  * gst_toc_new:
106  *
107  * Create a new #GstToc structure.
108  *
109  * Returns: (transfer full): newly allocated #GstToc structure, free it
110  *     with gst_toc_unref().
111  */
112 GstToc *
113 gst_toc_new (void)
114 {
115   GstToc *toc;
116
117   toc = g_slice_new0 (GstToc);
118
119   gst_mini_object_init (GST_MINI_OBJECT_CAST (toc), 0, GST_TYPE_TOC,
120       (GstMiniObjectCopyFunction) gst_toc_copy, NULL,
121       (GstMiniObjectFreeFunction) gst_toc_free);
122
123   toc->tags = gst_tag_list_new_empty ();
124
125   return toc;
126 }
127
128 /**
129  * gst_toc_set_tags:
130  * @toc: A #GstToc instance
131  * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
132  *
133  * Set a #GstTagList with tags for the complete @toc.
134  */
135 void
136 gst_toc_set_tags (GstToc * toc, GstTagList * tags)
137 {
138   g_return_if_fail (toc != NULL);
139   g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
140
141   if (toc->tags)
142     gst_tag_list_unref (toc->tags);
143   toc->tags = tags;
144 }
145
146 /**
147  * gst_toc_merge_tags:
148  * @toc: A #GstToc instance
149  * @tags: (allow-none): A #GstTagList or %NULL
150  * @mode: A #GstTagMergeMode
151  *
152  * Merge @tags into the existing tags of @toc using @mode.
153  */
154 void
155 gst_toc_merge_tags (GstToc * toc, GstTagList * tags, GstTagMergeMode mode)
156 {
157   g_return_if_fail (toc != NULL);
158   g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
159
160   if (!toc->tags) {
161     toc->tags = gst_tag_list_ref (tags);
162   } else {
163     GstTagList *tmp = gst_tag_list_merge (toc->tags, tags, mode);
164     gst_tag_list_unref (toc->tags);
165     toc->tags = tmp;
166   }
167 }
168
169 /**
170  * gst_toc_get_tags:
171  * @toc: A #GstToc instance
172  *
173  * Gets the tags for @toc.
174  *
175  * Returns: (transfer none): A #GstTagList for @entry
176  */
177 GstTagList *
178 gst_toc_get_tags (const GstToc * toc)
179 {
180   g_return_val_if_fail (toc != NULL, NULL);
181
182   return toc->tags;
183 }
184
185 /**
186  * gst_toc_append_entry:
187  * @toc: A #GstToc instance
188  * @entry: (transfer full): A #GstTocEntry
189  *
190  * Appends the #GstTocEntry @entry to @toc.
191  */
192 void
193 gst_toc_append_entry (GstToc * toc, GstTocEntry * entry)
194 {
195   g_return_if_fail (toc != NULL);
196   g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (toc)));
197
198   toc->entries = g_list_append (toc->entries, entry);
199
200   GST_LOG ("appended %s entry with uid %s to toc %p",
201       gst_toc_entry_type_get_nick (entry->type), entry->uid, toc);
202
203   gst_toc_dump (toc);
204 }
205
206 /**
207  * gst_toc_get_entries:
208  * @toc: A #GstToc instance
209  *
210  * Gets the list of #GstTocEntry of @toc.
211  *
212  * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry for @entry
213  */
214 GList *
215 gst_toc_get_entries (const GstToc * toc)
216 {
217   g_return_val_if_fail (toc != NULL, NULL);
218
219   return toc->entries;
220 }
221
222 static GstTocEntry *
223 gst_toc_entry_new_internal (GstTocEntryType type, const gchar * uid)
224 {
225   GstTocEntry *entry;
226
227   entry = g_slice_new0 (GstTocEntry);
228
229   gst_mini_object_init (GST_MINI_OBJECT_CAST (entry), 0, GST_TYPE_TOC_ENTRY,
230       (GstMiniObjectCopyFunction) gst_toc_entry_copy, NULL,
231       (GstMiniObjectFreeFunction) gst_toc_entry_free);
232
233   entry->uid = g_strdup (uid);
234   entry->type = type;
235   entry->tags = NULL;
236   entry->start = entry->stop = GST_CLOCK_TIME_NONE;
237
238   return entry;
239 }
240
241 /**
242  * gst_toc_entry_new:
243  * @type: entry type.
244  * @uid: unique ID (UID) in the whole TOC.
245  *
246  * Create new #GstTocEntry structure.
247  *
248  * Returns: newly allocated #GstTocEntry structure, free it with gst_toc_entry_unref().
249  */
250 GstTocEntry *
251 gst_toc_entry_new (GstTocEntryType type, const gchar * uid)
252 {
253   g_return_val_if_fail (uid != NULL, NULL);
254
255   return gst_toc_entry_new_internal (type, uid);
256 }
257
258 static void
259 gst_toc_free (GstToc * toc)
260 {
261   g_list_foreach (toc->entries, (GFunc) gst_mini_object_unref, NULL);
262   g_list_free (toc->entries);
263
264   if (toc->tags != NULL)
265     gst_tag_list_unref (toc->tags);
266
267   g_slice_free (GstToc, toc);
268 }
269
270 static void
271 gst_toc_entry_free (GstTocEntry * entry)
272 {
273   g_return_if_fail (entry != NULL);
274
275   g_list_foreach (entry->subentries, (GFunc) gst_mini_object_unref, NULL);
276   g_list_free (entry->subentries);
277
278   g_free (entry->uid);
279
280   if (entry->tags != NULL)
281     gst_tag_list_unref (entry->tags);
282
283   g_slice_free (GstTocEntry, entry);
284 }
285
286 static GstTocEntry *
287 gst_toc_entry_find_sub_entry (const GstTocEntry * entry, const gchar * uid)
288 {
289   GList *cur;
290   GstTocEntry *subentry, *subsubentry;
291
292   g_return_val_if_fail (entry != NULL, NULL);
293   g_return_val_if_fail (uid != NULL, NULL);
294
295   cur = entry->subentries;
296   while (cur != NULL) {
297     subentry = cur->data;
298
299     if (g_strcmp0 (subentry->uid, uid) == 0)
300       return subentry;
301
302     subsubentry = gst_toc_entry_find_sub_entry (subentry, uid);
303     if (subsubentry != NULL)
304       return subsubentry;
305
306     cur = cur->next;
307   }
308
309   return NULL;
310 }
311
312 /**
313  * gst_toc_find_entry:
314  * @toc: #GstToc to search in.
315  * @uid: UID to find #GstTocEntry with.
316  *
317  * Find #GstTocEntry with given @uid in the @toc.
318  *
319  * Returns: (transfer none): #GstTocEntry with specified @uid from the @toc, or NULL if not found.
320  */
321 GstTocEntry *
322 gst_toc_find_entry (const GstToc * toc, const gchar * uid)
323 {
324   GList *cur;
325   GstTocEntry *entry, *subentry;
326
327   g_return_val_if_fail (toc != NULL, NULL);
328   g_return_val_if_fail (uid != NULL, NULL);
329
330   cur = toc->entries;
331   while (cur != NULL) {
332     entry = cur->data;
333
334     if (g_strcmp0 (entry->uid, uid) == 0)
335       return entry;
336
337     subentry = gst_toc_entry_find_sub_entry (entry, uid);
338     if (subentry != NULL)
339       return subentry;
340     cur = cur->next;
341   }
342
343   return NULL;
344 }
345
346 /**
347  * gst_toc_entry_copy:
348  * @entry: #GstTocEntry to copy.
349  *
350  * Copy #GstTocEntry with all subentries (deep copy).
351  *
352  * Returns: newly allocated #GstTocEntry in case of success, NULL otherwise;
353  * free it when done with gst_toc_entry_unref().
354  */
355 static GstTocEntry *
356 gst_toc_entry_copy (const GstTocEntry * entry)
357 {
358   GstTocEntry *ret, *sub;
359   GstTagList *list;
360   GList *cur;
361
362   g_return_val_if_fail (entry != NULL, NULL);
363
364   ret = gst_toc_entry_new (entry->type, entry->uid);
365
366   ret->start = entry->start;
367   ret->stop = entry->stop;
368
369   if (GST_IS_TAG_LIST (entry->tags)) {
370     list = gst_tag_list_copy (entry->tags);
371     if (ret->tags)
372       gst_tag_list_unref (ret->tags);
373     ret->tags = list;
374   }
375
376   cur = entry->subentries;
377   while (cur != NULL) {
378     sub = gst_toc_entry_copy (cur->data);
379
380     if (sub != NULL)
381       ret->subentries = g_list_prepend (ret->subentries, sub);
382
383     cur = cur->next;
384   }
385   ret->subentries = g_list_reverse (ret->subentries);
386
387   return ret;
388 }
389
390 /**
391  * gst_toc_copy:
392  * @toc: #GstToc to copy.
393  *
394  * Copy #GstToc with all subentries (deep copy).
395  *
396  * Returns: newly allocated #GstToc in case of success, NULL otherwise;
397  * free it when done with gst_toc_free().
398  */
399 static GstToc *
400 gst_toc_copy (const GstToc * toc)
401 {
402   GstToc *ret;
403   GstTocEntry *entry;
404   GList *cur;
405   GstTagList *list;
406
407   g_return_val_if_fail (toc != NULL, NULL);
408
409   ret = gst_toc_new ();
410
411   if (GST_IS_TAG_LIST (toc->tags)) {
412     list = gst_tag_list_copy (toc->tags);
413     gst_tag_list_unref (ret->tags);
414     ret->tags = list;
415   }
416
417   cur = toc->entries;
418   while (cur != NULL) {
419     entry = gst_toc_entry_copy (cur->data);
420
421     if (entry != NULL)
422       ret->entries = g_list_prepend (ret->entries, entry);
423
424     cur = cur->next;
425   }
426   ret->entries = g_list_reverse (ret->entries);
427
428   return ret;
429 }
430
431 /**
432  * gst_toc_entry_set_start_stop_times:
433  * @entry: #GstTocEntry to set values.
434  * @start: start value to set.
435  * @stop: stop value to set.
436  *
437  * Set @start and @stop values for the @entry.
438  */
439 void
440 gst_toc_entry_set_start_stop_times (GstTocEntry * entry, gint64 start,
441     gint64 stop)
442 {
443   g_return_if_fail (entry != NULL);
444
445   entry->start = start;
446   entry->stop = stop;
447 }
448
449 /**
450  * gst_toc_entry_get_start_stop_times:
451  * @entry: #GstTocEntry to get values from.
452  * @start: (out): the storage for the start value, leave #NULL if not need.
453  * @stop: (out): the storage for the stop value, leave #NULL if not need.
454  *
455  * Get start and stop values from the @entry and write them into appropriate storages.
456  *
457  * Returns: TRUE if all non-NULL storage pointers were filled with appropriate values,
458  * FALSE otherwise.
459  */
460 gboolean
461 gst_toc_entry_get_start_stop_times (const GstTocEntry * entry, gint64 * start,
462     gint64 * stop)
463 {
464   gboolean ret = TRUE;
465
466   g_return_val_if_fail (entry != NULL, FALSE);
467
468   if (start != NULL)
469     *start = entry->start;
470   if (stop != NULL)
471     *stop = entry->stop;
472
473   return ret;
474 }
475
476 /**
477  * gst_toc_entry_type_get_nick:
478  * @type: a #GstTocEntryType.
479  *
480  * Converts @type to a string representation.
481  *
482  * Returns: Returns a human-readable string for @type. This string is
483  *    only for debugging purpose and should not be displayed in a user
484  *    interface.
485  */
486 const gchar *
487 gst_toc_entry_type_get_nick (GstTocEntryType type)
488 {
489   switch (type) {
490     case GST_TOC_ENTRY_TYPE_ANGLE:
491       return "angle";
492     case GST_TOC_ENTRY_TYPE_VERSION:
493       return "version";
494     case GST_TOC_ENTRY_TYPE_EDITION:
495       return "edition";
496     case GST_TOC_ENTRY_TYPE_TITLE:
497       return "title";
498     case GST_TOC_ENTRY_TYPE_TRACK:
499       return "track";
500     case GST_TOC_ENTRY_TYPE_CHAPTER:
501       return "chapter";
502     default:
503       break;
504   }
505   return "invalid";
506 }
507
508 /**
509  * gst_toc_entry_get_entry_type:
510  * @entry: a #GstTocEntry
511  *
512  * Returns: @entry's entry type
513  */
514 GstTocEntryType
515 gst_toc_entry_get_entry_type (const GstTocEntry * entry)
516 {
517   g_return_val_if_fail (entry != NULL, GST_TOC_ENTRY_TYPE_INVALID);
518
519   return entry->type;
520 }
521
522 /**
523  * gst_toc_entry_is_alternative:
524  * @entry: a #GstTocEntry
525  *
526  * Returns: %TRUE if @entry's type is an alternative type, otherwise %FALSE
527  */
528 gboolean
529 gst_toc_entry_is_alternative (const GstTocEntry * entry)
530 {
531   g_return_val_if_fail (entry != NULL, FALSE);
532
533   return GST_TOC_ENTRY_TYPE_IS_ALTERNATIVE (entry->type);
534 }
535
536 /**
537  * gst_toc_entry_is_sequence:
538  * @entry: a #GstTocEntry
539  *
540  * Returns: %TRUE if @entry's type is a sequence type, otherwise %FALSE
541  */
542 gboolean
543 gst_toc_entry_is_sequence (const GstTocEntry * entry)
544 {
545   g_return_val_if_fail (entry != NULL, FALSE);
546
547   return GST_TOC_ENTRY_TYPE_IS_SEQUENCE (entry->type);
548 }
549
550 /**
551  * gst_toc_entry_get_uid:
552  * @entry: A #GstTocEntry instance
553  *
554  * Gets the UID of @entry.
555  *
556  * Returns: (transfer none): The UID of @entry
557  */
558 const gchar *
559 gst_toc_entry_get_uid (const GstTocEntry * entry)
560 {
561   g_return_val_if_fail (entry != NULL, NULL);
562
563   return entry->uid;
564 }
565
566 /**
567  * gst_toc_entry_append_sub_entry:
568  * @entry: A #GstTocEntry instance
569  * @subentry: (transfer full): A #GstTocEntry
570  *
571  * Appends the #GstTocEntry @subentry to @entry.
572  */
573 void
574 gst_toc_entry_append_sub_entry (GstTocEntry * entry, GstTocEntry * subentry)
575 {
576   g_return_if_fail (entry != NULL);
577   g_return_if_fail (subentry != NULL);
578   g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
579
580   entry->subentries = g_list_append (entry->subentries, subentry);
581
582   GST_LOG ("appended %s subentry with uid %s to entry %s",
583       gst_toc_entry_type_get_nick (subentry->type), subentry->uid, entry->uid);
584 }
585
586 /**
587  * gst_toc_entry_get_uid:
588  * @entry: A #GstTocEntry instance
589  *
590  * Gets the sub-entries of @entry.
591  *
592  * Returns: (transfer none) (element-type Gst.TocEntry): A #GList of #GstTocEntry of @entry
593  */
594 GList *
595 gst_toc_entry_get_sub_entries (const GstTocEntry * entry)
596 {
597   g_return_val_if_fail (entry != NULL, NULL);
598
599   return entry->subentries;
600 }
601
602 /**
603  * gst_toc_entry_set_tags:
604  * @entry: A #GstTocEntry instance
605  * @tags: (allow-none) (transfer full): A #GstTagList or %NULL
606  *
607  * Set a #GstTagList with tags for the complete @entry.
608  */
609 void
610 gst_toc_entry_set_tags (GstTocEntry * entry, GstTagList * tags)
611 {
612   g_return_if_fail (entry != NULL);
613   g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
614
615   if (entry->tags)
616     gst_tag_list_unref (entry->tags);
617   entry->tags = tags;
618 }
619
620 /**
621  * gst_toc_entry_merge_tags:
622  * @entry: A #GstTocEntry instance
623  * @tags: (allow-none): A #GstTagList or %NULL
624  * @mode: A #GstTagMergeMode
625  *
626  * Merge @tags into the existing tags of @entry using @mode.
627  */
628 void
629 gst_toc_entry_merge_tags (GstTocEntry * entry, GstTagList * tags,
630     GstTagMergeMode mode)
631 {
632   g_return_if_fail (entry != NULL);
633   g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (entry)));
634
635   if (!entry->tags) {
636     entry->tags = gst_tag_list_ref (tags);
637   } else {
638     GstTagList *tmp = gst_tag_list_merge (entry->tags, tags, mode);
639     gst_tag_list_unref (entry->tags);
640     entry->tags = tmp;
641   }
642 }
643
644 /**
645  * gst_toc_entry_get_tags:
646  * @entry: A #GstTocEntry instance
647  *
648  * Gets the tags for @entry.
649  *
650  * Returns: (transfer none): A #GstTagList for @entry
651  */
652 GstTagList *
653 gst_toc_entry_get_tags (const GstTocEntry * entry)
654 {
655   g_return_val_if_fail (entry != NULL, NULL);
656
657   return entry->tags;
658 }
659
660 #ifndef GST_DISABLE_GST_DEBUG
661 static void
662 gst_toc_dump_entries (GList * entries, guint depth)
663 {
664   GList *e;
665   gchar *indent;
666
667   indent = g_malloc0 (depth + 1);
668   memset (indent, ' ', depth);
669   for (e = entries; e != NULL; e = e->next) {
670     GstTocEntry *entry = e->data;
671
672     GST_TRACE ("%s+ %s (%s), %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT ", "
673         "tags: %" GST_PTR_FORMAT, indent, entry->uid,
674         gst_toc_entry_type_get_nick (entry->type),
675         GST_TIME_ARGS (entry->start), GST_TIME_ARGS (entry->stop), entry->tags);
676
677     if (entry->subentries != NULL)
678       gst_toc_dump_entries (entry->subentries, depth + 2);
679   }
680   g_free (indent);
681 }
682 #endif
683
684 void
685 gst_toc_dump (GstToc * toc)
686 {
687 #ifndef GST_DISABLE_GST_DEBUG
688   GST_TRACE ("        Toc %p, tags: %" GST_PTR_FORMAT, toc, toc->tags);
689   gst_toc_dump_entries (toc->entries, 2);
690 #endif
691 }