forked from Glimesh/glimesh.tv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.ex
More file actions
353 lines (296 loc) · 11.3 KB
/
stream.ex
File metadata and controls
353 lines (296 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
defmodule GlimeshWeb.UserLive.Stream do
use GlimeshWeb, :live_view
alias Glimesh.Accounts
alias Glimesh.{ChannelHostsLookups, ChannelLookups}
alias Glimesh.Presence
alias Glimesh.Raids
alias Glimesh.Streams
def mount(%{"username" => streamer_username} = params, session, socket) do
case ChannelLookups.get_channel_for_username(streamer_username) do
%Glimesh.Streams.Channel{} = channel ->
if session["locale"], do: Gettext.put_locale(session["locale"])
maybe_user = Accounts.get_user_by_session_token(session["user_token"])
streamer = Accounts.get_user!(channel.streamer_id)
{:ok,
%{
:redirect_to_hosted_target => redirect_to_hosted_target,
:hosting_channel => hosting_channel
}} = get_hosting_data(params, channel, maybe_user, streamer, session["user_agent"])
if redirect_to_hosted_target do
{:ok,
socket
|> redirect(
to:
"/#{hosting_channel.target.user.username}/?host=#{hosting_channel.host.user.username}"
)}
else
if connected?(socket) do
# Wait until the socket connection is ready to load the stream
if Streams.is_live?(channel) do
Process.send(self(), :load_stream, [])
end
Streams.subscribe_to(:channel, channel.id)
if not is_nil(maybe_user) do
Streams.subscribe_to(:raid, channel.id)
Process.send(self(), :track_raid_channel, [])
end
end
avatar_url = Glimesh.Avatar.url({streamer.avatar, streamer}, :original)
has_some_support_option =
length(Glimesh.Streams.list_support_tabs(channel.user, channel)) > 0
viewer_can_raid =
if is_nil(maybe_user),
do: false,
else: ChannelLookups.can_viewer_raid_channel?(maybe_user, channel)
{:ok,
socket
|> put_page_title(channel.title)
|> assign(:show_debug, false)
|> assign(:show_support_modal, socket.assigns.live_action == :support)
|> assign(:support_modal_tab, Map.get(params, "tab"))
|> assign(:stripe_session_id, Map.get(params, "stripe_session_id"))
|> assign(:unique_user, Map.get(session, "unique_user"))
|> assign(:country, Map.get(session, "country"))
|> assign(:prompt_mature, Streams.prompt_mature_content(channel, maybe_user))
|> assign(:streamer, channel.user)
|> assign(:can_receive_payments, Accounts.can_receive_payments?(channel.user))
|> assign(:has_some_support_option, has_some_support_option)
|> assign(:channel, channel)
|> assign(:hosting_channel, hosting_channel)
|> assign(:stream, channel.stream)
|> assign(:channel_poster, get_stream_thumbnail(channel))
|> assign(:custom_meta, meta_tags(channel, avatar_url))
|> assign(:janus_url, "Pending...")
|> assign(:janus_hostname, "Pending...")
|> assign(:lost_packets, 0)
|> assign(:stream_metadata, get_last_stream_metadata(channel.stream))
|> assign(:player_error, nil)
|> assign(:user, maybe_user)
|> assign(:ultrawide, false)
|> assign(:interactive_toggle, false)}
|> assign(:webrtc_error, false)
|> assign(:can_raid, viewer_can_raid)
|> assign(:raid_starting, false)
|> assign(:raid_group_id, "")
|> assign(:raid_target, nil)
|> assign(:raid_time, 0)}
end
nil ->
{:ok, redirect(socket, to: "/#{streamer_username}/profile")}
end
end
defp get_hosting_data(params, channel, maybe_user, streamer, user_agent) do
is_live = Streams.is_live?(channel)
is_twitterbot = user_agent =~ "Twitterbot"
hosting_channel =
cond do
params["host"] ->
ChannelHostsLookups.get_targets_host_info(params["host"], channel)
is_live == false ->
ChannelHostsLookups.get_current_hosting_target(channel)
true ->
nil
end
# this channel is hosting another
not_bot_and_not_live = is_twitterbot == false and is_live == false
host_valid = hosting_channel != nil and check_host_params(params)
not_streamer = maybe_user == nil or maybe_user.id != streamer.id
{:ok,
%{
is_live: is_live,
hosting_channel: hosting_channel,
redirect_to_hosted_target: not_bot_and_not_live and host_valid and not_streamer
}}
end
defp check_host_params(params) do
params["host"] == nil and params["follow_host"] != "false"
end
def handle_params(_unsigned_params, _uri, socket) do
{:noreply, socket |> assign(:show_support_modal, socket.assigns.live_action == :support)}
end
def handle_info(:load_stream, socket) do
case Glimesh.Janus.get_closest_edge_location(socket.assigns.country) do
%Glimesh.Janus.EdgeRoute{id: janus_edge_id, url: janus_url, hostname: janus_hostname} ->
Presence.track_presence(
self(),
Streams.get_subscribe_topic(:viewers, socket.assigns.channel.id),
socket.assigns.unique_user,
%{
janus_edge_id: janus_edge_id
}
)
Process.send(self(), :remove_packet_warning, [])
{:noreply,
socket
|> push_event("load_video", %{
janus_url: janus_url,
channel_id: socket.assigns.channel.id
})
|> assign(:janus_url, janus_url)
|> assign(:janus_hostname, janus_hostname)}
_ ->
# In the event we can't find an edge, something is real wrong
{:noreply,
socket
|> assign(:player_error, "Unable to find edge video location, we'll be back soon!")}
end
end
def handle_info(:remove_packet_warning, socket) do
Process.send_after(self(), :remove_packet_warning, 15_000)
{:noreply, socket |> assign(:player_error, nil)}
end
def handle_info({:channel, channel}, socket) do
if socket.assigns.status == "offline" and channel.status == "live" and
socket.assigns.prompt_mature == false do
Process.send(self(), :load_stream, [])
end
{:noreply, socket |> assign(:stream, channel.stream)}
end
def handle_info({:raid, %{:action => "pending"} = payload}, socket) do
seconds_till_raid = abs(NaiveDateTime.diff(payload[:time], NaiveDateTime.utc_now(), :second))
{:noreply,
socket
|> assign(:raid_starting, not is_nil(socket.assigns.user))
|> assign(:raid_group_id, payload[:group_id])
|> assign(:raid_target, payload[:target])
|> assign(:raid_time, seconds_till_raid)}
end
def handle_info({:raid, %{:action => "cancelled"}}, socket) do
{:noreply,
socket
|> assign(:raid_starting, false)
|> assign(:raid_group_id, "")
|> assign(:raid_target, nil)
|> assign(:raid_time, 0)
|> push_event("cancel_raid", %{})
|> put_flash(:info, gettext("Streamer has cancelled pending raid."))}
end
def handle_info(:track_raid_channel, socket) do
# only logged in users can participate in a raid
if not is_nil(socket.assigns.user) do
Presence.track_presence(
self(),
Streams.get_subscribe_topic(:raid, socket.assigns.channel.id),
socket.assigns.unique_user,
%{
logged_in_user_id: socket.assigns.user.id
}
)
end
{:noreply, socket}
end
def handle_info(
%{
event: "presence_diff",
topic: "streams:raid:" <> _streamer = _topic
},
socket
) do
{:noreply, socket}
end
def handle_info({:raid, %{:action => "active"} = payload}, socket) do
target_channel_username = payload[:target]
raid_users = payload[:users]
if is_current_user_in_raid?(raid_users, socket.assigns.user) do
{:noreply,
socket
|> assign(:raid_starting, false)
|> assign(:raid_group_id, payload[:group_id])
|> push_redirect(to: Routes.user_stream_path(socket, :index, target_channel_username))}
else
{:noreply, socket}
end
end
def handle_event("decline-raid", _value, socket) do
raid_group_id = socket.assigns.raid_group_id
user = socket.assigns.user
if not is_nil(user) do
Raids.remove_pending_raid_user(raid_group_id, user.id)
end
{:noreply,
socket
|> assign(:raid_starting, false)
|> assign(:raid_group_id, "")
|> assign(:raid_target, nil)
|> assign(:raid_time, 0)}
end
def handle_event("show_mature", _value, socket) do
Process.send(self(), :load_stream, [])
{:noreply, assign(socket, :prompt_mature, false)}
end
def handle_event("toggle_debug", _value, socket) do
if socket.assigns.show_debug === false do
# Opening the window
{:noreply,
socket
|> assign(:stream_metadata, get_last_stream_metadata(socket.assigns.stream))
|> assign(:show_debug, true)}
else
{:noreply, assign(socket, :show_debug, false)}
end
end
def handle_event("lost_packets", %{"uplink" => _uplink, "lostPackets" => lost_packets}, socket)
when is_integer(lost_packets) do
message =
if lost_packets > 6,
do:
gettext(
"We're detecting some networking problems between you and the streamer. You may experience video drops, jitter, or other issues! If this continues, the streamer is recommended to submit a ticket in #streaming-help in our Discord."
),
else: nil
{:noreply,
socket
|> update(:lost_packets, &(&1 + lost_packets))
|> assign(:player_error, message)}
end
def handle_event("lost_packets", _, socket) do
{:noreply, socket}
end
def handle_event("webrtc_error", message, socket) do
{:noreply, socket |> assign(:webrtc_error, message)}
end
def handle_event("ultrawide", %{"enabled" => enabled}, socket) do
{:noreply, socket |> assign(:ultrawide, enabled)}
end
def handle_event("toggle_interactive", _value, socket) do
{:noreply,
socket
|> assign(:interactive_toggle, !socket.assigns.interactive_toggle)}
end
defp get_stream_thumbnail(channel) do
case channel.stream do
%Glimesh.Streams.Stream{} = stream ->
Glimesh.StreamThumbnail.url({stream.thumbnail, stream}, :original)
_ ->
Glimesh.ChannelPoster.url({channel.poster, channel}, :original)
end
end
defp get_last_stream_metadata(%Glimesh.Streams.Stream{} = stream) do
case Glimesh.Streams.get_last_stream_metadata(stream) do
%Glimesh.Streams.StreamMetadata{} = metadata -> metadata
_ -> %Glimesh.Streams.StreamMetadata{stream: stream}
end
end
defp get_last_stream_metadata(_) do
%Glimesh.Streams.StreamMetadata{}
end
defp meta_tags(%Streams.Channel{status: "live"} = channel, _) do
%{
title: channel.title,
description: "#{channel.user.displayname} is streaming live on Glimesh.tv!",
card_type: "summary_large_image",
image_url: get_stream_thumbnail(channel)
}
end
defp meta_tags(%Streams.Channel{} = channel, avatar_url) do
%{
title: "#{channel.user.displayname}'s Glimesh Channel",
description:
"#{channel.user.displayname}'s channel on Glimesh, the next-gen live streaming platform.",
image_url: avatar_url
}
end
defp is_current_user_in_raid?(raid_users, current_user) do
Enum.any?(raid_users, fn raid_user -> raid_user.user_id == current_user.id end)
end
end