interaudio: Properly pass through caps from the sink to the source

Otherwise a magic capsfilter after the source is required with
exactly the same caps as the input.
This commit is contained in:
Sebastian Dröge 2014-10-22 18:40:01 +02:00
parent 211a39e55e
commit 6f72e8ceab
5 changed files with 136 additions and 52 deletions

View File

@ -61,6 +61,8 @@ static void gst_inter_audio_sink_get_times (GstBaseSink * sink,
GstBuffer * buffer, GstClockTime * start, GstClockTime * end); GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
static gboolean gst_inter_audio_sink_start (GstBaseSink * sink); static gboolean gst_inter_audio_sink_start (GstBaseSink * sink);
static gboolean gst_inter_audio_sink_stop (GstBaseSink * sink); static gboolean gst_inter_audio_sink_stop (GstBaseSink * sink);
static gboolean gst_inter_audio_sink_set_caps (GstBaseSink * sink,
GstCaps * caps);
static GstFlowReturn gst_inter_audio_sink_render (GstBaseSink * sink, static GstFlowReturn gst_inter_audio_sink_render (GstBaseSink * sink,
GstBuffer * buffer); GstBuffer * buffer);
@ -108,6 +110,7 @@ gst_inter_audio_sink_class_init (GstInterAudioSinkClass * klass)
GST_DEBUG_FUNCPTR (gst_inter_audio_sink_get_times); GST_DEBUG_FUNCPTR (gst_inter_audio_sink_get_times);
base_sink_class->start = GST_DEBUG_FUNCPTR (gst_inter_audio_sink_start); base_sink_class->start = GST_DEBUG_FUNCPTR (gst_inter_audio_sink_start);
base_sink_class->stop = GST_DEBUG_FUNCPTR (gst_inter_audio_sink_stop); base_sink_class->stop = GST_DEBUG_FUNCPTR (gst_inter_audio_sink_stop);
base_sink_class->set_caps = GST_DEBUG_FUNCPTR (gst_inter_audio_sink_set_caps);
base_sink_class->render = GST_DEBUG_FUNCPTR (gst_inter_audio_sink_render); base_sink_class->render = GST_DEBUG_FUNCPTR (gst_inter_audio_sink_render);
g_object_class_install_property (gobject_class, PROP_CHANNEL, g_object_class_install_property (gobject_class, PROP_CHANNEL,
@ -177,10 +180,10 @@ gst_inter_audio_sink_get_times (GstBaseSink * sink, GstBuffer * buffer,
if (GST_BUFFER_DURATION_IS_VALID (buffer)) { if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
*end = *start + GST_BUFFER_DURATION (buffer); *end = *start + GST_BUFFER_DURATION (buffer);
} else { } else {
if (interaudiosink->fps_n > 0) { if (interaudiosink->info.rate > 0) {
*end = *start + *end = *start +
gst_util_uint64_scale_int (GST_SECOND, interaudiosink->fps_d, gst_util_uint64_scale_int (gst_buffer_get_size (buffer), GST_SECOND,
interaudiosink->fps_n); interaudiosink->info.rate * interaudiosink->info.bpf);
} }
} }
} }
@ -194,6 +197,9 @@ gst_inter_audio_sink_start (GstBaseSink * sink)
GST_DEBUG ("start"); GST_DEBUG ("start");
interaudiosink->surface = gst_inter_surface_get (interaudiosink->channel); interaudiosink->surface = gst_inter_surface_get (interaudiosink->channel);
g_mutex_lock (&interaudiosink->surface->mutex);
memset (&interaudiosink->surface->audio_info, 0, sizeof (GstAudioInfo));
g_mutex_unlock (&interaudiosink->surface->mutex);
return TRUE; return TRUE;
} }
@ -207,6 +213,7 @@ gst_inter_audio_sink_stop (GstBaseSink * sink)
g_mutex_lock (&interaudiosink->surface->mutex); g_mutex_lock (&interaudiosink->surface->mutex);
gst_adapter_clear (interaudiosink->surface->audio_adapter); gst_adapter_clear (interaudiosink->surface->audio_adapter);
memset (&interaudiosink->surface->audio_info, 0, sizeof (GstAudioInfo));
g_mutex_unlock (&interaudiosink->surface->mutex); g_mutex_unlock (&interaudiosink->surface->mutex);
gst_inter_surface_unref (interaudiosink->surface); gst_inter_surface_unref (interaudiosink->surface);
@ -215,22 +222,41 @@ gst_inter_audio_sink_stop (GstBaseSink * sink)
return TRUE; return TRUE;
} }
static gboolean
gst_inter_audio_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
{
GstInterAudioSink *interaudiosink = GST_INTER_AUDIO_SINK (sink);
GstAudioInfo info;
if (!gst_audio_info_from_caps (&info, caps)) {
GST_ERROR_OBJECT (sink, "Failed to parse caps %" GST_PTR_FORMAT, caps);
return FALSE;
}
g_mutex_lock (&interaudiosink->surface->mutex);
interaudiosink->surface->audio_info = info;
interaudiosink->info = info;
g_mutex_unlock (&interaudiosink->surface->mutex);
return TRUE;
}
static GstFlowReturn static GstFlowReturn
gst_inter_audio_sink_render (GstBaseSink * sink, GstBuffer * buffer) gst_inter_audio_sink_render (GstBaseSink * sink, GstBuffer * buffer)
{ {
GstInterAudioSink *interaudiosink = GST_INTER_AUDIO_SINK (sink); GstInterAudioSink *interaudiosink = GST_INTER_AUDIO_SINK (sink);
int n; int n, bpf;
GST_DEBUG ("render %" G_GSIZE_FORMAT, gst_buffer_get_size (buffer)); GST_DEBUG ("render %" G_GSIZE_FORMAT, gst_buffer_get_size (buffer));
bpf = interaudiosink->info.bpf;
g_mutex_lock (&interaudiosink->surface->mutex); g_mutex_lock (&interaudiosink->surface->mutex);
n = gst_adapter_available (interaudiosink->surface->audio_adapter) / 4; n = gst_adapter_available (interaudiosink->surface->audio_adapter) / 4;
#define SIZE 1600 #define SIZE 1600
if (n > (SIZE * 3)) { if (n > SIZE * 3) {
int n_chunks = (n / (SIZE / 2)) - 4; GST_WARNING ("flushing %d samples", SIZE / 2);
GST_WARNING ("flushing %d samples", n_chunks * 800);
gst_adapter_flush (interaudiosink->surface->audio_adapter, gst_adapter_flush (interaudiosink->surface->audio_adapter,
n_chunks * (SIZE / 2) * 4); (SIZE / 2) * bpf);
} }
gst_adapter_push (interaudiosink->surface->audio_adapter, gst_adapter_push (interaudiosink->surface->audio_adapter,
gst_buffer_ref (buffer)); gst_buffer_ref (buffer));

View File

@ -41,8 +41,7 @@ struct _GstInterAudioSink
GstInterSurface *surface; GstInterSurface *surface;
char *channel; char *channel;
int fps_n; GstAudioInfo info;
int fps_d;
}; };
struct _GstInterAudioSinkClass struct _GstInterAudioSinkClass

