API
1 month ago
Blocks
10 months ago
License
1 month ago
AdminNotice.php
3 weeks ago
AdminNotices.php
3 weeks ago
AjaxActions.php
3 weeks ago
Blocks.php
1 year ago
Compatibility.php
10 months ago
Learn.php
1 month ago
Menu.php
1 month ago
Migrations.php
1 year ago
NpsSurvey.php
5 months ago
Onboarding.php
3 weeks ago
Player.php
3 days ago
PluginInstaller.php
1 month ago
PreloadService.php
1 year ago
ProCompatibility.php
1 month ago
ReusableVideos.php
2 weeks ago
RewriteRulesManager.php
1 year ago
Scripts.php
2 weeks ago
Settings.php
1 month ago
Shortcodes.php
1 month ago
Streamer.php
3 weeks ago
Translation.php
3 weeks ago
Usage.php
3 weeks ago
VideoPostType.php
2 weeks ago
Streamer.php
188 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Byte-range video streamer. |
| 4 | * |
| 5 | * @package PrestoPlayer |
| 6 | */ |
| 7 | |
| 8 | namespace PrestoPlayer\Services; |
| 9 | |
| 10 | /** |
| 11 | * Streams a file to the browser with HTTP byte-range support. |
| 12 | */ |
| 13 | class Streamer { |
| 14 | |
| 15 | /** |
| 16 | * Path to the file being streamed. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | private $path = ''; |
| 21 | |
| 22 | /** |
| 23 | * Open file stream resource. |
| 24 | * |
| 25 | * @var resource|false|string |
| 26 | */ |
| 27 | private $stream = ''; |
| 28 | |
| 29 | /** |
| 30 | * Number of bytes to read per chunk. |
| 31 | * |
| 32 | * @var int |
| 33 | */ |
| 34 | private $buffer = 0; |
| 35 | |
| 36 | /** |
| 37 | * Start byte of the requested range. |
| 38 | * |
| 39 | * @var int |
| 40 | */ |
| 41 | private $start = -1; |
| 42 | |
| 43 | /** |
| 44 | * End byte of the requested range. |
| 45 | * |
| 46 | * @var int |
| 47 | */ |
| 48 | private $end = -1; |
| 49 | |
| 50 | /** |
| 51 | * Total size of the file in bytes. |
| 52 | * |
| 53 | * @var int |
| 54 | */ |
| 55 | private $size = 0; |
| 56 | |
| 57 | /** |
| 58 | * MIME type of the file being streamed. |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
| 62 | private $type = ''; |
| 63 | |
| 64 | /** |
| 65 | * Constructor. |
| 66 | * |
| 67 | * @param string $file_path Path to the file to stream. |
| 68 | * @param string $file_type MIME type of the file. |
| 69 | * @param int $buffer Number of bytes to read per chunk. |
| 70 | */ |
| 71 | public function __construct( $file_path, $file_type, $buffer = 102400 ) { |
| 72 | $this->path = $file_path; |
| 73 | $this->type = $file_type; |
| 74 | $this->buffer = $buffer; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Start streaming video content. |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | public function start() { |
| 83 | $this->open(); |
| 84 | $this->set_header(); |
| 85 | $this->stream(); |
| 86 | $this->end(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Open stream |
| 91 | */ |
| 92 | private function open() { |
| 93 | |
| 94 | $this->stream = fopen( $this->path, 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- Byte-range video streaming requires a low-level file handle; WP_Filesystem cannot stream ranges. |
| 95 | |
| 96 | if ( ! $this->stream ) { |
| 97 | die( 'Could not open stream for reading' ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Set proper header to serve the video content |
| 103 | */ |
| 104 | private function set_header() { |
| 105 | |
| 106 | $this->start = 0; |
| 107 | $this->size = filesize( $this->path ); |
| 108 | $this->end = $this->size - 1; |
| 109 | |
| 110 | ob_get_clean(); |
| 111 | header( 'Content-Type: ' . $this->type ); |
| 112 | header( 'Cache-Control: max-age=2592000, public' ); |
| 113 | header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 2592000 ) . ' GMT' ); |
| 114 | header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', @filemtime( $this->path ) ) . ' GMT' ); |
| 115 | header( 'Accept-Ranges: 0-' . $this->end ); |
| 116 | |
| 117 | if ( isset( $_SERVER['HTTP_RANGE'] ) ) { |
| 118 | |
| 119 | $c_start = $this->start; |
| 120 | $c_end = $this->end; |
| 121 | |
| 122 | $parts = explode( '=', sanitize_text_field( wp_unslash( $_SERVER['HTTP_RANGE'] ) ), 2 ); |
| 123 | if ( count( $parts ) < 2 ) { |
| 124 | header( 'HTTP/1.1 416 Requested Range Not Satisfiable' ); |
| 125 | header( "Content-Range: bytes $this->start-$this->end/$this->size" ); |
| 126 | exit; |
| 127 | } |
| 128 | $range = $parts[1]; |
| 129 | if ( strpos( $range, ',' ) !== false ) { |
| 130 | header( 'HTTP/1.1 416 Requested Range Not Satisfiable' ); |
| 131 | header( "Content-Range: bytes $this->start-$this->end/$this->size" ); |
| 132 | exit; |
| 133 | } |
| 134 | if ( '-' == $range ) { |
| 135 | $c_start = $this->size - substr( $range, 1 ); |
| 136 | } else { |
| 137 | $range = explode( '-', $range ); |
| 138 | $c_start = $range[0]; |
| 139 | $c_end = ( isset( $range[1] ) && is_numeric( $range[1] ) ) ? $range[1] : $c_end; |
| 140 | } |
| 141 | $c_end = ( $c_end > $this->end ) ? $this->end : $c_end; |
| 142 | if ( $c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size ) { |
| 143 | header( 'HTTP/1.1 416 Requested Range Not Satisfiable' ); |
| 144 | header( "Content-Range: bytes $this->start-$this->end/$this->size" ); |
| 145 | exit; |
| 146 | } |
| 147 | $this->start = $c_start; |
| 148 | $this->end = $c_end; |
| 149 | $length = $this->end - $this->start + 1; |
| 150 | fseek( $this->stream, $this->start ); |
| 151 | header( 'HTTP/1.1 206 Partial Content' ); |
| 152 | header( 'Content-Length: ' . $length ); |
| 153 | header( "Content-Range: bytes $this->start-$this->end/" . $this->size ); |
| 154 | } else { |
| 155 | |
| 156 | header( 'Content-Length: ' . $this->size ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Perform the streaming of calculated range. |
| 162 | */ |
| 163 | private function stream() { |
| 164 | |
| 165 | $i = $this->start; |
| 166 | set_time_limit( 0 ); |
| 167 | |
| 168 | while ( ! feof( $this->stream ) && $i <= $this->end ) { |
| 169 | $bytes_to_read = $this->buffer; |
| 170 | if ( ( $i + $bytes_to_read ) > $this->end ) { |
| 171 | $bytes_to_read = $this->end - $i + 1; |
| 172 | } |
| 173 | $data = fread( $this->stream, $bytes_to_read ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread -- Byte-range video streaming requires direct stream reads. |
| 174 | echo $data; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Raw video byte stream; escaping would corrupt binary output. |
| 175 | flush(); |
| 176 | $i += $bytes_to_read; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Close currently opened stream. |
| 182 | */ |
| 183 | private function end() { |
| 184 | fclose( $this->stream ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Closing the stream handle opened for byte-range streaming. |
| 185 | exit; |
| 186 | } |
| 187 | } |
| 188 |