<commit_before>/* GStreamer Editing Services
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <ges/ges.h>
#include <gst/profile/gstprofile.h>
#include <regex.h>

/* GLOBAL VARIABLE */
static guint repeat = 0;
GESTimelinePipeline *pipeline = NULL;
static gboolean seenerrors = FALSE;

gboolean pattern_source_fill_func (GESTimelineObject * object, GESTrackObject
    * trobject, GstElement * gnlobj, gpointer user_data);

void print_transition_list (void);

gboolean check_path (char *path);

GESTimelineObject *pattern_source_new (guint pattern);

gboolean check_time (char *time);

guint64 str_to_time (char *time);

void print_pattern_list (void);

gboolean thumbnail_cb (gpointer pipeline);

gboolean
thumbnail_cb (gpointer pipeline)
{
  static int i = 0;
  GESTimelinePipeline *p = (GESTimelinePipeline *) pipeline;
  gchar *filename;
  gboolean res;

  filename = g_strdup_printf ("thumbnail%d.jpg", i++);

  res = ges_timeline_pipeline_save_thumbnail (p, -1, -1,
      (gchar *) "image/jpeg", filename);

  g_free (filename);

  return res;
}

gboolean
pattern_source_fill_func (GESTimelineObject * object,
    GESTrackObject * trobject, GstElement * gnlobj, gpointer user_data)
{
  guint pattern = GPOINTER_TO_UINT (user_data);
  GESTrack *track = trobject->track;
  GstElement *testsrc;

  g_assert (track);

  if ((track->type) == GES_TRACK_TYPE_VIDEO) {
    testsrc = gst_element_factory_make ("videotestsrc", NULL);
    g_object_set (testsrc, "pattern", pattern, NULL);
  } else if ((track->type) == GES_TRACK_TYPE_AUDIO) {
    testsrc = gst_element_factory_make ("audiotestsrc", NULL);
    g_object_set (testsrc, "volume", (gdouble) 0, NULL);
  } else
    return FALSE;

  return gst_bin_add (GST_BIN (gnlobj), testsrc);
}

GESTimelineObject *
pattern_source_new (guint pattern)
{
  GESTimelineObject *ret;
  ret = GES_TIMELINE_OBJECT (ges_timeline_test_source_new ());
  g_object_set (ret, "vpattern", pattern, NULL);
  return ret;
}

gboolean
check_path (char *path)
{
  FILE *fp = fopen (path, "r");
  if (fp) {
    fclose (fp);
    return TRUE;
  }

  return FALSE;
}

gboolean
check_time (char *time)
{
  static regex_t re;
  static gboolean compiled = FALSE;

  if (!compiled) {
    compiled = TRUE;
    regcomp (&re, "^[0-9]+(.[0-9]+)?$", REG_EXTENDED | REG_NOSUB);
  }

  if (!regexec (&re, time, (size_t) 0, NULL, 0))
    return TRUE;
  return FALSE;
}

guint64
str_to_time (char *time)
{
  if (check_time (time)) {
    return (guint64) (atof (time) * GST_SECOND);
  }
  g_error ("%s not a valid time", time);
  return 0;
}

static GstEncodingProfile *
make_encoding_profile (gchar * audio, gchar * video, gchar * video_restriction,
    gchar * container)
{
  GstEncodingProfile *profile;
  GstStreamEncodingProfile *stream;
  GstCaps *caps;

  caps = gst_caps_from_string (container);
  profile = gst_encoding_profile_new ((gchar *) "ges-test4", caps, NULL, FALSE);
  gst_caps_unref (caps);

  caps = gst_caps_from_string (audio);
  stream =
      gst_stream_encoding_profile_new (GST_ENCODING_PROFILE_AUDIO,
      caps, NULL, NULL, 0);
  gst_encoding_profile_add_stream (profile, stream);
  gst_caps_unref (caps);

  caps = gst_caps_from_string (video);
  stream = gst_stream_encoding_profile_new (GST_ENCODING_PROFILE_VIDEO,
      gst_caps_from_string (video),
      NULL, gst_caps_from_string (video_restriction), 0);
  gst_encoding_profile_add_stream (profile, stream);
  gst_caps_unref (caps);

  return profile;
}

