Debian Codegraph @

src/av-cp/renderer-controls.c

raw
1 /*
2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]>
3 *
4 * Authors: Zeeshan Ali (Khattak) <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <gtk/gtk.h>
27 #include <gmodule.h>
28
29 #include "playlist-treeview.h"
30 #include "renderer-controls.h"
31 #include "renderer-combo.h"
32
33 #define SEC_PER_MIN 60
34 #define SEC_PER_HOUR 3600
35
36 GtkWidget *volume_scale;
37 GtkWidget *position_scale;
38 GtkWidget *play_button;
39 GtkWidget *pause_button;
40 GtkWidget *stop_button;
41 GtkWidget *next_button;
42 GtkWidget *prev_button;
43 GtkWidget *lenient_mode_menuitem;
44
45 static guint timeout_id;
46
47 void
48 on_play_button_clicked (GtkButton *button,
49 gpointer user_data);
50 void
51 on_pause_button_clicked (GtkButton *button,
52 gpointer user_data);
53
54 void
55 on_stop_button_clicked (GtkButton *button,
56 gpointer user_data);
57
58 void
59 on_next_button_clicked (GtkButton *button,
60 gpointer user_data);
61
62 void
63 on_previous_button_clicked (GtkButton *button,
64 gpointer user_data);
65
66 void
67 on_clear_state_button_clicked (GtkButton *button,
68 gpointer user_data);
69
70 gboolean
71 on_position_scale_value_changed (GtkRange *range,
72 gpointer user_data);
73
74 typedef struct
75 {
76 GCallback callback;
77
78 GUPnPDIDLLiteResource *resource;
79 } SetAVTransportURIData;
80
81 static SetAVTransportURIData *
82 set_av_transport_uri_data_new (GCallback callback,
83 GUPnPDIDLLiteResource *resource)
84 {
85 SetAVTransportURIData *data;
86
87 data = g_slice_new (SetAVTransportURIData);
88
89 data->callback = callback;
90 data->resource = resource; /* Steal the ref */
91
92 return data;
93 }
94
95 static void
96 set_av_transport_uri_data_free (SetAVTransportURIData *data)
97 {
98 g_object_unref (data->resource);
99 g_slice_free (SetAVTransportURIData, data);
100 }
101
102 static void
103 av_transport_action_cb (GObject *object, GAsyncResult *res, gpointer user_data)
104 {
105 const char *action_name;
106 GError *error = NULL;
107 GUPnPServiceProxyAction *action;
108 const char *udn;
109 GUPnPServiceProxy *proxy = GUPNP_SERVICE_PROXY (object);
110
111 action_name = (const char *) user_data;
112
113 error = NULL;
114 udn = gupnp_service_info_get_udn (GUPNP_SERVICE_INFO (object));
115 action = gupnp_service_proxy_call_action_finish (proxy, res, &error);
116 if (error != NULL) {
117 g_warning ("Failed to send action '%s' to '%s': %s",
118 action_name,
119 udn,
120 error->message);
121
122 goto out;
123 }
124
125 if (!gupnp_service_proxy_action_get_result (action, &error, NULL)) {
126
127 g_warning ("Failed to send action '%s' to '%s': %s",
128 action_name,
129 udn,
130 error->message);
131 }
132
133 out:
134 g_clear_error (&error);
135 g_object_unref (object);
136 }
137
138 static void
139 g_value_free (gpointer data)
140 {
141 g_value_unset ((GValue *) data);
142 g_slice_free (GValue, data);
143 }
144
145 static GList *
146 create_av_transport_args (char **additional_args, GList **out_values)
147 {
148 GValue *instance_id;
149 gint i;
150 GList *names = NULL, *values = NULL;
151
152 instance_id = g_slice_alloc0 (sizeof (GValue));
153 g_value_init (instance_id, G_TYPE_UINT);
154 g_value_set_uint (instance_id, 0);
155
156 names = g_list_append (names, g_strdup ("InstanceID"));
157 values = g_list_append (values, instance_id);
158
159
160 if (additional_args != NULL) {
161 for (i = 0; additional_args[i]; i += 2) {
162 GValue *value;
163
164 value = g_slice_alloc0 (sizeof (GValue));
165 g_value_init (value, G_TYPE_STRING);
166 g_value_set_string (value, additional_args[i + 1]);
167 names = g_list_append (names,
168 g_strdup (additional_args[i]));
169 values = g_list_append (values, value);
170 }
171 }
172
173 *out_values = values;
174
175 return names;
176 }
177
178 void
179 av_transport_send_action (const char *action_name, char *additional_args[])
180 {
181 GUPnPServiceProxy *av_transport;
182 GList *names, *values;
183 GUPnPServiceProxyAction *action;
184
185 av_transport = get_selected_av_transport (NULL);
186 if (av_transport == NULL) {
187 g_warning ("No renderer selected");
188 return;
189 }
190
191 names = create_av_transport_args (additional_args, &values);
192 action = gupnp_service_proxy_action_new_from_list (action_name,
193 names,
194 values);
195
196 gupnp_service_proxy_call_action_async (av_transport,
197 action,
198 NULL,
199 av_transport_action_cb,
200 (char *) action_name);
201
202 gupnp_service_proxy_action_unref (action);
203 g_list_free_full (names, g_free);
204 g_list_free_full (values, g_value_free);
205 }
206
207 static void
208 play (void)
209 {
210 const char *args[] = { "Speed", "1", NULL };
211
212 av_transport_send_action ("Play", (char **) args);
213 }
214
215 static void
216 set_av_transport_uri_cb (GObject *object, GAsyncResult *res, gpointer user_data)
217 {
218 SetAVTransportURIData *data = (SetAVTransportURIData *) user_data;
219 GError *error = NULL;
220 const char *udn;
221
222 udn = gupnp_service_info_get_udn (GUPNP_SERVICE_INFO (object));
223
224 GUPnPServiceProxyAction *action;
225 action = gupnp_service_proxy_call_action_finish (
226 GUPNP_SERVICE_PROXY (object),
227 res,
228 &error);
229
230 // The above call will only catch issues with the HTTP transport, not
231 // with the call itself. So we need to call an empty get on the action
232 // as well to get any SOAP error
233 if (error == NULL) {
234 gupnp_service_proxy_action_get_result (action, &error, NULL);
235 }
236 if (error != NULL) {
237 g_warning ("Failed to set URI '%s' on %s: %s",
238 gupnp_didl_lite_resource_get_uri (data->resource),
239 udn,
240 error->message);
241 } else {
242 long duration ;
243 if (data->callback) {
244 data->callback ();
245 }
246
247 duration = gupnp_didl_lite_resource_get_duration
248 (data->resource);
249 if (duration > 0) {
250 gtk_range_set_range (GTK_RANGE (position_scale),
251 0.0,
252 duration);
253 }
254 }
255
256 g_clear_error (&error);
257 set_av_transport_uri_data_free (data);
258 g_object_unref (object);
259 }
260
261 G_MODULE_EXPORT
262 gboolean
263 on_volume_scale_value_changed (GtkRange *range,
264 gpointer user_data);
265
266 void
267 set_volume_scale (guint volume)
268 {
269 g_signal_handlers_block_by_func (volume_scale,
270 on_volume_scale_value_changed,
271 NULL);
272
273 gtk_range_set_value (GTK_RANGE (volume_scale), volume);
274
275 g_signal_handlers_unblock_by_func (volume_scale,
276 on_volume_scale_value_changed,
277 NULL);
278 }
279
280 static void
281 on_didl_object_available (GUPnPDIDLLiteParser *parser,
282 GUPnPDIDLLiteObject *object,
283 gpointer user_data)
284 {
285 GUPnPDIDLLiteResource **resource;
286 GUPnPServiceProxy *av_transport;
287 char *sink_protocol_info;
288 gboolean lenient_mode;
289
290 resource = (GUPnPDIDLLiteResource **) user_data;
291
292 av_transport = get_selected_av_transport (&sink_protocol_info);
293 if (av_transport == NULL) {
294 g_warning ("No renderer selected");
295
296 return;
297 }
298
299 lenient_mode = gtk_check_menu_item_get_active
300 (GTK_CHECK_MENU_ITEM (lenient_mode_menuitem));
301
302 *resource = gupnp_didl_lite_object_get_compat_resource
303 (object,
304 sink_protocol_info,
305 lenient_mode);
306 g_free (sink_protocol_info);
307 g_object_unref (av_transport);
308 }
309
310 static GUPnPDIDLLiteResource *
311 find_compat_res_from_metadata (const char *metadata)
312 {
313 GUPnPDIDLLiteParser *parser;
314 GUPnPDIDLLiteResource *resource;
315 GError *error;
316
317 parser = gupnp_didl_lite_parser_new ();
318 resource = NULL;
319 error = NULL;
320
321 g_signal_connect (parser,
322 "object-available",
323 G_CALLBACK (on_didl_object_available),
324 &resource);
325
326 /* Assumption: metadata only contains a single didl object */
327 gupnp_didl_lite_parser_parse_didl (parser, metadata, &error);
328 if (error) {
329 g_warning ("%s\n", error->message);
330
331 g_error_free (error);
332 }
333
334 g_object_unref (parser);
335
336 return resource;
337 }
338
339 void
340 set_av_transport_uri (const char *metadata,
341 GCallback callback)
342 {
343 GUPnPServiceProxy *av_transport;
344 SetAVTransportURIData *data;
345 GUPnPDIDLLiteResource *resource;
346 const char *uri;
347 GUPnPServiceProxyAction *action;
348
349 av_transport = get_selected_av_transport (NULL);
350 if (av_transport == NULL) {
351 g_warning ("No renderer selected");
352 return;
353 }
354
355 resource = find_compat_res_from_metadata (metadata);
356 if (resource == NULL) {
357 g_warning ("no compatible URI found.");
358
359 g_object_unref (av_transport);
360
361 return;
362 }
363
364 data = set_av_transport_uri_data_new (callback, resource);
365 uri = gupnp_didl_lite_resource_get_uri (resource);
366
367 action = gupnp_service_proxy_action_new ("SetAVTransportURI",
368 "InstanceID",
369 G_TYPE_UINT,
370 0,
371 "CurrentURI",
372 G_TYPE_STRING,
373 uri,
374 "CurrentURIMetaData",
375 G_TYPE_STRING,
376 metadata,
377 NULL);
378
379 gupnp_service_proxy_call_action_async (av_transport,
380 action,
381 NULL,
382 set_av_transport_uri_cb,
383 data);
384 gupnp_service_proxy_action_unref (action);
385 }
386
387 G_MODULE_EXPORT
388 void
389 on_play_button_clicked (GtkButton *button,
390 gpointer user_data)
391 {
392 PlaybackState state;
393
394 state = get_selected_renderer_state ();
395
396 if (state == PLAYBACK_STATE_STOPPED ||
397 state == PLAYBACK_STATE_UNKNOWN) {
398 get_selected_object ((MetadataFunc) set_av_transport_uri, play);
399 } else {
400 play ();
401 }
402 }
403
404 G_MODULE_EXPORT
405 void
406 on_pause_button_clicked (GtkButton *button,
407 gpointer user_data)
408 {
409 av_transport_send_action ("Pause", NULL);
410 }
411
412 G_MODULE_EXPORT
413 void
414 on_stop_button_clicked (GtkButton *button,
415 gpointer user_data)
416 {
417 av_transport_send_action ("Stop", NULL);
418 }
419
420 G_MODULE_EXPORT
421 void
422 on_next_button_clicked (GtkButton *button,
423 gpointer user_data)
424 {
425 select_next_object ();
426 }
427
428 G_MODULE_EXPORT
429 void
430 on_previous_button_clicked (GtkButton *button,
431 gpointer user_data)
432 {
433 select_prev_object ();
434 }
435
436 G_MODULE_EXPORT
437 void
438 on_clear_state_button_clicked (GtkButton *button,
439 gpointer user_data)
440 {
441 clear_selected_renderer_state ();
442 }
443
444 G_MODULE_EXPORT
445 gboolean
446 on_position_scale_value_changed (GtkRange *range,
447 gpointer user_data)
448 {
449 char *args[] = { (char *) "Unit", (char *) "ABS_TIME", (char *)"Target", NULL, NULL };
450 guint total_secs;
451 guint hours;
452 guint minutes;
453 guint seconds;
454
455 total_secs = (guint) gtk_range_get_value (range);
456 hours = total_secs / 3600;
457 minutes = (total_secs / 60) - (hours * 60);
458 seconds = total_secs - (hours * 3600) - (minutes * 60);
459
460 args[3] = g_strdup_printf ("%u:%02u:%02u",
461 hours,
462 minutes,
463 seconds);
464
465 if (args[3]) {
466 av_transport_send_action ("Seek", (char **)args);
467 g_free (args[3]);
468 }
469
470 return TRUE;
471 }
472
473 static gdouble
474 seconds_from_time (const char *time_str)
475 {
476 char **tokens;
477 gdouble seconds = -1.0;
478
479 tokens = g_strsplit (time_str, ":", -1);
480 if (tokens == NULL) {
481 return -1.0;
482 }
483
484 if (tokens[0] == NULL ||
485 tokens[1] == NULL ||
486 tokens[2] == NULL) {
487 goto return_point;
488 }
489
490 seconds = g_strtod (tokens[2], NULL);
491 seconds += g_strtod (tokens[1], NULL) * SEC_PER_MIN;
492 seconds += g_strtod (tokens[0], NULL) * SEC_PER_HOUR;
493
494 return_point:
495 g_strfreev (tokens);
496
497 return seconds;
498 }
499
500 void
501 set_position_scale_duration (const char *duration_str)
502 {
503 gdouble duration;
504
505 duration = seconds_from_time (duration_str);
506 if (duration > 0.0) {
507 gtk_range_set_range (GTK_RANGE (position_scale),
508 0.0,
509 duration);
510 }
511 }
512
513 void
514 set_position_scale_position (const char *position_str)
515 {
516 gdouble position;
517
518 position = seconds_from_time (position_str);
519 if (position >= 0.0) {
520 g_signal_handlers_block_by_func
521 (position_scale,
522 on_position_scale_value_changed,
523 NULL);
524
525 gtk_range_set_value (GTK_RANGE (position_scale), position);
526
527 g_signal_handlers_unblock_by_func
528 (position_scale,
529 on_position_scale_value_changed,
530 NULL);
531 }
532 }
533
534 static void
535 get_position_info_cb (GObject *object, GAsyncResult *res, gpointer user_data)
536 {
537 gchar *position;
538 gchar *duration;
539 const gchar *udn;
540 GError *error = NULL;
541 GUPnPServiceProxyAction *action;
542 GUPnPServiceProxy *proxy = GUPNP_SERVICE_PROXY (object);
543
544 udn = gupnp_service_info_get_udn (GUPNP_SERVICE_INFO (object));
545
546 action = gupnp_service_proxy_call_action_finish (proxy, res, &error);
547 if (error != NULL) {
548 g_warning ("Failed to get current media position"
549 "from media renderer '%s':%s\n",
550 udn,
551 error->message);
552
553 goto return_point;
554 }
555
556 if (!gupnp_service_proxy_action_get_result (action,
557 &error,
558 "AbsTime",
559 G_TYPE_STRING,
560 &position,
561 "TrackDuration",
562 G_TYPE_STRING,
563 &duration,
564 NULL)) {
565 g_warning ("Failed to get current media position"
566 "from media renderer '%s':%s\n",
567 udn,
568 error->message);
569
570 goto return_point;
571 }
572
573 set_position_scale_position (position);
574 set_position_scale_duration (duration);
575 char *tooltip = g_strdup_printf ("%s/%s", position, duration);
576 gtk_widget_set_tooltip_text (GTK_WIDGET (position_scale), tooltip);
577 gtk_widget_set_has_tooltip (GTK_WIDGET (position_scale), TRUE);
578 g_free (tooltip);
579 g_free (position);
580 g_free (duration);
581
582 return_point:
583 g_clear_error (&error);
584 g_object_unref (object);
585 }
586
587 static gboolean
588 update_position (gpointer data)
589 {
590 GUPnPServiceProxy *av_transport;
591 GUPnPServiceProxyAction *action;
592
593 av_transport = get_selected_av_transport (NULL);
594 if (av_transport == NULL) {
595 return FALSE;
596 }
597
598 action = gupnp_service_proxy_action_new ("GetPositionInfo",
599 "InstanceID",
600 G_TYPE_UINT,
601 0,
602 NULL);
603
604 gupnp_service_proxy_call_action_async (av_transport,
605 action,
606 NULL,
607 get_position_info_cb,
608 NULL);
609
610 gupnp_service_proxy_action_unref (action);
611
612 return TRUE;
613 }
614
615 static void
616 add_timeout (void)
617 {
618 if (timeout_id == 0) {
619 timeout_id = g_timeout_add (1000,
620 update_position,
621 NULL);
622 }
623 }
624
625 static void
626 remove_timeout (void)
627 {
628 if (timeout_id != 0) {
629 g_source_remove (timeout_id);
630 timeout_id = 0;
631 }
632 }
633
634 void
635 prepare_controls_for_state (PlaybackState state)
636 {
637 gboolean play_possible;
638 gboolean pause_possible;
639 gboolean stop_possible;
640
641 switch (state) {
642 case PLAYBACK_STATE_STOPPED:
643 play_possible = TRUE;
644 pause_possible = FALSE;
645 stop_possible = FALSE;
646
647 remove_timeout ();
648
649 break;
650
651 case PLAYBACK_STATE_PAUSED:
652 play_possible = TRUE;
653 pause_possible = FALSE;
654 stop_possible = TRUE;
655
656 remove_timeout ();
657
658 update_position (NULL);
659
660 break;
661
662 case PLAYBACK_STATE_PLAYING:
663 play_possible = FALSE;
664 pause_possible = TRUE;
665 stop_possible = TRUE;
666
667 /* Start tracking media position in playing state */
668 add_timeout ();
669
670 break;
671
672 case PLAYBACK_STATE_TRANSITIONING:
673 play_possible = FALSE;
674 pause_possible = FALSE;
675 stop_possible = FALSE;
676
677 remove_timeout ();
678
679 break;
680
681 case PLAYBACK_STATE_UNKNOWN:
682 default:
683 play_possible = TRUE;
684 pause_possible = TRUE;
685 stop_possible = TRUE;
686
687 remove_timeout ();
688
689 break;
690 }
691
692 /* Disable the seekbar when the state is stopped */
693 gtk_widget_set_sensitive (position_scale, stop_possible);
694 if (!stop_possible) {
695 g_signal_handlers_block_by_func
696 (position_scale,
697 on_position_scale_value_changed,
698 NULL);
699
700 gtk_range_set_value (GTK_RANGE (position_scale), 0.0);
701
702 g_signal_handlers_unblock_by_func
703 (position_scale,
704 on_position_scale_value_changed,
705 NULL);
706 }
707
708 gtk_widget_set_sensitive (play_button, play_possible);
709 gtk_widget_set_sensitive (pause_button, pause_possible);
710 gtk_widget_set_sensitive (stop_button, stop_possible);
711 }
712
713 static void
714 set_volume_cb (GObject *object, GAsyncResult *res, gpointer user_data)
715 {
716 GError *error;
717 GUPnPServiceProxy *proxy = GUPNP_SERVICE_PROXY (object);
718 GUPnPServiceProxyAction *action;
719
720 error = NULL;
721 action = gupnp_service_proxy_call_action_finish (proxy, res, &error);
722
723 if (error == NULL) {
724 // No transport error, check for SOAP error
725 gupnp_service_proxy_action_get_result (action, &error, NULL);
726 }
727
728 if (error != NULL) {
729 const char *udn;
730
731 udn = gupnp_service_info_get_udn (GUPNP_SERVICE_INFO (object));
732
733 g_warning ("Failed to set volume of %s: %s",
734 udn,
735 error->message);
736
737 g_error_free (error);
738
739 /* Update the range according to the current volume */
740 set_volume_scale (get_selected_renderer_volume ());
741 }
742
743 g_object_unref (object);
744 }
745
746 G_MODULE_EXPORT
747 gboolean
748 on_volume_scale_value_changed (GtkRange *range,
749 gpointer user_data)
750 {
751 GUPnPServiceProxy *rendering_control;
752 guint desired_volume;
753 GUPnPServiceProxyAction *action;
754
755 rendering_control = get_selected_rendering_control ();
756 if (rendering_control == NULL) {
757 g_warning ("No renderer selected");
758 return FALSE;
759 }
760
761 desired_volume = (guint) gtk_range_get_value (range);
762
763 action = gupnp_service_proxy_action_new ("SetVolume",
764 "InstanceID",
765 G_TYPE_UINT,
766 0,
767 "Channel",
768 G_TYPE_STRING,
769 "Master",
770 "DesiredVolume",
771 G_TYPE_UINT,
772 desired_volume,
773 NULL);
774
775 gupnp_service_proxy_call_action_async (rendering_control,
776 action,
777 NULL,
778 set_volume_cb,
779 NULL);
780
781 gupnp_service_proxy_action_unref (action);
782
783 return TRUE;
784 }
785
786 void
787 setup_renderer_controls (GtkBuilder *builder)
788 {
789 volume_scale = GTK_WIDGET (gtk_builder_get_object (builder,
790 "volume-scale"));
791 g_assert (volume_scale != NULL);
792
793 position_scale = GTK_WIDGET (gtk_builder_get_object (
794 builder,
795 "position-scale"));
796 g_assert (position_scale != NULL);
797
798 play_button = GTK_WIDGET (gtk_builder_get_object (builder,
799 "play-button"));
800 g_assert (play_button != NULL);
801
802 pause_button = GTK_WIDGET (gtk_builder_get_object (builder,
803 "pause-button"));
804 g_assert (pause_button != NULL);
805
806 stop_button = GTK_WIDGET (gtk_builder_get_object (builder,
807 "stop-button"));
808 g_assert (stop_button != NULL);
809
810 next_button = GTK_WIDGET (gtk_builder_get_object (builder,
811 "next-button"));
812 g_assert (next_button != NULL);
813
814 prev_button = GTK_WIDGET (gtk_builder_get_object (builder,
815 "previous-button"));
816 g_assert (prev_button != NULL);
817
818 lenient_mode_menuitem = GTK_WIDGET (gtk_builder_get_object (
819 builder,
820 "lenient_mode_menuitem"));
821 g_assert (lenient_mode_menuitem != NULL);
822
823 timeout_id = 0;
824
825 g_object_weak_ref (G_OBJECT (position_scale),
826 (GWeakNotify) remove_timeout,
827 NULL);
828 }
829