queue2: Avoid races when posting buffering messages

When posting a buffering message succesfully:
* Remember the *actual* percentage value that was posted
* Make sure we only reset the percent_changed variable if the value we just
  posted is indeed different from the current value

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/632>
This commit is contained in:
Edward Hervey 2020-06-04 11:21:45 +02:00 committed by Tim-Philipp Müller
parent fa8ea66418
commit b935ac6642

View file

@ -311,7 +311,8 @@ static gboolean gst_queue2_is_filled (GstQueue2 * queue);
static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
static void update_in_rates (GstQueue2 * queue, gboolean force);
static GstMessage *gst_queue2_get_buffering_message (GstQueue2 * queue);
static GstMessage *gst_queue2_get_buffering_message (GstQueue2 * queue,
gint * percent);
static void gst_queue2_post_buffering (GstQueue2 * queue);
typedef enum
@ -1124,7 +1125,7 @@ get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
/* Called with the lock taken */
static GstMessage *
gst_queue2_get_buffering_message (GstQueue2 * queue)
gst_queue2_get_buffering_message (GstQueue2 * queue, gint * percent)
{
GstMessage *msg = NULL;
if (queue->percent_changed) {
@ -1141,10 +1142,10 @@ gst_queue2_get_buffering_message (GstQueue2 * queue)
* the queue becomes empty for a short period of time. */
if (!queue->waiting_del
&& queue->last_posted_buffering_percent != queue->buffering_percent) {
gint percent = queue->buffering_percent;
*percent = queue->buffering_percent;
GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", *percent);
msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), *percent);
gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
queue->avg_out, queue->buffering_left);
@ -1158,10 +1159,11 @@ static void
gst_queue2_post_buffering (GstQueue2 * queue)
{
GstMessage *msg = NULL;
gint percent = -1;
g_mutex_lock (&queue->buffering_post_lock);
GST_QUEUE2_MUTEX_LOCK (queue);
msg = gst_queue2_get_buffering_message (queue);
msg = gst_queue2_get_buffering_message (queue, &percent);
GST_QUEUE2_MUTEX_UNLOCK (queue);
if (msg != NULL) {
@ -1171,10 +1173,12 @@ gst_queue2_post_buffering (GstQueue2 * queue)
* this post attempt failed, and the next one won't be done, because
* gst_queue2_get_buffering_message() checks these states and decides
* based on their values that it won't produce a message. */
queue->last_posted_buffering_percent = queue->percent_changed;
queue->percent_changed = FALSE;
queue->last_posted_buffering_percent = percent;
if (percent == queue->buffering_percent)
queue->percent_changed = FALSE;
GST_QUEUE2_MUTEX_UNLOCK (queue);
GST_DEBUG_OBJECT (queue, "successfully posted buffering message");
GST_DEBUG_OBJECT (queue, "successfully posted %d%% buffering message",
percent);
} else
GST_DEBUG_OBJECT (queue, "could not post buffering message");
}
@ -2193,8 +2197,9 @@ gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
/* update the buffering status */
if (queue->use_buffering) {
GstMessage *msg;
gint percent = -1;
update_buffering (queue);
msg = gst_queue2_get_buffering_message (queue);
msg = gst_queue2_get_buffering_message (queue, &percent);
if (msg) {
gboolean post_ok;
@ -2210,9 +2215,11 @@ gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
* this post attempt failed, and the next one won't be done, because
* gst_queue2_get_buffering_message() checks these states and decides
* based on their values that it won't produce a message. */
queue->last_posted_buffering_percent = queue->percent_changed;
queue->percent_changed = FALSE;
GST_DEBUG_OBJECT (queue, "successfully posted buffering message");
queue->last_posted_buffering_percent = percent;
if (percent == queue->buffering_percent)
queue->percent_changed = FALSE;
GST_DEBUG_OBJECT (queue, "successfully posted %d%% buffering message",
percent);
} else {
GST_DEBUG_OBJECT (queue, "could not post buffering message");
}