-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update the rtsp_to_hls demo #283
Open
Noarkhh
wants to merge
3
commits into
master
Choose a base branch
from
update-rtsp-to-hls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if we should put the server code in the |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
defmodule Membrane.Demo.RTSPToHLS.Server.Handler do | ||
@moduledoc false | ||
|
||
use Membrane.RTSP.Server.Handler | ||
|
||
require Membrane.Logger | ||
|
||
alias Membrane.RTSP.Response | ||
@test_pt 96 | ||
@test_clock_rate 90_000 | ||
|
||
@impl true | ||
def init(config) do | ||
config | ||
|> Map.put(:pipeline_pid, nil) | ||
|> Map.put(:socket, nil) | ||
end | ||
|
||
@impl true | ||
def handle_open_connection(conn, state) do | ||
%{state | socket: conn} | ||
end | ||
|
||
@impl true | ||
def handle_describe(_req, state) do | ||
sdp = """ | ||
v=0 | ||
m=video 0 RTP/AVP 96 | ||
a=control:/control | ||
a=rtpmap:#{@test_pt} H264/#{@test_clock_rate} | ||
a=fmtp:#{@test_pt} packetization-mode=1 | ||
""" | ||
|
||
response = | ||
Response.new(200) | ||
|> Response.with_header("Content-Type", "application/sdp") | ||
|> Response.with_body(sdp) | ||
|
||
{response, state} | ||
end | ||
|
||
@impl true | ||
def handle_setup(_req, state) do | ||
{Response.new(200), state} | ||
end | ||
|
||
@impl true | ||
def handle_play(configured_media_context, state) do | ||
media_context = configured_media_context |> Map.values() |> List.first() | ||
|
||
{client_rtp_port, _client_rtcp_port} = media_context.client_port | ||
|
||
arg = %{ | ||
socket: state.socket, | ||
ssrc: media_context.ssrc, | ||
pt: @test_pt, | ||
clock_rate: @test_clock_rate, | ||
client_port: client_rtp_port, | ||
client_ip: media_context.address, | ||
server_rtp_socket: media_context.rtp_socket, | ||
fixture_path: state.fixture_path | ||
} | ||
|
||
{:ok, _sup_pid, pipeline_pid} = | ||
Membrane.Demo.RTSPToHLS.Server.Pipeline.start_link(arg) | ||
|
||
{Response.new(200), %{state | pipeline_pid: pipeline_pid}} | ||
end | ||
|
||
@impl true | ||
def handle_pause(state) do | ||
{Response.new(501), state} | ||
end | ||
|
||
@impl true | ||
def handle_teardown(state) do | ||
{Response.new(200), state} | ||
end | ||
|
||
@impl true | ||
def handle_closed_connection(_state), do: :ok | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule Membrane.Demo.RTSPToHLS.Server.Pipeline do | ||
@moduledoc false | ||
|
||
use Membrane.Pipeline | ||
|
||
@spec start_link(map()) :: Membrane.Pipeline.on_start() | ||
def start_link(config) do | ||
Membrane.Pipeline.start_link(__MODULE__, config) | ||
end | ||
|
||
@impl true | ||
def handle_init(_ctx, opts) do | ||
spec = | ||
child(:mp4_in_file_source, %Membrane.File.Source{ | ||
location: opts.fixture_path, | ||
seekable?: true | ||
}) | ||
|> child(:mp4_demuxer, %Membrane.MP4.Demuxer.ISOM{optimize_for_non_fast_start?: true}) | ||
|
||
{[spec: spec], opts} | ||
end | ||
|
||
@impl true | ||
def handle_child_notification({:new_tracks, tracks}, :mp4_demuxer, _ctx, state) do | ||
spec = | ||
Enum.map(tracks, fn | ||
{id, %Membrane.AAC{}} -> | ||
get_child(:mp4_demuxer) | ||
|> via_out(Pad.ref(:output, id)) | ||
|> child(Membrane.Debug.Sink) | ||
|
||
{id, %Membrane.H264{}} -> | ||
get_child(:mp4_demuxer) | ||
|> via_out(Pad.ref(:output, id)) | ||
|> child(:parser, %Membrane.H264.Parser{ | ||
output_alignment: :nalu, | ||
repeat_parameter_sets: true, | ||
skip_until_keyframe: true, | ||
output_stream_structure: :annexb | ||
}) | ||
|> via_in(Pad.ref(:input, state.ssrc), | ||
options: [payloader: Membrane.RTP.H264.Payloader] | ||
) | ||
|> child(:rtp, Membrane.RTP.SessionBin) | ||
|> via_out(Pad.ref(:rtp_output, state.ssrc), | ||
options: [ | ||
payload_type: state.pt, | ||
clock_rate: state.clock_rate | ||
] | ||
) | ||
|> child(:realtimer, Membrane.Realtimer) | ||
|> child(:udp_sink, %Membrane.UDP.Sink{ | ||
destination_address: state.client_ip, | ||
destination_port_no: state.client_port, | ||
local_socket: state.server_rtp_socket | ||
}) | ||
end) | ||
|
||
{[spec: spec], state} | ||
end | ||
|
||
@impl true | ||
def handle_child_notification(_notification, _element, _ctx, state) do | ||
{[], state} | ||
end | ||
|
||
@impl true | ||
def handle_element_end_of_stream(:udp_sink, :input, _ctx, state) do | ||
Process.sleep(50) | ||
:gen_tcp.close(state.socket) | ||
{[terminate: :normal], state} | ||
end | ||
|
||
@impl true | ||
def handle_element_end_of_stream(_child, _pad, _ctx, state) do | ||
{[], state} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is that needed?