Skip to content

Commit 6c59398

Browse files
Merge pull request #11 from innoflash/add-streaming-events
Add streaming events
2 parents 78ecabd + 82e3d13 commit 6c59398

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed

src/Events/VideoStreamEnded.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Iman\Streamer\Events;
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\Foundation\Events\Dispatchable;
11+
use Illuminate\Queue\SerializesModels;
12+
use Iman\Streamer\Video;
13+
14+
class VideoStreamEnded
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
/**
18+
* @var Video
19+
*/
20+
private $video;
21+
22+
/**
23+
* Create a new event instance.
24+
*
25+
* @param Video $video
26+
*/
27+
public function __construct(Video $video)
28+
{
29+
$this->video = $video;
30+
}
31+
32+
/**
33+
* @return Video
34+
*/
35+
public function getVideo(): Video
36+
{
37+
return $this->video;
38+
}
39+
40+
41+
/**
42+
* Get the channels the event should broadcast on.
43+
*
44+
* @return \Illuminate\Broadcasting\Channel|array
45+
*/
46+
public function broadcastOn()
47+
{
48+
return new PrivateChannel('channel-name');
49+
}
50+
}

src/Events/VideoStreamStarted.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Iman\Streamer\Events;
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\Foundation\Events\Dispatchable;
11+
use Illuminate\Queue\SerializesModels;
12+
use Iman\Streamer\Video;
13+
14+
class VideoStreamStarted
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
/**
18+
* @var Video
19+
*/
20+
private $video;
21+
22+
/**
23+
* Create a new event instance.
24+
*
25+
* @param Video $video
26+
*/
27+
public function __construct(Video $video)
28+
{
29+
$this->video = $video;
30+
info('video ended');
31+
}
32+
33+
/**
34+
* @return Video
35+
*/
36+
public function getVideo(): Video
37+
{
38+
return $this->video;
39+
}
40+
41+
42+
/**
43+
* Get the channels the event should broadcast on.
44+
*
45+
* @return \Illuminate\Broadcasting\Channel|array
46+
*/
47+
public function broadcastOn()
48+
{
49+
return new PrivateChannel('channel-name');
50+
}
51+
}

src/Video.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Iman\Streamer;
4+
5+
class Video
6+
{
7+
/**
8+
* Video path.
9+
*
10+
* @var string
11+
*/
12+
private $path;
13+
14+
/**
15+
* Video MIME type.
16+
*
17+
* @var string
18+
*/
19+
private $mime;
20+
21+
/**
22+
* Video size.
23+
*
24+
* @var int
25+
*/
26+
private $size;
27+
28+
/**
29+
* Video progress.
30+
*
31+
* @var int
32+
*/
33+
private $progress;
34+
35+
/**
36+
* @return string
37+
*/
38+
public function getPath(): string
39+
{
40+
return $this->path;
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
public function getMime(): string
47+
{
48+
return $this->mime;
49+
}
50+
51+
/**
52+
* @return int
53+
*/
54+
public function getSize(): int
55+
{
56+
return $this->size;
57+
}
58+
59+
/**
60+
* @param string $path
61+
*/
62+
public function setPath(string $path): void
63+
{
64+
$this->path = $path;
65+
66+
$this->setMime(mime_content_type($path));
67+
68+
$this->setSize(filesize($path));
69+
}
70+
71+
/**
72+
* @param string $mime
73+
*/
74+
private function setMime(string $mime): void
75+
{
76+
$this->mime = $mime;
77+
}
78+
79+
/**
80+
* @param int $size
81+
*/
82+
private function setSize(int $size): void
83+
{
84+
$this->size = $size;
85+
}
86+
87+
/**
88+
* @return int
89+
*/
90+
public function getProgress(): int
91+
{
92+
return $this->progress;
93+
}
94+
95+
/**
96+
* @param int $progress
97+
*/
98+
public function setProgress(int $progress): void
99+
{
100+
$this->progress = $progress;
101+
}
102+
}

src/VideoStreamer.php

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

33
namespace Iman\Streamer;
44

5+
use Iman\Streamer\Events\VideoStreamEnded;
6+
use Iman\Streamer\Events\VideoStreamStarted;
7+
58
class VideoStreamer
69
{
710
private $path = '';
@@ -18,11 +21,19 @@ class VideoStreamer
1821

1922
private $mime = '';
2023

24+
/**
25+
* @var Video
26+
*/
27+
private $video;
28+
2129
public function __construct($filePath, $stream)
2230
{
2331
$this->path = $filePath;
2432
$this->stream = $stream;
2533
$this->mime = mime_content_type($filePath);
34+
35+
$this->video = new Video();
36+
$this->video->setPath($this->path);
2637
}
2738

2839
/**
@@ -87,6 +98,7 @@ private function setHeader()
8798
private function end()
8899
{
89100
fclose($this->stream);
101+
event(new VideoStreamEnded($this->video));
90102
exit;
91103
}
92104

@@ -95,9 +107,15 @@ private function end()
95107
*/
96108
private function stream()
97109
{
110+
$this->video->setProgress($this->start);
111+
event(new VideoStreamStarted($this->video));
112+
98113
$i = $this->start;
99114
set_time_limit(0);
100115
while (! feof($this->stream) && $i <= $this->end) {
116+
117+
$this->video->setProgress($i);
118+
101119
$bytesToRead = $this->buffer;
102120
if (($i + $bytesToRead) > $this->end) {
103121
$bytesToRead = $this->end - $i + 1;

0 commit comments

Comments
 (0)