Skip to content

Commit f5a1fb4

Browse files
committed
Write and fix tests
1 parent bc8c0cd commit f5a1fb4

File tree

7 files changed

+82
-69
lines changed

7 files changed

+82
-69
lines changed

lib/membrane_http_adaptive_stream/hls_source.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ defmodule Membrane.HLS.Source do
6464
],
6565
buffered_stream_time: [
6666
spec: Membrane.Time.t(),
67-
default: Membrane.Time.seconds(1),
67+
default: Membrane.Time.seconds(10),
6868
inspector: &Membrane.Time.inspect/1,
6969
description: """
7070
Amount of time of stream, that will be buffered by #{inspect(__MODULE__)}.
@@ -122,7 +122,7 @@ defmodule Membrane.HLS.Source do
122122
{[audio_stream_format], [video_stream_format]} =
123123
ClientGenServer.get_tracks_info(state.client_genserver)
124124
|> Map.values()
125-
|> Enum.split_with(&is_audio_stream_format/1)
125+
|> Enum.split_with(&audio_stream_format?/1)
126126

127127
actions = [
128128
stream_format: {:audio_output, audio_stream_format},
@@ -133,7 +133,7 @@ defmodule Membrane.HLS.Source do
133133
{actions, state}
134134
end
135135

136-
defp is_audio_stream_format(stream_format) do
136+
defp audio_stream_format?(stream_format) do
137137
case stream_format do
138138
%RemoteStream{content_format: AAC} -> true
139139
%RemoteStream{content_format: H264} -> false

lib/membrane_http_adaptive_stream/hls_source/client_genserver.ex

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,7 @@ defmodule Membrane.HLS.Source.ClientGenServer do
5656
def handle_info(:setup, state) do
5757
state =
5858
%{state | client: Client.new(state.url)}
59-
60-
# |> choose_variant()
61-
62-
variants = Client.get_variants(state.client)
63-
64-
state =
65-
if variants != %{} do
66-
get_resolution_fn = fn {_id, %{resolution: {width, height}}} -> width * height end
67-
get_bandwidth_fn = fn {_id, %{bandwidth: bandwidth}} -> bandwidth end
68-
69-
chosen_variant_id =
70-
case state.variant_selection_policy do
71-
:lowest_resolution ->
72-
variants |> Enum.min_by(get_resolution_fn) |> elem(0)
73-
74-
:highest_resolution ->
75-
variants |> Enum.max_by(get_resolution_fn) |> elem(0)
76-
77-
:lowest_bandwidth ->
78-
variants |> Enum.min_by(get_bandwidth_fn) |> elem(0)
79-
80-
:highest_bandwidth ->
81-
variants |> Enum.max_by(get_bandwidth_fn) |> elem(0)
82-
83-
custom_policy when is_function(custom_policy, 1) ->
84-
variants |> custom_policy.()
85-
end
86-
87-
client = state.client |> Client.choose_variant(chosen_variant_id)
88-
%{state | client: client}
89-
else
90-
state
91-
end
59+
|> choose_variant()
9260

9361
{:noreply, state}
9462
end
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/membrane_http_adaptive_stream/integration_test/hls_source_test.exs

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,102 @@ defmodule Membrane.HLS.Source.Test do
22
use ExUnit.Case, async: true
33

44
import Membrane.ChildrenSpec
5-
import Membrane.Testing.Assertions
6-
75
alias Membrane.Testing
86

97
@mpegts_url "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8"
108
@fmp4_url "https://raw.githubusercontent.com/membraneframework-labs/ex_hls/refs/heads/plug-demuxing-engine-into-client/fixture/output.m3u8"
119

12-
@tag :a
13-
test "fmp4" do
14-
spec = [
15-
child(:hls_source, %Membrane.HLS.Source{url: @fmp4_url})
16-
|> via_out(:video_output)
17-
|> child(%Membrane.Transcoder{output_stream_format: Membrane.RawVideo})
18-
|> child(Membrane.SDL.Player),
19-
get_child(:hls_source)
20-
|> via_out(:audio_output)
21-
|> child(%Membrane.Transcoder{output_stream_format: Membrane.RawAudio})
22-
|> child(Membrane.PortAudio.Sink)
23-
]
10+
@hls_source_fixtures "test/membrane_http_adaptive_stream/integration_test/fixtures/hls_source"
11+
@fmp4_video_fixture Path.join(@hls_source_fixtures, "fmp4/video.h264")
12+
@fmp4_audio_fixture Path.join(@hls_source_fixtures, "fmp4/audio.aac")
13+
@mpeg_ts_video_fixture Path.join(@hls_source_fixtures, "mpeg_ts/video.h264")
14+
@mpeg_ts_audio_fixture Path.join(@hls_source_fixtures, "mpeg_ts/audio.aac")
15+
16+
describe "Membrane.HLS.Source demuxes audio and video from HLS stream" do
17+
@tag :tmp_dir
18+
test "(fMP4)", %{tmp_dir: tmp_dir} do
19+
audio_result_file = Path.join(tmp_dir, "audio.aac")
20+
video_result_file = Path.join(tmp_dir, "video.h264")
21+
22+
spec =
23+
pipeline_spec(
24+
@fmp4_url,
25+
%Membrane.Transcoder{output_stream_format: Membrane.AAC},
26+
audio_result_file,
27+
video_result_file
28+
)
29+
30+
pipeline = Testing.Pipeline.start_link_supervised!(spec: spec)
31+
Process.sleep(10_000)
32+
Testing.Pipeline.terminate(pipeline)
2433

25-
pipeline = Testing.Pipeline.start_link_supervised!(spec: spec)
34+
# fixtures created locally with a quite good internet connection have
35+
# - 139_085 bytes for audio
36+
# - 500_571 bytes for video
37+
assert_track(audio_result_file, @fmp4_audio_fixture, 70_000)
38+
assert_track(video_result_file, @fmp4_video_fixture, 200_000)
39+
end
2640

27-
Process.sleep(10_000)
28-
Testing.Pipeline.terminate(pipeline)
41+
@tag :tmp_dir
42+
test "(MPEG-TS)", %{tmp_dir: tmp_dir} do
43+
audio_result_file = Path.join(tmp_dir, "audio.aac")
44+
video_result_file = Path.join(tmp_dir, "video.h264")
45+
46+
spec =
47+
pipeline_spec(
48+
@mpegts_url,
49+
%Membrane.Transcoder{
50+
assumed_input_stream_format: %Membrane.AAC{
51+
encapsulation: :ADTS
52+
},
53+
output_stream_format: Membrane.AAC
54+
},
55+
audio_result_file,
56+
video_result_file
57+
)
58+
59+
pipeline = Testing.Pipeline.start_link_supervised!(spec: spec)
60+
Process.sleep(10_000)
61+
Testing.Pipeline.terminate(pipeline)
62+
63+
# fixtures created locally with a quite good internet connection have
64+
# - 78_732 bytes for audio
65+
# - 136_754 bytes for video
66+
assert_track(audio_result_file, @mpeg_ts_audio_fixture, 40_000)
67+
assert_track(video_result_file, @mpeg_ts_video_fixture, 70_000)
68+
end
2969
end
3070

31-
@tag :b
32-
test "mpeg-ts" do
33-
spec = [
34-
child(:hls_source, %Membrane.HLS.Source{url: @mpegts_url})
71+
defp pipeline_spec(url, audio_transcoder, audio_result_file, video_result_file) do
72+
[
73+
child(:hls_source, %Membrane.HLS.Source{
74+
url: url,
75+
variant_selection_policy: :lowest_resolution
76+
})
3577
|> via_out(:video_output)
3678
|> child(%Membrane.Transcoder{
37-
output_stream_format: Membrane.RawVideo
79+
output_stream_format: Membrane.H264
3880
})
3981
|> child(Membrane.Realtimer)
40-
|> child(Membrane.SDL.Player),
82+
|> child(%Membrane.File.Sink{
83+
location: video_result_file
84+
}),
4185
get_child(:hls_source)
4286
|> via_out(:audio_output)
43-
|> child(%Membrane.Transcoder{
44-
assumed_input_stream_format: %Membrane.AAC{
45-
encapsulation: :ADTS
46-
},
47-
output_stream_format: Membrane.RawAudio
48-
})
87+
|> child(audio_transcoder)
4988
|> child(Membrane.Realtimer)
50-
|> child(Membrane.PortAudio.Sink)
89+
|> child(%Membrane.File.Sink{
90+
location: audio_result_file
91+
})
5192
]
93+
end
5294

53-
pipeline = Testing.Pipeline.start_link_supervised!(spec: spec)
95+
defp assert_track(result_file, fixture, asserted_bytes) do
96+
<<expected_prefix::binary-size(asserted_bytes), _sufix::binary>> =
97+
fixture |> File.read!()
5498

55-
Process.sleep(10_000)
56-
Testing.Pipeline.terminate(pipeline)
99+
assert result_file
100+
|> File.read!()
101+
|> String.starts_with?(expected_prefix)
57102
end
58103
end

0 commit comments

Comments
 (0)