<commit_before>/*
 * (C) Copyright 2014 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 "kmsenctreebin.h"
#include "kmsutils.h"

#define GST_DEFAULT_NAME "enctreebin"
#define GST_CAT_DEFAULT kms_enc_tree_bin_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

#define kms_enc_tree_bin_parent_class parent_class
G_DEFINE_TYPE (KmsEncTreeBin, kms_enc_tree_bin, KMS_TYPE_TREE_BIN);

#define KMS_ENC_TREE_BIN_GET_PRIVATE(obj) ( \
  G_TYPE_INSTANCE_GET_PRIVATE (             \
    (obj),                                  \
    KMS_TYPE_ENC_TREE_BIN,                  \
    KmsEncTreeBinPrivate                    \
  )                                         \
)

typedef enum
{
  VP8,
  X264,
  OPENH264,
  UNSUPPORTED
} EncoderType;

struct _KmsEncTreeBinPrivate
{
  GstPad *enc_sink;
  GstElement *enc;
  EncoderType enc_type;
  RembEventManager *remb_manager;

  gint remb_bitrate;
};

static void
configure_encoder (GstElement * encoder, EncoderType type, gint target_bitrate)
{
  GST_DEBUG ("Configure encoder: %" GST_PTR_FORMAT, encoder);
  switch (type) {
    case VP8:
    {
      g_object_set (G_OBJECT (encoder), "deadline", G_GINT64_CONSTANT (200000),
          "threads", 1, "cpu-used", 16, "resize-allowed", TRUE,
          "target-bitrate", target_bitrate, "end-usage", /* cbr */ 1, NULL);
      break;
    }
    case X264:
    {
      g_object_set (G_OBJECT (encoder), "speed-preset", 1 /* ultrafast */ ,
          "threads", (guint) 1, "bitrate", target_bitrate / 1000, NULL);
      break;
    }
    case OPENH264:
    {
      g_object_set (G_OBJECT (encoder), "rate-control", 1 /* bitrate */ ,
          "bitrate", target_bitrate, NULL);
      break;
    }
    default:
      GST_DEBUG ("Codec %" GST_PTR_FORMAT
          " not configured because it is not supported", encoder);
      break;
  }
}

static void
kms_enc_tree_bin_set_encoder_type (KmsEncTreeBin * self)
{
  gchar *name;

  g_object_get (self->priv->enc, "name", &name, NULL);

  if (g_str_has_prefix (name, "vp8enc")) {
    self->priv->enc_type = VP8;
  } else if (g_str_has_prefix (name, "x264enc")) {
    self->priv->enc_type = X264;
  } else if (g_str_has_prefix (name, "openh264enc")) {
    self->priv->enc_type = OPENH264;
  } else {
    self->priv->enc_type = UNSUPPORTED;
  }

  g_free (name);
}

static void
kms_enc_tree_bin_create_encoder_for_caps (KmsEncTreeBin * self,
    const GstCaps * caps, gint target_bitrate)
{
  GList *encoder_list, *filtered_list, *l;
  GstElementFactory *encoder_factory = NULL;

  encoder_list =
      gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_ENCODER,
      GST_RANK_NONE);

  /* HACK: Augment the openh264 rank */
  for (l = encoder_list; l != NULL; l = l->next) {
    encoder_factory = GST_ELEMENT_FACTORY (l->data);

    if (g_str_has_prefix (GST_OBJECT_NAME (encoder_factory), "openh264")) {
      encoder_list = g_list_remove (encoder_list, l->data);
      encoder_list = g_list_prepend (encoder_list, encoder_factory);
      break;
    }
  }
