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

@ -46,11 +46,13 @@ class LiveStreamController extends Controller
$stream->description = $request->input('description');
$stream->visibility = $request->input('visibility');
$stream->profile_id = $request->user()->profile_id;
$stream->stream_id = Str::random(40);
$stream->stream_key = Str::random(64);
$stream->stream_id = Str::random(40) . '_' . $stream->profile_id;
$stream->stream_key = 'streamkey-' . Str::random(64);
$stream->save();
return [
'host' => $stream->getStreamServer(),
'key' => $stream->stream_key,
'url' => $stream->getStreamKeyUrl(),
'id' => $stream->stream_id
];
@ -143,8 +145,8 @@ class LiveStreamController extends Controller
}
$res = collect(LiveStreamService::getComments($stream->profile_id))
->map(function($r) {
return json_decode($r);
->map(function($res) {
return json_decode($res);
});
return $res;
@ -233,4 +235,81 @@ class LiveStreamController extends Controller
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

@ -16,32 +16,26 @@ class LiveStream extends Model
return url($path);
}
public function getStreamKeyUrl()
public function getStreamServer()
{
$proto = 'rtmp://';
$host = config('livestreaming.server.host');
$port = ':' . config('livestreaming.server.port');
$path = '/' . config('livestreaming.server.path') . '?';
$query = http_build_query([
'name' => $this->stream_id,
'key' => $this->stream_key,
'ts' => time()
]);
$path = '/' . config('livestreaming.server.path');
return $proto . $host . $port . $path;
}
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()
{
$proto = 'rtmp://';
$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;
return $this->getStreamServer() . '/' . $this->stream_id;
}
}