01b17a5d91cf165561e39fc74d4959e596a17d87
[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  * @short_description: Functions and classes to support encrypted streams.
23  *
24  * The GstProtectionMeta class enables the information needed to decrypt a
25  * #GstBuffer to be attached to that buffer.
26  *
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
35  * frame.
36  *
37  * Since: 1.6
38  */
39
40 #include "gst_private.h"
41 #include "glib-compat-private.h"
42
43 #include "gstprotection.h"
44
45 #define GST_CAT_DEFAULT GST_CAT_PROTECTION
46
47 static gboolean gst_protection_meta_init (GstMeta * meta, gpointer params,
48     GstBuffer * buffer);
49
50 static void gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer);
51
52 static const gchar *gst_protection_factory_check (GstElementFactory * fact,
53     const gchar ** system_identifiers);
54
55 GType
56 gst_protection_meta_api_get_type (void)
57 {
58   static volatile GType type;
59   static const gchar *tags[] = { NULL };
60
61   if (g_once_init_enter (&type)) {
62     GType _type = gst_meta_api_type_register ("GstProtectionMetaAPI", tags);
63     g_once_init_leave (&type, _type);
64   }
65   return type;
66 }
67
68 static gboolean
69 gst_protection_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
70 {
71   GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
72
73   protection_meta->info = NULL;
74
75   return TRUE;
76 }
77
78 static void
79 gst_protection_meta_free (GstMeta * meta, GstBuffer * buffer)
80 {
81   GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
82
83   if (protection_meta->info)
84     gst_structure_free (protection_meta->info);
85 }
86
87 static gboolean
88 gst_protection_meta_transform (GstBuffer * transbuf, GstMeta * meta,
89     GstBuffer * buffer, GQuark type, gpointer data)
90 {
91   GstProtectionMeta *protection_meta = (GstProtectionMeta *) meta;
92
93   if (GST_META_TRANSFORM_IS_COPY (type)) {
94     GstMetaTransformCopy *copy = data;
95     if (!copy->region) {
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));
99     } else {
100       return FALSE;
101     }
102   } else {
103     /* transform type not supported */
104     return FALSE;
105   }
106   return TRUE;
107 }
108
109 const GstMetaInfo *
110 gst_protection_meta_get_info (void)
111 {
112   static const GstMetaInfo *protection_meta_info = NULL;
113
114   if (g_once_init_enter ((GstMetaInfo **) & 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);
119
120     g_once_init_leave ((GstMetaInfo **) & protection_meta_info,
121         (GstMetaInfo *) meta);
122   }
123   return protection_meta_info;
124 }
125
126 /**
127  * gst_buffer_add_protection_meta:
128  * @buffer: #GstBuffer holding an encrypted sample, to which protection
129  *     metadata should be added.
130  * @info: (transfer full): a #GstStructure holding cryptographic
131  *     information relating to the sample contained in @buffer. This
132  *     function takes ownership of @info.
133  *
134  * Attaches protection metadata to a #GstBuffer.
135  *
136  * Returns: (transfer none): a pointer to the added #GstProtectionMeta if successful; %NULL if
137  * unsuccessful.
138  *
139  * Since: 1.6
140  */
141 GstProtectionMeta *
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): 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): One of the strings from @system_identifiers that
169  * indicates the highest ranked element that implements the protection system
170  * indicated by that system ID, or %NULL if no element has been found.
171  *
172  * Since: 1.6
173  */
174 const gchar *
175 gst_protection_select_system (const gchar ** system_identifiers)
176 {
177   GList *decryptors, *walk;
178   const gchar *retval = NULL;
179
180   decryptors =
181       gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
182       GST_RANK_MARGINAL);
183
184   for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
185     GstElementFactory *fact = (GstElementFactory *) walk->data;
186
187     retval = gst_protection_factory_check (fact, system_identifiers);
188   }
189
190   gst_plugin_feature_list_free (decryptors);
191
192   return retval;
193 }
194
195 static const gchar *
196 gst_protection_factory_check (GstElementFactory * fact,
197     const gchar ** system_identifiers)
198 {
199   const GList *template, *walk;
200   const gchar *retval = NULL;
201
202   template = gst_element_factory_get_static_pad_templates (fact);
203   for (walk = template; walk && !retval; walk = g_list_next (walk)) {
204     GstStaticPadTemplate *templ = walk->data;
205     GstCaps *caps = gst_static_pad_template_get_caps (templ);
206     guint leng = gst_caps_get_size (caps);
207     guint i, j;
208
209     for (i = 0; !retval && i < leng; ++i) {
210       GstStructure *st;
211
212       st = gst_caps_get_structure (caps, i);
213       if (gst_structure_has_field_typed (st,
214               GST_PROTECTION_SYSTEM_ID_CAPS_FIELD, G_TYPE_STRING)) {
215         const gchar *sys_id =
216             gst_structure_get_string (st, GST_PROTECTION_SYSTEM_ID_CAPS_FIELD);
217         GST_DEBUG ("Found decryptor that supports protection system %s",
218             sys_id);
219         for (j = 0; !retval && system_identifiers[j]; ++j) {
220           GST_TRACE ("  compare with %s", system_identifiers[j]);
221           if (g_ascii_strcasecmp (system_identifiers[j], sys_id) == 0) {
222             GST_DEBUG ("  Selecting %s", system_identifiers[j]);
223             retval = system_identifiers[j];
224           }
225         }
226       }
227     }
228     gst_caps_unref (caps);
229   }
230
231   return retval;
232 }