Skip to content

Commit 2c2cc2d

Browse files
authored
Merge pull request #64 from membraneframework/bump-webrtc-plugin
Fix bouncing logo example, add appropriate test
2 parents 0dae52a + a376c5a commit 2c2cc2d

File tree

8 files changed

+77
-31
lines changed

8 files changed

+77
-31
lines changed

examples.livemd

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ System.put_env("PATH", "/opt/homebrew/bin:#{System.get_env("PATH")}")
1212

1313
# MIX_INSTALL_CONFIG_BEGIN
1414
boombox = {:boombox, github: "membraneframework/boombox"}
15+
1516
# This livebook uses boombox from the master branch. If any examples happen to not work, the latest stable version of this livebook
1617
# can be found on https://hexdocs.pm/boombox/examples.html or in the latest github release.
1718
# MIX_INSTALL_CONFIG_END
@@ -90,7 +91,10 @@ To send the stream, visit http://localhost:1234/whip.html.
9091
Note: don't stop this cell to finish recording - click 'disconnect' or close the browser tab instead, so the recording is finalized properly.
9192

9293
```elixir
93-
Boombox.run(input: {:whip, "http://localhost:8829", token: "whip_it!"}, output: "#{out_dir}/webrtc_to_mp4.mp4")
94+
Boombox.run(
95+
input: {:whip, "http://localhost:8829", token: "whip_it!"},
96+
output: "#{out_dir}/webrtc_to_mp4.mp4"
97+
)
9498
```
9599

96100
```elixir
@@ -509,7 +513,7 @@ System.shell("ffplay #{out_dir}/rtmp_to_mp4.mp4")
509513
## Stream MP4 via WebRTC, receive it and record to MP4 again
510514

511515
```elixir
512-
signaling = Membrane.WebRTC.SignalingChannel.new()
516+
signaling = Membrane.WebRTC.Signaling.new()
513517

