summaryrefslogtreecommitdiff
path: root/libdleyna/renderer/server.c
blob: 425d8f55244d31caa04b9ddb760efe57bee2bed5 (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
/*
 * dLeyna
 *
 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Mark Ryan <mark.d.ryan@intel.com>
 *
 */


#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

#include <libdleyna/core/connector.h>
#include <libdleyna/core/control-point.h>
#include <libdleyna/core/error.h>
#include <libdleyna/core/log.h>
#include <libdleyna/core/task-processor.h>

#include "async.h"
#include "control-point-renderer.h"
#include "device.h"
#include "prop-defs.h"
#include "server.h"
#include "upnp.h"

#ifdef UA_PREFIX
	#define DLR_PRG_NAME UA_PREFIX " dLeyna/" VERSION
#else
	#define DLR_PRG_NAME "dLeyna/" VERSION
#endif

#define DLR_INTERFACE_GET_VERSION "GetVersion"
#define DLR_INTERFACE_GET_RENDERERS "GetRenderers"
#define DLR_INTERFACE_RESCAN "Rescan"
#define DLR_INTERFACE_RELEASE "Release"

#define DLR_INTERFACE_FOUND_RENDERER "FoundRenderer"
#define DLR_INTERFACE_LOST_RENDERER "LostRenderer"

#define DLR_INTERFACE_HOST_FILE "HostFile"
#define DLR_INTERFACE_REMOVE_FILE "RemoveFile"

#define DLR_INTERFACE_VERSION "Version"
#define DLR_INTERFACE_RENDERERS "Renderers"

#define DLR_INTERFACE_PATH "Path"
#define DLR_INTERFACE_URI "Uri"
#define DLR_INTERFACE_ID "Id"
#define DLR_INTERFACE_METADATA "Metadata"

#define DLR_INTERFACE_CHANGED_PROPERTIES "changed_properties"
#define DLR_INTERFACE_INVALIDATED_PROPERTIES "invalidated_properties"
#define DLR_INTERFACE_GET "Get"
#define DLR_INTERFACE_GET_ALL "GetAll"
#define DLR_INTERFACE_SET "Set"
#define DLR_INTERFACE_INTERFACE_NAME "interface_name"
#define DLR_INTERFACE_PROPERTY_NAME "property_name"
#define DLR_INTERFACE_PROPERTIES_VALUE "properties"
#define DLR_INTERFACE_VALUE "value"
#define DLR_INTERFACE_OFFSET "offset"
#define DLR_INTERFACE_POSITION "position"
#define DLR_INTERFACE_TRACKID "trackid"
#define DLR_INTERFACE_TRACK_NUMBER "TrackNumber"

#define DLR_INTERFACE_RAISE "Raise"
#define DLR_INTERFACE_QUIT "Quit"
#define DLR_INTERFACE_PLAY "Play"
#define DLR_INTERFACE_PLAY_PAUSE "PlayPause"
#define DLR_INTERFACE_NEXT "Next"
#define DLR_INTERFACE_PREVIOUS "Previous"
#define DLR_INTERFACE_PAUSE "Pause"
#define DLR_INTERFACE_STOP "Stop"
#define DLR_INTERFACE_OPEN_URI "OpenUri"
#define DLR_INTERFACE_OPEN_URI_EX "OpenUriEx"
#define DLR_INTERFACE_SET_URI "SetUri"
#define DLR_INTERFACE_SEEK "Seek"
#define DLR_INTERFACE_SET_POSITION "SetPosition"
#define DLR_INTERFACE_GOTO_TRACK "GotoTrack"

#define DLR_INTERFACE_CANCEL "Cancel"
#define DLR_INTERFACE_GET_ICON "GetIcon"
#define DLR_INTERFACE_RESOLUTION "Resolution"
#define DLR_INTERFACE_ICON_BYTES "Bytes"
#define DLR_INTERFACE_MIME_TYPE "MimeType"
#define DLR_INTERFACE_REQ_MIME_TYPE "RequestedMimeType"

typedef struct dlr_context_t_ dlr_context_t;
struct dlr_context_t_ {
	guint dlr_id;
	dleyna_connector_id_t connection;
	guint watchers;
	dleyna_task_processor_t *processor;
	const dleyna_connector_t *connector;
	dlr_upnp_t *upnp;
	dleyna_settings_t *settings;
};

static dlr_context_t g_context;

static const gchar g_root_introspection[] =
	"<node>"
	"  <interface name='"DLEYNA_SERVER_INTERFACE_MANAGER"'>"
	"    <method name='"DLR_INTERFACE_GET_VERSION"'>"
	"      <arg type='s' name='"DLR_INTERFACE_VERSION"'"
	"           direction='out'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_RELEASE"'>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_GET_RENDERERS"'>"
	"      <arg type='as' name='"DLR_INTERFACE_RENDERERS"'"
	"           direction='out'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_RESCAN"'>"
	"    </method>"
	"    <signal name='"DLR_INTERFACE_FOUND_RENDERER"'>"
	"      <arg type='s' name='"DLR_INTERFACE_PATH"'/>"
	"    </signal>"
	"    <signal name='"DLR_INTERFACE_LOST_RENDERER"'>"
	"      <arg type='s' name='"DLR_INTERFACE_PATH"'/>"
	"    </signal>"
	"  </interface>"
	"</node>";

static const gchar g_server_introspection[] =
	"<node>"
	"  <interface name='"DLR_INTERFACE_PROPERTIES"'>"
	"    <method name='"DLR_INTERFACE_GET"'>"
	"      <arg type='s' name='"DLR_INTERFACE_INTERFACE_NAME"'"
	"           direction='in'/>"
	"      <arg type='s' name='"DLR_INTERFACE_PROPERTY_NAME"'"
	"           direction='in'/>"
	"      <arg type='v' name='"DLR_INTERFACE_VALUE"'"
	"           direction='out'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_GET_ALL"'>"
	"      <arg type='s' name='"DLR_INTERFACE_INTERFACE_NAME"'"
	"           direction='in'/>"
	"      <arg type='a{sv}' name='"DLR_INTERFACE_PROPERTIES_VALUE"'"
	"           direction='out'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_SET"'>"
	"      <arg type='s' name='"DLR_INTERFACE_INTERFACE_NAME"'"
	"           direction='in'/>"
	"      <arg type='s' name='"DLR_INTERFACE_PROPERTY_NAME"'"
	"           direction='in'/>"
	"      <arg type='v' name='"DLR_INTERFACE_VALUE"'"
	"           direction='in'/>"
	"    </method>"
	"    <signal name='"DLR_INTERFACE_PROPERTIES_CHANGED"'>"
	"      <arg type='s' name='"DLR_INTERFACE_INTERFACE_NAME"'/>"
	"      <arg type='a{sv}' name='"DLR_INTERFACE_CHANGED_PROPERTIES"'/>"
	"      <arg type='as' name='"DLR_INTERFACE_INVALIDATED_PROPERTIES"'/>"
	"    </signal>"
	"  </interface>"
	"  <interface name='"DLR_INTERFACE_SERVER"'>"
	"    <method name='"DLR_INTERFACE_RAISE"'>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_QUIT"'>"
	"    </method>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_CAN_QUIT"'"
	"       access='read'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_CAN_RAISE"'"
	"       access='read'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_CAN_SET_FULLSCREEN"'"
	"       access='read'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_HAS_TRACK_LIST"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_IDENTITY"'"
	"       access='read'/>"
	"    <property type='as' name='"DLR_INTERFACE_PROP_SUPPORTED_URIS"'"
	"       access='read'/>"
	"    <property type='as' name='"DLR_INTERFACE_PROP_SUPPORTED_MIME"'"
	"       access='read'/>"
	"  </interface>"
	"  <interface name='"DLR_INTERFACE_PLAYER"'>"
	"    <method name='"DLR_INTERFACE_PLAY"'>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_PAUSE"'>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_PLAY_PAUSE"'>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_STOP"'>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_NEXT"'>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_PREVIOUS"'>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_OPEN_URI"'>"
	"      <arg type='s' name='"DLR_INTERFACE_URI"'"
	"           direction='in'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_OPEN_URI_EX"'>"
	"      <arg type='s' name='"DLR_INTERFACE_URI"'"
	"           direction='in'/>"
	"      <arg type='s' name='"DLR_INTERFACE_METADATA"'"
	"           direction='in'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_SET_URI"'>"
	"      <arg type='s' name='"DLR_INTERFACE_URI"'"
	"           direction='in'/>"
	"      <arg type='s' name='"DLR_INTERFACE_METADATA"'"
	"           direction='in'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_SEEK"'>"
	"      <arg type='x' name='"DLR_INTERFACE_OFFSET"'"
	"           direction='in'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_SET_POSITION"'>"
	"      <arg type='o' name='"DLR_INTERFACE_TRACKID"'"
	"           direction='in'/>"
	"      <arg type='x' name='"DLR_INTERFACE_POSITION"'"
	"           direction='in'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_GOTO_TRACK"'>"
	"      <arg type='u' name='"DLR_INTERFACE_TRACK_NUMBER"'"
	"           direction='in'/>"
	"    </method>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_PLAYBACK_STATUS"'"
	"       access='read'/>"
	"    <property type='d' name='"DLR_INTERFACE_PROP_RATE"'"
	"       access='readwrite'/>"
	"    <property type='d' name='"DLR_INTERFACE_PROP_MINIMUM_RATE"'"
	"       access='read'/>"
	"    <property type='d' name='"DLR_INTERFACE_PROP_MAXIMUM_RATE"'"
	"       access='read'/>"
	"    <property type='ad'"
	"       name='"DLR_INTERFACE_PROP_TRANSPORT_PLAY_SPEEDS"'"
	"       access='read'/>"
	"    <property type='d' name='"DLR_INTERFACE_PROP_VOLUME"'"
	"       access='readwrite'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_CAN_PLAY"'"
	"       access='read'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_CAN_SEEK"'"
	"       access='read'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_CAN_CONTROL"'"
	"       access='read'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_CAN_PAUSE"'"
	"       access='read'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_CAN_NEXT"'"
	"       access='read'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_CAN_PREVIOUS"'"
	"       access='read'/>"
	"    <property type='x' name='"DLR_INTERFACE_PROP_POSITION"'"
	"       access='read'/>"
	"    <property type='a{sv}' name='"DLR_INTERFACE_PROP_METADATA"'"
	"       access='read'/>"
	"    <property type='u' name='"DLR_INTERFACE_PROP_CURRENT_TRACK"'"
	"       access='read'/>"
	"    <property type='u' name='"DLR_INTERFACE_PROP_NUMBER_OF_TRACKS"'"
	"       access='read'/>"
	"    <property type='b' name='"DLR_INTERFACE_PROP_MUTE"'"
	"       access='readwrite'/>"
	"  </interface>"
	"  <interface name='"DLEYNA_INTERFACE_PUSH_HOST"'>"
	"    <method name='"DLR_INTERFACE_HOST_FILE"'>"
	"      <arg type='s' name='"DLR_INTERFACE_PATH"'"
	"           direction='in'/>"
	"      <arg type='s' name='"DLR_INTERFACE_URI"'"
	"           direction='out'/>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_REMOVE_FILE"'>"
	"      <arg type='s' name='"DLR_INTERFACE_PATH"'"
	"           direction='in'/>"
	"    </method>"
	"  </interface>"
	"  <interface name='"DLEYNA_SERVER_INTERFACE_RENDERER_DEVICE"'>"
	"    <method name='"DLR_INTERFACE_CANCEL"'>"
	"    </method>"
	"    <method name='"DLR_INTERFACE_GET_ICON"'>"
	"      <arg type='s' name='"DLR_INTERFACE_REQ_MIME_TYPE"'"
	"           direction='in'/>"
	"      <arg type='s' name='"DLR_INTERFACE_RESOLUTION"'"
	"           direction='in'/>"
	"      <arg type='ay' name='"DLR_INTERFACE_ICON_BYTES"'"
	"           direction='out'/>"
	"      <arg type='s' name='"DLR_INTERFACE_MIME_TYPE"'"
	"           direction='out'/>"
	"    </method>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_DEVICE_TYPE"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_UDN"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_FRIENDLY_NAME"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_ICON_URL"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_MANUFACTURER"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_MANUFACTURER_URL"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_MODEL_DESCRIPTION"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_MODEL_NAME"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_MODEL_NUMBER"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_SERIAL_NUMBER"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_PRESENTATION_URL"'"
	"       access='read'/>"
	"    <property type='s' name='"DLR_INTERFACE_PROP_PROTOCOL_INFO"'"
	"       access='read'/>"
	"  </interface>"
	"</node>";

static void prv_process_task(dleyna_task_atom_t *task, gpointer user_data);

static void prv_dlr_method_call(dleyna_connector_id_t conn,
				const gchar *sender,
				const gchar *object,
				const gchar *interface,
				const gchar *method,
				GVariant *parameters,
				dleyna_connector_msg_id_t invocation);

static void prv_dlr_device_method_call(dleyna_connector_id_t conn,
				       const gchar *sender,
				       const gchar *object,
				       const gchar *interface,
				       const gchar *method,
				       GVariant *parameters,
				       dleyna_connector_msg_id_t invocation);

static void prv_props_method_call(dleyna_connector_id_t conn,
				  const gchar *sender,
				  const gchar *object,
				  const gchar *interface,
				  const gchar *method,
				  GVariant *parameters,
				  dleyna_connector_msg_id_t invocation);

static void prv_dlr_player_method_call(dleyna_connector_id_t conn,
				       const gchar *sender,
				       const gchar *object,
				       const gchar *interface,
				       const gchar *method,
				       GVariant *parameters,
				       dleyna_connector_msg_id_t invocation);

static void prv_dlr_push_host_method_call(dleyna_connector_id_t conn,
					  const gchar *sender,
					  const gchar *object,
					  const gchar *interface,
					  const gchar *method,
					  GVariant *parameters,
					  dleyna_connector_msg_id_t invocation);

static void prv_renderer_device_method_call(
					dleyna_connector_id_t conn,
					const gchar *sender,
					const gchar *object,
					const gchar *interface,
					const gchar *method,
					GVariant *parameters,
					dleyna_connector_msg_id_t invocation);

static const dleyna_connector_dispatch_cb_t g_root_vtables[1] = {
	prv_dlr_method_call
};

static const dleyna_connector_dispatch_cb_t
				g_server_vtables[DLR_INTERFACE_INFO_MAX] = {
	/* MUST be in the exact same order as g_msu_server_introspection */
	prv_props_method_call,
	prv_dlr_device_method_call,
	prv_dlr_player_method_call,
	prv_dlr_push_host_method_call,
	prv_renderer_device_method_call
};

const dleyna_connector_t *dlr_renderer_get_connector(void)
{
	return g_context.connector;
}

dleyna_task_processor_t *dlr_renderer_service_get_task_processor(void)
{
	return g_context.processor;
}

dlr_upnp_t *dlr_renderer_service_get_upnp(void)
{
	return g_context.upnp;
}

static void prv_process_sync_task(dlr_task_t *task)
{
	GError *error;

	switch (task->type) {
	case DLR_TASK_GET_VERSION:
		task->result = g_variant_ref_sink(g_variant_new_string(
								VERSION));
		dlr_task_complete(task);
		break;
	case DLR_TASK_GET_SERVERS:
		task->result = dlr_upnp_get_server_ids(g_context.upnp);
		dlr_task_complete(task);
		break;
	case DLR_TASK_RESCAN:
		dlr_upnp_rescan(g_context.upnp);
		dlr_task_complete(task);
		break;
	case DLR_TASK_RAISE:
	case DLR_TASK_QUIT:
		error = g_error_new(DLEYNA_SERVER_ERROR,
				    DLEYNA_ERROR_NOT_SUPPORTED,
				    "Command not supported.");
		dlr_task_fail(task, error);
		g_error_free(error);
		break;
	default:
		goto finished;
		break;
	}

	dleyna_task_queue_task_completed(task->atom.queue_id);

finished:
	return;
}

static void prv_async_task_complete(dlr_task_t *task, GError *error)
{
	DLEYNA_LOG_DEBUG("Enter");

	if (!error) {
		dlr_task_complete(task);
	} else {
		dlr_task_fail(task, error);
		g_error_free(error);
	}

	dleyna_task_queue_task_completed(task->atom.queue_id);

	DLEYNA_LOG_DEBUG("Exit");
}

static void prv_process_async_task(dlr_task_t *task)
{
	dlr_async_task_t *async_task = (dlr_async_task_t *)task;

	DLEYNA_LOG_DEBUG("Enter");

	async_task->cancellable = g_cancellable_new();

	switch (task->type) {
	case DLR_TASK_GET_PROP:
		dlr_upnp_get_prop(g_context.upnp, task,
				  prv_async_task_complete);
		break;
	case DLR_TASK_GET_ALL_PROPS:
		dlr_upnp_get_all_props(g_context.upnp, task,
				       prv_async_task_complete);
		break;
	case DLR_TASK_SET_PROP:
		dlr_upnp_set_prop(g_context.upnp, task,
				  prv_async_task_complete);
		break;
	case DLR_TASK_PLAY:
		dlr_upnp_play(g_context.upnp, task,
			      prv_async_task_complete);
		break;
	case DLR_TASK_PAUSE:
		dlr_upnp_pause(g_context.upnp, task,
			       prv_async_task_complete);
		break;
	case DLR_TASK_PLAY_PAUSE:
		dlr_upnp_play_pause(g_context.upnp, task,
				    prv_async_task_complete);
		break;
	case DLR_TASK_STOP:
		dlr_upnp_stop(g_context.upnp, task,
			      prv_async_task_complete);
		break;
	case DLR_TASK_NEXT:
		dlr_upnp_next(g_context.upnp, task,
			      prv_async_task_complete);
		break;
	case DLR_TASK_PREVIOUS:
		dlr_upnp_previous(g_context.upnp, task,
				  prv_async_task_complete);
		break;
	case DLR_TASK_OPEN_URI:
	case DLR_TASK_SET_URI:
		dlr_upnp_open_uri(g_context.upnp, task,
				  prv_async_task_complete);
		break;
	case DLR_TASK_SEEK:
		dlr_upnp_seek(g_context.upnp, task,
			      prv_async_task_complete);
		break;
	case DLR_TASK_SET_POSITION:
		dlr_upnp_set_position(g_context.upnp, task,
				      prv_async_task_complete);
		break;
	case DLR_TASK_GOTO_TRACK:
		dlr_upnp_goto_track(g_context.upnp, task,
				    prv_async_task_complete);
		break;
	case DLR_TASK_HOST_URI:
		dlr_upnp_host_uri(g_context.upnp, task,
				  prv_async_task_complete);
		break;
	case DLR_TASK_REMOVE_URI:
		dlr_upnp_remove_uri(g_context.upnp, task,
				    prv_async_task_complete);
		break;
	case DLR_TASK_GET_ICON:
		dlr_upnp_get_icon(g_context.upnp, task,
				  prv_async_task_complete);
		break;
	default:
		break;
	}

	DLEYNA_LOG_DEBUG("Exit");
}

static void prv_process_task(dleyna_task_atom_t *task, gpointer user_data)
{
	dlr_task_t *client_task = (dlr_task_t *)task;

	if (client_task->synchronous)
		prv_process_sync_task(client_task);
	else
		prv_process_async_task(client_task);
}

static void prv_cancel_task(dleyna_task_atom_t *task, gpointer user_data)
{
	dlr_task_cancel((dlr_task_t *)task);
}

static void prv_delete_task(dleyna_task_atom_t *task, gpointer user_data)
{
	dlr_task_delete((dlr_task_t *)task);
}

static void prv_remove_client(const gchar *name)
{
	dleyna_task_processor_remove_queues_for_source(g_context.processor,
						       name);

	dlr_upnp_lost_client(g_context.upnp, name);

	g_context.watchers--;
	if (g_context.watchers == 0)
		if (!dleyna_settings_is_never_quit(g_context.settings))
			dleyna_task_processor_set_quitting(g_context.processor);
}

static void prv_lost_client(const gchar *name)
{
	DLEYNA_LOG_INFO("Client %s lost", name);
	prv_remove_client(name);
}

static void prv_control_point_initialize(const dleyna_connector_t *connector,
					 dleyna_task_processor_t *processor,
					 dleyna_settings_t *settings)
{
	memset(&g_context, 0, sizeof(g_context));

	g_context.processor = processor;
	g_context.settings = settings;
	g_context.connector = connector;
	g_context.connector->set_client_lost_cb(prv_lost_client);

	g_set_prgname(DLR_PRG_NAME);
}

static void prv_control_point_stop_service(void)
{
	if (g_context.upnp) {
		dlr_upnp_unsubscribe(g_context.upnp);
		dlr_upnp_delete(g_context.upnp);
	}

	if (g_context.connection) {
		if (g_context.dlr_id)
			g_context.connector->unpublish_object(
							g_context.connection,
							g_context.dlr_id);
	}
}

static void prv_control_point_free(void)
{
}

static void prv_add_task(dlr_task_t *task, const gchar *source,
			 const gchar *sink)
{
	const dleyna_task_queue_key_t *queue_id;

	if (g_context.connector->watch_client(source))
		g_context.watchers++;

	queue_id = dleyna_task_processor_lookup_queue(g_context.processor,
						      source, sink);
	if (!queue_id)
		queue_id = dleyna_task_processor_add_queue(
					g_context.processor,
					source,
					sink,
					DLEYNA_TASK_QUEUE_FLAG_AUTO_START,
					prv_process_task,
					prv_cancel_task,
					prv_delete_task);

	dleyna_task_queue_add_task(queue_id, &task->atom);
}

static void prv_dlr_method_call(dleyna_connector_id_t conn,
				const gchar *sender, const gchar *object,
				const gchar *interface,
				const gchar *method, GVariant *parameters,
				dleyna_connector_msg_id_t invocation)
{
	dlr_task_t *task;

	DLEYNA_LOG_INFO("Calling %s method", method);

	if (!strcmp(method, DLR_INTERFACE_RELEASE)) {
		g_context.connector->unwatch_client(sender);
		prv_remove_client(sender);
		g_context.connector->return_response(invocation, NULL);
	} else {
		if (!strcmp(method, DLR_INTERFACE_GET_VERSION))
			task = dlr_task_get_version_new(invocation);
		else if (!strcmp(method, DLR_INTERFACE_GET_RENDERERS))
			task = dlr_task_get_servers_new(invocation);
		else if (!strcmp(method, DLR_INTERFACE_RESCAN))
			task = dlr_task_rescan_new(invocation);
		else
			goto finished;

		prv_add_task(task, sender, DLR_RENDERER_SINK);
	}

finished:

	return;
}

static const gchar *prv_get_device_id(const gchar *object, GError **error)
{
	dlr_device_t *device;

	device = dlr_device_from_path(object,
				dlr_upnp_get_server_udn_map(g_context.upnp));


	if (!device) {
		DLEYNA_LOG_WARNING("Cannot locate device for %s", object);

		*error = g_error_new(DLEYNA_SERVER_ERROR,
				     DLEYNA_ERROR_OBJECT_NOT_FOUND,
				     "Cannot locate device corresponding to the specified path");
		goto on_error;
	}

	return device->path;

on_error:

	return NULL;
}

static void prv_props_method_call(dleyna_connector_id_t conn,
				  const gchar *sender,
				  const gchar *object,
				  const gchar *interface,
				  const gchar *method,
				  GVariant *parameters,
				  dleyna_connector_msg_id_t invocation)
{
	dlr_task_t *task;
	const gchar *device_id;
	GError *error = NULL;

	device_id = prv_get_device_id(object, &error);
	if (!device_id) {
		g_context.connector->return_error(invocation, error);
		g_error_free(error);

		goto finished;
	}

	if (!strcmp(method, DLR_INTERFACE_GET_ALL))
		task = dlr_task_get_props_new(invocation, object, parameters);
	else if (!strcmp(method, DLR_INTERFACE_GET))
		task = dlr_task_get_prop_new(invocation, object, parameters);
	else if (!strcmp(method, DLR_INTERFACE_SET))
		task = dlr_task_set_prop_new(invocation, object, parameters);
	else
		goto finished;

	prv_add_task(task, sender, device_id);

finished:

	return;
}

static void prv_dlr_device_method_call(dleyna_connector_id_t conn,
				       const gchar *sender,
				       const gchar *object,
				       const gchar *interface,
				       const gchar *method,
				       GVariant *parameters,
				       dleyna_connector_msg_id_t invocation)
{
	dlr_task_t *task;
	const gchar *device_id;
	GError *error = NULL;

	device_id = prv_get_device_id(object, &error);
	if (!device_id) {
		g_context.connector->return_error(invocation, error);
		g_error_free(error);

		goto finished;
	}

	if (!strcmp(method, DLR_INTERFACE_RAISE))
		task = dlr_task_raise_new(invocation);
	else if (!strcmp(method, DLR_INTERFACE_QUIT))
		task = dlr_task_quit_new(invocation);
	else
		goto finished;

	prv_add_task(task, sender, device_id);

finished:

	return;
}

static void prv_dlr_player_method_call(dleyna_connector_id_t conn,
				       const gchar *sender,
				       const gchar *object,
				       const gchar *interface,
				       const gchar *method,
				       GVariant *parameters,
				       dleyna_connector_msg_id_t invocation)
{
	dlr_task_t *task;
	const gchar *device_id;
	GError *error = NULL;

	device_id = prv_get_device_id(object, &error);
	if (!device_id) {
		g_context.connector->return_error(invocation, error);
		g_error_free(error);

		goto finished;
	}

	if (!strcmp(method, DLR_INTERFACE_PLAY))
		task = dlr_task_play_new(invocation, object);
	else if (!strcmp(method, DLR_INTERFACE_PAUSE))
		task = dlr_task_pause_new(invocation, object);
	else if (!strcmp(method, DLR_INTERFACE_PLAY_PAUSE))
		task = dlr_task_play_pause_new(invocation, object);
	else if (!strcmp(method, DLR_INTERFACE_STOP))
		task = dlr_task_stop_new(invocation, object);
	else if (!strcmp(method, DLR_INTERFACE_NEXT))
		task = dlr_task_next_new(invocation, object);
	else if (!strcmp(method, DLR_INTERFACE_PREVIOUS))
		task = dlr_task_previous_new(invocation, object);
	else if (!strcmp(method, DLR_INTERFACE_OPEN_URI))
		task = dlr_task_open_uri_new(invocation, object, parameters);
	else if (!strcmp(method, DLR_INTERFACE_OPEN_URI_EX))
		task = dlr_task_open_uri_ex_new(invocation, object, parameters);
	else if (!strcmp(method, DLR_INTERFACE_SET_URI))
		task = dlr_task_set_uri_new(invocation, object, parameters);
	else if (!strcmp(method, DLR_INTERFACE_SEEK))
		task = dlr_task_seek_new(invocation, object, parameters);
	else if (!strcmp(method, DLR_INTERFACE_SET_POSITION))
		task = dlr_task_set_position_new(invocation, object,
						 parameters);
	else if (!strcmp(method, DLR_INTERFACE_GOTO_TRACK))
		task = dlr_task_goto_track_new(invocation, object, parameters);
	else
		goto finished;

	prv_add_task(task, sender, device_id);

finished:

	return;
}

static void prv_dlr_push_host_method_call(dleyna_connector_id_t conn,
					  const gchar *sender,
					  const gchar *object,
					  const gchar *interface,
					  const gchar *method,
					  GVariant *parameters,
					  dleyna_connector_msg_id_t invocation)
{
	dlr_task_t *task;
	const gchar *device_id;
	GError *error = NULL;

	device_id = prv_get_device_id(object, &error);
	if (!device_id) {
		g_context.connector->return_error(invocation, error);
		g_error_free(error);

		goto on_error;
	}

	if (!strcmp(method, DLR_INTERFACE_HOST_FILE))
		task = dlr_task_host_uri_new(invocation, object, sender,
					     parameters);
	else if (!strcmp(method, DLR_INTERFACE_REMOVE_FILE))
		task = dlr_task_remove_uri_new(invocation, object, sender,
					       parameters);
	else
		goto on_error;

	prv_add_task(task, sender, device_id);

on_error:

	return;
}

static void prv_renderer_device_method_call(
					dleyna_connector_id_t conn,
					const gchar *sender,
					const gchar *object,
					const gchar *interface,
					const gchar *method,
					GVariant *parameters,
					dleyna_connector_msg_id_t invocation)
{
	dlr_task_t *task;
	const gchar *device_id = NULL;
	GError *error = NULL;
	const dleyna_task_queue_key_t *queue_id;

	device_id = prv_get_device_id(object, &error);
	if (!device_id) {
		g_context.connector->return_error(invocation, error);
		g_error_free(error);

		goto finished;
	}

	if (!strcmp(method, DLR_INTERFACE_CANCEL)) {
		queue_id = dleyna_task_processor_lookup_queue(
							g_context.processor,
							sender, device_id);
		if (queue_id)
			dleyna_task_processor_cancel_queue(queue_id);

		g_context.connector->return_response(invocation, NULL);
	} else if (!strcmp(method, DLR_INTERFACE_GET_ICON)) {
		task = dlr_task_get_icon_new(invocation, object, parameters);

		prv_add_task(task, sender, device_id);
	}

finished:

	return;
}

static void prv_found_media_server(const gchar *path)
{
	DLEYNA_LOG_INFO("New media server %s", path);

	(void) g_context.connector->notify(g_context.connection,
					   DLEYNA_SERVER_OBJECT,
					   DLEYNA_SERVER_INTERFACE_MANAGER,
					   DLR_INTERFACE_FOUND_RENDERER,
					   g_variant_new("(s)", path),
					   NULL);
}

static void prv_lost_media_server(const gchar *path)
{
	DLEYNA_LOG_INFO("Lost %s", path);

	(void) g_context.connector->notify(g_context.connection,
					   DLEYNA_SERVER_OBJECT,
					   DLEYNA_SERVER_INTERFACE_MANAGER,
					   DLR_INTERFACE_LOST_RENDERER,
					   g_variant_new("(s)", path),
					   NULL);

	dleyna_task_processor_remove_queues_for_sink(g_context.processor, path);
}

static gboolean prv_control_point_start_service(
					dleyna_connector_id_t connection)
{
	gboolean retval = TRUE;

	g_context.connection = connection;

	g_context.dlr_id = g_context.connector->publish_object(
							connection,
							DLEYNA_SERVER_OBJECT,
							TRUE,
							0,
							g_root_vtables);

	if (g_context.dlr_id)
		g_context.upnp = dlr_upnp_new(connection,
					     g_server_vtables,
					     prv_found_media_server,
					     prv_lost_media_server);
	else
		retval = FALSE;

	return retval;
}

static const gchar *prv_control_point_server_name(void)
{
	return DLEYNA_SERVER_NAME;
}

static const gchar *prv_control_point_server_introspection(void)
{
	return g_server_introspection;
}

static const gchar *prv_control_point_root_introspection(void)
{
	return g_root_introspection;
}

static const dleyna_control_point_t g_control_point = {
	prv_control_point_initialize,
	prv_control_point_free,
	prv_control_point_server_name,
	prv_control_point_server_introspection,
	prv_control_point_root_introspection,
	prv_control_point_start_service,
	prv_control_point_stop_service
};

const dleyna_control_point_t *dleyna_control_point_get_renderer(void)
{
	return &g_control_point;
}