Blame gst/gstprotection.c

Packit Service 963350
/* GStreamer
Packit Service 963350
 * Copyright (C) <2013> YouView TV Ltd.
Packit Service 963350
 *
Packit Service 963350
 * This library is free software; you can redistribute it and/or
Packit Service 963350
 * modify it under the terms of the GNU Library General Public
Packit Service 963350
 * License as published by the Free Software Foundation; either
Packit Service 963350
 * version 2 of the License, or (at your option) any later version.
Packit Service 963350
 *
Packit Service 963350
 * This library is distributed in the hope that it will be useful,
Packit Service 963350
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 963350
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 963350
 * Library General Public License for more details.
Packit Service 963350
 *
Packit Service 963350
 * You should have received a copy of the GNU Library General Public
Packit Service 963350
 * License along with this library; if not, write to the
Packit Service 963350
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit Service 963350
 * Boston, MA 02110-1301, USA.
Packit Service 963350
 */
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * SECTION:gstprotection
Packit Service 963350
 * @title: GstProtection
Packit Service 963350
 * @short_description: Functions and classes to support encrypted streams.
Packit Service 963350
 *
Packit Service 963350
 * The GstProtectionMeta class enables the information needed to decrypt a
Packit Service 963350
 * #GstBuffer to be attached to that buffer.
Packit Service 963350
 *
Packit Service 963350
 * Typically, a demuxer element would attach GstProtectionMeta objects
Packit Service 963350
 * to the buffers that it pushes downstream. The demuxer would parse the
Packit Service 963350
 * protection information for a video/audio frame from its input data and use
Packit Service 963350
 * this information to populate the #GstStructure @info field,
Packit Service 963350
 * which is then encapsulated in a GstProtectionMeta object and attached to
Packit Service 963350
 * the corresponding output buffer using the gst_buffer_add_protection_meta()
Packit Service 963350
 * function. The information in this attached GstProtectionMeta would be
Packit Service 963350
 * used by a downstream decrypter element to recover the original unencrypted
Packit Service 963350
 * frame.
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.6
Packit Service 963350
 */
Packit Service 963350
Packit Service 963350
#include "gst_private.h"
Packit Service 963350
#include "glib-compat-private.h"
Packit Service 963350
Packit Service 963350
#include "gstprotection.h"
Packit Service 963350
Packit Service 963350
#define GST_CAT_DEFAULT GST_CAT_PROTECTION
Packit Service 963350
Packit Service 963350
static gboolean gst_protection_meta_init (GstMeta * meta, gpointer params,
Packit Service 963350
    GstBuffer * buffer);
Packit Service 963350
Packit Service 963350
static void gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer);
Packit Service 963350
Packit Service 963350
static const gchar *gst_protection_factory_check (GstElementFactory * fact,
Packit Service 963350
    const gchar ** system_identifiers);
