From 565727fd6c722c65c1e5d07df28c539235bed38e Mon Sep 17 00:00:00 2001 From: Billal GHILAS Date: Sat, 10 Jan 2026 10:54:55 +0100 Subject: [PATCH] Improve message serialization --- lib/ex_rtmp/message.ex | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/ex_rtmp/message.ex b/lib/ex_rtmp/message.ex index ddfd7ce..75b7dfc 100644 --- a/lib/ex_rtmp/message.ex +++ b/lib/ex_rtmp/message.ex @@ -142,7 +142,7 @@ defmodule ExRTMP.Message do * `:chunk_size` - The size of each chunk (default: 128) * `:chunk_stream_id` - The chunk stream id to use (default: 2) """ - @spec serialize(t(), keyword()) :: iodata() + @spec serialize(t(), keyword()) :: binary() def serialize(message, opts \\ []) do chunk_size = Keyword.get(opts, :chunk_size, 128) chunk_stream_id = Keyword.get(opts, :chunk_stream_id, 2) @@ -166,14 +166,20 @@ defmodule ExRTMP.Message do } 2..entries//1 - |> Stream.map(fn idx -> + |> Enum.map(fn idx -> offset = (idx - 1) * chunk_size size = min(byte_size(payload) - offset, chunk_size) binary_part(payload, offset, size) end) - |> Stream.map(&%Chunk{payload: &1, stream_id: chunk_stream_id, fmt: 3}) - |> Enum.map(&Chunk.serialize/1) - |> then(&[Chunk.serialize(first_chunk) | &1]) + |> chunks_to_binary(chunk_stream_id, Chunk.serialize(first_chunk)) + end + + defp chunks_to_binary([], _chunk_stream_id, acc), do: acc + + # we always use stream id smaller than 63 since we only allow + # one stream per client/session + defp chunks_to_binary([chunk | rest], stream_id, acc) do + chunks_to_binary(rest, stream_id, <>) end defp parse_payload(%__MODULE__{type: 1, payload: payload} = msg) do