-
Notifications
You must be signed in to change notification settings - Fork 24
Implement HLS manager #117
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8192431
Implement HLS manager
Karolk99 bbb4ee7
Change requests
Karolk99 ecd6cc9
Add tests for hls manager
Karolk99 17fa622
Use HTTPoison instead of Hackney in ex_aws
Karolk99 639cfa0
Change requests
Karolk99 3bde795
Fix api spec
Karolk99 5286b8c
Change requests
Karolk99 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,19 @@ | ||
| defmodule Jellyfish.Component.HLS.HTTPoison do | ||
| @moduledoc false | ||
|
|
||
| @behaviour ExAws.Request.HttpClient | ||
|
|
||
| @impl true | ||
| def request(method, url, body \\ "", headers \\ [], http_opts \\ []) do | ||
| case HTTPoison.request(method, url, body, headers, http_opts) do | ||
| {:ok, %HTTPoison.Response{status_code: status, headers: headers}} -> | ||
| {:ok, %{status_code: status, headers: headers}} | ||
|
|
||
| {:ok, %HTTPoison.Response{status_code: status, headers: headers, body: body}} -> | ||
| {:ok, %{status_code: status, headers: headers, body: body}} | ||
|
|
||
| {:error, %HTTPoison.Error{reason: reason}} -> | ||
| {:error, reason} | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,121 @@ | ||||||
| defmodule Jellyfish.Component.HLS.Manager do | ||||||
| @moduledoc """ | ||||||
| Module responsible for HLS processing. | ||||||
|
mickel8 marked this conversation as resolved.
|
||||||
| It: | ||||||
| * uploads HLS playlist to S3 | ||||||
| * removes HLS playlist from a disk | ||||||
| """ | ||||||
|
|
||||||
| use GenServer, restart: :temporary | ||||||
| use Bunch | ||||||
|
|
||||||
| require Logger | ||||||
|
|
||||||
| alias Jellyfish.Room | ||||||
|
|
||||||
| @hls_extensions [".m4s", ".m3u8", ".mp4"] | ||||||
| @playlist_content_type "application/vnd.apple.mpegurl" | ||||||
|
|
||||||
| @type options :: %{ | ||||||
| room_id: Room.id(), | ||||||
| engine_pid: pid(), | ||||||
| hls_dir: String.t(), | ||||||
| hls_options: map() | ||||||
| } | ||||||
|
|
||||||
| @spec start(Room.id(), pid(), String.t(), map()) :: {:ok, pid()} | {:error, term()} | ||||||
| def start(room_id, engine_pid, hls_dir, hls_options) do | ||||||
| DynamicSupervisor.start_child( | ||||||
| Jellyfish.HLS.ManagerSupervisor, | ||||||
| {__MODULE__, | ||||||
| %{room_id: room_id, engine_pid: engine_pid, hls_dir: hls_dir, hls_options: hls_options}} | ||||||
| ) | ||||||
| end | ||||||
|
|
||||||
| @spec start_link(options()) :: GenServer.on_start() | ||||||
| def start_link(opts) do | ||||||
|
mickel8 marked this conversation as resolved.
|
||||||
| GenServer.start_link(__MODULE__, opts) | ||||||
| end | ||||||
|
|
||||||
| @impl true | ||||||
| def init(%{engine_pid: engine_pid, room_id: room_id} = state) do | ||||||
| Process.monitor(engine_pid) | ||||||
| Logger.info("Initialize hls manager, room: #{room_id}") | ||||||
|
|
||||||
| {:ok, state} | ||||||
| end | ||||||
|
|
||||||
| @impl true | ||||||
| def handle_info( | ||||||
| {:DOWN, _ref, :process, engine_pid, _reason}, | ||||||
| %{engine_pid: engine_pid, hls_options: hls_options, hls_dir: hls_dir, room_id: room_id} = | ||||||
| state | ||||||
| ) do | ||||||
| unless is_nil(hls_options.s3), do: upload_to_s3(hls_dir, room_id, hls_options.s3) | ||||||
|
Contributor
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. Shouldn't that work?
Suggested change
|
||||||
| unless hls_options.persistent, do: remove_hls(hls_dir, room_id) | ||||||
|
|
||||||
| {:stop, :normal, state} | ||||||
| end | ||||||
|
|
||||||
| defp upload_to_s3(hls_dir, room_id, credentials) do | ||||||
| Logger.info("Start uploading to s3, room: #{room_id}") | ||||||
|
|
||||||
| config = create_aws_config(credentials) | ||||||
|
|
||||||
| result = | ||||||
| hls_dir | ||||||
| |> get_hls_files() | ||||||
| |> Bunch.Enum.try_each(fn file -> | ||||||
|
Karolk99 marked this conversation as resolved.
|
||||||
| content = get_content(hls_dir, file) | ||||||
| s3_path = get_s3_path(room_id, file) | ||||||
| opts = get_options(file) | ||||||
|
|
||||||
| upload_file_to_s3(content, s3_path, opts, config, credentials) | ||||||
| end) | ||||||
|
|
||||||
| Logger.info("Finished uploading to s3 with result: #{result}, room: #{room_id}") | ||||||
| end | ||||||
|
|
||||||
| defp upload_file_to_s3(content, s3_path, opts, config, credentials) do | ||||||
| result = | ||||||
| credentials.bucket | ||||||
| |> ExAws.S3.put_object(s3_path, content, opts) | ||||||
| |> ExAws.request(config) | ||||||
|
|
||||||
| case result do | ||||||
| {:ok, _value} -> :ok | ||||||
| error -> error | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| defp get_hls_files(hls_dir) do | ||||||
| hls_dir | ||||||
| |> File.ls!() | ||||||
| |> Enum.filter(fn file -> String.ends_with?(file, @hls_extensions) end) | ||||||
| end | ||||||
|
|
||||||
| defp create_aws_config(credentials) do | ||||||
| credentials | ||||||
| |> Enum.reject(fn {key, _value} -> key == :bucket end) | ||||||
| |> then(&ExAws.Config.new(:s3, &1)) | ||||||
| |> Map.to_list() | ||||||
| end | ||||||
|
|
||||||
| defp get_content(hls_dir, file) do | ||||||
| {:ok, content} = hls_dir |> Path.join(file) |> File.read() | ||||||
| content | ||||||
| end | ||||||
|
|
||||||
| defp get_options(file) do | ||||||
| if String.ends_with?(file, ".m3u8"), | ||||||
| do: [content_type: @playlist_content_type], | ||||||
| else: [] | ||||||
| end | ||||||
|
|
||||||
| defp get_s3_path(room_id, file), do: Path.join(room_id, file) | ||||||
|
|
||||||
| defp remove_hls(hls_dir, room_id) do | ||||||
| File.rm_rf!(hls_dir) | ||||||
| Logger.info("Remove hls from a disk, room: #{room_id}") | ||||||
| end | ||||||
| end | ||||||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.