1
0
mirror of https://github.com/algora-io/tv.git synced 2024-11-16 00:58:59 +02:00

Terminate interrupted streams (#75)

This commit is contained in:
Gabriel Piriz 2024-08-31 20:15:24 -03:00 committed by GitHub
parent 47e5916441
commit 09b0623af7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 74 additions and 0 deletions

View File

@ -55,6 +55,7 @@ defmodule Algora.Application do
start: {Membrane.RTMP.Source.TcpServer, :start_link, [tcp_server_options]}
},
Algora.Stargazer,
Algora.Terminate,
ExMarcel.TableWrapper,
Algora.Youtube.Chat.Supervisor
# Start a worker by calling: Algora.Worker.start_link(arg)

View File

@ -226,6 +226,43 @@ defmodule Algora.Library do
|> Repo.one()
end
def terminate_stream(video_id) do
video = Repo.get!(Video, video_id)
resp = Finch.build(:get, "#{video.url_root}/g3cFdmlkZW8.m3u8") |> Finch.request(Algora.Finch)
with {:ok, %Finch.Response{status: 200, body: body}} <- resp,
{:ok, duration} <- get_duration(video) do
if !String.ends_with?(body, "#EXT-X-ENDLIST\n") do
Storage.upload(
String.trim_trailing(body) <> "\n#EXT-X-ENDLIST\n",
"#{video.uuid}/g3cFdmlkZW8.m3u8",
content_type: "application/x-mpegURL"
)
end
video
|> change()
|> put_change(:duration, duration)
|> Repo.update()
else
_missing_manifest ->
video
|> change()
|> put_change(:corrupted, true)
|> Repo.update()
end
end
def terminate_interrupted_streams() do
from(v in Video,
where: v.duration == 0 and v.is_live == false and v.format == :hls and v.corrupted == false,
select: v.id
)
|> Repo.all()
|> Enum.each(&terminate_stream/1)
end
def toggle_streamer_live(%Video{} = video, is_live) do
video = get_video!(video.id)
user = Accounts.get_user!(video.user_id)

View File

@ -17,6 +17,7 @@ defmodule Algora.Library.Video do
field :description, :string
field :type, Ecto.Enum, values: [vod: 1, livestream: 2]
field :format, Ecto.Enum, values: [mp4: 1, hls: 2, youtube: 3]
field :corrupted, :boolean, default: false
field :is_live, :boolean, default: false
field :thumbnail_url, :string
field :vertical_thumbnail_url, :string

26
lib/algora/terminate.ex Normal file
View File

@ -0,0 +1,26 @@
defmodule Algora.Terminate do
use GenServer
@terminate_interval :timer.hours(1)
def start_link(_) do
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
end
@impl true
def init(state) do
schedule_terminate()
{:ok, state}
end
@impl true
def handle_info(:terminate, state) do
Algora.Library.terminate_interrupted_streams()
schedule_terminate()
{:noreply, state}
end
defp schedule_terminate() do
Process.send_after(self(), :terminate, @terminate_interval)
end
end

View File

@ -0,0 +1,9 @@
defmodule Algora.Repo.Local.Migrations.AddCorruptedToVideo do
use Ecto.Migration
def change do
alter table(:videos) do
add :corrupted, :boolean, default: false
end
end
end