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_transform (GstBuffer * transbuf, GstMeta * meta,
89 GstBuffer * buffer, GQuark type, gpointer data)
91 GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
93 if (GST_META_TRANSFORM_IS_COPY (type)) {
94 GstMetaTransformCopy *copy = data;
96 /* only copy if the complete data is copied as well */
97 gst_buffer_add_protection_meta (transbuf,
98 gst_structure_copy (protection_meta->info));
103 /* transform type not supported */
110 gst_protection_meta_get_info (void)
112 static const GstMetaInfo *protection_meta_info = NULL;
114 if (g_once_init_enter (&protection_meta_info)) {
115 const GstMetaInfo *meta =
116 gst_meta_register (GST_PROTECTION_META_API_TYPE, "GstProtectionMeta",
117 sizeof (GstProtectionMeta), gst_protection_meta_init,
118 gst_protection_meta_free, gst_protection_meta_transform);
120 g_once_init_leave (&protection_meta_info, meta);
122 return protection_meta_info;
126 * gst_buffer_add_protection_meta:
127 * @buffer: #GstBuffer holding an encrypted sample, to which protection
128 * metadata should be added.
129 * @info: (transfer full): a #GstStructure holding cryptographic
130 * information relating to the sample contained in @buffer. This
131 * function takes ownership of @info.
133 * Attaches protection metadata to a #GstBuffer.
135 * Returns: (transfer none): a pointer to the added #GstProtectionMeta if successful; %NULL if
141 gst_buffer_add_protection_meta (GstBuffer * buffer, GstStructure * info)
143 GstProtectionMeta *meta;
145 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
146 g_return_val_if_fail (info != NULL, NULL);
149 (GstProtectionMeta *) gst_buffer_add_meta (buffer,
150 GST_PROTECTION_META_INFO, NULL);
158 * gst_protection_select_system:
159 * @system_identifiers: (transfer none): A null terminated array of strings
160 * that contains the UUID values of each protection system that is to be
163 * Iterates the supplied list of UUIDs and checks the GstRegistry for
164 * an element that supports one of the supplied UUIDs. If more than one
165 * element matches, the system ID of the highest ranked element is selected.
167 * Returns: (transfer none): One of the strings from @system_identifiers that
168 * indicates the highest ranked element that implements the protection system
169 * indicated by that system ID, or %NULL if no element has been found.
174 gst_protection_select_system (const gchar ** system_identifiers)
176 GList *decryptors, *walk;
177 const gchar *retval = NULL;
180 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
183 for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
184 GstElementFactory *fact = (GstElementFactory *) walk->data;
186 retval = gst_protection_factory_check (fact, system_identifiers);
189 gst_plugin_feature_list_free (decryptors);
195 gst_protection_factory_check (GstElementFactory * fact,
196 const gchar ** system_identifiers)
198 const GList *template, *walk;
199 const gchar *retval = NULL;
201 template = gst_element_factory_get_static_pad_templates (fact);
202 for (walk = template; walk && !retval; walk = g_list_next (walk)) {
203 GstStaticPadTemplate *templ = walk->data;
204 GstCaps *caps = gst_static_pad_template_get_caps (templ);
205 guint leng = gst_caps_get_size (caps);
208 for (i = 0; !retval && i < leng; ++i) {
211 st = gst_caps_get_structure (caps, i);
212 if (gst_structure_has_field_typed (st,
213 GST_PROTECTION_SYSTEM_ID_CAPS_FIELD, G_TYPE_STRING)) {
214 const gchar *sys_id =
215 gst_structure_get_string (st, GST_PROTECTION_SYSTEM_ID_CAPS_FIELD);
216 GST_DEBUG ("Found decryptor that supports protection system %s",
218 for (j = 0; !retval && system_identifiers[j]; ++j) {
219 GST_TRACE (" compare with %s", system_identifiers[j]);
220 if (g_ascii_strcasecmp (system_identifiers[j], sys_id) == 0) {
221 GST_DEBUG (" Selecting %s", system_identifiers[j]);
222 retval = system_identifiers[j];
227 gst_caps_unref (caps);