@@ -55,11 +55,19 @@ defmodule Membrane.RTMP.Sink do
5555 ] ,
5656 reset_timestamps: [
5757 spec: boolean ( ) ,
58- default: true ,
58+ default: false ,
5959 description: """
6060 If enabled, this feature adjusts the timing of outgoing FLV packets so that they begin from zero.
6161 Leaves it untouched otherwise.
6262 """
63+ ] ,
64+ connect_in_advance: [
65+ spec: boolean ( ) ,
66+ default: true ,
67+ description: """
68+ If enabled, the Sink will connect to the server during the element setup.
69+ Otherwise, the connection will be made when stream data is already available in the element.
70+ """
6371 ]
6472
6573 @ impl true
@@ -105,23 +113,41 @@ defmodule Membrane.RTMP.Sink do
105113 end
106114
107115 @ impl true
108- def handle_setup ( _ctx , state ) do
109- audio? = :audio in state . tracks
110- video? = :video in state . tracks
111-
112- { :ok , native } = Native . create ( state . rtmp_url , audio? , video? )
116+ def handle_setup ( _ctx , % { connect_in_advance: true } = state ) do
117+ initialize_native ( state )
118+ end
113119
114- state
115- |> Map . put ( :native , native )
116- |> try_connect ( )
117- |> then ( & { [ ] , & 1 } )
120+ @ impl true
121+ def handle_setup ( _ctx , state ) do
122+ { [ ] , state }
118123 end
119124
120125 @ impl true
121126 def handle_playing ( _ctx , state ) do
122127 { build_demand ( state ) , state }
123128 end
124129
130+ @ impl true
131+ def handle_start_of_stream ( _pad , ctx , % { connect_in_advance: false } = state ) do
132+ all_pads_connected? =
133+ Enum . all? ( state . tracks , fn track ->
134+ Enum . any? ( ctx . pads , fn
135+ { Pad . ref ( ^ track , _ref ) , pad } ->
136+ pad . start_of_stream?
137+
138+ _other ->
139+ false
140+ end )
141+ end )
142+
143+ if all_pads_connected? , do: initialize_native ( state ) , else: { [ ] , state }
144+ end
145+
146+ @ impl true
147+ def handle_start_of_stream ( _pad , _ctx , state ) do
148+ { [ ] , state }
149+ end
150+
125151 @ impl true
126152 def handle_pad_added ( Pad . ref ( _type , stream_id ) , _ctx , _state ) when stream_id != 0 ,
127153 do: raise ( ArgumentError , message: "Stream id must always be 0" )
@@ -348,4 +374,16 @@ defmodule Membrane.RTMP.Sink do
348374 { base_dts , state } = Bunch.Map . get_updated! ( state , :video_base_dts , & ( & 1 || dts ) )
349375 { { dts - base_dts , pts - base_dts } , state }
350376 end
377+
378+ defp initialize_native ( state ) do
379+ audio? = :audio in state . tracks
380+ video? = :video in state . tracks
381+
382+ { :ok , native } = Native . create ( state . rtmp_url , audio? , video? )
383+
384+ state
385+ |> Map . put ( :native , native )
386+ |> try_connect ( )
387+ |> then ( & { [ ] , & 1 } )
388+ end
351389end
0 commit comments