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) (nullable): One of the strings from
170 * @system_identifiers that indicates the highest ranked element that
171 * implements the protection system indicated by that system ID, or %NULL if no
172 * element has been found.
177 gst_protection_select_system (const gchar ** system_identifiers)
179 GList *decryptors, *walk;
180 const gchar *retval = NULL;
183 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
186 for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
187 GstElementFactory *fact = (GstElementFactory *) walk->data;
189 retval = gst_protection_factory_check (fact, system_identifiers);
192 gst_plugin_feature_list_free (decryptors);
198 * gst_protection_filter_systems_by_available_decryptors:
199 * @system_identifiers: (transfer none) (array zero-terminated=1):
200 * A null terminated array of strings that contains the UUID values of each
201 * protection system that is to be checked.
203 * Iterates the supplied list of UUIDs and checks the GstRegistry for
204 * all the decryptors supporting one of the supplied UUIDs.
206 * Returns: (transfer full) (array zero-terminated=1) (nullable):
207 * A null terminated array containing all
208 * the @system_identifiers supported by the set of available decryptors, or
209 * %NULL if no matches were found.
214 gst_protection_filter_systems_by_available_decryptors (const gchar **
217 GList *decryptors, *walk;
219 guint i = 0, decryptors_number;
222 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
225 decryptors_number = g_list_length (decryptors);
227 GST_TRACE ("found %u decrytors", decryptors_number);
229 if (decryptors_number == 0)
232 retval = g_new (gchar *, decryptors_number + 1);
234 for (walk = decryptors; walk; walk = g_list_next (walk)) {
235 GstElementFactory *fact = (GstElementFactory *) walk->data;
236 const char *found_sys_id =
237 gst_protection_factory_check (fact, system_identifiers);
239 GST_DEBUG ("factory %s is valid for %s", GST_OBJECT_NAME (fact),
243 retval[i++] = g_strdup (found_sys_id);
248 if (retval[0] == NULL) {
253 gst_plugin_feature_list_free (decryptors);
259 gst_protection_factory_check (GstElementFactory * fact,
260 const gchar ** system_identifiers)
262 const GList *template, *walk;
263 const gchar *retval = NULL;
265 template = gst_element_factory_get_static_pad_templates (fact);
266 for (walk = template; walk && !retval; walk = g_list_next (walk)) {
267 GstStaticPadTemplate *templ = walk->data;
268 GstCaps *caps = gst_static_pad_template_get_caps (templ);
269 guint leng = gst_caps_get_size (caps);
272 for (i = 0; !retval && i < leng; ++i) {
275 st = gst_caps_get_structure (caps, i);
276 if (gst_structure_has_field_typed (st,
277 GST_PROTECTION_SYSTEM_ID_CAPS_FIELD, G_TYPE_STRING)) {
278 const gchar *sys_id =
279 gst_structure_get_string (st, GST_PROTECTION_SYSTEM_ID_CAPS_FIELD);
280 GST_DEBUG ("Found decryptor that supports protection system %s",
282 for (j = 0; !retval && system_identifiers[j]; ++j) {
283 GST_TRACE (" compare with %s", system_identifiers[j]);
284 if (g_ascii_strcasecmp (system_identifiers[j], sys_id) == 0) {
285 GST_DEBUG (" Selecting %s", system_identifiers[j]);
286 retval = system_identifiers[j];
291 gst_caps_unref (caps);