-
Notifications
You must be signed in to change notification settings - Fork 26
/
RtmpStream.class.php
executable file
·138 lines (129 loc) · 2.56 KB
/
RtmpStream.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
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
class RtmpStream
{
private $_index = 0;
private $_data;
public function __construct($data = "")
{
$this->_data = $data;
}
public function reset()
{
$this->_index = 0;
}
public function flush($length = -1)
{
if($length == -1)
{
$d = $this->_data;
$this->_data = "";
}
else
{
$d = substr($this->_data,0,$length);
$this->_data = substr($this->_data,$length);
}
$this->_index = 0;
return $d;
}
public function dump()
{
return $this->_data;
}
public function begin()
{
$this->_index = 0;
return $this;
}
public function move($pos)
{
$this->_index = max(array(0,min(array($pos,strlen($data)))));
return $this;
}
public function end()
{
$this->_index = strlen($this->_data);
return $this;
}
public function push($data)
{
$this->_data .= $data;
return $this;
}
//--------------------------------
// Writer
//--------------------------------
public function writeByte($value)
{
$this->_data .= is_int($value)?chr($value):$value;
$this->_index++;
}
public function writeInt16($value)
{
$this->_data .= pack("s",$value);
$this->_index += 2;
}
public function writeInt24($value)
{
$this->_data .= substr(pack("N",$value),1);
$this->_index += 3;
}
public function writeInt32($value)
{
$this->_data .= pack("N",$value);
$this->_index += 4;
}
public function writeInt32LE($value)
{
$this->_data .= pack("V",$value);
$this->_index += 4;
}
public function write($value)
{
$this->_data .= $value;
$this->_index += strlen($value);
}
//-------------------------------
// Reader
//-------------------------------
public function readByte()
{
return ($this->_data[$this->_index++]);
}
public function readTinyInt()
{
return ord($this->readByte());
}
public function readInt16()
{
return $this->read("s",2);
}
public function readInt24()
{
$m = unpack("N","\x00".substr($this->_data,$this->_index,3));
$this->_index += 3;
return $m[1];
}
public function readInt32()
{
return $this->read("N",4);
}
public function readInt32LE()
{
return $this->read("V",4);
}
public function readRaw($length = 0)
{
if($length == 0)
$length = strlen($this->_data) - $this->_index;
$datas = substr($this->_data,$this->_index,$length);
$this->_index += $length;
return $datas;
}
private function read($type, $size)
{
$m = unpack("$type",substr($this->_data,$this->_index,$size));
$this->_index += $size;
return $m[1];
}
}