forked from qwantix/php-rtmp-client
/
RtmpMessage.class.php
executable file
·125 lines (111 loc) · 3.22 KB
/
RtmpMessage.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
require_once 'SabreAMF/OutputStream.php';
require_once 'SabreAMF/InputStream.php';
require_once 'SabreAMF/AMF0/Serializer.php';
require_once 'SabreAMF/AMF0/Deserializer.php';
class RtmpMessage
{
private static $currentTransactionID = 0;
public $commandName;
public $transactionId;
public $commandObject;
public $arguments;
private $packet;
public function __construct($commandName = "",$commandObject = null,$arguments = null)
{
$this->commandName = $commandName;
$this->commandObject = $commandObject;
$this->arguments = $arguments;
}
/**
* getPacket
*
* @return RtmpPacket
*/
public function getPacket()
{
return $this->packet;
}
public function setPacket($packet)
{
$this->packet = $packet;
}
/**
* Encode Message
*
* @param int $amfVersion
* @return RtmpPacket
*/
public function encode()
{
$amfVersion = 3; //Using AMF3
//Increment transaction id
$this->transactionId = self::$currentTransactionID++;
//Create packet
$p = new RtmpPacket();
if($this->commandName == "connect")
{
$this->transactionId = 1;
$amfVersion = 0; //Connect packet must be in AMF0
}
$p->chunkStreamId = 3;
$p->streamId = 0;
$p->chunkType = RtmpPacket::CHUNK_TYPE_0;
$p->type = $amfVersion == 0 ? RtmpPacket::TYPE_INVOKE_AMF0 : RtmpPacket::TYPE_INVOKE_AMF3 ; //Invoke
//Encoding payload
$stream = new SabreAMF_OutputStream();
$serializer = new SabreAMF_AMF0_Serializer($stream);
$serializer->writeAMFData($this->commandName);
$serializer->writeAMFData($this->transactionId);
$serializer->writeAMFData($this->commandObject);
if($this->arguments != null)
foreach($this->arguments as $arg)
$serializer->writeAMFData($arg);
$p->payload = '';
if($amfVersion == 3)
$p->payload = "\x00"; //XXX: put empty bytes in amf3 mode...I don't know why..*/
$p->payload .= $stream->getRawData();
$this->packet = $p;
return $p;
}
public function decode(RtmpPacket $p)
{
$this->packet = $p;
$amfVersion = $p->type == RtmpPacket::TYPE_INVOKE_AMF0?0:3;
if($amfVersion==3 && $p->payload{0}==chr(0))
{
$p->payload = substr($p->payload,1);
$amfVersion = 0;
}
$stream = new SabreAMF_InputStream($p->payload);
$deserializer = $amfVersion == 0 ? new SabreAMF_AMF0_Deserializer($stream) : new SabreAMF_AMF3_Deserializer($stream);
$this->commandName = $deserializer->readAMFData();
$this->transactionId = $deserializer->readAMFData();
$this->commandObject = $deserializer->readAMFData();
try
{
$this->arguments = $deserializer->readAMFData();
}
catch(Exception $e)
{
//if not exists InputStream throw exeception
$this->arguments = null;
}
if(($this->commandName == "_error") || (is_array($this->arguments) && !empty($this->arguments) && isset($this->arguments['level']) && ($this->arguments['level']=='error')))
$this->_isError = true;
}
private $_isError = false;
public function isError()
{
return $this->_isError;
}
/**
* Return if message is a response message
*
* @return bool
*/
public function isResponseCommand()
{
return $this->commandName == "_result" || $this->commandName == "_error";
}
}