gst-discoverer.c 16.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GStreamer
 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18 19 20 21 22 23 24 25 26 27 28 29
 */

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

#include <stdlib.h>
#include <glib.h>
#include <gst/gst.h>
#include <gst/pbutils/pbutils.h>

static gboolean async = FALSE;
30
static gboolean show_toc = FALSE;
31 32 33 34 35 36 37 38 39
static gboolean verbose = FALSE;

typedef struct
{
  GstDiscoverer *dc;
  int argc;
  char **argv;
} PrivStruct;

40 41 42 43 44 45 46 47 48 49 50 51 52
static void
my_g_string_append_printf (GString * str, int depth, const gchar * format, ...)
{
  va_list args;

  while (depth-- > 0) {
    g_string_append (str, "  ");
  }

  va_start (args, format);
  g_string_append_vprintf (str, format, args);
  va_end (args);
}
53 54 55 56 57

static gchar *
gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
    gint depth)
{
58
  GstDiscovererAudioInfo *audio_info;
59 60
  GString *s;
  gchar *tmp;
61
  const gchar *ctmp;
62 63 64 65 66 67 68 69
  int len = 400;
  const GstTagList *tags;
  GstCaps *caps;

  g_return_val_if_fail (info != NULL, NULL);

  s = g_string_sized_new (len);

70
  my_g_string_append_printf (s, depth, "Codec:\n");
71 72 73
  caps = gst_discoverer_stream_info_get_caps (info);
  tmp = gst_caps_to_string (caps);
  gst_caps_unref (caps);
74
  my_g_string_append_printf (s, depth, "  %s\n", tmp);
75 76
  g_free (tmp);

77
  my_g_string_append_printf (s, depth, "Additional info:\n");
78 79
  if (gst_discoverer_stream_info_get_misc (info)) {
    tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
80
    my_g_string_append_printf (s, depth, "  %s\n", tmp);
81 82
    g_free (tmp);
  } else {
83
    my_g_string_append_printf (s, depth, "  None\n");
84 85
  }

86 87 88
  my_g_string_append_printf (s, depth, "Stream ID: %s\n",
      gst_discoverer_stream_info_get_stream_id (info));

89
  audio_info = (GstDiscovererAudioInfo *) info;
90
  ctmp = gst_discoverer_audio_info_get_language (audio_info);
91 92 93
  my_g_string_append_printf (s, depth, "Language: %s\n",
      ctmp ? ctmp : "<unknown>");
  my_g_string_append_printf (s, depth, "Channels: %u\n",
94
      gst_discoverer_audio_info_get_channels (audio_info));
95
  my_g_string_append_printf (s, depth, "Sample rate: %u\n",
96
      gst_discoverer_audio_info_get_sample_rate (audio_info));
97
  my_g_string_append_printf (s, depth, "Depth: %u\n",
98
      gst_discoverer_audio_info_get_depth (audio_info));
99

100
  my_g_string_append_printf (s, depth, "Bitrate: %u\n",
101
      gst_discoverer_audio_info_get_bitrate (audio_info));
102
  my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
103
      gst_discoverer_audio_info_get_max_bitrate (audio_info));
104

105
  my_g_string_append_printf (s, depth, "Tags:\n");
106 107
  tags = gst_discoverer_stream_info_get_tags (info);
  if (tags) {
108
    tmp = gst_tag_list_to_string (tags);
109
    my_g_string_append_printf (s, depth, "  %s\n", tmp);
110 111
    g_free (tmp);
  } else {
112
    my_g_string_append_printf (s, depth, "  None\n");
113 114
  }
  if (verbose)
115
    my_g_string_append_printf (s, depth, "\n");
116 117 118 119 120 121 122 123

  return g_string_free (s, FALSE);
}

