2 * Copyright (C) <2013> YouView TV Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:gstprotection
22 * @short_description: Functions and classes to support encrypted streams.
24 * The GstProtectionMeta class enables the information needed to decrypt a
25 * #GstBuffer to be attached to that buffer.
27 * Typically, a demuxer element would attach GstProtectionMeta objects
28 * to the buffers that it pushes downstream. The demuxer would parse the
29 * protection information for a video/audio frame from its input data and use
30 * this information to populate the #GstStructure @info field,
31 * which is then encapsulated in a GstProtectionMeta object and attached to
32 * the corresponding output buffer using the gst_buffer_add_protection_meta()
33 * function. The information in this attached GstProtectionMeta would be
34 * used by a downstream decrypter element to recover the original unencrypted
40 #include "gst_private.h"
41 #include "glib-compat-private.h"
43 #include "gstprotection.h"
45 #define GST_CAT_DEFAULT GST_CAT_PROTECTION
47 static gboolean gst_protection_meta_init (GstMeta * meta, gpointer params,
50 static void gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer);
52 static const gchar *gst_protection_factory_check (GstElementFactory * fact,
53 const gchar ** system_identifiers);
56 gst_protection_meta_api_get_type (void)
58 static volatile GType type;
59 static const gchar *tags[] = { NULL };
61 if (g_once_init_enter (&type)) {
62 GType _type = gst_meta_api_type_register ("GstProtectionMetaAPI", tags);
63 g_once_init_leave (&type, _type);
69 gst_protection_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
71 GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
73 protection_meta->info = NULL;
79 gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer)
81 GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
83 if (protection_meta->info)
84 gst_structure_free (protection_meta->info);
88 gst_protection_meta_get_info (void)
90 static const GstMetaInfo *protection_meta_info = NULL;
92 if (g_once_init_enter (&protection_meta_info)) {
93 const GstMetaInfo *meta =
94 gst_meta_register (GST_PROTECTION_META_API_TYPE, "GstProtectionMeta",
95 sizeof (GstProtectionMeta), gst_protection_meta_init,
96 gst_protection_meta_free,
97 (GstMetaTransformFunction) NULL);
99 g_once_init_leave (&protection_meta_info, meta);
101 return protection_meta_info;
105 * gst_buffer_add_protection_meta:
106 * @buffer: #GstBuffer holding an encrypted sample, to which protection
107 * metadata should be added.
108 * @info: (transfer full): a #GstStructure holding cryptographic
109 * information relating to the sample contained in @buffer. This
110 * function takes ownership of @info.
112 * Attaches protection metadata to a #GstBuffer.
114 * Returns: a pointer to the added #GstProtectionMeta if successful; %NULL if
120 gst_buffer_add_protection_meta (GstBuffer * buffer, GstStructure * info)
122 GstProtectionMeta *meta;
124 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
125 g_return_val_if_fail (info != NULL, NULL);
128 (GstProtectionMeta *) gst_buffer_add_meta (buffer,
129 GST_PROTECTION_META_INFO, NULL);
137 * gst_protection_select_system:
138 * @system_identifiers: (transfer none): A null terminated array of strings
139 * that contains the UUID values of each protection system that is to be
142 * Iterates the supplied list of UUIDs and checks the GstRegistry for
143 * an element that supports one of the supplied UUIDs. If more than one
144 * element matches, the system ID of the highest ranked element is selected.
146 * Returns: (transfer none): One of the strings from @system_identifiers that
147 * indicates the highest ranked element that implements the protection system
148 * indicated by that system ID, or %NULL if no element has been found.
153 gst_protection_select_system (const gchar ** system_identifiers)
155 GList *decryptors, *walk;
156 const gchar *retval = NULL;
159 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
162 for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
163 GstElementFactory *fact = (GstElementFactory *) walk->data;
165 retval = gst_protection_factory_check (fact, system_identifiers);
168 gst_plugin_feature_list_free (decryptors);
174 gst_protection_factory_check (GstElementFactory * fact,
175 const gchar ** system_identifiers)
177 const GList *template, *walk;
178 const gchar *retval = NULL;
180 template = gst_element_factory_get_static_pad_templates (fact);
181 for (walk = template; walk && !retval; walk = g_list_next (walk)) {
182 GstStaticPadTemplate *templ = walk->data;
183 GstCaps *caps = gst_static_pad_template_get_caps (templ);
184 guint leng = gst_caps_get_size (caps);
186 for (guint i = 0; !retval && i < leng; ++i) {
189 st = gst_caps_get_structure (caps, i);
190 if (gst_structure_has_field_typed (st,
191 GST_PROTECTION_SYSTEM_ID_CAPS_FIELD, G_TYPE_STRING)) {
192 const gchar *sys_id =
193 gst_structure_get_string (st, GST_PROTECTION_SYSTEM_ID_CAPS_FIELD);
194 GST_DEBUG ("Found decryptor that supports protection system %s",
196 for (guint j = 0; !retval && system_identifiers[j]; ++j) {
197 GST_TRACE (" compare with %s", system_identifiers[j]);
198 if (g_ascii_strcasecmp (system_identifiers[j], sys_id) == 0) {
199 GST_DEBUG (" Selecting %s", system_identifiers[j]);
200 retval = system_identifiers[j];
205 gst_caps_unref (caps);