<commit_before>/*
 * (C) Copyright 2013 Kurento (http://kurento.org/)
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * 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
 * Lesser General Public License for more details.
 *
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "kmshubport.h"
#include "kmsagnosticcaps.h"
#include "kmsutils.h"

#define PLUGIN_NAME "hubport"

#define KEY_ELEM_DATA "kms-hub-elem-data"
#define KEY_TYPE_DATA "kms-hub-type-data"
#define KEY_PAD_DATA "kms-hub-pad-data"

GST_DEBUG_CATEGORY_STATIC (kms_hub_port_debug_category);
#define GST_CAT_DEFAULT kms_hub_port_debug_category

#define KMS_HUB_PORT_GET_PRIVATE(obj) (         \
  G_TYPE_INSTANCE_GET_PRIVATE (                 \
    (obj),                                      \
    KMS_TYPE_HUB_PORT,                          \
    KmsHubPortPrivate                           \
  )                                             \
)

struct _KmsHubPortPrivate
{
  GstPad *audio_internal;
  GstPad *vieo_internal;
};

/* Pad templates */
static GstStaticPadTemplate hub_audio_sink_factory =
GST_STATIC_PAD_TEMPLATE (HUB_AUDIO_SINK_PAD,
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS (KMS_AGNOSTIC_AUDIO_CAPS)
    );

static GstStaticPadTemplate hub_video_sink_factory =
GST_STATIC_PAD_TEMPLATE (HUB_VIDEO_SINK_PAD,
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS (KMS_AGNOSTIC_VIDEO_CAPS)
    );

static GstStaticPadTemplate hub_audio_src_factory =
GST_STATIC_PAD_TEMPLATE (HUB_AUDIO_SRC_PAD,
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS (KMS_AGNOSTIC_AUDIO_CAPS)
    );

static GstStaticPadTemplate hub_video_src_factory =
GST_STATIC_PAD_TEMPLATE (HUB_VIDEO_SRC_PAD,
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS (KMS_AGNOSTIC_VIDEO_CAPS)
    );

/* class initialization */

G_DEFINE_TYPE_WITH_CODE (KmsHubPort, kms_hub_port,
    KMS_TYPE_ELEMENT,
    GST_DEBUG_CATEGORY_INIT (kms_hub_port_debug_category, PLUGIN_NAME,
        0, "debug category for hubport element"));

static GstPad *
kms_hub_port_generate_sink_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name, const GstCaps * caps,
    GstElement * agnosticbin)
{
  GstPad *agnostic_pad, *pad;

  agnostic_pad = gst_element_get_static_pad (agnosticbin, "sink");
  pad = gst_ghost_pad_new_from_template (name, agnostic_pad, templ);
  g_object_unref (agnostic_pad);

  if (GST_STATE (element) >= GST_STATE_PAUSED
      || GST_STATE_PENDING (element) >= GST_STATE_PAUSED
      || GST_STATE_TARGET (element) >= GST_STATE_PAUSED) {
    gst_pad_set_active (pad, TRUE);
  }

  if (gst_element_add_pad (element, pad))
    return pad;

  GST_ERROR_OBJECT (element, "Cannot add pad %" GST_PTR_FORMAT, pad);
  g_object_unref (pad);

  return NULL;
}

static GstPad *
kms_hub_port_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
{
  GstElement *agnosticbin = NULL;

  if (templ ==
      gst_element_class_get_pad_template (GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS
              (element)), HUB_AUDIO_SINK_PAD)) {

    if (g_strcmp0 (name, HUB_AUDIO_SINK_PAD) != 0) {
      GST_ERROR_OBJECT (element,
          "Invalid pad name %s for template %" GST_PTR_FORMAT, name, templ);
      return NULL;
    }

    agnosticbin = kms_element_get_audio_agnosticbin (KMS_ELEMENT (element));
  } else if (templ ==
      gst_element_class_get_pad_template (GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS
              (element)), HUB_VIDEO_SINK_PAD)) {
    if (g_strcmp0 (name, HUB_VIDEO_SINK_PAD) != 0) {
      GST_ERROR_OBJECT (element,
          "Invalid pad name %s for template %" GST_PTR_FORMAT, name, templ);
      return NULL;
    }

    agnosticbin = kms_element_get_video_agnosticbin (KMS_ELEMENT (element));
  }

  if (agnosticbin == NULL) {
    GST_WARNING_OBJECT (element, "No agnosticbin got for template %"
        GST_PTR_FORMAT, templ);
    return NULL;
  } else {
    return kms_hub_port_generate_sink_pad (element, templ, name, caps,
        agnosticbin);
  }
}

static void
kms_hub_port_internal_src_unhandled (KmsHubPort * self, GstPad * pad)
{
  GstPad *sink = g_object_get_data (G_OBJECT (pad), KEY_PAD_DATA);

  g_return_if_fail (sink);

  kms_element_remove_sink (KMS_ELEMENT (self), sink);

  g_object_set_data (G_OBJECT (pad), KEY_PAD_DATA, NULL);
}