static gchar *
gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
    gint depth)
{
124
  GstDiscovererVideoInfo *video_info;
125 126 127 128 129 130 131 132 133 134 135
  GString *s;
  gchar *tmp;
  int len = 500;
  const GstStructure *misc;
  const GstTagList *tags;
  GstCaps *caps;

  g_return_val_if_fail (info != NULL, NULL);

  s = g_string_sized_new (len);

136
  my_g_string_append_printf (s, depth, "Codec:\n");
137 138 139
  caps = gst_discoverer_stream_info_get_caps (info);
  tmp = gst_caps_to_string (caps);
  gst_caps_unref (caps);
140
  my_g_string_append_printf (s, depth, "  %s\n", tmp);
141 142
  g_free (tmp);

143
  my_g_string_append_printf (s, depth, "Additional info:\n");
144 145 146
  misc = gst_discoverer_stream_info_get_misc (info);
  if (misc) {
    tmp = gst_structure_to_string (misc);
147
    my_g_string_append_printf (s, depth, "  %s\n", tmp);
148 149
    g_free (tmp);
  } else {
150
    my_g_string_append_printf (s, depth, "  None\n");
151 152
  }

153 154 155
  my_g_string_append_printf (s, depth, "Stream ID: %s\n",
      gst_discoverer_stream_info_get_stream_id (info));

156
  video_info = (GstDiscovererVideoInfo *) info;
157
  my_g_string_append_printf (s, depth, "Width: %u\n",
158
      gst_discoverer_video_info_get_width (video_info));
159
  my_g_string_append_printf (s, depth, "Height: %u\n",
160
      gst_discoverer_video_info_get_height (video_info));
161
  my_g_string_append_printf (s, depth, "Depth: %u\n",
162
      gst_discoverer_video_info_get_depth (video_info));
163

164
  my_g_string_append_printf (s, depth, "Frame rate: %u/%u\n",
165 166
      gst_discoverer_video_info_get_framerate_num (video_info),
      gst_discoverer_video_info_get_framerate_denom (video_info));
167

168
  my_g_string_append_printf (s, depth, "Pixel aspect ratio: %u/%u\n",
169 170
      gst_discoverer_video_info_get_par_num (video_info),
      gst_discoverer_video_info_get_par_denom (video_info));
171

172
  my_g_string_append_printf (s, depth, "Interlaced: %s\n",
173
      gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
174

175
  my_g_string_append_printf (s, depth, "Bitrate: %u\n",
176
      gst_discoverer_video_info_get_bitrate (video_info));
177
  my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
178
      gst_discoverer_video_info_get_max_bitrate (video_info));
179

180
  my_g_string_append_printf (s, depth, "Tags:\n");
181 182
  tags = gst_discoverer_stream_info_get_tags (info);
  if (tags) {
183
    tmp = gst_tag_list_to_string (tags);
184
    my_g_string_append_printf (s, depth, "  %s\n", tmp);
185 186
    g_free (tmp);
  } else {
187
    my_g_string_append_printf (s, depth, "  None\n");
188 189
  }
  if (verbose)
190
    my_g_string_append_printf (s, depth, "\n");
191 192 193 194

  return g_string_free (s, FALSE);
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
static gchar *
gst_stream_subtitle_information_to_string (GstDiscovererStreamInfo * info,
    gint depth)
{
  GstDiscovererSubtitleInfo *subtitle_info;
  GString *s;
  gchar *tmp;
  const gchar *ctmp;
  int len = 400;
  const GstTagList *tags;
  GstCaps *caps;

  g_return_val_if_fail (info != NULL, NULL);

  s = g_string_sized_new (len);

211
  my_g_string_append_printf (s, depth, "Codec:\n");
212 213 214
  caps = gst_discoverer_stream_info_get_caps (info);
  tmp = gst_caps_to_string (caps);
  gst_caps_unref (caps);
215
  my_g_string_append_printf (s, depth, "  %s\n", tmp);
216 217
  g_free (tmp);

218
  my_g_string_append_printf (s, depth, "Additional info:\n");
219 220
  if (gst_discoverer_stream_info_get_misc (info)) {
    tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
221
    my_g_string_append_printf (s, depth, "  %s\n", tmp);
222 223
    g_free (tmp);
  } else {
224
    my_g_string_append_printf (s, depth, "  None\n");
225 226
  }

227 228 229
  my_g_string_append_printf (s, depth, "Stream ID: %s\n",
      gst_discoverer_stream_info_get_stream_id (info));

230 231
  subtitle_info = (GstDiscovererSubtitleInfo *) info;
  ctmp = gst_discoverer_subtitle_info_get_language (subtitle_info);
232 233
  my_g_string_append_printf (s, depth, "Language: %s\n",
      ctmp ? ctmp : "<unknown>");
234

235
  my_g_string_append_printf (s, depth, "Tags:\n");
236 237
  tags = gst_discoverer_stream_info_get_tags (info);
  if (tags) {
238
    tmp = gst_tag_list_to_string (tags);
239
    my_g_string_append_printf (s, depth, "  %s\n", tmp);
240 241
    g_free (tmp);
  } else {
242
    my_g_string_append_printf (s, depth, "  None\n");
243 244
  }
  if (verbose)
245
    my_g_string_append_printf (s, depth, "\n");
246 247 248 249

  return g_string_free (s, FALSE);
}

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
static void
print_stream_info (GstDiscovererStreamInfo * info, void *depth)
{
  gchar *desc = NULL;
  GstCaps *caps;

  caps = gst_discoverer_stream_info_get_caps (info);

  if (caps) {
    if (gst_caps_is_fixed (caps) && !verbose)
      desc = gst_pb_utils_get_codec_description (caps);
    else
      desc = gst_caps_to_string (caps);
    gst_caps_unref (caps);
  }

  g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
      gst_discoverer_stream_info_get_stream_type_nick (info), desc);

  if (desc) {
    g_free (desc);
    desc = NULL;
  }

  if (verbose) {
    if (GST_IS_DISCOVERER_AUDIO_INFO (info))
      desc =
          gst_stream_audio_information_to_string (info,
          GPOINTER_TO_INT (depth) + 1);
    else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
      desc =
          gst_stream_video_information_to_string (info,
          GPOINTER_TO_INT (depth) + 1);
283 284 285 286
    else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
      desc =
          gst_stream_subtitle_information_to_string (info,
          GPOINTER_TO_INT (depth) + 1);
Stefan Kost's avatar
Stefan Kost committed
287 288 289 290
    if (desc) {
      g_print ("%s", desc);
      g_free (desc);
    }
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  }
}

