gstpad: Probes that return HANDLED can reset the data info field
[platform/upstream/gstreamer.git] / gst / gstprotection.c
1 /* GStreamer
2  * Copyright (C) <2013> YouView TV Ltd.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:gstprotection
22  * @title: GstProtection
23  * @short_description: Functions and classes to support encrypted streams.
24  *
25  * The GstProtectionMeta class enables the information needed to decrypt a
26  * #GstBuffer to be attached to that buffer.
27  *
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
36  * frame.
37  *
38  * Since: 1.6
39  */
40
41 #include "gst_private.h"
42 #include "glib-compat-private.h"
43
44 #include "gstprotection.h"
45
46 #define GST_CAT_DEFAULT GST_CAT_PROTECTION
47
48 static gboolean gst_protection_meta_init (GstMeta * meta, gpointer params,
49     GstBuffer * buffer);
50
51 static void gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer);
52
53 static const gchar *gst_protection_factory_check (GstElementFactory * fact,
54     const gchar ** system_identifiers);
55
56 GType
57 gst_protection_meta_api_get_type (void)
58 {
59   static volatile GType type;
60   static const gchar *tags[] = { NULL };
61
62   if (g_once_init_enter (&type)) {
63     GType _type = gst_meta_api_type_register ("GstProtectionMetaAPI", tags);
64     g_once_init_leave (&type, _type);
65   }
66   return type;
67 }
68
69 static gboolean
70 gst_protection_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
71 {
72   GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
73
74   protection_meta->info = NULL;
75
76   return TRUE;
77 }
78
79 static void
80 gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer)
81 {
82   GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
83
84   if (protection_meta->info)
85     gst_structure_free (protection_meta->info);
86 }
87
88 static gboolean
89 gst_protection_meta_transform (GstBuffer * transbuf, GstMeta * meta,
90     GstBuffer * buffer, GQuark type, gpointer data)
91 {
92   GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
93
94   if (GST_META_TRANSFORM_IS_COPY (type)) {
95     GstMetaTransformCopy *copy = data;
96     if (!copy->region) {
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));
100     } else {
101       return FALSE;
102     }
103   } else {
104     /* transform type not supported */
105     return FALSE;
106   }
107   return TRUE;
108 }
109
110 const GstMetaInfo *
111 gst_protection_meta_get_info (void)
112 {
113   static const GstMetaInfo *protection_meta_info = NULL;
114
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);
120
121     g_once_init_leave ((GstMetaInfo **) & protection_meta_info,
122         (GstMetaInfo *) meta);
123   }
124   return protection_meta_info;
125 }
126
127 /**
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.
134  *
135  * Attaches protection metadata to a #GstBuffer.
136  *
137  * Returns: (transfer none): a pointer to the added #GstProtectionMeta if successful; %NULL if
138  * unsuccessful.
139  *
140  * Since: 1.6
141  */
142 GstProtectionMeta *
143 gst_buffer_add_protection_meta (GstBuffer * buffer, GstStructure * info)
144 {
145   GstProtectionMeta *meta;
146
147   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
148   g_return_val_if_fail (info != NULL, NULL);
149
150   meta =
151       (GstProtectionMeta *) gst_buffer_add_meta (buffer,
152       GST_PROTECTION_META_INFO, NULL);
153
154   meta->info = info;
155
156   return meta;
157 }
158
159 /**
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
163  * checked.
164  *
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.
168  *
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.
173  *
174  * Since: 1.6
175  */
176 const gchar *
177 gst_protection_select_system (const gchar ** system_identifiers)
178 {
179   GList *decryptors, *walk;
180   const gchar *retval = NULL;
181
182   decryptors =
183       gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
184       GST_RANK_MARGINAL);
185
186   for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
187     GstElementFactory *fact = (GstElementFactory *) walk->data;
188
189     retval = gst_protection_factory_check (fact, system_identifiers);
190   }
191
192   gst_plugin_feature_list_free (decryptors);
193
194   return retval;
195 }
196
197 /**
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.
202  *
203  * Iterates the supplied list of UUIDs and checks the GstRegistry for
204  * all the decryptors supporting one of the supplied UUIDs.
205  *
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.
210  *
211  * Since: 1.14
212  */
213 gchar **
214 gst_protection_filter_systems_by_available_decryptors (const gchar **
215     system_identifiers)
216 {
217   GList *decryptors, *walk;
218   gchar **retval = NULL;
219   guint i = 0, decryptors_number;
220
221   decryptors =
222       gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
223       GST_RANK_MARGINAL);
224
225   decryptors_number = g_list_length (decryptors);
226
227   GST_TRACE ("found %u decrytors", decryptors_number);
228
229   if (decryptors_number == 0)
230     goto beach;
231
232   retval = g_new (gchar *, decryptors_number + 1);
233
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);
238
239     GST_DEBUG ("factory %s is valid for %s", GST_OBJECT_NAME (fact),
240         found_sys_id);
241
242     if (found_sys_id) {
243       retval[i++] = g_strdup (found_sys_id);
244     }
245   }
246   retval[i] = NULL;
247
248   if (retval[0] == NULL) {
249     g_free (retval);
250     retval = NULL;
251   }
252
253 beach:
254   gst_plugin_feature_list_free (decryptors);
255
256   return retval;
257 }
258
259 static const gchar *
260 gst_protection_factory_check (GstElementFactory * fact,
261     const gchar ** system_identifiers)
262 {
263   const GList *template, *walk;
264   const gchar *retval = NULL;
265
266   template = gst_element_factory_get_static_pad_templates (fact);
267   for (walk = template; walk && !retval; walk = g_list_next (walk)) {
268     GstStaticPadTemplate *templ = walk->data;
269     GstCaps *caps = gst_static_pad_template_get_caps (templ);
270     guint leng = gst_caps_get_size (caps);
271     guint i, j;
272
273     for (i = 0; !retval && i < leng; ++i) {
274       GstStructure *st;
275
276       st = gst_caps_get_structure (caps, i);
277       if (gst_structure_has_field_typed (st,
278               GST_PROTECTION_SYSTEM_ID_CAPS_FIELD, G_TYPE_STRING)) {
279         const gchar *sys_id =
280             gst_structure_get_string (st, GST_PROTECTION_SYSTEM_ID_CAPS_FIELD);
281         GST_DEBUG ("Found decryptor that supports protection system %s",
282             sys_id);
283         for (j = 0; !retval && system_identifiers[j]; ++j) {
284           GST_TRACE ("  compare with %s", system_identifiers[j]);
285           if (g_ascii_strcasecmp (system_identifiers[j], sys_id) == 0) {
286             GST_DEBUG ("  Selecting %s", system_identifiers[j]);
287             retval = system_identifiers[j];
288           }
289         }
290       }
291     }
292     gst_caps_unref (caps);
293   }
294
295   return retval;
296 }