Update LiveStream model

This commit is contained in:
Daniel Supernault 2022-06-15 00:46:20 -06:00
parent 4ff179ad4d
commit 494f11202f
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
2 changed files with 280 additions and 207 deletions

View File

@ -12,225 +12,304 @@ use App\Services\LiveStreamService;
class LiveStreamController extends Controller class LiveStreamController extends Controller
{ {
public function createStream(Request $request) public function createStream(Request $request)
{ {
abort_if(!config('livestreaming.enabled'), 400); abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
if(config('livestreaming.broadcast.limits.enabled')) { if(config('livestreaming.broadcast.limits.enabled')) {
if($request->user()->is_admin) { if($request->user()->is_admin) {
} else { } else {
$limits = config('livestreaming.broadcast.limits'); $limits = config('livestreaming.broadcast.limits');
$user = $request->user(); $user = $request->user();
abort_if($limits['admins_only'] && $user->is_admin == false, 401, 'LSE:003'); abort_if($limits['admins_only'] && $user->is_admin == false, 401, 'LSE:003');
if($limits['min_account_age']) { if($limits['min_account_age']) {
abort_if($user->created_at->gt(now()->subDays($limits['min_account_age'])), 403, 'LSE:005'); abort_if($user->created_at->gt(now()->subDays($limits['min_account_age'])), 403, 'LSE:005');
} }
if($limits['min_follower_count']) { if($limits['min_follower_count']) {
$account = AccountService::get($user->profile_id); $account = AccountService::get($user->profile_id);
abort_if($account['followers_count'] < $limits['min_follower_count'], 403, 'LSE:008'); abort_if($account['followers_count'] < $limits['min_follower_count'], 403, 'LSE:008');
} }
} }
} }
$this->validate($request, [ $this->validate($request, [
'name' => 'nullable|string|max:80', 'name' => 'nullable|string|max:80',
'description' => 'nullable|string|max:240', 'description' => 'nullable|string|max:240',
'visibility' => 'required|in:public,private' 'visibility' => 'required|in:public,private'
]); ]);
$stream = new LiveStream; $stream = new LiveStream;
$stream->name = $request->input('name'); $stream->name = $request->input('name');
$stream->description = $request->input('description'); $stream->description = $request->input('description');
$stream->visibility = $request->input('visibility'); $stream->visibility = $request->input('visibility');
$stream->profile_id = $request->user()->profile_id; $stream->profile_id = $request->user()->profile_id;
$stream->stream_id = Str::random(40); $stream->stream_id = Str::random(40) . '_' . $stream->profile_id;
$stream->stream_key = Str::random(64); $stream->stream_key = 'streamkey-' . Str::random(64);
$stream->save(); $stream->save();
return [ return [
'url' => $stream->getStreamKeyUrl(), 'host' => $stream->getStreamServer(),
'id' => $stream->stream_id 'key' => $stream->stream_key,
]; 'url' => $stream->getStreamKeyUrl(),
} 'id' => $stream->stream_id
];
}
public function getUserStream(Request $request) public function getUserStream(Request $request)
{ {
abort_if(!config('livestreaming.enabled'), 400); abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$stream = LiveStream::whereProfileId($request->input('profile_id'))->first(); $stream = LiveStream::whereProfileId($request->input('profile_id'))->first();
if(!$stream) { if(!$stream) {
return []; return [];
} }
$res = []; $res = [];
$owner = $stream->profile_id == $request->user()->profile_id; $owner = $stream->profile_id == $request->user()->profile_id;
if($stream->visibility === 'private') { if($stream->visibility === 'private') {
abort_if(!$owner && !FollowerService::follows($request->user()->profile_id, $stream->profile_id), 403, 'LSE:011'); abort_if(!$owner && !FollowerService::follows($request->user()->profile_id, $stream->profile_id), 403, 'LSE:011');
} }
if($owner) { if($owner) {
$res['stream_key'] = $stream->stream_key; $res['stream_key'] = $stream->stream_key;
$res['stream_id'] = $stream->stream_id; $res['stream_id'] = $stream->stream_id;
$res['stream_url'] = $stream->getStreamKeyUrl(); $res['stream_url'] = $stream->getStreamKeyUrl();
} }
if($stream->live_at == null) { if($stream->live_at == null) {
$res['hls_url'] = null; $res['hls_url'] = null;
$res['name'] = $stream->name; $res['name'] = $stream->name;
$res['description'] = $stream->description; $res['description'] = $stream->description;
return $res; return $res;
} }
$res = [ $res = [
'hls_url' => $stream->getHlsUrl(), 'hls_url' => $stream->getHlsUrl(),
'name' => $stream->name, 'name' => $stream->name,
'description' => $stream->description 'description' => $stream->description
]; ];
return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES); return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
} }
public function deleteStream(Request $request) public function deleteStream(Request $request)
{ {
abort_if(!config('livestreaming.enabled'), 400); abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
LiveStream::whereProfileId($request->user()->profile_id) LiveStream::whereProfileId($request->user()->profile_id)
->get() ->get()
->each(function($stream) { ->each(function($stream) {
Storage::deleteDirectory("public/live-hls/{$stream->stream_id}"); Storage::deleteDirectory("public/live-hls/{$stream->stream_id}");
$stream->delete(); $stream->delete();
}); });
return [200]; return [200];
} }
public function getActiveStreams(Request $request) public function getActiveStreams(Request $request)
{ {
abort_if(!config('livestreaming.enabled'), 400); abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
return LiveStream::whereVisibility('local')->whereNotNull('live_at')->get()->map(function($stream) { return LiveStream::whereVisibility('local')->whereNotNull('live_at')->get()->map(function($stream) {
return [ return [
'account' => AccountService::get($stream->profile_id), 'account' => AccountService::get($stream->profile_id),
'stream_id' => $stream->stream_id 'stream_id' => $stream->stream_id
]; ];
}); });
} }
public function getLatestChat(Request $request) public function getLatestChat(Request $request)
{ {
abort_if(!config('livestreaming.enabled'), 400); abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$stream = LiveStream::whereProfileId($request->input('profile_id')) $stream = LiveStream::whereProfileId($request->input('profile_id'))
->whereNotNull('live_at') ->whereNotNull('live_at')
->first(); ->first();
if(!$stream) { if(!$stream) {
return []; return [];
} }
$owner = $stream->profile_id == $request->user()->profile_id; $owner = $stream->profile_id == $request->user()->profile_id;
if($stream->visibility === 'private') { if($stream->visibility === 'private') {
abort_if(!$owner && !FollowerService::follows($request->user()->profile_id, $stream->profile_id), 403, 'LSE:021'); abort_if(!$owner && !FollowerService::follows($request->user()->profile_id, $stream->profile_id), 403, 'LSE:021');
} }
$res = collect(LiveStreamService::getComments($stream->profile_id)) $res = collect(LiveStreamService::getComments($stream->profile_id))
->map(function($r) { ->map(function($res) {
return json_decode($r); return json_decode($res);
}); });
return $res; return $res;
} }
public function addChatComment(Request $request) public function addChatComment(Request $request)
{ {
abort_if(!config('livestreaming.enabled'), 400); abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'profile_id' => 'required|exists:profiles,id', 'profile_id' => 'required|exists:profiles,id',
'message' => 'required|max:140' 'message' => 'required|max:140'
]); ]);
$stream = LiveStream::whereProfileId($request->input('profile_id'))->firstOrFail(); $stream = LiveStream::whereProfileId($request->input('profile_id'))->firstOrFail();
$owner = $stream->profile_id == $request->user()->profile_id; $owner = $stream->profile_id == $request->user()->profile_id;
if($stream->visibility === 'private') { if($stream->visibility === 'private') {
abort_if(!$owner && !FollowerService::follows($request->user()->profile_id, $stream->profile_id), 403, 'LSE:022'); abort_if(!$owner && !FollowerService::follows($request->user()->profile_id, $stream->profile_id), 403, 'LSE:022');
} }
$res = [ $res = [
'pid' => (string) $request->user()->profile_id, 'pid' => (string) $request->user()->profile_id,
'username' => $request->user()->username, 'username' => $request->user()->username,
'text' => $request->input('message'), 'text' => $request->input('message'),
'ts' => now()->timestamp 'ts' => now()->timestamp
]; ];
LiveStreamService::addComment($stream->profile_id, json_encode($res, JSON_UNESCAPED_SLASHES)); LiveStreamService::addComment($stream->profile_id, json_encode($res, JSON_UNESCAPED_SLASHES));
return $res; return $res;
} }
public function editStream(Request $request) public function editStream(Request $request)
{ {
abort_if(!config('livestreaming.enabled'), 400); abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'name' => 'nullable|string|max:80', 'name' => 'nullable|string|max:80',
'description' => 'nullable|string|max:240' 'description' => 'nullable|string|max:240'
]); ]);
$stream = LiveStream::whereProfileId($request->user()->profile_id)->firstOrFail(); $stream = LiveStream::whereProfileId($request->user()->profile_id)->firstOrFail();
$stream->name = $request->input('name'); $stream->name = $request->input('name');
$stream->description = $request->input('description'); $stream->description = $request->input('description');
$stream->save(); $stream->save();
return; return;
} }
public function deleteChatComment(Request $request) public function deleteChatComment(Request $request)
{ {
abort_if(!config('livestreaming.enabled'), 400); abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'profile_id' => 'required|exists:profiles,id', 'profile_id' => 'required|exists:profiles,id',
'message' => 'required' 'message' => 'required'
]); ]);
abort_if($request->user()->profile_id != $request->input('profile_id'), 403); abort_if($request->user()->profile_id != $request->input('profile_id'), 403);
$stream = LiveStream::whereProfileId($request->user()->profile_id)->firstOrFail(); $stream = LiveStream::whereProfileId($request->user()->profile_id)->firstOrFail();
$payload = $request->input('message'); $payload = $request->input('message');
$payload = json_encode($payload, JSON_UNESCAPED_SLASHES); $payload = json_encode($payload, JSON_UNESCAPED_SLASHES);
LiveStreamService::deleteComment($stream->profile_id, $payload); LiveStreamService::deleteComment($stream->profile_id, $payload);
return; return;
} }
public function getConfig(Request $request) public function getConfig(Request $request)
{ {
abort_if(!config('livestreaming.enabled'), 400); abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$res = [ $res = [
'enabled' => config('livestreaming.enabled'), 'enabled' => config('livestreaming.enabled'),
'broadcast' => [ 'broadcast' => [
'sources' => config('livestreaming.broadcast.sources'), 'sources' => config('livestreaming.broadcast.sources'),
'limits' => config('livestreaming.broadcast.limits') 'limits' => config('livestreaming.broadcast.limits')
], ],
]; ];
return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES); return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
} }
public function clientBroadcastPublish(Request $request)
{
abort_if(!config('livestreaming.enabled'), 400);
$key = $request->input('name');
$name = $request->input('name');
abort_if(!$name, 400);
if(empty($key)) {
abort_if(!$request->filled('tcurl'), 400);
$url = $this->parseStreamUrl($request->input('tcurl'));
$key = $request->filled('name') ? $request->input('name') : $url['name'];
}
$token = substr($name, 0, 10) === 'streamkey-';
if($token) {
$stream = LiveStream::whereStreamKey($key)->firstOrFail();
return redirect($stream->getStreamRtmpUrl(), 301);
} else {
$stream = LiveStream::whereStreamId($key)->firstOrFail();
}
if($request->filled('name') && $token == false) {
$stream->live_at = now();
$stream->save();
return [];
} else {
abort(400);
}
abort(400);
}
public function clientBroadcastFinish(Request $request)
{
abort_if(!config('livestreaming.enabled'), 400);
abort_if(!$request->filled('tcurl'), 400);
$url = $this->parseStreamUrl($request->input('tcurl'));
$name = $url['name'] ?? $request->input('name');
$stream = LiveStream::whereStreamId($name)->whereStreamKey($url['key'])->firstOrFail();
if(config('livestreaming.broadcast.delete_token_after_finished')) {
$stream->delete();
} else {
$stream->live_at = null;
$stream->save();
}
return [];
}
protected function parseStreamUrl($url)
{
$name = null;
$key = null;
$query = parse_url($url, PHP_URL_QUERY);
$parts = explode('&', $query);
foreach($parts as $part) {
if (!strlen(trim($part))) {
continue;
}
$s = explode('=', $part);
if(in_array($s[0], ['name', 'key'])) {
if($s[0] === 'name') {
$name = $s[1];
}
if($s[0] === 'key') {
$key = $s[1];
}
}
}
return ['name' => $name, 'key' => $key];
}
} }

