[GStreamer][EME] Base class for decryption support
[WebKit-https.git] / Source / WebCore / platform / graphics / gstreamer / eme / WebKitCommonEncryptionDecryptorGStreamer.cpp
1 /* GStreamer ClearKey common encryption decryptor
2  *
3  * Copyright (C) 2013 YouView TV Ltd. <alex.ashley@youview.com>
4  * Copyright (C) 2016 Metrological
5  * Copyright (C) 2016 Igalia S.L
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
20  * Boston, MA 02110-1335, USA.
21  */
22
23 #include "config.h"
24 #include "WebKitCommonEncryptionDecryptorGStreamer.h"
25
26 #if ENABLE(LEGACY_ENCRYPTED_MEDIA) && USE(GSTREAMER)
27
28 #include "GRefPtrGStreamer.h"
29 #include <wtf/Condition.h>
30 #include <wtf/RunLoop.h>
31
32 #define WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_MEDIA_CENC_DECRYPT, WebKitMediaCommonEncryptionDecryptPrivate))
33 struct _WebKitMediaCommonEncryptionDecryptPrivate {
34     GRefPtr<GstEvent> protectionEvent;
35
36     bool keyReceived;
37     Lock mutex;
38     Condition condition;
39 };
40
41 static GstStateChangeReturn webKitMediaCommonEncryptionDecryptorChangeState(GstElement*, GstStateChange transition);
42 static void webKitMediaCommonEncryptionDecryptorFinalize(GObject*);
43 static GstCaps* webkitMediaCommonEncryptionDecryptTransformCaps(GstBaseTransform*, GstPadDirection, GstCaps*, GstCaps*);
44 static GstFlowReturn webkitMediaCommonEncryptionDecryptTransformInPlace(GstBaseTransform*, GstBuffer*);
45 static gboolean webkitMediaCommonEncryptionDecryptSinkEventHandler(GstBaseTransform*, GstEvent*);
46
47 static gboolean webKitMediaCommonEncryptionDecryptDefaultSetupCipher(WebKitMediaCommonEncryptionDecrypt*);
48 static void webKitMediaCommonEncryptionDecryptDefaultReleaseCipher(WebKitMediaCommonEncryptionDecrypt*);
49
50 GST_DEBUG_CATEGORY_STATIC(webkit_media_common_encryption_decrypt_debug_category);
51 #define GST_CAT_DEFAULT webkit_media_common_encryption_decrypt_debug_category
52
53 #define webkit_media_common_encryption_decrypt_parent_class parent_class
54 G_DEFINE_TYPE(WebKitMediaCommonEncryptionDecrypt, webkit_media_common_encryption_decrypt, GST_TYPE_BASE_TRANSFORM);
55
56 static void webkit_media_common_encryption_decrypt_class_init(WebKitMediaCommonEncryptionDecryptClass* klass)
57 {
58     GObjectClass* gobjectClass = G_OBJECT_CLASS(klass);
59     gobjectClass->finalize = webKitMediaCommonEncryptionDecryptorFinalize;
60
61     GST_DEBUG_CATEGORY_INIT(webkit_media_common_encryption_decrypt_debug_category,
62         "webkitcenc", 0, "Common Encryption base class");
63
64     GstElementClass* elementClass = GST_ELEMENT_CLASS(klass);
65     elementClass->change_state = GST_DEBUG_FUNCPTR(webKitMediaCommonEncryptionDecryptorChangeState);
66
67     GstBaseTransformClass* baseTransformClass = GST_BASE_TRANSFORM_CLASS(klass);
68     baseTransformClass->transform_ip = GST_DEBUG_FUNCPTR(webkitMediaCommonEncryptionDecryptTransformInPlace);
69     baseTransformClass->transform_caps = GST_DEBUG_FUNCPTR(webkitMediaCommonEncryptionDecryptTransformCaps);
70     baseTransformClass->transform_ip_on_passthrough = FALSE;
71     baseTransformClass->sink_event = GST_DEBUG_FUNCPTR(webkitMediaCommonEncryptionDecryptSinkEventHandler);
72
73     klass->setupCipher = GST_DEBUG_FUNCPTR(webKitMediaCommonEncryptionDecryptDefaultSetupCipher);
74     klass->releaseCipher = GST_DEBUG_FUNCPTR(webKitMediaCommonEncryptionDecryptDefaultReleaseCipher);
75
76     g_type_class_add_private(klass, sizeof(WebKitMediaCommonEncryptionDecryptPrivate));
77 }
78
79 static void webkit_media_common_encryption_decrypt_init(WebKitMediaCommonEncryptionDecrypt* self)
80 {
81     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
82
83     self->priv = priv;
84     new (priv) WebKitMediaCommonEncryptionDecryptPrivate();
85
86     GstBaseTransform* base = GST_BASE_TRANSFORM(self);
87     gst_base_transform_set_in_place(base, TRUE);
88     gst_base_transform_set_passthrough(base, FALSE);
89     gst_base_transform_set_gap_aware(base, FALSE);
90 }
91
92 static void webKitMediaCommonEncryptionDecryptorFinalize(GObject* object)
93 {
94     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(object);
95     WebKitMediaCommonEncryptionDecryptPrivate* priv = self->priv;
96
97     priv->~WebKitMediaCommonEncryptionDecryptPrivate();
98     GST_CALL_PARENT(G_OBJECT_CLASS, finalize, (object));
99 }
100
101 static GstCaps* webkitMediaCommonEncryptionDecryptTransformCaps(GstBaseTransform* base, GstPadDirection direction, GstCaps* caps, GstCaps* filter)
102 {
103     if (direction == GST_PAD_UNKNOWN)
104         return nullptr;
105
106     GST_DEBUG_OBJECT(base, "direction: %s, caps: %" GST_PTR_FORMAT " filter: %" GST_PTR_FORMAT, (direction == GST_PAD_SRC) ? "src" : "sink", caps, filter);
107
108     GstCaps* transformedCaps = gst_caps_new_empty();
109     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(base);
110     WebKitMediaCommonEncryptionDecryptClass* klass = WEBKIT_MEDIA_CENC_DECRYPT_GET_CLASS(self);
111
112     unsigned size = gst_caps_get_size(caps);
113     for (unsigned i = 0; i < size; ++i) {
114         GstStructure* incomingStructure = gst_caps_get_structure(caps, i);
115         GRefPtr<GstStructure> outgoingStructure = nullptr;
116
117         if (direction == GST_PAD_SINK) {
118             if (!gst_structure_has_field(incomingStructure, "original-media-type"))
119                 continue;
120
121             outgoingStructure = adoptGRef(gst_structure_copy(incomingStructure));
122             gst_structure_set_name(outgoingStructure.get(), gst_structure_get_string(outgoingStructure.get(), "original-media-type"));
123
124             // Filter out the DRM related fields from the down-stream caps.
125             for (int j = 0; j < gst_structure_n_fields(incomingStructure); ++j) {
126                 const gchar* fieldName = gst_structure_nth_field_name(incomingStructure, j);
127
128                 if (g_str_has_prefix(fieldName, "protection-system")
129                     || g_str_has_prefix(fieldName, "original-media-type"))
130                     gst_structure_remove_field(outgoingStructure.get(), fieldName);
131             }
132         } else {
133             outgoingStructure = adoptGRef(gst_structure_copy(incomingStructure));
134             // Filter out the video related fields from the up-stream caps,
135             // because they are not relevant to the input caps of this element and
136             // can cause caps negotiation failures with adaptive bitrate streams.
137             for (int index = gst_structure_n_fields(outgoingStructure.get()) - 1; index >= 0; --index) {
138                 const gchar* fieldName = gst_structure_nth_field_name(outgoingStructure.get(), index);
139                 GST_TRACE("Check field \"%s\" for removal", fieldName);
140
141                 if (!g_strcmp0(fieldName, "base-profile")
142                     || !g_strcmp0(fieldName, "codec_data")
143                     || !g_strcmp0(fieldName, "height")
144                     || !g_strcmp0(fieldName, "framerate")
145                     || !g_strcmp0(fieldName, "level")
146                     || !g_strcmp0(fieldName, "pixel-aspect-ratio")
147                     || !g_strcmp0(fieldName, "profile")
148                     || !g_strcmp0(fieldName, "rate")
149                     || !g_strcmp0(fieldName, "width")) {
150                     gst_structure_remove_field(outgoingStructure.get(), fieldName);
151                     GST_TRACE("Removing field %s", fieldName);
152                 }
153             }
154
155             gst_structure_set(outgoingStructure.get(), "protection-system", G_TYPE_STRING, klass->protectionSystemId,
156                 "original-media-type", G_TYPE_STRING, gst_structure_get_name(incomingStructure), nullptr);
157
158             gst_structure_set_name(outgoingStructure.get(), "application/x-cenc");
159         }
160
161         bool duplicate = false;
162         unsigned size = gst_caps_get_size(transformedCaps);
163
164         for (unsigned index = 0; !duplicate && index < size; ++index) {
165             GstStructure* structure = gst_caps_get_structure(transformedCaps, index);
166             if (gst_structure_is_equal(structure, outgoingStructure.get()))
167                 duplicate = true;
168         }
169
170         if (!duplicate)
171             gst_caps_append_structure(transformedCaps, outgoingStructure.leakRef());
172     }
173
174     if (filter) {
175         GstCaps* intersection;
176
177         GST_DEBUG_OBJECT(base, "Using filter caps %" GST_PTR_FORMAT, filter);
178         intersection = gst_caps_intersect_full(transformedCaps, filter, GST_CAPS_INTERSECT_FIRST);
179         gst_caps_unref(transformedCaps);
180         transformedCaps = intersection;
181     }
182
183     GST_DEBUG_OBJECT(base, "returning %" GST_PTR_FORMAT, transformedCaps);
184     return transformedCaps;
185 }
186
187 static GstFlowReturn webkitMediaCommonEncryptionDecryptTransformInPlace(GstBaseTransform* base, GstBuffer* buffer)
188 {
189     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(base);
190     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
191     LockHolder locker(priv->mutex);
192
193     // The key might not have been received yet. Wait for it.
194     if (!priv->keyReceived) {
195         GST_DEBUG_OBJECT(self, "key not available yet, waiting for it");
196         if (GST_STATE(GST_ELEMENT(self)) < GST_STATE_PAUSED || (GST_STATE_TARGET(GST_ELEMENT(self)) != GST_STATE_VOID_PENDING && GST_STATE_TARGET(GST_ELEMENT(self)) < GST_STATE_PAUSED)) {
197             GST_ERROR_OBJECT(self, "can't process key requests in less than PAUSED state");
198             return GST_FLOW_NOT_SUPPORTED;
199         }
200         priv->condition.waitFor(priv->mutex, std::chrono::seconds(5), [priv] {
201             return priv->keyReceived;
202         });
203         if (!priv->keyReceived) {
204             GST_ERROR_OBJECT(self, "key not available");
205             return GST_FLOW_NOT_SUPPORTED;
206         }
207         GST_DEBUG_OBJECT(self, "key received, continuing");
208     }
209
210     GstProtectionMeta* protectionMeta = reinterpret_cast<GstProtectionMeta*>(gst_buffer_get_protection_meta(buffer));
211     if (!protectionMeta) {
212         GST_ERROR_OBJECT(self, "Failed to get GstProtection metadata from buffer %p", buffer);
213         return GST_FLOW_NOT_SUPPORTED;
214     }
215
216     unsigned ivSize;
217     if (!gst_structure_get_uint(protectionMeta->info, "iv_size", &ivSize)) {
218         GST_ERROR_OBJECT(self, "Failed to get iv_size");
219         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
220         return GST_FLOW_NOT_SUPPORTED;
221     }
222
223     gboolean encrypted;
224     if (!gst_structure_get_boolean(protectionMeta->info, "encrypted", &encrypted)) {
225         GST_ERROR_OBJECT(self, "Failed to get encrypted flag");
226         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
227         return GST_FLOW_NOT_SUPPORTED;
228     }
229
230     if (!ivSize || !encrypted) {
231         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
232         return GST_FLOW_OK;
233     }
234
235     GST_DEBUG_OBJECT(base, "protection meta: %" GST_PTR_FORMAT, protectionMeta->info);
236
237     unsigned subSampleCount;
238     if (!gst_structure_get_uint(protectionMeta->info, "subsample_count", &subSampleCount)) {
239         GST_ERROR_OBJECT(self, "Failed to get subsample_count");
240         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
241         return GST_FLOW_NOT_SUPPORTED;
242     }
243
244     const GValue* value;
245     GstBuffer* subSamplesBuffer = nullptr;
246     if (subSampleCount) {
247         value = gst_structure_get_value(protectionMeta->info, "subsamples");
248         if (!value) {
249             GST_ERROR_OBJECT(self, "Failed to get subsamples");
250             gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
251             return GST_FLOW_NOT_SUPPORTED;
252         }
253         subSamplesBuffer = gst_value_get_buffer(value);
254     }
255
256     WebKitMediaCommonEncryptionDecryptClass* klass = WEBKIT_MEDIA_CENC_DECRYPT_GET_CLASS(self);
257     if (!klass->setupCipher(self)) {
258         GST_ERROR_OBJECT(self, "Failed to configure cipher");
259         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
260         return GST_FLOW_NOT_SUPPORTED;
261     }
262
263     value = gst_structure_get_value(protectionMeta->info, "iv");
264     if (!value) {
265         GST_ERROR_OBJECT(self, "Failed to get IV for sample");
266         klass->releaseCipher(self);
267         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
268         return GST_FLOW_NOT_SUPPORTED;
269     }
270
271     GstBuffer* ivBuffer = gst_value_get_buffer(value);
272     GST_TRACE_OBJECT(self, "decrypting");
273     if (!klass->decrypt(self, ivBuffer, buffer, subSampleCount, subSamplesBuffer)) {
274         GST_ERROR_OBJECT(self, "Decryption failed");
275         klass->releaseCipher(self);
276         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
277         return GST_FLOW_NOT_SUPPORTED;
278     }
279
280     klass->releaseCipher(self);
281     gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
282     return GST_FLOW_OK;
283 }
284
285
286 static gboolean webkitMediaCommonEncryptionDecryptSinkEventHandler(GstBaseTransform* trans, GstEvent* event)
287 {
288     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(trans);
289     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
290     WebKitMediaCommonEncryptionDecryptClass* klass = WEBKIT_MEDIA_CENC_DECRYPT_GET_CLASS(self);
291     gboolean result = FALSE;
292
293     switch (GST_EVENT_TYPE(event)) {
294     case GST_EVENT_PROTECTION: {
295         const char* systemId;
296         const char* origin;
297         GstBuffer* initDataBuffer;
298
299         GST_DEBUG_OBJECT(self, "received protection event");
300         gst_event_parse_protection(event, &systemId, &initDataBuffer, &origin);
301         GST_DEBUG_OBJECT(self, "systemId: %s", systemId);
302
303         if (!g_str_equal(systemId, klass->protectionSystemId)) {
304             gst_event_unref(event);
305             result = TRUE;
306             break;
307         }
308
309         // Keep the event ref around so that the parsed event data
310         // remains valid until the drm-key-need message has been sent.
311         priv->protectionEvent = adoptGRef(event);
312         RunLoop::main().dispatch([self, initDataBuffer] {
313             WebKitMediaCommonEncryptionDecryptClass* klass = WEBKIT_MEDIA_CENC_DECRYPT_GET_CLASS(self);
314             if (klass) {
315                 klass->requestDecryptionKey(self, initDataBuffer);
316                 self->priv->protectionEvent = nullptr;
317             }});
318
319         result = TRUE;
320         break;
321     }
322     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB: {
323         if (klass->handleKeyResponse(self, event)) {
324             GST_DEBUG_OBJECT(self, "key received");
325             priv->keyReceived = true;
326             priv->condition.notifyOne();
327         }
328
329         gst_event_unref(event);
330         result = TRUE;
331         break;
332     }
333     default:
334         result = GST_BASE_TRANSFORM_CLASS(parent_class)->sink_event(trans, event);
335         break;
336     }
337
338     return result;
339 }
340
341 static GstStateChangeReturn webKitMediaCommonEncryptionDecryptorChangeState(GstElement* element, GstStateChange transition)
342 {
343     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(element);
344     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
345
346     switch (transition) {
347     case GST_STATE_CHANGE_PAUSED_TO_READY:
348         GST_DEBUG_OBJECT(self, "PAUSED->READY");
349         priv->condition.notifyOne();
350         break;
351     default:
352         break;
353     }
354
355     GstStateChangeReturn result = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
356
357     // Add post-transition code here.
358
359     return result;
360 }
361
362
363 static gboolean webKitMediaCommonEncryptionDecryptDefaultSetupCipher(WebKitMediaCommonEncryptionDecrypt*)
364 {
365     return true;
366 }
367
368
369 static void webKitMediaCommonEncryptionDecryptDefaultReleaseCipher(WebKitMediaCommonEncryptionDecrypt*)
370 {
371 }
372
373
374 #endif // ENABLE(LEGACY_ENCRYPTED_MEDIA) && USE(GSTREAMER)