Packit Service 963350
Packit Service 963350
GType
Packit Service 963350
gst_protection_meta_api_get_type (void)
Packit Service 963350
{
Packit Service 963350
  static volatile GType type;
Packit Service 963350
  static const gchar *tags[] = { NULL };
Packit Service 963350
Packit Service 963350
  if (g_once_init_enter (&type)) {
Packit Service 963350
    GType _type = gst_meta_api_type_register ("GstProtectionMetaAPI", tags);
Packit Service 963350
    g_once_init_leave (&type, _type);
Packit Service 963350
  }
Packit Service 963350
  return type;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static gboolean
Packit Service 963350
gst_protection_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
Packit Service 963350
{
Packit Service 963350
  GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
Packit Service 963350
Packit Service 963350
  protection_meta->info = NULL;
Packit Service 963350
Packit Service 963350
  return TRUE;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer)
Packit Service 963350
{
Packit Service 963350
  GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
Packit Service 963350
Packit Service 963350
  if (protection_meta->info)
Packit Service 963350
    gst_structure_free (protection_meta->info);
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static gboolean
Packit Service 963350
gst_protection_meta_transform (GstBuffer * transbuf, GstMeta * meta,
Packit Service 963350
    GstBuffer * buffer, GQuark type, gpointer data)
Packit Service 963350
{
Packit Service 963350
  GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
Packit Service 963350
Packit Service 963350
  if (GST_META_TRANSFORM_IS_COPY (type)) {
Packit Service 963350
    GstMetaTransformCopy *copy = data;
Packit Service 963350
    if (!copy->region) {
Packit Service 963350
      /* only copy if the complete data is copied as well */
Packit Service 963350
      gst_buffer_add_protection_meta (transbuf,
Packit Service 963350
          gst_structure_copy (protection_meta->info));
Packit Service 963350
    } else {
Packit Service 963350
      return FALSE;
Packit Service 963350
    }
Packit Service 963350
  } else {
Packit Service 963350
    /* transform type not supported */
Packit Service 963350
    return FALSE;
Packit Service 963350
  }
Packit Service 963350
  return TRUE;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
const GstMetaInfo *
Packit Service 963350
gst_protection_meta_get_info (void)
Packit Service 963350
{
Packit Service 963350
  static const GstMetaInfo *protection_meta_info = NULL;
Packit Service 963350
Packit Service 963350
  if (g_once_init_enter ((GstMetaInfo **) & protection_meta_info)) {
Packit Service 963350
    const GstMetaInfo *meta =
Packit Service 963350
        gst_meta_register (GST_PROTECTION_META_API_TYPE, "GstProtectionMeta",
Packit Service 963350
        sizeof (GstProtectionMeta), gst_protection_meta_init,
Packit Service 963350
        gst_protection_meta_free, gst_protection_meta_transform);
Packit Service 963350
Packit Service 963350
    g_once_init_leave ((GstMetaInfo **) & protection_meta_info,
Packit Service 963350
        (GstMetaInfo *) meta);
Packit Service 963350
  }
Packit Service 963350
  return protection_meta_info;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * gst_buffer_add_protection_meta:
Packit Service 963350
 * @buffer: #GstBuffer holding an encrypted sample, to which protection
Packit Service 963350
 *     metadata should be added.
Packit Service 963350
 * @info: (transfer full): a #GstStructure holding cryptographic
Packit Service 963350
 *     information relating to the sample contained in @buffer. This
Packit Service 963350
 *     function takes ownership of @info.
Packit Service 963350
 *
Packit Service 963350
 * Attaches protection metadata to a #GstBuffer.
Packit Service 963350
 *
Packit Service 963350
 * Returns: (transfer none): a pointer to the added #GstProtectionMeta if successful; %NULL if
Packit Service 963350
 * unsuccessful.
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.6
Packit Service 963350
 */
Packit Service 963350
GstProtectionMeta *
Packit Service 963350
gst_buffer_add_protection_meta (GstBuffer * buffer, GstStructure * info)
Packit Service 963350
{
Packit Service 963350
  GstProtectionMeta *meta;
Packit Service 963350
Packit Service 963350
  g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
Packit Service 963350
  g_return_val_if_fail (info != NULL, NULL);
Packit Service 963350
Packit Service 963350
  meta =
Packit Service 963350
      (GstProtectionMeta *) gst_buffer_add_meta (buffer,
Packit Service 963350
      GST_PROTECTION_META_INFO, NULL);
Packit Service 963350
Packit Service 963350
  meta->info = info;
Packit Service 963350
Packit Service 963350
  return meta;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * gst_protection_select_system:
Packit Service 963350
 * @system_identifiers: (transfer none) (array zero-terminated=1): A null terminated array of strings
Packit Service 963350
 * that contains the UUID values of each protection system that is to be
Packit Service 963350
 * checked.
Packit Service 963350
 *
Packit Service 963350
 * Iterates the supplied list of UUIDs and checks the GstRegistry for
Packit Service 963350
 * an element that supports one of the supplied UUIDs. If more than one
Packit Service 963350
 * element matches, the system ID of the highest ranked element is selected.
Packit Service 963350
 *
Packit Service 963350
 * Returns: (transfer none) (nullable): One of the strings from
Packit Service 963350
 * @system_identifiers that indicates the highest ranked element that
Packit Service 963350
 * implements the protection system indicated by that system ID, or %NULL if no
Packit Service 963350
 * element has been found.
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.6
Packit Service 963350
 */
Packit Service 963350
const gchar *
Packit Service 963350
gst_protection_select_system (const gchar ** system_identifiers)
Packit Service 963350
{
Packit Service 963350
  GList *decryptors, *walk;
Packit Service 963350
  const gchar *retval = NULL;
Packit Service 963350
Packit Service 963350
  decryptors =
Packit Service 963350
      gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
Packit Service 963350
      GST_RANK_MARGINAL);
Packit Service 963350
Packit Service 963350
  for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
Packit Service 963350
    GstElementFactory *fact = (GstElementFactory *) walk->data;
Packit Service 963350
Packit Service 963350
    retval = gst_protection_factory_check (fact, system_identifiers);
Packit Service 963350
  }
Packit Service 963350
Packit Service 963350
  gst_plugin_feature_list_free (decryptors);
Packit Service 963350
Packit Service 963350
  return retval;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * gst_protection_filter_systems_by_available_decryptors:
Packit Service 963350
 * @system_identifiers: (transfer none): A null terminated array of strings
Packit Service 963350
 * that contains the UUID values of each protection system that is to be
Packit Service 963350
 * checked.
Packit Service 963350
 *
Packit Service 963350
 * Iterates the supplied list of UUIDs and checks the GstRegistry for
Packit Service 963350
 * all the decryptors supporting one of the supplied UUIDs.
Packit Service 963350
 *
Packit Service 963350
 * Returns: (transfer full) (nullable): A null terminated array containing all
Packit Service 963350
 * the @system_identifiers supported by the set of available decryptors, or
Packit Service 963350
 * %NULL if no matches were found.
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.14
Packit Service 963350
 */
Packit Service 963350
gchar **
Packit Service 963350
gst_protection_filter_systems_by_available_decryptors (const gchar **
Packit Service 963350
    system_identifiers)
Packit Service 963350
{
Packit Service 963350
  GList *decryptors, *walk;
Packit Service 963350
  gchar **retval;
Packit Service 963350
  guint i = 0, decryptors_number;
Packit Service 963350
Packit Service 963350
  decryptors =
Packit Service 963350
      gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
Packit Service 963350
      GST_RANK_MARGINAL);
Packit Service 963350
Packit Service 963350
  decryptors_number = g_list_length (decryptors);
Packit Service 963350
Packit Service 963350
  GST_TRACE ("found %u decrytors", decryptors_number);
Packit Service 963350
Packit Service 963350
  if (decryptors_number == 0)
Packit Service 963350
    return NULL;
Packit Service 963350
Packit Service 963350
  retval = g_new (gchar *, decryptors_number + 1);
Packit Service 963350
Packit Service 963350
  for (walk = decryptors; walk; walk = g_list_next (walk)) {
Packit Service 963350
    GstElementFactory *fact = (GstElementFactory *) walk->data;
Packit Service 963350
    const char *found_sys_id =
Packit Service 963350
        gst_protection_factory_check (fact, system_identifiers);
Packit Service 963350
Packit Service 963350
    GST_DEBUG ("factory %s is valid for %s", GST_OBJECT_NAME (fact),
Packit Service 963350
        found_sys_id);
Packit Service 963350
Packit Service 963350
    if (found_sys_id) {
Packit Service 963350
      retval[i++] = g_strdup (found_sys_id);
Packit Service 963350
    }
Packit Service 963350
  }
Packit Service 963350
  retval[i] = NULL;
Packit Service 963350
Packit Service 963350
  if (retval[0] == NULL) {
Packit Service 963350
    g_free (retval);
Packit Service 963350
    retval = NULL;
Packit Service 963350
  }
Packit Service 963350
Packit Service 963350
  gst_plugin_feature_list_free (decryptors);
Packit Service 963350
Packit Service 963350
  return retval;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static const gchar *
Packit Service 963350
gst_protection_factory_check (GstElementFactory * fact,
Packit Service 963350
    const gchar ** system_identifiers)
Packit Service 963350
{
Packit Service 963350
  const GList *template, *walk;
Packit Service 963350
  const gchar *retval = NULL;
Packit Service 963350
Packit Service 963350
  template = gst_element_factory_get_static_pad_templates (fact);
Packit Service 963350
  for (walk = template; walk && !retval; walk = g_list_next (walk)) {
Packit Service 963350
    GstStaticPadTemplate *templ = walk->data;
Packit Service 963350
    GstCaps *caps = gst_static_pad_template_get_caps (templ);
Packit Service 963350
    guint leng = gst_caps_get_size (caps);
Packit Service 963350
    guint i, j;
Packit Service 963350
Packit Service 963350
    for (i = 0; !retval && i < leng; ++i) {
Packit Service 963350
      GstStructure *st;
Packit Service 963350
Packit Service 963350
      st = gst_caps_get_structure (caps, i);
Packit Service 963350
      if (gst_structure_has_field_typed (st,
Packit Service 963350
              GST_PROTECTION_SYSTEM_ID_CAPS_FIELD, G_TYPE_STRING)) {
Packit Service 963350
        const gchar *sys_id =
Packit Service 963350
            gst_structure_get_string (st, GST_PROTECTION_SYSTEM_ID_CAPS_FIELD);
Packit Service 963350
        GST_DEBUG ("Found decryptor that supports protection system %s",
Packit Service 963350
            sys_id);
Packit Service 963350
        for (j = 0; !retval && system_identifiers[j]; ++j) {
Packit Service 963350
          GST_TRACE ("  compare with %s", system_identifiers[j]);
Packit Service 963350
          if (g_ascii_strcasecmp (system_identifiers[j], sys_id) == 0) {
Packit Service 963350
            GST_DEBUG ("  Selecting %s", system_identifiers[j]);
Packit Service 963350
            retval = system_identifiers[j];
Packit Service 963350
          }
Packit Service 963350
        }
Packit Service 963350
      }
Packit Service 963350
    }
Packit Service 963350
    gst_caps_unref (caps);
Packit Service 963350
  }
Packit Service 963350
Packit Service 963350
  return retval;
Packit Service 963350
}