<commit_msg>enctreebin: Configure opus encoder correctly to enable inband fec<commit_after>/*
 * (C) Copyright 2014 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 "kmsenctreebin.h"
#include "kmsutils.h"

#define GST_DEFAULT_NAME "enctreebin"
#define GST_CAT_DEFAULT kms_enc_tree_bin_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

#define kms_enc_tree_bin_parent_class parent_class
G_DEFINE_TYPE (KmsEncTreeBin, kms_enc_tree_bin, KMS_TYPE_TREE_BIN);

#define KMS_ENC_TREE_BIN_GET_PRIVATE(obj) ( \
  G_TYPE_INSTANCE_GET_PRIVATE (             \
    (obj),                                  \
    KMS_TYPE_ENC_TREE_BIN,                  \
    KmsEncTreeBinPrivate                    \
  )                                         \
)

typedef enum
{
  VP8,
  X264,
  OPENH264,
  OPUS,
  UNSUPPORTED
} EncoderType;

struct _KmsEncTreeBinPrivate
{
  GstPad *enc_sink;
  GstElement *enc;
  EncoderType enc_type;
  RembEventManager *remb_manager;

  gint remb_bitrate;
};

static void
configure_encoder (GstElement * encoder, EncoderType type, gint target_bitrate)
{
  GST_DEBUG ("Configure encoder: %" GST_PTR_FORMAT, encoder);
  switch (type) {
    case VP8:
    {
      g_object_set (G_OBJECT (encoder), "deadline", G_GINT64_CONSTANT (200000),
          "threads", 1, "cpu-used", 16, "resize-allowed", TRUE,
          "target-bitrate", target_bitrate, "end-usage", /* cbr */ 1, NULL);
      break;
    }
    case X264:
    {
      g_object_set (G_OBJECT (encoder), "speed-preset", 1 /* ultrafast */ ,
          "threads", (guint) 1, "bitrate", target_bitrate / 1000, NULL);
      break;
    }
    case OPENH264:
    {
      g_object_set (G_OBJECT (encoder), "rate-control", 1 /* bitrate */ ,
          "bitrate", target_bitrate, NULL);
      break;
    }
    case OPUS:
    {
      g_object_set (G_OBJECT (encoder), "inband-fec", TRUE, NULL);
      break;
    }
    default:
      GST_DEBUG ("Codec %" GST_PTR_FORMAT
          " not configured because it is not supported", encoder);
      break;
  }
}

static void
kms_enc_tree_bin_set_encoder_type (KmsEncTreeBin * self)
{
  gchar *name;

  g_object_get (self->priv->enc, "name", &name, NULL);

  if (g_str_has_prefix (name, "vp8enc")) {
    self->priv->enc_type = VP8;
  } else if (g_str_has_prefix (name, "x264enc")) {
    self->priv->enc_type = X264;
  } else if (g_str_has_prefix (name, "openh264enc")) {
    self->priv->enc_type = OPENH264;
  } else if (g_str_has_prefix (name, "opusenc")) {
    self->priv->enc_type = OPUS;
  } else {
    self->priv->enc_type = UNSUPPORTED;
  }

  g_free (name);
}

static void
kms_enc_tree_bin_create_encoder_for_caps (KmsEncTreeBin * self,
    const GstCaps * caps, gint target_bitrate)
{
  GList *encoder_list, *filtered_list, *l;
  GstElementFactory *encoder_factory = NULL;

  encoder_list =
      gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_ENCODER,
      GST_RANK_NONE);

  /* HACK: Augment the openh264 rank */
  for (l = encoder_list; l != NULL; l = l->next) {
    encoder_factory = GST_ELEMENT_FACTORY (l->data);

    if (g_str_has_prefix (GST_OBJECT_NAME (encoder_factory), "openh264")) {
      encoder_list = g_list_remove (encoder_list, l->data);
      encoder_list = g_list_prepend (encoder_list, encoder_factory);
      break;
    }
  }

{'fraction_non_alphanumeric': 0.10662707517883654, 'fraction_numerical': 0.0164664597111621, 'mean_word_length': 3.1864406779661016, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2991317', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2590, 'n_words': 611}