CakePHP中的HTML5视频搜索


HTML5 video seeking in CakePHP

我的视频流在CakePHP中运行得很好。由于视频对每个用户都是私有的,如果用户经过身份验证,我有一个CakePHP控制器来提供文件。我还注意到,所有发送到服务器的请求在其请求中都有一个Cookie: CAKEPHP=<stuff>头。

问题是,当用户暂停,然后播放视频时,或者当用户沿着视频寻找时,Chrome会发送一个具有特定字节范围的Range请求,供我的服务器发送。请求会立即被取消。需要注意的是,此请求没有Cookie: CAKEPHP=<stuff>标头。

我认为请求被拒绝是因为没有会话cookie。如何强制chrome(可能还有其他浏览器)发送会话cookie?

我的CakePHP版本是2.4.1

编辑

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>

(控制器中的view_media功能为空)

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没有视图)

这些是请求。manage_media.js现在是空白的,我会用它来加载所有媒体文件——这个视频正在工作。

链接

我认为请求实际上是正确的,只是控制台没有正确显示它,因为服务器的响应不正确。请尝试使用类似Charles的调试代理来检查请求。

无效的响应范围

响应总是从字节0开始服务,并且它使用第一个字节位置作为最后一个字节位置,这两者都是因为在正则表达式匹配中使用了错误的索引。索引0包含整个匹配主题,即完整的bytes=xyz字符串,索引1包含第一个捕获,即第一个字节位置。

您要查找的实际捕获值位于索引12中。

$begin=intval($matches[1]);
if(!empty($matches[2])) {
    $end=intval($matches[2]);
}

无效的内容范围

此外,Content-Range标头错误,最后一个字节的位置必须是end - 1,因为第一个字节从0开始。

$last = $end - 1;
header("Content-Range: bytes $begin-$last/$size");

另请参阅http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35

字节内容范围规范的字节范围resp规范的最后一个字节pos值小于其第一个字节的pos值,或者其实例长度值小于或等于其最后一个字符pos值是无效的。无效字节内容范围规范的接收者必须忽略它以及与它一起传输的任何内容。

[…]

字节内容范围规范值的示例,假设实体总共包含1234个字节:

. The first 500 bytes:
bytes 0-499/1234

. The second 500 bytes:
bytes 500-999/1234

. All except for the first 500 bytes:
bytes 500-1233/1234

. The last 500 bytes:
bytes 734-1233/1234

请改用CakePHP

顺便说一句,你为什么不使用CakePHP功能来查询数据库和提供文件?那样你会省去很多麻烦的。

请参阅http://book.cakephp.org/2.0/en/controllers/request-response.html#cakeresponse

一些(未经测试)示例代码:

public function get_media($id, $type)
{
    $media = $this->Media->findById($id, array('Media.owner', 'Media.type'));
    if($media['Media']['owner'] !== $this->Auth->user('id'))
    {
        throw new ForbiddenException();
    }
    $file = null;
    if($media['Media']['type'] == 'png')
    {
        $file = "/srv/Ads/Ad_".$id.".png";
    }
    else if($media['Media']['type'] == 'mp4')
    {
        if($type == "mp4")
        {
            $file = "/srv/Ads/Ad_".$id.".mp4";
        }
        else if($type == "webm")
        {
            $file = "/srv/Ads/Ad_".$id.".webm";
        }
    }
    $this->response->file($file, array('download' => true));
    return $this->response;
}