Skip to content

Commit 36ac644

Browse files
committed
Propagate info about available layers
1 parent d750283 commit 36ac644

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

lib/live_ex_webrtc/player.ex

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ defmodule LiveExWebRTC.Player do
7777

7878
defstruct id: nil,
7979
publisher_id: nil,
80+
publisher_audio_track: nil,
81+
publisher_video_track: nil,
8082
pubsub: nil,
8183
pc: nil,
8284
audio_track_id: nil,
@@ -92,6 +94,7 @@ defmodule LiveExWebRTC.Player do
9294
munger: nil,
9395
layer: nil,
9496
target_layer: nil,
97+
video_layers: [{"h", "high"}],
9598
# codec that will be used for video sending
9699
video_send_codec: nil
97100

@@ -207,9 +210,7 @@ defmodule LiveExWebRTC.Player do
207210
<div class="">
208211
<video id={@player.id} phx-hook="Player" class={@class} controls autoplay muted></video>
209212
<select id="lexp-video-quality">
210-
<option value="h" selected>High</option>
211-
<option value="m">Medium</option>
212-
<option value="l">Low</option>
213+
<option :for={{id, layer} <- @publisher.video_layers} value={id}>{layer}</option>
213214
</select>
214215
</div>
215216
"""
@@ -227,6 +228,7 @@ defmodule LiveExWebRTC.Player do
227228
receive do
228229
{^ref, %Player{publisher_id: ^pub_id} = player} ->
229230
player = %Player{player | layer: "h", target_layer: "h"}
231+
PubSub.subscribe(player.pubsub, "streams:info:#{player.publisher_id}")
230232
assign(socket, player: player)
231233
after
232234
5000 -> exit(:timeout)
@@ -271,6 +273,41 @@ defmodule LiveExWebRTC.Player do
271273
{:noreply, socket}
272274
end
273275

276+
@impl true
277+
def handle_info({:live_ex_webrtc, :info, publisher_audio_track, publisher_video_track}, socket) do
278+
%{player: player} = socket.assigns
279+
280+
player =
281+
case player do
282+
%Player{publisher_audio_track: nil, publisher_video_track: nil} ->
283+
video_layers = (publisher_video_track && publisher_video_track.rids) || ["h"]
284+
285+
video_layers =
286+
Enum.map(video_layers, fn
287+
"h" -> {"h", "high"}
288+
"m" -> {"m", "medium"}
289+
"l" -> {"l", "low"}
290+
end)
291+
292+
%Player{
293+
player
294+
| publisher_audio_track: publisher_audio_track,
295+
publisher_video_track: publisher_video_track,
296+
video_layers: video_layers
297+
}
298+
299+
%Player{
300+
publisher_audio_track: ^publisher_audio_track,
301+
publisher_video_track: ^publisher_video_track
302+
} ->
303+
player
304+
end
305+
306+
socket = assign(socket, :player, player)
307+
308+
{:noreply, socket}
309+
end
310+
274311
def handle_info({:live_ex_webrtc, :audio, packet}, socket) do
275312
%{player: player} = socket.assigns
276313

lib/live_ex_webrtc/publisher.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,21 @@ defmodule LiveExWebRTC.Publisher do
520520
{:noreply, socket}
521521
end
522522

523+
@impl true
524+
def handle_info(:streams_info, socket) do
525+
%{publisher: publisher} = socket.assigns
526+
527+
PubSub.broadcast(
528+
publisher.pubsub,
529+
"streams:info:#{publisher.id}",
530+
{:live_ex_webrtc, :info, publisher.audio_track, publisher.video_track}
531+
)
532+
533+
Process.send_after(self(), :streams_info, 1_000)
534+
535+
{:noreply, socket}
536+
end
537+
523538
@impl true
524539
def handle_event("start-streaming", _, socket) do
525540
{:noreply,
@@ -564,6 +579,8 @@ defmodule LiveExWebRTC.Publisher do
564579
video_track: video_track
565580
}
566581

582+
Process.send_after(self(), :streams_info, 1_000)
583+
567584
{:noreply,
568585
socket
569586
|> assign(publisher: new_publisher)

0 commit comments

Comments
 (0)