Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 21 additions & 75 deletions rtp/lib/membrane_demo_rtp/receive_pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,92 +11,38 @@ defmodule Membrane.Demo.RTP.ReceivePipeline do
def handle_init(_ctx, opts) do
%{audio_port: audio_port, video_port: video_port, secure?: secure?, srtp_key: srtp_key} = opts

spec = [
child(:video_src, %UDP.Source{
local_port_no: video_port,
local_address: @local_ip
})
|> via_in(:rtp_input)
|> child(:rtp, %RTP.SessionBin{
secure?: secure?,
srtp_policies: [
%ExLibSRTP.Policy{
ssrc: :any_inbound,
key: srtp_key
}
],
fmt_mapping: %{
96 => {:H264, 90_000},
120 => {:OPUS, 48_000}
}
}),
child(:audio_src, %UDP.Source{
local_port_no: audio_port,
local_address: @local_ip
})
|> via_in(:rtp_input)
|> get_child(:rtp)
]
srtp =
if secure? do
[%ExLibSRTP.Policy{ssrc: :any_inbound, key: srtp_key}]
else
false
end

{[spec: spec], %{}}
end

@impl true
def handle_child_notification({:new_rtp_stream, ssrc, 96, _extensions}, :rtp, _ctx, state) do
state = Map.put(state, :video, ssrc)
actions = handle_stream(state)
{actions, state}
end

@impl true
def handle_child_notification({:new_rtp_stream, ssrc, 120, _extensions}, :rtp, _ctx, state) do
state = Map.put(state, :audio, ssrc)
actions = handle_stream(state)
{actions, state}
end

@impl true
def handle_child_notification(
{:new_rtp_stream, _ssrc, encoding_name, _extensions},
:rtp,
_ctx,
_state
) do
raise "Unsupported encoding: #{inspect(encoding_name)}"
end

@impl true
def handle_child_notification({:connection_info, @local_ip, _port}, :audio_src, _ctx, state) do
Logger.info("Audio UDP source connected.")
{[], state}
end

@impl true
def handle_child_notification({:connection_info, @local_ip, _port}, :video_src, _ctx, state) do
Logger.info("Video UDP source connected.")
{[], state}
end

defp handle_stream(%{audio: audio_ssrc, video: video_ssrc}) do
spec =
{[
get_child(:rtp)
|> via_out(Pad.ref(:output, video_ssrc), options: [depayloader: RTP.H264.Depayloader])
child(:video_src, %UDP.Source{
local_port_no: video_port,
local_address: @local_ip
})
|> child(:video_rtp_demuxer, %RTP.Demuxer{srtp: srtp})
|> via_out(:output, options: [stream_id: {:encoding_name, :H264}])
|> child(:video_depayloader, RTP.H264.Depayloader)
|> child(:video_parser, %H264.Parser{
generate_best_effort_timestamps: %{framerate: {30, 1}}
})
|> child(:video_decoder, H264.FFmpeg.Decoder)
|> child(:video_player, Membrane.SDL.Player),
get_child(:rtp)
|> via_out(Pad.ref(:output, audio_ssrc), options: [depayloader: RTP.Opus.Depayloader])
child(:audio_src, %UDP.Source{
local_port_no: audio_port,
local_address: @local_ip
})
|> child(:audio_rtp_demuxer, %RTP.Demuxer{srtp: srtp})
|> via_out(:output, options: [stream_id: {:encoding_name, :opus}])
|> child(:audio_depayloader, RTP.Opus.Depayloader)
|> child(:audio_decoder, Opus.Decoder)
|> child(:audio_player, Membrane.PortAudio.Sink)
], stream_sync: :sinks}

[spec: spec]
end

defp handle_stream(_state) do
[]
{[spec: spec], %{}}
end
end
27 changes: 11 additions & 16 deletions rtp/lib/membrane_demo_rtp/send_pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ defmodule Membrane.Demo.RTP.SendPipeline do
%{
audio_port: audio_port,
video_port: video_port,
audio_ssrc: audio_ssrc,
video_ssrc: video_ssrc,
secure?: secure?,
srtp_key: srtp_key
} = opts

srtp =
if secure? do
[%ExLibSRTP.Policy{ssrc: :any_inbound, key: srtp_key}]
else
false
end

spec = [
child(:video_src, %File.Source{
location: "samples/video.h264"
Expand All @@ -22,17 +27,8 @@ defmodule Membrane.Demo.RTP.SendPipeline do
generate_best_effort_timestamps: %{framerate: {30, 1}},
output_alignment: :nalu
})
|> via_in(Pad.ref(:input, video_ssrc), options: [payloader: RTP.H264.Payloader])
|> child(:rtp, %RTP.SessionBin{
secure?: secure?,
srtp_policies: [
%ExLibSRTP.Policy{
ssrc: :any_inbound,
key: srtp_key
}
]
})
|> via_out(Pad.ref(:rtp_output, video_ssrc), options: [encoding: :H264])
|> child(:video_payloader, RTP.H264.Payloader)
|> child(:video_rtp_muxer, %RTP.Muxer{srtp: srtp})
|> child(:video_realtimer, Membrane.Realtimer)
|> child(:video_sink, %UDP.Sink{
destination_port_no: video_port,
Expand All @@ -46,9 +42,8 @@ defmodule Membrane.Demo.RTP.SendPipeline do
delimitation: :undelimit,
generate_best_effort_timestamps?: true
})
|> via_in(Pad.ref(:input, audio_ssrc), options: [payloader: RTP.Opus.Payloader])
|> get_child(:rtp)
|> via_out(Pad.ref(:rtp_output, audio_ssrc), options: [encoding: :OPUS])
|> child(:audio_payloader, RTP.Opus.Payloader)
|> child(:audio_rtp_muxer, %RTP.Muxer{srtp: srtp})
|> child(:audio_realtimer, Membrane.Realtimer)
|> child(:audio_sink, %UDP.Sink{
destination_port_no: audio_port,
Expand Down
6 changes: 3 additions & 3 deletions rtp/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ defmodule Membrane.Demo.RTP.MixProject do
defp deps do
[
{:membrane_core, "~> 1.0"},
{:membrane_rtp_plugin, "~> 0.27.1"},
{:membrane_rtp_plugin, "~> 0.31.0"},
{:membrane_udp_plugin, "~> 0.13.0"},
{:membrane_h264_ffmpeg_plugin, "~> 0.31.6"},
{:membrane_h26x_plugin, "~> 0.10.1"},
{:membrane_rtp_h264_plugin, "~> 0.19.1"},
{:membrane_rtp_h264_plugin, "~> 0.20.1"},
{:membrane_opus_plugin, "~> 0.20.1"},
{:membrane_rtp_opus_plugin, "~> 0.9.0"},
{:membrane_rtp_opus_plugin, "~> 0.10.0"},
{:membrane_sdl_plugin, "~> 0.18.2"},
{:membrane_portaudio_plugin, "~> 0.19.2"},
{:membrane_file_plugin, "~> 0.17.0"},
Expand Down
Loading