summaryrefslogtreecommitdiff
path: root/remoting/remoting-plugin.c
blob: 4d4dea7e32ddd72c05df5e04c5a8987222b43a2c (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/*
 * Copyright © 2018 Renesas Electronics Corp.
 *
 * Based on vaapi-recorder by:
 *   Copyright (c) 2012 Intel Corporation. All Rights Reserved.
 *   Copyright © 2013 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.
 *
 * Authors: IGEL Co., Ltd.
 */

#include "config.h"

#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>

#include <gst/gst.h>
#include <gst/allocators/gstdmabuf.h>
#include <gst/app/gstappsrc.h>
#include <gst/video/gstvideometa.h>

#include <libweston/remoting-plugin.h>
#include <libweston/backend-drm.h>
#include "shared/helpers.h"
#include "shared/timespec-util.h"
#include "shared/weston-drm-fourcc.h"
#include "shared/string-helpers.h"
#include "backend.h"
#include "libweston-internal.h"

#define MAX_RETRY_COUNT	3

struct weston_remoting {
	struct weston_compositor *compositor;
	struct wl_list output_list;
	struct wl_listener destroy_listener;
	const struct weston_drm_virtual_output_api *virtual_output_api;

	GstAllocator *allocator;
};

struct remoted_gstpipe {
	int readfd;
	int writefd;
	struct wl_event_source *source;
};

/* supported gbm format list */
struct remoted_output_support_gbm_format {
	/* GBM_FORMAT_* tokens are strictly aliased with DRM_FORMAT_*, so we
	 * use the latter to avoid a dependency on GBM */
	uint32_t gbm_format;
	const char *gst_format_string;
	GstVideoFormat gst_video_format;
};

static const struct remoted_output_support_gbm_format supported_formats[] = {
	{
		.gbm_format = DRM_FORMAT_XRGB8888,
		.gst_format_string = "BGRx",
		.gst_video_format = GST_VIDEO_FORMAT_BGRx,
	}, {
		.gbm_format = DRM_FORMAT_RGB565,
		.gst_format_string = "RGB16",
		.gst_video_format = GST_VIDEO_FORMAT_RGB16,
	}, {
		.gbm_format = DRM_FORMAT_XRGB2101010,
		.gst_format_string = "r210",
		.gst_video_format = GST_VIDEO_FORMAT_r210,
	}
};

struct remoted_output {
	struct weston_output *output;
	int (*saved_enable)(struct weston_output *output);
	int (*saved_disable)(struct weston_output *output);
	int (*saved_start_repaint_loop)(struct weston_output *output);

	char *host;
	int port;
	char *gst_pipeline;
	const struct remoted_output_support_gbm_format *format;

	struct weston_head *head;

	struct weston_remoting *remoting;
	struct wl_event_source *finish_frame_timer;
	struct wl_list link;
	bool submitted_frame;
	int fence_sync_fd;
	struct wl_event_source *fence_sync_event_source;

	GstElement *pipeline;
	GstAppSrc *appsrc;
	GstBus *bus;
	struct remoted_gstpipe gstpipe;
	GstClockTime start_time;
	int retry_count;
	enum dpms_enum dpms;
};

struct mem_free_cb_data {
	struct remoted_output *output;
	struct drm_fb *output_buffer;
};

struct gst_frame_buffer_data {
	struct remoted_output *output;
	GstBuffer *buffer;
};

/* message type for pipe */
#define GSTPIPE_MSG_BUS_SYNC		1
#define GSTPIPE_MSG_BUFFER_RELEASE	2

struct gstpipe_msg_data {
	int type;
	void *data;
};

static int
remoting_gst_init(struct weston_remoting *remoting)
{
	GError *err = NULL;

	if (!gst_init_check(NULL, NULL, &err)) {
		weston_log("GStreamer initialization error: %s\n",
			   err->message);
		g_error_free(err);
		return -1;
	}

	remoting->allocator = gst_dmabuf_allocator_new();

	return 0;
}

static void
remoting_gst_deinit(struct weston_remoting *remoting)
{
	gst_object_unref(remoting->allocator);
}

static GstBusSyncReply
remoting_gst_bus_sync_handler(GstBus *bus, GstMessage *message,
			      gpointer user_data)
{
	struct remoted_gstpipe *pipe = user_data;
	struct gstpipe_msg_data msg = {
		.type = GSTPIPE_MSG_BUS_SYNC,
		.data = NULL
	};
	ssize_t ret;

	ret = write(pipe->writefd, &msg, sizeof(msg));
	if (ret != sizeof(msg))
		weston_log("ERROR: failed to write, ret=%zd, errno=%d\n",
			   ret, errno);

	return GST_BUS_PASS;
}

static int
remoting_gst_pipeline_init(struct remoted_output *output)
{
	GstCaps *caps;
	GError *err = NULL;
	GstStateChangeReturn ret;
	struct weston_mode *mode = output->output->current_mode;

	if (!output->gst_pipeline) {
		char pipeline_str[1024];
		/* TODO: use encodebin instead of jpegenc */
		snprintf(pipeline_str, sizeof(pipeline_str),
			 "rtpbin name=rtpbin "
			 "appsrc name=src ! videoconvert ! "
			 "video/x-raw,format=I420 ! jpegenc ! rtpjpegpay ! "
			 "rtpbin.send_rtp_sink_0 "
			 "rtpbin.send_rtp_src_0 ! "
			 "udpsink name=sink host=%s port=%d "
			 "rtpbin.send_rtcp_src_0 ! "
			 "udpsink host=%s port=%d sync=false async=false "
			 "udpsrc port=%d ! rtpbin.recv_rtcp_sink_0",
			 output->host, output->port, output->host,
			 output->port + 1, output->port + 2);
		output->gst_pipeline = strdup(pipeline_str);
	}
	weston_log("GST pipeline: %s\n", output->gst_pipeline);

	output->pipeline = gst_parse_launch(output->gst_pipeline, &err);
	if (!output->pipeline) {
		weston_log("Could not create gstreamer pipeline. Error: %s\n",
			   err->message);
		g_error_free(err);
		return -1;
	}

	output->appsrc = (GstAppSrc*)
		gst_bin_get_by_name(GST_BIN(output->pipeline), "src");
	if (!output->appsrc) {
		weston_log("Could not get appsrc from gstreamer pipeline\n");
		goto err;
	}

	/* check sink */
	if (!gst_bin_get_by_name(GST_BIN(output->pipeline), "sink")) {
		weston_log("Could not get sink from gstreamer pipeline\n");
		goto err;
	}

	caps = gst_caps_new_simple("video/x-raw",
				   "format", G_TYPE_STRING,
					     output->format->gst_format_string,
				   "width", G_TYPE_INT, mode->width,
				   "height", G_TYPE_INT, mode->height,
				   "framerate", GST_TYPE_FRACTION,
						mode->refresh, 1000,
				   NULL);
	if (!caps) {
		weston_log("Could not create gstreamer caps.\n");
		goto err;
	}
	g_object_set(G_OBJECT(output->appsrc),
		     "caps", caps,
		     "stream-type", 0,
		     "format", GST_FORMAT_TIME,
		     "is-live", TRUE,
		     NULL);
	gst_caps_unref(caps);

	output->bus = gst_pipeline_get_bus(GST_PIPELINE(output->pipeline));
	if (!output->bus) {
		weston_log("Could not get bus from gstreamer pipeline\n");
		goto err;
	}
	gst_bus_set_sync_handler(output->bus, remoting_gst_bus_sync_handler,
				 &output->gstpipe, NULL);

	output->start_time = 0;
	ret = gst_element_set_state(output->pipeline, GST_STATE_PLAYING);
	if (ret == GST_STATE_CHANGE_FAILURE) {
		weston_log("Couldn't set GST_STATE_PLAYING to pipeline\n");
		goto err;
	}

	return 0;

err:
	gst_object_unref(GST_OBJECT(output->pipeline));
	output->pipeline = NULL;
	return -1;
}

static void
remoting_gst_pipeline_deinit(struct remoted_output *output)
{
	if (!output->pipeline)
		return;

	gst_element_set_state(output->pipeline, GST_STATE_NULL);
	if (output->bus)
		gst_object_unref(GST_OBJECT(output->bus));
	gst_object_unref(GST_OBJECT(output->pipeline));
	output->pipeline = NULL;
}

static int
remoting_output_disable(struct weston_output *output);

static void
remoting_gst_restart(void *data)
{
	struct remoted_output *output = data;

	if (remoting_gst_pipeline_init(output) < 0) {
		weston_log("gst: Could not restart pipeline!!\n");
		remoting_output_disable(output->output);
	}
}

static void
remoting_gst_schedule_restart(struct remoted_output *output)
{
	struct wl_event_loop *loop;
	struct weston_compositor *c = output->remoting->compositor;

	loop = wl_display_get_event_loop(c->wl_display);
	wl_event_loop_add_idle(loop, remoting_gst_restart, output);
}

static void
remoting_gst_bus_message_handler(struct remoted_output *output)
{
	GstMessage *message;
	GError *error;
	gchar *debug;

	/* get message from bus queue */
	message = gst_bus_pop(output->bus);
	if (!message)
		return;

	switch (GST_MESSAGE_TYPE(message)) {
	case GST_MESSAGE_STATE_CHANGED: {
		GstState new_state;
		gst_message_parse_state_changed(message, NULL, &new_state,
						NULL);
		if (!strcmp(GST_OBJECT_NAME(message->src), "sink") &&
		    new_state == GST_STATE_PLAYING)
			output->retry_count = 0;
		break;
	}
	case GST_MESSAGE_WARNING:
		gst_message_parse_warning(message, &error, &debug);
		weston_log("gst: Warning: %s: %s\n",
			   GST_OBJECT_NAME(message->src), error->message);
		break;
	case GST_MESSAGE_ERROR:
		gst_message_parse_error(message, &error, &debug);
		weston_log("gst: Error: %s: %s\n",
			   GST_OBJECT_NAME(message->src), error->message);
		if (output->retry_count < MAX_RETRY_COUNT) {
			output->retry_count++;
			remoting_gst_pipeline_deinit(output);
			remoting_gst_schedule_restart(output);
		} else {
			remoting_output_disable(output->output);
		}
		break;
	default:
		break;
	}
}

static void
remoting_output_buffer_release(struct remoted_output *output, void *buffer)
{
	const struct weston_drm_virtual_output_api *api
		= output->remoting->virtual_output_api;

	api->buffer_released(buffer);
}

static int
remoting_gstpipe_handler(int fd, uint32_t mask, void *data)
{
	ssize_t ret;
	struct gstpipe_msg_data msg;
	struct remoted_output *output = data;

	/* receive message */
	ret = read(fd, &msg, sizeof(msg));
	if (ret != sizeof(msg)) {
		weston_log("ERROR: failed to read, ret=%zd, errno=%d\n",
			   ret, errno);
		remoting_output_disable(output->output);
		return 0;
	}

	switch (msg.type) {
	case GSTPIPE_MSG_BUS_SYNC:
		remoting_gst_bus_message_handler(output);
		break;
	case GSTPIPE_MSG_BUFFER_RELEASE:
		remoting_output_buffer_release(output, msg.data);
		break;
	default:
		weston_log("Received unknown message! msg=%d\n", msg.type);
	}
	return 1;
}

static int
remoting_gstpipe_init(struct weston_compositor *c,
		      struct remoted_output *output)
{
	struct wl_event_loop *loop;
	int fd[2];

	if (pipe2(fd, O_CLOEXEC) == -1)
		return -1;

	output->gstpipe.readfd = fd[0];
	output->gstpipe.writefd = fd[1];
	loop = wl_display_get_event_loop(c->wl_display);
	output->gstpipe.source =
		wl_event_loop_add_fd(loop, output->gstpipe.readfd,
				     WL_EVENT_READABLE,
				     remoting_gstpipe_handler, output);
	if (!output->gstpipe.source) {
		close(fd[0]);
		close(fd[1]);
		return -1;
	}

	return 0;
}

static void
remoting_gstpipe_release(struct remoted_gstpipe *pipe)
{
	wl_event_source_remove(pipe->source);
	close(pipe->readfd);
	close(pipe->writefd);
}

static void
remoting_output_destroy(struct weston_output *output);

static void
weston_remoting_destroy(struct wl_listener *l, void *data)
{
	struct weston_remoting *remoting =
		container_of(l, struct weston_remoting, destroy_listener);
	struct remoted_output *output, *next;

	wl_list_for_each_safe(output, next, &remoting->output_list, link)
		remoting_output_destroy(output->output);

	/* Finalize gstreamer */
	remoting_gst_deinit(remoting);

	wl_list_remove(&remoting->destroy_listener.link);
	free(remoting);
}

static struct weston_remoting *
weston_remoting_get(struct weston_compositor *compositor)
{
	struct wl_listener *listener;
	struct weston_remoting *remoting;

	listener = wl_signal_get(&compositor->destroy_signal,
				 weston_remoting_destroy);
	if (!listener)
		return NULL;

	remoting = wl_container_of(listener, remoting, destroy_listener);
	return remoting;
}

static int
remoting_output_finish_frame_handler(void *data)
{
	struct remoted_output *output = data;
	const struct weston_drm_virtual_output_api *api
		= output->remoting->virtual_output_api;
	struct timespec now;
	int64_t msec;

	if (output->submitted_frame) {
		struct weston_compositor *c = output->remoting->compositor;
		output->submitted_frame = false;
		weston_compositor_read_presentation_clock(c, &now);
		api->finish_frame(output->output, &now, 0);
	}

	if (output->dpms == WESTON_DPMS_ON) {
		msec = millihz_to_nsec(output->output->current_mode->refresh) / 1000000;
		wl_event_source_timer_update(output->finish_frame_timer, msec);
	} else {
		wl_event_source_timer_update(output->finish_frame_timer, 0);
	}
	return 0;
}

static void
remoting_gst_mem_free_cb(struct mem_free_cb_data *cb_data, GstMiniObject *obj)
{
	struct remoted_output *output = cb_data->output;
	struct remoted_gstpipe *pipe = &output->gstpipe;
	struct gstpipe_msg_data msg = {
		.type = GSTPIPE_MSG_BUFFER_RELEASE,
		.data = cb_data->output_buffer
	};
	ssize_t ret;

	ret = write(pipe->writefd, &msg, sizeof(msg));
	if (ret != sizeof(msg))
		weston_log("ERROR: failed to write, ret=%zd, errno=%d\n", ret,
			   errno);
	free(cb_data);
}

static struct remoted_output *
lookup_remoted_output(struct weston_output *output)
{
	struct weston_compositor *c = output->compositor;
	struct weston_remoting *remoting = weston_remoting_get(c);
	struct remoted_output *remoted_output;

	/* XXX: This could happen on the compositor shutdown path with our
	 * destroy listener being removed, and remoting_output_destroy() being
	 * called as a virtual destructor.
	 *
	 * See https://gitlab.freedesktop.org/wayland/weston/-/issues/591 for
	 * an alternative to the shutdown sequence.
	 */
	if (!remoting)
		return NULL;

	wl_list_for_each(remoted_output, &remoting->output_list, link) {
		if (remoted_output->output == output)
			return remoted_output;
	}

	weston_log("%s: %s: could not find output\n", __FILE__, __func__);
	return NULL;
}

static void
remoting_output_gst_push_buffer(struct remoted_output *output,
				GstBuffer *buffer)
{
	struct timespec current_frame_ts;
	GstClockTime ts, current_frame_time;

	weston_compositor_read_presentation_clock(output->remoting->compositor,
						  &current_frame_ts);
	current_frame_time = GST_TIMESPEC_TO_TIME(current_frame_ts);
	if (output->start_time == 0)
		output->start_time = current_frame_time;
	ts = current_frame_time - output->start_time;

	if (GST_CLOCK_TIME_IS_VALID(ts))
		GST_BUFFER_PTS(buffer) = ts;
	else
		GST_BUFFER_PTS(buffer) = GST_CLOCK_TIME_NONE;
	GST_BUFFER_DURATION(buffer) = GST_CLOCK_TIME_NONE;

	gst_app_src_push_buffer(output->appsrc, buffer);
	output->submitted_frame = true;
}

static int
remoting_output_fence_sync_handler(int fd, uint32_t mask, void *data)
{
	struct gst_frame_buffer_data *frame_data = data;
	struct remoted_output *output = frame_data->output;

	remoting_output_gst_push_buffer(output, frame_data->buffer);

	wl_event_source_remove(output->fence_sync_event_source);
	close(output->fence_sync_fd);
	free(frame_data);

	return 0;
}

static int
remoting_output_frame(struct weston_output *output_base, int fd, int stride,
		      struct drm_fb *output_buffer)
{
	struct remoted_output *output = lookup_remoted_output(output_base);
	struct weston_remoting *remoting = output->remoting;
	struct weston_mode *mode;
	const struct weston_drm_virtual_output_api *api
		= output->remoting->virtual_output_api;
	struct wl_event_loop *loop;
	GstBuffer *buf;
	GstMemory *mem;
	gsize offsets[4] = { 0, };
	gint strides[4] = { stride, };
	struct mem_free_cb_data *cb_data;
	struct gst_frame_buffer_data *frame_data;

	if (!output)
		return -1;

	cb_data = zalloc(sizeof *cb_data);
	if (!cb_data)
		return -1;

	mode = output->output->current_mode;
	buf = gst_buffer_new();
	mem = gst_dmabuf_allocator_alloc(remoting->allocator, fd,
					 stride * mode->height);
	gst_buffer_append_memory(buf, mem);
	gst_buffer_add_video_meta_full(buf,
				       GST_VIDEO_FRAME_FLAG_NONE,
				       output->format->gst_video_format,
				       mode->width,
				       mode->height,
				       1,
				       offsets,
				       strides);

	cb_data->output = output;
	cb_data->output_buffer = output_buffer;
	gst_mini_object_weak_ref(GST_MINI_OBJECT(mem),
				 (GstMiniObjectNotify)remoting_gst_mem_free_cb,
				 cb_data);

	output->fence_sync_fd = api->get_fence_sync_fd(output->output);
	/* Push buffer to gstreamer immediately on get_fence_sync_fd failure */
	if (output->fence_sync_fd == -1) {
		remoting_output_gst_push_buffer(output, buf);
		return 0;
	}

	frame_data = zalloc(sizeof *frame_data);
	if (!frame_data) {
		close(output->fence_sync_fd);
		remoting_output_gst_push_buffer(output, buf);
		return 0;
	}

	frame_data->output = output;
	frame_data->buffer = buf;
	loop = wl_display_get_event_loop(remoting->compositor->wl_display);
	output->fence_sync_event_source =
		wl_event_loop_add_fd(loop, output->fence_sync_fd,
				     WL_EVENT_READABLE,
				     remoting_output_fence_sync_handler,
				     frame_data);

	return 0;
}

static void
remoting_output_destroy(struct weston_output *output)
{
	struct remoted_output *remoted_output = lookup_remoted_output(output);
	struct weston_mode *mode, *next;

	if (!remoted_output)
		return;

	weston_head_release(remoted_output->head);

	wl_list_for_each_safe(mode, next, &output->mode_list, link) {
		wl_list_remove(&mode->link);
		free(mode);
	}

	remoting_gst_pipeline_deinit(remoted_output);
	remoting_gstpipe_release(&remoted_output->gstpipe);

	if (remoted_output->host)
		free(remoted_output->host);
	if (remoted_output->gst_pipeline)
		free(remoted_output->gst_pipeline);

	wl_list_remove(&remoted_output->link);
	free(remoted_output->head);
	free(remoted_output);
}

static int
remoting_output_start_repaint_loop(struct weston_output *output)
{
	struct remoted_output *remoted_output = lookup_remoted_output(output);
	int64_t msec;

	remoted_output->saved_start_repaint_loop(output);

	msec = millihz_to_nsec(remoted_output->output->current_mode->refresh)
			/ 1000000;
	wl_event_source_timer_update(remoted_output->finish_frame_timer, msec);

	return 0;
}

static void
remoting_output_set_dpms(struct weston_output *base_output, enum dpms_enum level)
{
	struct remoted_output *output = lookup_remoted_output(base_output);

	if (output->dpms == level)
		return;

	output->dpms = level;
	remoting_output_finish_frame_handler(output);
}

static int
remoting_output_enable(struct weston_output *output)
{
	struct remoted_output *remoted_output = lookup_remoted_output(output);
	struct weston_compositor *c = output->compositor;
	const struct weston_drm_virtual_output_api *api
		= remoted_output->remoting->virtual_output_api;
	struct wl_event_loop *loop;
	int ret;

	api->set_submit_frame_cb(output, remoting_output_frame);

	ret = remoted_output->saved_enable(output);
	if (ret < 0)
		return ret;

	remoted_output->saved_start_repaint_loop = output->start_repaint_loop;
	output->start_repaint_loop = remoting_output_start_repaint_loop;
	output->set_dpms = remoting_output_set_dpms;

	ret = remoting_gst_pipeline_init(remoted_output);
	if (ret < 0) {
		remoted_output->saved_disable(output);
		return ret;
	}

	loop = wl_display_get_event_loop(c->wl_display);
	remoted_output->finish_frame_timer =
		wl_event_loop_add_timer(loop,
					remoting_output_finish_frame_handler,
					remoted_output);

	remoted_output->dpms = WESTON_DPMS_ON;
	return 0;
}

static int
remoting_output_disable(struct weston_output *output)
{
	struct remoted_output *remoted_output = lookup_remoted_output(output);

	wl_event_source_remove(remoted_output->finish_frame_timer);
	remoting_gst_pipeline_deinit(remoted_output);

	return remoted_output->saved_disable(output);
}

static struct weston_output *
remoting_output_create(struct weston_compositor *c, char *name)
{
	struct weston_remoting *remoting = weston_remoting_get(c);
	struct remoted_output *output;
	struct weston_head *head;
	const struct weston_drm_virtual_output_api *api;
	const char *make = "Renesas";
	const char *model = "Virtual Display";
	const char *serial_number = "unknown";
	const char *connector_name = "remoting";
	char *remoting_name;

	if (!name || !strlen(name))
		return NULL;

	api = remoting->virtual_output_api;

	output = zalloc(sizeof *output);
	if (!output)
		return NULL;

	head = zalloc(sizeof *head);
	if (!head)
		goto err;

	if (remoting_gstpipe_init(c, output) < 0) {
		weston_log("Can not create pipe for gstreamer\n");
		goto err;
	}

	output->output = api->create_output(c, name, remoting_output_destroy);
	if (!output->output) {
		weston_log("Can not create virtual output\n");
		goto err;
	}

	output->saved_enable = output->output->enable;
	output->output->enable = remoting_output_enable;
	output->saved_disable = output->output->disable;
	output->output->disable = remoting_output_disable;
	output->remoting = remoting;
	wl_list_insert(remoting->output_list.prev, &output->link);

	str_printf(&remoting_name, "%s-%s", connector_name, name);
	weston_head_init(head, remoting_name);
	weston_head_set_subpixel(head, WL_OUTPUT_SUBPIXEL_NONE);
	weston_head_set_monitor_strings(head, make, model, serial_number);
	head->compositor = c;

	weston_output_attach_head(output->output, head);
	output->head = head;

	/* set XRGB8888 format */
	output->format = &supported_formats[0];
	free(remoting_name);

	return output->output;

err:
	if (output->gstpipe.source)
		remoting_gstpipe_release(&output->gstpipe);
	if (head)
		free(head);
	free(output);
	return NULL;
}

static bool
remoting_output_is_remoted(struct weston_output *output)
{
	struct remoted_output *remoted_output = lookup_remoted_output(output);

	if (remoted_output)
		return true;

	return false;
}

static int
remoting_output_set_mode(struct weston_output *output, const char *modeline)
{
	struct weston_mode *mode;
	int n, width, height, refresh = 0;

	if (!remoting_output_is_remoted(output)) {
		weston_log("Output is not remoted.\n");
		return -1;
	}

	if (!modeline)
		return -1;

	n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
	if (n != 2 && n != 3)
		return -1;

	mode = zalloc(sizeof *mode);
	if (!mode)
		return -1;

	mode->flags = WL_OUTPUT_MODE_CURRENT;
	mode->width = width;
	mode->height = height;
	mode->refresh = (refresh ? refresh : 60) * 1000LL;

	wl_list_insert(output->mode_list.prev, &mode->link);

	output->current_mode = mode;

	return 0;
}

static void
remoting_output_set_gbm_format(struct weston_output *output,
			       const char *gbm_format)
{
	struct remoted_output *remoted_output = lookup_remoted_output(output);
	const struct weston_drm_virtual_output_api *api;
	uint32_t format, i;

	if (!remoted_output)
		return;

	api = remoted_output->remoting->virtual_output_api;
	format = api->set_gbm_format(output, gbm_format);

	for (i = 0; i < ARRAY_LENGTH(supported_formats); i++) {
		if (format == supported_formats[i].gbm_format) {
			remoted_output->format = &supported_formats[i];
			return;
		}
	}
}

static void
remoting_output_set_seat(struct weston_output *output, const char *seat)
{
	/* for now, nothing todo */
}

static void
remoting_output_set_host(struct weston_output *output, char *host)
{
	struct remoted_output *remoted_output = lookup_remoted_output(output);

	if (!remoted_output)
		return;

	if (remoted_output->host)
		free(remoted_output->host);
	remoted_output->host = strdup(host);
}

static void
remoting_output_set_port(struct weston_output *output, int port)
{
	struct remoted_output *remoted_output = lookup_remoted_output(output);

	if (remoted_output)
		remoted_output->port = port;
}

static void
remoting_output_set_gst_pipeline(struct weston_output *output,
				 char *gst_pipeline)
{
	struct remoted_output *remoted_output = lookup_remoted_output(output);

	if (!remoted_output)
		return;

	if (remoted_output->gst_pipeline)
		free(remoted_output->gst_pipeline);
	remoted_output->gst_pipeline = strdup(gst_pipeline);
}

static const struct weston_remoting_api remoting_api = {
	remoting_output_create,
	remoting_output_is_remoted,
	remoting_output_set_mode,
	remoting_output_set_gbm_format,
	remoting_output_set_seat,
	remoting_output_set_host,
	remoting_output_set_port,
	remoting_output_set_gst_pipeline,
};

WL_EXPORT int
weston_module_init(struct weston_compositor *compositor)
{
	int ret;
	struct weston_remoting *remoting;
	const struct weston_drm_virtual_output_api *api =
		weston_drm_virtual_output_get_api(compositor);

	if (!api)
		return -1;

	remoting = zalloc(sizeof *remoting);
	if (!remoting)
		return -1;

	if (!weston_compositor_add_destroy_listener_once(compositor,
							 &remoting->destroy_listener,
							 weston_remoting_destroy)) {
		free(remoting);
		return 0;
	}

	remoting->virtual_output_api = api;
	remoting->compositor = compositor;
	wl_list_init(&remoting->output_list);

	ret = weston_plugin_api_register(compositor, WESTON_REMOTING_API_NAME,
					 &remoting_api, sizeof(remoting_api));

	if (ret < 0) {
		weston_log("Failed to register remoting API.\n");
		goto failed;
	}

	/* Initialize gstreamer */
	ret = remoting_gst_init(remoting);
	if (ret < 0) {
		weston_log("Failed to initialize gstreamer.\n");
		goto failed;
	}

	return 0;

failed:
	wl_list_remove(&remoting->destroy_listener.link);
	free(remoting);
	return -1;
}