ca32872bfcd4685846f6d45caea046b95b899cd7
[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(ENCRYPTED_MEDIA) && USE(GSTREAMER)
27
28 #include "GStreamerCommon.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*, GstBuffer*);
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         GUniquePtr<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 = GUniquePtr<GstStructure>(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 = GUniquePtr<GstStructure>(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.release());
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         // Send "drm-cdm-instance-needed" message to the player to resend the CDMInstance if available.
201         gst_element_post_message(GST_ELEMENT(self), gst_message_new_element(GST_OBJECT(self), gst_structure_new_empty("drm-cdm-instance-needed")));
202
203         priv->condition.waitFor(priv->mutex, Seconds(5), [priv] {
204             return priv->keyReceived;
205         });
206         if (!priv->keyReceived) {
207             GST_ERROR_OBJECT(self, "key not available");
208             return GST_FLOW_NOT_SUPPORTED;
209         }
210         GST_DEBUG_OBJECT(self, "key received, continuing");
211     }
212
213     GstProtectionMeta* protectionMeta = reinterpret_cast<GstProtectionMeta*>(gst_buffer_get_protection_meta(buffer));
214     if (!protectionMeta) {
215         GST_ERROR_OBJECT(self, "Failed to get GstProtection metadata from buffer %p", buffer);
216         return GST_FLOW_NOT_SUPPORTED;
217     }
218
219     unsigned ivSize;
220     if (!gst_structure_get_uint(protectionMeta->info, "iv_size", &ivSize)) {
221         GST_ERROR_OBJECT(self, "Failed to get iv_size");
222         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
223         return GST_FLOW_NOT_SUPPORTED;
224     }
225
226     gboolean encrypted;
227     if (!gst_structure_get_boolean(protectionMeta->info, "encrypted", &encrypted)) {
228         GST_ERROR_OBJECT(self, "Failed to get encrypted flag");
229         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
230         return GST_FLOW_NOT_SUPPORTED;
231     }
232
233     if (!ivSize || !encrypted) {
234         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
235         return GST_FLOW_OK;
236     }
237
238     GST_DEBUG_OBJECT(base, "protection meta: %" GST_PTR_FORMAT, protectionMeta->info);
239
240     unsigned subSampleCount;
241     if (!gst_structure_get_uint(protectionMeta->info, "subsample_count", &subSampleCount)) {
242         GST_ERROR_OBJECT(self, "Failed to get subsample_count");
243         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
244         return GST_FLOW_NOT_SUPPORTED;
245     }
246
247     const GValue* value;
248     GstBuffer* subSamplesBuffer = nullptr;
249     if (subSampleCount) {
250         value = gst_structure_get_value(protectionMeta->info, "subsamples");
251         if (!value) {
252             GST_ERROR_OBJECT(self, "Failed to get subsamples");
253             gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
254             return GST_FLOW_NOT_SUPPORTED;
255         }
256         subSamplesBuffer = gst_value_get_buffer(value);
257     }
258
259     value = gst_structure_get_value(protectionMeta->info, "kid");
260     GstBuffer* keyIDBuffer = nullptr;
261     if (value)
262         keyIDBuffer = gst_value_get_buffer(value);
263
264     WebKitMediaCommonEncryptionDecryptClass* klass = WEBKIT_MEDIA_CENC_DECRYPT_GET_CLASS(self);
265     if (!klass->setupCipher(self, keyIDBuffer)) {
266         GST_ERROR_OBJECT(self, "Failed to configure cipher");
267         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
268         return GST_FLOW_NOT_SUPPORTED;
269     }
270
271     value = gst_structure_get_value(protectionMeta->info, "iv");
272     if (!value) {
273         GST_ERROR_OBJECT(self, "Failed to get IV for sample");
274         klass->releaseCipher(self);
275         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
276         return GST_FLOW_NOT_SUPPORTED;
277     }
278
279     GstBuffer* ivBuffer = gst_value_get_buffer(value);
280     GST_TRACE_OBJECT(self, "decrypting");
281     if (!klass->decrypt(self, ivBuffer, buffer, subSampleCount, subSamplesBuffer)) {
282         GST_ERROR_OBJECT(self, "Decryption failed");
283         klass->releaseCipher(self);
284         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
285         return GST_FLOW_NOT_SUPPORTED;
286     }
287
288     klass->releaseCipher(self);
289     gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
290     return GST_FLOW_OK;
291 }
292
293
294 static gboolean webkitMediaCommonEncryptionDecryptSinkEventHandler(GstBaseTransform* trans, GstEvent* event)
295 {
296     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(trans);
297     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
298     WebKitMediaCommonEncryptionDecryptClass* klass = WEBKIT_MEDIA_CENC_DECRYPT_GET_CLASS(self);
299     gboolean result = FALSE;
300
301     switch (GST_EVENT_TYPE(event)) {
302     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB: {
303         if (klass->handleKeyResponse(self, event)) {
304             GST_DEBUG_OBJECT(self, "key received");
305             priv->keyReceived = true;
306             priv->condition.notifyOne();
307         }
308
309         gst_event_unref(event);
310         result = TRUE;
311         break;
312     }
313     default:
314         result = GST_BASE_TRANSFORM_CLASS(parent_class)->sink_event(trans, event);
315         break;
316     }
317
318     return result;
319 }
320
321 static GstStateChangeReturn webKitMediaCommonEncryptionDecryptorChangeState(GstElement* element, GstStateChange transition)
322 {
323     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(element);
324     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
325
326     switch (transition) {
327     case GST_STATE_CHANGE_PAUSED_TO_READY:
328         GST_DEBUG_OBJECT(self, "PAUSED->READY");
329         priv->condition.notifyOne();
330         break;
331     default:
332         break;
333     }
334
335     GstStateChangeReturn result = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
336
337     // Add post-transition code here.
338
339     return result;
340 }
341
342
343 static gboolean webKitMediaCommonEncryptionDecryptDefaultSetupCipher(WebKitMediaCommonEncryptionDecrypt*, GstBuffer*)
344 {
345     return true;
346 }
347
348
349 static void webKitMediaCommonEncryptionDecryptDefaultReleaseCipher(WebKitMediaCommonEncryptionDecrypt*)
350 {
351 }
352
353 #endif // ENABLE(ENCRYPTED_MEDIA) && USE(GSTREAMER)