From 8648e0688b5cfabff785745241fc41865cb78401 Mon Sep 17 00:00:00 2001 From: Jakub Pryc <94321002+Noarkhh@users.noreply.github.com> Date: Mon, 18 Mar 2024 12:21:12 +0100 Subject: [PATCH] Stop ignoring timestamps (#34) --- README.md | 2 +- lib/membrane_sdl/player.ex | 34 ++++++++++++++++++++-------------- mix.exs | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ee80a38..b7a8c07 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Add the following line to your `deps` in `mix.exs`. Run `mix deps.get`. ```elixir def deps do [ - {:membrane_sdl_plugin, "~> 0.18.1"} + {:membrane_sdl_plugin, "~> 0.18.2"} ] end ``` diff --git a/lib/membrane_sdl/player.ex b/lib/membrane_sdl/player.ex index d1a5663..7829834 100644 --- a/lib/membrane_sdl/player.ex +++ b/lib/membrane_sdl/player.ex @@ -20,8 +20,7 @@ defmodule Membrane.SDL.Player do @impl true def handle_init(_options, _ctx) do - state = %{cnode: nil, timer_started?: false} - {[latency: @latency], state} + {[latency: @latency], %{cnode: nil, last_pts: nil, last_payload: nil}} end @impl true @@ -32,9 +31,8 @@ defmodule Membrane.SDL.Player do end @impl true - def handle_stream_format(:input, stream_format, ctx, state) do + def handle_stream_format(:input, stream_format, ctx, %{cnode: cnode} = state) do %{input: input} = ctx.pads - %{cnode: cnode} = state if !input.stream_format || stream_format == input.stream_format do :ok = CNode.call(cnode, :create, [stream_format.width, stream_format.height]) @@ -45,23 +43,31 @@ defmodule Membrane.SDL.Player do end @impl true - def handle_start_of_stream(:input, ctx, state) do - use Numbers, overload_operators: true - {nom, denom} = ctx.pads.input.stream_format.framerate - timer = {:demand_timer, Ratio.new(Time.seconds(denom), nom)} - - {[demand: :input, start_timer: timer], %{state | timer_started?: true}} + def handle_start_of_stream(:input, _ctx, state) do + {[demand: :input, start_timer: {:demand_timer, :no_interval}], state} end @impl true - def handle_buffer(:input, %Buffer{payload: payload}, _ctx, state) do + def handle_buffer(:input, %Buffer{payload: payload, pts: pts}, _ctx, state) do payload = Membrane.Payload.to_binary(payload) - :ok = CNode.call(state.cnode, :display_frame, [payload]) - {[], state} + + actions = + case state do + %{last_pts: nil, last_payload: nil} -> + :ok = CNode.call(state.cnode, :display_frame, [payload]) + + [demand: :input] + + %{last_pts: last_pts} -> + [timer_interval: {:demand_timer, pts - last_pts}] + end + + {actions, %{state | last_pts: pts, last_payload: payload}} end @impl true def handle_tick(:demand_timer, _ctx, state) do - {[demand: :input], state} + :ok = CNode.call(state.cnode, :display_frame, [state.last_payload]) + {[timer_interval: {:demand_timer, :no_interval}, demand: :input], state} end end diff --git a/mix.exs b/mix.exs index 4e0ee1b..63ab4f4 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Membrane.SDL.Plugin.MixProject do use Mix.Project - @version "0.18.1" + @version "0.18.2" @github_url "https://github.com/membraneframework/membrane_sdl_plugin" def project do