-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-sink.c
129 lines (99 loc) · 3.75 KB
/
multi-sink.c
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
#include <stdio.h>
#include <gst/gst.h>
#include <glib.h>
typedef struct _CustomData {
GstElement *pipeline;
GstElement *source;
GstElement *demuxer;
int count;
} CustomData;
static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
{
GMainLoop *loop = (GMainLoop *) data;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_EOS:
g_print ("End of stream\n");
g_main_loop_quit (loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error (msg, &error, &debug);
g_free (debug);
g_printerr ("Error: %s\n", error->message);
g_error_free (error);
g_main_loop_quit (loop);
break;
}
default:
break;
}
return TRUE;
}
static void on_pad_added (GstElement *element, GstPad *pad, gpointer data)
{
GstElement *queue, *parser, *decoder, *sink;
CustomData *d = (CustomData *)data;
char ename[20];
if (d->count >= 3)
return;
sprintf(ename, "queue%02d", d->count); queue = gst_element_factory_make ("queue", ename);
sprintf(ename, "h264-parser%02d", d->count); parser = gst_element_factory_make ("h264parse", ename);
sprintf(ename, "h264-decoder%02d", d->count); decoder = gst_element_factory_make ("avdec_h264", ename);
sprintf(ename, "video-sink%02d", d->count); sink = gst_element_factory_make ("autovideosink", ename);
if (!queue || !parser || !decoder || !sink) {
g_printerr("Failed to create an element in on_pad_added()\n");
return;
}
d->count++;
gst_bin_add_many (GST_BIN (d->pipeline), queue, parser, decoder, sink, NULL);
gst_element_sync_state_with_parent(queue);
gst_element_sync_state_with_parent(parser);
gst_element_sync_state_with_parent(decoder);
gst_element_sync_state_with_parent(sink);
gst_element_link_many(element, queue, parser, decoder, sink, NULL);
sprintf(ename, "sink%d", d->count);
GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(d->pipeline), GST_DEBUG_GRAPH_SHOW_ALL, ename);
}
int main (int argc, char *argv[])
{
GMainLoop *loop;
CustomData data;
GstBus *bus;
guint bus_watch_id;
/* Initialisation */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* Check input arguments */
if (argc != 2) {
g_printerr ("Usage: multi-sink <filename>.mkv\n");
return -1;
}
/* Create gstreamer elements */
data.pipeline = gst_pipeline_new ("video-player");
data.source = gst_element_factory_make ("filesrc", "file-source");
data.demuxer = gst_element_factory_make ("matroskademux", "matroska-demuxer");
data.count = 0;
if (!data.pipeline || !data.source || !data.demuxer) {
g_printerr ("One element could not be created. Exiting.\n");
return -1;
}
g_object_set (G_OBJECT (data.source), "location", argv[1], NULL);
bus = gst_pipeline_get_bus (GST_PIPELINE (data.pipeline));
bus_watch_id = gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.demuxer, NULL);
gst_element_link (data.source, data.demuxer);
g_signal_connect (data.demuxer, "pad-added", G_CALLBACK (on_pad_added), &data);
g_print ("Now playing: %s\n", argv[1]);
gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
g_print ("Running...\n");
g_main_loop_run (loop);
g_print ("Returned, stopping playback\n");
gst_element_set_state (data.pipeline, GST_STATE_NULL);
g_print ("Deleting pipeline\n");
gst_object_unref (GST_OBJECT (data.pipeline));
g_source_remove (bus_watch_id);
g_main_loop_unref (loop);
return 0;
}