Commit 2f8467d6 authored by Sebastian Dröge's avatar Sebastian Dröge
Browse files

playsink: Add audio and video converter convenience bins

These reconfigure based on the caps and plugin in converters if
necessary. This also makes switching between compressed and raw
streams work flawlessly without loosing the states of any element
somewhere or having running time problems.
parent 105da803
......@@ -18,6 +18,8 @@ libgstplaybin_la_SOURCES = \
gststreaminfo.c \
gststreamselector.c \
gstsubtitleoverlay.c \
gstplaysinkvideoconvert.c \
gstplaysinkaudioconvert.c \
gststreamsynchronizer.c
nodist_libgstplaybin_la_SOURCES = $(built_sources)
......@@ -57,6 +59,8 @@ noinst_HEADERS = \
gststreamselector.h \
gstrawcaps.h \
gstsubtitleoverlay.h \
gstplaysinkvideoconvert.h \
gstplaysinkaudioconvert.h \
gststreamsynchronizer.h
BUILT_SOURCES = $(built_headers) $(built_sources)
......
......@@ -31,6 +31,8 @@
#include "gstplaysink.h"
#include "gststreamsynchronizer.h"
#include "gstplaysinkvideoconvert.h"
#include "gstplaysinkaudioconvert.h"
GST_DEBUG_CATEGORY_STATIC (gst_play_sink_debug);
#define GST_CAT_DEFAULT gst_play_sink_debug
......@@ -59,7 +61,6 @@ typedef struct
GstPad *sinkpad;
GstElement *queue;
GstElement *conv;
GstElement *resample;
GstElement *volume; /* element with the volume property */
gboolean sink_volume; /* if the volume was provided by the sink */
GstElement *mute; /* element with the mute property */
......@@ -81,7 +82,6 @@ typedef struct
GstPad *sinkpad;
GstElement *queue;
GstElement *conv;
GstElement *scale;
GstElement *sink;
gboolean async;
GstElement *ts_offset;
......@@ -1278,46 +1278,19 @@ gen_video_chain (GstPlaySink * playsink, gboolean raw, gboolean async)
head = prev = chain->queue;
}
if (raw && !(playsink->flags & GST_PLAY_FLAG_NATIVE_VIDEO)) {
GST_DEBUG_OBJECT (playsink, "creating ffmpegcolorspace");
chain->conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
if (chain->conv == NULL) {
post_missing_element_message (playsink, "ffmpegcolorspace");
GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
(_("Missing element '%s' - check your GStreamer installation."),
"ffmpegcolorspace"), ("video rendering might fail"));
if (!(playsink->flags & GST_PLAY_FLAG_NATIVE_VIDEO)) {
GST_DEBUG_OBJECT (playsink, "creating videoconverter");
chain->conv =
g_object_new (GST_TYPE_PLAY_SINK_VIDEO_CONVERT, "name", "vconv", NULL);
gst_bin_add (bin, chain->conv);
if (prev) {
if (!gst_element_link_pads_full (prev, "src", chain->conv, "sink",
GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
goto link_failed;
} else {
gst_bin_add (bin, chain->conv);
if (prev) {
if (!gst_element_link_pads_full (prev, "src", chain->conv, "sink",
GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
goto link_failed;
} else {
head = chain->conv;
}
prev = chain->conv;
}
GST_DEBUG_OBJECT (playsink, "creating videoscale");
chain->scale = gst_element_factory_make ("videoscale", "vscale");
if (chain->scale == NULL) {
post_missing_element_message (playsink, "videoscale");
GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
(_("Missing element '%s' - check your GStreamer installation."),
"videoscale"), ("possibly a liboil version mismatch?"));
} else {
/* Add black borders if necessary to keep the DAR */
g_object_set (chain->scale, "add-borders", TRUE, NULL);
gst_bin_add (bin, chain->scale);
if (prev) {
if (!gst_element_link_pads_full (prev, "src", chain->scale, "sink",
GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
goto link_failed;
} else {
head = chain->scale;
}
prev = chain->scale;
head = chain->conv;
}
prev = chain->conv;
}
if (prev) {
......@@ -1388,8 +1361,7 @@ setup_video_chain (GstPlaySink * playsink, gboolean raw, gboolean async)
chain = playsink->videochain;
if (chain->chain.raw != raw)
return FALSE;
chain->chain.raw = raw;
/* if the chain was active we don't do anything */
if (GST_PLAY_CHAIN (chain)->activated == TRUE)
......@@ -1768,54 +1740,32 @@ gen_audio_chain (GstPlaySink * playsink, gboolean raw)
chain->sink_volume = FALSE;
}
if (raw && !(playsink->flags & GST_PLAY_FLAG_NATIVE_AUDIO)) {
if (!(playsink->flags & GST_PLAY_FLAG_NATIVE_AUDIO) || (!have_volume
&& playsink->flags & GST_PLAY_FLAG_SOFT_VOLUME)) {
GST_DEBUG_OBJECT (playsink, "creating audioconvert");
chain->conv = gst_element_factory_make ("audioconvert", "aconv");
if (chain->conv == NULL) {
post_missing_element_message (playsink, "audioconvert");
GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
(_("Missing element '%s' - check your GStreamer installation."),
"audioconvert"), ("possibly a liboil version mismatch?"));
chain->conv =
g_object_new (GST_TYPE_PLAY_SINK_AUDIO_CONVERT, "name", "aconv", NULL);
gst_bin_add (bin, chain->conv);
if (prev) {
if (!gst_element_link_pads_full (prev, "src", chain->conv, "sink",
GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
goto link_failed;
} else {
gst_bin_add (bin, chain->conv);
if (prev) {
if (!gst_element_link_pads_full (prev, "src", chain->conv, "sink",
GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
goto link_failed;
} else {
head = chain->conv;
}
prev = chain->conv;
head = chain->conv;
}
prev = chain->conv;
GST_DEBUG_OBJECT (playsink, "creating audioresample");
chain->resample = gst_element_factory_make ("audioresample", "aresample");
if (chain->resample == NULL) {
post_missing_element_message (playsink, "audioresample");
GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
(_("Missing element '%s' - check your GStreamer installation."),
"audioresample"), ("possibly a liboil version mismatch?"));
} else {
gst_bin_add (bin, chain->resample);
if (prev) {
if (!gst_element_link_pads_full (prev, "src", chain->resample, "sink",
GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
goto link_failed;
} else {
head = chain->resample;
}
prev = chain->resample;
}
GST_PLAY_SINK_AUDIO_CONVERT_CAST (chain->conv)->use_converters =
!(playsink->flags & GST_PLAY_FLAG_NATIVE_AUDIO);
GST_PLAY_SINK_AUDIO_CONVERT_CAST (chain->conv)->use_volume = (!have_volume
&& playsink->flags & GST_PLAY_FLAG_SOFT_VOLUME);
if (!have_volume && playsink->flags & GST_PLAY_FLAG_SOFT_VOLUME) {
GST_DEBUG_OBJECT (playsink, "creating volume");
chain->volume = gst_element_factory_make ("volume", "volume");
if (chain->volume == NULL) {
post_missing_element_message (playsink, "volume");
GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
(_("Missing element '%s' - check your GStreamer installation."),
"volume"), ("possibly a liboil version mismatch?"));
} else {
GstPlaySinkAudioConvert *conv =
GST_PLAY_SINK_AUDIO_CONVERT_CAST (chain->conv);
if (conv->volume) {
chain->volume = conv->volume;
have_volume = TRUE;
g_signal_connect (chain->volume, "notify::volume",
......@@ -1830,16 +1780,6 @@ gen_audio_chain (GstPlaySink * playsink, gboolean raw)
g_object_set (G_OBJECT (chain->volume), "volume", playsink->volume,
NULL);
g_object_set (G_OBJECT (chain->mute), "mute", playsink->mute, NULL);
gst_bin_add (bin, chain->volume);
if (prev) {
if (!gst_element_link_pads_full (prev, "src", chain->volume, "sink",
GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
goto link_failed;
} else {
head = chain->volume;
}
prev = chain->volume;
}
}
}
......@@ -1921,8 +1861,7 @@ setup_audio_chain (GstPlaySink * playsink, gboolean raw)
chain = playsink->audiochain;
if (chain->chain.raw != raw)
return FALSE;
chain->chain.raw = raw;
/* if the chain was active we don't do anything */
if (GST_PLAY_CHAIN (chain)->activated == TRUE)
......@@ -1967,29 +1906,35 @@ setup_audio_chain (GstPlaySink * playsink, gboolean raw)
g_signal_connect (chain->mute, "notify::mute",
G_CALLBACK (notify_mute_cb), playsink);
}
GST_PLAY_SINK_AUDIO_CONVERT_CAST (chain->conv)->use_volume = FALSE;
} else {
GstPlaySinkAudioConvert *conv =
GST_PLAY_SINK_AUDIO_CONVERT_CAST (chain->conv);
/* no volume, we need to add a volume element when we can */
conv->use_volume = TRUE;
GST_DEBUG_OBJECT (playsink, "the sink has no volume property");
if (!raw) {
GST_LOG_OBJECT (playsink, "non-raw format, can't do soft volume control");
disconnect_chain (chain, playsink);
chain->volume = NULL;
chain->mute = NULL;
} else {
/* both last and current chain are raw audio, there should be a volume
* element already, unless the sink changed from one with a volume
* property to one that hasn't got a volume property, in which case we
* re-generate the chain */
if (chain->volume == NULL) {
GST_DEBUG_OBJECT (playsink, "no existing volume element to re-use");
/* undo background state change done earlier */
gst_element_set_state (chain->sink, GST_STATE_NULL);
return FALSE;
}
/* Disconnect signals */
disconnect_chain (chain, playsink);
if (conv->volume) {
chain->volume = conv->volume;
chain->mute = chain->volume;
GST_DEBUG_OBJECT (playsink, "reusing existing volume element");
g_signal_connect (chain->volume, "notify::volume",
G_CALLBACK (notify_volume_cb), playsink);
g_signal_connect (chain->mute, "notify::mute",
G_CALLBACK (notify_mute_cb), playsink);
/* configure with the latest volume and mute */
g_object_set (G_OBJECT (chain->volume), "volume", playsink->volume, NULL);
g_object_set (G_OBJECT (chain->mute), "mute", playsink->mute, NULL);
}
GST_DEBUG_OBJECT (playsink, "reusing existing volume element");
}
return TRUE;
}
......
/* GStreamer
* Copyright (C) <2011> Sebastian Dröge <sebastian.droege@collabora.co.uk>
*
* 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstplaysinkaudioconvert.h"
#include <gst/pbutils/pbutils.h>
#include <gst/gst-i18n-plugin.h>
GST_DEBUG_CATEGORY_STATIC (gst_play_sink_audio_convert_debug);
#define GST_CAT_DEFAULT gst_play_sink_audio_convert_debug
#define parent_class gst_play_sink_audio_convert_parent_class
G_DEFINE_TYPE (GstPlaySinkAudioConvert, gst_play_sink_audio_convert,
GST_TYPE_BIN);
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
static gboolean
is_raw_caps (GstCaps * caps)
{
gint i, n;
GstStructure *s;
const gchar *name;
n = gst_caps_get_size (caps);
for (i = 0; i < n; i++) {
s = gst_caps_get_structure (caps, i);
name = gst_structure_get_name (s);
if (!g_str_has_prefix (name, "audio/x-raw"))
return FALSE;
}
return TRUE;
}
static void
post_missing_element_message (GstPlaySinkAudioConvert * self,
const gchar * name)
{
GstMessage *msg;
msg = gst_missing_element_message_new (GST_ELEMENT_CAST (self), name);
gst_element_post_message (GST_ELEMENT_CAST (self), msg);
}
static void
distribute_running_time (GstElement * element, const GstSegment * segment)
{
GstEvent *event;
GstPad *pad;
pad = gst_element_get_static_pad (element, "sink");
if (segment->accum) {
event = gst_event_new_new_segment_full (FALSE, segment->rate,
segment->applied_rate, segment->format, 0, segment->accum, 0);
gst_pad_push_event (pad, event);
}
event = gst_event_new_new_segment_full (FALSE, segment->rate,
segment->applied_rate, segment->format,
segment->start, segment->stop, segment->time);
gst_pad_push_event (pad, event);
gst_object_unref (pad);
}
static void
pad_blocked_cb (GstPad * pad, gboolean blocked, GstPlaySinkAudioConvert * self)
{
GstPad *peer;
GstCaps *caps;
gboolean raw;
GST_PLAY_SINK_AUDIO_CONVERT_LOCK (self);
self->sink_proxypad_blocked = blocked;
GST_DEBUG_OBJECT (self, "Pad blocked: %d", blocked);
if (!blocked)
goto done;
/* There must be a peer at this point */
peer = gst_pad_get_peer (self->sinkpad);
caps = gst_pad_get_negotiated_caps (peer);
if (!caps)
caps = gst_pad_get_caps_reffed (peer);
gst_object_unref (peer);
raw = is_raw_caps (caps);
GST_DEBUG_OBJECT (self, "Caps %" GST_PTR_FORMAT " are raw: %d", caps, raw);
gst_caps_unref (caps);
if (raw == self->raw)
goto unblock;
self->raw = raw;
if (raw) {
GstBin *bin = GST_BIN_CAST (self);
GstElement *head = NULL, *prev = NULL;
GstPad *pad;
GST_DEBUG_OBJECT (self, "Creating raw conversion pipeline");
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
if (self->use_converters) {
self->conv = gst_element_factory_make ("audioconvert", "conv");
if (self->conv == NULL) {
post_missing_element_message (self, "audioconvert");
GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
(_("Missing element '%s' - check your GStreamer installation."),
"audioconvert"), ("audio rendering might fail"));
} else {
gst_bin_add (bin, self->conv);
gst_element_sync_state_with_parent (self->conv);
distribute_running_time (self->conv, &self->segment);
prev = head = self->conv;
}
self->resample = gst_element_factory_make ("audioresample", "resample");
if (self->resample == NULL) {
post_missing_element_message (self, "audioresample");
GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
(_("Missing element '%s' - check your GStreamer installation."),
"audioresample"), ("possibly a liboil version mismatch?"));
} else {
gst_bin_add (bin, self->resample);
gst_element_sync_state_with_parent (self->resample);
distribute_running_time (self->resample, &self->segment);
if (prev) {
if (!gst_element_link_pads_full (prev, "src", self->resample, "sink",
GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
goto link_failed;
} else {
head = self->resample;
}
prev = self->resample;
}
}
if (self->use_volume && self->volume) {
gst_bin_add (bin, gst_object_ref (self->volume));
gst_element_sync_state_with_parent (self->volume);
distribute_running_time (self->volume, &self->segment);
if (prev) {
if (!gst_element_link_pads_full (prev, "src", self->volume, "sink",
GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
goto link_failed;
} else {
head = self->volume;
}
prev = self->volume;
}
if (head) {
pad = gst_element_get_static_pad (head, "sink");
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad);
gst_object_unref (pad);
}
if (prev) {
pad = gst_element_get_static_pad (prev, "src");
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), pad);
gst_object_unref (pad);
}
if (!head && !prev) {
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
self->sink_proxypad);
}
GST_DEBUG_OBJECT (self, "Raw conversion pipeline created");
} else {
GstBin *bin = GST_BIN_CAST (self);
GST_DEBUG_OBJECT (self, "Removing raw conversion pipeline");
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
if (self->conv) {
gst_element_set_state (self->conv, GST_STATE_NULL);
gst_bin_remove (bin, self->conv);
self->conv = NULL;
}
if (self->resample) {
gst_element_set_state (self->resample, GST_STATE_NULL);
gst_bin_remove (bin, self->resample);
self->resample = NULL;
}
if (self->volume) {
gst_element_set_state (self->volume, GST_STATE_NULL);
if (GST_OBJECT_PARENT (self->volume) == GST_OBJECT_CAST (self)) {
gst_bin_remove (GST_BIN_CAST (self), self->volume);
}
}
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
self->sink_proxypad);
GST_DEBUG_OBJECT (self, "Raw conversion pipeline removed");
}
unblock:
gst_pad_set_blocked_async_full (self->sink_proxypad, FALSE,
(GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
(GDestroyNotify) gst_object_unref);
done:
GST_PLAY_SINK_AUDIO_CONVERT_UNLOCK (self);
return;
link_failed:
{
GST_ELEMENT_ERROR (self, CORE, PAD,
(NULL), ("Failed to configure the audio converter."));
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
self->sink_proxypad);
gst_pad_set_blocked_async_full (self->sink_proxypad, FALSE,
(GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
(GDestroyNotify) gst_object_unref);
return;
}
}
static gboolean
gst_play_sink_audio_convert_sink_event (GstPad * pad, GstEvent * event)
{
GstPlaySinkAudioConvert *self =
GST_PLAY_SINK_AUDIO_CONVERT (gst_pad_get_parent (pad));
gboolean ret;
ret = self->sink_event (pad, gst_event_ref (event));
if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
gboolean update;
gdouble rate, applied_rate;
GstFormat format;
gint64 start, stop, position;
GST_PLAY_SINK_AUDIO_CONVERT_LOCK (self);
gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
&format, &start, &stop, &position);
GST_DEBUG_OBJECT (self, "Segment before %" GST_SEGMENT_FORMAT,
&self->segment);
gst_segment_set_newsegment_full (&self->segment, update, rate, applied_rate,
format, start, stop, position);
GST_DEBUG_OBJECT (self, "Segment after %" GST_SEGMENT_FORMAT,
&self->segment);
GST_PLAY_SINK_AUDIO_CONVERT_UNLOCK (self);
} else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
GST_PLAY_SINK_AUDIO_CONVERT_LOCK (self);
GST_DEBUG_OBJECT (self, "Resetting segment");
gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
GST_PLAY_SINK_AUDIO_CONVERT_UNLOCK (self);
}
gst_event_unref (event);
gst_object_unref (self);
return ret;
}
static gboolean
gst_play_sink_audio_convert_sink_setcaps (GstPad * pad, GstCaps * caps)
{
GstPlaySinkAudioConvert *self =
GST_PLAY_SINK_AUDIO_CONVERT (gst_pad_get_parent (pad));
gboolean ret;
GstStructure *s;
const gchar *name;
gboolean reconfigure = FALSE;
GST_PLAY_SINK_AUDIO_CONVERT_LOCK (self);
s = gst_caps_get_structure (caps, 0);
name = gst_structure_get_name (s);
if (g_str_has_prefix (name, "audio/x-raw-")) {
if (!self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
GST_DEBUG_OBJECT (self, "Changing caps from non-raw to raw");
reconfigure = TRUE;
gst_pad_set_blocked_async_full (self->sink_proxypad, TRUE,
(GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
(GDestroyNotify) gst_object_unref);
}
} else {
if (self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
GST_DEBUG_OBJECT (self, "Changing caps from raw to non-raw");
reconfigure = TRUE;
gst_pad_set_blocked_async_full (self->sink_proxypad, TRUE,
(GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
(GDestroyNotify) gst_object_unref);
}
}
/* Otherwise the setcaps below fails */
if (reconfigure) {
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
}
GST_PLAY_SINK_AUDIO_CONVERT_UNLOCK (self);
ret = self->sink_setcaps (pad, caps);
GST_DEBUG_OBJECT (self, "Setting sink caps %" GST_PTR_FORMAT ": %d", caps,
ret);
gst_object_unref (self);
return ret;
}
static GstCaps *
gst_play_sink_audio_convert_getcaps (GstPad * pad)
{
GstPlaySinkAudioConvert *self =
GST_PLAY_SINK_AUDIO_CONVERT (gst_pad_get_parent (pad));