@@ -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.
@@ -574,28 +574,31 @@ reader2 =
574574writer = Boombox .run (input: {:writer , video: :image , audio: false }, output: output)
575575
576576Stream .unfold (%{}, fn _state ->
577- {result1, packet1} = Boombox .read (reader1)
578- {result2, packet2} = Boombox .read (reader2)
577+ case {Boombox .read (reader1), Boombox .read (reader2)} do
578+ {:finished , :finished } ->
579+ nil
579580
580- joined_image =
581- Vix .Vips .Operation .join! (packet1.payload, packet2.payload, :VIPS_DIRECTION_HORIZONTAL )
582-
583- packet = %Boombox .Packet {
584- pts: max (packet1.pts, packet2.pts),
585- payload: joined_image,
586- kind: :video
587- }
588-
589- Boombox .write (writer, packet)
590-
591- if :finished in [result1, result2] do
592- if result1 == :ok , do:
581+ {{:ok , _packet }, :finished } ->
593582 Boombox .close (reader1)
594- if result2 == :ok , do:
583+ nil
584+
585+ {:finished , {:ok , _packet }} ->
595586 Boombox .close (reader2)
596- nil
597- else
598- {nil , %{}}
587+ nil
588+
589+ {{:ok , packet1}, {:ok , packet2}} ->
590+ joined_image =
591+ Vix .Vips .Operation .join! (packet1.payload, packet2.payload, :VIPS_DIRECTION_HORIZONTAL )
592+
593+ packet = %Boombox .Packet {
594+ pts: max (packet1.pts, packet2.pts),
595+ payload: joined_image,
596+ kind: :video
597+ }
598+
599+ Boombox .write (writer, packet)
600+
601+ {nil , %{}}
599602 end
600603end )
601604|> Stream .run ()
@@ -624,29 +627,35 @@ defmodule MyServer do
624627
625628 {:ok ,
626629 %{
627- input_boomboxes_states : %{
630+ bb_states : %{
628631 bb1: %{last_packet: nil , eos: false },
629632 bb2: %{last_packet: nil , eos: false }
630633 },
631- input_boomboxes : %{bb1 => :bb1 , bb2 => :bb2 },
634+ bbs : %{bb1 => :bb1 , bb2 => :bb2 },
632635 output_writer: output_writer
633636 }}
634637 end
635638
636639 @impl true
637- def handle_info ({:boombox_packet , bb, packet}, state) do
638- boombox_id = state.input_boomboxes [bb]
639- state = put_in (state.input_boomboxes_states [boombox_id].last_packet, packet)
640+ 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)
640643
641- if Enum .all? (Map .values (state.input_boomboxes_states ), & (&1 .last_packet != nil )) do
644+ if Enum .all? (Map .values (state.bb_states ), & (&1 .last_packet != nil )) do
642645 joined_image =
643646 Vix .Vips .Operation .join! (
644- state.input_boomboxes_states .bb1.last_packet.payload,
645- state.input_boomboxes_states .bb2.last_packet.payload,
647+ state.bb_states .bb1.last_packet.payload,
648+ state.bb_states .bb2.last_packet.payload,
646649 :VIPS_DIRECTION_HORIZONTAL
647650 )
648651
649- packet = %Boombox .Packet {packet | payload: joined_image}
652+ pts =
653+ max (
654+ state.bb_states.bb1.last_packet.pts,
655+ state.bb_states.bb2.last_packet.pts
656+ )
657+
658+ packet = %Boombox .Packet {packet | payload: joined_image, pts: pts}
650659
651660 Boombox .write (state.output_writer, packet)
652661 end
@@ -656,10 +665,10 @@ defmodule MyServer do
656665
657666 @impl true
658667 def handle_info ({:boombox_finished , bb}, state) do
659- boombox_id = state.input_boomboxes [bb]
660- state = put_in (state.input_boomboxes_states [boombox_id].eos, true )
668+ boombox_id = state.bbs [bb]
669+ state = put_in (state.bb_states [boombox_id].eos, true )
661670
662- if Enum .all? (Map .values (state.input_boomboxes_states ), & &1 .eos) do
671+ if Enum .all? (Map .values (state.bb_states ), & &1 .eos) do
663672 Boombox .close (state.output_writer)
664673 {:stop , :normal , state}
665674 else
@@ -672,7 +681,7 @@ input1 = "#{input_dir}/bun.mp4"
672681input2 = " #{ input_dir } /ffmpeg-testsrc.mp4"
673682output = " #{ out_dir } /index.m3u8"
674683
675- {:ok , server} = MyServer .start (%{input1: input , input2: input , output: output})
684+ {:ok , server} = MyServer .start (%{input1: input1 , input2: input2 , output: output})
676685monitor = Process .monitor (server)
677686
678687receive do
0 commit comments