@@ -11,7 +11,7 @@ System.put_env("PATH", "/opt/homebrew/bin:#{System.get_env("PATH")}")
1111# Examples that don't mention them should still work.
1212
1313# MIX_INSTALL_CONFIG_BEGIN
14- boombox = {:boombox , github: " membraneframework/boombox" }
14+ boombox = {:boombox , github: " membraneframework/boombox" , branch: " refactor-elixir-endpoints " }
1515
1616# This livebook uses boombox from the master branch. If any examples happen to not work, the latest stable version of this livebook
1717# can be found on https://hexdocs.pm/boombox/examples.html or in the latest github release.
@@ -24,7 +24,8 @@ Mix.install([
2424 :exla ,
2525 :bumblebee ,
2626 :websockex ,
27- :membrane_simple_rtsp_server
27+ :membrane_simple_rtsp_server ,
28+ {:coerce , " >= 1.0.2" }
2829])
2930
3031Nx .global_default_backend (EXLA .Backend )
@@ -573,19 +574,8 @@ reader2 =
573574
574575writer = Boombox .run (input: {:writer , video: :image , audio: false }, output: output)
575576
576- Stream .unfold (%{}, fn _state ->
577+ Stream .repeatedly ( fn ->
577578 case {Boombox .read (reader1), Boombox .read (reader2)} do
578- {:finished , :finished } ->
579- nil
580-
581- {{:ok , _packet }, :finished } ->
582- Boombox .close (reader1)
583- nil
584-
585- {:finished , {:ok , _packet }} ->
586- Boombox .close (reader2)
587- nil
588-
589579 {{:ok , packet1}, {:ok , packet2}} ->
590580 joined_image =
591581 Vix .Vips .Operation .join! (packet1.payload, packet2.payload, :VIPS_DIRECTION_HORIZONTAL )
@@ -598,12 +588,15 @@ Stream.unfold(%{}, fn _state ->
598588
599589 Boombox .write (writer, packet)
600590
601- {nil , %{}}
591+ _finished ->
592+ :eos
602593 end
603594end )
604- |> Stream . run ( )
595+ |> Enum . find ( & &1 == :eos )
605596
606597Boombox .close (writer)
598+ Boombox .close (reader1)
599+ Boombox .close (reader2)
607600```
608601
609602The second cell uses ` :message ` endpoints, meaning that the server communicates with boomboxes by
@@ -620,39 +613,39 @@ defmodule MyServer do
620613
621614 @impl true
622615 def init (args) do
623- bb1 = Boombox .run (input: args.input1, output: {:message , video: :image , audio: false })
624- bb2 = Boombox .run (input: args.input2, output: {:message , video: :image , audio: false })
616+ boombox1 = Boombox .run (input: args.input1, output: {:message , video: :image , audio: false })
617+ boombox2 = Boombox .run (input: args.input2, output: {:message , video: :image , audio: false })
625618 output_writer =
626619 Boombox .run (input: {:writer , video: :image , audio: false }, output: args.output)
627620
628621 {:ok ,
629622 %{
630- bb_states : %{
631- bb1 : %{last_packet: nil , eos: false },
632- bb2 : %{last_packet: nil , eos: false }
623+ boombox_states : %{
624+ boombox1 : %{last_packet: nil , eos: false },
625+ boombox2 : %{last_packet: nil , eos: false }
633626 },
634- bbs : %{bb1 => :bb1 , bb2 => :bb2 },
627+ boomboxes : %{boombox1 => :boombox1 , boombox2 => :boombox2 },
635628 output_writer: output_writer
636629 }}
637630 end
638631
639632 @impl true
640633 def handle_info ({:boombox_packet , bb, %Boombox .Packet {} = packet}, state) do
641- boombox_id = state.bbs [bb]
642- state = put_in (state.bb_states [boombox_id].last_packet, packet)
634+ boombox_id = state.boomboxes [bb]
635+ state = put_in (state.boombox_states [boombox_id].last_packet, packet)
643636
644- if Enum .all? (Map .values (state.bb_states ), & (&1 .last_packet != nil )) do
637+ if Enum .all? (Map .values (state.boombox_states ), & (&1 .last_packet != nil )) do
645638 joined_image =
646639 Vix .Vips .Operation .join! (
647- state.bb_states.bb1 .last_packet.payload,
648- state.bb_states.bb2 .last_packet.payload,
640+ state.boombox_states.boombox1 .last_packet.payload,
641+ state.boombox_states.boombox2 .last_packet.payload,
649642 :VIPS_DIRECTION_HORIZONTAL
650643 )
651644
652645 pts =
653646 max (
654- state.bb_states.bb1 .last_packet.pts,
655- state.bb_states.bb2 .last_packet.pts
647+ state.boombox_states.boombox1 .last_packet.pts,
648+ state.boombox_states.boombox2 .last_packet.pts
656649 )
657650
658651 packet = %Boombox .Packet {packet | payload: joined_image, pts: pts}
@@ -665,10 +658,10 @@ defmodule MyServer do
665658
666659 @impl true
667660 def handle_info ({:boombox_finished , bb}, state) do
668- boombox_id = state.bbs [bb]
669- state = put_in (state.bb_states [boombox_id].eos, true )
661+ boombox_id = state.boomboxes [bb]
662+ state = put_in (state.boombox_states [boombox_id].eos, true )
670663
671- if Enum .all? (Map .values (state.bb_states ), & &1 .eos) do
664+ if Enum .all? (Map .values (state.boombox_states ), & &1 .eos) do
672665 Boombox .close (state.output_writer)
673666 {:stop , :normal , state}
674667 else
@@ -686,7 +679,6 @@ monitor = Process.monitor(server)
686679
687680receive do
688681 {:DOWN , ^monitor , :process , ^server , reason} ->
689- IO .inspect (reason)
690682 :ok
691683end
692684```
0 commit comments