<div dir="ltr">looks good, ack<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Apr 30, 2013 at 8:24 PM, Yonit Halperin <span dir="ltr"><<a href="mailto:yhalperi@redhat.com" target="_blank">yhalperi@redhat.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">rhbz#951664<br>
<br>
When the src and dst servers have different mm-times, we can<br>
hit a case when for a short period of time the session mm-time and<br>
the video mm-time are not synced. If the video mm-time is much<br>
bigger than the session mm-time, the next stream rendering will be<br>
scheduled to (video-mm-time - session-mm-time), and even after<br>
the different mm-times are synced, the video won't be rendered<br>
till the rendering timeout that was scheduled based on a wrong mm-time,<br>
takes place.<br>
This patch protects from such cases. You can find more details in the<br>
code comments.<br>
---<br>
 gtk/channel-display.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++-<br>
 1 file changed, 107 insertions(+), 1 deletion(-)<br>
<br>
diff --git a/gtk/channel-display.c b/gtk/channel-display.c<br>
index 9e03727..27ae0cb 100644<br>
--- a/gtk/channel-display.c<br>
+++ b/gtk/channel-display.c<br>
@@ -116,6 +116,8 @@ static gboolean display_stream_render(display_stream *st);<br>
 static void spice_display_channel_reset(SpiceChannel *channel, gboolean migrating);<br>
 static void spice_display_channel_reset_capabilities(SpiceChannel *channel);<br>
 static void destroy_canvas(display_surface *surface);<br>
+static void _msg_in_unref_func(gpointer data, gpointer user_data);<br>
+static void display_session_mm_time_reset_cb(SpiceSession *session, gpointer data);<br>
<br>
 /* ------------------------------------------------------------------ */<br>
<br>
@@ -157,6 +159,10 @@ static void spice_display_channel_constructed(GObject *object)<br>
     g_return_if_fail(c->palettes != NULL);<br>
<br>
     c->monitors = g_array_new(FALSE, TRUE, sizeof(SpiceDisplayMonitorConfig));<br>
+    spice_g_signal_connect_object(s, "mm-time-reset",<br>
+                                  G_CALLBACK(display_session_mm_time_reset_cb),<br>
+                                  SPICE_CHANNEL(object), 0);<br>
+<br>
<br>
     if (G_OBJECT_CLASS(spice_display_channel_parent_class)->constructed)<br>
         G_OBJECT_CLASS(spice_display_channel_parent_class)->constructed(object);<br>
@@ -1089,6 +1095,8 @@ static gboolean display_stream_schedule(display_stream *st)<br>
     guint32 time, d;<br>
     SpiceStreamDataHeader *op;<br>
     SpiceMsgIn *in;<br>
+<br>
+    SPICE_DEBUG("%s", __FUNCTION__);<br>
     if (st->timeout)<br>
         return TRUE;<br>
<br>
@@ -1103,6 +1111,9 @@ static gboolean display_stream_schedule(display_stream *st)<br>
         st->timeout = g_timeout_add(d, (GSourceFunc)display_stream_render, st);<br>
         return TRUE;<br>
     } else {<br>
+        SPICE_DEBUG("%s: rendering too late by %u ms (ts: %u, mmtime: %u), dropping ",<br>
+                    __FUNCTION__, time - op->multi_media_time,<br>
+                    op->multi_media_time, time);<br>
         in = g_queue_pop_head(st->msgq);<br>
         spice_msg_in_unref(in);<br>
         st->num_drops_on_playback++;<br>
@@ -1303,7 +1314,100 @@ static void display_update_stream_report(SpiceDisplayChannel *channel, uint32_t<br>
     }<br>
 }<br>
