Advertisement
julong

video stream

Sep 22nd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. video stream
  2.  
  3. <?php
  4. /**
  5. * Description of VideoStream
  6. *
  7. * @author Rana
  8. * @link http://codesamplez.com/programming/php-html5-video-streaming-tutorial
  9. */
  10. class VideoStream
  11. {
  12. private $path = "";
  13. private $stream = "";
  14. private $buffer = 102400;
  15. private $start = -1;
  16. private $end = -1;
  17. private $size = 0;
  18.  
  19. function __construct($filePath)
  20. {
  21. $this->path = $filePath;
  22. }
  23.  
  24. /**
  25. * Open stream
  26. */
  27. private function open()
  28. {
  29. if (!($this->stream = fopen($this->path, 'rb'))) {
  30. die('Could not open stream for reading');
  31. }
  32.  
  33. }
  34.  
  35. /**
  36. * Set proper header to serve the video content
  37. */
  38. private function setHeader()
  39. {
  40. ob_get_clean();
  41. header("Content-Type: video/mp4");
  42. header("Cache-Control: max-age=2592000, public");
  43. header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
  44. header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' );
  45. $this->start = 0;
  46. $this->size = filesize($this->path);
  47. $this->end = $this->size - 1;
  48. header("Accept-Ranges: 0-".$this->end);
  49.  
  50. if (isset($_SERVER['HTTP_RANGE'])) {
  51.  
  52. $c_start = $this->start;
  53. $c_end = $this->end;
  54.  
  55. list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  56. if (strpos($range, ',') !== false) {
  57. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  58. header("Content-Range: bytes $this->start-$this->end/$this->size");
  59. exit;
  60. }
  61. if ($range == '-') {
  62. $c_start = $this->size - substr($range, 1);
  63. }else{
  64. $range = explode('-', $range);
  65. $c_start = $range[0];
  66.  
  67. $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
  68. }
  69. $c_end = ($c_end > $this->end) ? $this->end : $c_end;
  70. if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
  71. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  72. header("Content-Range: bytes $this->start-$this->end/$this->size");
  73. exit;
  74. }
  75. $this->start = $c_start;
  76. $this->end = $c_end;
  77. $length = $this->end - $this->start + 1;
  78. fseek($this->stream, $this->start);
  79. header('HTTP/1.1 206 Partial Content');
  80. header("Content-Length: ".$length);
  81. header("Content-Range: bytes $this->start-$this->end/".$this->size);
  82. }
  83. else
  84. {
  85. header("Content-Length: ".$this->size);
  86. }
  87.  
  88. }
  89.  
  90. /**
  91. * close curretly opened stream
  92. */
  93. private function end()
  94. {
  95. fclose($this->stream);
  96. exit;
  97. }
  98.  
  99. /**
  100. * perform the streaming of calculated range
  101. */
  102. private function stream()
  103. {
  104. $i = $this->start;
  105. set_time_limit(0);
  106. while(!feof($this->stream) && $i <= $this->end) {
  107. $bytesToRead = $this->buffer;
  108. if(($i+$bytesToRead) > $this->end) {
  109. $bytesToRead = $this->end - $i + 1;
  110. }
  111. $data = fread($this->stream, $bytesToRead);
  112. echo $data;
  113. flush();
  114. $i += $bytesToRead;
  115. }
  116. }
  117.  
  118. /**
  119. * Start streaming video content
  120. */
  121. function start()
  122. {
  123. $this->open();
  124. $this->setHeader();
  125. $this->stream();
  126. $this->end();
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement