1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/* GStreamer EME Utilities class
*
* Copyright (C) 2017 Metrological
* Copyright (C) 2017 Igalia S.L
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#if ENABLE(ENCRYPTED_MEDIA) && USE(GSTREAMER)
#include "GStreamerCommon.h"
#include "SharedBuffer.h"
#include <gst/gst.h>
#include <wtf/text/WTFString.h>
#define WEBCORE_GSTREAMER_EME_UTILITIES_CLEARKEY_UUID "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b"
#if ENABLE(THUNDER)
#define WEBCORE_GSTREAMER_EME_UTILITIES_WIDEVINE_UUID "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed"
#endif
GST_DEBUG_CATEGORY_EXTERN(webkit_media_common_encryption_decrypt_debug_category);
namespace WebCore {
class InitData {
public:
InitData() = default;
// FIXME: We should have an enum for system uuids for better type safety.
InitData(const String& systemId, GstBuffer* initData)
: m_systemId(systemId)
{
auto mappedInitData = GstMappedOwnedBuffer::create(initData);
if (!mappedInitData) {
GST_CAT_LEVEL_LOG(webkit_media_common_encryption_decrypt_debug_category, GST_LEVEL_ERROR, nullptr, "cannot map %s protection data", systemId.utf8().data());
ASSERT_NOT_REACHED();
}
m_payload = extractCencIfNeeded(mappedInitData->createSharedBuffer());
}
InitData(const String& systemId, RefPtr<FragmentedSharedBuffer>&& payload)
: m_systemId(systemId)
{
if (payload)
m_payload = extractCencIfNeeded(payload->makeContiguous());
}
void append(InitData&& initData)
{
// FIXME: There is some confusion here about how to detect the
// correct "initialization data type", if the system ID is
// GST_PROTECTION_UNSPECIFIED_SYSTEM_ID, then we know it came
// from WebM. If the system id is specified with one of the
// defined ClearKey / Playready / Widevine / etc UUIDs, then
// we know it's MP4. For the latter case, it does not matter
// which of the UUIDs it is, so we just overwrite it. This is
// a quirk of how GStreamer provides protection events, and
// it's not very robust, so be careful here!
m_systemId = initData.m_systemId;
m_payload.append(*initData.payload());
}
RefPtr<FragmentedSharedBuffer> payload() const { return m_payload.get(); }
const String& systemId() const { return m_systemId; }
String payloadContainerType() const
{
#if GST_CHECK_VERSION(1, 16, 0)
if (m_systemId == GST_PROTECTION_UNSPECIFIED_SYSTEM_ID)
return "webm"_s;
#endif
return "cenc"_s;
}
private:
static RefPtr<SharedBuffer> extractCencIfNeeded(RefPtr<SharedBuffer>&&);
String m_systemId;
SharedBufferBuilder m_payload;
};
class ProtectionSystemEvents {
public:
using EventVector = Vector<GRefPtr<GstEvent>>;
explicit ProtectionSystemEvents(GstMessage* message)
{
const GstStructure* structure = gst_message_get_structure(message);
const GValue* streamEncryptionEventsList = gst_structure_get_value(structure, "stream-encryption-events");
ASSERT(streamEncryptionEventsList && GST_VALUE_HOLDS_LIST(streamEncryptionEventsList));
unsigned numEvents = gst_value_list_get_size(streamEncryptionEventsList);
m_events.reserveInitialCapacity(numEvents);
for (unsigned i = 0; i < numEvents; ++i)
m_events.uncheckedAppend(GRefPtr<GstEvent>(static_cast<GstEvent*>(g_value_get_boxed(gst_value_list_get_value(streamEncryptionEventsList, i)))));
const GValue* streamEncryptionAllowedSystemsValue = gst_structure_get_value(structure, "available-stream-encryption-systems");
const char** streamEncryptionAllowedSystems = reinterpret_cast<const char**>(g_value_get_boxed(streamEncryptionAllowedSystemsValue));
if (streamEncryptionAllowedSystems) {
for (unsigned i = 0; streamEncryptionAllowedSystems[i]; ++i)
m_availableSystems.append(streamEncryptionAllowedSystems[i]);
}
}
const EventVector& events() const { return m_events; }
const Vector<String>& availableSystems() const { return m_availableSystems; }
private:
EventVector m_events;
Vector<String> m_availableSystems;
};
class GStreamerEMEUtilities {
public:
static constexpr char const* s_ClearKeyUUID = WEBCORE_GSTREAMER_EME_UTILITIES_CLEARKEY_UUID;
static constexpr char const* s_ClearKeyKeySystem = "org.w3.clearkey";
#if ENABLE(THUNDER)
static constexpr char const* s_WidevineUUID = WEBCORE_GSTREAMER_EME_UTILITIES_WIDEVINE_UUID;
static constexpr char const* s_WidevineKeySystem = "com.widevine.alpha";
#endif
static bool isClearKeyKeySystem(const String& keySystem)
{
return equalIgnoringASCIICase(keySystem, s_ClearKeyKeySystem);
}
#if ENABLE(THUNDER)
static bool isWidevineKeySystem(const String& keySystem)
{
return equalIgnoringASCIICase(keySystem, s_WidevineKeySystem);
}
#endif
static const char* keySystemToUuid(const String& keySystem)
{
if (isClearKeyKeySystem(keySystem))
return s_ClearKeyUUID;
#if ENABLE(THUNDER)
if (isWidevineKeySystem(keySystem))
return s_WidevineUUID;
#endif
ASSERT_NOT_REACHED();
return { };
}
};
}
#endif // ENABLE(ENCRYPTED_MEDIA) && USE(GSTREAMER)
|