generated from membraneframework/membrane_template_plugin
-
Notifications
You must be signed in to change notification settings - Fork 8
/
on_expect_additional_media.ex
66 lines (52 loc) · 1.69 KB
/
on_expect_additional_media.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
defmodule Membrane.RTMP.Messages.OnExpectAdditionalMedia do
@moduledoc """
Defines the RTMP `onExpectAdditionalMedia` command that is related to Twitch's RTMP additional
media track message.
The command is usually a part of `@setDataFrame` command but for more convencience it is
extracted.
"""
@behaviour Membrane.RTMP.Message
alias Membrane.RTMP.AMF0.Encoder
@attributes_to_keys %{
"additionalMedia" => :additional_media,
"defaultMedia" => :default_media,
"processingIntents" => :processing_intents
}
@keys_to_attributes Map.new(@attributes_to_keys, fn {key, value} -> {value, key} end)
defstruct Map.keys(@keys_to_attributes)
@type t :: %__MODULE__{
additional_media: map(),
default_media: map(),
processing_intents: [String.t()]
}
@impl true
def from_data(["@setDataFrame", "onExpectAdditionalMedia", properties]) do
new(properties)
end
@spec new([{String.t(), any()}]) :: t()
def new(options) do
params =
options
|> Map.new()
|> Map.take(Map.keys(@attributes_to_keys))
|> Enum.map(fn {key, value} ->
{Map.fetch!(@attributes_to_keys, key), value}
end)
struct!(__MODULE__, params)
end
# helper for message serialization
@doc false
@spec to_map(t()) :: map()
def to_map(%__MODULE__{} = message) do
Map.new(message, fn {key, value} -> {Map.fetch!(@keys_to_attributes, key), value} end)
end
defimpl Membrane.RTMP.Messages.Serializer do
require Membrane.RTMP.Header
@impl true
def serialize(%@for{} = message) do
Encoder.encode([@for.to_map(message)])
end
@impl true
def type(%@for{}), do: Membrane.RTMP.Header.type(:amf_data)
end
end