View File

@ -8,40 +8,34 @@ use Storage;
class LiveStream extends Model class LiveStream extends Model
{ {
use HasFactory; use HasFactory;
public function getHlsUrl() public function getHlsUrl()
{ {
$path = Storage::url("live-hls/{$this->stream_id}/index.m3u8"); $path = Storage::url("live-hls/{$this->stream_id}/index.m3u8");
return url($path); return url($path);
} }
public function getStreamKeyUrl() public function getStreamServer()
{ {
$proto = 'rtmp://'; $proto = 'rtmp://';
$host = config('livestreaming.server.host'); $host = config('livestreaming.server.host');
$port = ':' . config('livestreaming.server.port'); $port = ':' . config('livestreaming.server.port');
$path = '/' . config('livestreaming.server.path') . '?'; $path = '/' . config('livestreaming.server.path');
$query = http_build_query([ return $proto . $host . $port . $path;
'name' => $this->stream_id, }
'key' => $this->stream_key,
'ts' => time()
]);
return $proto . $host . $port . $path . $query; public function getStreamKeyUrl()
} {
$path = $this->getStreamServer() . '?';
$query = http_build_query([
'name' => $this->stream_key,
]);
return $path . $query;
}
public function getStreamRtmpUrl() public function getStreamRtmpUrl()
{ {
$proto = 'rtmp://'; return $this->getStreamServer() . '/' . $this->stream_id;
$host = config('livestreaming.server.host'); }
$port = ':' . config('livestreaming.server.port');
$path = '/' . config('livestreaming.server.path') . '/'. $this->stream_id . '?';
$query = http_build_query([
'key' => $this->stream_key,
'ts' => time()
]);
return $proto . $host . $port . $path . $query;
}
} }