Commit ff04bd8c authored by Nirbheek Chauhan's avatar Nirbheek Chauhan 🐜
Browse files

audiolatency: Handle audio buffers with invalid duration

pipewiresrc outputs audio buffers without a valid duration, so we need
to calculate it manually in that case.

Upstream issue: pipewire/pipewire#1438

Part-of: <gstreamer/gst-plugins-bad!2444>
parent ce1c7b97
......@@ -288,7 +288,7 @@ buffer_has_wave (GstBuffer * buffer, GstPad * pad)
GstMapInfo minfo;
guint64 duration;
gint64 offset;
gint ii, channels, fsize;
gint ii, channels, fsize, rate;
gfloat *fdata;
gboolean ret;
GstMemory *memory = NULL;
......@@ -313,18 +313,26 @@ buffer_has_wave (GstBuffer * buffer, GstPad * pad)
caps = gst_pad_get_current_caps (pad);
s = gst_caps_get_structure (caps, 0);
ret = gst_structure_get_int (s, "channels", &channels);
/* channels and rate are required in caps, so will always be present */
gst_structure_get_int (s, "channels", &channels);
gst_structure_get_int (s, "rate", &rate);
gst_caps_unref (caps);
if (!ret) {
GST_WARNING_OBJECT (pad, "unknown number of channels, can't detect wave");
return -1;
}
fdata = (gfloat *) minfo.data;
fsize = minfo.size / sizeof (gfloat);
offset = -1;
duration = GST_BUFFER_DURATION (buffer);
if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
duration = GST_BUFFER_DURATION (buffer);
} else {
/* Cannot do a rounding-accurate duration calculation here because in the
* case when the duration is invalid, the pts might also be invalid */
duration = gst_util_uint64_scale_int_round (GST_SECOND, fsize / channels,
rate);
GST_LOG_OBJECT (pad, "buffer duration is invalid, calculated likely "
"duration as %" G_GINT64_FORMAT "us", duration / GST_USECOND);
}
/* Read only one channel */
for (ii = 1; ii < fsize; ii += channels) {
if (ABS (fdata[ii]) > 0.7) {
......@@ -416,8 +424,9 @@ gst_audiolatency_sink_chain (GstPad * pad, GstObject * parent,
latency = (self->recv_pts - self->send_pts);
gst_audiolatency_set_latency (self, latency);
GST_INFO ("recv pts: %" G_GINT64_FORMAT "us, latency: %" G_GINT64_FORMAT "ms",
self->recv_pts, latency / 1000);
GST_INFO ("recv pts: %" G_GINT64_FORMAT "us, latency: %" G_GINT64_FORMAT
"ms, offset: %" G_GINT64_FORMAT "ms", self->recv_pts, latency / 1000,
offset / 1000);
out:
gst_buffer_unref (buffer);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment