diff --git a/lib/asciinema/streaming/live_stream.ex b/lib/asciinema/streaming/live_stream.ex index 8fe1e68cb..cdba8e977 100644 --- a/lib/asciinema/streaming/live_stream.ex +++ b/lib/asciinema/streaming/live_stream.ex @@ -23,6 +23,7 @@ defmodule Asciinema.Streaming.LiveStream do field :peak_viewer_count, :integer field :buffer_time, :float field :parser, :string + field :snapshot, Asciinema.Ecto.Type.JsonArray timestamps() diff --git a/lib/asciinema/streaming/live_stream_server.ex b/lib/asciinema/streaming/live_stream_server.ex index b7a6ff1f6..297fa7651 100644 --- a/lib/asciinema/streaming/live_stream_server.ex +++ b/lib/asciinema/streaming/live_stream_server.ex @@ -1,5 +1,6 @@ defmodule Asciinema.Streaming.LiveStreamServer do use GenServer, restart: :transient + alias Asciinema.Recordings.Snapshot alias Asciinema.Streaming.ViewerTracker alias Asciinema.{Colors, PubSub, Streaming, Vt} require Logger @@ -169,7 +170,8 @@ defmodule Asciinema.Streaming.LiveStreamServer do Streaming.update_live_stream(state.stream, current_viewer_count: state.viewer_count, cols: cols, - rows: rows + rows: rows, + snapshot: generate_snapshot(state.vt) ) nil -> @@ -265,4 +267,12 @@ defmodule Asciinema.Streaming.LiveStreamServer do theme_palette: Enum.join(Enum.slice(colors, 2..-1), ":") ] end + + defp generate_snapshot(vt) do + {:ok, {lines, cursor}} = Vt.dump_screen(vt) + + {lines, cursor} + |> Snapshot.new() + |> Snapshot.unwrap() + end end diff --git a/lib/asciinema_web/controllers/recording_svg.ex b/lib/asciinema_web/controllers/recording_svg.ex index ca8941136..ea7e782ef 100644 --- a/lib/asciinema_web/controllers/recording_svg.ex +++ b/lib/asciinema_web/controllers/recording_svg.ex @@ -422,6 +422,4 @@ defmodule AsciinemaWeb.RecordingSVG do |> Snapshot.new() |> Snapshot.crop(cols, rows) end - - defp snapshot(_, _), do: Snapshot.new([]) end diff --git a/priv/repo/migrations/20240421122804_add_snapshot_to_live_streams.exs b/priv/repo/migrations/20240421122804_add_snapshot_to_live_streams.exs new file mode 100644 index 000000000..30582d99a --- /dev/null +++ b/priv/repo/migrations/20240421122804_add_snapshot_to_live_streams.exs @@ -0,0 +1,9 @@ +defmodule Asciinema.Repo.Migrations.AddSnapshotToLiveStreams do + use Ecto.Migration + + def change do + alter table(:live_streams) do + add :snapshot, :text + end + end +end