static GESTimelinePipeline *
create_timeline (int nbargs, gchar ** argv)
{
  GESTimelinePipeline *pipeline;
  GESTimelineLayer *layer;
  GESTrack *tracka, *trackv;
  GESTimeline *timeline;
  guint i;

  timeline = ges_timeline_new ();

  tracka = ges_track_audio_raw_new ();
  trackv = ges_track_video_raw_new ();

  /* We are only going to be doing one layer of timeline objects */
  layer = (GESTimelineLayer *) ges_simple_timeline_layer_new ();

  /* Add the tracks and the layer to the timeline */
  if (!ges_timeline_add_layer (timeline, layer) ||
      !ges_timeline_add_track (timeline, tracka) ||
      !ges_timeline_add_track (timeline, trackv))
    return NULL;

  /* Here we've finished initializing our timeline, we're 
   * ready to start using it... by solely working with the layer !*/

  for (i = 0; i < nbargs / 3; i++) {
    GESTimelineObject *obj;

    char *source = argv[i * 3];
    char *arg0 = argv[(i * 3) + 1];
    guint64 duration = str_to_time (argv[(i * 3) + 2]);

    if (!g_strcmp0 ("+pattern", source)) {
      obj = GES_TIMELINE_OBJECT (ges_timeline_test_source_new_for_nick (arg0));
      if (!obj)
        g_error ("%s invalid pattern!", arg0);

      g_object_set (G_OBJECT (obj), "duration", duration, NULL);

      g_printerr ("Adding <pattern:%s> duration %" GST_TIME_FORMAT "\n",
          arg0, GST_TIME_ARGS (duration));
    }

    else if (!g_strcmp0 ("+transition", source)) {
      obj = GES_TIMELINE_OBJECT (ges_timeline_transition_new_for_nick (arg0));

      if (!obj)
        g_error ("invalid transition type\n");

      if (duration <= 0)
        g_error ("durations must be greater than 0");

      g_object_set (G_OBJECT (obj), "duration", duration, NULL);

      g_printerr ("Adding <transition:%s> duration %" GST_TIME_FORMAT "\n",
          arg0, GST_TIME_ARGS (duration));

    }

    else if (!g_strcmp0 ("+title", source)) {
      obj = GES_TIMELINE_OBJECT (ges_timeline_title_source_new ());

      g_object_set (obj, "duration", duration, "text", arg0, NULL);

      g_printerr ("Adding <title:%s> duration %" GST_TIME_FORMAT "\n",
          arg0, GST_TIME_ARGS (duration));
    }

    else {
      gchar *uri;
      guint64 inpoint;

      if (!check_path (source))
        g_error ("'%s': could not open path!", source);

      uri = g_strdup_printf ("file://%s", source);
      inpoint = str_to_time (argv[i * 3 + 1]);

      obj = GES_TIMELINE_OBJECT (ges_timeline_filesource_new (uri));
      g_object_set (obj,
          "in-point", (guint64) inpoint, "duration", (guint64) duration, NULL);

      g_printerr ("Adding %s inpoint:%" GST_TIME_FORMAT " duration:%"
          GST_TIME_FORMAT "\n", uri, GST_TIME_ARGS (inpoint),
          GST_TIME_ARGS (duration));

      g_free (uri);

    }

    g_assert (obj);

    /* Since we're using a GESSimpleTimelineLayer, objects will be automatically
     * appended to the end of the layer */
    ges_timeline_layer_add_object (layer, obj);
  }

  /* In order to view our timeline, let's grab a convenience pipeline to put
   * our timeline in. */
  pipeline = ges_timeline_pipeline_new ();

  /* Add the timeline to that pipeline */
  if (!ges_timeline_pipeline_add_timeline (pipeline, timeline))
    return NULL;

  return pipeline;
}

static void
bus_message_cb (GstBus * bus, GstMessage * message, GMainLoop * mainloop)
{
  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR:
      g_printerr ("ERROR\n");
      seenerrors = TRUE;
      g_main_loop_quit (mainloop);
      break;
    case GST_MESSAGE_EOS:
      if (repeat > 0) {
        g_printerr ("Looping again\n");
        if (!gst_element_seek_simple (GST_ELEMENT (pipeline), GST_FORMAT_TIME,
                GST_SEEK_FLAG_FLUSH, 0))
          g_printerr ("seeking failed\n");
        else
          g_printerr ("seeking succeeded\n");
        gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
        g_printerr ("Looping set\n");
        repeat -= 1;
      } else {
        g_printerr ("Done\n");
        g_main_loop_quit (mainloop);
      }
      break;
    default:
      break;
  }
}

