Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Noarkhh committed Jul 3, 2024
1 parent 5778c96 commit b792e52
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions lib/membrane_vpx/encoder/vpx_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,48 +44,18 @@ defmodule Membrane.VPx.Encoder do

@spec handle_stream_format(:input, RawVideo.t(), CallbackContext.t(), State.t()) ::
callback_return()
def handle_stream_format(
:input,
raw_video_format,
ctx,
%State{codec_module: codec_module} = state
) do
def handle_stream_format(:input, stream_format, ctx, %State{codec_module: codec_module} = state) do
%RawVideo{
width: width,
height: height,
framerate: framerate,
pixel_format: pixel_format
} = raw_video_format
framerate: framerate
} = stream_format

output_stream_format =
struct(codec_module, width: width, height: height, framerate: framerate)

encoding_deadline =
case {state.encoding_deadline, framerate} do
{:auto, nil} -> @default_encoding_deadline |> Membrane.Time.as_microseconds(:round)
{:auto, {num, denom}} -> div(denom * 1_000_000, num)
{fixed_deadline, _framerate} -> fixed_deadline |> Membrane.Time.as_microseconds(:round)
end

{buffers, encoder_ref} =
case ctx.pads.input.stream_format do
^raw_video_format ->
{[], state.encoder_ref}

nil ->
encoder_ref =
Native.create!(state.codec, width, height, pixel_format, encoding_deadline)

{[], encoder_ref}

_changed_format ->
buffers = flush(state.encoder_ref)

encoder_ref =
Native.create!(state.codec, width, height, pixel_format, encoding_deadline)

{buffers, encoder_ref}
end
maybe_recreate_encoder(ctx.pads.input.stream_format, stream_format, state)

{[buffer: {:output, buffers}, stream_format: {:output, output_stream_format}],
%{state | encoder_ref: encoder_ref}}
Expand All @@ -109,6 +79,39 @@ defmodule Membrane.VPx.Encoder do
{[buffer: {:output, buffers}, end_of_stream: :output], state}
end

@spec maybe_recreate_encoder(
previous_stream_format :: RawVideo.t(),
new_stream_format :: RawVideo.t(),
State.t()
) :: {flushed_buffers :: [Buffer.t()], encoder_ref :: reference()}
defp maybe_recreate_encoder(unchanged_stream_format, unchanged_stream_format, state) do
{[], state.encoder_ref}
end

defp maybe_recreate_encoder(_previous_stream_format, new_stream_format, state) do
%RawVideo{
width: width,
height: height,
framerate: framerate,
pixel_format: pixel_format
} = new_stream_format

encoding_deadline =
case {state.encoding_deadline, framerate} do
{:auto, nil} -> @default_encoding_deadline |> Membrane.Time.as_microseconds(:round)
{:auto, {num, denom}} -> div(denom * 1_000_000, num)
{fixed_deadline, _framerate} -> fixed_deadline |> Membrane.Time.as_microseconds(:round)
end

new_encoder_ref =
Native.create!(state.codec, width, height, pixel_format, encoding_deadline)

case state.encoder_ref do
nil -> {[], new_encoder_ref}
old_encoder_ref -> {flush(old_encoder_ref), new_encoder_ref}
end
end

@spec flush(reference()) :: [Membrane.Buffer.t()]
defp flush(encoder_ref) do
{:ok, encoded_frames, timestamps} = Native.flush(encoder_ref)
Expand Down

0 comments on commit b792e52

Please sign in to comment.