-
Notifications
You must be signed in to change notification settings - Fork 9
/
media.ex
233 lines (195 loc) · 7.41 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
defmodule ExSDP.Media do
@moduledoc """
This module represents the Media field of SDP.
For more details please see [RFC4566 Section 5.14](https://tools.ietf.org/html/rfc4566#section-5.14)
"""
use Bunch
use Bunch.Access
alias ExSDP.{
Attribute,
Bandwidth,
ConnectionData,
Encryption,
Utils
}
@enforce_keys [:type, :port, :protocol, :fmt]
defstruct @enforce_keys ++
[
:title,
:encryption,
port_count: 1,
connection_data: [],
bandwidth: [],
attributes: []
]
@type t :: %__MODULE__{
type: type(),
port: :inet.port_number(),
port_count: non_neg_integer(),
protocol: binary(),
fmt: binary() | [0..127],
title: binary() | nil,
connection_data: [ConnectionData.t()],
bandwidth: [Bandwidth.t()],
encryption: Encryption.t() | nil,
attributes: [Attribute.t()]
}
@typedoc """
Represents type of media. In [RFC4566](https://tools.ietf.org/html/rfc4566#section-5.14)
there are defined "audio", "video", "text", "application", and "message" types.
Known types are represented as atoms others are binaries.
"""
@type type :: :audio | :video | :text | :application | :message | binary()
@spec new(type(), :inet.port_number(), binary(), binary() | 0..127 | [0..127],
port_count: non_neg_integer()
) :: t()
def new(type, port, protocol, fmt, opts \\ []) do
%__MODULE__{
type: type,
port: port,
port_count: opts[:port_count] || 1,
protocol: protocol,
fmt: Bunch.listify(fmt)
}
end
@deprecated "Use ExSDP.add_attribute/2 instead"
@spec add_attribute(t(), Attribute.t()) :: t()
def add_attribute(media, attribute), do: add_attributes(media, [attribute])
@deprecated "Use ExSDP.add_attributes/2 instead"
@spec add_attributes(t(), [Attribute.t()]) :: t()
def add_attributes(media, attributes) when is_list(attributes),
do: Map.update!(media, :attributes, &(&1 ++ attributes))
@deprecated "Use ExSDP.get_attribute/2 instead"
@spec get_attribute(t(), Attribute.key()) :: Attribute.t() | nil
def get_attribute(media, key), do: Utils.get_attribute(media, key)
@deprecated "Use ExSDP.get_attributes/2 instead"
@spec get_attributes(t(), Attribute.key()) :: [Attribute.t()]
def get_attributes(media, key), do: Utils.get_attributes(media, key)
@spec parse(binary()) :: {:ok, t()} | {:error, :invalid_media_spec | :malformed_port_number}
def parse(media) do
withl conn: [type, port, proto, fmt] <- String.split(media, " ", parts: 4),
port: {port, port_count} when port in 0..65_535 <- Integer.parse(port),
port_count: port_count when port_count > 0 <- parse_port_count(port_count),
fmt: {:ok, fmt} <- parse_fmt(fmt, proto) do
media = %__MODULE__{
type: parse_type(type),
port: port,
port_count: port_count,
protocol: proto,
fmt: fmt
}
{:ok, media}
else
conn: _ -> {:error, :invalid_media_spec}
port: _ -> {:error, :invalid_port_number}
port_count: _ -> {:error, :invalid_port_count}
fmt: error -> error
end
end
@spec parse_optional([binary()], t()) :: {:ok, {[binary()], t()}} | {:error, atom()}
def parse_optional(lines, media)
def parse_optional([""], media), do: {:ok, {[""], finalize_optional_parsing(media)}}
def parse_optional(["" | rest], media),
do: parse_optional(rest, media)
def parse_optional(["m=" <> _ | _] = lines, media),
do: {:ok, {lines, finalize_optional_parsing(media)}}
def parse_optional(["i=" <> title | rest], media),
do: parse_optional(rest, %__MODULE__{media | title: title})
def parse_optional(["c=" <> conn | rest], media) do
with {:ok, %ConnectionData{} = connection_data} <- ConnectionData.parse(conn) do
connection_data = media.connection_data ++ [connection_data]
connection_data = %__MODULE__{media | connection_data: connection_data}
parse_optional(rest, connection_data)
end
end
def parse_optional(["b=" <> bandwidth | rest], %__MODULE__{bandwidth: acc_bandwidth} = media) do
with {:ok, bandwidth} <- Bandwidth.parse(bandwidth) do
bandwidth = %__MODULE__{media | bandwidth: [bandwidth | acc_bandwidth]}
parse_optional(rest, bandwidth)
end
end
def parse_optional(["k=" <> encryption | rest], media) do
with {:ok, encryption} <- Encryption.parse(encryption) do
encryption = %__MODULE__{media | encryption: encryption}
parse_optional(rest, encryption)
end
end
def parse_optional(["a=" <> attribute | rest], %__MODULE__{attributes: attrs} = media) do
with {:ok, attribute} <- Attribute.parse(attribute, media_type: media.type) do
media = %__MODULE__{media | attributes: [attribute | attrs]}
parse_optional(rest, media)
end
end
@spec apply_session(__MODULE__.t(), ExSDP.t()) :: __MODULE__.t()
def apply_session(media, session) do
session
|> Map.from_struct()
|> Enum.reduce(Map.from_struct(media), fn
{inherited_key, value}, acc
when inherited_key == :encryption ->
if acc[inherited_key] != nil,
do: acc,
else: Map.put(acc, inherited_key, value)
{inherited_key, value}, acc when inherited_key in [:connection_data, :bandwidth] ->
if acc[inherited_key] != [],
do: acc,
else: Map.put(acc, inherited_key, value)
_key_value, acc ->
acc
end)
~> struct(__MODULE__, &1)
end
defp finalize_optional_parsing(%__MODULE__{attributes: attrs} = media) do
%__MODULE__{media | attributes: Enum.reverse(attrs)}
end
defp parse_type(type) when type in ["audio", "video", "text", "application", "message"],
do: String.to_atom(type)
defp parse_type(type) when is_binary(type), do: type
defp parse_fmt(fmt, proto) when proto in ["RTP/AVP", "RTP/SAVP", "UDP/TLS/RTP/SAVPF"] do
fmt
|> String.split(" ")
|> Bunch.Enum.try_map(fn single_fmt ->
case Integer.parse(single_fmt) do
{parsed_fmt, ""} -> {:ok, parsed_fmt}
_invalid_fmt -> {:error, :invalid_fmt}
end
end)
end
defp parse_fmt(fmt, _proto), do: {:ok, fmt}
defp parse_port_count(""), do: 1
defp parse_port_count("/" <> port_count) do
case Integer.parse(port_count) do
{port_count, ""} -> port_count
_not_matched -> :error
end
end
end
defimpl String.Chars, for: ExSDP.Media do
@impl true
def to_string(media) do
import ExSDP.Sigil
serialized_header = media |> header_fields |> String.trim()
~n"""
#{serialized_header}
#{ExSDP.Serializer.maybe_serialize("i", media.title)}
#{ExSDP.Serializer.maybe_serialize("c", media.connection_data)}
#{ExSDP.Serializer.maybe_serialize("b", media.bandwidth)}
#{ExSDP.Serializer.maybe_serialize("k", media.encryption)}
#{ExSDP.Serializer.maybe_serialize("a", media.attributes)}
"""
# to_string has to return string without `\r\n` at the end
|> String.trim()
end
defp header_fields(media) do
"""
#{media.type} \
#{serialize_port(media.port, media.port_count)} \
#{media.protocol} \
#{serialize_fmt(media.fmt)} \
"""
end
defp serialize_port(port, 1), do: "#{port}"
defp serialize_port(port, port_count), do: "#{port}/#{port_count}"
defp serialize_fmt(fmt) when is_binary(fmt), do: fmt
defp serialize_fmt(fmt), do: Enum.map_join(fmt, " ", &Kernel.to_string/1)
end