forked from qwantix/php-rtmp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RtmpClient.class.php
executable file
·539 lines (453 loc) · 12.6 KB
/
RtmpClient.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
require "RtmpPacket.class.php";
require "RtmpStream.class.php";
require "RtmpMessage.class.php";
require "RtmpOperation.class.php";
require "RtmpSocket.class.php";
class RTMPClient
{
const RTMP_SIG_SIZE = 1536;
/**
* Socket object
*
* @var RtmpSocket
*/
private $socket;
private $host;
private $application;
private $port;
private $chunkSizeR = 128, $chunkSizeW = 128;
private $operations = array();
private $connected = false;
private $client;
public function setClient($client)
{
if(is_object($client) || is_null($client))
$this->client = $client;
}
/**
* Connect
*
* @param string $host
* @param string $application
* @param int $port
*/
public function connect($host,$application,$port = 1935, $connectParams=null)
{
$this->close();
$this->host = $host;
$this->application = $application;
$this->port = $port;
if($this->initSocket())
{
$aReadSockets = array($this->socket);
$this->handshake();
$this->send_ConnectPacket($connectParams);
}
}
/**
* Close connection
*
*/
public function close()
{
$this->socket && $this->socket->close();
$this->chunkSizeR = $this->chunkSizeW = 128;
}
/**
* Call remote procedure (RPC)
*
* @param string $procedureName
* @param array $args array of arguments, null if not args
* @param callback $handler
*
* @return mixed result of RPC
*/
public function call($procedureName,array $args = null,$handler = null)
{
return $this->sendOperation(new RtmpOperation(new RtmpMessage($procedureName,null,$args), $handler));
}
public function __call($name, $arguments)
{
return $this->call($name, $arguments);
}
//------------------------------------
// Socket
//------------------------------------
private function initSocket()
{
$this->socket = new RtmpSocket();
return $this->socket->connect($this->host, $this->port);
}
private function socketRead($length)
{
return $this->socket->read($length);
}
private function socketWrite(RtmpStream $data, $n = -1)
{
return $this->socket->write($data, $n);
}
//-------------------------------------
private $listening = false;
/**
* listen socket
*
* @return mixed last result
*/
private function listen()
{
if($this->listening)
return;
if(!$this->socket)
return;
$this->listening = true;
$stop = false;
$return = null;
while (!$stop)
{
if($p = $this->readPacket())
{
switch($p->type)
{
case 0x01; //Chunk size
$this->handle_setChunkSize($p);
break;
case 0x03: //Bytes Read
break;
case 0x04: //Ping
unset($this->operations[$p->chunkStreamId]);
break;
case 0x05: //Window Acknowledgement Size
$this->handle_windowAcknowledgementSize($p);
break;
case 0x06: //Peer BW
$this->handle_setPeerBandwidth($p);
break;
case 0x08: //Audio Data
break;
case 0x09: //Video Data
break;
case 0x12: //Notify
break;
case RtmpPacket::TYPE_INVOKE_AMF0:
case RtmpPacket::TYPE_INVOKE_AMF3: //Invoke
$return = $this->handle_invoke($p);
if(sizeof($this->operations) == 0)
$stop = true;
break;
case 0x16:
break;
case 0x36: //agregate
break;
default:
break;
}
}
usleep(1);
}
$this->listening = false;
return $return;
}
/**
* Previous packet
* @internal
*
* @var RtmpPacket
*/
private $prevReadingPacket = array();
/**
* Read packet
*
* @return RtmpPacket
*/
private function readPacket()
{
$p = new RtmpPacket();
$header = $this->socketRead(1)->readTinyInt();
$p->chunkType = (($header & 0xc0) >> 6);
$p->chunkStreamId = $header & 0x3f;
switch($p->chunkStreamId)
{
case 0: //range of 64-319, second byte + 64
$p->chunkStreamId = 64 + $this->socketRead(1)->readTinyInt();
break;
case 1: //range of 64-65599,thrid byte * 256 + second byte + 64
$p->chunkStreamId = 64 + $this->socketRead(1)->readTinyInt() + $this->socketRead(1)->readTinyInt()*256;
break;
case 2:
break;
default: //range of 3-63
// complete stream ids
}
switch($p->chunkType)
{
case RtmpPacket::CHUNK_TYPE_3:
$p->timestamp = $this->prevReadingPacket[$p->chunkStreamId]->timestamp;
case RtmpPacket::CHUNK_TYPE_2:
$p->length = $this->prevReadingPacket[$p->chunkStreamId]->length;
$p->type = $this->prevReadingPacket[$p->chunkStreamId]->type;
case RtmpPacket::CHUNK_TYPE_1:
$p->streamId = $this->prevReadingPacket[$p->chunkStreamId]->streamId;
case RtmpPacket::CHUNK_TYPE_0:
break;
}
$this->prevReadingPacket[$p->chunkStreamId] = $p;
$headerSize = RtmpPacket::$SIZES[$p->chunkType];
if($headerSize == RtmpPacket::MAX_HEADER_SIZE)
$p->hasAbsTimestamp = true;
//If not operation exists, create it
if(!isset($this->operations[$p->chunkStreamId]))
$this->operations[$p->chunkStreamId] = new RtmpOperation();
if($this->operations[$p->chunkStreamId]->getResponse())
{
//Operation chunking....
$p = $this->operations[$p->chunkStreamId]->getResponse()->getPacket();
$headerSize = 0; //no header
}
else
{
//Create response from packet
$this->operations[$p->chunkStreamId]->createResponse($p);
}
$headerSize--;
$header;
if($headerSize>0)
$header = $this->socketRead($headerSize);
if($headerSize >= 3)
$p->timestamp = $header->readInt24();
if($headerSize >= 6)
{
$p->length = $header->readInt24();
$p->bytesRead = 0;
$p->free();
}
if($headerSize > 6)
$p->type = $header->readTinyInt();
if($headerSize == 11)
$p->streamId = $header->readInt32LE();
$nToRead = $p->length - $p->bytesRead;
$nChunk = $this->chunkSizeR;
if($nToRead < $nChunk)
$nChunk = $nToRead;
if($p->payload == null)
$p->payload = "";
$p->payload .= $this->socketRead($nChunk)->flush();
if($p->bytesRead + $nChunk != strlen($p->payload))
throw new Exception("Read failed, have read ".strlen($p->payload)." of ".($p->bytesRead + $nChunk));
$p->bytesRead += $nChunk;
if($p->isReady())
return $p;
return null;
}
/**
* Previous packet
* @internal
*
* @var RtmpPacket
*/
private $prevSendingPacket = array();
/**
* Send packet
*
* @param RtmpPacket $packet
* @return bool
*/
private function sendPacket(RtmpPacket $packet)
{
if(!$packet->length)
$packet->length = strlen($packet->payload);
if(isset($this->prevSendingPacket[$packet->chunkStreamId]))
{
if($packet->length == $this->prevSendingPacket[$packet->chunkStreamId]->length)
$packet->chunkType = RtmpPacket::CHUNK_TYPE_2;
else
$packet->chunkType = RtmpPacket::CHUNK_TYPE_1;
}
if($packet->chunkType > 3) //sanity
throw new Exception("sanity failed!! tring to send header of type: 0x%02x");
$this->prevSendingPacket[$packet->chunkStreamId] = $packet;
$headerSize = RtmpPacket::$SIZES[$packet->chunkType];
//Initialize header
$header = new RtmpStream();
$header->writeByte($packet->chunkType << 6 | $packet->chunkStreamId);
if($headerSize > 1)
{
$packet->timestamp = time();
$header->writeInt24($packet->timestamp);
}
if($headerSize > 4)
{
$header->writeInt24($packet->length);
$header->writeByte($packet->type);
}
if($headerSize > 8)
$header->writeInt32LE($packet->streamId);
// Send header
$this->socketWrite($header);
$headerSize = $packet->length;
$buffer = new RtmpStream($packet->payload);
while($headerSize)
{
$chunkSize = $packet->type == RtmpPacket::TYPE_INVOKE_AMF0 || $packet->type == RtmpPacket::TYPE_INVOKE_AMF3 ? $this->chunkSizeW : $packet->length;
if($headerSize < $this->chunkSizeW)
$chunkSize = $headerSize;
if(!$this->socketWrite($buffer, $chunkSize))
throw new Exception("Socket write error (write : $chunkSize)");
$headerSize -= $chunkSize;
//$buffer = substr($buffer,$chunkSize);
if($headerSize > 0)
{
$sep = (0xc0 | $packet->chunkStreamId);
if(!$this->socketWrite(new RtmpStream(chr($sep)),1))
return false;
}
}
return true;
}
protected function sendOperation(RtmpOperation $op)
{
$this->operations[$op->getChunkStreamID()] = $op;
$this->sendPacket($op->getCall()->getPacket());
return $this->listen();
}
//------------------------------------
// RTMP Methods
//------------------------------------
/**
* Perform handshake
*
*/
private function handshake()
{
/// Send C0, the version
$stream = new RtmpStream();
$stream->writeByte("\x03"); //"\x03";
$this->socketWrite($stream);
/// Send C1
$ctime = time();
$stream->writeInt32($ctime); //Time
$stream->write("\x80\x00\x03\x02"); //Zero zone? Flex put : 0x80 0x00 0x03 0x02, maybe new handshake style?
$crandom = "";
for($i=0; $i<self::RTMP_SIG_SIZE - 8; $i++)
$crandom .= chr(rand(0,256)); //TODO: better method to randomize
$stream->write($crandom);
$this->socketWrite($stream);
///Read S0
$s0 = $this->socketRead(1)->readTinyInt();
if($s0 != 0x03)
throw new Exception("Packet version ".$s0." not supported");
///Read S1
$s1 = $this->socketRead(self::RTMP_SIG_SIZE);
///Send C2
$c2 = new RtmpStream();
$c2->writeInt32($s1->readInt32());
$s1->readInt32();
$c2->writeInt32($ctime);
$raw = $s1->readRaw();
$c2->write($raw);
$this->socketWrite($c2);
///Read S2
$resp = $this->socketRead(self::RTMP_SIG_SIZE);
//TODO check integrity
return true;
}
private function send_ConnectPacket($connectParams=null)
{
$this->sendOperation(
new RtmpOperation(new RtmpMessage("connect",(object)array(
"app" => $this->application,
"flashVer" => "LNX 10,0,32,18",
"swfUrl" => null,
"tcUrl" => "rtmp://$this->host:$this->port/$this->application",
"fpad" => false,
"capabilities" => 0.0,
"audioCodecs" => 0x01,
"videoCodecs" => 0xFF,
"videoFunction" => 0,
"pageUrl" => null,
"objectEncoding" => 0x03
), $connectParams), array($this,"onConnect"))
);
}
private function send_SetChunkSize()
{
}
private function send_AbortMessage()
{
}
private function send_Acknowledgement()
{
}
private function send_UserControlMessage()
{
}
private function send_WindowAcknowledgementSize()
{
}
private function send_SetPeerBandwidth()
{
}
private function handle_windowAcknowledgementSize(RtmpPacket $p)
{
$s = new RtmpStream($p->payload);
$size = $s->readInt32();
//TODO
unset($this->operations[$p->chunkStreamId]);
}
private function handle_setPeerBandwidth(RtmpPacket $p)
{
$s = new RtmpStream($p->payload);
$size = $s->readInt32();
$limitType = $s->readTinyInt();
//TODO
unset($this->operations[$p->chunkStreamId]);
}
private function handle_setChunkSize(RtmpPacket $p)
{
$s = new RtmpStream($p->payload);
$this->chunkSizeR = $s->readInt32();
unset($this->operations[$p->chunkStreamId]);
}
private function handle_invoke(RtmpPacket $p)
{
$op = $this->operations[$p->chunkStreamId];
$op->getResponse()->decode($p);
if($op->getCall() && $op->getResponse()->isResponseCommand() )
{
//Result
unset($this->operations[$p->chunkStreamId]);
$op->invokeHandler();
$data = $op->getResponse()->arguments instanceof SabreAMF_AMF3_Wrapper ? $op->getResponse()->arguments->getData() : $op->getResponse()->arguments;
if($op->getResponse()->isError())
{
$data = (object)$data;
throw new Exception($data->description . (isset($data->application)&&!empty($data->application)?" (Application specific message: {$data->application})":''));
}
return $data;
}
else
{
//Remote invoke from server
$h = $op->getResponse()->commandName;
if($this->client)
$h = array($this->client,$h);
is_callable($h) && call_user_func_array($h,$op->getResponse()->arguments);
$op->clearResponse();
return;
}
}
//------------------------------------
// Internal handlers
//------------------------------------
/**
* On connect handler
* @internal
* @param RtmpMessage $m
*
*/
public function onConnect(RtmpOperation $m)
{
$this->connected = true;
unset($this->prevSendingPacket[$m->getResponse()->getPacket()->chunkStreamId]);
}
}