From 21de4aa9079771340d4200562af192cd6b4e2ce7 Mon Sep 17 00:00:00 2001 From: noarkhh Date: Mon, 4 Mar 2024 15:42:00 +0100 Subject: [PATCH 1/2] Stop ignoring timestamps --- README.md | 2 +- lib/membrane_sdl/player.ex | 38 +++++++++++++++++++++----------------- mix.exs | 2 +- 3 files changed, 23 insertions(+), 19 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..357d369 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,11 +31,8 @@ defmodule Membrane.SDL.Player do end @impl true - def handle_stream_format(:input, stream_format, ctx, state) do - %{input: input} = ctx.pads - %{cnode: cnode} = state - - if !input.stream_format || stream_format == input.stream_format do + def handle_stream_format(:input, stream_format, ctx, %{cnode: cnode} = state) do + if !ctx.pads.input.stream_format || stream_format == ctx.pads.input.stream_format do :ok = CNode.call(cnode, :create, [stream_format.width, stream_format.height]) {[], state} else @@ -45,23 +41,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} -> + [demand: :input] + + %{last_pts: last_pts, last_payload: last_payload} -> + :ok = CNode.call(state.cnode, :display_frame, [last_payload]) + + [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 From 2b1b0f11bb548e612fd7feb0fff34ff6db12c933 Mon Sep 17 00:00:00 2001 From: noarkhh Date: Wed, 6 Mar 2024 11:39:58 +0100 Subject: [PATCH 2/2] Apply reviewers suggestions --- lib/membrane_sdl/player.ex | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/membrane_sdl/player.ex b/lib/membrane_sdl/player.ex index 357d369..7829834 100644 --- a/lib/membrane_sdl/player.ex +++ b/lib/membrane_sdl/player.ex @@ -32,7 +32,9 @@ defmodule Membrane.SDL.Player do @impl true def handle_stream_format(:input, stream_format, ctx, %{cnode: cnode} = state) do - if !ctx.pads.input.stream_format || stream_format == ctx.pads.input.stream_format do + %{input: input} = ctx.pads + + if !input.stream_format || stream_format == input.stream_format do :ok = CNode.call(cnode, :create, [stream_format.width, stream_format.height]) {[], state} else @@ -52,11 +54,11 @@ defmodule Membrane.SDL.Player do actions = case state do %{last_pts: nil, last_payload: nil} -> - [demand: :input] + :ok = CNode.call(state.cnode, :display_frame, [payload]) - %{last_pts: last_pts, last_payload: last_payload} -> - :ok = CNode.call(state.cnode, :display_frame, [last_payload]) + [demand: :input] + %{last_pts: last_pts} -> [timer_interval: {:demand_timer, pts - last_pts}] end