[EME][GStreamer] Add a new message "decrypt-key-needed" send from the decryptor to the application.
https://bugs.webkit.org/show_bug.cgi?id=181858

Patch by Yacine Bandou <yacine.bandou_ext@softathome.com> on 2018-04-27
Reviewed by Xabier Rodriguez-Calvar.

Add a new message "decrypt-key-needed" that the decryptor can send when it doesn't have an available key.
This message should be handled by the application in order to dispatch or send the key to the decryptor.
This patch is a preparation for the patch 181855.
With the patch 181855, the decryptor will be in the PlaybackPipeline instead of AppendPipeline, thus we can
get the DRM license or key before to instantiate or load the decryptor plugin in PlaybackPipeline.
When the decryptor plugin is instantiated or loaded, it should able to ask the application to resend
the DRM license or key by using this new message "decrypt-key-needed".

* platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp:
(WebCore::MediaPlayerPrivateGStreamer::handleMessage):
* platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp:
(WebCore::MediaPlayerPrivateGStreamerBase::dispatchCDMInstance):
* platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.h:
* platform/graphics/gstreamer/eme/WebKitCommonEncryptionDecryptorGStreamer.cpp:
(webkitMediaCommonEncryptionDecryptTransformInPlace):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@231088 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index bc57ac4..7bde60d 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,27 @@
+2018-04-27  Yacine Bandou  <yacine.bandou_ext@softathome.com>
+
+        [EME][GStreamer] Add a new message "decrypt-key-needed" send from the decryptor to the application.
+        https://bugs.webkit.org/show_bug.cgi?id=181858
+
+        Reviewed by Xabier Rodriguez-Calvar.
+
+        Add a new message "decrypt-key-needed" that the decryptor can send when it doesn't have an available key.
+        This message should be handled by the application in order to dispatch or send the key to the decryptor.
+        This patch is a preparation for the patch 181855.
+        With the patch 181855, the decryptor will be in the PlaybackPipeline instead of AppendPipeline, thus we can
+        get the DRM license or key before to instantiate or load the decryptor plugin in PlaybackPipeline.
+        When the decryptor plugin is instantiated or loaded, it should able to ask the application to resend
+        the DRM license or key by using this new message "decrypt-key-needed".
+
+
+        * platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp:
+        (WebCore::MediaPlayerPrivateGStreamer::handleMessage):
+        * platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp:
+        (WebCore::MediaPlayerPrivateGStreamerBase::dispatchCDMInstance):
+        * platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.h:
+        * platform/graphics/gstreamer/eme/WebKitCommonEncryptionDecryptorGStreamer.cpp:
+        (webkitMediaCommonEncryptionDecryptTransformInPlace):
+
 2018-04-26  Justin Fan  <justin_fan@apple.com>
 
         tex[Sub]Image2D slow when passing in a <canvas>, faster with ImageData.
diff --git a/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp b/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp
index 784c961..36a9526 100644
--- a/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp
+++ b/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp
@@ -1245,6 +1245,9 @@
             GRefPtr<GstEvent> event;
             gst_structure_get(structure, "event", GST_TYPE_EVENT, &event.outPtr(), nullptr);
             handleProtectionEvent(event.get());
+        } else if (gst_structure_has_name(structure, "decrypt-key-needed")) {
+            GST_DEBUG("decrypt-key-needed message from %s", GST_MESSAGE_SRC_NAME(message));
+            MediaPlayerPrivateGStreamerBase::dispatchCDMInstance();
         }
 #endif
         else if (gst_structure_has_name(structure, "http-headers")) {
diff --git a/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp b/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp
index 8e476ff..1557475 100644
--- a/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp
+++ b/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp
@@ -1244,6 +1244,13 @@
     GST_TRACE("emitted decryption cipher key on pipeline, event handled %s, need to resend credentials %s", boolForPrinting(eventHandled), boolForPrinting(m_needToResendCredentials));
 }
 
+void MediaPlayerPrivateGStreamerBase::dispatchCDMInstance()
+{
+    // This function dispatches the CDMInstance in GStreamer playback pipeline.
+    if (m_cdmInstance)
+        m_player->attemptToDecryptWithInstance(const_cast<CDMInstance&>(*m_cdmInstance.get()));
+}
+
 void MediaPlayerPrivateGStreamerBase::handleProtectionEvent(GstEvent* event)
 {
     if (m_handledProtectionEvents.contains(GST_EVENT_SEQNUM(event))) {
diff --git a/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.h b/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.h
index 8e8db7f..64ffb8d 100644
--- a/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.h
+++ b/Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.h
@@ -143,6 +143,7 @@
     void handleProtectionEvent(GstEvent*);
     void attemptToDecryptWithLocalInstance();
     void attemptToDecryptWithInstance(CDMInstance&) override;
+    void dispatchCDMInstance();
 #endif
 
     static bool supportsKeySystem(const String& keySystem, const String& mimeType);
diff --git a/Source/WebCore/platform/graphics/gstreamer/eme/WebKitCommonEncryptionDecryptorGStreamer.cpp b/Source/WebCore/platform/graphics/gstreamer/eme/WebKitCommonEncryptionDecryptorGStreamer.cpp
index f420af9..33a330a 100644
--- a/Source/WebCore/platform/graphics/gstreamer/eme/WebKitCommonEncryptionDecryptorGStreamer.cpp
+++ b/Source/WebCore/platform/graphics/gstreamer/eme/WebKitCommonEncryptionDecryptorGStreamer.cpp
@@ -197,6 +197,9 @@
             GST_ERROR_OBJECT(self, "can't process key requests in less than PAUSED state");
             return GST_FLOW_NOT_SUPPORTED;
         }
+        // Send "decrypt-key-needed" message to the application in order to resend the key if it is available in the application.
+        gst_element_post_message(GST_ELEMENT(self), gst_message_new_element(GST_OBJECT(self), gst_structure_new_empty("decrypt-key-needed")));
+
         priv->condition.waitFor(priv->mutex, Seconds(5), [priv] {
             return priv->keyReceived;
         });