1 #include <gst/gst.h>
2 #include "gstsplitmuxpartreader.h"
3 #include "gstsplitmuxsrc.h"
4 
5 GST_DEBUG_CATEGORY_EXTERN (splitmux_debug);
6 
7 static const gchar *const path = "out001.mp4";
8 
9 typedef struct _CustomData
10 {
11   GstSplitMuxPartReader *reader;
12   GMainLoop *main_loop;
13   GstBus *bus;
14 } CustomData;
15 
16 static void
part_prepared(GstSplitMuxPartReader * reader)17 part_prepared (GstSplitMuxPartReader * reader)
18 {
19   g_print ("Part prepared\n");
20 }
21 
22 static gboolean
handle_message(GstBus * bus,GstMessage * msg,CustomData * data)23 handle_message (GstBus * bus, GstMessage * msg, CustomData * data)
24 {
25   GError *err;
26   gchar *debug_info;
27 
28   switch (GST_MESSAGE_TYPE (msg)) {
29     case GST_MESSAGE_ERROR:
30       gst_message_parse_error (msg, &err, &debug_info);
31       g_print ("Error received from element %s: %s\n",
32           GST_OBJECT_NAME (msg->src), err->message);
33       g_print ("Debugging information: %s\n", debug_info ? debug_info : "none");
34       g_clear_error (&err);
35       g_free (debug_info);
36       g_main_loop_quit (data->main_loop);
37       break;
38     case GST_MESSAGE_EOS:
39       g_print ("End-Of-Stream reached.\n");
40       g_main_loop_quit (data->main_loop);
41       break;
42     default:
43       break;
44   }
45 
46   return TRUE;
47 }
48 
49 static gboolean
start_reader(CustomData * data)50 start_reader (CustomData * data)
51 {
52   g_print ("Preparing part reader for %s\n", path);
53   gst_splitmux_part_reader_prepare (data->reader);
54   return FALSE;
55 }
56 
57 static GstPad *
handle_get_pad(GstSplitMuxPartReader * reader,GstPad * src_pad,CustomData * data)58 handle_get_pad (GstSplitMuxPartReader * reader, GstPad * src_pad,
59     CustomData * data)
60 {
61   /* Create a dummy target pad for the reader */
62   GstPad *new_pad = g_object_new (SPLITMUX_TYPE_SRC_PAD,
63       "name", GST_PAD_NAME (src_pad), "direction", GST_PAD_SRC, NULL);
64 
65   g_print ("Creating new dummy pad %s\n", GST_PAD_NAME (src_pad));
66 
67   return new_pad;
68 }
69 
70 int
main(int argc,char ** argv)71 main (int argc, char **argv)
72 {
73   CustomData data;
74 
75   gst_init (&argc, &argv);
76 
77   data.main_loop = g_main_loop_new (NULL, FALSE);
78 
79   data.reader = g_object_new (GST_TYPE_SPLITMUX_PART_READER, NULL);
80   data.bus = gst_element_get_bus (GST_ELEMENT_CAST (data.reader));
81 
82   /* Listen for bus messages */
83   gst_bus_add_watch (data.bus, (GstBusFunc) handle_message, &data);
84 
85   gst_splitmux_part_reader_set_location (data.reader, path);
86 
87   /* Connect to prepare signal */
88   g_signal_connect (data.reader, "prepared", (GCallback) part_prepared, &data);
89   gst_splitmux_part_reader_set_callbacks (data.reader, &data,
90       (GstSplitMuxPartReaderPadCb) handle_get_pad);
91 
92   g_idle_add ((GSourceFunc) start_reader, &data);
93 
94   /* Run mainloop */
95   g_main_loop_run (data.main_loop);
96 
97   gst_splitmux_part_reader_unprepare (data.reader);
98 
99   g_main_loop_unref (data.main_loop);
100   gst_object_unref (data.bus);
101   g_object_unref (data.reader);
102 
103   return 0;
104 }
105