gst/parse: Also pass -DGST_EXPORTS here
[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 (&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 (&protection_meta_info, meta);
121   }
122   return protection_meta_info;
123 }
124
125 /**
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.
132  *
133  * Attaches protection metadata to a #GstBuffer.
134  *
135  * Returns: (transfer none): a pointer to the added #GstProtectionMeta if successful; %NULL if
136  * unsuccessful.
137  *
138  * Since: 1.6
139  */
140 GstProtectionMeta *
141 gst_buffer_add_protection_meta (GstBuffer * buffer, GstStructure * info)
142 {
143   GstProtectionMeta *meta;
144
145   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
146   g_return_val_if_fail (info != NULL, NULL);
147
148   meta =
149       (GstProtectionMeta *) gst_buffer_add_meta (buffer,
150       GST_PROTECTION_META_INFO, NULL);
151
152   meta->info = info;
153
154   return meta;
155 }
156
157 /**
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
161  * checked.
162  *
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.
166  *
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.
170  *
171  * Since: 1.6
172  */
173 const gchar *
174 gst_protection_select_system (const gchar ** system_identifiers)
175 {
176   GList *decryptors, *walk;
177   const gchar *retval = NULL;
178
179   decryptors =
180       gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
181       GST_RANK_MARGINAL);
182
183   for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
184     GstElementFactory *fact = (GstElementFactory *) walk->data;
185
186     retval = gst_protection_factory_check (fact, system_identifiers);
187   }
188
189   gst_plugin_feature_list_free (decryptors);
190
191   return retval;
192 }
193
194 static const gchar *
195 gst_protection_factory_check (GstElementFactory * fact,
196     const gchar ** system_identifiers)
197 {
198   const GList *template, *walk;
199   const gchar *retval = NULL;
200
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);
206     guint i, j;
207
208     for (i = 0; !retval && i < leng; ++i) {
209       GstStructure *st;
210
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",
217             sys_id);
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];
223           }
224         }
225       }
226     }
227     gst_caps_unref (caps);
228   }
229
230   return retval;
231 }