<commit_before>/* GStreamer
 * Copyright (C) 2008 <NAME> <<EMAIL> at <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.
 */

#include "rtsp-media.h"

G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);

static void gst_rtsp_media_finalize (GObject * obj);

static void
gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = gst_rtsp_media_finalize;
}

static void
gst_rtsp_media_init (GstRTSPMedia * media)
{
  media->streams = g_array_new (FALSE, TRUE, sizeof (GstRTSPMediaStream *));
}

static void
gst_rtsp_media_stream_free (GstRTSPMediaStream *stream)
{
}

static void
gst_rtsp_media_finalize (GObject * obj)
{
  GstRTSPMedia *media;
  guint i;

  media = GST_RTSP_MEDIA (obj);

  g_free (media->location);
  gst_rtsp_url_free (media->url);

  for (i = 0; i < media->streams->len; i++) {
    GstRTSPMediaStream *stream;

    stream = g_array_index (media->streams, GstRTSPMediaStream *, i);

    gst_rtsp_media_stream_free (stream);
  }
  g_array_free (media->streams, TRUE);

  G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
}

/**
 * gst_rtsp_media_new:
 * @location: the URL of the media
 *
 * Create a new #GstRTSPMedia instance.
 *
 * Returns: a new #GstRTSPMedia object or %NULL when location did not contain a
 * valid or understood URL.
 */
GstRTSPMedia *
gst_rtsp_media_new (const gchar *location)
{
  GstRTSPMedia *result;
  GstRTSPUrl *url;

  url = NULL;

  if (gst_rtsp_url_parse (location, &url) != GST_RTSP_OK)
    goto invalid_url;

  result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
  result->location = g_strdup (location);
  result->url = url;

  return result;

  /* ERRORS */
invalid_url:
  {
    return NULL;
  }
}

static void
caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
{
  if (stream->caps)
    gst_caps_unref (stream->caps);
  if ((stream->caps = GST_PAD_CAPS (pad)))
    gst_caps_ref (stream->caps);
}

/**
 *
 * STREAMING CONFIGURATION
 *
 * gst_rtsp_media_prepare:
 * @media: a #GstRTSPMedia
 * @bin: the parent bin to create the elements in.
 *
 * Prepare the media object so that it creates its streams. Implementations
 * should crate the needed gstreamer elements and add them to @bin. No state
 * changes should be performed on them yet.
 *
 * One or more GstRTSPMediaStream objects should be added to @media with the
 * srcpad member set to a source pad that produces buffer of type 
 * application/x-rtp.
 *
 * Returns: %TRUE if the media could be prepared.
 */
