summaryrefslogtreecommitdiffstats
path: root/ext/annodex/gstcmmltag.c
blob: a224c6da748a100a69a49884b912b8626d5c2d43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
 * gstcmmltags.c - GStreamer CMML tag support
 * Copyright (C) 2005 Alessandro Decina
 * 
 * Authors:
 *   Alessandro Decina <alessandro@nnva.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>

#include "gstcmmlparser.h"
#include "gstcmmltag.h"
#include "gstannodex.h"

enum
{
  ARG_0,
  GST_CMML_TAG_STREAM_TIMEBASE,
  GST_CMML_TAG_STREAM_UTC,
  GST_CMML_TAG_STREAM_IMPORTS,
  GST_CMML_TAG_HEAD_TITLE,
  GST_CMML_TAG_HEAD_BASE,
  GST_CMML_TAG_HEAD_META,
  GST_CMML_TAG_CLIP_EMPTY,
  GST_CMML_TAG_CLIP_ID,
  GST_CMML_TAG_CLIP_TRACK,
  GST_CMML_TAG_CLIP_START_TIME,
  GST_CMML_TAG_CLIP_END_TIME,
  GST_CMML_TAG_CLIP_ANCHOR_HREF,
  GST_CMML_TAG_CLIP_ANCHOR_TEXT,
  GST_CMML_TAG_CLIP_IMG_SRC,
  GST_CMML_TAG_CLIP_IMG_ALT,
  GST_CMML_TAG_CLIP_DESC_TEXT,
  GST_CMML_TAG_CLIP_META,
};

