Skip to content

Commit af389d3

Browse files
authored
Merge pull request #1944 from danog/perf-nal-strpos
Find H.264/HEVC start codes with strpos
2 parents 95624d6 + c2a1c1d commit af389d3

3 files changed

Lines changed: 19 additions & 29 deletions

File tree

‎src/API.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ final class API extends AbstractAPI
5454
*
5555
* @var string
5656
*/
57-
public const RELEASE = '8.15.4';
57+
public const RELEASE = '8.15.5';
5858
/**
5959
* We're not logged in.
6060
*

‎src/Tgcalls/CallRecorder.php‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -871,24 +871,19 @@ private static function h264Nals(string $frame): array
871871
{
872872
$nals = [];
873873
$len = \strlen($frame);
874-
$i = 0;
875874
$start = -1;
876-
while ($i < $len) {
877-
// Find a start code.
878-
if ($i + 3 <= $len && $frame[$i] === "\x00" && $frame[$i + 1] === "\x00" && $frame[$i + 2] === "\x01") {
879-
if ($start >= 0) {
880-
$end = $i;
881-
// Trim a trailing zero belonging to the next 4-byte start code.
882-
if ($end > $start && $frame[$end - 1] === "\x00") {
883-
$end--;
884-
}
885-
$nals[] = substr($frame, $start, $end - $start);
875+
// Every frame the RTMP publisher sends goes through here: search the start codes with strpos, comparing
876+
// byte by byte in PHP took a tenth of magnaluna's CPU.
877+
for ($i = strpos($frame, "\x00\x00\x01"); $i !== false; $i = strpos($frame, "\x00\x00\x01", $start)) {
878+
if ($start >= 0) {
879+
$end = $i;
880+
// Trim a trailing zero belonging to the next 4-byte start code.
881+
if ($end > $start && $frame[$end - 1] === "\x00") {
882+
$end--;
886883
}
887-
$i += 3;
888-
$start = $i;
889-
continue;
884+
$nals[] = substr($frame, $start, $end - $start);
890885
}
891-
$i++;
886+
$start = $i + 3;
892887
}
893888
if ($start >= 0 && $start < $len) {
894889
$nals[] = substr($frame, $start);

‎src/Tgcalls/HevcBitstream.php‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,17 @@ public static function nalUnits(string $frame): array
145145
{
146146
$nals = [];
147147
$len = \strlen($frame);
148-
$i = 0;
149148
$start = -1;
150-
while ($i + 3 <= $len) {
151-
if ($frame[$i] === "\x00" && $frame[$i + 1] === "\x00" && $frame[$i + 2] === "\x01") {
152-
if ($start >= 0) {
153-
$end = $i;
154-
if ($end > $start && $frame[$end - 1] === "\x00") {
155-
$end--;
156-
}
157-
$nals[] = substr($frame, $start, $end - $start);
149+
// strpos, not a byte by byte comparison in PHP: this runs on every frame sent over RTMP.
150+
for ($i = strpos($frame, "\x00\x00\x01"); $i !== false; $i = strpos($frame, "\x00\x00\x01", $start)) {
151+
if ($start >= 0) {
152+
$end = $i;
153+
if ($end > $start && $frame[$end - 1] === "\x00") {
154+
$end--;
158155
}
159-
$i += 3;
160-
$start = $i;
161-
continue;
156+
$nals[] = substr($frame, $start, $end - $start);
162157
}
163-
$i++;
158+
$start = $i + 3;
164159
}
165160
if ($start >= 0 && $start <= $len) {
166161
$nals[] = substr($frame, $start);

0 commit comments

Comments
 (0)