gboolean
gst_rtsp_media_prepare (GstRTSPMedia *media, GstBin *bin)
{
  GstRTSPMediaStream *stream;
  GstElement *pay, *element;
  GstPad * pad;
  gint i;

  /* if we're already prepared we must exit */
  g_return_val_if_fail (media->prepared == FALSE, FALSE);

  g_print ("%s\n", media->url->abspath);

  if (g_str_has_prefix (media->url->abspath, "/camera")) {
    /* live */
    element = gst_parse_launch ("( "
	"v4l2src ! video/x-raw-yuv,width=352,height=288,framerate=15/1 ! "
	"queue ! videorate ! ffmpegcolorspace ! "
	"x264enc bitrate=300 ! rtph264pay name=pay0 "
	"alsasrc ! audio/x-raw-int,rate=8000 ! queue ! "
	"amrnbenc ! rtpamrpay name=pay1 "
	")", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/h264")) {
    /* transcode h264 */
    element = gst_parse_launch ("( uridecodebin "
	"uri=file:///home/cschalle/Videos/mi2.avi ! "
	"x264enc bitrate=300 ! rtph264pay name=pay0 )", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/theora")) {
    /* transcode theora */
    element = gst_parse_launch ("( uridecodebin "
  	"uri=file:///home/wim/data/mi2.avi ! "
	"theoraenc ! rtptheorapay name=pay0 )", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/macpclinux")) {
    /* theora/vorbis */
    element = gst_parse_launch ("( filesrc "
	"location=/home/cschalle/Videos/mac_pc_linux_2.ogg ! oggdemux name=d ! "
	"queue ! theoraparse ! rtptheorapay name=pay0 "
	"d. ! queue ! vorbisparse ! rtpvorbispay name=pay1 )", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/rtspproxy")) {
    /* proxy RTSP transcode */
    element = gst_parse_launch ("( uridecodebin "
	"uri=rtsp://ia300135.us.archive.org:554/0/items/uncovered_interviews/uncovered_interviews_3_256kb.mp4 ! "
	"x264enc bitrate=1800 ! rtph264pay name=pay0 )", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/httpproxy")) {
    /* proxy HTTP transcode */
    element = gst_parse_launch ("( uridecodebin "
	"uri=http://movies.apple.com/movies/fox/maxpayne/maxpayne-tlre_h480.mov name=d "
	"d. ! queue ! x264enc bitrate=1800 ! rtph264pay name=pay0 "
	"d. ! queue ! faac ! rtpmp4gpay name=pay1 )", NULL);
  }
  else
    return FALSE;

  gst_bin_add (bin, element);

  for (i = 0; ; i++) {
    gchar *name;

    name = g_strdup_printf ("pay%d", i);

    if (!(pay = gst_bin_get_by_name (GST_BIN (element), name))) {
      g_free (name);
      break;
    }
    
    /* create the stream */<commit_msg>Set the payload types for the different payloaders. Maybe this shoulde be done automatically instead.<commit_after>/* GStreamer
 * Copyright (C) 2008 <NAME> <wim.taymans at gmail.<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.
 */

#include "rtsp-media.h"

G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);

static void gst_rtsp_media_finalize (GObject * obj);

static void
gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = gst_rtsp_media_finalize;
}

static void
gst_rtsp_media_init (GstRTSPMedia * media)
{
  media->streams = g_array_new (FALSE, TRUE, sizeof (GstRTSPMediaStream *));
}

static void
gst_rtsp_media_stream_free (GstRTSPMediaStream *stream)
{
}

static void
gst_rtsp_media_finalize (GObject * obj)
{
  GstRTSPMedia *media;
  guint i;

  media = GST_RTSP_MEDIA (obj);

  g_free (media->location);
  gst_rtsp_url_free (media->url);

  for (i = 0; i < media->streams->len; i++) {
    GstRTSPMediaStream *stream;

    stream = g_array_index (media->streams, GstRTSPMediaStream *, i);

    gst_rtsp_media_stream_free (stream);
  }
  g_array_free (media->streams, TRUE);

  G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
}

/**
 * gst_rtsp_media_new:
 * @location: the URL of the media
 *
 * Create a new #GstRTSPMedia instance.
 *
 * Returns: a new #GstRTSPMedia object or %NULL when location did not contain a
 * valid or understood URL.
 */
GstRTSPMedia *
gst_rtsp_media_new (const gchar *location)
{
  GstRTSPMedia *result;
  GstRTSPUrl *url;

  url = NULL;

  if (gst_rtsp_url_parse (location, &url) != GST_RTSP_OK)
    goto invalid_url;

  result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
  result->location = g_strdup (location);
  result->url = url;

  return result;

  /* ERRORS */
invalid_url:
  {
    return NULL;
  }
}

static void
caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
{
  if (stream->caps)
    gst_caps_unref (stream->caps);
  if ((stream->caps = GST_PAD_CAPS (pad)))
    gst_caps_ref (stream->caps);
}

/**
 *
 * STREAMING CONFIGURATION
 *
 * gst_rtsp_media_prepare:
 * @media: a #GstRTSPMedia
 * @bin: the parent bin to create the elements in.
 *
 * Prepare the media object so that it creates its streams. Implementations
 * should crate the needed gstreamer elements and add them to @bin. No state
 * changes should be performed on them yet.
 *
 * One or more GstRTSPMediaStream objects should be added to @media with the
 * srcpad member set to a source pad that produces buffer of type 
 * application/x-rtp.
 *
 * Returns: %TRUE if the media could be prepared.
 */
gboolean
gst_rtsp_media_prepare (GstRTSPMedia *media, GstBin *bin)
{
  GstRTSPMediaStream *stream;
  GstElement *pay, *element;
  GstPad * pad;
  gint i;

  /* if we're already prepared we must exit */
  g_return_val_if_fail (media->prepared == FALSE, FALSE);

  g_print ("%s\n", media->url->abspath);

  if (g_str_has_prefix (media->url->abspath, "/camera")) {
    /* live */
    element = gst_parse_launch ("( "
	"v4l2src ! video/x-raw-yuv,width=352,height=288,framerate=15/1 ! "
	"queue ! videorate ! ffmpegcolorspace ! "
	"x264enc bitrate=300 ! rtph264pay name=pay0 pt=96 "
	"alsasrc ! audio/x-raw-int,rate=8000 ! queue ! "
	"amrnbenc ! rtpamrpay name=pay1 pt=97 "
	")", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/h264")) {
    /* transcode h264 */
    element = gst_parse_launch ("( uridecodebin "
	"uri=file:///home/cschalle/Videos/mi2.avi ! "
	"x264enc bitrate=300 ! rtph264pay name=pay0 )", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/theora")) {
    /* transcode theora */
    element = gst_parse_launch ("( uridecodebin "
  	"uri=file:///home/wim/data/mi2.avi ! "
	"theoraenc ! rtptheorapay name=pay0 )", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/macpclinux")) {
    /* theora/vorbis */
    element = gst_parse_launch ("( filesrc "
	"location=/home/cschalle/Videos/mac_pc_linux_2.ogg ! oggdemux name=d ! "
	"queue ! theoraparse ! rtptheorapay name=pay0 "
	"d. ! queue ! vorbisparse ! rtpvorbispay name=pay1 )", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/rtspproxy")) {
    /* proxy RTSP transcode */
    element = gst_parse_launch ("( uridecodebin "
	"uri=rtsp://ia300135.us.archive.org:554/0/items/uncovered_interviews/uncovered_interviews_3_256kb.mp4 ! "
	"x264enc bitrate=1800 ! rtph264pay name=pay0 )", NULL);
  }
  else if (g_str_has_prefix (media->url->abspath, "/httpproxy")) {
    /* proxy HTTP transcode */
    element = gst_parse_launch ("( uridecodebin "
	"uri=http://movies.apple.com/movies/fox/maxpayne/maxpayne-tlre_h480.mov name=d "
	"d. ! queue ! x264enc bitrate=1800 ! rtph264pay name=pay0 pt=96 "
	"d. ! queue ! faac ! rtpmp4gpay name=pay1 pt=97 )", NULL);
  }
  else
    return FALSE;

  gst_bin_add (bin, element);

  for (i = 0; ; i++) {
    gchar *name;

    name = g_strdup_printf ("pay%d", i);

    if (!(pay = gst_bin_get_by_name (GST_BIN (element), name))) {
      g_free (name);
      break;
    }
    
    /* create the stream */
{'fraction_non_alphanumeric': 0.12179042331714088, 'fraction_numerical': 0.01995142262317835, 'mean_word_length': 3.912228376651044, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 70, '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': '4179229', 'n_tokens_mistral': 4427, 'n_tokens_neox': 4222, 'n_words': 1090}