1 /* GStreamer
2  *
3  * giosrc-mounting: example application that shows how to handle the
4  * "not-mounted" message
5  *
6  * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include <gst/gst.h>
25 #include <gtk/gtk.h>
26 
27 #include <string.h>
28 
29 static GstElement *pipeline = NULL;
30 
31 static void
mount_cb(GObject * obj,GAsyncResult * res,gpointer user_data)32 mount_cb (GObject * obj, GAsyncResult * res, gpointer user_data)
33 {
34   gboolean ret;
35   GError *err = NULL;
36 
37   ret = g_file_mount_enclosing_volume_finish (G_FILE (obj), res, &err);
38 
39   if (ret) {
40     g_print ("mounted successfully\n");
41     gst_bus_set_flushing ((GstBus *) user_data, FALSE);
42 
43     gst_element_set_state (pipeline, GST_STATE_PLAYING);
44   } else {
45     g_print ("mounting failed: %s\n", err->message);
46     g_clear_error (&err);
47     gtk_main_quit ();
48   }
49 }
50 
51 static gboolean
message_handler(GstBus * bus,GstMessage * message,gpointer user_data)52 message_handler (GstBus * bus, GstMessage * message, gpointer user_data)
53 {
54   switch (GST_MESSAGE_TYPE (message)) {
55     case GST_MESSAGE_ELEMENT:{
56       const GstStructure *s = gst_message_get_structure (message);
57       const gchar *name = gst_structure_get_name (s);
58 
59       if (strcmp (name, "not-mounted") == 0) {
60         GMountOperation *mop = gtk_mount_operation_new (NULL);
61         GFile *file =
62             G_FILE (g_value_get_object (gst_structure_get_value (s, "file")));
63 
64         g_print ("not-mounted\n");
65         gst_element_set_state (pipeline, GST_STATE_NULL);
66         gst_bus_set_flushing (bus, TRUE);
67 
68         g_file_mount_enclosing_volume (file, G_MOUNT_MOUNT_NONE,
69             mop, NULL, mount_cb, bus);
70 
71         g_object_unref (mop);
72       }
73       break;
74     }
75 
76     case GST_MESSAGE_EOS:
77       g_print ("EOS\n");
78       gtk_main_quit ();
79       break;
80     case GST_MESSAGE_ERROR:{
81       GError *err = NULL;
82 
83       gst_message_parse_error (message, &err, NULL);
84       g_print ("error: %s\n", err->message);
85       g_clear_error (&err);
86 
87       gtk_main_quit ();
88       break;
89     }
90     default:
91       break;
92   }
93 
94   return TRUE;
95 }
96 
97 int
main(int argc,char * argv[])98 main (int argc, char *argv[])
99 {
100   GstBus *bus;
101   gint watch_id;
102 
103   if (argc != 2) {
104     g_print ("usage: giosrc-mounting URI\n");
105     return -1;
106   }
107 
108   gst_init (NULL, NULL);
109   gtk_init (NULL, NULL);
110 
111   pipeline = gst_element_factory_make ("playbin", NULL);
112   g_assert (pipeline);
113   g_object_set (G_OBJECT (pipeline), "uri", argv[1], NULL);
114 
115   bus = gst_element_get_bus (pipeline);
116   watch_id = gst_bus_add_watch (bus, message_handler, NULL);
117   gst_object_unref (bus);
118 
119   gst_element_set_state (pipeline, GST_STATE_PLAYING);
120 
121   gtk_main ();
122 
123   g_source_remove (watch_id);
124   gst_element_set_state (pipeline, GST_STATE_NULL);
125   gst_object_unref (pipeline);
126 
127   return 0;
128 }
129