static void
print_topology (GstDiscovererStreamInfo * info, gint depth)
{
  GstDiscovererStreamInfo *next;

  if (!info)
    return;

  print_stream_info (info, GINT_TO_POINTER (depth));

  next = gst_discoverer_stream_info_get_next (info);
  if (next) {
    print_topology (next, depth + 1);
    gst_discoverer_stream_info_unref (next);
  } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
    GList *tmp, *streams;

311 312 313
    streams =
        gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
        (info));
314 315 316 317 318 319 320 321
    for (tmp = streams; tmp; tmp = tmp->next) {
      GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
      print_topology (tmpinf, depth + 1);
    }
    gst_discoverer_stream_info_list_free (streams);
  }
}

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
static void
print_tag_foreach (const GstTagList * tags, const gchar * tag,
    gpointer user_data)
{
  GValue val = { 0, };
  gchar *str;
  gint depth = GPOINTER_TO_INT (user_data);

  gst_tag_list_copy_value (&val, tags, tag);

  if (G_VALUE_HOLDS_STRING (&val))
    str = g_value_dup_string (&val);
  else
    str = gst_value_serialize (&val);

  g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
  g_free (str);

  g_value_unset (&val);
}

#define MAX_INDENT 40

static void
print_toc_entry (gpointer data, gpointer user_data)
{
  GstTocEntry *entry = (GstTocEntry *) data;
  gint depth = GPOINTER_TO_INT (user_data);
  guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
351 352
  GstTagList *tags;
  GList *subentries;
353 354
  gint64 start, stop;

355
  gst_toc_entry_get_start_stop_times (entry, &start, &stop);
356
  g_print ("%*s%s: start: %" GST_TIME_FORMAT " stop: %" GST_TIME_FORMAT "\n",
357 358
      depth, " ",
      gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)),
359 360 361 362
      GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
  indent += 2;

  /* print tags */
363 364
  tags = gst_toc_entry_get_tags (entry);
  if (tags) {
365
    g_print ("%*sTags:\n", 2 * depth, " ");
366 367
    gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
  }
368 369

  /* loop over sub-toc entries */
370 371
  subentries = gst_toc_entry_get_sub_entries (entry);
  g_list_foreach (subentries, print_toc_entry, GUINT_TO_POINTER (indent));
372 373
}

374
static void
Stefan Kost's avatar
Stefan Kost committed
375
print_properties (GstDiscovererInfo * info, gint tab)
376
{
377
  const GstTagList *tags;
378
  const GstToc *toc;
379

Stefan Kost's avatar
Stefan Kost committed
380
  g_print ("%*sDuration: %" GST_TIME_FORMAT "\n", tab + 1, " ",
381
      GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
Stefan Kost's avatar
Stefan Kost committed
382 383
  g_print ("%*sSeekable: %s\n", tab + 1, " ",
      (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
384 385
  if ((tags = gst_discoverer_info_get_tags (info))) {
    g_print ("%*sTags: \n", tab + 1, " ");
386
    gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (tab + 2));
387
  }
388
  if (show_toc && (toc = gst_discoverer_info_get_toc (info))) {
389 390
    GList *entries;

391
    g_print ("%*sTOC: \n", tab + 1, " ");
392 393
    entries = gst_toc_get_entries (toc);
    g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (tab + 5));
394
  }
395 396 397 398 399 400
}

static void
print_info (GstDiscovererInfo * info, GError * err)
{
  GstDiscovererResult result = gst_discoverer_info_get_result (info);
401
  GstDiscovererStreamInfo *sinfo;
402 403

  g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
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
  switch (result) {
    case GST_DISCOVERER_OK:
    {
      break;
    }
    case GST_DISCOVERER_URI_INVALID:
    {
      g_print ("URI is not valid\n");
      break;
    }
    case GST_DISCOVERER_ERROR:
    {
      g_print ("An error was encountered while discovering the file\n");
      g_print (" %s\n", err->message);
      break;
    }
    case GST_DISCOVERER_TIMEOUT:
    {
      g_print ("Analyzing URI timed out\n");
      break;
    }
    case GST_DISCOVERER_BUSY:
    {
      g_print ("Discoverer was busy\n");
      break;
    }
    case GST_DISCOVERER_MISSING_PLUGINS:
    {
      g_print ("Missing plugins\n");
      if (verbose) {
        gchar *tmp =
            gst_structure_to_string (gst_discoverer_info_get_misc (info));
        g_print (" (%s)\n", tmp);
        g_free (tmp);
      }
      break;
440 441 442
    }
  }

443 444 445
  if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
    g_print ("\nTopology:\n");
    print_topology (sinfo, 1);
Stefan Kost's avatar
Stefan Kost committed
446 447
    g_print ("\nProperties:\n");
    print_properties (info, 1);
448 449 450
    gst_discoverer_stream_info_unref (sinfo);
  }

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
  g_print ("\n");
}

