+
+ /* FIXME: currently, decodebin isn't possible for
+ video streams. in that case, make a manual pipeline. */
+
+ const char *ext = strrchr(filename, '.');
+ if (!ext)
+ ext = filename;
+
+ int is_mpeg_ps = !(strcasecmp(ext, ".mpeg") && strcasecmp(ext, ".mpg") && strcasecmp(ext, ".vob") && strcasecmp(ext, ".bin"));
+ int is_mpeg_ts = !strcasecmp(ext, ".ts");
+ int is_mp3 = !strcasecmp(ext, ".mp3"); /* force mp3 instead of decodebin */
+ int is_video = is_mpeg_ps || is_mpeg_ts;
+ int is_streaming = !strncmp(filename, "http://", 7);
+
+ eDebug("filename: %s, is_mpeg_ps: %d, is_mpeg_ts: %d, is_video: %d, is_streaming: %d, is_mp3: %d", filename, is_mpeg_ps, is_mpeg_ts, is_video, is_streaming, is_mp3);
+
+ int is_audio = !is_video;
+
+ int all_ok = 0;
+
+ m_gst_pipeline = gst_pipeline_new ("audio-player");
+ if (!m_gst_pipeline)
+ eWarning("failed to create pipeline");
+
+ if (!is_streaming)
+ source = gst_element_factory_make ("filesrc", "file-source");
+ else
+ {
+ source = gst_element_factory_make ("neonhttpsrc", "http-source");
+ if (source)
+ g_object_set (G_OBJECT (source), "automatic-redirect", TRUE, NULL);
+ }
+
+ if (!source)
+ eWarning("failed to create %s", is_streaming ? "neonhttpsrc" : "filesrc");
+ else
+ /* configure source */
+ g_object_set (G_OBJECT (source), "location", filename, NULL);
+
+ if (is_audio)
+ {
+ /* filesrc -> decodebin -> audioconvert -> capsfilter -> alsasink */
+ const char *decodertype = is_mp3 ? "mad" : "decodebin";
+
+ decoder = gst_element_factory_make (decodertype, "decoder");
+ if (!decoder)
+ eWarning("failed to create %s decoder", decodertype);
+
+ /* mp3 decoding needs id3demux to extract ID3 data. 'decodebin' would do that internally. */
+ if (is_mp3)
+ {
+ filter = gst_element_factory_make ("id3demux", "filter");
+ if (!filter)
+ eWarning("failed to create id3demux");
+ }
+
+ conv = gst_element_factory_make ("audioconvert", "converter");
+ if (!conv)
+ eWarning("failed to create audioconvert");
+
+ flt = gst_element_factory_make ("capsfilter", "flt");
+ if (!flt)
+ eWarning("failed to create capsfilter");
+
+ /* for some reasons, we need to set the sample format to depth/width=16, because auto negotiation doesn't work. */
+ /* endianness, however, is not required to be set anymore. */
+ if (flt)
+ {
+ GstCaps *caps = gst_caps_new_simple("audio/x-raw-int", /* "endianness", G_TYPE_INT, 4321, */ "depth", G_TYPE_INT, 16, "width", G_TYPE_INT, 16, "channels", G_TYPE_INT, 2, (char*)0);
+ g_object_set (G_OBJECT (flt), "caps", caps, (char*)0);
+ gst_caps_unref(caps);
+ }
+
+ sink = gst_element_factory_make ("alsasink", "alsa-output");
+ if (!sink)
+ eWarning("failed to create osssink");
+
+ if (source && decoder && conv && sink)
+ all_ok = 1;
+ } else /* is_video */
+ {
+ /* filesrc -> mpegdemux -> | queue_audio -> dvbaudiosink
+ | queue_video -> dvbvideosink */
+
+ audio = gst_element_factory_make("dvbaudiosink", "audio");
+ queue_audio = gst_element_factory_make("queue", "queue_audio");
+
+ video = gst_element_factory_make("dvbvideosink", "video");
+ queue_video = gst_element_factory_make("queue", "queue_video");
+
+ if (is_mpeg_ps)
+ mpegdemux = gst_element_factory_make("flupsdemux", "mpegdemux");
+ else
+ mpegdemux = gst_element_factory_make("flutsdemux", "mpegdemux");
+
+ if (!mpegdemux)
+ {
+ eDebug("fluendo mpegdemux not available, falling back to mpegdemux\n");
+ mpegdemux = gst_element_factory_make("mpegdemux", "mpegdemux");
+ }
+
+ eDebug("audio: %p, queue_audio %p, video %p, queue_video %p, mpegdemux %p", audio, queue_audio, video, queue_video, mpegdemux);
+ if (audio && queue_audio && video && queue_video && mpegdemux)
+ {
+ g_object_set (G_OBJECT (queue_audio), "max-size-buffers", 0, NULL);
+ g_object_set (G_OBJECT (queue_audio), "max-size-time", (guint64)0, NULL);
+ g_object_set (G_OBJECT (queue_video), "max-size-buffers", 0, NULL);
+ g_object_set (G_OBJECT (queue_video), "max-size-time", (guint64)0, NULL);
+ all_ok = 1;
+ }
+ }
+
+ if (m_gst_pipeline && all_ok)
+ {
+ gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
+
+ if (is_audio)
+ {
+ if (!is_mp3)
+ {
+ /* decodebin has dynamic pads. When they get created, we connect them to the audio bin */
+ g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
+ g_signal_connect (decoder, "unknown-type", G_CALLBACK(gstCBunknownType), this);
+ }
+
+ /* gst_bin will take the 'floating references' */
+ gst_bin_add_many (GST_BIN (m_gst_pipeline),
+ source, decoder, NULL);
+
+ if (filter)
+ {
+ /* id3demux also has dynamic pads, which need to be connected to the decoder (this is done in the 'gstCBfilterPadAdded' CB) */
+ gst_bin_add(GST_BIN(m_gst_pipeline), filter);
+ gst_element_link(source, filter);
+ m_decoder = decoder;
+ g_signal_connect (filter, "pad-added", G_CALLBACK(gstCBfilterPadAdded), this);
+ } else
+ /* in decodebin's case we can just connect the source with the decodebin, and decodebin will take care about id3demux (or whatever is required) */
+ gst_element_link(source, decoder);
+
+ /* create audio bin with the audioconverter, the capsfilter and the audiosink */
+ m_gst_audio = gst_bin_new ("audiobin");
+
+ GstPad *audiopad = gst_element_get_pad (conv, "sink");
+ gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, (char*)0);
+ gst_element_link_many(conv, flt, sink, (char*)0);
+ gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
+ gst_object_unref(audiopad);
+ gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
+
+ /* in mad's case, we can directly connect the decoder to the audiobin. otherwise, we do this in gstCBnewPad */
+ if (is_mp3)
+ gst_element_link(decoder, m_gst_audio);
+ } else
+ {
+ gst_bin_add_many(GST_BIN(m_gst_pipeline), source, mpegdemux, audio, queue_audio, video, queue_video, NULL);
+ gst_element_link(source, mpegdemux);
+ gst_element_link(queue_audio, audio);
+ gst_element_link(queue_video, video);
+
+ m_gst_audioqueue = queue_audio;
+ m_gst_videoqueue = queue_video;
+
+ g_signal_connect(mpegdemux, "pad-added", G_CALLBACK (gstCBpadAdded), this);
+ }
+ } else
+ {
+ if (m_gst_pipeline)
+ gst_object_unref(GST_OBJECT(m_gst_pipeline));
+ if (source)
+ gst_object_unref(GST_OBJECT(source));
+ if (decoder)
+ gst_object_unref(GST_OBJECT(decoder));
+ if (conv)
+ gst_object_unref(GST_OBJECT(conv));
+ if (sink)
+ gst_object_unref(GST_OBJECT(sink));
+
+ if (audio)
+ gst_object_unref(GST_OBJECT(audio));
+ if (queue_audio)
+ gst_object_unref(GST_OBJECT(queue_audio));
+ if (video)
+ gst_object_unref(GST_OBJECT(video));
+ if (queue_video)
+ gst_object_unref(GST_OBJECT(queue_video));
+ if (mpegdemux)
+ gst_object_unref(GST_OBJECT(mpegdemux));
+
+ eDebug("sorry, can't play.");
+ m_gst_pipeline = 0;
+ }
+
+ gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);