-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGstCencDecrypt.cpp
301 lines (251 loc) · 11 KB
/
GstCencDecrypt.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright RDK Management
*
* 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, version 2
* of the license.
*
* 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.
*/
#include "GstCencDecrypt.h"
#include "IGstDecryptor.h"
#include <gst/base/gstbasetransform.h>
#include <gst/gst.h>
#include <gst/gstprotection.h>
#include <map>
#include <mutex>
using namespace CENCDecryptor;
GST_DEBUG_CATEGORY_STATIC(gst_cencdecrypt_debug_category);
#define GST_CAT_DEFAULT gst_cencdecrypt_debug_category
G_DEFINE_TYPE_WITH_CODE(GstCencDecrypt, gst_cencdecrypt, GST_TYPE_BASE_TRANSFORM,
GST_DEBUG_CATEGORY_INIT(gst_cencdecrypt_debug_category, "cencdecrypt", 0,
"debug category for cencdecrypt element"));
constexpr static auto clearContentTypes = { "audio/mp4", "audio/mpeg", "audio/opus", "video/mp4", "video/x-h264", "video/x-h265", "video/x-vp9" };
constexpr static auto cencMime = "application/x-cenc";
constexpr static auto webmMime = "application/x-webm-enc";
static std::map<std::string, std::string> keySystems{ { "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed", "com.widevine.alpha" },
{ "9a04f079-9840-4286-ab92-e65be0885f95", "com.microsoft.playready" }, { "1077efec-c0b2-4d02-ace3-3c1e52e2fb4b", "org.w3.clearkey" } };
static GstCaps* TransformCaps(GstBaseTransform* trans, GstPadDirection direction,
GstCaps* caps, GstCaps* filter);
static gboolean SinkEvent(GstBaseTransform* trans, GstEvent* event);
static GstFlowReturn TransformIp(GstBaseTransform* trans, GstBuffer* buffer);
static GstCaps* SinkCaps(GstCencDecryptClass*)
{
GstCaps* caps = gst_caps_new_empty();
for (auto& type : clearContentTypes) {
gst_caps_append_structure(caps,
gst_structure_new(webmMime,
"original-media-type", G_TYPE_STRING, type, NULL));
for (auto& system : keySystems) {
gst_caps_append_structure(caps,
gst_structure_new(cencMime,
"original-media-type", G_TYPE_STRING, type,
"protection-system", G_TYPE_STRING, system.first.c_str(), NULL));
}
}
return caps;
}
static GstCaps* SrcCaps()
{
GstCaps* caps = gst_caps_new_empty();
for (auto& type : clearContentTypes) {
gst_caps_append_structure(caps, gst_structure_new_from_string(type));
}
return caps;
}
struct GstCencDecryptImpl {
std::unique_ptr<IGstDecryptor> _decryptor;
std::string _keySystem;
std::mutex _initLock;
bool _isDecryptorInitialized;
};
void Finalize(GObject* object)
{
GstCencDecrypt* cencdecrypt = GST_CENCDECRYPT(object);
GST_DEBUG_OBJECT(cencdecrypt, "finalize");
cencdecrypt->_impl->_decryptor.reset();
G_OBJECT_CLASS(gst_cencdecrypt_parent_class)->finalize(object);
}
static void gst_cencdecrypt_class_init(GstCencDecryptClass* klass)
{
GstBaseTransformClass* base_transform_class = GST_BASE_TRANSFORM_CLASS(klass);
gst_element_class_add_pad_template(GST_ELEMENT_CLASS(klass),
gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS, SrcCaps()));
gst_element_class_add_pad_template(GST_ELEMENT_CLASS(klass),
gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS, SinkCaps(klass)));
gst_element_class_set_static_metadata(GST_ELEMENT_CLASS(klass),
"CENC decryptor", GST_ELEMENT_FACTORY_KLASS_DECRYPTOR, "Decrypts content with local instance of OpenCDM",
"FIXME <k.plata@metrological.com>");
G_OBJECT_CLASS(klass)->finalize = Finalize;
base_transform_class->transform_caps = GST_DEBUG_FUNCPTR(TransformCaps);
base_transform_class->transform_ip_on_passthrough = FALSE;
base_transform_class->transform_ip = GST_DEBUG_FUNCPTR(TransformIp);
base_transform_class->sink_event = GST_DEBUG_FUNCPTR(SinkEvent);
}
static void gst_cencdecrypt_init(GstCencDecrypt* cencdecrypt)
{
GstBaseTransform* base = GST_BASE_TRANSFORM(cencdecrypt);
gst_base_transform_set_in_place(base, TRUE);
gst_base_transform_set_passthrough(base, FALSE);
gst_base_transform_set_gap_aware(base, FALSE);
cencdecrypt->_impl = std::unique_ptr<GstCencDecryptImpl>(new GstCencDecryptImpl());
cencdecrypt->_impl->_decryptor = IGstDecryptor::Create();
GST_FIXME_OBJECT(cencdecrypt, "Caps are constructed based on hard coded keysystem values");
}
static gboolean SrcCapsTransform(GstCapsFeatures*,
GstStructure* structure,
gpointer user_data)
{
// gst_structure_remove_fields takes care of checking
// if a field with a corresponding name exists.
GstCencDecryptImpl* impl = reinterpret_cast<GstCencDecryptImpl*>(user_data);
gst_structure_remove_fields(structure, "base-profile",
"codec_data",
"height",
"framerate",
"level",
"pixel-aspect-ratio",
"profile",
"rate",
"width",
nullptr);
gst_structure_set(structure, "original-media-type", G_TYPE_STRING, gst_structure_get_name(structure), nullptr);
if (impl->_keySystem == GST_PROTECTION_UNSPECIFIED_SYSTEM_ID) {
gst_structure_set_name(structure, webmMime);
} else {
gst_structure_set(structure,
"protection-system", G_TYPE_STRING, impl->_keySystem.c_str(), nullptr);
gst_structure_set_name(structure, cencMime);
}
return TRUE;
}
static gboolean SinkCapsTransform(GstCapsFeatures*,
GstStructure* structure,
gpointer)
{
gst_structure_set_name(structure, gst_structure_get_string(structure, "original-media-type"));
gst_structure_remove_field(structure, "protection-system");
gst_structure_remove_field(structure, "original-media-type");
gst_structure_remove_field(structure, "encryption-algorithm");
gst_structure_remove_field(structure, "encoding-scope");
gst_structure_remove_field(structure, "cipher-mode");
return TRUE;
}
static GstCaps* TransformCaps(GstBaseTransform* trans, GstPadDirection direction,
GstCaps* caps, GstCaps* filter)
{
GstCencDecrypt* cencdecrypt = GST_CENCDECRYPT(trans);
GstCaps* othercaps = gst_caps_copy(caps);
GST_DEBUG_OBJECT(cencdecrypt, "transform_caps");
if (direction == GST_PAD_SRC) {
GST_DEBUG_OBJECT(cencdecrypt, "Transforming caps going upstream");
gst_caps_filter_and_map_in_place(othercaps, SrcCapsTransform, nullptr);
} else {
GST_DEBUG_OBJECT(cencdecrypt, "Transforming caps going downstream");
gst_caps_filter_and_map_in_place(othercaps, SinkCapsTransform, nullptr);
}
if (filter) {
GstCaps* intersect;
intersect = gst_caps_intersect(othercaps, filter);
gst_caps_unref(othercaps);
othercaps = intersect;
}
return othercaps;
}
static void InitializeDecryptor(GstCencDecrypt* cencdecrypt,
const std::string& keySystem,
const std::string& origin,
const std::string& initDataType,
GstBuffer* initData)
{
std::lock_guard<std::mutex> lk(cencdecrypt->_impl->_initLock);
if (!cencdecrypt->_impl->_isDecryptorInitialized) {
BufferView initDataView(initData, GST_MAP_READ);
auto result = cencdecrypt->_impl->_decryptor->Initialize(
keySystem,
origin,
initDataType,
initDataView);
if(result == IGstDecryptor::Status::SUCCESS) {
cencdecrypt->_impl->_isDecryptorInitialized = true;
GST_INFO_OBJECT(cencdecrypt, "Initialized decryptor for keysystem [%s]", keySystem.c_str());
} else if(result == IGstDecryptor::Status::ERROR_KEYSYSTEM_NOT_SUPPORTED) {
// If the keysystem was the only one supported by the asset, playback will abort.
GST_WARNING_OBJECT(cencdecrypt, "Keysystem [%s] not supported by decryptor.", keySystem.c_str());
} else {
GST_ERROR_OBJECT(cencdecrypt, "Decryptor initialization for keysystem [%s] failed", keySystem.c_str());
}
GST_MEMDUMP_OBJECT(cencdecrypt, "InitData extracted from protection system metadata ", initDataView.Raw(), initDataView.Size());
}
}
static gboolean SinkEvent(GstBaseTransform* trans, GstEvent* event)
{
GstCencDecrypt* cencdecrypt = GST_CENCDECRYPT(trans);
GST_DEBUG_OBJECT(cencdecrypt, "sink_event");
switch (GST_EVENT_TYPE(event)) {
case GST_EVENT_PROTECTION: {
const char *systemId, *origin;
GstBuffer* initData;
gst_event_parse_protection(event, &systemId, &initData, &origin);
cencdecrypt->_impl->_keySystem = std::string(systemId);
// MP4 parser will raise this event multiple times for earch supported keysystem.
// In the case of a WebM container, there will be no information about the keysystem
// that is meant to be used by the decryptor. Because of this, we're going to try
// with WideVine by default and allow for overriding the UUID.
std::string keySystem, initDataType;
if (cencdecrypt->_impl->_keySystem == GST_PROTECTION_UNSPECIFIED_SYSTEM_ID) {
auto overrideKeySystem = std::getenv("OVERRIDE_WEBM_KEYSYSTEM_UUID");
keySystem = (overrideKeySystem != nullptr) ? overrideKeySystem : "edef8ba9-79d6-4ace-a3c8-27dcd51d21ed";
initDataType = "webm";
} else {
keySystem = cencdecrypt->_impl->_keySystem;
initDataType = "cenc";
}
InitializeDecryptor(cencdecrypt, keySystem, origin, initDataType, initData);
gst_event_unref(event);
return true;
}
default: {
return GST_BASE_TRANSFORM_CLASS(gst_cencdecrypt_parent_class)->sink_event(trans, event);
}
}
}
static GstFlowReturn TransformIp(GstBaseTransform* trans, GstBuffer* buffer)
{
GstCencDecrypt* cencdecrypt = GST_CENCDECRYPT(trans);
GST_DEBUG_OBJECT(cencdecrypt, "Processing encrypted buffer %" GST_PTR_FORMAT, buffer);
auto encryptedBuffer = std::make_shared<EncryptedBuffer>(buffer);
IGstDecryptor::Status result = cencdecrypt->_impl->_decryptor->Decrypt(encryptedBuffer);
return (result == IGstDecryptor::Status::SUCCESS) ? GST_FLOW_OK : GST_FLOW_ERROR;
}
static gboolean plugin_init(GstPlugin* plugin)
{
return gst_element_register(plugin, "cencdecrypt", GST_RANK_PRIMARY,
GST_TYPE_CENCDECRYPT);
}
#ifndef VERSION
#define VERSION "0.2"
#endif
#ifndef PACKAGE
#define PACKAGE "gstcencdecryptor"
#endif
#ifndef PACKAGE_NAME
#define PACKAGE_NAME "gstcencdecryptor"
#endif
#ifndef GST_PACKAGE_ORIGIN
#define GST_PACKAGE_ORIGIN "https://github.com/WebPlatformForEmbedded/gstcencdecryptor/"
#endif
GST_PLUGIN_DEFINE(GST_VERSION_MAJOR,
GST_VERSION_MINOR,
cencdecrypt,
"Decryptor plugin using a local instance of OpenCDM",
plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)