void
print_transition_list (void)
{
  GEnumClass *smpte_class;
  GESTimelineTransition *tr;
  GESTimelineTransitionClass *klass;
  GParamSpec *pspec;
  GEnumValue *v;

  tr = ges_timeline_transition_new (GES_VIDEO_TRANSITION_TYPE_CROSSFADE);
  klass = GES_TIMELINE_TRANSITION_GET_CLASS (tr);

  pspec = g_object_class_find_property (G_OBJECT_CLASS (klass), "vtype");

  smpte_class = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));

  for (v = smpte_class->values; v->value != 0; v++) {
    g_print ("%s\n", v->value_nick);
  }

  g_type_class_unref (smpte_class);
  g_object_unref (tr);
}

void
print_pattern_list (void)
{
  GEnumClass *enum_class;
  GESTimelineTestSource *tr;
  GESTimelineTestSourceClass *klass;
  GParamSpec *pspec;
  GEnumValue *v;

  tr = ges_timeline_test_source_new ();
  klass = GES_TIMELINE_TEST_SOURCE_GET_CLASS (tr);

  pspec = g_object_class_find_property (G_OBJECT_CLASS (klass), "vpattern");

  enum_class = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));

  for (v = enum_class->values; v->value_nick != NULL; v++) {
    g_print ("%s\n", v->value_nick);
  }

  g_type_class_unref (enum_class);
  g_object_unref (tr);

}