<br>
+static void display_stream_reset_rendering_timer(display_stream *st)<br>
+{<br>
+    SPICE_DEBUG("%s", __FUNCTION__);<br>
+    if (st->timeout != 0) {<br>
+        g_source_remove(st->timeout);<br>
+        st->timeout = 0;<br>
+    }<br>
+    while (!display_stream_schedule(st)) {<br>
+    }<br>
+}<br>
+<br>
+/*<br>
+ * Migration can occur between 2 spice-servers with different mm-times.<br>
+ * Then, the following cases can happen after migration completes:<br>
+ * (We refer to src/dst-time as the mm-times on the src/dst servers):<br>
+ *<br>
+ * (case 1) Frames with time ~= dst-time arrive to the client before the<br>
+ *          playback-channel updates the session's mm-time (i.e., the mm_time<br>
+ *          of the session is still based on the src-time).<br>
+ *     (a) If src-time < dst-time:<br>
+ *         display_stream_schedule schedules the next rendering to<br>
+ *         ~(dst-time - src-time) milliseconds from now.<br>
+ *         Since we assume monotonic mm_time, display_stream_schedule,<br>
+ *         returns immediately when a rendering timeout<br>
+ *         has already been set, and doesn't update the timeout,<br>
+ *         even after the mm_time is updated.<br>
+ *         When src-time << dst-time, a significant video frames loss will occur.<br>
+ *     (b) If src-time > dst-time<br>
+ *         Frames will be dropped till the mm-time will be updated.<br>
+ * (case 2) mm-time is synced with dst-time, but frames that were in the command<br>
+ *         ring during migration still arrive (such frames hold src-time).<br>
+ *    (a) If src-time < dst-time<br>
+ *        The frames that hold src-time will be dropped, since their<br>
+ *        mm_time < session-mm_time. But all the new frames that are generated in<br>
+ *        the driver after migration, will be rendered appropriately.<br>
+ *    (b) If src-time > dst-time<br>
+ *        Similar consequences as in 1 (a)<br>
+ * case 2 is less likely, since at takes at least 20 frames till the dst-server re-identifies<br>
+ * the video stream and starts sending stream data<br>
+ *<br>
+ * display_session_mm_time_reset_cb handles case 1.a, and<br>
+ * display_stream_test_frames_mm_time_reset handles case 2.b<br>
+ */<br>
+<br>
+static void display_session_mm_time_reset_cb(SpiceSession *session, gpointer data)<br>
+{<br>
+    SpiceChannel *channel = data;<br>
+    SpiceDisplayChannelPrivate *c = SPICE_DISPLAY_CHANNEL(channel)->priv;<br>
+    guint i;<br>
+<br>
+    CHANNEL_DEBUG(channel, "%s", __FUNCTION__);<br>
+<br>
+    for (i = 0; i < c->nstreams; i++) {<br>
+        display_stream *st;<br>
+<br>
+        if (c->streams[i] == NULL) {<br>
+            continue;<br>
+        }<br>
+        SPICE_DEBUG("%s: stream-id %d", __FUNCTION__, i);<br>
+        st = c->streams[i];<br>
+        display_stream_reset_rendering_timer(st);<br>
+    }<br>
+}<br>
+<br>
+static void display_stream_test_frames_mm_time_reset(display_stream *st,<br>
+                                                     SpiceMsgIn *new_frame_msg,<br>
+                                                     guint32 mm_time)<br>
+{<br>
+    SpiceStreamDataHeader *tail_op, *new_op;<br>
+    SpiceMsgIn *tail_msg;<br>
+<br>
+    SPICE_DEBUG("%s", __FUNCTION__);<br>
+    g_return_if_fail(new_frame_msg != NULL);<br>
+    tail_msg = g_queue_peek_tail(st->msgq);<br>
+    if (!tail_msg) {<br>
+        return;<br>
+    }<br>
+    tail_op = spice_msg_in_parsed(tail_msg);<br>
+    new_op = spice_msg_in_parsed(new_frame_msg);<br>
+<br>
+    if (new_op->multi_media_time < tail_op->multi_media_time) {<br>
+        SPICE_DEBUG("new-frame-time < tail-frame-time (%u < %u):"<br>
+                    " reseting stream, id %d",<br>
+                    new_op->multi_media_time,<br>
+                    tail_op->multi_media_time,<br>
+                    new_op->id);<br>
+        g_queue_foreach(st->msgq, _msg_in_unref_func, NULL);<br>
+        g_queue_clear(st->msgq);<br>
+        display_stream_reset_rendering_timer(st);<br>
+    }<br>
+}<br>
+<br>
 #define STREAM_PLAYBACK_SYNC_DROP_SEQ_LEN_LIMIT 5<br>
+<br>
 /* coroutine context */<br>
 static void display_handle_stream_data(SpiceChannel *channel, SpiceMsgIn *in)<br>
 {<br>
@@ -1349,8 +1453,10 @@ static void display_handle_stream_data(SpiceChannel *channel, SpiceMsgIn *in)<br>
     } else {<br>
         CHANNEL_DEBUG(channel, "video latency: %d", latency);<br>
         spice_msg_in_ref(in);<br>
+        display_stream_test_frames_mm_time_reset(st, in, mmtime);<br>
         g_queue_push_tail(st->msgq, in);<br>
-        display_stream_schedule(st);<br>
+        while (!display_stream_schedule(st)) {<br>
+        }<br>
         if (st->cur_drops_seq_stats.len) {<br>
             st->cur_drops_seq_stats.duration = op->multi_media_time -<br>
                                                st->cur_drops_seq_stats.start_mm_time;<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.8.1.4<br>
<br>
_______________________________________________<br>
Spice-devel mailing list<br>
<a href="mailto:Spice-devel@lists.freedesktop.org">Spice-devel@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/spice-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/spice-devel</a><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br>Marc-André Lureau
</div>