summaryrefslogtreecommitdiff
path: root/libweston/content-protection.c
blob: d732b1657952d56625cd8835894e2d0cb6e33f37 (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * Copyright © 2019 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <libweston/libweston.h>
#include <libweston/weston-log.h>
#include "libweston-internal.h"
#include "weston-content-protection-server-protocol.h"
#include "shared/helpers.h"
#include "shared/timespec-util.h"

#define content_protection_log(cp, ...) \
	weston_log_scope_printf((cp)->debug, __VA_ARGS__)

static const char * const content_type_name [] = {
	[WESTON_PROTECTED_SURFACE_TYPE_UNPROTECTED] = "UNPROTECTED",
	[WESTON_PROTECTED_SURFACE_TYPE_HDCP_0] = "TYPE-0",
	[WESTON_PROTECTED_SURFACE_TYPE_HDCP_1] = "TYPE-1",
};

void
weston_protected_surface_send_event(struct protected_surface *psurface,
				       enum weston_hdcp_protection protection)
{
	struct wl_resource *p_resource;
	enum weston_protected_surface_type protection_type;
	struct content_protection *cp;
	struct wl_resource *surface_resource;

	p_resource = psurface->protection_resource;
	if (!p_resource)
		return;
	/* No event to be sent to client, in case of enforced mode */
	if (psurface->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED)
		return;
	protection_type = (enum weston_protected_surface_type) protection;
	weston_protected_surface_send_status(p_resource, protection_type);

	cp = psurface->cp_backptr;
	surface_resource = psurface->surface->resource;
	content_protection_log(cp, "wl_surface@%"PRIu32" Protection type set to %s\n",
			       wl_resource_get_id(surface_resource),
			       content_type_name[protection_type]);
}

static void
set_type(struct wl_client *client, struct wl_resource *resource,
	 enum weston_protected_surface_type content_type)
{
	struct content_protection *cp;
	struct protected_surface *psurface;
	enum weston_hdcp_protection weston_cp;
	struct wl_resource *surface_resource;

	psurface = wl_resource_get_user_data(resource);
	if (!psurface)
		return;
	cp = psurface->cp_backptr;
	surface_resource = psurface->surface->resource;

	if (content_type < WESTON_PROTECTED_SURFACE_TYPE_UNPROTECTED ||
	    content_type > WESTON_PROTECTED_SURFACE_TYPE_HDCP_1) {
		wl_resource_post_error(resource,
				       WESTON_PROTECTED_SURFACE_ERROR_INVALID_TYPE,
				       "wl_surface@%"PRIu32" Invalid content-type %d for request:set_type\n",
				       wl_resource_get_id(surface_resource), content_type);

		content_protection_log(cp, "wl_surface@%"PRIu32" Invalid content-type %d for request:set_type\n",
				       wl_resource_get_id(surface_resource), content_type);
		return;
	}

	content_protection_log(cp, "wl_surface@%"PRIu32" Request: Enable Content-Protection Type: %s\n",
			       wl_resource_get_id(surface_resource),
			       content_type_name[content_type]);

	weston_cp = (enum weston_hdcp_protection) content_type;
	psurface->surface->pending.desired_protection = weston_cp;
}

static void
protected_surface_destroy(struct wl_client *client, struct wl_resource *resource)
{
	struct protected_surface *psurface;

	psurface = wl_resource_get_user_data(resource);
	if (!psurface)
		return;
	psurface->surface->pending.desired_protection = WESTON_HDCP_DISABLE;
}

static void
set_enforce_mode(struct wl_client *client, struct wl_resource *resource)
{
	/*
	 * Enforce Censored-Visibility. Compositor censors the protected
	 * surface on an unsecured output.
	 * In case of a surface, being shown on an unprotected output, the
	 * compositor hides the surface, not allowing it to be displayed on
	 * the unprotected output, without bothering the client. No difference
	 * for the protected outputs.
	 *
	 * The member 'protection_mode' is "double-buffered", so setting it in
	 * pending_state will cause the setting of the corresponding
	 * 'protection_mode' in weston_surface, after the commit.
	 *
	 * This function sets the 'protection_mode' of the weston_surface_state
	 * to 'enfoced'. The renderers inspect the flag and compare the
	 * desired_protection of the surface, to the current_protection of the
	 * output, based on that the real surface or a place-holder content,
	 * (e.g. solid color) are shown.
	 */

	struct protected_surface *psurface;

	psurface = wl_resource_get_user_data(resource);
	if (!psurface)
		return;

	psurface->surface->pending.protection_mode =
		WESTON_SURFACE_PROTECTION_MODE_ENFORCED;
}

static void
set_relax_mode(struct wl_client *client, struct wl_resource *resource)
{
	/*
	 * Relaxed mode. By default this mode will be activated.
	 * In case of a surface, being shown in unprotected output,
	 * compositor just sends the event for protection status changed.
	 *
	 * On setting the relaxed mode, the 'protection_mode' member is queued
	 * to be set to 'relax' from the existing 'enforce' mode.
	 */

	struct protected_surface *psurface;

	psurface = wl_resource_get_user_data(resource);
	if (!psurface)
		return;

	psurface->surface->pending.protection_mode =
		WESTON_SURFACE_PROTECTION_MODE_RELAXED;
}

static const struct weston_protected_surface_interface protected_surface_implementation = {
	protected_surface_destroy,
	set_type,
	set_enforce_mode,
	set_relax_mode,
};

static void
cp_destroy_listener(struct wl_listener *listener, void *data)
{
	struct content_protection *cp;

	cp = container_of(listener, struct content_protection,
			  destroy_listener);
	wl_list_remove(&cp->destroy_listener.link);
	wl_list_remove(&cp->protected_list);
	weston_log_scope_destroy(cp->debug);
	cp->debug = NULL;
	if (cp->surface_protection_update)
		wl_event_source_remove(cp->surface_protection_update);
	cp->surface_protection_update = NULL;
	cp->compositor->content_protection = NULL;
	free(cp);
}

static void
free_protected_surface(struct protected_surface *psurface)
{
	psurface->surface->pending.desired_protection = WESTON_HDCP_DISABLE;
	wl_resource_set_user_data(psurface->protection_resource, NULL);
	wl_list_remove(&psurface->surface_destroy_listener.link);
	wl_list_remove(&psurface->link);
	free(psurface);
}

static void
surface_destroyed(struct wl_listener *listener, void *data)
{
	struct protected_surface *psurface;

	psurface = container_of(listener, struct protected_surface,
				surface_destroy_listener);
	free_protected_surface(psurface);
}

static void
destroy_protected_surface(struct wl_resource *resource)
{
	struct protected_surface *psurface;

	psurface = wl_resource_get_user_data(resource);
	if (!psurface)
		return;
	free_protected_surface(psurface);
}

static void
get_protection(struct wl_client *client, struct wl_resource *cp_resource,
	     uint32_t id, struct wl_resource *surface_resource)
{
	struct wl_resource *resource;
	struct weston_surface *surface;
	struct content_protection *cp;
	struct protected_surface *psurface;
	struct wl_listener *listener;

	surface = wl_resource_get_user_data(surface_resource);
	assert(surface);
	cp = wl_resource_get_user_data(cp_resource);
	assert(cp);

	/*
	 * Check if this client has a corresponding protected-surface
	 */

	listener = wl_resource_get_destroy_listener(surface->resource,
						    surface_destroyed);

	if (listener) {
		wl_resource_post_error(cp_resource,
				       WESTON_CONTENT_PROTECTION_ERROR_SURFACE_EXISTS,
				       "wl_surface@%"PRIu32" Protection already exists",
				       wl_resource_get_id(surface_resource));
		return;
	}

	psurface = zalloc(sizeof(struct protected_surface));
	if (!psurface) {
		wl_client_post_no_memory(client);
		return;
	}
	psurface->cp_backptr = cp;
	resource = wl_resource_create(client, &weston_protected_surface_interface,
				      1, id);
	if (!resource) {
		free(psurface);
		wl_client_post_no_memory(client);
		return;
	}

	wl_list_insert(&cp->protected_list, &psurface->link);
	wl_resource_set_implementation(resource, &protected_surface_implementation,
				       psurface,
				       destroy_protected_surface);

	psurface->protection_resource = resource;
	psurface->surface = surface;
	psurface->surface_destroy_listener.notify = surface_destroyed;
	wl_resource_add_destroy_listener(surface->resource,
					 &psurface->surface_destroy_listener);
	weston_protected_surface_send_event(psurface,
					    psurface->surface->current_protection);
}

static void
destroy_protection(struct wl_client *client, struct wl_resource *cp_resource)
{
	wl_resource_destroy(cp_resource);
}

static const
struct weston_content_protection_interface content_protection_implementation = {
	destroy_protection,
	get_protection,
};

static void
bind_weston_content_protection(struct wl_client *client, void *data,
			       uint32_t version, uint32_t id)
{
	struct content_protection *cp = data;
	struct wl_resource *resource;

	resource = wl_resource_create(client,
				      &weston_content_protection_interface,
				      1, id);
	if (!resource) {
		wl_client_post_no_memory(client);
		return;
	}

	wl_resource_set_implementation(resource,
				       &content_protection_implementation,
				       cp, NULL);
}
/* Advertise the content-protection support.
 *
 * Calling this function sets up the content-protection support via HDCP.
 * This exposes the global interface, visible to the client, enabling them to
 * request for content-protection for their surfaces according to the type of
 * content.
 */

WL_EXPORT int
weston_compositor_enable_content_protection(struct weston_compositor *compositor)
{
	struct content_protection *cp;

	cp = zalloc(sizeof(*cp));
	if (cp == NULL)
		return -1;
	cp->compositor = compositor;

	compositor->content_protection = cp;
	wl_list_init(&cp->protected_list);
	if (wl_global_create(compositor->wl_display,
			     &weston_content_protection_interface, 1, cp,
			     bind_weston_content_protection) == NULL)
		return -1;

	cp->destroy_listener.notify = cp_destroy_listener;
	wl_signal_add(&compositor->destroy_signal, &cp->destroy_listener);
	cp->debug = weston_compositor_add_log_scope(compositor, "content-protection-debug",
						    "debug-logs for content-protection",
						    NULL, NULL, NULL);
	return 0;
}