<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 <stdlib.h>
#include <string.h>
#include <glib.h>
#include <ges/ges.h>
#include <gst/profile/gstprofile.h>

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

/* create a table of a subset of the test patterns available from videotestsrc
 */

typedef struct _pattern
{
  char *name;
  int value;
} pattern;

pattern patterns[] = {
  {"smpte", 0},
  {"snow", 1},
  {"black", 2},
  {"white", 3},
  {"red", 4},
  {"green", 5},
  {"blue", 6},
};

#define N_PATTERNS 7
#define INVALID_PATTERN -1

/* and a function to get the pattern for the source */

int
pattern_for_name (char *name)
{
  size_t length = strlen (name);

  if (g_str_has_prefix (name, "<pattern:") && (name[length - 1] == '>')) {
    int i;

    /* FIXME: just use a regex for god's sake... */
    size_t substrlen = length - 10;
    char *tmp = strdup (name + 9);

    tmp[substrlen] = '\0';

    for (i = 0; i < N_PATTERNS; i++) {
      pattern *p = &patterns[i];

      if (!g_strcmp0 (p->name, tmp)) {
        return patterns[i].value;
      }
    }

    free (tmp);
  }

  return INVALID_PATTERN;
}

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, "freq", 440.0, NULL);
  } else
    return FALSE;

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

GESCustomTimelineSource *
pattern_source_new (guint pattern)
{
  return ges_custom_timeline_source_new (pattern_source_fill_func,
      GUINT_TO_POINTER (pattern));
}

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

  profile = gst_encoding_profile_new ("ges-test4",
      gst_caps_from_string (container), NULL, FALSE);
  stream =
      gst_stream_encoding_profile_new (GST_ENCODING_PROFILE_AUDIO,
      gst_caps_from_string (audio), NULL, NULL, 0);
  gst_encoding_profile_add_stream (profile, stream);
  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);
  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++) {
    gchar *uri = g_strdup_printf ("file://%s", argv[i * 3]);
    GESTimelineSource *src;
    guint64 inpoint = atoi (argv[i * 3 + 1]) * GST_SECOND;
    guint64 duration = atoi (argv[i * 3 + 2]) * GST_SECOND;

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

    int pattern;
    if ((pattern = pattern_for_name (argv[i * 3])) != INVALID_PATTERN) {
      src = GES_TIMELINE_SOURCE (pattern_source_new (pattern));
      g_assert (src);
    } else {
      gchar *uri = g_strdup_printf ("file://%s", argv[i * 3]);
      src = GES_TIMELINE_SOURCE (ges_timeline_filesource_new (uri));
      g_assert (src);
      g_free (uri);

    }
    g_object_set (src,
        "in-point", (long) (atof (argv[i * 3 + 1]) * GST_SECOND),
        "duration", (long) (atof (argv[i * 3 + 2]) * GST_SECOND), NULL);
    /* Since we're using a GESSimpleTimelineLayer, objects will be automatically
     * appended to the end of the layer */
    ges_timeline_layer_add_object (layer, (GESTimelineObject *) src);
  }

  /* 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;
<commit_msg>tests/examples/playlist.c: re-work pattern command line syntax<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 <stdlib.h>
#include <string.h>
#include <glib.h>
#include <ges/ges.h>
#include <gst/profile/gstprofile.h>

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

/* create a table of a subset of the test patterns available from videotestsrc
 */

typedef struct _pattern
{
  char *name;
  int value;
} pattern;

pattern patterns[] = {
  {"smpte", 0},
  {"snow", 1},
  {"black", 2},
  {"white", 3},
  {"red", 4},
  {"green", 5},
  {"blue", 6},
};

#define N_PATTERNS 7
#define INVALID_PATTERN -1

/* and a function to get the pattern for the source */

int
pattern_for_name (char *name)
{
  int i;

  for (i = 0; i < N_PATTERNS; i++) {
    if (!g_strcmp0 (patterns[i].name, name)) {
      return patterns[i].value;
    }
  }

  return INVALID_PATTERN;
}

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, "freq", 440.0, NULL);
  } else
    return FALSE;

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

GESCustomTimelineSource *
pattern_source_new (guint pattern)
{
  return ges_custom_timeline_source_new (pattern_source_fill_func,
      GUINT_TO_POINTER (pattern));
}

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

  profile = gst_encoding_profile_new ("ges-test4",
      gst_caps_from_string (container), NULL, FALSE);
  stream =
      gst_stream_encoding_profile_new (GST_ENCODING_PROFILE_AUDIO,
      gst_caps_from_string (audio), NULL, NULL, 0);
  gst_encoding_profile_add_stream (profile, stream);
  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);
  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++) {
    gchar *uri = g_strdup_printf ("file://%s", argv[i * 3]);
    GESTimelineSource *src;


    int pattern;
    char *source = argv[i * 3];
    char *arg0 = argv[(i * 3) + 1];
    guint64 duration = (guint64) (atof (argv[(i * 3) + 2]) * GST_SECOND);

    if (!g_strcmp0 ("+pattern", source)) {
      pattern = pattern_for_name (arg0);
      if (pattern == INVALID_PATTERN)
        g_error ("%d invalid pattern!", pattern);

      src = GES_TIMELINE_SOURCE (pattern_source_new (pattern));
      g_object_set (G_OBJECT (src), "duration", duration, NULL);

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

    else {
      gchar *uri = g_strdup_printf ("file://%s", source);
      guint64 inpoint = atoi (argv[i * 3 + 1]) * GST_SECOND;
      src = GES_TIMELINE_SOURCE (ges_timeline_filesource_new (uri));
      g_object_set (src,
          "in-point", (guint64) inpoint, "duration", (guint64) duration, NULL);

      g_print ("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 (src);

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

  /* 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;

{'fraction_non_alphanumeric': 0.11385199240986717, 'fraction_numerical': 0.010522684146972572, 'mean_word_length': 3.819201995012469, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 28, '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': '4861058', 'n_tokens_mistral': 4092, 'n_tokens_neox': 3888, 'n_words': 1158}