Port gtk-doc comments to their equivalent markdown syntax
[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): 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): One of the strings from @system_identifiers that
170  * indicates the highest ranked element that implements the protection system
171  * indicated by that system ID, or %NULL if no element has been found.
172  *
173  * Since: 1.6
174  */
175 const gchar *
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 static const gchar *
197 gst_protection_factory_check (GstElementFactory * fact,
198     const gchar ** system_identifiers)
199 {
200   const GList *template, *walk;
201   const gchar *retval = NULL;
202
203   template = gst_element_factory_get_static_pad_templates (fact);
204   for (walk = template; walk && !retval; walk = g_list_next (walk)) {
205     GstStaticPadTemplate *templ = walk->data;
206     GstCaps *caps = gst_static_pad_template_get_caps (templ);
207     guint leng = gst_caps_get_size (caps);
208     guint i, j;
209
210     for (i = 0; !retval && i < leng; ++i) {
211       GstStructure *st;
212
213       st = gst_caps_get_structure (caps, i);
214       if (gst_structure_has_field_typed (st,
215               GST_PROTECTION_SYSTEM_ID_CAPS_FIELD, G_TYPE_STRING)) {
216         const gchar *sys_id =
217             gst_structure_get_string (st, GST_PROTECTION_SYSTEM_ID_CAPS_FIELD);
218         GST_DEBUG ("Found decryptor that supports protection system %s",
219             sys_id);
220         for (j = 0; !retval && system_identifiers[j]; ++j) {
221           GST_TRACE ("  compare with %s", system_identifiers[j]);
222           if (g_ascii_strcasecmp (system_identifiers[j], sys_id) == 0) {
223             GST_DEBUG ("  Selecting %s", system_identifiers[j]);
224             retval = system_identifiers[j];
225           }
226         }
227       }
228     }
229     gst_caps_unref (caps);
230   }
231
232   return retval;
233 }