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
|
/* sample application for testing decodebin3
*
* Copyright (C) 2015 Centricular Ltd
* @author: Edward Hervey <edward@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include <glib-object.h>
#include <glib/gprintf.h>
#include <gst/gst.h>
/* Global structure */
typedef struct _MyDataStruct
{
GMainLoop *mainloop;
GstElement *pipeline;
GstBus *demux_bus;
GstElement *decodebin;
GstElement *src;
GList *other_src;
GstElement *playsink;
/* Current collection */
GstStreamCollection *collection;
guint notify_id;
guint current_audio;
guint current_video;
guint current_text;
glong timeout_id;
} MyDataStruct;
static void
print_tag_foreach (const GstTagList * tags, const gchar * tag,
gpointer user_data)
{
GValue val = { 0, };
gchar *str;
gint depth = GPOINTER_TO_INT (user_data);
if (!gst_tag_list_copy_value (&val, tags, tag))
return;
if (G_VALUE_HOLDS_STRING (&val))
str = g_value_dup_string (&val);
else
str = gst_value_serialize (&val);
g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
g_free (str);
g_value_unset (&val);
}
static void
dump_collection (GstStreamCollection * collection)
{
guint i;
GstTagList *tags;
GstCaps *caps;
for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
GstStream *stream = gst_stream_collection_get_stream (collection, i);
g_print (" Stream %u type %s flags 0x%x\n", i,
gst_stream_type_get_name (gst_stream_get_stream_type (stream)),
gst_stream_get_stream_flags (stream));
g_print (" ID: %s\n", gst_stream_get_stream_id (stream));
caps = gst_stream_get_caps (stream);
if (caps) {
gchar *caps_str = gst_caps_to_string (caps);
g_print (" caps: %s\n", caps_str);
g_free (caps_str);
gst_caps_unref (caps);
}
tags = gst_stream_get_tags (stream);
if (tags) {
g_print (" tags:\n");
gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (3));
gst_tag_list_unref (tags);
}
}
}
static gboolean
switch_streams (MyDataStruct * data)
{
guint i, nb_streams;
gint nb_video = 0, nb_audio = 0, nb_text = 0;
GstStream *videos[256], *audios[256], *texts[256];
GList *streams = NULL;
GstEvent *ev;
g_print ("Switching Streams...\n");
/* Calculate the number of streams of each type */
nb_streams = gst_stream_collection_get_size (data->collection);
for (i = 0; i < nb_streams; i++) {
GstStream *stream = gst_stream_collection_get_stream (data->collection, i);
GstStreamType stype = gst_stream_get_stream_type (stream);
if (stype == GST_STREAM_TYPE_VIDEO) {
videos[nb_video] = stream;
nb_video += 1;
} else if (stype == GST_STREAM_TYPE_AUDIO) {
audios[nb_audio] = stream;
nb_audio += 1;
} else if (stype == GST_STREAM_TYPE_TEXT) {
texts[nb_text] = stream;
nb_text += 1;
}
}
if (nb_video) {
data->current_video = (data->current_video + 1) % nb_video;
streams =
g_list_append (streams,
(gchar *) gst_stream_get_stream_id (videos[data->current_video]));
g_print (" Selecting video channel #%d : %s\n", data->current_video,
gst_stream_get_stream_id (videos[data->current_video]));
}
if (nb_audio) {
data->current_audio = (data->current_audio + 1) % nb_audio;
streams =
g_list_append (streams,
(gchar *) gst_stream_get_stream_id (audios[data->current_audio]));
g_print (" Selecting audio channel #%d : %s\n", data->current_audio,
gst_stream_get_stream_id (audios[data->current_audio]));
}
if (nb_text) {
data->current_text = (data->current_text + 1) % nb_text;
streams =
g_list_append (streams,
(gchar *) gst_stream_get_stream_id (texts[data->current_text]));
g_print (" Selecting text channel #%d : %s\n", data->current_text,
gst_stream_get_stream_id (texts[data->current_text]));
}
ev = gst_event_new_select_streams (streams);
gst_element_send_event (data->pipeline, ev);
return G_SOURCE_CONTINUE;
}
static void
stream_notify_cb (GstStreamCollection * collection, GstStream * stream,
GParamSpec * pspec, guint * val)
{
g_print ("Got stream-notify from stream %s for %s (collection %p)\n",
stream->stream_id, pspec->name, collection);
if (g_str_equal (pspec->name, "caps")) {
GstCaps *caps = gst_stream_get_caps (stream);
gchar *caps_str = gst_caps_to_string (caps);
g_print (" New caps: %s\n", caps_str);
g_free (caps_str);
gst_caps_unref (caps);
}
}
static GstBusSyncReply
_on_bus_message (GstBus * bus, GstMessage * message, MyDataStruct * data)
{
GstObject *src = GST_MESSAGE_SRC (message);
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_ERROR:{
GError *err = NULL;
gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
gst_message_parse_error (message, &err, NULL);
g_printerr ("ERROR: from element %s: %s\n", name, err->message);
g_error_free (err);
g_free (name);
g_printf ("Stopping\n");
g_main_loop_quit (data->mainloop);
break;
}
case GST_MESSAGE_EOS:
g_printf ("EOS ! Stopping \n");
g_main_loop_quit (data->mainloop);
break;
case GST_MESSAGE_STREAM_COLLECTION:
{
GstStreamCollection *collection = NULL;
gst_message_parse_stream_collection (message, &collection);
if (collection) {
g_printf ("Got a collection from %s:\n",
src ? GST_OBJECT_NAME (src) : "Unknown");
dump_collection (collection);
if (data->collection && data->notify_id) {
g_signal_handler_disconnect (data->collection, data->notify_id);
data->notify_id = 0;
}
gst_object_replace ((GstObject **) & data->collection,
(GstObject *) collection);
if (data->collection) {
data->notify_id =
g_signal_connect (data->collection, "stream-notify",
(GCallback) stream_notify_cb, data);
}
if (data->timeout_id == 0)
/* In 5s try to change streams */
data->timeout_id =
g_timeout_add_seconds (5, (GSourceFunc) switch_streams, data);
gst_object_unref (collection);
}
break;
}
default:
break;
}
return GST_BUS_PASS;
}
static void
decodebin_pad_added_cb (GstElement * dbin, GstPad * pad, MyDataStruct * data)
{
gchar *pad_name = gst_pad_get_name (pad);
const gchar *sink_pad = NULL;
GST_DEBUG_OBJECT (pad, "New pad ! Link to playsink !");
if (!g_ascii_strncasecmp (pad_name, "video_", 6))
sink_pad = "video_sink";
else if (!g_ascii_strncasecmp (pad_name, "audio_", 6))
sink_pad = "audio_sink";
else if (!g_ascii_strncasecmp (pad_name, "text_", 5))
sink_pad = "text_sink";
else
GST_WARNING_OBJECT (pad, "non audio/video/text pad");
g_free (pad_name);
if (sink_pad) {
GstPad *playsink_pad;
playsink_pad = gst_element_request_pad_simple (data->playsink, sink_pad);
if (playsink_pad)
gst_pad_link (pad, playsink_pad);
}
}
int
main (int argc, gchar ** argv)
{
GError *error = NULL;
GstBus *bus;
MyDataStruct *data;
int i;
gst_init (&argc, &argv);
data = g_new0 (MyDataStruct, 1);
if (argc < 2) {
g_print ("Usage: decodebin3 URI\n");
return 1;
}
data->pipeline = gst_pipeline_new ("pipeline");
data->decodebin = gst_element_factory_make ("decodebin3", NULL);
data->src =
gst_element_make_from_uri (GST_URI_SRC, argv[1], "source", &error);
if (error) {
g_printf ("pipeline could not be constructed: %s\n", error->message);
g_error_free (error);
return 1;
}
data->playsink = gst_element_factory_make ("playsink", NULL);
#if 0
{
GstElement *sink = gst_element_factory_make ("fakesink", NULL);
g_object_set (sink, "sync", FALSE, NULL);
g_object_set (data->playsink, "video-sink", sink, NULL);
sink = gst_element_factory_make ("fakesink", NULL);
g_object_set (sink, "sync", FALSE, NULL);
g_object_set (data->playsink, "audio-sink", sink, NULL);
}
#endif
gst_bin_add_many ((GstBin *) data->pipeline, data->src, data->decodebin,
data->playsink, NULL);
if (!gst_element_link (data->src, (GstElement *) data->decodebin)) {
g_printf ("Could not link source to demuxer\n");
return 1;
}
/* Handle other inputs if specified */
if (argc > 2) {
for (i = 2; i < argc; i++) {
GstElement *new_src =
gst_element_make_from_uri (GST_URI_SRC, argv[i], NULL, &error);
GstPad *src_pad, *sink_pad;
if (error) {
g_printf ("pipeline could not be constructed: %s\n", error->message);
g_error_free (error);
return 1;
}
data->other_src = g_list_append (data->other_src, new_src);
gst_bin_add ((GstBin *) data->pipeline, new_src);
src_pad = gst_element_get_static_pad (new_src, "src");
sink_pad = gst_element_request_pad_simple (data->decodebin, "sink_%u");
if (gst_pad_link (src_pad, sink_pad) != GST_PAD_LINK_OK) {
g_printf ("Could not link new source to decodebin : %s\n", argv[i]);
return 1;
}
}
}
g_signal_connect (data->decodebin, "pad-added",
(GCallback) decodebin_pad_added_cb, data);
data->mainloop = g_main_loop_new (NULL, FALSE);
/* Put a bus handler */
bus = gst_pipeline_get_bus (GST_PIPELINE (data->pipeline));
gst_bus_set_sync_handler (bus, (GstBusSyncHandler) _on_bus_message, data,
NULL);
/* Start pipeline */
gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
g_main_loop_run (data->mainloop);
gst_element_set_state (data->pipeline, GST_STATE_NULL);
gst_object_unref (data->pipeline);
gst_object_unref (bus);
return 0;
}
|