1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\FFMpeg; |
4
|
|
|
|
5
|
|
|
use Alchemy\BinaryDriver\Listeners\ListenerInterface; |
6
|
|
|
use Evenement\EventEmitter; |
7
|
|
|
use ProtoneMedia\LaravelFFMpeg\Support\ProcessOutput; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
class StdListener extends EventEmitter implements ListenerInterface |
11
|
|
|
{ |
12
|
|
|
const TYPE_ALL = 'all'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Name of the emitted event. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $eventName; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Container for the outputted lines. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $data = [ |
27
|
|
|
self::TYPE_ALL => [], |
28
|
|
|
Process::ERR => [], |
29
|
|
|
Process::OUT => [], |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
public function __construct(string $eventName = 'listen') |
33
|
|
|
{ |
34
|
|
|
$this->eventName = $eventName; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Handler for a new line of data. |
39
|
|
|
* |
40
|
|
|
* @param string $type |
41
|
|
|
* @param string $data |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function handle($type, $data) |
45
|
|
|
{ |
46
|
|
|
$lines = preg_split('/\n|\r\n?/', $data); |
47
|
|
|
|
48
|
|
|
foreach ($lines as $line) { |
49
|
|
|
$line = trim($line); |
50
|
|
|
|
51
|
|
|
$this->data[$type][] = $line; |
52
|
|
|
|
53
|
|
|
$this->data[static::TYPE_ALL][] = $line; |
54
|
|
|
|
55
|
|
|
$this->emit($this->eventName, [$line, $type]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the collected output lines. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function get(): ProcessOutput |
65
|
|
|
{ |
66
|
|
|
return new ProcessOutput( |
67
|
|
|
$this->data[static::TYPE_ALL], |
68
|
|
|
$this->data[Process::ERR], |
69
|
|
|
$this->data[Process::OUT] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function forwardedEvents() |
74
|
|
|
{ |
75
|
|
|
return [$this->eventName]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|