int
main (int argc, gchar ** argv)
{
  GError *err = NULL;
  gchar *outputuri = NULL;
  gchar *container = (gchar *) "application/ogg";
  gchar *audio = (gchar *) "audio/x-vorbis";
  gchar *video = (gchar *) "video/x-theora";
  gchar *video_restriction = (gchar *) "ANY";
  static gboolean render = FALSE;
  static gboolean smartrender = FALSE;<commit_msg>ges-launch: Gracefully handle missing properties<commit_after>/* GStreamer Editing Services
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <ges/ges.h>
#include <gst/profile/gstprofile.h>
#include <regex.h>

/* GLOBAL VARIABLE */
static guint repeat = 0;
GESTimelinePipeline *pipeline = NULL;
static gboolean seenerrors = FALSE;

gboolean pattern_source_fill_func (GESTimelineObject * object, GESTrackObject
    * trobject, GstElement * gnlobj, gpointer user_data);

void print_transition_list (void);

gboolean check_path (char *path);

GESTimelineObject *pattern_source_new (guint pattern);

gboolean check_time (char *time);

guint64 str_to_time (char *time);

void print_pattern_list (void);

gboolean thumbnail_cb (gpointer pipeline);

gboolean
thumbnail_cb (gpointer pipeline)
{
  static int i = 0;
  GESTimelinePipeline *p = (GESTimelinePipeline *) pipeline;
  gchar *filename;
  gboolean res;

  filename = g_strdup_printf ("thumbnail%d.jpg", i++);

  res = ges_timeline_pipeline_save_thumbnail (p, -1, -1,
      (gchar *) "image/jpeg", filename);

  g_free (filename);

  return res;
}

gboolean
pattern_source_fill_func (GESTimelineObject * object,
    GESTrackObject * trobject, GstElement * gnlobj, gpointer user_data)
{
  guint pattern = GPOINTER_TO_UINT (user_data);
  GESTrack *track = trobject->track;
  GstElement *testsrc;

  g_assert (track);

  if ((track->type) == GES_TRACK_TYPE_VIDEO) {
    testsrc = gst_element_factory_make ("videotestsrc", NULL);
    g_object_set (testsrc, "pattern", pattern, NULL);
  } else if ((track->type) == GES_TRACK_TYPE_AUDIO) {
    testsrc = gst_element_factory_make ("audiotestsrc", NULL);
    g_object_set (testsrc, "volume", (gdouble) 0, NULL);
  } else
    return FALSE;

  return gst_bin_add (GST_BIN (gnlobj), testsrc);
}

GESTimelineObject *
pattern_source_new (guint pattern)
{
  GESTimelineObject *ret;
  ret = GES_TIMELINE_OBJECT (ges_timeline_test_source_new ());
  g_object_set (ret, "vpattern", pattern, NULL);
  return ret;
}

gboolean
check_path (char *path)
{
  FILE *fp = fopen (path, "r");
  if (fp) {
    fclose (fp);
    return TRUE;
  }

  return FALSE;
}

gboolean
check_time (char *time)
{
  static regex_t re;
  static gboolean compiled = FALSE;

  if (!compiled) {
    compiled = TRUE;
    regcomp (&re, "^[0-9]+(.[0-9]+)?$", REG_EXTENDED | REG_NOSUB);
  }

  if (!regexec (&re, time, (size_t) 0, NULL, 0))
    return TRUE;
  return FALSE;
}

guint64
str_to_time (char *time)
{
  if (check_time (time)) {
    return (guint64) (atof (time) * GST_SECOND);
  }
  g_error ("%s not a valid time", time);
  return 0;
}

static GstEncodingProfile *
make_encoding_profile (gchar * audio, gchar * video, gchar * video_restriction,
    gchar * container)
{
  GstEncodingProfile *profile;
  GstStreamEncodingProfile *stream;
  GstCaps *caps;

  caps = gst_caps_from_string (container);
  profile = gst_encoding_profile_new ((gchar *) "ges-test4", caps, NULL, FALSE);
  gst_caps_unref (caps);

  caps = gst_caps_from_string (audio);
  stream =
      gst_stream_encoding_profile_new (GST_ENCODING_PROFILE_AUDIO,
      caps, NULL, NULL, 0);
  gst_encoding_profile_add_stream (profile, stream);
  gst_caps_unref (caps);

  caps = gst_caps_from_string (video);
  stream = gst_stream_encoding_profile_new (GST_ENCODING_PROFILE_VIDEO,
      gst_caps_from_string (video),
      NULL, gst_caps_from_string (video_restriction), 0);
  gst_encoding_profile_add_stream (profile, stream);
  gst_caps_unref (caps);

  return profile;
}

static GESTimelinePipeline *
create_timeline (int nbargs, gchar ** argv)
{
  GESTimelinePipeline *pipeline;
  GESTimelineLayer *layer;
  GESTrack *tracka, *trackv;
  GESTimeline *timeline;
  guint i;

  timeline = ges_timeline_new ();

  tracka = ges_track_audio_raw_new ();
  trackv = ges_track_video_raw_new ();

  /* We are only going to be doing one layer of timeline objects */
  layer = (GESTimelineLayer *) ges_simple_timeline_layer_new ();

  /* Add the tracks and the layer to the timeline */
  if (!ges_timeline_add_layer (timeline, layer) ||
      !ges_timeline_add_track (timeline, tracka) ||
      !ges_timeline_add_track (timeline, trackv))
    return NULL;

  /* Here we've finished initializing our timeline, we're 
   * ready to start using it... by solely working with the layer !*/

  for (i = 0; i < nbargs / 3; i++) {
    GESTimelineObject *obj;

    char *source = argv[i * 3];
    char *arg0 = argv[(i * 3) + 1];
    guint64 duration = str_to_time (argv[(i * 3) + 2]);

    if (!g_strcmp0 ("+pattern", source)) {
      obj = GES_TIMELINE_OBJECT (ges_timeline_test_source_new_for_nick (arg0));
      if (!obj)
        g_error ("%s invalid pattern!", arg0);

      g_object_set (G_OBJECT (obj), "duration", duration, NULL);

      g_printerr ("Adding <pattern:%s> duration %" GST_TIME_FORMAT "\n",
          arg0, GST_TIME_ARGS (duration));
    }

    else if (!g_strcmp0 ("+transition", source)) {
      obj = GES_TIMELINE_OBJECT (ges_timeline_transition_new_for_nick (arg0));

      if (!obj)
        g_error ("invalid transition type\n");

      if (duration <= 0)
        g_error ("durations must be greater than 0");

      g_object_set (G_OBJECT (obj), "duration", duration, NULL);

      g_printerr ("Adding <transition:%s> duration %" GST_TIME_FORMAT "\n",
          arg0, GST_TIME_ARGS (duration));

    }

    else if (!g_strcmp0 ("+title", source)) {
      obj = GES_TIMELINE_OBJECT (ges_timeline_title_source_new ());

      g_object_set (obj, "duration", duration, "text", arg0, NULL);

      g_printerr ("Adding <title:%s> duration %" GST_TIME_FORMAT "\n",
          arg0, GST_TIME_ARGS (duration));
    }

    else {
      gchar *uri;
      guint64 inpoint;

      if (!check_path (source))
        g_error ("'%s': could not open path!", source);

      uri = g_strdup_printf ("file://%s", source);
      inpoint = str_to_time (argv[i * 3 + 1]);

      obj = GES_TIMELINE_OBJECT (ges_timeline_filesource_new (uri));
      g_object_set (obj,
          "in-point", (guint64) inpoint, "duration", (guint64) duration, NULL);

      g_printerr ("Adding %s inpoint:%" GST_TIME_FORMAT " duration:%"
          GST_TIME_FORMAT "\n", uri, GST_TIME_ARGS (inpoint),
          GST_TIME_ARGS (duration));

      g_free (uri);

    }

    g_assert (obj);

    /* Since we're using a GESSimpleTimelineLayer, objects will be automatically
     * appended to the end of the layer */
    ges_timeline_layer_add_object (layer, obj);
  }

  /* In order to view our timeline, let's grab a convenience pipeline to put
   * our timeline in. */
  pipeline = ges_timeline_pipeline_new ();

  /* Add the timeline to that pipeline */
  if (!ges_timeline_pipeline_add_timeline (pipeline, timeline))
    return NULL;

  return pipeline;
}

static void
bus_message_cb (GstBus * bus, GstMessage * message, GMainLoop * mainloop)
{
  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR:
      g_printerr ("ERROR\n");
      seenerrors = TRUE;
      g_main_loop_quit (mainloop);
      break;
    case GST_MESSAGE_EOS:
      if (repeat > 0) {
        g_printerr ("Looping again\n");
        if (!gst_element_seek_simple (GST_ELEMENT (pipeline), GST_FORMAT_TIME,
                GST_SEEK_FLAG_FLUSH, 0))
          g_printerr ("seeking failed\n");
        else
          g_printerr ("seeking succeeded\n");
        gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
        g_printerr ("Looping set\n");
        repeat -= 1;
      } else {
        g_printerr ("Done\n");
        g_main_loop_quit (mainloop);
      }
      break;
    default:
      break;
  }
}

