Skip to content

Commit 197dad9

Browse files
committed
Broadcast: Mensagens via Websocket para Informativos dos Processos do Video
1 parent 914f9c4 commit 197dad9

File tree

18 files changed

+1647
-34
lines changed

18 files changed

+1647
-34
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Events\VideoFlix;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Broadcasting\PresenceChannel;
8+
use Illuminate\Broadcasting\PrivateChannel;
9+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
11+
use Illuminate\Foundation\Events\Dispatchable;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class EncodeVideoFinished implements ShouldBroadcastNow
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
/**
19+
* Create a new event instance.
20+
*/
21+
public function __construct(private int $videoId)
22+
{
23+
//
24+
}
25+
26+
public function broadcastWith(): array
27+
{
28+
return [
29+
'videoId' => $this->videoId
30+
];
31+
}
32+
33+
/**
34+
* Get the channels the event should broadcast on.
35+
*
36+
* @return array<int, \Illuminate\Broadcasting\Channel>
37+
*/
38+
public function broadcastOn(): array
39+
{
40+
return [
41+
new Channel('videos'),
42+
];
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Events\VideoFlix;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Broadcasting\PresenceChannel;
8+
use Illuminate\Broadcasting\PrivateChannel;
9+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
11+
use Illuminate\Foundation\Events\Dispatchable;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class EncodeVideoProgress implements ShouldBroadcastNow
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
/**
19+
* Create a new event instance.
20+
*/
21+
public function __construct(private int $videoId, private int $progress)
22+
{
23+
//
24+
}
25+
26+
public function broadcastWith(): array
27+
{
28+
return [
29+
'videoId' => $this->videoId,
30+
'progress' => $this->progress
31+
];
32+
}
33+
34+
/**
35+
* Get the channels the event should broadcast on.
36+
*
37+
* @return array<int, \Illuminate\Broadcasting\Channel>
38+
*/
39+
public function broadcastOn(): array
40+
{
41+
return [
42+
new Channel('videos'),
43+
];
44+
}
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Events\VideoFlix;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Broadcasting\PresenceChannel;
8+
use Illuminate\Broadcasting\PrivateChannel;
9+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
11+
use Illuminate\Foundation\Events\Dispatchable;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class EncodeVideoStarted implements ShouldBroadcastNow
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
/**
19+
* Create a new event instance.
20+
*/
21+
public function __construct(private int $videoId)
22+
{
23+
//
24+
}
25+
26+
public function broadcastWith(): array
27+
{
28+
return [
29+
'videoId' => $this->videoId
30+
];
31+
}
32+
33+
/**
34+
* Get the channels the event should broadcast on.
35+
*
36+
* @return array<int, \Illuminate\Broadcasting\Channel>
37+
*/
38+
public function broadcastOn(): array
39+
{
40+
return [
41+
new Channel('videos'),
42+
];
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Events\VideoFlix;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Broadcasting\PresenceChannel;
8+
use Illuminate\Broadcasting\PrivateChannel;
9+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
11+
use Illuminate\Foundation\Events\Dispatchable;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class VideoThumbGenerated implements ShouldBroadcastNow
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
/**
19+
* Create a new event instance.
20+
*/
21+
public function __construct(private int $videoId, private string $thumb)
22+
{
23+
//
24+
}
25+
26+
public function broadcastWith(): array
27+
{
28+
return [
29+
'videoId' => $this->videoId,
30+
'thumb' => $this->thumb
31+
];
32+
}
33+
34+
/**
35+
* Get the channels the event should broadcast on.
36+
*
37+
* @return array<int, \Illuminate\Broadcasting\Channel>
38+
*/
39+
public function broadcastOn(): array
40+
{
41+
return [
42+
new Channel('videos'),
43+
];
44+
}
45+
}

app/Http/Controllers/Media/VideosController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Jobs\VideoEncodingJob;
7+
use App\Jobs\VideoThumbGenerateJob;
78
use App\Models\Content;
89
use Illuminate\Http\Request;
910
use Illuminate\Http\UploadedFile;
@@ -68,6 +69,7 @@ public function processChunck(Content $content, $video, Request $request)
6869
'video' => $save->getFile()->storeAs('', str()->uuid(), 'videos')
6970
]);
7071

72+
dispatch(new VideoThumbGenerateJob($video));
7173
dispatch(new VideoEncodingJob($video));
7274
}
7375

app/Jobs/VideoEncodingJob.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace App\Jobs;
44

5+
use App\Events\VideoFlix\EncodeVideoFinished;
6+
use App\Events\VideoFlix\EncodeVideoProgress;
7+
use App\Events\VideoFlix\EncodeVideoStarted;
58
use App\Models\Video;
69
use FFMpeg\Format\Video\X264;
710
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -26,7 +29,7 @@ public function __construct(private Video $video)
2629
*/
2730
public function handle(): void
2831
{
29-
//Dispararia um evento de inicializacao do encoding
32+
event(new EncodeVideoStarted($this->video->id));
3033

3134
$videoNewName = str_replace(strrchr($this->video->video, '.'), '', $this->video->video) . '.m3u8';
3235

@@ -42,7 +45,7 @@ public function handle(): void
4245
->addFormat($mid)
4346
->addFormat($high)
4447
->onProgress(function($progress){
45-
//Dispararia um evento via broadcast com as infos do progresso
48+
event(new EncodeVideoProgress($this->video->id, $progress));
4649
})
4750
->toDisk('videos_encoded')
4851
->save($this->video->code . '/' . $videoNewName);
@@ -54,6 +57,6 @@ public function handle(): void
5457
'is_processed' => true
5558
]);
5659

57-
//Dispararia um evento de finalizacao do encoding
60+
event(new EncodeVideoFinished($this->video->id));
5861
}
5962
}

app/Jobs/VideoThumbGenerateJob.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Events\VideoFlix\VideoThumbGenerated;
6+
use App\Models\Video;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Queue\Queueable;
9+
use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
10+
11+
class VideoThumbGenerateJob implements ShouldQueue
12+
{
13+
use Queueable;
14+
15+
/**
16+
* Create a new job instance.
17+
*/
18+
public function __construct(private Video $video)
19+
{
20+
//
21+
}
22+
23+
/**
24+
* Execute the job.
25+
*/
26+
public function handle(): void
27+
{
28+
$thumb = 'thumbs/' . $this->video->code . '/thumb.png';
29+
30+
FFMpeg::fromDisk('videos')
31+
->open($this->video->video)
32+
->getFrameFromSeconds(5)
33+
->export()
34+
->toDisk('public')
35+
->save($thumb);
36+
37+
$this->video->update([
38+
'thumb' => $thumb
39+
]);
40+
41+
event(new VideoThumbGenerated($this->video->id, $thumb));
42+
}
43+
}

bootstrap/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
->withRouting(
1212
web: __DIR__.'/../routes/web.php',
1313
commands: __DIR__.'/../routes/console.php',
14+
channels: __DIR__.'/../routes/channels.php',
1415
health: '/up',
1516
)
1617
->withMiddleware(function (Middleware $middleware) {

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"inertiajs/inertia-laravel": "^2.0",
1414
"laravel/fortify": "^1.30",
1515
"laravel/framework": "^12.0",
16+
"laravel/reverb": "^1.0",
1617
"laravel/tinker": "^2.10.1",
1718
"laravel/wayfinder": "^0.1.9",
1819
"pbmedia/laravel-ffmpeg": "^8.7",

0 commit comments

Comments
 (0)