static void
process_file (GstDiscoverer * dc, const gchar * filename)
{
  GError *err = NULL;
  GDir *dir;
  gchar *uri, *path;
  GstDiscovererInfo *info;

  if (!gst_uri_is_valid (filename)) {
    /* Recurse into directories */
    if ((dir = g_dir_open (filename, 0, NULL))) {
      const gchar *entry;

      while ((entry = g_dir_read_name (dir))) {
        gchar *path;
        path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
        process_file (dc, path);
        g_free (path);
      }

      g_dir_close (dir);
      return;
    }

    if (!g_path_is_absolute (filename)) {
      gchar *cur_dir;

      cur_dir = g_get_current_dir ();
      path = g_build_filename (cur_dir, filename, NULL);
      g_free (cur_dir);
    } else {
      path = g_strdup (filename);
    }

    uri = g_filename_to_uri (path, NULL, &err);
    g_free (path);
    path = NULL;

    if (err) {
      g_warning ("Couldn't convert filename to URI: %s\n", err->message);
      g_error_free (err);
      return;
    }
  } else {
    uri = g_strdup (filename);
  }

  if (async == FALSE) {
    g_print ("Analyzing %s\n", uri);
    info = gst_discoverer_discover_uri (dc, uri, &err);
    print_info (info, err);
505 506
    if (err)
      g_error_free (err);
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
    gst_discoverer_info_unref (info);
  } else {
    gst_discoverer_discover_uri_async (dc, uri);
  }

  g_free (uri);
}

static void
_new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
{
  print_info (info, err);
}

static gboolean
_run_async (PrivStruct * ps)
{
  gint i;

  for (i = 1; i < ps->argc; i++)
    process_file (ps->dc, ps->argv[i]);

  return FALSE;
}

static void
533
_discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
{
  g_main_loop_quit (ml);
}

int
main (int argc, char **argv)
{
  GError *err = NULL;
  GstDiscoverer *dc;
  gint timeout = 10;
  GOptionEntry options[] = {
    {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
        "Run asynchronously", NULL},
    {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
        "Specify timeout (in seconds, default 10)", "T"},
    /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
    /*     "Seek on elements instead of pads", NULL}, */
551 552
    {"toc", 'c', 0, G_OPTION_ARG_NONE, &show_toc,
        "Output TOC (chapters and editions)", NULL},
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
        "Verbose properties", NULL},
    {NULL}
  };
  GOptionContext *ctx;

  ctx =
      g_option_context_new
      ("- discover files synchronously with GstDiscoverer");
  g_option_context_add_main_entries (ctx, options, NULL);
  g_option_context_add_group (ctx, gst_init_get_option_group ());

  if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
    g_print ("Error initializing: %s\n", err->message);
    exit (1);
  }

  g_option_context_free (ctx);

  if (argc < 2) {
    g_print ("usage: %s <uris>\n", argv[0]);
    exit (-1);
  }

  dc = gst_discoverer_new (timeout * GST_SECOND, &err);
  if (G_UNLIKELY (dc == NULL)) {
    g_print ("Error initializing: %s\n", err->message);
    exit (1);
  }

  if (async == FALSE) {
    gint i;
    for (i = 1; i < argc; i++)
      process_file (dc, argv[i]);
  } else {
    PrivStruct *ps = g_new0 (PrivStruct, 1);
    GMainLoop *ml = g_main_loop_new (NULL, FALSE);

    ps->dc = dc;
    ps->argc = argc;
    ps->argv = argv;

    /* adding uris will be started when the mainloop runs */
    g_idle_add ((GSourceFunc) _run_async, ps);

    /* connect signals */
    g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
600
    g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
601 602 603 604 605 606 607

    gst_discoverer_start (dc);
    /* run mainloop */
    g_main_loop_run (ml);

    gst_discoverer_stop (dc);
    g_free (ps);
608
    g_main_loop_unref (ml);
609 610 611 612 613
  }
  g_object_unref (dc);

  return 0;
}