void
print_transition_list (void)
{
  GEnumClass *smpte_class;
  GESTimelineTransition *tr;
  GESTimelineTransitionClass *klass;
  GParamSpec *pspec;
  GEnumValue *v;

  tr = ges_timeline_transition_new (GES_VIDEO_TRANSITION_TYPE_CROSSFADE);
  klass = GES_TIMELINE_TRANSITION_GET_CLASS (tr);

  if ((pspec = g_object_class_find_property (G_OBJECT_CLASS (klass), "vtype"))) {

    smpte_class = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));

    for (v = smpte_class->values; v->value != 0; v++) {
      g_print ("%s\n", v->value_nick);
    }

    g_type_class_unref (smpte_class);
  }

  g_object_unref (tr);
}

void
print_pattern_list (void)
{
  GEnumClass *enum_class;
  GESTimelineTestSource *tr;
  GESTimelineTestSourceClass *klass;
  GParamSpec *pspec;
  GEnumValue *v;

  tr = ges_timeline_test_source_new ();
  klass = GES_TIMELINE_TEST_SOURCE_GET_CLASS (tr);

  if ((pspec =
          g_object_class_find_property (G_OBJECT_CLASS (klass), "vpattern"))) {

    enum_class = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));

    for (v = enum_class->values; v->value_nick != NULL; v++) {
      g_print ("%s\n", v->value_nick);
    }

    g_type_class_unref (enum_class);
  }
  g_object_unref (tr);

}

int
main (int argc, gchar ** argv)
{
  GError *err = NULL;
  gchar *outputuri = NULL;
  gchar *container = (gchar *) "application/ogg";
  gchar *audio = (gchar *) "audio/x-vorbis";
  gchar *video = (gchar *) "video/x-theora";
  gchar *video_restriction = (gchar *) "ANY";
  static gboolean render = FALSE;
  static gboolean smartrender = FALSE;
{'fraction_non_alphanumeric': 0.11654964894684053, 'fraction_numerical': 0.007422266800401204, 'mean_word_length': 3.7740004788125447, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2804188', 'n_tokens_mistral': 7225, 'n_tokens_neox': 6969, 'n_words': 1819}