mirror of
https://github.com/pixelfed/pixelfed.git
synced 2025-03-04 10:39:08 +00:00
Update LiveStream model
This commit is contained in:
parent
4ff179ad4d
commit
494f11202f
2 changed files with 280 additions and 207 deletions
|
@ -46,11 +46,13 @@ class LiveStreamController extends Controller
|
||||||
$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 [
|
||||||
|
'host' => $stream->getStreamServer(),
|
||||||
|
'key' => $stream->stream_key,
|
||||||
'url' => $stream->getStreamKeyUrl(),
|
'url' => $stream->getStreamKeyUrl(),
|
||||||
'id' => $stream->stream_id
|
'id' => $stream->stream_id
|
||||||
];
|
];
|
||||||
|
@ -143,8 +145,8 @@ class LiveStreamController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
$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;
|
||||||
|
@ -233,4 +235,81 @@ class LiveStreamController extends Controller
|
||||||
|
|
||||||
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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,32 +16,26 @@ class LiveStream extends Model
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue