-
Notifications
You must be signed in to change notification settings - Fork 15
Update return values in callbacks to reflect 1.0 spec #103
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
Changes from 1 commit
0d8ea79
ea2b9fa
df86952
9297575
d04878e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,28 +8,28 @@ which is invoked once the pipeline is initialized. | |
| **_`lib/rtmp_to_hls/pipeline.ex`_** | ||
|
|
||
| ```elixir | ||
| @impl true | ||
| def handle_init(_opts) do | ||
| ... | ||
| children: %{ | ||
| src: %Membrane.RTMP.SourceBin{port: 9009}, | ||
| sink: %Membrane.HTTPAdaptiveStream.SinkBin{ | ||
| manifest_module: Membrane.HTTPAdaptiveStream.HLS, | ||
| target_window_duration: 20 |> Membrane.Time.seconds(), | ||
| muxer_segment_duration: 8 |> Membrane.Time.seconds(), | ||
| storage: %Membrane.HTTPAdaptiveStream.Storages.FileStorage{directory: "output"} | ||
| } | ||
| }, | ||
| ... | ||
| end | ||
| @impl true | ||
| def handle_init(_opts) do | ||
| ... | ||
| children: %{ | ||
| src: %Membrane.RTMP.SourceBin{port: 9009}, | ||
| sink: %Membrane.HTTPAdaptiveStream.SinkBin{ | ||
| manifest_module: Membrane.HTTPAdaptiveStream.HLS, | ||
| target_window_duration: 20 |> Membrane.Time.seconds(), | ||
| muxer_segment_duration: 8 |> Membrane.Time.seconds(), | ||
| storage: %Membrane.HTTPAdaptiveStream.Storages.FileStorage{directory: "output"} | ||
| } | ||
| }, | ||
|
Collaborator
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. we don't use the |
||
| ... | ||
| end | ||
| ``` | ||
|
|
||
| First, we define the list of children. The following children are defined: | ||
|
|
||
| - `:src` - a `Membrane.RTMP.SourceBin`, an RTMP server, which, according to its `:port` configuration, will be listening on port `9009`. This bin will be acting as a source for our pipeline. For more information on RTMP Source Bin please visit [the documentation](https://hexdocs.pm/membrane_rtmp_plugin/Membrane.RTMP.SourceBin.html). | ||
| - `:sink` - a `Membrane.HTTPAdaptiveStream.SinkBin`, acting as a sink of the pipeline. The full documentation of that bin is available [here](https://hexdocs.pm/membrane_http_adaptive_stream_plugin/Membrane.HTTPAdaptiveStream.SinkBin.html). We need to specify some of its options: | ||
| - `:manifest_module` - a module which implements [`Membrane.HTTPAdaptiveStream.Manifest`](https://hexdocs.pm/membrane_http_adaptive_stream_plugin/Membrane.HTTPAdaptiveStream.Manifest.html#c:serialize/1) behavior. A manifest allows aggregate tracks (of a different type, i.e. an audio track and a video track as well as many tracks of the same type, i.e. a few video tracks with different resolutions). For each track, the manifest holds a reference to a list of segments, which form that track. Furthermore, the manifest module is equipped with the `serialize/1` method, which allows transforming that manifest to a string (which later on can be written to a file). In that case, we use a built-in implementation of a manifest module - the `Membrane.HTTPAdaptiveStream.HLS`, designed to serialize a manifest into a form required by HLS. | ||
| - `:target_window_duriation` - that value determines the minimal manifest's duration. The oldest segments of the tracks will be removed whenever possible if persisting them would result in exceeding the manifest duration. | ||
| - `:target_window_duriation` - determines the minimal manifest's duration. The oldest segments of the tracks will be removed whenever possible if persisting them would result in exceeding the manifest duration. | ||
| - `:muxer_segment_duration` - the maximal duration of a segment. Each segment of each track shouldn't exceed that value. In our case, we have decided to limit the length of each segment to 8 seconds. | ||
| - `:storage` - the sink element, the module responsible for writing down the HLS playlist and manifest files. In our case, we use a pre-implemented `Membrane.HTTPAdaptiveStream.FileStorage` module, designed to write the files to the local filesystem. We configure it so that the directory where the files will be put in the `output/` directory (make sure that that directory exists as the storage module won't create it itself). | ||
|
|
||
|
|
@@ -39,21 +39,21 @@ After providing the children's specifications, we are ready to connect the pads | |
| **_`lib/rtmp_to_hls/pipeline.ex`_** | ||
|
|
||
| ```elixir | ||
| @impl true | ||
| def handle_init(_opts) do | ||
| ... | ||
| links: [ | ||
| link(:src) | ||
| |> via_out(:audio) | ||
| |> via_in(Pad.ref(:input, :audio), options: [encoding: :AAC]) | ||
| |> to(:sink), | ||
| link(:src) | ||
| |> via_out(:video) | ||
| |> via_in(Pad.ref(:input, :video), options: [encoding: :H264]) | ||
| |> to(:sink) | ||
| ] | ||
| ... | ||
| end | ||
| @impl true | ||
| def handle_init(_opts) do | ||
| ... | ||
| links: [ | ||
| link(:src) | ||
| |> via_out(:audio) | ||
| |> via_in(Pad.ref(:input, :audio), options: [encoding: :AAC]) | ||
| |> to(:sink), | ||
| link(:src) | ||
| |> via_out(:video) | ||
| |> via_in(Pad.ref(:input, :video), options: [encoding: :H264]) | ||
| |> to(:sink) | ||
|
Collaborator
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. This one is also unfortunately outdated, we don't use |
||
| ] | ||
| ... | ||
| end | ||
| ``` | ||
|
|
||
| The structure of links reflects the desired architecture of the application. | ||
|
|
@@ -67,11 +67,11 @@ The final thing that is done in the `handle_init/1` callback's implementation is | |
| **_`lib/rtmp_to_hls/pipeline.ex`_** | ||
|
|
||
| ```elixir | ||
| @impl true | ||
| def handle_init(_opts) do | ||
| ... | ||
| { {:ok, spec: spec, playback: :playing}, %{} } | ||
| end | ||
| @impl true | ||
| def handle_init(_opts) do | ||
| ... | ||
| {[spec: spec, playback: :playing], %{}} | ||
|
Collaborator
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. We don't have |
||
| end | ||
| ``` | ||
|
|
||
| The first action is the `:spec` action, which spawns the children. The second action changes the playback state of the pipeline into the `:playing` - meaning, that data can start flowing through the pipeline. | ||
|
|
@@ -83,51 +83,51 @@ The pipeline is started with `Supervisor.start_link`, as a child of the applicat | |
| **_`lib/rtmp_to_hls/application.ex`_** | ||
|
|
||
| ```elixir | ||
| @impl true | ||
| def start(_type, _args) do | ||
| children = [ | ||
| # Start the Pipeline | ||
| Membrane.Demo.RtmpToHls, | ||
| ... | ||
| ] | ||
| opts = [strategy: :one_for_one, name: RtmpToHls.Supervisor] | ||
| Supervisor.start_link(children, opts) | ||
| end | ||
| @impl true | ||
| def start(_type, _args) do | ||
| children = [ | ||
| # Start the Pipeline | ||
| Membrane.Demo.RtmpToHls, | ||
| ... | ||
| ] | ||
| opts = [strategy: :one_for_one, name: RtmpToHls.Supervisor] | ||
| Supervisor.start_link(children, opts) | ||
| end | ||
| ``` | ||
|
|
||
| ## HLS controller | ||
|
|
||
| The files produced with the pipeline are written down to the `output/` directory. We need to make them accessible via HTTP. | ||
| The Phoenix Framework provides tools to achieve that - take a look at the `RtmpToHlsWeb.Router`: | ||
| The Phoenix Framework provides tools to achieve that - take a look at `RtmpToHlsWeb.Router`: | ||
| **_`lib/rtmp_to_hls_web/router.ex`_** | ||
|
|
||
| ```elixir | ||
| scope "/", RtmpToHlsWeb do | ||
| pipe_through :browser | ||
| pipe_through :browser | ||
|
|
||
| get "/", PageController, :index | ||
| get "/video/:filename", HlsController, :index | ||
| end | ||
| ``` | ||
| get "/", PageController, :index | ||
| get "/video/:filename", HlsController, :index | ||
| end | ||
| ``` | ||
|
|
||
| We are directing HTTP requests on `/video/:filename` to the HlsController, whose implementation is shown below: | ||
| **_`lib/rtmp_to_hls_web/controllers/hls_controller.ex`_** | ||
|
|
||
| ```elixir | ||
| defmodule RtmpToHlsWeb.HlsController do | ||
| use RtmpToHlsWeb, :controller | ||
| use RtmpToHlsWeb, :controller | ||
|
|
||
| alias Plug | ||
| alias Plug | ||
|
|
||
| def index(conn, %{"filename" => filename}) do | ||
| path = "output/#{filename}" | ||
| def index(conn, %{"filename" => filename}) do | ||
| path = "output/#{filename}" | ||
|
|
||
| if File.exists?(path) do | ||
| conn |> Plug.Conn.send_file(200, path) | ||
| else | ||
| conn |> Plug.Conn.send_resp(404, "File not found") | ||
| end | ||
| if File.exists?(path) do | ||
| conn |> Plug.Conn.send_file(200, path) | ||
| else | ||
| conn |> Plug.Conn.send_resp(404, "File not found") | ||
| end | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
|
|
||
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.
I just spotted this one as well :D