From bc0528214d001eb293d84b3de6227912e7d46cc4 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Thu, 23 Jan 2025 15:40:39 +0100 Subject: [PATCH] Include time in EOT message --- lib/asciinema/streaming/live_stream_server.ex | 7 +++++-- lib/asciinema_web/live_stream_consumer_socket.ex | 12 ++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/asciinema/streaming/live_stream_server.ex b/lib/asciinema/streaming/live_stream_server.ex index 74a8a561f..75d4e0f97 100644 --- a/lib/asciinema/streaming/live_stream_server.ex +++ b/lib/asciinema/streaming/live_stream_server.ex @@ -207,7 +207,8 @@ defmodule Asciinema.Streaming.LiveStreamServer do Logger.info("stream/#{state.stream_id}: terminating (#{inspect(reason)})") Logger.debug("stream/#{state.stream_id}: state: #{inspect(state)}") - publish(state.stream_id, :end) + time = current_stream_time(state.last_stream_time, state.last_event_time) || 0.0 + publish(state.stream_id, :end, %{time: time}) Streaming.update_live_stream(state.stream, online: false) :ok @@ -222,7 +223,7 @@ defmodule Asciinema.Streaming.LiveStreamServer do %{state | last_stream_time: time, last_event_time: Timex.now()} end - defp publish(stream_id, event, data \\ nil) do + defp publish(stream_id, event, data) do update = %Update{ stream_id: stream_id, event: event, @@ -271,6 +272,8 @@ defmodule Asciinema.Streaming.LiveStreamServer do %{state | shutdown_timer: timer} end + defp current_stream_time(nil, nil), do: nil + defp current_stream_time(last_stream_time, last_event_time) do last_stream_time + Timex.diff(Timex.now(), last_event_time, :microseconds) end diff --git a/lib/asciinema_web/live_stream_consumer_socket.ex b/lib/asciinema_web/live_stream_consumer_socket.ex index 7be8352e4..0f55cef35 100644 --- a/lib/asciinema_web/live_stream_consumer_socket.ex +++ b/lib/asciinema_web/live_stream_consumer_socket.ex @@ -128,8 +128,10 @@ defmodule AsciinemaWeb.LiveStreamConsumerSocket do {:reply, marker_message(time, label), state} end - def websocket_info(%LiveStreamServer.Update{event: :end}, state) do - {:reply, eot_message(), state} + def websocket_info(%LiveStreamServer.Update{event: :end} = update, state) do + %{time: time} = update.data + + {:reply, eot_message(time), state} end def websocket_info(:client_ping, state) do @@ -310,10 +312,12 @@ defmodule AsciinemaWeb.LiveStreamConsumerSocket do {:binary, msg} end - defp eot_message do + defp eot_message(time) do msg = << # message type: EOT - 0x04::8 + 0x04::8, + # current stream time + time::little-64 >> {:binary, msg}