514518
t =
515519
Task.async(fn ->

lib/boombox.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Boombox do
88

99
alias Membrane.RTP
1010

11-
@type webrtc_signaling :: Membrane.WebRTC.SignalingChannel.t() | String.t()
11+
@type webrtc_signaling :: Membrane.WebRTC.Signaling.t() | String.t()
1212
@type in_stream_opts :: [
1313
{:audio, :binary | boolean()}
1414
| {:video, :image | boolean()}
@@ -186,7 +186,7 @@ defmodule Boombox do
186186
{:mp4, location} when is_binary(location) and direction == :output ->
187187
{:mp4, location}
188188

189-
{:webrtc, %Membrane.WebRTC.SignalingChannel{}} ->
189+
{:webrtc, %Membrane.WebRTC.Signaling{}} ->
190190
value
191191

192192
{:webrtc, uri} when is_binary(uri) ->

lib/boombox/webrtc.ex

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ defmodule Boombox.WebRTC do
8989
if webrtc_input?(state) do
9090
# let's spawn websocket server for webrtc source before the source starts
9191
{:webrtc, input_signaling} = state.input
92-
signaling_channel = resolve_signaling(input_signaling, :input, ctx.utility_supervisor)
93-
state = %{state | input: {:webrtc, signaling_channel}}
92+
signaling = resolve_signaling(input_signaling, :input, ctx.utility_supervisor)
93+
state = %{state | input: {:webrtc, signaling}}
9494

9595
{%Wait{actions: [spec: spec]}, state}
9696
else
@@ -188,7 +188,7 @@ defmodule Boombox.WebRTC do
188188
end
189189

190190
defp resolve_signaling(
191-
%WebRTC.SignalingChannel{} = signaling,
191+
%WebRTC.Signaling{} = signaling,
192192
_direction,
193193
_utility_supervisor
194194
) do
@@ -202,7 +202,7 @@ defmodule Boombox.WebRTC do
202202
end
203203

204204
defp resolve_signaling({:whip, uri, opts}, :output, utility_supervisor) do
205-
signaling = WebRTC.SignalingChannel.new()
205+
signaling = WebRTC.Signaling.new()
206206

207207
Membrane.UtilitySupervisor.start_link_child(
208208
utility_supervisor,
@@ -221,7 +221,7 @@ defmodule Boombox.WebRTC do
221221
end
222222

223223
defp setup_whip_server(opts, utility_supervisor) do
224-
signaling = WebRTC.SignalingChannel.new()
224+
signaling = WebRTC.Signaling.new()
225225
clients_cnt = :atomics.new(1, [])
226226
{valid_token, opts} = Keyword.pop(opts, :token)
227227

mix.exs

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ defmodule Boombox.Mixfile do
4848
[
4949
{:membrane_core, "~> 1.1"},
5050
{:membrane_transcoder_plugin, "~> 0.1.2"},
51-
# {:membrane_webrtc_plugin, "~> 0.23.2"},
52-
{:membrane_webrtc_plugin, github: "membraneframework/membrane_webrtc_plugin"},
51+
{:membrane_webrtc_plugin, "~> 0.24.0"},
5352
{:membrane_mp4_plugin, "~> 0.35.2"},
5453
{:membrane_realtimer_plugin, "~> 0.9.0"},
5554
{:membrane_http_adaptive_stream_plugin, "~> 0.18.5"},

mix.lock

+11-11
Large diffs are not rendered by default.

test/boombox_test.exs

+9-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ defmodule BoomboxTest do
6666
@tag :file_webrtc
6767
async_test "mp4 file -> webrtc -> mp4 file", %{tmp_dir: tmp} do
6868
output = Path.join(tmp, "output.mp4")
69-
signaling = Membrane.WebRTC.SignalingChannel.new()
69+
signaling = Membrane.WebRTC.Signaling.new()
7070
t = Task.async(fn -> Boombox.run(input: @bbb_mp4, output: {:webrtc, signaling}) end)
7171
Boombox.run(input: {:webrtc, signaling}, output: output)
7272
Task.await(t)
@@ -90,7 +90,7 @@ defmodule BoomboxTest do
9090
@tag :http_webrtc
9191
async_test "http mp4 -> webrtc -> mp4 file", %{tmp_dir: tmp} do
9292
output = Path.join(tmp, "output.mp4")
93-
signaling = Membrane.WebRTC.SignalingChannel.new()
93+
signaling = Membrane.WebRTC.Signaling.new()
9494
t = Task.async(fn -> Boombox.run(input: @bbb_mp4_url, output: {:webrtc, signaling}) end)
9595
Boombox.run(input: {:webrtc, signaling}, output: output)
9696
Task.await(t)
@@ -100,7 +100,7 @@ defmodule BoomboxTest do
100100
@tag :webrtc_audio
101101
async_test "mp4 -> webrtc -> mp4 audio", %{tmp_dir: tmp} do
102102
output = Path.join(tmp, "output.mp4")
103-
signaling = Membrane.WebRTC.SignalingChannel.new()
103+
signaling = Membrane.WebRTC.Signaling.new()
104104

105105
t =
106106
Task.async(fn -> Boombox.run(input: @bbb_mp4_a, output: {:webrtc, signaling}) end)
@@ -113,7 +113,7 @@ defmodule BoomboxTest do
113113
@tag :webrtc_video
114114
async_test "mp4 -> webrtc -> mp4 video", %{tmp_dir: tmp} do
115115
output = Path.join(tmp, "output.mp4")
116-
signaling = Membrane.WebRTC.SignalingChannel.new()
116+
signaling = Membrane.WebRTC.Signaling.new()
117117

118118
t =
119119
Task.async(fn -> Boombox.run(input: @bbb_mp4_v, output: {:webrtc, signaling}) end)
@@ -126,8 +126,8 @@ defmodule BoomboxTest do
126126
@tag :webrtc2
127127
async_test "mp4 -> webrtc -> webrtc -> mp4", %{tmp_dir: tmp} do
128128
output = Path.join(tmp, "output.mp4")
129-
signaling1 = Membrane.WebRTC.SignalingChannel.new()
130-
signaling2 = Membrane.WebRTC.SignalingChannel.new()
129+
signaling1 = Membrane.WebRTC.Signaling.new()
130+
signaling2 = Membrane.WebRTC.Signaling.new()
131131

132132
t1 =
133133
Task.async(fn -> Boombox.run(input: @bbb_mp4, output: {:webrtc, signaling1}) end)
@@ -198,7 +198,7 @@ defmodule BoomboxTest do
198198
async_test "rtmp -> webrtc -> mp4", %{tmp_dir: tmp} do
199199
output = Path.join(tmp, "output.mp4")
200200
url = "rtmp://localhost:5002/app/stream_key"
201-
signaling = Membrane.WebRTC.SignalingChannel.new()
201+
signaling = Membrane.WebRTC.Signaling.new()
202202

203203
t1 =
204204
Task.async(fn -> Boombox.run(input: url, output: {:webrtc, signaling}) end)
@@ -279,7 +279,7 @@ defmodule BoomboxTest do
279279
async_test "rtsp -> webrtc -> mp4", %{tmp_dir: tmp} do
280280
rtsp_port = 8556
281281
output = Path.join(tmp, "output.mp4")
282-
signaling = Membrane.WebRTC.SignalingChannel.new()
282+
signaling = Membrane.WebRTC.Signaling.new()
283283

284284
Membrane.SimpleRTSPServer.start_link(@bbb_mp4, port: rtsp_port)
285285

@@ -314,7 +314,7 @@ defmodule BoomboxTest do
314314

315315
@tag :bouncing_bubble_webrtc_mp4
316316
async_test "bouncing bubble -> webrtc -> mp4", %{tmp_dir: tmp} do
317-
signaling = Membrane.WebRTC.SignalingChannel.new()
317+
signaling = Membrane.WebRTC.Signaling.new()
318318

319319
Task.async(fn ->
320320
overlay =

test/browser_test.exs

+43
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,49 @@ defmodule Boombox.BrowserTest do
150150
end
151151
end
152152

153+
test "boombox -> browser (bouncing logo)", %{browser: browser} do
154+
overlay =
155+
__DIR__
156+
|> Path.join("fixtures/logo.png")
157+
|> Image.open!()
158+
159+
bg = Image.new!(640, 480, color: :light_gray)
160+
max_x = Image.width(bg) - Image.width(overlay)
161+
max_y = Image.height(bg) - Image.height(overlay)
162+
163+
stream =
164+
Stream.iterate({_x = 300, _y = 0, _dx = 1, _dy = 2, _pts = 0}, fn {x, y, dx, dy, pts} ->
165+
dx = if (x + dx) in 0..max_x, do: dx, else: -dx
166+
dy = if (y + dy) in 0..max_y, do: dy, else: -dy
167+
pts = pts + div(Membrane.Time.seconds(1), _fps = 60)
168+
{x + dx, y + dy, dx, dy, pts}
169+
end)
170+
|> Stream.map(fn {x, y, _dx, _dy, pts} ->
171+
img = Image.compose!(bg, overlay, x: x, y: y)
172+
%Boombox.Packet{kind: :video, payload: img, pts: pts}
173+
end)
174+
175+
boombox_task =
176+
Task.async(fn ->
177+
stream
178+
|> Boombox.run(
179+
input: {:stream, video: :image, audio: false},
180+
output: {:webrtc, "ws://localhost:8830"}
181+
)
182+
end)
183+
184+
page = start_page(browser, "webrtc_to_browser")
185+
186+
seconds = 10
187+
Process.sleep(seconds * 1000)
188+
189+
assert_page_connected(page)
190+
assert_frames_decoded(page, seconds)
191+
192+
close_page(page)
193+
Task.shutdown(boombox_task)
194+
end
195+
153196
defp start_page(browser, page) do
154197
url = "http://localhost:#{@port}/#{page}.html"
155198
do_start_page(browser, url)

test/fixtures/logo.png

8.65 KB
Loading

0 commit comments

Comments
 (0)