36f36adf6d454d92fdfd47d8fac2767b6f57dc00
[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 const GstMetaInfo *
88 gst_protection_meta_get_info (void)
89 {
90   static const GstMetaInfo *protection_meta_info = NULL;
91
92   if (g_once_init_enter (&protection_meta_info)) {
93     const GstMetaInfo *meta =
94         gst_meta_register (GST_PROTECTION_META_API_TYPE, "GstProtectionMeta",
95         sizeof (GstProtectionMeta), gst_protection_meta_init,
96         gst_protection_meta_free,
97         (GstMetaTransformFunction) NULL);
98
99     g_once_init_leave (&protection_meta_info, meta);
100   }
101   return protection_meta_info;
102 }
103
104 /**
105  * gst_buffer_add_protection_meta:
106  * @buffer: #GstBuffer holding an encrypted sample, to which protection
107  *     metadata should be added.
108  * @info: (transfer full): a #GstStructure holding cryptographic
109  *     information relating to the sample contained in @buffer. This
110  *     function takes ownership of @info.
111  *
112  * Attaches protection metadata to a #GstBuffer.
113  *
114  * Returns: a pointer to the added #GstProtectionMeta if successful; %NULL if
115  * unsuccessful.
116  *
117  * Since: 1.6
118  */
119 GstProtectionMeta *
120 gst_buffer_add_protection_meta (GstBuffer * buffer, GstStructure * info)
121 {
122   GstProtectionMeta *meta;
123
124   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
125   g_return_val_if_fail (info != NULL, NULL);
126
127   meta =
128       (GstProtectionMeta *) gst_buffer_add_meta (buffer,
129       GST_PROTECTION_META_INFO, NULL);
130
131   meta->info = info;
132
133   return meta;
134 }
135
136 /**
137  * gst_protection_select_system:
138  * @system_identifiers: (transfer none): A null terminated array of strings
139  * that contains the UUID values of each protection system that is to be
140  * checked.
141  *
142  * Iterates the supplied list of UUIDs and checks the GstRegistry for
143  * an element that supports one of the supplied UUIDs. If more than one
144  * element matches, the system ID of the highest ranked element is selected.
145  *
146  * Returns: (transfer none): One of the strings from @system_identifiers that
147  * indicates the highest ranked element that implements the protection system
148  * indicated by that system ID, or %NULL if no element has been found.
149  *
150  * Since: 1.6
151  */
152 const gchar *
153 gst_protection_select_system (const gchar ** system_identifiers)
154 {
155   GList *decryptors, *walk;
156   const gchar *retval = NULL;
157
158   decryptors =
159       gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECRYPTOR,
160       GST_RANK_MARGINAL);
161
162   for (walk = decryptors; !retval && walk; walk = g_list_next (walk)) {
163     GstElementFactory *fact = (GstElementFactory *) walk->data;
164
165     retval = gst_protection_factory_check (fact, system_identifiers);
166   }
167
168   gst_plugin_feature_list_free (decryptors);
169
170   return retval;
171 }
172
173 static const gchar *
174 gst_protection_factory_check (GstElementFactory * fact,
175     const gchar ** system_identifiers)
176 {
177   const GList *template, *walk;
178   const gchar *retval = NULL;
179
180   template = gst_element_factory_get_static_pad_templates (fact);
181   for (walk = template; walk && !retval; walk = g_list_next (walk)) {
182     GstStaticPadTemplate *templ = walk->data;
183     GstCaps *caps = gst_static_pad_template_get_caps (templ);
184     guint leng = gst_caps_get_size (caps);
185
186     for (guint i = 0; !retval && i < leng; ++i) {
187       GstStructure *st;
188
189       st = gst_caps_get_structure (caps, i);
190       if (gst_structure_has_field_typed (st, PROTECTION_SYSTEM_ID_CAPS_FIELD,
191               G_TYPE_STRING)) {
192         const gchar *sys_id =
193             gst_structure_get_string (st, PROTECTION_SYSTEM_ID_CAPS_FIELD);
194         GST_DEBUG ("Found decryptor that supports protection system %s",
195             sys_id);
196         for (guint j = 0; !retval && system_identifiers[j]; ++j) {
197           GST_TRACE ("  compare with %s", system_identifiers[j]);
198           if (g_ascii_strcasecmp (system_identifiers[j], sys_id) == 0) {
199             GST_DEBUG ("  Selecting %s", system_identifiers[j]);
200             retval = system_identifiers[j];
201           }
202         }
203       }
204     }
205     gst_caps_unref (caps);
206   }
207
208   return retval;
209 }