|
| 1 | +defmodule Membrane.RTMP.Messages.OnMetaData do |
| 2 | + @moduledoc """ |
| 3 | + Defines the RTMP `onMetaData` command (sent by nginx client). |
| 4 | + """ |
| 5 | + |
| 6 | + @behaviour Membrane.RTMP.Message |
| 7 | + |
| 8 | + alias Membrane.RTMP.AMF.Encoder |
| 9 | + |
| 10 | + @attributes_to_keys %{ |
| 11 | + "duration" => :duration, |
| 12 | + "width" => :width, |
| 13 | + "height" => :height, |
| 14 | + "videocodecid" => :video_codec_id, |
| 15 | + "videodatarate" => :video_data_rate, |
| 16 | + "framerate" => :framerate, |
| 17 | + "audiocodecid" => :audio_codec_id, |
| 18 | + "audiodatarate" => :audio_data_rate |
| 19 | + } |
| 20 | + |
| 21 | + @keys_to_attributes Map.new(@attributes_to_keys, fn {key, value} -> {value, key} end) |
| 22 | + |
| 23 | + defstruct Map.keys(@keys_to_attributes) |
| 24 | + |
| 25 | + @type t :: %__MODULE__{ |
| 26 | + duration: number(), |
| 27 | + # video related |
| 28 | + width: number(), |
| 29 | + height: number(), |
| 30 | + video_codec_id: number(), |
| 31 | + video_data_rate: number(), |
| 32 | + framerate: number(), |
| 33 | + # audio related |
| 34 | + audio_codec_id: number(), |
| 35 | + audio_data_rate: number() |
| 36 | + } |
| 37 | + |
| 38 | + @impl true |
| 39 | + def from_data(["onMetaData", properties]) do |
| 40 | + new(properties) |
| 41 | + end |
| 42 | + |
| 43 | + @spec new([{String.t(), any()}]) :: t() |
| 44 | + def new(options) do |
| 45 | + params = |
| 46 | + options |
| 47 | + |> Map.new() |
| 48 | + |> Map.take(Map.keys(@attributes_to_keys)) |
| 49 | + |> Enum.map(fn {key, value} -> |
| 50 | + {Map.fetch!(@attributes_to_keys, key), value} |
| 51 | + end) |
| 52 | + |
| 53 | + struct!(__MODULE__, params) |
| 54 | + end |
| 55 | + |
| 56 | + # helper for message serialization |
| 57 | + @doc false |
| 58 | + @spec to_map(t()) :: map() |
| 59 | + def to_map(%__MODULE__{} = message) do |
| 60 | + Map.new(message, fn {key, value} -> {Map.fetch!(@keys_to_attributes, key), value} end) |
| 61 | + end |
| 62 | + |
| 63 | + defimpl Membrane.RTMP.Messages.Serializer do |
| 64 | + require Membrane.RTMP.Header |
| 65 | + |
| 66 | + @impl true |
| 67 | + def serialize(%@for{} = message) do |
| 68 | + Encoder.encode([@for.to_map(message)]) |
| 69 | + end |
| 70 | + |
| 71 | + @impl true |
| 72 | + def type(%@for{}), do: Membrane.RTMP.Header.type(:amf_data) |
| 73 | + end |
| 74 | +end |
0 commit comments