void
kms_hub_port_unhandled (KmsHubPort * self)
{
  GstPad *video_src, *audio_src;

  g_return_if_fail (!self);
<commit_msg>hubport: Fix fail condition<commit_after>/*
 * (C) Copyright 2013 Kurento (http://kurento.org/)
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * 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
 * Lesser General Public License for more details.
 *
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "kmshubport.h"
#include "kmsagnosticcaps.h"
#include "kmsutils.h"

#define PLUGIN_NAME "hubport"

#define KEY_ELEM_DATA "kms-hub-elem-data"
#define KEY_TYPE_DATA "kms-hub-type-data"
#define KEY_PAD_DATA "kms-hub-pad-data"

GST_DEBUG_CATEGORY_STATIC (kms_hub_port_debug_category);
#define GST_CAT_DEFAULT kms_hub_port_debug_category

#define KMS_HUB_PORT_GET_PRIVATE(obj) (         \
  G_TYPE_INSTANCE_GET_PRIVATE (                 \
    (obj),                                      \
    KMS_TYPE_HUB_PORT,                          \
    KmsHubPortPrivate                           \
  )                                             \
)

struct _KmsHubPortPrivate
{
  GstPad *audio_internal;
  GstPad *vieo_internal;
};

/* Pad templates */
static GstStaticPadTemplate hub_audio_sink_factory =
GST_STATIC_PAD_TEMPLATE (HUB_AUDIO_SINK_PAD,
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS (KMS_AGNOSTIC_AUDIO_CAPS)
    );

static GstStaticPadTemplate hub_video_sink_factory =
GST_STATIC_PAD_TEMPLATE (HUB_VIDEO_SINK_PAD,
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS (KMS_AGNOSTIC_VIDEO_CAPS)
    );

static GstStaticPadTemplate hub_audio_src_factory =
GST_STATIC_PAD_TEMPLATE (HUB_AUDIO_SRC_PAD,
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS (KMS_AGNOSTIC_AUDIO_CAPS)
    );

static GstStaticPadTemplate hub_video_src_factory =
GST_STATIC_PAD_TEMPLATE (HUB_VIDEO_SRC_PAD,
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS (KMS_AGNOSTIC_VIDEO_CAPS)
    );

/* class initialization */

G_DEFINE_TYPE_WITH_CODE (KmsHubPort, kms_hub_port,
    KMS_TYPE_ELEMENT,
    GST_DEBUG_CATEGORY_INIT (kms_hub_port_debug_category, PLUGIN_NAME,
        0, "debug category for hubport element"));

static GstPad *
kms_hub_port_generate_sink_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name, const GstCaps * caps,
    GstElement * agnosticbin)
{
  GstPad *agnostic_pad, *pad;

  agnostic_pad = gst_element_get_static_pad (agnosticbin, "sink");
  pad = gst_ghost_pad_new_from_template (name, agnostic_pad, templ);
  g_object_unref (agnostic_pad);

  if (GST_STATE (element) >= GST_STATE_PAUSED
      || GST_STATE_PENDING (element) >= GST_STATE_PAUSED
      || GST_STATE_TARGET (element) >= GST_STATE_PAUSED) {
    gst_pad_set_active (pad, TRUE);
  }

  if (gst_element_add_pad (element, pad))
    return pad;

  GST_ERROR_OBJECT (element, "Cannot add pad %" GST_PTR_FORMAT, pad);
  g_object_unref (pad);

  return NULL;
}

static GstPad *
kms_hub_port_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
{
  GstElement *agnosticbin = NULL;

  if (templ ==
      gst_element_class_get_pad_template (GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS
              (element)), HUB_AUDIO_SINK_PAD)) {

    if (g_strcmp0 (name, HUB_AUDIO_SINK_PAD) != 0) {
      GST_ERROR_OBJECT (element,
          "Invalid pad name %s for template %" GST_PTR_FORMAT, name, templ);
      return NULL;
    }

    agnosticbin = kms_element_get_audio_agnosticbin (KMS_ELEMENT (element));
  } else if (templ ==
      gst_element_class_get_pad_template (GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS
              (element)), HUB_VIDEO_SINK_PAD)) {
    if (g_strcmp0 (name, HUB_VIDEO_SINK_PAD) != 0) {
      GST_ERROR_OBJECT (element,
          "Invalid pad name %s for template %" GST_PTR_FORMAT, name, templ);
      return NULL;
    }

    agnosticbin = kms_element_get_video_agnosticbin (KMS_ELEMENT (element));
  }

  if (agnosticbin == NULL) {
    GST_WARNING_OBJECT (element, "No agnosticbin got for template %"
        GST_PTR_FORMAT, templ);
    return NULL;
  } else {
    return kms_hub_port_generate_sink_pad (element, templ, name, caps,
        agnosticbin);
  }
}

static void
kms_hub_port_internal_src_unhandled (KmsHubPort * self, GstPad * pad)
{
  GstPad *sink = g_object_get_data (G_OBJECT (pad), KEY_PAD_DATA);

  g_return_if_fail (sink);

  kms_element_remove_sink (KMS_ELEMENT (self), sink);

  g_object_set_data (G_OBJECT (pad), KEY_PAD_DATA, NULL);
}

void
kms_hub_port_unhandled (KmsHubPort * self)
{
  GstPad *video_src, *audio_src;

  g_return_if_fail (self);

{'fraction_non_alphanumeric': 0.0842737343410291, 'fraction_numerical': 0.0026917900403768506, 'mean_word_length': 3.782178217821782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6523352', 'n_tokens_mistral': 3678, 'n_tokens_neox': 3587, 'n_words': 727}