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
gst_protection_meta_api_get_type(void)57 gst_protection_meta_api_get_type (void)
58 {
59 static 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
gst_protection_meta_init(GstMeta * meta,gpointer params,GstBuffer * buffer)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
gst_protection_meta_free(GstMeta * meta,GstBuffer * buffer)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
gst_protection_meta_transform(GstBuffer * transbuf,GstMeta * meta,GstBuffer * buffer,GQuark type,gpointer data)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 *
gst_protection_meta_get_info(void)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
138 *
139 * Since: 1.6
140 */
141 GstProtectionMeta *
gst_buffer_add_protection_meta(GstBuffer * buffer,GstStructure * info)142 gst_buffer_add_protection_meta (GstBuffer * buffer, GstStructure * info)
143 {
144 GstProtectionMeta *meta;
145
146 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
147 g_return_val_if_fail (info != NULL, NULL);
148
149 meta =
150 (GstProtectionMeta *) gst_buffer_add_meta (buffer,
151 GST_PROTECTION_META_INFO, NULL);
152
153 meta->info = info;
154
155 return meta;
156 }
157
158 /**
159 * gst_protection_select_system:
160 * @system_identifiers: (transfer none) (array zero-terminated=1): A null terminated array of strings
161 * that contains the UUID values of each protection system that is to be
162 * checked.
163 *
164 * Iterates the supplied list of UUIDs and checks the GstRegistry for
165 * an element that supports one of the supplied UUIDs. If more than one
166 * element matches, the system ID of the highest ranked element is selected.
167 *
168 * Returns: (transfer none) (nullable): One of the strings from
169 * @system_identifiers that indicates the highest ranked element that
170 * implements the protection system indicated by that system ID, or %NULL if no
171 * element has been found.
172 *
173 * Since: 1.6
174 */
175 const gchar *
gst_protection_select_system(const gchar ** system_identifiers)176 gst_protection_select_system (const gchar ** system_identifiers)
177 {
178 GList *decryptors, *walk;
179 const gchar *retval = NULL;
180
181 decryptors =
182 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
183 GST_RANK_MARGINAL);
184
185 for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
186 GstElementFactory *fact = (GstElementFactory *) walk->data;
187
188 retval = gst_protection_factory_check (fact, system_identifiers);
189 }
190
191 gst_plugin_feature_list_free (decryptors);
192
193 return retval;
194 }
195
196 /**
197 * gst_protection_filter_systems_by_available_decryptors:
198 * @system_identifiers: (transfer none) (array zero-terminated=1):
199 * A null terminated array of strings that contains the UUID values of each
200 * protection system that is to be checked.
201 *
202 * Iterates the supplied list of UUIDs and checks the GstRegistry for
203 * all the decryptors supporting one of the supplied UUIDs.
204 *
205 * Returns: (transfer full) (array zero-terminated=1) (nullable):
206 * A null terminated array containing all
207 * the @system_identifiers supported by the set of available decryptors, or
208 * %NULL if no matches were found.
209 *
210 * Since: 1.14
211 */
212 gchar **
gst_protection_filter_systems_by_available_decryptors(const gchar ** system_identifiers)213 gst_protection_filter_systems_by_available_decryptors (const gchar **
214 system_identifiers)
215 {
216 GList *decryptors, *walk;
217 gchar **retval = NULL;
218 guint i = 0, decryptors_number;
219
220 decryptors =
221 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
222 GST_RANK_MARGINAL);
223
224 decryptors_number = g_list_length (decryptors);
225
226 GST_TRACE ("found %u decrytors", decryptors_number);
227
228 if (decryptors_number == 0)
229 goto beach;
230
231 retval = g_new (gchar *, decryptors_number + 1);
232
233 for (walk = decryptors; walk; walk = g_list_next (walk)) {
234 GstElementFactory *fact = (GstElementFactory *) walk->data;
235 const char *found_sys_id =
236 gst_protection_factory_check (fact, system_identifiers);
237
238 GST_DEBUG ("factory %s is valid for %s", GST_OBJECT_NAME (fact),
239 found_sys_id);
240
241 if (found_sys_id) {
242 retval[i++] = g_strdup (found_sys_id);
243 }
244 }
245 retval[i] = NULL;
246
247 if (retval[0] == NULL) {
248 g_free (retval);
249 retval = NULL;
250 }
251
252 beach:
253 gst_plugin_feature_list_free (decryptors);
254
255 return retval;
256 }
257
258 static const gchar *
gst_protection_factory_check(GstElementFactory * fact,const gchar ** system_identifiers)259 gst_protection_factory_check (GstElementFactory * fact,
260 const gchar ** system_identifiers)
261 {
262 const GList *template, *walk;
263 const gchar *retval = NULL;
264
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);
270 guint i, j;
271
272 for (i = 0; !retval && i < leng; ++i) {
273 GstStructure *st;
274
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",
281 sys_id);
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];
287 }
288 }
289 }
290 }
291 gst_caps_unref (caps);
292 }
293
294 return retval;
295 }
296