I have video streaming working just fine in CakePHP. Since the videos are private to each user, I have a CakePHP controller serve up the files if the users are authenticated. I also notice that all requests sent to the server have a Cookie: CAKEPHP=<stuff>
header in their request.
The problem is that when a user pauses, then plays the video or when the user seeks along the video, Chrome sends a Range request with a certain byte range for my server to deliver. The request gets cancelled immediately. It should be noted that this request has no Cookie: CAKEPHP=<stuff>
header.
I believe the request is denied because there is no session cookie. How can I force chrome (and possibly other browsers) to send a session cookie?
My CakePHP Version is 2.4.1
EDIT:
view_media.ctp
<?php echo $this->Html->script(array('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', 'mediaelement-and-player'), array('inline' => false));
echo $this->Html->css(array('mediaelementplayer.min'), array('inline' => false));
?>
<video width="320" height="240" controls preload="none">
<?php
if(isset($_SERVER['HTTP_USER_AGENT'])){
$agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($agent,'Chrome') !== false) {
echo '<source src="/pages/get_media/264/webm" type="video/webm">';
}
if (strpos($agent,'Mozilla') !== false) {
echo '<source src="/pages/get_media/264/mp4" type="video/mp4">';
}
}
?>
Your browser does not support HTML5.
</video>
(The view_media function in the Controller is blank)
Relevant section of PagesController
public function get_media($id, $type){
$dbh = new PDO('mysql:host='.$dbhost;dbname=$dbname, $username, $password);
//get media info
$sth = $dbh->prepare("SELECT `owner`, `type` FROM media WHERE id=:id");
$sth->bindParam(':id', $id);
$sth->execute();
$row = $sth->fetch();
if($row['owner'] == $this->Auth->user('id')){
if($row['type'] == 'png'){
header("Content-type: image/png");
echo file_get_contents("/srv/Ads/Ad_".$id.".png");
}else if($row['type'] == 'mp4'){
if($type == "mp4"){
header("Content-type: video/mp4");
$filename = "/srv/Ads/Ad_".$id.".mp4";
}else if($type == "webm"){
header("Content-type: video/webm");
$filename = "/srv/Ads/Ad_".$id.".webm";
}else{
die;
}
$this->send_video($filename);
}
die;
}
}
//thanks to http://stackoverflow.com/questions/16732419/mp4-from-php-not-playing-in-html5-video-tag
private function send_video($path){
if (file_exists($path)){
$size=filesize($path);
$fm=@fopen($path,'rb');
if(!$fm) {
// You can also redirect here
header ("HTTP/1.0 404 Not Found");
die;
}
$begin=0;
$end=$size;
if(isset($_SERVER['HTTP_RANGE'])) {
if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i',
$_SERVER['HTTP_RANGE'],$matches)){
$begin=intval($matches[0]);
if(!empty($matches[1])) {
$end=intval($matches[1]);
}
}
}
if($begin>0||$end<$size)
header('HTTP/1.0 206 Partial Content');
else
header('HTTP/1.0 200 OK');
header('Accept-Ranges: bytes');
header('Content-Length:'.($end-$begin));
header("Content-Disposition: inline;");
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');
$cur=$begin;
fseek($fm,$begin,0);
while(!feof($fm)&&$cur<$end&&(connection_status()==0)){
echo fread($fm,min(1024*16,$end-$cur));
$cur+=1024*16;
usleep(1000);
}
die;
}
}
(get_media has no view)
These are the requests. manage_media.js is blank for now, I will use it to load all media files one this video thing is working.