G_DEFINE_TYPE (GstCmmlTagStream, gst_cmml_tag_stream, G_TYPE_OBJECT);
static void gst_cmml_tag_stream_finalize (GObject * object);
static void gst_cmml_tag_stream_set_property (GObject * object,
    guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_stream_get_property (GObject * object,
    guint property_id, GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_stream_value_from_string_value (const GValue * src,
    GValue * dest);

G_DEFINE_TYPE (GstCmmlTagHead, gst_cmml_tag_head, G_TYPE_OBJECT);
static void gst_cmml_tag_head_finalize (GObject * object);
static void gst_cmml_tag_head_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_head_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_head_value_from_string_value (const GValue * src,
    GValue * dest);

G_DEFINE_TYPE (GstCmmlTagClip, gst_cmml_tag_clip, G_TYPE_OBJECT);
static void gst_cmml_tag_clip_finalize (GObject * object);
static void gst_cmml_tag_clip_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_clip_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec);

static void gst_cmml_tag_clip_value_from_string_value (const GValue * src,
    GValue * dest);

static void set_object_on_value (GObject * object, GValue * dest);

static const gchar default_preamble[] =
    "<?xml version=\"1.0\" standalone=\"yes\"?>";

/* Stream tag */
static void
gst_cmml_tag_stream_class_init (GstCmmlTagStreamClass * stream_class)
{
  GObjectClass *klass = G_OBJECT_CLASS (stream_class);

  klass->set_property = gst_cmml_tag_stream_set_property;
  klass->get_property = gst_cmml_tag_stream_get_property;
  klass->finalize = gst_cmml_tag_stream_finalize;

  g_object_class_install_property (klass, GST_CMML_TAG_STREAM_TIMEBASE,
      g_param_spec_string ("base-time",
          "Base time",
          "Playback time (in seconds) of the first data packet",
          "0", G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_STREAM_UTC,
      g_param_spec_string ("calendar-base-time",
          "Calendar base time",
          "Date and wall-clock time (expressed as UTC time in the format "
          "YYYYMMDDTHHMMSS.sssZ) associated with the base-time",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_STREAM_IMPORTS,
      g_param_spec_value_array ("input-streams",
          "Input streams",
          "List of input streams that compose this bitstream",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_CMML_TAG_STREAM,
      gst_cmml_tag_stream_value_from_string_value);
}

static void
gst_cmml_tag_stream_init (GstCmmlTagStream * stream)
{
}

static void
gst_cmml_tag_stream_finalize (GObject * object)
{
  GstCmmlTagStream *stream = GST_CMML_TAG_STREAM (object);

  g_free (stream->timebase);
  g_free (stream->utc);
  if (stream->imports)
    g_value_array_free (stream->imports);

  if (G_OBJECT_CLASS (gst_cmml_tag_stream_parent_class)->finalize)
    G_OBJECT_CLASS (gst_cmml_tag_stream_parent_class)->finalize (object);
}

static void
gst_cmml_tag_stream_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  GstCmmlTagStream *stream = GST_CMML_TAG_STREAM (object);

  switch (property_id) {
    case GST_CMML_TAG_STREAM_TIMEBASE:
      g_free (stream->timebase);
      stream->timebase = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_STREAM_UTC:
      g_free (stream->utc);
      stream->utc = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_STREAM_IMPORTS:
    {
      GValueArray *va = g_value_get_boxed (value);

      if (stream->imports)
        g_value_array_free (stream->imports);
      stream->imports = va != NULL ? g_value_array_copy (va) : NULL;
      break;
    }
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}


static void
gst_cmml_tag_stream_value_from_string_value (const GValue * src, GValue * dest)
{
  GstCmmlParser *parser;
  const gchar *str;
  guint size;

  parser = gst_cmml_parser_new (GST_CMML_PARSER_DECODE);
  parser->user_data = dest;
  parser->stream_callback = (GstCmmlParserStreamCallback) set_object_on_value;
  gst_cmml_parser_parse_chunk (parser,
      default_preamble, strlen (default_preamble), NULL);

  str = g_value_get_string (src);
  size = strlen (str);
  gst_cmml_parser_parse_chunk (parser, str, size, NULL);

  gst_cmml_parser_free (parser);
}

static void
gst_cmml_tag_stream_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
{
  GstCmmlTagStream *stream = GST_CMML_TAG_STREAM (object);

  switch (property_id) {
    case GST_CMML_TAG_STREAM_TIMEBASE:
      g_value_set_string (value, (gchar *) stream->timebase);
      break;
    case GST_CMML_TAG_STREAM_UTC:
      g_value_set_string (value, (gchar *) stream->utc);
      break;
    case GST_CMML_TAG_STREAM_IMPORTS:
      g_value_set_boxed (value, stream->imports);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

/* Head tag */
static void
gst_cmml_tag_head_class_init (GstCmmlTagHeadClass * head_class)
{
  GObjectClass *klass = G_OBJECT_CLASS (head_class);

  klass->set_property = gst_cmml_tag_head_set_property;
  klass->get_property = gst_cmml_tag_head_get_property;
  klass->finalize = gst_cmml_tag_head_finalize;

  g_object_class_install_property (klass, GST_CMML_TAG_HEAD_TITLE,
      g_param_spec_string ("title",
          "Title",
          "Title of the bitstream",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_HEAD_BASE,
      g_param_spec_string ("base-uri",
          "Base URI",
          "Base URI of the bitstream. All relative URIs are relative to this",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_HEAD_META,
      g_param_spec_value_array ("meta",
          "Meta annotations",
          "Meta annotations for the complete Annodex bitstream",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_CMML_TAG_HEAD,
      gst_cmml_tag_head_value_from_string_value);
}

static void
gst_cmml_tag_head_init (GstCmmlTagHead * head)
{
}

static void
gst_cmml_tag_head_finalize (GObject * object)
{
  GstCmmlTagHead *head = GST_CMML_TAG_HEAD (object);

  g_free (head->title);
  g_free (head->base);
  if (head->meta)
    g_value_array_free (head->meta);

  if (G_OBJECT_CLASS (gst_cmml_tag_head_parent_class)->finalize)
    G_OBJECT_CLASS (gst_cmml_tag_head_parent_class)->finalize (object);
}

static void
gst_cmml_tag_head_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  GstCmmlTagHead *head = GST_CMML_TAG_HEAD (object);

  switch (property_id) {
    case GST_CMML_TAG_HEAD_TITLE:
      g_free (head->title);
      head->title = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_HEAD_BASE:
      g_free (head->base);
      head->base = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_HEAD_META:
    {
      GValueArray *va = g_value_get_boxed (value);

      if (head->meta)
        g_value_array_free (head->meta);
      head->meta = va != NULL ? g_value_array_copy (va) : NULL;
      break;
    }
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
gst_cmml_tag_head_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
{
  GstCmmlTagHead *head = GST_CMML_TAG_HEAD (object);

  switch (property_id) {
    case GST_CMML_TAG_HEAD_TITLE:
      g_value_set_string (value, (gchar *) head->title);
      break;
    case GST_CMML_TAG_HEAD_BASE:
      g_value_set_string (value, (gchar *) head->base);
      break;
    case GST_CMML_TAG_HEAD_META:
      g_value_set_boxed (value, head->meta);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
gst_cmml_tag_head_value_from_string_value (const GValue * src, GValue * dest)
{
  GstCmmlParser *parser;
  const gchar *str;
  guint size;

  parser = gst_cmml_parser_new (GST_CMML_PARSER_DECODE);
  parser->user_data = dest;
  parser->head_callback = (GstCmmlParserHeadCallback) set_object_on_value;
  gst_cmml_parser_parse_chunk (parser,
      default_preamble, strlen (default_preamble), NULL);

  str = g_value_get_string (src);
  size = strlen (str);
  gst_cmml_parser_parse_chunk (parser, str, size, NULL);

  gst_cmml_parser_free (parser);
}

/* Clip tag */
static void
gst_cmml_tag_clip_class_init (GstCmmlTagClipClass * clip_class)
{
  GObjectClass *klass = G_OBJECT_CLASS (clip_class);

  klass->set_property = gst_cmml_tag_clip_set_property;
  klass->get_property = gst_cmml_tag_clip_get_property;
  klass->finalize = gst_cmml_tag_clip_finalize;

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_EMPTY,
      g_param_spec_boolean ("empty",
          "Empty clip flag",
          "An empty clip only marks the end of the previous clip",
          TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_ID,
      g_param_spec_string ("id",
          "Clip id",
          "Id of the clip", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_TRACK,
      g_param_spec_string ("track",
          "Track number",
          "The track this clip belongs to",
          "default", G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_START_TIME,
      g_param_spec_uint64 ("start-time",
          "Start time",
          "The start time (in seconds) of the clip",
          0, G_MAXUINT64, GST_CLOCK_TIME_NONE,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_END_TIME,
      g_param_spec_uint64 ("end-time",
          "End time",
          "The end time (in seconds) of the clip (only set if extract-mode=true)",
          0, G_MAXUINT64, GST_CLOCK_TIME_NONE,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_ANCHOR_HREF,
      g_param_spec_string ("anchor-uri",
          "Anchor URI",
          "The location of a Web resource closely connected to the clip",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_ANCHOR_TEXT,
      g_param_spec_string ("anchor-text",
          "Anchor text",
          "A short description of the resource pointed by anchor-uri",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_IMG_SRC,
      g_param_spec_string ("img-uri",
          "Image URI",
          "The URI of a representative image for the clip",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_IMG_ALT,
      g_param_spec_string ("img-alt",
          "Image alternative text",
          "Alternative text to be displayed instead of the image "
          "specified in img-uri", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_DESC_TEXT,
      g_param_spec_string ("description",
          "Description",
          "A textual description of the content of the clip",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property (klass, GST_CMML_TAG_CLIP_META,
      g_param_spec_value_array ("meta",
          "Meta annotations",
          "Meta annotations for the clip",
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_CMML_TAG_CLIP,
      gst_cmml_tag_clip_value_from_string_value);
}

static void
gst_cmml_tag_clip_init (GstCmmlTagClip * clip)
{
}

static void
gst_cmml_tag_clip_finalize (GObject * object)
{
  GstCmmlTagClip *clip = GST_CMML_TAG_CLIP (object);

  g_free (clip->id);
  g_free (clip->track);
  g_free (clip->anchor_href);
  g_free (clip->anchor_text);
  g_free (clip->img_src);
  g_free (clip->img_alt);
  g_free (clip->desc_text);
  if (clip->meta)
    g_value_array_free (clip->meta);

  if (G_OBJECT_CLASS (gst_cmml_tag_clip_parent_class)->finalize)
    G_OBJECT_CLASS (gst_cmml_tag_clip_parent_class)->finalize (object);
}

static void
gst_cmml_tag_clip_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  GstCmmlTagClip *clip = GST_CMML_TAG_CLIP (object);

  switch (property_id) {
    case GST_CMML_TAG_CLIP_EMPTY:
      clip->empty = g_value_get_boolean (value);
      break;
    case GST_CMML_TAG_CLIP_ID:
      g_free (clip->id);
      clip->id = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_CLIP_TRACK:
      g_free (clip->track);
      clip->track = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_CLIP_START_TIME:
      clip->start_time = g_value_get_uint64 (value);
      break;
    case GST_CMML_TAG_CLIP_END_TIME:
      clip->end_time = g_value_get_uint64 (value);
      break;
    case GST_CMML_TAG_CLIP_ANCHOR_HREF:
      g_free (clip->anchor_href);
      clip->anchor_href = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_CLIP_ANCHOR_TEXT:
      g_free (clip->anchor_text);
      clip->anchor_text = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_CLIP_IMG_SRC:
      g_free (clip->img_src);
      clip->img_src = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_CLIP_IMG_ALT:
      g_free (clip->img_alt);
      clip->img_alt = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_CLIP_DESC_TEXT:
      g_free (clip->desc_text);
      clip->desc_text = (guchar *) g_value_dup_string (value);
      break;
    case GST_CMML_TAG_CLIP_META:
    {
      GValueArray *va = (GValueArray *) g_value_get_boxed (value);

      if (clip->meta)
        g_value_array_free (clip->meta);

      clip->meta = va != NULL ? g_value_array_copy (va) : NULL;

      break;
    }
  }
}

static void
gst_cmml_tag_clip_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
{
  GstCmmlTagClip *clip = GST_CMML_TAG_CLIP (object);

  switch (property_id) {
    case GST_CMML_TAG_CLIP_EMPTY:
      g_value_set_boolean (value, clip->empty);
      break;
    case GST_CMML_TAG_CLIP_ID:
      g_value_set_string (value, (gchar *) clip->id);
      break;
    case GST_CMML_TAG_CLIP_TRACK:
      g_value_set_string (value, (gchar *) clip->track);
      break;
    case GST_CMML_TAG_CLIP_START_TIME:
      g_value_set_uint64 (value, clip->start_time);
      break;
    case GST_CMML_TAG_CLIP_END_TIME:
      g_value_set_uint64 (value, clip->end_time);
      break;
    case GST_CMML_TAG_CLIP_ANCHOR_HREF:
      g_value_set_string (value, (gchar *) clip->anchor_href);
      break;
    case GST_CMML_TAG_CLIP_ANCHOR_TEXT:
      g_value_set_string (value, (gchar *) clip->anchor_text);
      break;
    case GST_CMML_TAG_CLIP_IMG_SRC:
      g_value_set_string (value, (gchar *) clip->img_src);
      break;
    case GST_CMML_TAG_CLIP_IMG_ALT:
      g_value_set_string (value, (gchar *) clip->img_alt);
      break;
    case GST_CMML_TAG_CLIP_DESC_TEXT:
      g_value_set_string (value, (gchar *) clip->desc_text);
      break;
    case GST_CMML_TAG_CLIP_META:
      g_value_set_boxed (value, clip->meta);
      break;
  }
}

static void
gst_cmml_tag_clip_value_from_string_value (const GValue * src, GValue * dest)
{
  GstCmmlParser *parser;
  const gchar *str;
  guint size;

  parser = gst_cmml_parser_new (GST_CMML_PARSER_DECODE);
  parser->user_data = dest;
  parser->clip_callback = (GstCmmlParserClipCallback) set_object_on_value;

  gst_cmml_parser_parse_chunk (parser, default_preamble,
      strlen (default_preamble), NULL);

  str = g_value_get_string (src);
  size = strlen (str);

  gst_cmml_parser_parse_chunk (parser, str, size, NULL);

  gst_cmml_parser_free (parser);
}

static void
set_object_on_value (GObject * tag, GValue * dest)
{
  g_value_take_object (dest, tag);
}