1 /* GStreamer ClearKey common encryption decryptor
3 * Copyright (C) 2013 YouView TV Ltd. <alex.ashley@youview.com>
4 * Copyright (C) 2016 Metrological
5 * Copyright (C) 2016 Igalia S.L
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.
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.
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.
24 #include "WebKitCommonEncryptionDecryptorGStreamer.h"
26 #if ENABLE(LEGACY_ENCRYPTED_MEDIA) && USE(GSTREAMER)
28 #include "GRefPtrGStreamer.h"
29 #include <wtf/Condition.h>
30 #include <wtf/RunLoop.h>
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;
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*);
47 static gboolean webKitMediaCommonEncryptionDecryptDefaultSetupCipher(WebKitMediaCommonEncryptionDecrypt*);
48 static void webKitMediaCommonEncryptionDecryptDefaultReleaseCipher(WebKitMediaCommonEncryptionDecrypt*);
50 GST_DEBUG_CATEGORY_STATIC(webkit_media_common_encryption_decrypt_debug_category);
51 #define GST_CAT_DEFAULT webkit_media_common_encryption_decrypt_debug_category
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);
56 static void webkit_media_common_encryption_decrypt_class_init(WebKitMediaCommonEncryptionDecryptClass* klass)
58 GObjectClass* gobjectClass = G_OBJECT_CLASS(klass);
59 gobjectClass->finalize = webKitMediaCommonEncryptionDecryptorFinalize;
61 GST_DEBUG_CATEGORY_INIT(webkit_media_common_encryption_decrypt_debug_category,
62 "webkitcenc", 0, "Common Encryption base class");
64 GstElementClass* elementClass = GST_ELEMENT_CLASS(klass);
65 elementClass->change_state = GST_DEBUG_FUNCPTR(webKitMediaCommonEncryptionDecryptorChangeState);
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);
73 klass->setupCipher = GST_DEBUG_FUNCPTR(webKitMediaCommonEncryptionDecryptDefaultSetupCipher);
74 klass->releaseCipher = GST_DEBUG_FUNCPTR(webKitMediaCommonEncryptionDecryptDefaultReleaseCipher);
76 g_type_class_add_private(klass, sizeof(WebKitMediaCommonEncryptionDecryptPrivate));
79 static void webkit_media_common_encryption_decrypt_init(WebKitMediaCommonEncryptionDecrypt* self)
81 WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
84 new (priv) WebKitMediaCommonEncryptionDecryptPrivate();
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);
92 static void webKitMediaCommonEncryptionDecryptorFinalize(GObject* object)
94 WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(object);
95 WebKitMediaCommonEncryptionDecryptPrivate* priv = self->priv;
97 priv->~WebKitMediaCommonEncryptionDecryptPrivate();
98 GST_CALL_PARENT(G_OBJECT_CLASS, finalize, (object));
101 static GstCaps* webkitMediaCommonEncryptionDecryptTransformCaps(GstBaseTransform* base, GstPadDirection direction, GstCaps* caps, GstCaps* filter)
103 if (direction == GST_PAD_UNKNOWN)
106 GST_DEBUG_OBJECT(base, "direction: %s, caps: %" GST_PTR_FORMAT " filter: %" GST_PTR_FORMAT, (direction == GST_PAD_SRC) ? "src" : "sink", caps, filter);
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);
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;
117 if (direction == GST_PAD_SINK) {
118 if (!gst_structure_has_field(incomingStructure, "original-media-type"))
121 outgoingStructure = adoptGRef(gst_structure_copy(incomingStructure));
122 gst_structure_set_name(outgoingStructure.get(), gst_structure_get_string(outgoingStructure.get(), "original-media-type"));
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);
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);
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);
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);
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);
158 gst_structure_set_name(outgoingStructure.get(), "application/x-cenc");
161 bool duplicate = false;
162 unsigned size = gst_caps_get_size(transformedCaps);
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()))
171 gst_caps_append_structure(transformedCaps, outgoingStructure.leakRef());
175 GstCaps* intersection;
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;
183 GST_DEBUG_OBJECT(base, "returning %" GST_PTR_FORMAT, transformedCaps);
184 return transformedCaps;
187 static GstFlowReturn webkitMediaCommonEncryptionDecryptTransformInPlace(GstBaseTransform* base, GstBuffer* buffer)
189 WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(base);
190 WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
191 LockHolder locker(priv->mutex);
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;
200 priv->condition.waitFor(priv->mutex, Seconds(5), [priv] {
201 return priv->keyReceived;
203 if (!priv->keyReceived) {
204 GST_ERROR_OBJECT(self, "key not available");
205 return GST_FLOW_NOT_SUPPORTED;
207 GST_DEBUG_OBJECT(self, "key received, continuing");
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;
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;
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;
230 if (!ivSize || !encrypted) {
231 gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
235 GST_DEBUG_OBJECT(base, "protection meta: %" GST_PTR_FORMAT, protectionMeta->info);
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;
245 GstBuffer* subSamplesBuffer = nullptr;
246 if (subSampleCount) {
247 value = gst_structure_get_value(protectionMeta->info, "subsamples");
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;
253 subSamplesBuffer = gst_value_get_buffer(value);
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;
263 value = gst_structure_get_value(protectionMeta->info, "iv");
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;
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;
280 klass->releaseCipher(self);
281 gst_buffer_remove_meta(buffer, reinterpret_cast<GstMeta*>(protectionMeta));
286 static gboolean webkitMediaCommonEncryptionDecryptSinkEventHandler(GstBaseTransform* trans, GstEvent* event)
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;
293 switch (GST_EVENT_TYPE(event)) {
294 case GST_EVENT_PROTECTION: {
295 const char* systemId;
297 GstBuffer* initDataBuffer;
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);
303 if (!g_str_equal(systemId, klass->protectionSystemId)) {
304 gst_event_unref(event);
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);
315 klass->requestDecryptionKey(self, initDataBuffer);
316 self->priv->protectionEvent = nullptr;
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();
329 gst_event_unref(event);
334 result = GST_BASE_TRANSFORM_CLASS(parent_class)->sink_event(trans, event);
341 static GstStateChangeReturn webKitMediaCommonEncryptionDecryptorChangeState(GstElement* element, GstStateChange transition)
343 WebKitMediaCommonEncryptionDecrypt* self = WEBKIT_MEDIA_CENC_DECRYPT(element);
344 WebKitMediaCommonEncryptionDecryptPrivate* priv = WEBKIT_MEDIA_CENC_DECRYPT_GET_PRIVATE(self);
346 switch (transition) {
347 case GST_STATE_CHANGE_PAUSED_TO_READY:
348 GST_DEBUG_OBJECT(self, "PAUSED->READY");
349 priv->condition.notifyOne();
355 GstStateChangeReturn result = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
357 // Add post-transition code here.
363 static gboolean webKitMediaCommonEncryptionDecryptDefaultSetupCipher(WebKitMediaCommonEncryptionDecrypt*)
369 static void webKitMediaCommonEncryptionDecryptDefaultReleaseCipher(WebKitMediaCommonEncryptionDecrypt*)
374 #endif // ENABLE(LEGACY_ENCRYPTED_MEDIA) && USE(GSTREAMER)