-
Notifications
You must be signed in to change notification settings - Fork 11
/
file_endpoint_test.exs
182 lines (150 loc) · 4.46 KB
/
file_endpoint_test.exs
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
defmodule Membrane.RTC.FileEndpointTest do
use ExUnit.Case
alias ExSDP.Attribute.FMTP
alias Membrane.RTC.Engine
alias Membrane.RTC.Engine.Endpoint
alias Membrane.RTC.Engine.Message
alias Membrane.RTC.Engine.Support
@fixtures_dir "./test/fixtures/"
setup do
options = [id: "test_rtc"]
{:ok, pid} = Engine.start_link(options, [])
Engine.register(pid, self())
on_exit(fn -> Engine.terminate(pid) end)
[rtc_engine: pid]
end
@source_endpoint_id "source-endpoint-id"
@sink_endpoint_id "sink-endpoint-id"
@out_opus_reference "out_audio.opus"
describe "File Endpoint test" do
for mode <- [:autoplay, :manual, :wait_for_first_subscriber] do
@tag :tmp_dir
test "test audio with #{mode}", %{rtc_engine: rtc_engine, tmp_dir: tmp_dir} do
test_endpoint(
type: :audio,
rtc_engine: rtc_engine,
tmp_dir: tmp_dir,
playback_mode: unquote(mode),
reference_path: @out_opus_reference
)
end
@tag :tmp_dir
test "test video with #{mode}", %{rtc_engine: rtc_engine, tmp_dir: tmp_dir} do
test_endpoint(
type: :video,
rtc_engine: rtc_engine,
tmp_dir: tmp_dir,
playback_mode: unquote(mode)
)
end
end
end
defp test_endpoint(opts) do
file_name = get_filename(opts[:type])
file_path = Path.join(@fixtures_dir, file_name)
reference_path =
if Keyword.has_key?(opts, :reference_path) do
Path.join(@fixtures_dir, opts[:reference_path])
else
file_path
end
output_file = Path.join([opts[:tmp_dir], "out_#{file_name}"])
rtc_engine = opts[:rtc_engine]
:ok =
Engine.add_endpoint(
rtc_engine,
%Support.Sink{
rtc_engine: rtc_engine,
file_path: output_file
},
id: @sink_endpoint_id
)
file_endpoint =
create_file_endpoint(
opts[:type],
rtc_engine,
file_path,
opts[:playback_mode]
)
:ok = Engine.add_endpoint(rtc_engine, file_endpoint, id: @source_endpoint_id)
assert_receive %Message.EndpointMessage{
endpoint_id: @source_endpoint_id,
message: :tracks_added
}
assert_receive %Message.EndpointMessage{
message: :tracks_subscribed
}
if opts[:playback_mode] == :manual do
Endpoint.File.start_sending(rtc_engine, @source_endpoint_id)
end
assert_receive %Message.EndpointRemoved{endpoint_id: @source_endpoint_id}, 25_000
assert_receive %Message.EndpointRemoved{endpoint_id: @sink_endpoint_id}, 25_000
refute_received %Message.EndpointCrashed{endpoint_id: @sink_endpoint_id}
refute_received %Message.EndpointCrashed{endpoint_id: @source_endpoint_id}
assert File.exists?(output_file)
output = File.read!(output_file)
reference = File.read!(reference_path)
output_size = output |> byte_size()
reference_size = reference |> byte_size()
if opts[:playback_mode] == :wait_for_first_subscriber do
assert output_size == reference_size
assert File.read!(output_file) == File.read!(reference_path)
else
assert output_size > 0
assert output == binary_slice(reference, reference_size - output_size, reference_size)
end
end
defp get_filename(:video), do: "video.h264"
defp get_filename(:audio), do: "audio.ogg"
defp create_file_endpoint(
:video,
rtc_engine,
video_file_path,
playback_mode
) do
video_track_config = %Endpoint.File.TrackConfig{
type: :video,
encoding: :H264,
clock_rate: 90_000,
fmtp: %FMTP{
pt: 96
},
opts: [framerate: {60, 1}]
}
%Endpoint.File{
rtc_engine: rtc_engine,
file_path: video_file_path,
track_config: video_track_config,
payload_type: 96,
playback_mode: playback_mode
}
end
defp create_file_endpoint(
:audio,
rtc_engine,
audio_file_path,
playback_mode
) do
ext = String.split(audio_file_path, ".") |> List.last()
encoding =
case ext do
"aac" -> :AAC
"ogg" -> :OPUS
end
audio_track_config = %Endpoint.File.TrackConfig{
type: :audio,
encoding: encoding,
clock_rate: 48_000,
fmtp: %FMTP{
pt: 108
}
}
%Endpoint.File{
rtc_engine: rtc_engine,
file_path: audio_file_path,
track_config: audio_track_config,
payload_type: 108,
playback_mode: playback_mode
}
end
end