#include<gst/gst.h>
#include <gst/pbutils/pbutils.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>

void print_duration(GstDiscoverer *disc, char *buf) {
  GError *err;
  GstDiscovererInfo *info;
  info = gst_discoverer_discover_uri(disc, buf, &err);
  printf("Duration is: %ld\n", gst_discoverer_info_get_duration(info)/GST_SECOND);
  g_object_unref(info);
}

int main(int argc, char **argv) {
  GstDiscoverer *disc;
  char buf[1024];
  char path[1024];
  GError *err = NULL;
  int fd;
  gst_init(&argc, &argv);
  if(argc != 2) {
    printf("%s <input file>\n", argv[1]);
    return 1;
  }

  disc = gst_discoverer_new(GST_SECOND, &err);
  if(argv[1][0] != '/') {
    getcwd(buf, 1024);
    snprintf(path, 1024, "%s/%s", buf, argv[1]);
  } else {
    snprintf(path, 1024, "%s", argv[1]);
  }
  /* This works */
  snprintf(buf, 1024, "file://%s", path);
  print_duration(disc, buf);

  fd = open(path, O_RDONLY);
  /* This works too */
  snprintf(buf, 1024, "file:///proc/self/fd/%d", fd);
  print_duration(disc, buf);

  /* This does not work */
  snprintf(buf, 1024, "fd://%d", fd);
  print_duration(disc, buf);

  g_object_unref(G_OBJECT(disc));
  return 0;
}
