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 * @title: GstProtection
23 * @short_description: Functions and classes to support encrypted streams.
25 * The GstProtectionMeta class enables the information needed to decrypt a
26 * #GstBuffer to be attached to that buffer.
28 * Typically, a demuxer element would attach GstProtectionMeta objects
29 * to the buffers that it pushes downstream. The demuxer would parse the
30 * protection information for a video/audio frame from its input data and use
31 * this information to populate the #GstStructure @info field,
32 * which is then encapsulated in a GstProtectionMeta object and attached to
33 * the corresponding output buffer using the gst_buffer_add_protection_meta()
34 * function. The information in this attached GstProtectionMeta would be
35 * used by a downstream decrypter element to recover the original unencrypted
41 #include "gst_private.h"
42 #include "glib-compat-private.h"
44 #include "gstprotection.h"
46 #define GST_CAT_DEFAULT GST_CAT_PROTECTION
48 static gboolean gst_protection_meta_init (GstMeta * meta, gpointer params,
51 static void gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer);
53 static const gchar *gst_protection_factory_check (GstElementFactory * fact,
54 const gchar ** system_identifiers);
57 gst_protection_meta_api_get_type (void)
59 static volatile GType type;
60 static const gchar *tags[] = { NULL };
62 if (g_once_init_enter (&type)) {
63 GType _type = gst_meta_api_type_register ("GstProtectionMetaAPI", tags);
64 g_once_init_leave (&type, _type);
70 gst_protection_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
72 GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
74 protection_meta->info = NULL;
80 gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer)
82 GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
84 if (protection_meta->info)
85 gst_structure_free (protection_meta->info);
89 gst_protection_meta_transform (GstBuffer * transbuf, GstMeta * meta,
90 GstBuffer * buffer, GQuark type, gpointer data)
92 GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
94 if (GST_META_TRANSFORM_IS_COPY (type)) {
95 GstMetaTransformCopy *copy = data;
97 /* only copy if the complete data is copied as well */
98 gst_buffer_add_protection_meta (transbuf,
99 gst_structure_copy (protection_meta->info));
104 /* transform type not supported */
111 gst_protection_meta_get_info (void)
113 static const GstMetaInfo *protection_meta_info = NULL;
115 if (g_once_init_enter ((GstMetaInfo **) & protection_meta_info)) {
116 const GstMetaInfo *meta =
117 gst_meta_register (GST_PROTECTION_META_API_TYPE, "GstProtectionMeta",
118 sizeof (GstProtectionMeta), gst_protection_meta_init,
119 gst_protection_meta_free, gst_protection_meta_transform);
121 g_once_init_leave ((GstMetaInfo **) & protection_meta_info,
122 (GstMetaInfo *) meta);
124 return protection_meta_info;
128 * gst_buffer_add_protection_meta:
129 * @buffer: #GstBuffer holding an encrypted sample, to which protection
130 * metadata should be added.
131 * @info: (transfer full): a #GstStructure holding cryptographic
132 * information relating to the sample contained in @buffer. This
133 * function takes ownership of @info.
135 * Attaches protection metadata to a #GstBuffer.
137 * Returns: (transfer none): a pointer to the added #GstProtectionMeta if successful; %NULL if
143 gst_buffer_add_protection_meta (GstBuffer * buffer, GstStructure * info)
145 GstProtectionMeta *meta;
147 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
148 g_return_val_if_fail (info != NULL, NULL);
151 (GstProtectionMeta *) gst_buffer_add_meta (buffer,
152 GST_PROTECTION_META_INFO, NULL);
160 * gst_protection_select_system:
161 * @system_identifiers: (transfer none) (array zero-terminated=1): A null terminated array of strings
162 * that contains the UUID values of each protection system that is to be
165 * Iterates the supplied list of UUIDs and checks the GstRegistry for
166 * an element that supports one of the supplied UUIDs. If more than one
167 * element matches, the system ID of the highest ranked element is selected.
169 * Returns: (transfer none): One of the strings from @system_identifiers that
170 * indicates the highest ranked element that implements the protection system
171 * indicated by that system ID, or %NULL if no element has been found.
176 gst_protection_select_system (const gchar ** system_identifiers)
178 GList *decryptors, *walk;
179 const gchar *retval = NULL;
182 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
185 for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
186 GstElementFactory *fact = (GstElementFactory *) walk->data;
188 retval = gst_protection_factory_check (fact, system_identifiers);
191 gst_plugin_feature_list_free (decryptors);
197 * gst_protection_filter_systems_by_available_decryptors:
198 * @system_identifiers: (transfer none): A null terminated array of strings
199 * that contains the UUID values of each protection system that is to be
202 * Iterates the supplied list of UUIDs and checks the GstRegistry for
203 * all the decryptors supporting one of the supplied UUIDs.
205 * Returns: (transfer full): A null terminated array containing all the
206 * @system_identifiers supported by the set of available decryptors, or %NULL
207 * if no matches were found.
212 gst_protection_filter_systems_by_available_decryptors (const gchar **
215 GList *decryptors, *walk;
217 guint i = 0, decryptors_number;
220 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
223 decryptors_number = g_list_length (decryptors);
225 GST_TRACE ("found %u decrytors", decryptors_number);
227 if (decryptors_number == 0)
230 retval = g_new (gchar *, decryptors_number + 1);
232 for (walk = decryptors; walk; walk = g_list_next (walk)) {
233 GstElementFactory *fact = (GstElementFactory *) walk->data;
234 const char *found_sys_id =
235 gst_protection_factory_check (fact, system_identifiers);
237 GST_DEBUG ("factory %s is valid for %s", GST_OBJECT_NAME (fact),
241 retval[i++] = g_strdup (found_sys_id);
246 if (retval[0] == NULL) {
251 gst_plugin_feature_list_free (decryptors);
257 gst_protection_factory_check (GstElementFactory * fact,
258 const gchar ** system_identifiers)
260 const GList *template, *walk;
261 const gchar *retval = NULL;
263 template = gst_element_factory_get_static_pad_templates (fact);
264 for (walk = template; walk && !retval; walk = g_list_next (walk)) {
265 GstStaticPadTemplate *templ = walk->data;
266 GstCaps *caps = gst_static_pad_template_get_caps (templ);
267 guint leng = gst_caps_get_size (caps);
270 for (i = 0; !retval && i < leng; ++i) {
273 st = gst_caps_get_structure (caps, i);
274 if (gst_structure_has_field_typed (st,
275 GST_PROTECTION_SYSTEM_ID_CAPS_FIELD, G_TYPE_STRING)) {
276 const gchar *sys_id =
277 gst_structure_get_string (st, GST_PROTECTION_SYSTEM_ID_CAPS_FIELD);
278 GST_DEBUG ("Found decryptor that supports protection system %s",
280 for (j = 0; !retval && system_identifiers[j]; ++j) {
281 GST_TRACE (" compare with %s", system_identifiers[j]);
282 if (g_ascii_strcasecmp (system_identifiers[j], sys_id) == 0) {
283 GST_DEBUG (" Selecting %s", system_identifiers[j]);
284 retval = system_identifiers[j];
289 gst_caps_unref (caps);