[GStreamer][EME] waitingforkey event should consider decryptors' waiting status
[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/PrintStream.h>
31 #include <wtf/RunLoop.h>
32
33 #define WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_MEDIA_CENC_DECRYPT, WebKitMediaCommonEncryptionDecryptPrivate))
34 struct _WebKitMediaCommonEncryptionDecryptPrivate {
35     GRefPtr<GstEvent> protectionEvent;
36
37     bool keyReceived;
38     bool waitingForKey { false };
39     Lock mutex;
40     Condition condition;
41 };
42
43 static GstStateChangeReturn webKitMediaCommonEncryptionDecryptorChangeState(GstElement*, GstStateChange transition);
44 static void webKitMediaCommonEncryptionDecryptorFinalize(GObject*);
45 static GstCaps* webkitMediaCommonEncryptionDecryptTransformCaps(GstBaseTransform*, GstPadDirection, GstCaps*, GstCaps*);
46 static GstFlowReturn webkitMediaCommonEncryptionDecryptTransformInPlace(GstBaseTransform*, GstBuffer*);
47 static gboolean webkitMediaCommonEncryptionDecryptSinkEventHandler(GstBaseTransform*, GstEvent*);
48 static gboolean webkitMediaCommonEncryptionDecryptorQueryHandler(GstBaseTransform*, GstPadDirection, GstQuery*);
49
50
51 GST_DEBUG_CATEGORY_STATIC(webkit_media_common_encryption_decrypt_debug_category);
52 #define GST_CAT_DEFAULT webkit_media_common_encryption_decrypt_debug_category
53
54 #define webkit_media_common_encryption_decrypt_parent_class parent_class
55 G_DEFINE_TYPE(WebKitMediaCommonEncryptionDecrypt, webkit_media_common_encryption_decrypt, GST_TYPE_BASE_TRANSFORM);
56
57 static void webkit_media_common_encryption_decrypt_class_init(WebKitMediaCommonEncryptionDecryptClass* klass)
58 {
59     GObjectClass* gobjectClass = G_OBJECT_CLASS(klass);
60     gobjectClass->finalize = webKitMediaCommonEncryptionDecryptorFinalize;
61
62     GST_DEBUG_CATEGORY_INIT(webkit_media_common_encryption_decrypt_debug_category,
63         "webkitcenc", 0, "Common Encryption base class");
64
65     GstElementClass* elementClass = GST_ELEMENT_CLASS(klass);
66     elementClass->change_state = GST_DEBUG_FUNCPTR(webKitMediaCommonEncryptionDecryptorChangeState);
67
68     GstBaseTransformClass* baseTransformClass = GST_BASE_TRANSFORM_CLASS(klass);
69     baseTransformClass->transform_ip = GST_DEBUG_FUNCPTR(webkitMediaCommonEncryptionDecryptTransformInPlace);
70     baseTransformClass->transform_caps = GST_DEBUG_FUNCPTR(webkitMediaCommonEncryptionDecryptTransformCaps);
71     baseTransformClass->transform_ip_on_passthrough = FALSE;
72     baseTransformClass->sink_event = GST_DEBUG_FUNCPTR(webkitMediaCommonEncryptionDecryptSinkEventHandler);
73     baseTransformClass->query = GST_DEBUG_FUNCPTR(webkitMediaCommonEncryptionDecryptorQueryHandler);
74
75     g_type_class_add_private(klass, sizeof(WebKitMediaCommonEncryptionDecryptPrivate));
76 }
77
78 static void webkit_media_common_encryption_decrypt_init(WebKitMediaCommonEncryptionDecrypt* self)
79 {
80     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
81
82     self->priv = priv;
83     new (priv) WebKitMediaCommonEncryptionDecryptPrivate();
84
85     GstBaseTransform* base = GST_BASE_TRANSFORM(self);
86     gst_base_transform_set_in_place(base, TRUE);
87     gst_base_transform_set_passthrough(base, FALSE);
88     gst_base_transform_set_gap_aware(base, FALSE);
89 }
90
91 static void webKitMediaCommonEncryptionDecryptorFinalize(GObject* object)
92 {
93     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(object);
94     WebKitMediaCommonEncryptionDecryptPrivate* priv = self->priv;
95
96     priv->~WebKitMediaCommonEncryptionDecryptPrivate();
97     GST_CALL_PARENT(G_OBJECT_CLASS, finalize, (object));
98 }
99
100 static GstCaps* webkitMediaCommonEncryptionDecryptTransformCaps(GstBaseTransform* base, GstPadDirection direction, GstCaps* caps, GstCaps* filter)
101 {
102     if (direction == GST_PAD_UNKNOWN)
103         return nullptr;
104
105     GST_DEBUG_OBJECT(base, "direction: %s, caps: %" GST_PTR_FORMAT " filter: %" GST_PTR_FORMAT, (direction == GST_PAD_SRC) ? "src" : "sink", caps, filter);
106
107     GstCaps* transformedCaps = gst_caps_new_empty();
108     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(base);
109     WebKitMediaCommonEncryptionDecryptClass* klass = WEBKIT_MEDIA_CENC_DECRYPT_GET_CLASS(self);
110
111     unsigned size = gst_caps_get_size(caps);
112     for (unsigned i = 0; i < size; ++i) {
113         GstStructure* incomingStructure = gst_caps_get_structure(caps, i);
114         GUniquePtr<GstStructure> outgoingStructure = nullptr;
115
116         if (direction == GST_PAD_SINK) {
117             if (!gst_structure_has_field(incomingStructure, "original-media-type"))
118                 continue;
119
120             outgoingStructure = GUniquePtr<GstStructure>(gst_structure_copy(incomingStructure));
121             gst_structure_set_name(outgoingStructure.get(), gst_structure_get_string(outgoingStructure.get(), "original-media-type"));
122
123             // Filter out the DRM related fields from the down-stream caps.
124             for (int j = 0; j < gst_structure_n_fields(incomingStructure); ++j) {
125                 const gchar* fieldName = gst_structure_nth_field_name(incomingStructure, j);
126
127                 if (g_str_has_prefix(fieldName, "protection-system")
128                     || g_str_has_prefix(fieldName, "original-media-type")
129                     || g_str_has_prefix(fieldName, "encryption-algorithm")
130                     || g_str_has_prefix(fieldName, "encoding-scope")
131                     || g_str_has_prefix(fieldName, "cipher-mode"))
132                     gst_structure_remove_field(outgoingStructure.get(), fieldName);
133             }
134         } else {
135             outgoingStructure = GUniquePtr<GstStructure>(gst_structure_copy(incomingStructure));
136             // Filter out the video related fields from the up-stream caps,
137             // because they are not relevant to the input caps of this element and
138             // can cause caps negotiation failures with adaptive bitrate streams.
139             for (int index = gst_structure_n_fields(outgoingStructure.get()) - 1; index >= 0; --index) {
140                 const gchar* fieldName = gst_structure_nth_field_name(outgoingStructure.get(), index);
141                 GST_TRACE("Check field \"%s\" for removal", fieldName);
142
143                 if (!g_strcmp0(fieldName, "base-profile")
144                     || !g_strcmp0(fieldName, "codec_data")
145                     || !g_strcmp0(fieldName, "height")
146                     || !g_strcmp0(fieldName, "framerate")
147                     || !g_strcmp0(fieldName, "level")
148                     || !g_strcmp0(fieldName, "pixel-aspect-ratio")
149                     || !g_strcmp0(fieldName, "profile")
150                     || !g_strcmp0(fieldName, "rate")
151                     || !g_strcmp0(fieldName, "width")) {
152                     gst_structure_remove_field(outgoingStructure.get(), fieldName);
153                     GST_TRACE("Removing field %s", fieldName);
154                 }
155             }
156
157             gst_structure_set(outgoingStructure.get(), "protection-system", G_TYPE_STRING, klass->protectionSystemId,
158                 "original-media-type", G_TYPE_STRING, gst_structure_get_name(incomingStructure), nullptr);
159
160             // GST_PROTECTION_UNSPECIFIED_SYSTEM_ID was added in the GStreamer
161             // developement git master which will ship as version 1.16.0.
162             gst_structure_set_name(outgoingStructure.get(),
163 #if GST_CHECK_VERSION(1, 15, 0)
164                 !g_strcmp0(klass->protectionSystemId, GST_PROTECTION_UNSPECIFIED_SYSTEM_ID) ? "application/x-webm-enc" :
165 #endif
166                 "application/x-cenc");
167         }
168
169         bool duplicate = false;
170         unsigned size = gst_caps_get_size(transformedCaps);
171
172         for (unsigned index = 0; !duplicate && index < size; ++index) {
173             GstStructure* structure = gst_caps_get_structure(transformedCaps, index);
174             if (gst_structure_is_equal(structure, outgoingStructure.get()))
175                 duplicate = true;
176         }
177
178         if (!duplicate)
179             gst_caps_append_structure(transformedCaps, outgoingStructure.release());
180     }
181
182     if (filter) {
183         GstCaps* intersection;
184
185         GST_DEBUG_OBJECT(base, "Using filter caps %" GST_PTR_FORMAT, filter);
186         intersection = gst_caps_intersect_full(transformedCaps, filter, GST_CAPS_INTERSECT_FIRST);
187         gst_caps_unref(transformedCaps);
188         transformedCaps = intersection;
189     }
190
191     GST_DEBUG_OBJECT(base, "returning %" GST_PTR_FORMAT, transformedCaps);
192     return transformedCaps;
193 }
194
195 static GstFlowReturn webkitMediaCommonEncryptionDecryptTransformInPlace(GstBaseTransform* base, GstBuffer* buffer)
196 {
197     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(base);
198     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
199     LockHolder locker(priv->mutex);
200
201     // The key might not have been received yet. Wait for it.
202     if (!priv->keyReceived) {
203         GST_DEBUG_OBJECT(self, "key not available yet, waiting for it");
204         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)) {
205             GST_ERROR_OBJECT(self, "can't process key requests in less than PAUSED state");
206             return GST_FLOW_NOT_SUPPORTED;
207         }
208         // Send "drm-cdm-instance-needed" message to the player to resend the CDMInstance if available and inform we are waiting for key.
209         gst_element_post_message(GST_ELEMENT(self), gst_message_new_element(GST_OBJECT(self), gst_structure_new_empty("drm-cdm-instance-needed")));
210         priv->waitingForKey = true;
211         gst_element_post_message(GST_ELEMENT(self), gst_message_new_element(GST_OBJECT(self), gst_structure_new_empty("drm-waiting-for-key")));
212
213         priv->condition.waitFor(priv->mutex, Seconds(5), [priv] {
214             return priv->keyReceived;
215         });
216         if (!priv->keyReceived) {
217             GST_ERROR_OBJECT(self, "key not available");
218             return GST_FLOW_NOT_SUPPORTED;
219         }
220         GST_DEBUG_OBJECT(self, "key received, continuing");
221         priv->waitingForKey = false;
222         gst_element_post_message(GST_ELEMENT(self), gst_message_new_element(GST_OBJECT(self), gst_structure_new_empty("drm-key-received")));
223     }
224
225     GstProtectionMeta* protectionMeta = reinterpret_cast<GstProtectionMeta*>(gst_buffer_get_protection_meta(buffer));
226     if (!protectionMeta) {
227         GST_ERROR_OBJECT(self, "Failed to get GstProtection metadata from buffer %p", buffer);
228         return GST_FLOW_NOT_SUPPORTED;
229     }
230
231     unsigned ivSize;
232     if (!gst_structure_get_uint(protectionMeta->info, "iv_size", &ivSize)) {
233         GST_ERROR_OBJECT(self, "Failed to get iv_size");
234         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
235         return GST_FLOW_NOT_SUPPORTED;
236     }
237
238     gboolean encrypted;
239     if (!gst_structure_get_boolean(protectionMeta->info, "encrypted", &encrypted)) {
240         GST_ERROR_OBJECT(self, "Failed to get encrypted flag");
241         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
242         return GST_FLOW_NOT_SUPPORTED;
243     }
244
245     if (!ivSize || !encrypted) {
246         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
247         return GST_FLOW_OK;
248     }
249
250     GST_DEBUG_OBJECT(base, "protection meta: %" GST_PTR_FORMAT, protectionMeta->info);
251
252     unsigned subSampleCount;
253     if (!gst_structure_get_uint(protectionMeta->info, "subsample_count", &subSampleCount)) {
254         GST_ERROR_OBJECT(self, "Failed to get subsample_count");
255         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
256         return GST_FLOW_NOT_SUPPORTED;
257     }
258
259     const GValue* value;
260     GstBuffer* subSamplesBuffer = nullptr;
261     if (subSampleCount) {
262         value = gst_structure_get_value(protectionMeta->info, "subsamples");
263         if (!value) {
264             GST_ERROR_OBJECT(self, "Failed to get subsamples");
265             gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
266             return GST_FLOW_NOT_SUPPORTED;
267         }
268         subSamplesBuffer = gst_value_get_buffer(value);
269     }
270
271     value = gst_structure_get_value(protectionMeta->info, "kid");
272
273     if (!value) {
274         GST_ERROR_OBJECT(self, "Failed to get key id for buffer");
275         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
276         return GST_FLOW_NOT_SUPPORTED;
277     }
278     GstBuffer* keyIDBuffer = gst_value_get_buffer(value);
279
280     value = gst_structure_get_value(protectionMeta->info, "iv");
281     if (!value) {
282         GST_ERROR_OBJECT(self, "Failed to get IV for sample");
283         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
284         return GST_FLOW_NOT_SUPPORTED;
285     }
286
287     GstBuffer* ivBuffer = gst_value_get_buffer(value);
288     WebKitMediaCommonEncryptionDecryptClass* klass = WEBKIT_MEDIA_CENC_DECRYPT_GET_CLASS(self);
289
290     GST_TRACE_OBJECT(self, "decrypting");
291     if (!klass->decrypt(self, ivBuffer, keyIDBuffer, buffer, subSampleCount, subSamplesBuffer)) {
292         GST_ERROR_OBJECT(self, "Decryption failed");
293         gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
294         return GST_FLOW_NOT_SUPPORTED;
295     }
296
297     gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
298     return GST_FLOW_OK;
299 }
300
301
302 static gboolean webkitMediaCommonEncryptionDecryptSinkEventHandler(GstBaseTransform* trans, GstEvent* event)
303 {
304     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(trans);
305     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
306     WebKitMediaCommonEncryptionDecryptClass* klass = WEBKIT_MEDIA_CENC_DECRYPT_GET_CLASS(self);
307     gboolean result = FALSE;
308
309     switch (GST_EVENT_TYPE(event)) {
310     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB: {
311         if (klass->handleKeyResponse(self, event)) {
312             GST_DEBUG_OBJECT(self, "key received");
313             priv->keyReceived = true;
314             priv->condition.notifyOne();
315         }
316
317         gst_event_unref(event);
318         result = TRUE;
319         break;
320     }
321     default:
322         result = GST_BASE_TRANSFORM_CLASS(parent_class)->sink_event(trans, event);
323         break;
324     }
325
326     return result;
327 }
328
329 static gboolean webkitMediaCommonEncryptionDecryptorQueryHandler(GstBaseTransform* trans, GstPadDirection direction, GstQuery* query)
330 {
331     if (gst_structure_has_name(gst_query_get_structure(query), "any-decryptor-waiting-for-key")) {
332         WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(trans);
333         GST_TRACE_OBJECT(trans, "any-decryptor-waiting-for-key query, waitingforkey %s", boolForPrinting(priv->waitingForKey));
334         return priv->waitingForKey;
335     }
336     return GST_BASE_TRANSFORM_CLASS(parent_class)->query(trans, direction, query);
337 }
338
339 static GstStateChangeReturn webKitMediaCommonEncryptionDecryptorChangeState(GstElement* element, GstStateChange transition)
340 {
341     WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(element);
342     WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
343
344     switch (transition) {
345     case GST_STATE_CHANGE_PAUSED_TO_READY:
346         GST_DEBUG_OBJECT(self, "PAUSED->READY");
347         priv->condition.notifyOne();
348         break;
349     default:
350         break;
351     }
352
353     GstStateChangeReturn result = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
354
355     // Add post-transition code here.
356
357     return result;
358 }
359
360 #endif // ENABLE(ENCRYPTED_MEDIA) && USE(GSTREAMER)