View File

@ -57,6 +57,8 @@ static void gst_inter_audio_src_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec); guint property_id, GValue * value, GParamSpec * pspec);
static void gst_inter_audio_src_finalize (GObject * object); static void gst_inter_audio_src_finalize (GObject * object);
static GstCaps *gst_inter_audio_src_get_caps (GstBaseSrc * src,
GstCaps * filter);
static gboolean gst_inter_audio_src_set_caps (GstBaseSrc * src, GstCaps * caps); static gboolean gst_inter_audio_src_set_caps (GstBaseSrc * src, GstCaps * caps);
static gboolean gst_inter_audio_src_start (GstBaseSrc * src); static gboolean gst_inter_audio_src_start (GstBaseSrc * src);
static gboolean gst_inter_audio_src_stop (GstBaseSrc * src); static gboolean gst_inter_audio_src_stop (GstBaseSrc * src);
@ -86,6 +88,7 @@ GST_STATIC_PAD_TEMPLATE ("src",
/* class initialization */ /* class initialization */
#define parent_class gst_inter_audio_src_parent_class
G_DEFINE_TYPE (GstInterAudioSrc, gst_inter_audio_src, GST_TYPE_BASE_SRC); G_DEFINE_TYPE (GstInterAudioSrc, gst_inter_audio_src, GST_TYPE_BASE_SRC);
static void static void
@ -110,6 +113,7 @@ gst_inter_audio_src_class_init (GstInterAudioSrcClass * klass)
gobject_class->set_property = gst_inter_audio_src_set_property; gobject_class->set_property = gst_inter_audio_src_set_property;
gobject_class->get_property = gst_inter_audio_src_get_property; gobject_class->get_property = gst_inter_audio_src_get_property;
gobject_class->finalize = gst_inter_audio_src_finalize; gobject_class->finalize = gst_inter_audio_src_finalize;
base_src_class->get_caps = GST_DEBUG_FUNCPTR (gst_inter_audio_src_get_caps);
base_src_class->set_caps = GST_DEBUG_FUNCPTR (gst_inter_audio_src_set_caps); base_src_class->set_caps = GST_DEBUG_FUNCPTR (gst_inter_audio_src_set_caps);
base_src_class->start = GST_DEBUG_FUNCPTR (gst_inter_audio_src_start); base_src_class->start = GST_DEBUG_FUNCPTR (gst_inter_audio_src_start);
base_src_class->stop = GST_DEBUG_FUNCPTR (gst_inter_audio_src_stop); base_src_class->stop = GST_DEBUG_FUNCPTR (gst_inter_audio_src_stop);
@ -178,36 +182,51 @@ gst_inter_audio_src_finalize (GObject * object)
G_OBJECT_CLASS (gst_inter_audio_src_parent_class)->finalize (object); G_OBJECT_CLASS (gst_inter_audio_src_parent_class)->finalize (object);
} }
static GstCaps *
gst_inter_audio_src_get_caps (GstBaseSrc * src, GstCaps * filter)
{
GstInterAudioSrc *interaudiosrc = GST_INTER_AUDIO_SRC (src);
GstCaps *caps;
GST_DEBUG_OBJECT (interaudiosrc, "get_caps");
if (!interaudiosrc->surface)
return GST_BASE_SRC_CLASS (parent_class)->get_caps (src, filter);
g_mutex_lock (&interaudiosrc->surface->mutex);
if (interaudiosrc->surface->audio_info.finfo) {
caps = gst_audio_info_to_caps (&interaudiosrc->surface->audio_info);
if (filter) {
GstCaps *tmp;
tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
gst_caps_unref (caps);
caps = tmp;
}
} else {
caps = NULL;
}
g_mutex_unlock (&interaudiosrc->surface->mutex);
if (caps)
return caps;
else
return GST_BASE_SRC_CLASS (parent_class)->get_caps (src, filter);
}
static gboolean static gboolean
gst_inter_audio_src_set_caps (GstBaseSrc * src, GstCaps * caps) gst_inter_audio_src_set_caps (GstBaseSrc * src, GstCaps * caps)
{ {
GstInterAudioSrc *interaudiosrc = GST_INTER_AUDIO_SRC (src); GstInterAudioSrc *interaudiosrc = GST_INTER_AUDIO_SRC (src);
const GstStructure *structure;
GstAudioInfo info;
gboolean ret;
int sample_rate;
GST_DEBUG_OBJECT (interaudiosrc, "set_caps"); GST_DEBUG_OBJECT (interaudiosrc, "set_caps");
structure = gst_caps_get_structure (caps, 0); if (!gst_audio_info_from_caps (&interaudiosrc->info, caps)) {
GST_ERROR_OBJECT (src, "Failed to parse caps %" GST_PTR_FORMAT, caps);
if (!gst_structure_get_int (structure, "rate", &sample_rate)) {
GST_ERROR_OBJECT (src, "Audio caps without rate");
return FALSE; return FALSE;
} }
interaudiosrc->sample_rate = sample_rate; return gst_pad_set_caps (src->srcpad, caps);
if (!gst_audio_info_from_caps (&info, caps)) {
GST_ERROR_OBJECT (src, "Can't parse audio caps");
return FALSE;
}
interaudiosrc->finfo = info.finfo;
ret = gst_pad_set_caps (src->srcpad, caps);
return ret;
} }
static gboolean static gboolean
@ -218,6 +237,8 @@ gst_inter_audio_src_start (GstBaseSrc * src)
GST_DEBUG_OBJECT (interaudiosrc, "start"); GST_DEBUG_OBJECT (interaudiosrc, "start");
interaudiosrc->surface = gst_inter_surface_get (interaudiosrc->channel); interaudiosrc->surface = gst_inter_surface_get (interaudiosrc->channel);
interaudiosrc->timestamp_offset = 0;
interaudiosrc->n_samples = 0;
return TRUE; return TRUE;
} }
@ -231,7 +252,6 @@ gst_inter_audio_src_stop (GstBaseSrc * src)
gst_inter_surface_unref (interaudiosrc->surface); gst_inter_surface_unref (interaudiosrc->surface);
interaudiosrc->surface = NULL; interaudiosrc->surface = NULL;
interaudiosrc->finfo = NULL;
return TRUE; return TRUE;
} }
@ -268,38 +288,68 @@ gst_inter_audio_src_create (GstBaseSrc * src, guint64 offset, guint size,
GstBuffer ** buf) GstBuffer ** buf)
{ {
GstInterAudioSrc *interaudiosrc = GST_INTER_AUDIO_SRC (src); GstInterAudioSrc *interaudiosrc = GST_INTER_AUDIO_SRC (src);
GstCaps *caps;
GstBuffer *buffer; GstBuffer *buffer;
int n; int n, bpf;
GST_DEBUG_OBJECT (interaudiosrc, "create"); GST_DEBUG_OBJECT (interaudiosrc, "create");
buffer = NULL; buffer = NULL;
caps = NULL;
g_mutex_lock (&interaudiosrc->surface->mutex); g_mutex_lock (&interaudiosrc->surface->mutex);
n = gst_adapter_available (interaudiosrc->surface->audio_adapter) / 4; if (interaudiosrc->surface->audio_info.finfo) {
if (!gst_audio_info_is_equal (&interaudiosrc->surface->audio_info,
&interaudiosrc->info)) {
caps = gst_audio_info_to_caps (&interaudiosrc->surface->audio_info);
interaudiosrc->timestamp_offset =
gst_util_uint64_scale_int (interaudiosrc->n_samples, GST_SECOND,
interaudiosrc->info.rate);
interaudiosrc->n_samples = 0;
}
}
bpf = interaudiosrc->surface->audio_info.bpf;
n = bpf >
0 ? gst_adapter_available (interaudiosrc->surface->audio_adapter) /
bpf : 0;
if (n > SIZE * 3) { if (n > SIZE * 3) {
GST_WARNING ("flushing %d samples", SIZE / 2); GST_WARNING ("flushing %d samples", SIZE / 2);
gst_adapter_flush (interaudiosrc->surface->audio_adapter, (SIZE / 2) * 4); gst_adapter_flush (interaudiosrc->surface->audio_adapter, (SIZE / 2) * bpf);
n -= (SIZE / 2); n -= (SIZE / 2);
} }
if (n > SIZE) if (n > SIZE)
n = SIZE; n = SIZE;
if (n > 0) { if (n > 0) {
buffer = gst_adapter_take_buffer (interaudiosrc->surface->audio_adapter, buffer = gst_adapter_take_buffer (interaudiosrc->surface->audio_adapter,
n * 4); n * bpf);
} else { } else {
buffer = gst_buffer_new (); buffer = gst_buffer_new ();
} }
g_mutex_unlock (&interaudiosrc->surface->mutex); g_mutex_unlock (&interaudiosrc->surface->mutex);
if (caps) {
gboolean ret = gst_base_src_set_caps (src, caps);
gst_caps_unref (caps);
if (!ret) {
GST_ERROR_OBJECT (src, "Failed to set caps %" GST_PTR_FORMAT, caps);
if (buffer)
gst_buffer_unref (buffer);
return GST_FLOW_NOT_NEGOTIATED;
}
}
bpf = interaudiosrc->info.bpf;
if (n < SIZE) { if (n < SIZE) {
GstMapInfo map; GstMapInfo map;
GstMemory *mem; GstMemory *mem;
GST_WARNING ("creating %d samples of silence", SIZE - n); GST_WARNING ("creating %d samples of silence", SIZE - n);
mem = gst_allocator_alloc (NULL, (SIZE - n) * 4, NULL); mem = gst_allocator_alloc (NULL, (SIZE - n) * bpf, NULL);
if (gst_memory_map (mem, &map, GST_MAP_WRITE)) { if (gst_memory_map (mem, &map, GST_MAP_WRITE)) {
gst_audio_format_fill_silence (interaudiosrc->finfo, map.data, map.size); gst_audio_format_fill_silence (interaudiosrc->info.finfo, map.data,
map.size);
gst_memory_unmap (mem, &map); gst_memory_unmap (mem, &map);
} }
buffer = gst_buffer_make_writable (buffer); buffer = gst_buffer_make_writable (buffer);
@ -311,12 +361,12 @@ gst_inter_audio_src_create (GstBaseSrc * src, guint64 offset, guint size,
GST_BUFFER_OFFSET_END (buffer) = interaudiosrc->n_samples + n; GST_BUFFER_OFFSET_END (buffer) = interaudiosrc->n_samples + n;
GST_BUFFER_TIMESTAMP (buffer) = GST_BUFFER_TIMESTAMP (buffer) =
gst_util_uint64_scale_int (interaudiosrc->n_samples, GST_SECOND, gst_util_uint64_scale_int (interaudiosrc->n_samples, GST_SECOND,
interaudiosrc->sample_rate); interaudiosrc->info.rate);
GST_DEBUG_OBJECT (interaudiosrc, "create ts %" GST_TIME_FORMAT, GST_DEBUG_OBJECT (interaudiosrc, "create ts %" GST_TIME_FORMAT,
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer))); GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
GST_BUFFER_DURATION (buffer) = GST_BUFFER_DURATION (buffer) =
gst_util_uint64_scale_int (interaudiosrc->n_samples + n, GST_SECOND, gst_util_uint64_scale_int (interaudiosrc->n_samples + n, GST_SECOND,
interaudiosrc->sample_rate) - GST_BUFFER_TIMESTAMP (buffer); interaudiosrc->info.rate) - GST_BUFFER_TIMESTAMP (buffer);
GST_BUFFER_OFFSET (buffer) = interaudiosrc->n_samples; GST_BUFFER_OFFSET (buffer) = interaudiosrc->n_samples;
GST_BUFFER_OFFSET_END (buffer) = -1; GST_BUFFER_OFFSET_END (buffer) = -1;
GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT); GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT);
@ -333,6 +383,7 @@ gst_inter_audio_src_create (GstBaseSrc * src, guint64 offset, guint size,
static gboolean static gboolean
gst_inter_audio_src_query (GstBaseSrc * src, GstQuery * query) gst_inter_audio_src_query (GstBaseSrc * src, GstQuery * query)
{ {
GstInterAudioSrc *interaudiosrc = GST_INTER_AUDIO_SRC (src);
gboolean ret; gboolean ret;
GST_DEBUG_OBJECT (src, "query"); GST_DEBUG_OBJECT (src, "query");
@ -341,7 +392,12 @@ gst_inter_audio_src_query (GstBaseSrc * src, GstQuery * query)
case GST_QUERY_LATENCY:{ case GST_QUERY_LATENCY:{
GstClockTime min_latency, max_latency; GstClockTime min_latency, max_latency;
min_latency = 30 * gst_util_uint64_scale_int (GST_SECOND, SIZE, 48000); if (interaudiosrc->info.rate > 0) {
/* FIXME: Where does the 30 come from? */
min_latency =
30 * gst_util_uint64_scale_int (GST_SECOND, SIZE,
interaudiosrc->info.rate);
max_latency = min_latency; max_latency = min_latency;
@ -353,6 +409,9 @@ gst_inter_audio_src_query (GstBaseSrc * src, GstQuery * query)
gst_base_src_is_live (src), min_latency, max_latency); gst_base_src_is_live (src), min_latency, max_latency);
ret = TRUE; ret = TRUE;
} else {
ret = FALSE;
}
break; break;
} }
default: default:
@ -375,6 +434,7 @@ gst_inter_audio_src_fixate (GstBaseSrc * src, GstCaps * caps)
structure = gst_caps_get_structure (caps, 0); structure = gst_caps_get_structure (caps, 0);
gst_structure_fixate_field_string (structure, "format", GST_AUDIO_NE (S16));
gst_structure_fixate_field_nearest_int (structure, "channels", 2); gst_structure_fixate_field_nearest_int (structure, "channels", 2);
gst_structure_fixate_field_nearest_int (structure, "rate", 48000); gst_structure_fixate_field_nearest_int (structure, "rate", 48000);

View File

@ -43,9 +43,8 @@ struct _GstInterAudioSrc
char *channel; char *channel;
guint64 n_samples; guint64 n_samples;
int sample_rate; GstClockTime timestamp_offset;
GstAudioInfo info;
const GstAudioFormatInfo *finfo;
}; };
struct _GstInterAudioSrcClass struct _GstInterAudioSrcClass

View File

@ -21,6 +21,7 @@
#define _GST_INTER_SURFACE_H_ #define _GST_INTER_SURFACE_H_
#include <gst/base/gstadapter.h> #include <gst/base/gstadapter.h>
#include <gst/audio/audio.h>
#include <gst/video/video.h> #include <gst/video/video.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -39,8 +40,7 @@ struct _GstInterSurface
int video_buffer_count; int video_buffer_count;
/* audio */ /* audio */
int sample_rate; GstAudioInfo audio_info;
int n_channels;
GstBuffer *video_buffer; GstBuffer *video_buffer;
GstBuffer *sub_buffer; GstBuffer *sub_buffer;